Within the world of graphics (primarily Unix) PPM stands for "Portable PixMap" and acts as the absolute
lowest common denominator for color images (its kin the pgm and pbm handle gray-scale and bitmap images).
The default (ASCII) file format is quite simple:
P3
X Y
MAX
R G B R G B R G B ...
.
.
.
The 'P3' is a "
magic number" in the
Unix world which aids in identifying files. Rather than relying upon the
extension of a file to see what something is ('.ppm'), Unix looks into a file as described in '
/etc/magic' and uses that to identify files. The following is the section describing ppm and its kin:
# PBMPLUS
0 string P1 image/x-portable-bitmap 7bit
0 string P2 image/x-portable-greymap 7bit
0 string P3 image/x-portable-pixmap 7bit
0 string P4 image/x-portable-bitmap
0 string P5 image/x-portable-greymap
0 string P6 image/x-portable-pixmap
This is read as 'Starting at byte 0, look at the first string. If it is P1, then the file is a 7bit (ASCII) image/x-portable-bitmap'.
The second set of values in the ppm file are two numbers (in ASCII decimal) that are separated by some whitespace (some number of tabs and spaces or linefeeds) - it doesn't matter how much, as long as there is some space between them). These are shown above as X Y and are the X and Y dimensions of the image.
The third line is a single number that represents the range of color. Classically this will be a power of 2 - 1. If this number is '256' then the triplet '255 255 255' represents white and '128 128 128' is a 50% gray. However, if the number is '15' then '15 15 15' is white and '8 8 8' is a 50% gray.
From this point on, triplets of numbers (again in ASCII decimal) represent RGB values. The following sample is from the ppm man page:
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
This produces a 4x4 image with
magenta in the outer upper right and outer lower left and a lime green in the inner upper left and lower right. All of the other spots are black.
Comments may be added anywhere in the file and are designated by a '#' which causes the parser to ignore that point to the end of the line.
While its not required, it is recommended that no line be longer than 70 characters. This likely dates back to the days of the punch cards (80 col wide) allowing for 70 characters, a space and a '#' sign and then the 8 characters for comments. These 8 characters would most often be used for sequence information in case a deck got shuffled. As Fortran also used the last 8 characters of a line for comments most anyone who used punch cards had a program to sort a deck by values in the last 8 bytes on the card.
The astute reader will recognize that there are two entries for 'image/x-portable-pixmap' in the magic number file above. This is because there are two formats. The second one is a bit smaller and faster to work with than the ASCII form that was described above and uses raw bytes for the image map rather than decimal values. This does pose the limitation that the maximum value for a particular color is limited to (and often set at) 255. If the RAWBITS format is used, after the newline for the maximum value for a color, you've got raw data as bytes. Instead of '0' you have '0x00' and instead of '255' the value is '0xFF'. This results from a 2x decrease in (' 0' (2 bytes) to '0x00' (1 byte)) to a 4x decrease in the space used (' 255' (4 bytes) to '0xFF' (1 byte)). This will be more if more whitespace is used for easier human reading (as above).
So, why have it at all? This file format is not intended to be used for anything other than an intermediary between two other graphics file formats. The reason it has come about is for simplification in the world of conversion graphics files. Consider 20 file formats (thats the number listed in the ppm man page). You want to convert between these formats. If one was to write one program for each pair (or worse - two programs, one for each direction of translation) you'd have 380 (20 * 19) programs. Instead, there are 20 programs tiled '???toppm' and another 20 titled 'ppmto???'. Each one of these programs translates to or from the ppm file format and one other file format. Each of these programs is easier to write and debug than any one of the 380 ones that are not written.