Eric Weeks - personal pages - graphics techniques

Using PPM to make high-quality color pictures

including a C program which produces PPM output
[home] [research]
[software] [pics]
[misc] [about me]

[a fractal picture]

PPM stands for "Portable Pixel Map." This format can be used to make wonderful color pictures (and black & white). Similar to Sec. 2, PPM is used when you have a program to generate a rectangular picture, and you want to specific the color of each pixel. With PPM you can specific the exact color of each pixel.

How to do it: at the top of your PPM file it should say:

# comment line -- whatever you want
100 200

The top line (P3) specifies this is a PPM ASCII file; the second line can say whatever you want. The third line specifies the width and height of the picture. In this case 200 is the height. The fourth line always stays the same; it is the maximum intensity of your picture (so thus could be different from 255, although 255 is probably what you want to use). Thus you're using 0-255 to specify the Red, Green, and Blue levels.

After this header, you provide the data. This is just ASCII text specifying Red Green Blue values in order for each pixel. You do not need anything special at the end of the file. You can put carriage returns anywhere in the data you want.

255 255 255 is white; 0 0 0 is black; 255 0 0 is red; 0 255 0 is green; 0 0 255 is blue.

If you want to make a smaller file, you can specify the data in binary format. In this case change the P3 to P6, leave the rest of the header the same, and then do your output in binary format. For example, in C:

int x,y; int width,height,red,green,blue; printf("P6\n"); printf("# created by Eric R. Weeks using a C program\n"); printf("%d %d\n",width,height); printf("255\n"); /* Do this for the whole picture now */ for (y = 0;y < height;y++) { for (x = 0;x < width;x++) { . . . fputc((char)red,stdout); fputc((char)green,stdout); fputc((char)blue,stdout); } }

The data is in the file in row order: top column first, scanning each row left to right. This method is very easy to put into existing programs.

NOTE: I wrote a program called "a2ppm" which works exactly like "a2ps" (see Sec. 2 above). The input format is exactly the same, and the output is PPM. Click here if you'd like a copy of this program. To compile, use

cc -o a2ppm a2ppm.c

On Linux computers, "man ppm" will give you information about the PPM format. I don't know about other unix machines. To be honest, there's a lot more to the true PPM specification than what I've got on this webpage, but I believe at least everything I've said here is correct.

Black and White pictures

To make black and white pictures using this format, you must change the following things: At the top of the file use "P2" rather than "P3" (for ASCII data) or "P5" rather than "P6" (for binary data). Each pixel color is specified by only one number (or one byte) rather than three. This format is often called PGM (portable grey map?).

About the image:

This is a portion of a Julia set created using PPM output from a C program. For more information click here.


Current address:
Eric R. Weeks
weeks(circle a)
Department of Physics
Emory University
Atlanta, GA 30322-2430