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 in the lib subdirectory of the IDL distribution.

Calling Sequence

Result = READ_BMP( Filename, [, R, G, B [, Ihdr])



A scalar string specifying the full path name of the bitmap file to read.

R, G, B

Named variables that will contain the color tables from the file. There 16 elements each for 4 bit images, 256 elements each for 8 bit images. Color tables are not defined or used for 24 bit images.


A named variable that will contain a structure holding the BITMAPINFOHEADER from the file. Tag names are as defined in the MS Windows Programmer's Reference Manual, Chapter 7.



If this keyword is set, color interleaving of 16- and 24-bit images will be R, G, B, i.e., Result[ 0,i,j ] = red, Result[ 1,i,j ] = green, Result[2, i , j ] = blue.


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 = BYTARR(3, 200, 200)

a(0, *, *) = 255 ; Make a red square image.

TV, a, /TRUE ; View the image.

WRITE_BMP, 'foo.bmp', a

If you open the .bmp file in certain bitmap editors, you may find that the square is blue.

READ_BMP, 'foo.bmp', image

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.

TV, image, /TRUE; Displays the image in blue.

See Also