PBM, PGM, PNM, and PPM

Also Known As: Portable Bitmap Utilities, pbmplus


Type Bitmap
Colors Up to 24-bit
Compression None
Maximum Image Size NA
Multiple Images Per File NA
Numerical Format NA
Originator Jef Poskanzer
Platform UNIX, Intel-based PCs
Supporting Applications pbmplus, others
See Also Most of the formats in this book

Usage
File format conversion through an intermediary least-common-denominator format.

Comments
PBM, PGM, PNM, and PPM are intermediate formats used in the conversion of many little known formats via pbmplus, the Portable Bitmap Utilities. These formats are mainly available under UNIX and on Intel-based PCs.

Vendor specifications are available for this format.

Code fragments are available for this format.

Sample images are available for this format.


The Portable Bitmap Utilities (PBM) is a collection of programs organized, maintained, and primarily written by Jef Poskanzer. Although owned and copyrighted by Mr. Poskanzer, they are freely available in both source and executable form on the Internet and on many BBS systems. The "bitmap" in PBM is used in the older sense to refer to monochrome images. There are actually three other sets of programs encompassed by the PBM utilities. These are the Portable Greymap Utilities (PGM), the Portable Pixmap Utilities (PPM), and the Portable Anymap Utilities (PNM). PBM programs manipulate monochrome bitmaps, and PGM and PPM programs manipulate gray-scale bitmaps and color bitmaps, respectively. PNM programs operate on all of the bitmaps produced by the other programs. There is no file format associated with PNM itself. Most people call the overall set of programs PBM and the newer version pbmplus, however, and we'll follow this convention.

Contents:
File Organization
File Details
For Further Information

Associated with pbmplus are three least-common-denominator intermediate formats. When converting a graphics file from one format to another, we speak of the source file (in the current format) and the destination file (in the desired new format). pbmplus works by taking a source file and converting it into one of the intermediate formats. That intermediate format file is then converted into the destination format.

To see how this works, here are the steps necessary to convert a Microsoft Windows Bitmap (BMP) format file named testfile.bmp to a GIF format file. These are 256-color files, so we use the PPM utilities bmptoppm and ppmtogif:

bmptoppm testfile.bmp This produces testfile.ppm
ppmtogif testfile.ppm This produces testfile.gif

The latest version of pbmplus is available on the CD-ROM that accompanies this book.

File Organization

The PBM, PGM, and PPM formats are each designed to be as simple as possible. Each starts out with a header, and the bitmap data follows immediately after. The header is always written in ASCII, and data items are separated by white space (blanks, tabs, carriage returns, or linefeeds). The data portion of each file type can be written in either ASCII or binary form.

File Details

There are two versions of each of the the PBM, PGM, and PPM headers. Although all the headers are in ASCII format, one is used for the ASCII version of the format, and the other is used for the binary version.

PBM Header

A PBM header consists of the following entries, each separated by white space:

MagicValue Literally P1 for ASCII version, P4 for binary
ImageWidth Width of image in pixels (ASCII decimal value)
ImageHeight Height of image in pixels (ASCII decimal value)

PGM Header

A PGM header consists of the following entries, each separated by white space:

MagicValue Literally P2 for ASCII version, P5 for binary
ImageWidth Width of image in pixels (ASCII decimal value)
ImageHeight Height of image in pixels (ASCII decimal value)
MaxGrey Maximum gray value (ASCII decimal value)

PPM Header

A PPM header consists of the following entries, each separated by white space:

MagicValue Literally P3 for ASCII version, P6 for binary
ImageWidth Width of image in pixels (ASCII decimal value)
ImageHeight Height of image in pixels (ASCII decimal value)
MaxGrey Maximum color value (ASCII decimal value)

Image Data

After the header is a series of lines describing widthxheight pixels. For PPM, each pixel contains three ASCII decimal values between 0 and the specified maximum value, starting at the top-left corner of the pixmap, proceeding in normal English reading order. The three values for each pixel represent red, green, and blue, respectively; a value of 0 means that color is turned off, and the maximum value means that color is "maxxed out."

For PBM and PGM, there is only one ASCII decimal value per pixel. For PBM, the maximum value is implicitly 1.

Here is an example of a small pixmap in this format:

P3
# feep.ppm
4 4
15
 0  0  0    0  0  0    0  0  0   15  0 15
 0  0  0    0 15  7    0  0  0    0  0  0
 0  0  0    0  0  0    0 15  7    0  0  0
15  0 15    0  0  0    0  0  0    0  0  0

You can include comments in the PBM file. Characters from a # character to the next end-of-line are ignored. There is a suggested maximum of 70 characters per line, but this is not an actual restriction.

Mr. Poskanzer cautions that programs that read this format should be as lenient as possible, accepting anything that looks remotely like a pixmap.

RAWBITS Variant

There is also a variant on the format, available by setting the RAWBITS option at compile time. This variant differs from the traditional format in the following ways:

Note that this raw format can only be used for maximum values less than or equal to 255. If you use the PPM library and try to write a file with a larger maximum value, it automatically uses the slower, but more general, plain format.

For Further Information

For further information about the PBM, PGM, PNM, and PPM utilities, see the documentation on the CD-ROM. See also the code and documentation for the pbmplus utilities, also included on the CD-ROM.

For more information about PBM, PGM, PNM, and PPM, you can contact:

Jef Poskanzer
Email: jef@well.sf.ca.us



Copyright © 1996, 1994 O'Reilly & Associates, Inc. All Rights Reserved.