Inconsistent RGB Values from BufferedImage

I

itisben

Hi,
I am experimenting with reading color values out of image files
(jpg, gif, etc.), and have been using ImageReader to do so. What I am
trying to do is make changes in RGB values for the pixels of an image
translate to different actions --> consider a 2x2 pixel image, if the
pixel in coordinate (0,0) is black, then a popup box would launch that
would say - "You submitted an image with a black pixel in the top left
corner" (just an example - not really what I'm trying to do overall).

The problem I'm running into though, is that there doesn't seem to
be any RGB value consistency with jpgs (gifs seem to be okay, haven't
really tried any other types yet). For instance, on the first run, if
pixel at (0,0) is black and pixel at (1,0) (to the right of it) is
white, then when I try to grab the RGB value for (0,0) it returns an
RGB value of -15329770. Second run, I edit the image file (with MS
paint, this may be my whole problem right here) by changing pixel at
(1,0) from white to black, and when grabbing the RGB for (0,0) I get an
RGB value of -16579837.

Here is the code I'm using to test this (should compile if you toss
it in a main) -

// open the sample file (2x2 pixel jpg)
File f = new File("C:\\Test\\test.jpg");
ImageInputStream iis = ImageIO.createImageInputStream(f);
Iterator readers = ImageIO.getImageReaders(iis);
ImageReader reader = (ImageReader) readers.next();
reader.setInput(iis, true);
BufferedImage image = (BufferedImage) reader.read(0);

// store the overall RGB of the top left pixel since I have to use
it so much
int overallRGB = image.getRGB(0,0);
Color firstPixel = new Color(overallRGB);

// show me the RGB and A
System.out.println("RGB Value Overall " + overallRGB);
System.out.println("Red " + firstPixel.getRed());
System.out.println("Green " + firstPixel.getGreen());
System.out.println("Blue " + firstPixel.getBlue());
System.out.println("Alpha " + firstPixel.getAlpha());

My question is - what can I do about this RGB value inconsistency?
It seems to be based on bordering pixel colors. Is there some sort of
different ColorModel needed for the BufferedImage? Is there some sort
of different SampleModel needed for the Raster of the BufferedImage?

I guess a better question is - is this even Java doing this? I have
a sneaking suspicion that this simply may be how the jpg gets stored
after I save it in paint (I've messed with fireworks as well). In that
case, is there a way to save a jpg in such a way that it doesn't scale
the colors of the pixels due to surrounding pixels (I'm thinking black
becomes less black due to the fact that there is white near it, for
blending purposes), or is this inherent to the way jpgs store image
data? If it is inherent to the way a jpg is stored (that is, a jpg will
always darken or lighten a color based on surrounding pixels), is there
a relatively easy way to get around this in Java by telling the image
to conform to a certain color scheme?

Thanks,
Ben Brewer
 
M

Matt Humphrey

Hi,
I am experimenting with reading color values out of image files
(jpg, gif, etc.), and have been using ImageReader to do so. What I am
trying to do is make changes in RGB values for the pixels of an image
translate to different actions --> consider a 2x2 pixel image, if the
pixel in coordinate (0,0) is black, then a popup box would launch that
would say - "You submitted an image with a black pixel in the top left
corner" (just an example - not really what I'm trying to do overall).

The problem I'm running into though, is that there doesn't seem to
be any RGB value consistency with jpgs (gifs seem to be okay, haven't
really tried any other types yet). For instance, on the first run, if
pixel at (0,0) is black and pixel at (1,0) (to the right of it) is
white, then when I try to grab the RGB value for (0,0) it returns an
RGB value of -15329770. Second run, I edit the image file (with MS
paint, this may be my whole problem right here) by changing pixel at
(1,0) from white to black, and when grabbing the RGB for (0,0) I get an
RGB value of -16579837.

<snip code>

JPEG uses lossy compression and changes colors, especially at borders. See
if you can turn compression off and see what you get. You may want to use
something other than Paint.

Matt Humphrey (e-mail address removed) http://www.iviz.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,059
Latest member
cryptoseoagencies

Latest Threads

Top