PROGRAMMER'S BOOKSHELF

Examining Graphics File Formats

Jonathan Erickson

From our 1988 unraveling of TIFF to the 1993 examination of FLI, articles on graphics file formats have consistently been among the most popular DDJ has published. An admitted shortcoming, however, is that overall, the information is both inconsistent in presentation and incomplete in coverage. That is to say, Kent Quirk's August 1989 "Translating PCX Files" takes a much different approach than, say, Tom Swan's August 1993 "Diving into Windows Bitmaps"--although both ultimately reveal details about graphics file formats. And while we've covered many different file formats over the years, there are many more we haven't. I've often thought that what's needed is a book that examines, in detail, the various file formats available to programmers. The Encyclopedia of Graphics File Formats, Bitmapped Graphics Programming in C++, and Programming for Graphics Files in C and C++ are recently published books that do just this. As you might expect, there is overlap among them. Each begins with an overview of graphics file formats--color, compression, hardware and operating-system dependencies, bitmap vs. vector files, metafiles, conversion issues, and the like. Nor is it a surprise that the books generally cover the same file formats--TIFF, PCX, GIF, BMP, and so on. Likewise, all three are written for the same audience--programmers who need to import or export graphics files into or out of their applications. Nonetheless, each author does take a different approach to the task.

As its title suggests, the Encyclopedia of Graphics File Formats, by James Murray and William vanRyper, is a reference book. As such, it provides a consistent presentation of nearly 100 graphics file formats ranging from Adobe's Photoshop to Zenographics' ZGM. In between, you'll find the familiar (PCX, TIFF, EPS, QuickTime) alongside the uncommon (FITS, TDDD, VICAR2, SGI YAODL). Of particular relevance to this issue of DDJ, for instance, is a discussion of the POV and DKB file formats (see "Ray Tracing and the POV-Ray Toolkit," by Craig Lindley, on page 68) and the IFF format (see "Morphing 3-D Objects in C++," by Glenn Lewis, on page 18).

