A Short Primer on Creating, Reformatting, and Displaying Images
There are a large number of image formats in use today (e.g. tiff, gif,
jpeg, bmp, etc.) For this course, we will use a simple standard format
called a portable gray map (pgm) for gray-scale images and a portable
pix map (ppm) for color images. In addition we will use the Linux
utility image viewer called gimp.
There are several good reasons for this:
1. gimp will display pgm and ppm files.
2. gimp can be used to convert most other file formats to and from
pgm or ppm format.
3. pgm and ppm image files are easy to create and modify with a
user written C++ program.
PGM/PPM FORMAT
PGM is used for gray-scale images, PPM is used for color images.
Each image file begins with one of the following:
P2 (gray scale ascii)
P3 (color ascii)
P5 (gray scale raw) --- this is what we will use
P6 (color raw) --- this is what we will use
followed by a newline.
The next line may be a comment beginning with # and ending with a newline.
The next entry in either file type is the width and height of the
image in ascii decimal followed by a newline.
The next entry in either file is the maximum intensity value
(usually 255) in ascii decimal followed by a newline.
The remainder of the file is the list of gray-scale values or rgb
values for the pixels. In ascii mode (P2 or P3) these values are
expressed as integers (0-255) separated by a space no more than
70 to a line. In raw mode these values are expressed as unsigned
characters with no separating spaces. Note that if you want to store
the color 255 255 255 in ascii mode, it requires 12 bytes of memory,
but only 3 bytes in raw mode.
If you want to look at the contents of a pgm or ppm image file (not
display the image) use the Unix utility od (octal dump) as follows:
od -c myimage.ppm | more
This will show each character in the file, so you can make sure you
are creating your file correctly.
READING AND WRITING PGM/PPM FILES IN A C++ PROGRAM
Suppose your image file myimage.pgm looks like:
P5
# Hi Mom!
200 200
255
g1g2g3g4.....
where g1,g2, ... are gray-scale bytes.
You can read a pgm file in a C++ program: readpgm.cpp
You can create a pgm file in a C++ program: makepgm.cpp
CONVERTING FROM OTHER IMAGE FILE FORMATS
If you have obtained an image in one of the following file formats:
gif, tiff, X11, xpm, bmp, jpeg, Iris rgb, Sun rasterfile
you can convert it to pgm or ppm by viewing in gimp and then saving it
in pgm or ppm format.