GrayScale (8bit per pixel) Image Pixel Manipulation in Java

1.8k Views Asked by At

I've heard that the data in gray-scale images with 8-bits color depth is stored in the first 7 bits of a byte of each pixel and the last bit keep intact! So we can store some information using the last bit of all pixels, is it true?

If so, how the data could be interpreted in individual pixels? I mean there is no Red, Blue and Green! so what do those bits mean?

And How can I calculate the average value of all pixels of an image? I prefer to use pure java classes not JAI or other third parties.

Update 1

BufferedImage image = ...; // loading image
image.getRGB(i, j);

getRGB method always return an int which is bigger than one byte!!! What should I do?

1

There are 1 best solutions below

1
On BEST ANSWER

My understanding is that 8-bits colour depth means there is 8-bits per pixel (i.e. one byte) and that Red, Gren and Blue are all this value. e.g. greyscale=192 means Red=192, Green=192, Blue=192. There is no 7 bits plus another 1 bit.

AFAIK, you can just use a normal average. However I would use long for the sum and make sure each byte is unsigned i.e. `b & 0xff

EDIT: If the grey scale is say 128 (or 0x80), I would expect the RGB to be 128,128,128 or 0x808080.