PPM Image format

Chapter: PPM Image format

PPM stands for Portable Pix Map or Portable Pixel Map. This format is a convenient and simple way to save image data. It is true that .gif, .png and .jpg are more commonly found. However, you can use many good image manipulation programs to convert between the various formats. The more common formats use data compression to reduce file sizes. .ppm is a very simple format that allows very easy reading and writing of image files.

I would like you to spend some time playing with your image manipulation program. Import photos that you like, or download them from the web. Try to do cool things to them. Save them in different formats.

Now let's work with a simple boring file. Copy my simple.ppm and save it in your lab 7 folder. Open it with emacs. If you don't see an image, talk to me or Catherine right away.

As an image it's pretty boring. However, you can view it as a binary file (consisting of bytes expressed in hexadecimal) if you switch to hexl mode. An easy way to do this is to type ESC followed by x, and then in the bottom window type "hexl-mode".

Your buffer should change to something like:

The first, greyed out, line is a header to help you count column positions.

87654321 are used for a line number

0011 2233 4455 8899 . . . aabb ccdd eeff label the bytes on that line

To the right 01234567891bcdef also label the bytes on the line, but in the columns below, emacs will try to "decode" the bytes.

The next line is P6. It defines the image format. P6 means PPM (full color, binary encoding). The corresponding hexadecimal 5036 reflects the fact that the ASCII code for 'P' is 50 and for '6' is 36.

Then come 200 and 100 separated by a space. These specify the number of columns and the number of rows in the image. My file has 200 columns and 100 rows. Notice that '200' is 323030 in ASCII, spcae is 20, and '100' is 313030.

The 255 indicates the largest saturation value that will be used in this file for a color component. Thus the maximum amount of red is 255; as is the maximum amount of green, and the maximum amount of blue. Up until recently, any value other than 255 for the maximum color value would not have been acceptable. Indeed, a lot of older programs will still reject larger values.

After that come triples of bytes representing red, green and blue color components for successive pixels in the 100X200 raster. The first pixel has d2 (that's hex for 210) amount of red, b4 (hex for 180) of green, and 28 (that's 40 in decimal) of blue. If you type "ESC-x image-mode" to get the image back, you will see that this is a somewhat dirty yellow color.

Altogether there will be 20,000 such triples. Most of them are d2b428 for that dull yellow. However, some of them are 1428fa corresponding to a little bit (14 hex = 20 decimal) of red, a modicum (28 hex = 40 decimal) of green and a lot (fa hex = 250 decimal) of blue. And you can see that stripe when you view the image. Don't forget:

"ESC-x hexl-mode" to see the binary

"ESC-x image-mode" to see the image.

You can also use CTRL-c CTRL-c to toggle between the various modes.


Exercise 2

This exercise is solo. Get a photo of yourself, load it into an image manipulation program, and save it as a .ppm file. Name it me.ppm. Now load your image into emacs. Most likely, it will just appear as a picture in the emacs window.

Toggle between image-mode and hexl-mode and see if you can make sense of what you see in the first few lines of hexl-mode. Explain it in your lab notebook.



rpricejones@wooster.edu