Fanning Software Consulting

Converting 8-Bit Image to 24-bit Image

QUESTION: I have an 8-bit color image bitmap file that I read like this:

   bmp = READ_BMP(filename, r, g, b)
The bmp variable is an N x M array with values that are indices of the RGB arrays. For example, if bmp[5,10] = 3, the color triple for that pixel is R[3], G[3], B[3]. To convert this to an N x M x 3 array so I can use it on a button widget, I've used a brute force method like this:

   bmp = READ_BMP(filename, r, g, b)
   s = size(bmp, /structure)
   Nx = s.dimensions[0]
   Ny = s.dimensions[1]
   Nc = n_elements(R)
   bmpR = bytarr(Nx, Ny)
   bmpG = bytarr(Nx, Ny)
   bmpB = bytarr(Nx, Ny)
   for c = 0, Nc-1 do begin
      i = where(bmp eq c, count)
      if count gt 0 then begin
         bmpR[i] = R[c]
         bmpG[i] = G[c]
         bmpB[i] = B[c]
   bmp24 = [[[bmpR]],[[bmpG]],[[bmpB]]]

It seems like there should to be a better IDL way to do this, but my juggling skills are failing me. Can you suggest something that takes fewer lines of code and is more consistent with The IDL Way?

ANSWER: How about this:

   bmp = READ_BMP(filename, r, g, b)
   bmp24 = [[[r[bmp]]], [[g[bmp]]], [[b[bmp]]]]

This will create a band-interleaved 24-bit image. If you need something else, you can always rearrange the order of the interleaving with the Transpose command.

Web Coyote's Guide to IDL Programming