ppm P3 Image File Format

The ppm P3 format is a text-based format (meaning that it is readable text) that defines an image as a sequence of pixels beginning with the top-left pixel and stored in row order (i.e., every pixel in a row is stored in left-to-right order and before any pixel in the next row).


A file conforming to the ppm P3 format begins with header information. The header consists of the characters P3, the integer width of the image (in pixels), the integer height of the image, and the maximum value for a color component (we will use 255 for this value).

Immediately following the header is the color information for each pixel. A pixel's color is represented by three integers denoting the red, green, and blue components (in that order).

The following example shows how a file would look if it contained an image with one blue pixel, one red pixel, and one green pixel. In this example the pixel color components are all on the same line, but the only requirement is that they be separated by whitespace. You may see some files with each of the integers on a separate line.

3 1
0 0 255 255 0 0 0 255 0

The top-left pixel is said to be at location <0, 0> and the bottom-right at location <width - 1, height - 1>.

Color Components

As noted above, the color components of each pixel are integer values in the range [0, max_color] (255, for our purposes). In some programs, however, the value for a color component is stored as a double from [0.0, 1.0]. Before printing, this value must be converted to a corresponding integer in the range by multiplying and then truncating any fractional component.

In addition, if for some reason the double value is greater than 1.0, the output integer value must be capped at max_color. You will need to take this into account before printing the value of the color component.


On the lab machines, you can view ppm files with either eog or gthumb (among others). On your personal machine, you will need a viewer appropriate for your platform. You may consider IrfanView, ToyViewer, or gimp.