The
READ_BMP function reads a Microsoft Windows Version 3 device independent
bitmap file (.BMP) and returns a byte array containing the image. Dimensions are taken from the BITMAPINFOHEADER of the file. In the case of 4-bit or 8-bit images, the dimensions of the resulting array are (
biWidth
,
biHeight
).
For 24-bit images, the dimensions are (
3
,
biWidth
,
biHeight
). Color interleaving is blue, green, red; i.e., Result[
0,i,j
] = blue, Result[
1,i,j
] = green, etc.
READ_BMP does not handle 1-bit-deep images or compressed images, and is not fast for 4-bit images. The algorithm works best on images where the number of bytes in each scan-line is evenly divisible by 4.
This routine is written in the IDL language. Its source code can be found in the file
read_bmp.pro
in the
lib
subdirectory of the IDL distribution.
To open, read, and display the BMP file named
foo.bmp
in the current directory and store the color vectors in the variables R, G, and B, enter:
TV, READ_BMP('foo.bmp', R, G, B) ; Read and display an image
TVLCT, R, G, B ; Load its colors
Many applications that use 24-bit BMP files outside IDL expect BMP files to be stored as BGR. For example, enter the following commands.
a(0, *, *) = 255 ; Make a red square image.
TV, a, /TRUE ; View the image.
If you open the
.bmp
file in certain bitmap editors, you may find that the square is blue.
TV, image, /TRUE ; IDL reads the image back in and interprets it as red.
READ_BMP, 'foo.bmp', image, /RGB ; Flip the order of the indices by adding the RGB keyword.