Converting 8-Bit Images to 24-Bit Images

QUESTION: I have an 8-bit image, but I want to make a color TIFF or JPEG file. To do that, I need to have a 24-bit color image. How can I make a 24-bit image out of my 8-bit image?

ANSWER: All colors in IDL are represented as color triples. A color triple has three values that represent the amount of red, green, and blue light that should be directed on a particular pixel on the display. The amount of light is quantified as a byte value, so that there are 256 shades of each color possible, ranging from 0 or no color to 255 or as much color as the color gun is capable of producing.

Normally, the pixel values of an 8-bit image are used as indices into the RGB vectors of a color table. For example, suppose the image pixel has a value of 2. The pixel color then is represented by the numbers found in the R[2], G[2], and B[2] color table vectors. Suppose these numbers were 255, 128, and 0, respectively. Then any image pixel with a value of 2 would be represented by an orange color.

A 24-bit image has the color triples built directly into the image data itself. For example, a 24-bit image can be thought of as three color planes: a red plane, a blue plane, and a green plane.

The trick in making a 24-bit image from an 8-bit image and the color table vectors is to create those color planes. For example, consider the color above. We want that that pixel with a value of 2 to be represented by this color triple: [R[2], G[2], B[2]]. In fact, to do this for the whole image, we can write code like this:

      ; Scale the 2D image into the number of colors in the
      ; IDL session.

   scaled = BytScl(image2D, Top=!D.Table_Size-1)

      ; Create a 24-bit image of the proper size.

   s = Size(scaled, /Dimensions)
   image24 = BytArr(3, s[0], s[1])

      ; Get the color table vectors.

   TVLCT, r, g, b, /Get

      ; Prepare the image color separations.

   image24[0, *, *] = r[scaled]
   image24[1, *, *] = g[scaled]
   image24[2, *, *] = b[scaled]

Now you are all set to create a 24-bit TIFF or JPEG file. For example, to create a JPEG file, you would write this:

   Write_JPEG, 'test_image.jpg', image24, True=1, Quality=75

On a 24-Bit Display

Note that if you are on a 24-bit display, you can get a 24-bit image of the display window with the TVRD command, like this:

   image24 = TVRD(True=1)

Be sure you use the True keyword in the command above to indicate what kind of interleaving you want the image to have. (In this case, I chose a pixel interleaved image.) If you don't use a True keyword with the TVRD command on a 24-bit display, the resulting 2D image will contain the largest of the values in each of the three color channels. In other words, each 2D image pixel value at each image location will be calculated like this:

   pixel_value = Max([R[location], G[location], B[location]])

This almost never results in what you expect. In fact, just about the only time it will give you what you expect is if you have a gray-scale color table loaded, in which case these three values are all the same number.

[Return to IDL Programming Tips]