Algorithms: Overview of the PGM Format

Representation of images

Every black-and-white picture in the real world is a continuous function of brightness values. For every point of a picture, we may determine the corresponding gray-level value, which shows the brightness of this particular point. When storing an image in a computer, we specify the brightness for a finite number of selected points, thus approximating the real picture; the points that form a digitized image are called pixels. Scanners and digital cameras automatically convert a continuous picture into a set of points. The simplest technique to select points is to use a regular square grid: a scanner overlays a uniform grid on a given image and determines the brightness at the center of each square. The gray-level values are usually represented by integer numbers, ranging from 0 for black to 255 for white.

Storage formats

When digitizing an image, we have a choice among several standard formats, which involve storage of the pixel values along with auxiliary information about the image. For the programming projects, we use the PGM format, which stands for Portable Gray Map. The following example is a simplified PGM file:
P2
123 74
255
111 114 118 119 118 105 85 71 73 72 66 69  80 89 92 90 92
 93 82 78 92 92 95 100 104 107 108 109 109 112 108 100 92 84
73 60 50 50 54 54 46 39 42 57 72 86 87 90 94 99 105
109 112 115 113 108 101 92 87 89 93 100 102 91 76 68 55 48
56 72 76 76 66 54 53 67 81 94 78 56 42 48 68 85 92
96 97 104 114 115 100 79 64 60 61 66 77 84 78 60 45 49
42 50 72 83 71 55 49 42 41 39 40 43 43 35 24 45 49
54 57 58 58 110 114 122 124 121 97 72 53 52 53 49 52 62
The first line specifies the type of encoding: P2 or P5 means PGM. The two numbers in the second line are the width and height of the image, in pixels. The third line shows the maximal number of gray levels; in this case, the number of levels is 255. The other values are the gray levels of pixels, and their total number is (width * height); thus, the above example shows only a beginning of a large file. If the file begins with P2, then the gray levels are encoded by ASCII numbers, whereas P5 means that these levels are byte codes. The pixels are stored in the row-scanned order, starting from the top left corner of the image; thus, the first 123 pixels in the above example form the first row of the image, the next 123 pixels are the second row, and so on.

Format conversion

To view a PGM image, you need to convert it to a different format. You may use the Unix convert command, which can generate a variety of viewable and printable formats, including the following:
 
convert file.pgm file.pcx
.
  You can view a PCX file using xv in Unix or Paintbrush on a PC;
in addition, you may import PCX into Microsoft Word or WordPerfect.
convert file.pgm file.gif You can use xv in Unix to view GIF; you can also use Netscape or Internet Explorer.
convert file.pgm file.ps The result is a PostScript file, which can be sent to a printer on a Unix machine.
Back to the Projects page