WRITE_BMP

Syntax | Arguments | Keywords | Examples | Version History | See Also

The WRITE_BMP procedure writes an image and its color table vectors to a Microsoft Windows Version 3 device independent bitmap file (.BMP).

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

Syntax

WRITE_BMP, Filename, Image[, R, G, B] [, /FOUR_BIT] [, IHDR=structure] [, HEADER_DEFINE=h{define h before call}] [, /RGB]

Arguments

Filename

A scalar string containing the full pathname of the bitmap file to write.

Image

The array to write into the new bitmap file. The array should be scaled into a range of bytes for 8- and 24-bit deep images. Scale to 0-15 for 4-bit deep images. If the image has 3 dimensions and the first dimension is 3, a 24-bit deep bitmap file is created.

Note
For 24-bit images, color interleaving is blue, green, red: Image[0, ij] = blue, Image[1, ij] = green, Image[2, ij] = red.

R, G, B

Color tables. If omitted, the colors loaded in the COLORS common block are used.

Keywords

FOUR_BIT

Set this keyword to write as a 4-bit device independent bitmap. If omitted or zero, an 8-bit deep bitmap is written.

IHDR

Set this keyword to a BITMAPINFOHEADER structure containing the file header fields that are not obtained from the image itself. The fields in this structure that can be set are: bi{XY}PelsPerMeter, biClrUsed, and biClrImportant.

Tag names in the a BITMAPINFOHEADER structure are as defined in the Microsoft Developer Network Library; see http://msdn.microsoft.com/en-us/library/ms532290.aspx for details.

HEADER_DEFINE

If this keyword is set, WRITE_BMP returns an empty BITMAPINFOHEADER structure, containing zeros. No other actions are performed. This structure may be then modified with the pertinent fields and passed in via the IHDR keyword parameter. See the Microsoft Windows Programmers Reference Guide for a description of each field in the structure.

Note: this parameter must be defined before the call. For example:

H = 0 
WRITE_BMP, HEADER_DEFINE = H 

RGB

Set this keyword to reverse the color interleaving for 24-bit images to red, green, blue: Image[0, ij] = red, Image[1, ij] = green, Image[2, ij] = blue. By default, 24-bit images are written with color interleaving of blue, green, red.

Examples

The following command captures the contents of the current IDL graphics window and saves it to a Microsoft Windows Bitmap file with the name test.bmp. Note that this works only on a PseudoColor (8-bit) display:

WRITE_BMP, 'test.bmp', TVRD() 

The following lines create an image in an IDL graphics window, read it from the window and write it as a .bmp file in the temporary directory, then read the .bmp file and display it in the same graphics window:

; Save device settings and tell IDL to use a color table
DEVICE, GET_DECOMPOSED=old_decomposed
DEVICE, DECOMPOSED=0
LOADCT, 14

; Create an image and display it
IMAGE1 = DIST(300)
WINDOW, 1, XSIZE=300, YSIZE=300
TV, IMAGE1

; Write a bitmap file to the temporary directory
; Note the use of the TRUE keyword to TVRD
filename = FILEPATH('test.bmp', /TMP)
WRITE_BMP, filename, TVRD(/TRUE)
PRINT, 'File written to ', filename

; Read in the bitmap file
IMAGE2 = READ_BMP(filename)

; Display the IMAGE1 and IMAGE2 side by side
; Note the use of the TRUE keyword to TV
WINDOW, 1, XSIZE=600, YSIZE=300, $
   TITLE='Original (left) and Image read from file (right)'
TV, IMAGE1, 0
TV, IMAGE2, 1, /TRUE

; Restore device settings.
DEVICE, DECOMPOSED=old_decomposed

The following commands scale an image to 0-15, and then write a 4-bit BMP file, using a grayscale color table:

; Create a ramp from 0 to 255:
r = BYTSCL(INDGEN(16))

; Create an image
IMAGE = DIST(300)

WRITE_BMP, GETENV('IDL_TMPDIR')+'test2.bmp', $
   BYTSCL(Image, TOP=15), r, r, r, /FOUR

Version History

Pre-4.0

Introduced

See Also

READ_BMP, QUERY_* Routines