The READ_BMP function reads a Microsoft Windows Version 3 device independent bitmap file (.BMP) and returns the image.

READ_BMP does not handle 1-bit-images or compressed images.

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.

Note: To find information about a potential BMP file before trying to read its data, use the QUERY_BMP function.

Examples


To open, read, and display a BMP file named foo.bmp in the current directory and store the color vectors in the variables R, G, and B, enter:

; Read and display an image:
TV, READ_BMP('foo.bmp', R, G, B)
 
; Load its colors:
TVLCT, R, G, B

Many applications that use 24-bit BMP files outside IDL expect BMP files to be stored as BGR. For example, enter the following commands.

; Make a red square image:
a = BYTARR(3, 200, 200, /NOZERO)
a[0, *, *] = 255
;View the image:
TV, a, /TRUE
WRITE_BMP, 'foo.bmp', a

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

image = READ_BMP('foo.bmp')
; IDL reads the image back in and interprets it as red:
TV, image, /TRUE
; Reverse the order of the indices by adding the RGB keyword:
image = READ_BMP('foo.bmp', /RGB)
; Displays the image in blue:
TV, image, /TRUE

Syntax


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

Return Value


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. The RGB keyword can be used to change the interleaving.

For 32-bit images, the dimensions are (4, biWidth, biHeight). Color interleaving is blue, green, red, alpha; i.e., Result[0,i,j] = blue, Result[1,i,j] = green, Result[2,i,j] = red, Result[3,i,j] = alpha. The RGB keyword can be used to change the interleaving.

Arguments


Filename

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 or 32-bit images.

Ihdr

A named variable that will contain a structure holding the BITMAPINFOHEADER from the file.

Tag names in the a BITMAPINFOHEADER structure are as defined in the Microsoft Developer Network Library; see https://docs.microsoft.com/en-us/previous-versions//dd183376(v=vs.85) for details.

Keywords


RGB

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

Version History


Pre-4.0

Introduced

8.2

Add support for 32-bit (RGBA) bitmaps files

See Also


IOPEN, WRITE_BMP, QUERY_BMP