This code takes an input image and produces an output image whose size is two times larger. The first four lines in the inner loop write four copies of the original of the same size, the last four lines should overwrite small images with one copy of the input image twice as large as the original.
The code compiles and runs without errors in Java 8 Update 45 on Windows 8. The resulting image is not, as expected, one large copy of the input. The lower half is displayed as expected, but the upper half consists of two copies of the original, recorded by the first two lines inside the loop. Commenting on these two lines leads to the expected result as a result, so it seems that the first two lines are executed last and not the first, despite the fact that they appear first in the source code.
Is this a compiler error, a race condition at runtime, or brain percussion on my behalf?
If necessary, I will give examples somewhere.
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
class HelloWorldApp {
public static void main(String[] orgs) throws IOException {
BufferedImage pic = ImageIO.read(new File("cat.jpg"));
int w=pic.getWidth(),h=pic.getHeight();
BufferedImage out = new BufferedImage(w+w,h+h,pic.getType());
for (int y=0;y<h;y++) {
for (int x=0;x<w;x++) {
int pixel = pic.getRGB(x,y);
out.setRGB(x ,y ,pixel);
out.setRGB(x+w ,y ,pixel);
out.setRGB(x ,y+h ,pixel);
out.setRGB(x+w ,y+h ,pixel);
out.setRGB(x+x ,y+y ,pixel);
out.setRGB(x+x+1,y+y ,pixel);
out.setRGB(x+x ,y+y+1,pixel);
out.setRGB(x+x+1,y+y+1,pixel);
}
}
ImageIO.write(out, "bmp", new File("./cat.bmp"));
}
}