Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

changing gray pixels....

 
Michael Mas
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello there,

I have a buffered Image of TYPE_BYTE_GRAY and I would like to get the pixel intensity values(0-255) , change them and create a new bufferedImage.

Any ideas?

I spent plenty of time on this and I would be really greatfull to get a reply :-)
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not too familiar with BufferedImage, but from looking at the javadocs it seems you can construct a BI, and then use the getRGB and setRGB methods to copy pixels values from one image to the other. There's also a getRGB method that returns the value of an individual pixel.
 
Michael Mas
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes I know about these get/setRGB methods but since my image is gray it shouldn't give another type of colour-not an RGB?

And mainly, I have the pixel intensities in int values ... even if it returns RGB ...do you have any ideas on how can I change the gray int 0-255 values to RGB ..?

Thank you very much
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gray color means that red value = green value = blue value. So the value you get from getRGB most likely has the same 8 bits (which represent the gray value) in bits 0-7 (for blue), 8-15 (for green) and 16-23 (for red), and possibly an alpha mask in bits 24-31. Try masking the rgb value by Ox255, i.e. "(rgb & Ox255)", and check whether that is the gray value you want.
 
Michael Mas
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem was finally solved by turning everything into RGB format.

The final result is ok but I think that I waste system resources by using RGB color format instead of gray pixel intensity - an int from 0-255 ...

I would really like to listen to any ideas...
 
Michael Mas
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean by masking ... rgb & 0x255 ?

do i need to set Alpha ?

Thanks Ulf for your help.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's the 25¢ tour of BufferedImage:
  • A BufferedImage is a ColorModel plus a WriteableRaster.
  • The job of the ColorModel is to translate between pixel values and color components. A ColorModel has a ColorSpace.
  • A ColorSpace defines a space of colors, for example the space of gray colors.
  • A WriteableRaster is a SampleModel plus a DataBuffer. Its job is to hold the pixels values of the image.
  • The SampleModel is usually behind the scenes (notice it doesn't get mentioned in my demo). Its job is to set and get pixels from the DataBuffer, since the DataBuffer doesn't know the dimensions of the image, or how many samples there are per pixel.
  • A DataBuffer is just a thin wrapper around one array (usually) of primitive type. One thing to realize about representing images is that their data is stored in a one-dimensional array, rather that in a two-dimensional array of arrays, because a single array affords a better memory layout.

  • Here's a demo where I:
    (1) create an image from a byte[]
    (2) extract that byte[] from the image
    (3) create a new byte[] based on (2)
    (4) do (1) again, and
    (5) show the two resulting images.
     
    Ulf Dittmer
    Rancher
    Posts: 42968
    73
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I wouldn't worry about resources. Most likely, one 32-bit int would be used either way, whether it stores an 8-bit gray value, or three 8-bit color values. That's just a guess, though.

    The alpha value controls the transparency/opacity; you can ignore it if your application doesn't use image overlays and similar multi-image combinations. I don't think grayscale images support it, anyway.
    [ February 20, 2006: Message edited by: Ulf Dittmer ]
     
    Michael Mas
    Greenhorn
    Posts: 8
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    GrayExample looks very interesting but have to convert the int array with pixel intensities to byte[] ...
    Is there something simple to do about it ?

    Thanks a lot
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic