1PGM Format Specification(5) File Formats Manual PGM Format Specification(5)
2
3
4
6 pgm - Netpbm grayscale image format
7
8
10 This program is part of Netpbm(1).
11
12 The PGM format is a lowest common denominator grayscale file format.
13 It is designed to be extremely easy to learn and write programs for.
14 (It's so simple that most people will simply reverse engineer it
15 because it's easier than reading this specification).
16
17 A PGM image represents a grayscale graphic image. There are many
18 psueudo-PGM formats in use where everything is as specified herein
19 except for the meaning of individual pixel values. For most purposes,
20 a PGM image can just be thought of an array of arbitrary integers, and
21 all the programs in the world that think they're processing a grayscale
22 image can easily be tricked into processing something else.
23
24 The name "PGM" is an acronym derived from "Portable Gray Map."
25
26 One official variant of PGM is the transparency mask. A transparency
27 mask in Netpbm is represented by a PGM image, except that in place of
28 pixel intensities, there are opaqueness values. See below.
29
30 The format definition is as follows. You can use the libnetpbm(1)Csub‐
31 routinelibrarytoconveniently and accurately read and interpret the for‐
32 mat.
33
34 A PGM file consists of a sequence of one or more PGM images. There are
35 no data, delimiters, or padding before, after, or between images.
36
37 Each PGM image consists of the following:
38
39
40
41
42 · A 'magic number' for identifying the file type. A pgm image's
43 magic number is the two characters 'P5'.
44
45
46 · Whitespace (blanks, TABs, CRs, LFs).
47
48
49 · A width, formatted as ASCII characters in decimal.
50
51
52 · Whitespace.
53
54
55 · A height, again in ASCII decimal.
56
57
58 · Whitespace.
59
60
61 · The maximum gray value (Maxval), again in ASCII decimal. Must
62 be less than 65536, and more than zero.
63
64
65 · A single whitespace character (usually a newline).
66
67
68 · A raster of Height rows, in order from top to bottom. Each row
69 consists of Width gray values, in order from left to right.
70 Each gray value is a number from 0 through Maxval, with 0 being
71 black and Maxval being white. Each gray value is represented in
72 pure binary by either 1 or 2 bytes. If the Maxval is less than
73 256, it is 1 byte. Otherwise, it is 2 bytes. The most signifi‐
74 cant byte is first.
75
76 A row of an image is horizontal. A column is vertical. The
77 pixels in the image are square and contiguous.
78
79
80 · Each gray value is a number proportional to the intensity of the
81 pixel, adjusted by the ITU-R Recommendation BT.709 gamma trans‐
82 fer function. (That transfer function specifies a gamma number
83 of 2.2 and has a linear section for small intensities). A value
84 of zero is therefore black. A value of Maxval represents CIE
85 D65 white and the most intense value in the image and any other
86 image to which the image might be compared.
87
88
89 · Note that a common variation on the PGM format is to have the
90 gray value be 'linear,' i.e. as specified above except without
91 the gamma adjustment. pnmgamma takes such a PGM variant as
92 input and produces a true PGM as output.
93
94
95 · In the transparency mask variation on PGM, the value represents
96 opaqueness. It is proportional to the fraction of intensity of
97 a pixel that would show in place of an underlying pixel. So
98 what normally means white represents total opaqueness and what
99 normally means black represents total transparency. In between,
100 you would compute the intensity of a composite pixel of an
101 'under' and 'over' pixel as under * (1-(alpha/alpha_maxval)) +
102 over * (alpha/alpha_maxval). Note that there is no gamma trans‐
103 fer function in the transparency mask.
104
105
106 · Strings starting with '#' may be comments, the same as with
107 PBM(1).
108
109
110
111 Note that you can use pamdepth to convert between a the format with 1
112 byte per gray value and the one with 2 bytes per gray value.
113
114 There is actually another version of the PGM format that is fairly
115 rare: 'plain' PGM format. The format above, which generally considered
116 the normal one, is known as the 'raw' PGM format. See pbm(1)for‐
117 somecommentaryonhowplain and raw formats relate to one another and how
118 to use them.
119
120 The difference in the plain format is:
121
122
123
124 - There is exactly one image in a file.
125
126 - The magic number is P2 instead of P5.
127
128 - Each pixel in the raster is represented as an ASCII decimal num‐
129 ber (of arbitrary size).
130
131 - Each pixel in the raster has white space before and after it.
132 There must be at least one character of white space between any
133 two pixels, but there is no maximum.
134
135 - No line should be longer than 70 characters.
136
137
138 Here is an example of a small image in the plain PGM format.
139 P2
140 # feep.pgm
141 24 7
142 15
143 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
144 0 3 3 3 3 0 0 7 7 7 7 0 0 11 11 11 11 0 0 15 15 15 15 0
145 0 3 0 0 0 0 0 7 0 0 0 0 0 11 0 0 0 0 0 15 0 0 15 0
146 0 3 3 3 0 0 0 7 7 7 0 0 0 11 11 11 0 0 0 15 15 15 15 0
147 0 3 0 0 0 0 0 7 0 0 0 0 0 11 0 0 0 0 0 15 0 0 0 0
148 0 3 0 0 0 0 0 7 7 7 7 0 0 11 11 11 11 0 0 15 0 0 0 0
149 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
150
151 There is a newline character at the end of each of these lines.
152
153 Programs that read this format should be as lenient as possible,
154 accepting anything that looks remotely like a PGM.
155
156 All characters referred to herein are encoded in ASCII. 'newline'
157 refers the the character known in ASCII as Line Feed or LF. A 'white
158 space' character is space, CR, LF, TAB, VT, or FF (I.e. what the ANSI
159 standard C isspace() function calls white space).
160
161
162
164 Before April 2000, a raw format PGM file could not have a maxval
165 greater than 255. Hence, it could not have more than one byte per sam‐
166 ple. Old programs may depend on this.
167
168 Before July 2000, there could be at most one image in a PGM file. As a
169 result, most tools to process PGM files ignore (and don't read) any
170 data after the first image.
171
172
174 pnm(1), pbm(1), ppm(1), pam(1), libnetpbm(1), programsthatpro‐
175 cessPGM[1m(1),
176
177
179 Copyright (C) 1989, 1991 by Jef Poskanzer.
180
181
182
183netpbm documentation 03 October 2003 PGM Format Specification(5)