0

I am trying to retrieve an jpg image as a BufferedImage then decompose it into a 3D array [RED][GREEN][BLUE] and then turn it back into a BufferedImage and store it under a different file-name. All looks fine to me BUT, when I am trying to reload the 3D array using the new file created I get different values for RGB although the new image looks fine to the naked eye. I did the following.

BufferedImage bi = ImageIO.read(new File("old.jpg"));
int[][][] one = getArray(bi);
save("kite.jpg", one);

BufferedImage bi2 = ImageIO.read(new File("new.jpg"));
int[][][] two = getArray(bi2);

private void save(String destination, int[][][] in) {
    try {
    BufferedImage out = new BufferedImage(in.length, in[0].length, BufferedImage.TYPE_3BYTE_BGR);

        for (int x=0; x<out.getWidth(); x++) {
            for (int y = 0; y < out.getHeight(); y++) {
               out.setRGB(x, y, new Color(in[x][y][0], in[x][y][1], in[x][y][2]).getRGB());
            }
        }

        File f = new File("name");
        ImageIO.write(out, "JPEG", f);
    } catch (IOException e) {
        System.out.println(e.getMessage());
    }

}

so in the example above the values that arrays one and two are holding are different.

I am guessing there is something to do with the different types of retrieving and restoring images? I am trying to figure out what is going on all day but with no luck. Any help appreciated.

Rakim
  • 1,087
  • 9
  • 21
  • 40

1 Answers1

0

Pretty simple:

JPEG is a commonly used method of lossy compression for digital images

(from wikipedia). Each time the image is compressed, the image is altered to reduce file-size. In fact repeating the steps of decompression and compression for several hundred times alters the image to the point where the entire image turns into a plain gray area in most cases. There are a few compressions that work lossless, but most operation-modes will alter the image.