There are several web resources on these simple formats:
http://en.wikipedia.org/wiki/Portable_pixmap
http://www.physics.emory.edu/~weeks/graphics/mkppm.html
http://netpbm.sourceforge.net/doc/ppm.html
http://www.fileformat.info/format/pbm/
http://www.linuxjournal.com/article/1119
Note that there are some inconsistencies in the information resources above
-after all this is the web. Below is excerpted from the first link and this link
http://local.wasp.uwa.edu.au/~pbourke/dataformats/ppm/
A PPM file consists of two parts, a header and the image data. The header consists of at least three parts normally delineated by carriage returns and/or linefeeds but the PPM specification only requires white space. The first "line" is a magic PPM identifier, it can be "P3" or "P6" (not including the double quotes!). The next line consists of the width and height of the image as ascii numbers. The last part of the header gives the maximum value of the color components for the pixels, this allows the format to describe more than single byte (0..255) color values. In addition to the above required lines, a comment can be placed anywhere with a "#" character, the comment extends to the end of the line.
The following are all valid PPM headers.
Header example 1P6 1024 788 255Header example 2
P6 1024 788 # A comment 255Header example 3
P3 1024 # the image width 788 # the image height # A comment 1023
The format of the image data itself depends on the magic PPM identifier. If it is "P3" then the image is given as ascii text, the numerical value of each pixel ranges from 0 to the maximum value given in the header. The lines should not be longer than 70 characters.
PPM example 4 (216 byte P3 format), example4p6 (87 bytes P6 format) note: 28 bytes to write # example from the man pageP3 # example from the man page 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
If the PPM magic identifier is "P6" then the image data is stored in byte format, one byte per colour component (r,g,b). Comments can only occur before the last field of the header and only one byte may appear after the last header field, normally a carriage return or line feed. "P6" image files are obviously smaller than "P3" and much faster to read. Note that "P6" PPM files can only be used for single byte colours.
While not required by the format specification it is a standard convention to store the image in top to bottom, left to right order. Each pixel is stored as a byte, value 0 == black, value 255 == white. The components are stored in the "usual" order, red - green - blue.
P3 3 4 255 #the P3 means colors are in ascii, then 3 columns and 4 rows, then 255 for max color, then RGB triplets 255 0 0 0 255 0 0 0 255 255 255 0 255 255 255 0 0 0 0 255 255 75 75 75 127 127 127 150 150 150 150 150 150 150 150 150
The image (expanded):
This format is identical to the above except it stores greyscale information, that is, one value per pixel instead of 3 (r,g,b). The only difference in the header section is the magic identifiers which are "P2" and "P5", these correspond to the ascii and binary form of the data respectively.
PGM exampleAn example of a PGM file of type "P2" is given below
P2 24 7 15 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 3 3 3 0 0 7 7 7 7 0 0 11 11 11 11 0 0 15 15 15 15 0 0 3 0 0 0 0 0 7 0 0 0 0 0 11 0 0 0 0 0 15 0 0 15 0 0 3 3 3 0 0 0 7 7 7 0 0 0 11 11 11 0 0 0 15 15 15 15 0 0 3 0 0 0 0 0 7 0 0 0 0 0 11 0 0 0 0 0 15 0 0 0 0 0 3 0 0 0 0 0 7 7 7 7 0 0 11 11 11 11 0 0 15 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0