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-images or compressed images.

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.

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

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-, 24-, and 32-bit deep images. The array should be scaled to 0-15 for 4-bit deep images. If the image has 3 dimensions and the first dimension is 3, a 24-bit bitmap file is created. If the image has 3 dimensions and the first dimension is 4, a 32-bit 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.

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

R, G, B

Color tables to be used for 8-bit images. If omitted, the colors loaded in the COLORS common block are used. These arguments are ignored for 24-bit and 32-bit images.

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 https://docs.microsoft.com/en-us/previous-versions//dd183376(v=vs.85) 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 or 32-bit images to red, green, blue [, alpha]: Image[0, ij] = red, Image[1, ij] = green, Image[2, ij] = blue, and if there is an alpha channel, Image[3,i,j] = alpha. By default, 24-bit images are written with color interleaving of blue, green, red. By default, 32-bit images are written with color interleaving of blue, green, red, alpha.

Version History


Pre-4.0

Introduced

8.2

Add support for 32-bit (RGBA) bitmaps files

See Also


READ_BMP, QUERY_* Routines