Program: 11141
Project: 11141
Buggy source:
public static void main(String[] args) throws Exception {
  File input=new File("String_Node_Str");
  File output=new File("String_Node_Str");
  Dataset dataset=GDALUtil.open(input);
  Insets insets=new Insets(0,0,0,0);
  int tilesize=512;
  String format="String_Node_Str";
  int width=dataset.GetRasterXSize() - insets.left - insets.right;
  int height=dataset.GetRasterYSize() - insets.top - insets.bottom;
  int levels=levelCount(width,height,tilesize);
  System.out.println("String_Node_Str" + levels);
  int printWidth=width, printHeight=height;
  for (int level=levels - 1; level >= 0; level--) {
    System.out.println("String_Node_Str" + level + "String_Node_Str"+ printWidth+ "String_Node_Str"+ printHeight);
    printWidth=(printWidth + 1) / 2;
    printHeight=(printHeight + 1) / 2;
  }
  File levelDir=new File(output,String.valueOf(levels - 1));
  int xStrips=Math.max(1,tilesize / width);
  int yStrips=Math.max(1,tilesize / height);
  int rows=(height - 1) / (tilesize * xStrips) + 1;
  int cols=(width - 1) / (tilesize * yStrips) + 1;
  for (int y=0, row=0; y < height; y+=tilesize * xStrips, row++) {
    String rowPadded=Util.paddedInt(row,4);
    File rowDir=new File(levelDir,rowPadded);
    rowDir.mkdirs();
    for (int x=0, col=0; x < width; x+=tilesize * yStrips, col++) {
      int w=Math.min(tilesize * yStrips / xStrips,width - x);
      int h=Math.min(tilesize * xStrips / yStrips,height - y);
      Rectangle src=new Rectangle(x + insets.left,y + insets.top,w,h);
      GDALTileParameters parameters=new GDALTileParameters(dataset,src.getSize(),src);
      GDALTile tile=new GDALTile(parameters);
      BufferedImage image=tile.getAsImage();
      File imageFile=tileFile(levelDir,row,col,format);
      ImageIO.write(image,format,imageFile);
    }
  }
  for (int level=levels - 2; level >= 0; level--) {
    int lastRows=rows;
    int lastCols=cols;
    if (cols == 1) {
      xStrips<<=1;
      rows=(rows + 1) / 2;
    }
    if (rows == 1) {
      yStrips<<=1;
      cols=(cols + 1) / 2;
    }
    rows=(rows + 1) / 2;
    cols=(cols + 1) / 2;
    System.out.println("String_Node_Str" + level);
    System.out.println("String_Node_Str" + lastRows + "String_Node_Str"+ lastCols);
    System.out.println("String_Node_Str" + rows + "String_Node_Str"+ cols);
    System.out.println("String_Node_Str" + xStrips + "String_Node_Str"+ yStrips);
    File lastLevelDir=levelDir;
    levelDir=new File(output,String.valueOf(level));
    levelDir.mkdirs();
    int rowMultiplier=lastRows == 1 ? 0 : 1;
    int rowDivisor=lastCols == 1 ? 1 : 2;
    int colMultiplier=lastCols == 1 ? 0 : 1;
    int colDelta=lastRows == 1 ? 2 : 0;
    for (int row=0; row < rows; row++) {
      String rowPadded=Util.paddedInt(row,4);
      File rowDir=new File(levelDir,rowPadded);
      rowDir.mkdirs();
      int firstRow=row * 4;
      int r0=rowMultiplier * (firstRow + 0) / rowDivisor;
      int r1=rowMultiplier * (firstRow + 1) / rowDivisor;
      int r2=rowMultiplier * (firstRow + 2) / rowDivisor;
      int r3=rowMultiplier * (firstRow + 3) / rowDivisor;
      for (int col=0; col < cols; col++) {
        int firstCol=col * (lastRows == 1 ? 4 : 2);
        int c0=colMultiplier * (firstCol);
        int c1=colMultiplier * (firstCol + 1);
        int c2=colMultiplier * (firstCol + colDelta);
        int c3=colMultiplier * (firstCol + colDelta + 1);
        File src0=tileFile(lastLevelDir,r0,c0,format);
        File src1=tileFile(lastLevelDir,r1,c1,format);
        File src2=tileFile(lastLevelDir,r2,c2,format);
        File src3=tileFile(lastLevelDir,r3,c3,format);
        File imageFile=tileFile(levelDir,row,col,format);
        BufferedImage img0=src0.exists() ? ImageIO.read(src0) : null;
        BufferedImage img1=src1.exists() ? ImageIO.read(src1) : null;
        BufferedImage img2=src2.exists() ? ImageIO.read(src2) : null;
        BufferedImage img3=src3.exists() ? ImageIO.read(src3) : null;
        int w0=img0 == null ? 0 : (img0.getWidth() + 1) / 2;
        int w1=img1 == null ? 0 : (img1.getWidth() + 1) / 2;
        int w2=img2 == null ? 0 : (img2.getWidth() + 1) / 2;
        int w3=img3 == null ? 0 : (img3.getWidth() + 1) / 2;
        int h0=img0 == null ? 0 : (img0.getHeight() + 1) / 2;
        int h1=img1 == null ? 0 : (img1.getHeight() + 1) / 2;
        int h2=img2 == null ? 0 : (img2.getHeight() + 1) / 2;
        int h3=img3 == null ? 0 : (img3.getHeight() + 1) / 2;
        int w=w0 + (lastCols == 1 ? 0 : w1) + (lastRows == 1 ? w2 + w3 : 0);
        int h=h0 + (lastRows == 1 ? 0 : h2) + (lastCols == 1 ? h1 + h3 : 0);
        BufferedImage image=new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB);
        Graphics2D g=image.createGraphics();
        g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        int x=0, y=0;
        if (img0 != null) {
          g.drawImage(img0,x,y,w0,h0,null);
        }
        if (img1 != null) {
          x+=lastCols == 1 ? 0 : w0;
          y+=lastCols == 1 ? h0 : 0;
          g.drawImage(img1,x,y,w1,h1,null);
        }
        if (img2 != null) {
          x+=lastCols == 1 ? 0 : lastRows == 1 ? w1 : -w0;
          y+=lastCols == 1 ? h1 : lastRows == 1 ? 0 : h0;
          g.drawImage(img2,x,y,w2,h2,null);
        }
        if (img3 != null) {
          x+=lastCols == 1 ? 0 : w2;
          y+=lastCols == 1 ? h2 : 0;
          g.drawImage(img3,x,y,w3,h3,null);
        }
        g.dispose();
        ImageIO.write(image,format,imageFile);
      }
    }
  }
}

