PPM is a format for storing images. It stands for Portable Pix (or pixel) Map. Here is a formal definition.
The format has several variations which allow for color, for gray scale and for black/white. It also allows for ASCII format or binary format. We will use the color ASCII format, as being very easy to read and deal with in a program.
The whole file is purely a text file. You could read it in Notepad or Visual Studio or even MS Word. Give it a little time to load in; they tend to be pretty big.
Here's the format:
The upper left hand corner of the picture is considered row 0, column 0 and proceed from there. Why is the number of numbers R * C * THREE? Because each pixel is represented by three numbers, one for the Red component of the color, one for the Green component and one for the Blue. That is a very common way to store colors in picture files. They actually represent signals to the three electron guns (if you think of an old CRT) that fire for the three colors. The three numbers can each vary from zero up to the maximum number that was at the top of the file, in most cases 0-255. 0 0 0 is black, 255 255 255 is white. So the first three numbers represent the pixel at row 0, column 0. The next three numbers represent the pixel at row 0, column 1. The next three are for row 0, column 2.
Another kind of line which _may_ appear in a ppm file is a comment line, which will start with a #. If a line starts with a #, the rest of that line, up to the newline character, is a comment. We will omit these from our files, to simplify the problem for now.
How do you view ppm files? They are not a common type; MS Paint will not open them, nor the Windows Media Player. There is a very good viewer available called IrfanView. This is freeware for personal or educational use. You can find it at www.irfanview.com or many other download sites. This is a program that will not only view ppm files; you can use it to create a ppm file from any other format that it reads. It reads a LOT of formats. Hint: you do not have to view the files as images in this program; just looking at the numbers in the files will tell you whether you have the insertion correct.
To create a ppm file from some other format using Irfanview:
From the File menu, go to the Batch conversion/Rename choice,
choose the Output format of PPM-Portable Pixelmap,
on the Options button next to that, choose ASCII encoding.
Choose whatever output directory you want for your result files.
Browse (at the Look in window) to find your picture that you want
to convert. Select it and click on the Add button, then the
Start Batch button. It doesn't take long at all.
It will not overwrite files of the same name.
Note also that you can view the numbers in a ppm file just by loading into an editor like Notepad. Just be patient if you load a large file - Notepad is slow.
Why isn't this format used widely? the main reason is that it is terribly INefficient. A picture of a few hundred thousand pixels can take 1.5 megabytes to store. Interesting fact - because the file is just text and very repetitive, zipping it reduces its size by a factor of 90% sometimes. 1.5 megabytes becomes 50 kilobytes. Why is it used at all if it is that inefficient? it is used quite a bit as an _intermediate_ format. Utilities (especially in UNIX systems) convert one graphic image format to ppm, then from ppm to another format. Normally the ppm files are not stored for long periods of time, just long enough to do the conversion.
Try some of the samples provided. Load them into Notepad and look at them as text. Get IrfanView and load in the samples to see what they look like as pictures. Try converting your favorite picture from JPG (or other format) to ppm. How much does it change in size? Does it look the same to the naked eye as the original format? Use Paint to make a small jpg with some simple color blocks in it. Convert it to ppm, then look at the resulting file. Can you tell which block corresponds to which numbers?
Reminder- you cannot click on these links and have Windows pull up the image! You will have to save them and then open with irfanview if you want to see the image.