[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Write_bmp procedure changes RGB color order to BGR for input image



Hello,

I am not sure if others have encountered this problem before.

True color images that I read in IDL are always in the RGB color order
but when one writes MS Windows BMP format files, they should be
written in the BGR color order for them to be interpreted correctly by
image viewing utilities.

To my surprise, I discovered that using the RGB keyword in the IDL
procedure "write_bmp" actually changes the color order of the supplied
input image from RGB to BGR color order. This is non-intuitive. I
would have expected that the keyword was only to indicate that the
array is being supplied in the standard RGB color order. When IDL
writes the file with the RGB keyword set, it should swap the red and
blue buffers of the original image being written to the BMP file, but
not swap them in the original image.

For e.g.
IDL> img = read_image(some_true_color_image_file)  ; in RGB color order
IDL> tv, img, /true                                ; looks good
IDL> write_bmp, filename.bmp, img, /rgb 
IDL> tv, img, /true                                ; no good (:-(!

However filename.bmp is correctly viewed by an external viewer.
One can always use reverse(img, 1) after such a call to write_bmp
to fix the color order to RGB again. But this should not be necessary.

I looked at write_bmp.pro and found the statements below to be the
cause of the problem. As can be seen, the original image itself is
being swapped and written to the file.

###   excerpt from write_bmp.pro ###

    if keyword_set(rgb) then begin  ; Swap red and blue channels.
        buff = image[0,*,*]
        image[0,0,0] = image[2,*,*]
        image[2,0,0] = buff
    endif
    
    if n_elements(pad) eq 0 then writeu, unit, byte(image) $  ;Again, dunk it.
    else begin
        for i=0, ny-1 do writeu, unit, byte(image[*,*,i]), pad
    endelse

###

As far as I know, this problem exists in IDL version 5.3 and 5.4. Hope
this helps others who may have encountered this and wondered!


Sandeep Dalal

Philips Research
345, Scarborough Road
Briarcliff Manor, NY 10510.