How To Write 2D Palettized Color Images From IDL Direct Graphics to GIF, PNG or TIFF Files
Topic:
Indexed or palettized color images are 2D images, the color of whose pixels are determined by a 256-element color table on your computer's video card. Once upon a time computer monitor hardware used this color table pretty ubiquitously; it allowed much more efficient color display than 24- and 32-bit True Color display. However, with the evolution of faster processor speeds and massively larger video card memory, the 256-color setting on the display monitor has become obsolete.
Nevertheless, there is still a valid use for palettized color in image graphics files like GIF, PNG and TIFF. Unfortunately, the evolution of the modern computer monitor has affected the easiest way for IDL to get a snapshot of a graphics window for export to image file formats. Namely, IDL's
TVRD function on a modern computer monitor device no longer provides anything but RGB values (or values from one of the planes). It turns out that, to acquire the color indexes of an IDL indexed color graphic, the IDL Direct Graphics programmer needs to turn to a different device than the computer monitor. This Help Article provides the steps for using this alternative device.
Note: IDL Object Graphics has a completely different approach.
Discussion:
The device in question is the 'Z-Buffer'. This is a "virtual window" device (like a "pixmap") that was originally implemented in order to allow "depth-order rendering" of multiple graphics objects. Essentially, it is designed to simulate 3D graphics. Luckily, its default settings have one great virtue for indexed color processing - the Z-Buffer pixmap is stored strictly in color table indexes. That is,
TVRD() only returns the color table index values of the graphics window it is "snapshooting". That is precisely the unit of measure that 2D color GIF, PNG and TIFF files are looking for. Here is an example of how WRITE_GIF, WRITE_PNG or WRITE_TIFF can take advantage of this Z-Buffer property:Solution:
data = dist(360) ; simple example data
; the Z-Buffer uses the same color table as the 'X' or 'WIN' monitor devices
loadct, 5
oldDevice = !d.name ; This will be either 'X' or 'WIN'
set_plot, 'Z'
; Set the pixel width/height of the virtual window
device, SET_RESOLUTION=[500, 400]
; Note how no "device, DECOMPOSED=0" call is needed in the Z-Buffer. That
; is because it defaults to using palettized color. DECOMPOSED is not
; even a valid keyword in the 'Z-Buffer'
shade_surf, data, CHARSIZE=1.5 ; a colorful surface plot
snapshot = tvrd() ; returns a 2D array of color table indices
set_plot, oldDevice ; restore the original graphics device
; Load color table settings into arrays that can be passed to the image file
tvlct, r, g, b, /GET
; Export the Z-Buffer snapshot and its color table settings
write_png, 'dist_shade_surf.png', snapshot, r, g, b
write_gif, 'dist_shade_surf.gif', snapshot, r, g, b
write_tiff, 'dist_shade_surf.tif', reverse(snapshot, 2), $
RED=r, GREEN=g, BLUE=b