Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2006

Tip: Looking for answers? Try searching our database.

Inconsistent RGB Values from BufferedImage

Thread view: 
itisben@gmail.com - 04 Oct 2006 23:49 GMT
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
Matt Humphrey - 05 Oct 2006 01:06 GMT
> Hi,
>   I am experimenting with reading color values out of image files
[quoted text clipped - 14 lines]
> (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 matth@ivizNOSPAM.com http://www.iviz.com/


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.