Fixed version (ground truth):
public static void main(String[] args) throws Exception {
  File input=new File("String_Node_Str");
  File output=new File("String_Node_Str");
  Dataset dataset=GDALUtil.open(input);
  Insets insets=new Insets(408,833,14385 - 14376,38159 - 37443);
  int tilesize=512;
  String format="String_Node_Str";
  int width=dataset.GetRasterXSize() - insets.left - insets.right;
  int height=dataset.GetRasterYSize() - insets.top - insets.bottom;
  int levels=levelCount(width,height,tilesize);
  System.out.println("String_Node_Str" + levels);
  int printWidth=width, printHeight=height;
  for (int level=levels - 1; level >= 0; level--) {
    System.out.println("String_Node_Str" + level + "String_Node_Str"+ printWidth+ "String_Node_Str"+ printHeight);
    printWidth=(printWidth + 1) / 2;
    printHeight=(printHeight + 1) / 2;
  }
  File levelDir=new File(output,String.valueOf(levels - 1));
  int xStrips=Math.max(1,tilesize / width);
  int yStrips=Math.max(1,tilesize / height);
  int rows=(height - 1) / (tilesize * xStrips) + 1;
  int cols=(width - 1) / (tilesize * yStrips) + 1;
  for (int y=0, row=0; y < height; y+=tilesize * xStrips, row++) {
    String rowPadded=Util.paddedInt(row,4);
    File rowDir=new File(levelDir,rowPadded);
    rowDir.mkdirs();
    for (int x=0, col=0; x < width; x+=tilesize * yStrips, col++) {
      File imageFile=tileFile(levelDir,row,col,format);
      if (imageFile.exists()) {
        continue;
      }
      int w=Math.min(tilesize * yStrips / xStrips,width - x);
      int h=Math.min(tilesize * xStrips / yStrips,height - y);
      Rectangle src=new Rectangle(x + insets.left,y + insets.top,w,h);
      GDALTileParameters parameters=new GDALTileParameters(dataset,src.getSize(),src);
      GDALTile tile=new GDALTile(parameters);
      BufferedImage image=tile.getAsImage();
      ImageIO.write(image,format,imageFile);
    }
  }
  for (int level=levels - 2; level >= 0; level--) {
    int lastRows=rows;
    int lastCols=cols;
    if (cols == 1) {
      xStrips<<=1;
      rows=(rows + 1) / 2;
    }
    if (rows == 1) {
      yStrips<<=1;
      cols=(cols + 1) / 2;
    }
    rows=(rows + 1) / 2;
    cols=(cols + 1) / 2;
    System.out.println("String_Node_Str" + level);
    System.out.println("String_Node_Str" + lastRows + "String_Node_Str"+ lastCols);
    System.out.println("String_Node_Str" + rows + "String_Node_Str"+ cols);
    System.out.println("String_Node_Str" + xStrips + "String_Node_Str"+ yStrips);
    File lastLevelDir=levelDir;
    levelDir=new File(output,String.valueOf(level));
    levelDir.mkdirs();
    int rowMultiplier=lastRows == 1 ? 0 : 1;
    int rowDivisor=lastCols == 1 ? 1 : 2;
    int colMultiplier=lastCols == 1 ? 0 : 1;
    int colDelta=lastRows == 1 ? 2 : 0;
    for (int row=0; row < rows; row++) {
      String rowPadded=Util.paddedInt(row,4);
      File rowDir=new File(levelDir,rowPadded);
      rowDir.mkdirs();
      int firstRow=row * 4;
      int r0=rowMultiplier * (firstRow + 0) / rowDivisor;
      int r1=rowMultiplier * (firstRow + 1) / rowDivisor;
      int r2=rowMultiplier * (firstRow + 2) / rowDivisor;
      int r3=rowMultiplier * (firstRow + 3) / rowDivisor;
      for (int col=0; col < cols; col++) {
        File imageFile=tileFile(levelDir,row,col,format);
        if (imageFile.exists()) {
          continue;
        }
        int firstCol=col * (lastRows == 1 ? 4 : 2);
        int c0=colMultiplier * (firstCol);
        int c1=colMultiplier * (firstCol + 1);
        int c2=colMultiplier * (firstCol + colDelta);
        int c3=colMultiplier * (firstCol + colDelta + 1);
        File src0=tileFile(lastLevelDir,r0,c0,format);
        File src1=tileFile(lastLevelDir,r1,c1,format);
        File src2=tileFile(lastLevelDir,r2,c2,format);
        File src3=tileFile(lastLevelDir,r3,c3,format);
        BufferedImage img0=src0.exists() ? ImageIO.read(src0) : null;
        BufferedImage img1=src1.exists() ? ImageIO.read(src1) : null;
        BufferedImage img2=src2.exists() ? ImageIO.read(src2) : null;
        BufferedImage img3=src3.exists() ? ImageIO.read(src3) : null;
        int w0=img0 == null ? 0 : (img0.getWidth() + 1) / 2;
        int w1=img1 == null ? 0 : (img1.getWidth() + 1) / 2;
        int w2=img2 == null ? 0 : (img2.getWidth() + 1) / 2;
        int w3=img3 == null ? 0 : (img3.getWidth() + 1) / 2;
        int h0=img0 == null ? 0 : (img0.getHeight() + 1) / 2;
        int h1=img1 == null ? 0 : (img1.getHeight() + 1) / 2;
        int h2=img2 == null ? 0 : (img2.getHeight() + 1) / 2;
        int h3=img3 == null ? 0 : (img3.getHeight() + 1) / 2;
        int w=w0 + (lastCols == 1 ? 0 : w1) + (lastRows == 1 ? w2 + w3 : 0);
        int h=h0 + (lastRows == 1 ? 0 : h2) + (lastCols == 1 ? h1 + h3 : 0);
        BufferedImage image=new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB);
        Graphics2D g=image.createGraphics();
        g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        int x=0;
        int y=0;
        if (img0 != null) {
          g.drawImage(img0,x,y,w0,h0,null);
        }
        x+=lastCols == 1 ? 0 : w0;
        y+=lastCols == 1 ? h0 : 0;
        if (img1 != null) {
          g.drawImage(img1,x,y,w1,h1,null);
        }
        x+=lastCols == 1 ? 0 : lastRows == 1 ? w1 : -w0;
        y+=lastCols == 1 ? h1 : lastRows == 1 ? 0 : h0;
        if (img2 != null) {
          g.drawImage(img2,x,y,w2,h2,null);
        }
        x+=lastCols == 1 ? 0 : w2;
        y+=lastCols == 1 ? h2 : 0;
        if (img3 != null) {
          g.drawImage(img3,x,y,w3,h3,null);
        }
        g.dispose();
        ImageIO.write(image,format,imageFile);
      }
    }
  }
}