Results 1 to 9 of 9
  1. #1
    AndreB's Avatar
    AndreB is offline Senior Member
    Join Date
    Dec 2009
    Location
    Stuttgart, Germany
    Posts
    114
    Rep Power
    0

    Default BufferedImage - greyscale - color information

    Hi,

    I read a grey-scale image (sample_image.bmp) and i want to output a colour information.

    Java Code:
            public static void main(String[] args) throws Exception {
    		BufferedImage bi = ImageIO.read(new File("sample_image.bmp"));
    		System.out.println(bi.getRGB(0, 0));
    		System.out.println(Integer.toHexString(bi.getRGB(0, 0)));
    	}
    But now I'm confused. Why isn't the rgb value represents a black colour?
    I understand, that the type of the image is TYPE_BYTE_GREY but how can i get the "black" colour information in hex? or how to acquire the value of the grey tone?

    Suggestions?

    Thanks!
    Attached Thumbnails Attached Thumbnails BufferedImage - greyscale - color information-sample_image.bmp  
    "There is no foolproof thing; fools are too smart."
    "Why can't you solve my Problem ?"

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default

    Did you check the type of your BufferedImage? .bmp format isn't necessarily black and white (although some pictures may look like that).

    kind regards,

    Jos

  3. #3
    AndreB's Avatar
    AndreB is offline Senior Member
    Join Date
    Dec 2009
    Location
    Stuttgart, Germany
    Posts
    114
    Rep Power
    0

    Default

    The type of the input image is TYPE_BYTE_GREY.
    But why it isnt black? Gimp shows #040404 as a color value (of the pixel [0,0]).
    But the output of the HexString is FF222222. That does not look right. The first FF is the A value and thats allright. But the 222222 does not match 040404.
    "There is no foolproof thing; fools are too smart."
    "Why can't you solve my Problem ?"

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by AndreB View Post
    The type of the input image is TYPE_BYTE_GREY.
    But why it isnt black? Gimp shows #040404 as a color value (of the pixel [0,0]).
    But the output of the HexString is FF222222. That does not look right. The first FF is the A value and thats allright. But the 222222 does not match 040404.
    Hm, your code snippet does make sense and I've seen gimp play funny tricks before and 0x222222 is a very dark gray value ... and I played a bit with your little duck image and that value does make sense: that background value is indeed 0x222222 with full opacity.

    kind regards,

    Jos

  5. #5
    AndreB's Avatar
    AndreB is offline Senior Member
    Join Date
    Dec 2009
    Location
    Stuttgart, Germany
    Posts
    114
    Rep Power
    0

    Default

    so gimp isn't right here
    *trying irfanview*

    thanks!
    "There is no foolproof thing; fools are too smart."
    "Why can't you solve my Problem ?"

  6. #6
    AndreB's Avatar
    AndreB is offline Senior Member
    Join Date
    Dec 2009
    Location
    Stuttgart, Germany
    Posts
    114
    Rep Power
    0

    Default

    hm...

    irfanview also says, that RGB: 4,4,4 is right :-(
    if i try to copy and write the new image back it is brighter... somehow i don't understand whats going wrong

    Java Code:
    	public static void main(String[] args) throws Exception {
    		BufferedImage bi = ImageIO.read(new File("sample_image.bmp"));
    		System.out.println(bi.getRGB(0, 0));
    		System.out.println(Integer.toHexString(bi.getRGB(0, 0)));
    		
    		BufferedImage biCopy = new BufferedImage(bi.getWidth(), bi.getHeight(), BufferedImage.TYPE_INT_RGB);
    		for (int x = 0; x < bi.getWidth(); x++) {
    			for (int y = 0; y < bi.getHeight(); y++) {
    				biCopy.setRGB(x, y, bi.getRGB(x, y));
    			}
    		}
    		ImageIO.write(biCopy, "bmp", new File("sample_out.bmp"));
    	}
    "There is no foolproof thing; fools are too smart."
    "Why can't you solve my Problem ?"

  7. #7
    AndreB's Avatar
    AndreB is offline Senior Member
    Join Date
    Dec 2009
    Location
    Stuttgart, Germany
    Posts
    114
    Rep Power
    0

  8. #8
    AndreB's Avatar
    AndreB is offline Senior Member
    Join Date
    Dec 2009
    Location
    Stuttgart, Germany
    Posts
    114
    Rep Power
    0

    Default

    i hate doing evil hacks :-(

    but is there any other options besides THIS:

    Java Code:
          public static void main(String[] args) throws Exception {
    		BufferedImage bi = ImageIO.read(new File("sample_image.bmp"));
    		System.out.println(bi.getRGB(0, 0));
    		System.out.println(Integer.toHexString(bi.getRGB(0, 0)));		
    		BufferedImage biCopy = new BufferedImage(bi.getWidth(), bi.getHeight(), BufferedImage.TYPE_INT_RGB);
    
    		for (int x = 0; x < bi.getWidth(); x++) {
    			for (int y = 0; y < bi.getHeight(); y++) {				
    				int gray = bi.getRaster().getPixel(x, y, (int[]) null)[0];
    				int rgbVal = (gray << 16) + (gray << 8) + (gray); 
    				biCopy.setRGB(x, y, rgbVal);
    			}
    		}
    		ImageIO.write(biCopy, "bmp", new File("sample_out.bmp"));
    	}
    "There is no foolproof thing; fools are too smart."
    "Why can't you solve my Problem ?"

  9. #9
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by AndreB View Post
    i hate doing evil hacks :-(

    but is there any other options besides THIS:
    There's another workaround in the bug report (see your own link) but then you end up with a LUT image, I don't know it you want that. I never noticed this (old) bug. I opened your image in a MS image manipulation program and indeed it shows a bit darker than it shows up in my Java code; strange ...

    kind regards,

    Jos

Similar Threads

  1. BufferedImage rgb
    By Bill87 in forum New To Java
    Replies: 2
    Last Post: 02-25-2010, 06:31 PM
  2. [COLOR="Navy"]execute .bat file in mysql [/COLOR]
    By msankar.ravi in forum Networking
    Replies: 0
    Last Post: 02-24-2010, 04:27 AM
  3. Using BufferedImage
    By timkd127 in forum New To Java
    Replies: 5
    Last Post: 12-19-2009, 09:17 PM
  4. BufferedImage through FTP
    By dudejonne in forum New To Java
    Replies: 7
    Last Post: 11-05-2009, 05:36 PM
  5. BufferedImage to Byte
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-22-2008, 08:17 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •