Question

I want to load a 8-bit grayscale image using grayscaleImage = ImageIO.read(grayscaleFile). However, the BufferedImage class (which is the type of grayscaleImage) only provides a method getRGB(int x, int y). So, what are the values of color.getRed(), color.getGreen(), color.getBlue() and color.getAlpha() where

Color color = new Color(grayscaleImage.getRGB(x, y), true);

From a first observation, it seems like the 8-bit value, stored in the image file at pixel (x,y), is propagated into the red, green and blue component of color while it's alpha value is fixed at 255.

Can someone confirm this observation?

Was it helpful?

Solution

The value that is obtained with image.getRGB(x,y) is an int that consists of the three color components Red, Green and Blue, each having 8 bit. The Alpha value is fixed as 255 when the image does not contain transparency.

When the image is a grayscale image, the Red, Green and Blue components of this RGB value will be all equal. So you can obtain the "gray" value as

int rgb = image.getRGB(x,y);

// This yields a value between 0 (black)
// and 255 (white) :
int gray = rgb & 0xFF; 

OTHER TIPS

That's right. A gray color is just a color with same value in three color's components (red, green, blue). Although you only need one value to determine the color (because red, green and blue components have the same value), at image loading that value is assign to all three color components.

If you get the value for each pixel of the image, you must obtain the same value for three color's components for the same pixel x,y. For alpha component, as it's a grayscale image, is correct it be at 255.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top