Extracting color from 8 bit pixel

Is there any way of extracting pixel colors of an 8 bit image? For an RGB image you can do:

c = getPixel(x, y);
r = (c>>16)&0xff;
g = (c>>8)&0xff;
b = c&0xff;

Is there a way to do this with an 8 bit image? I do not want to convert the image to RGB and back.

Hello Carrot -

First, an 8-bit (color) image has 8-bit (unsigned byte) pixels
that are indices into a “LUT,” a color look-up table (sometimes
called the color palette).

I have not found a simple single java or scripting call that returns
the color values for an 8-bit image. (There might be one that I
haven’t tracked down.) But you can get the 8-bit pixel values,
and then translate them into rgb color values using the image’s
LUT. This is illustrated by the following jython (python) script.
(Java code would use essentially the same classes and method

from ij import IJ
# open a sample rgb image and convert it to 8-bit color
IJ.run ('Clown (14K)')
IJ.run ('8-bit Color', 'number=256')
# bp will be a ByteProcessor
bp = IJ.getImage().getProcessor()
# LUT is a subclass of java.awt.image.IndexColorModel
icm = bp.getLut()
# get a sample pixel
p = bp.getPixel (31, 31)
print '8-bit pixel value p =', p
# now get its color components
rgb = icm.getComponents (p, None, 0)
print 'rbg color components for p are rgb =', rgb

When I run this script (using the Script Editor in Fiji . ImageJ)
I get:

8-bit pixel value p = 73
rbg color components for p are rgb = array('i', [192, 104, 11])

(in practice, I generally don’t bother doing this. Unless efficiency
matters a lot, I just go ahead and convert the 8-bit color image to
a 24-bit rgb image, and use that.)

Thanks, mm


Thanks for the detailed response! The color look-up table looks like the best way of doing what I need to.