Each file format is presented in capsule form (name, type, colors, image size, and other details), followed by a brief overview, file organization (including source-code examples), file details (usually with more code), and pointers to additional information (it's gratifying that DDJ is often cited). More than a dozen pages, for example, are dedicated to the FLI (Flic) file format Jim Kent wrote about in the March 1993 issue of DDJ. Although the Encyclopedia doesn't publish a working program in hardcopy, as did Jim's DDJ article, it does provide much more detailed information.

Included with the Encyclopedia of Graphics File Formats is a CD-ROM containing code examples to read, write, and display files in most (but not all) of the formats covered in the book. Additionally, the CD-ROM details format specifications, sample images, and utilities for manipulating and converting graphic files. Note that the authors did not write all of the tools and utilities on the CD themselves, opting instead to provide publicly available programs for image conversion, manipulation, and the like. For browsing the CD-ROM, they provide Mosaic, and in most cases, the CD includes original format specifications (when the vendors allowed publication).

Marv Luse's Bitmapped Graphics Programming in C++ and John Levine's Programming for Graphics Files in C and C++ take a less structured approach. Both provide page after page of published source code, and both come with DOS diskettes containing the published code and more. It's interesting, however, that Luse based his book on code he wrote (not surprising, as he is founder of Autumn Hill Software, which specializes in DOS/Windows graphics development tools), while Levine for the most part wrote his book around publicly available source code from Jef Poskanzer, Sam Leffler, Tom Lane, and others.

Although acknowledging its importance, Luse doesn't waste much time with theory. In his "theoretical" overview of computer graphics, Luse jumps right into the complete source code for a C++ class that implements and converts color models. He quickly follows this with classes for palette modification, color mapping, dithering, and compression. In particular, it was interesting to learn that CompuServe's GIF format is based on Unisys's patented LZW compression. Although all three books pointed this out, Luse alone (in his introduction) cautioned against blindly developing GIF-based commercial software.

Although the subject isn't formally addressed until page 250 in the book, file formats are at the heart of Bitmapped Graphics Programming in C++. For the most part, Luse zeros in on those most commonly associated with PC (DOS/Windows) software: BMP, EPS, GIF, MSP, PCX, TGA, TIFF, and WPG. However, he also wanders into the non-DOS arenas of XBM (X Window X-11 bitmaps), RAS (Sun Raster Format), PNTG (Apple's MacPaint), and IMG (Digital Research's GEM format).

Luse categorizes bit-mapped files into simple and complex formats. In the simple-format section, each file format is approached in a consistent, code-dependent manner. In each chapter, Luse briefly introduces the file format, discusses its structure and header, then launches into C++ code that presents class and structure definitions for format. This is followed by a query program that prints a formatted listing of the format's data structures. Luse then presents a file-viewing program, another program that outputs a file from a VGA display or memory bitmap, and finally, a demo program that writes a file. Understandably, there are exceptions to this pattern. For instance, when discussing the GEM IMG format, Luse throws in the source code for an IMG-to-PCX conversion program (which, curiously, includes comments relating to the nearby IMG viewer file instead of the IMG2PCX program). Other variations on the pattern include a program that colorizes monochrome Sun raster files, as well as both Windows and DOS versions of a BMP file viewer.

Luse changes pace when approaching the complex formats, spending more time on explanation. In addition to the expected overview, he details the base format structures, any extensions, and image and data types. The author then jumps back to the familiar pattern of programs for file querying and viewing, outputting files from a VGA display or memory bitmap, and demos to write files. The included disk contains all the source-code listings from the book, plus additional files for building libraries, testing programs, and more.

Like Luse, John Levine also takes his time before launching into the heart of graphics file formats, devoting the first 100 pages or so of the book to background information, and to a greater extent, his "framework" for discussing bitmap files. In particular, Levine writes his book around the PBM utilities (created primarily by Jef Poskanzer) which enable you to read, write, and manipulate a number of bitmap file formats. In Levine's words, "PBM defines three extremely simple image formats called PBM, PGM, and PPM, as a 'lingua franca' into which all the other formats can be translated." (The source-code diskette contains, among other files, PBMPLUS, a toolkit for converting various image formats to and from portable formats, and therefore to and from each other. PBMPLUS consists of: PBM, for bitmaps; PGM, for grayscale images; PPM, for full-color images; and PNM, which performs content-independent manipulations on the three internal formats.)

After spending 70 or so pages explaining the PBM utilities, Levine dives into graphics file formats themselves. Instead of simply lumping file formats into simple and complex, he divides them into run-length compressed (MacPaint, PCX, IMG, and IFF), uncompressed (BMP and TGA), dictionary compressed (GIF, TIFF, JPEG), vector (HP-GL, Windows Metafiles), and printer (HP-PCL, Postscript).

Levine presents the requisite overview before discussing how you read and write a particular file format. When it comes to PCX, for example, he publishes a short utility that converts a PCX file to a PPM file by reading the PCX header, uncompressing the image data, reading the color map, and unscrambling the data before applying the color map. Levine then reverses the process by writing a PCX file and briefly discusses possible extensions. This pattern is generally applied throughout the book.

One unique feature of Programming for Graphics Files in C and C++ is its thorough discussion of JPEG, which Luse doesn't cover and Murray/vanRyper only touch upon. The disk includes version 4 of the complete JPEG read/write libraries; routines for reading/writing GIF, TGA, PBMPLUS, and JIFF; and driver code. The disk also contains the Leffler TIFF Library 3.0 which implements the complete TIFF 6.0 specification.

At about $40.00 and up, these aren't inexpensive books, and unless you're a full-time graphics programmer, you probably won't need all three. Considering my general needs--quick reference, as opposed to in-depth analysis--the Encyclopedia of Graphics File Formats is most useful. For more depth, I find myself picking up Marv Luse's Bitmapped Graphics Programming in C++, which is clearly written and straightforward in its presentation.

That said, it is interesting that, when putting together this issue of DDJ and needing background information on the IFF file format discussed by Glenn Lewis, I ended up referring to Levine's Programming for Graphics Files in C and C++. As you'd expect of a reference book, Murray and vanRyper give IFF cursory coverage, while Luse, none at all. Levine, however, provides useful, in-depth information on IFF bitmap images and the ILBM subformat.

Encyclopedia of Graphics File Formats

James D. Murray & William vanRyper O'Reilly & Associates, 650 pp., $59.95

ISBN 0-937175-058-9

Bitmapped Graphics Programming in C++

Marv Luse

Addison-Wesley, 705 pp., $37.95

ISBN 0-201-63209-8

Programming for Graphics Files in C and C++

John Levine

John Wiley & Sons, 494 pp., $49.95

ISBN 0-471-59856-9


Copyright © 1994, Dr. Dobb's Journal