A Netpbm file starts with a short header written as text. It gives the type, the size and, except for black and white images, the largest sample value. The raster follows it directly.
Magic numbers
| Magic | Bytes | Format | Name | Raster | Usual extension |
|---|---|---|---|---|---|
P1 | 50 31 | Plain PBM | Bitmap | ASCII digits | .pbm |
P2 | 50 32 | Plain PGM | Greyscale | ASCII decimal | .pgm |
P3 | 50 33 | Plain PPM | Colour | ASCII decimal | .ppm |
P4 | 50 34 | Raw PBM | Bitmap | Packed bits | .pbm |
P5 | 50 35 | Raw PGM | Greyscale | Binary samples | .pgm |
P6 | 50 36 | Raw PPM | Colour | Binary samples | .ppm |
P7 | 50 37 | PAM | General | Binary samples | .pam |
.pnm is used when a file may be any of PBM, PGM or PPM. None of these
formats has a registered Internet media type; the usual informal ones are
image/x-portable-bitmap, image/x-portable-graymap,
image/x-portable-pixmap and image/x-portable-anymap.
Header fields
| Order | Field | Form | In | Description |
|---|---|---|---|---|
| 1 | magic | 2 ASCII characters | all | One of the values above. |
| 2 | whitespace | 1 or more | all | Separates the fields. |
| 3 | width | ASCII decimal | all | Width in pixels. |
| 4 | whitespace | 1 or more | all | |
| 5 | height | ASCII decimal | all | Height in pixels. |
| 6 | whitespace | 1 or more | PGM, PPM | Only before maxval. |
| 7 | maxval | ASCII decimal | PGM, PPM | The largest sample value: greater than 0 and less than 65536. PBM has no maxval. |
| 8 | whitespace | exactly 1 | all | One character, usually a newline, ends the header. The raster starts at the next byte. |
In a raw file the single whitespace character matters: any further byte belongs to the raster, even if it looks like whitespace.
Whitespace
| Byte | Name |
|---|---|
20 | Space |
09 | Tab |
0A | Line feed |
0D | Carriage return |
0B | Vertical tab |
0C | Form feed |
The PBM and PGM pages give all six, the set C’s isspace() recognises. The
PPM page lists only blanks, tabs, carriage returns and line feeds.
Maxval and sample size
| Maxval | Bytes per raw sample |
|---|---|
| 1 to 255 | 1 |
| 256 to 65535 | 2, most significant byte first |
Before April 2000 a raw file could not have a maxval above 255.
Several images in one file
A raw file may hold more than one image, placed back to back with nothing between them; each has its own header. Before July 2000 a file could hold only one, and many programs still ignore everything after the first. A plain file holds exactly one image.
Widest compatibility
The specification says readers should be lenient. For the simplest readers, write only the magic and a newline, width and height separated by one space and a newline, a maxval of 255 and a newline where there is one, then the raster, with one image per file.
Comments
A
#starts a comment that runs to the next carriage return or line feed, or to the end of the file. Comments are ignored and can appear only before the single whitespace character that ends the header. Before June 2024 the specification said that the line end was part of the comment, although Netpbm never worked that way.