RGB Values Not Consistent Before and After Saving Image

edited August 2015 in Questions about Code

I've been trying to develop a steganography program that can send and receive messages through the least significant bits of an image, however, Processing seems to sometimes change an RGB value by one or two units for absolutely no reason.

I threw together the following program to demonstrate what I mean.

PImage originalImage, newImage;
String oldName = "input.jpeg", newName = "newImage.jpeg";

void setup() {
  originalImage = loadImage(oldName);

  printPixels(originalImage, oldName);
  makeNewImage(newName);
  printPixels(loadImage(newName), newName);
}

private void printPixels(PImage _image, String _name) {
  println(" === Pixels: " + _name + " === ");

  // Print only 15 pixels, just to keep things simple.
  for (int i = 0; i < 15; i++) {
    println(red(_image.pixels[i]) + " : " + green(_image.pixels[i]) + " : " + blue(_image.pixels[i]));
  }
  println();
}

private void makeNewImage(String _name) {
  // Create new image
  newImage = createImage(originalImage.width, originalImage.height, RGB); 
  // Copy the original image over
  newImage = originalImage.get();
  // Save new image
  newImage.save(_name);
}

All this program does is receive an image "input.jpeg", and save an identical copy of it called "newImage.jpeg". Easy enough, right? Well, here is what it outputs when it reads the pixel RGB values:

 === Pixels: input.jpeg === 
226.0 : 234.0 : 237.0
226.0 : 234.0 : 237.0
226.0 : 234.0 : 237.0
225.0 : 233.0 : 236.0
225.0 : 233.0 : 236.0
225.0 : 233.0 : 236.0
225.0 : 233.0 : 236.0
225.0 : 233.0 : 236.0
225.0 : 233.0 : 236.0
223.0 : 231.0 : 234.0
222.0 : 230.0 : 233.0
220.0 : 228.0 : 231.0
220.0 : 228.0 : 231.0
221.0 : 229.0 : 232.0
222.0 : 230.0 : 233.0

 === Pixels: newImage.jpeg === 
226.0 : 234.0 : 237.0
226.0 : 234.0 : 237.0
226.0 : 234.0 : 237.0
225.0 : 233.0 : 236.0
225.0 : 233.0 : 236.0
225.0 : 233.0 : 236.0
225.0 : 233.0 : 236.0
225.0 : 233.0 : 236.0
224.0 : 232.0 : 235.0
223.0 : 231.0 : 234.0
221.0 : 229.0 : 232.0
220.0 : 228.0 : 231.0
219.0 : 227.0 : 230.0
220.0 : 228.0 : 231.0
222.0 : 230.0 : 233.0

Look at the second last pixel data. Can anyone explain why the RGB values sometimes differ? Is there a better way to import and save images? Should I use a different picture format?

Thanks in advance, - Sono

Answers

Sign In or Register to comment.