code: private static Surface getSheetSize(Collection<ImageBuffer> tiles, int horizontalTiles)
      {
          final int tilesNumber = tiles.size();
          final int horizontals;
          final int verticals;
          if (horizontalTiles > 0)
          {
              horizontals = horizontalTiles;
              verticals = (int) Math.ceil(tilesNumber / (double) horizontalTiles);
          }
          else
          {
              horizontals = (int) Math.ceil(Math.sqrt(tilesNumber));
              verticals = horizontals;
          }
          return new Surface()
          {
              @Override
              public int getWidth()
              {
                  return Math.max(1, horizontals);
              }
  
              @Override
              public int getHeight()
              {
                  return Math.max(1, verticals);
              }
          };
      }
docs: Get the sheet size depending of the number of horizontal tiles and total number of tiles.
  
  @param tiles The tiles list.
  @param horizontalTiles The horizontal tiles.
  @return The horizontal and vertical tiles.