aspose file tools*
The moose likes Beginning Java and the fly likes changing gray pixels.... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "changing gray pixels...." Watch "changing gray pixels...." New topic
Author

changing gray pixels....

Michael Mas
Greenhorn

Joined: Feb 17, 2006
Posts: 8
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
Marshal

Joined: Mar 22, 2005
Posts: 39549
    
  27
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.


Ping & DNS - updated with new look and Ping home screen widget
Michael Mas
Greenhorn

Joined: Feb 17, 2006
Posts: 8
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
Marshal

Joined: Mar 22, 2005
Posts: 39549
    
  27
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

Joined: Feb 17, 2006
Posts: 8
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

Joined: Feb 17, 2006
Posts: 8
What do you mean by masking ... rgb & 0x255 ?

do i need to set Alpha ?

Thanks Ulf for your help.
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
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.


    There is no emoticon for what I am feeling!
    Ulf Dittmer
    Marshal

    Joined: Mar 22, 2005
    Posts: 39549
        
      27
    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

    Joined: Feb 17, 2006
    Posts: 8
    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
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: changing gray pixels....
     
    Similar Threads
    Star Office presentation stuff
    Code Challenges?
    (Joke) IT Company Full Names
    XML coding standards
    This weeks giveaway