34.10. PGM: NetPBM Image File Format

Rather than invest a significant amount of time into writing image file format readers for the myriad of different image files formats, DIRSIGreads in single file format known as PGM. Although the format of these files will be summarized here, tools for converting image files from other formats to PGM can acquired by down-loading the NetPBM distribution from the following site:

    ftp://wuarchive.wustl.edu/graphics/graphics/packages/NetPBM
    
The PGM file format supports both and binary formatted data, however, the format is suggested for cross-platform compatibility. The PGM image file contains a simple three line header that indicates the format and dimensions of the image data.

P2 The PGM image format marker
512 512 The X (column) and Y (row) size of the image
255 The maximum possible digital count of the pixel data

Following the header, is the image pixel data using a (Row #0, Column #0), (Row #0, Column #1) format. Line feeds in the image data section are optional. The following example depicts a simple 4 x 4 image:
P2
4 4
255
0   0   0   0 
85  85  85  85
170 170 170 170
255 255 255 255
    
To utilize binary image data, the header remains in format, however, the first line changes:
P5
4 4
255
[8-bit binary data begins here but cannot be depicted in this manual]
    

34.10.1. Using the NetPBM tools

The NetPBM distribution comes with programs and man pages for converting from various image formats to monochrome, grey scale, and color) images. If the NetPBM tools are available on your system, a TIFF format image can be converted to a PGM binary image using the following command line:

prompt> tifftopnm < example.tif > example.pgm
      
To create a raw PGM instead, use the following command line:
prompt> tifftopnm < example.tif | pnmtoraw > example.pgm