pgm.html 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.3//EN">
  2. <html>
  3. <head>
  4. <title>PGM Format Specification</title>
  5. <meta name="manual_section" content="5">
  6. </head>
  7. <body>
  8. <h1>pgm</h1>
  9. Updated: 09 October 2016
  10. <br>
  11. <a href="#index">Table Of Contents</a>
  12. <h2>NAME</h2>
  13. pgm - Netpbm grayscale image format
  14. <h2 id="description">DESCRIPTION</h2>
  15. <p>This program is part of <a href="index.html">Netpbm</a>.
  16. <p>The PGM format is a lowest common denominator grayscale file format.
  17. It is designed to be extremely easy to learn and write programs for.
  18. (It's so simple that most people will simply reverse engineer it
  19. because it's easier than reading this specification).
  20. <p>A PGM image represents a grayscale graphic image. There are many
  21. pseudo-PGM formats in use where everything is as specified herein except
  22. for the meaning of individual pixel values. For most purposes, a PGM
  23. image can just be thought of an array of arbitrary integers, and all the
  24. programs in the world that think they're processing a grayscale image
  25. can easily be tricked into processing something else.
  26. <p>The name "PGM" is an acronym derived from "Portable Gray Map."
  27. <p>One official variant of PGM is the transparency mask. A transparency
  28. mask in Netpbm is represented by a PGM image, except that in place of
  29. pixel intensities, there are opaqueness values. See below.
  30. <h2 id="format">THE FORMAT</h2>
  31. <p>The format definition is as follows. You can use the <a
  32. href="libnetpbm.html">libnetpbm</a> C subroutine library to conveniently
  33. and accurately read and interpret the format.
  34. <p>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. <p>Each PGM image consists of the following:
  37. <ol>
  38. <li>A "magic number" for identifying the file type.
  39. A pgm image's magic number is the two characters "P5".
  40. <li>Whitespace (blanks, TABs, CRs, LFs).
  41. <li>A width, formatted as ASCII characters in decimal.
  42. <li>Whitespace.
  43. <li>A height, again in ASCII decimal.
  44. <li>Whitespace.
  45. <li>The maximum gray value (Maxval), again in ASCII decimal. Must be less
  46. than 65536, and more than zero.
  47. <li>A single whitespace character (usually a newline).
  48. <li>A raster of Height rows, in order from top to bottom. Each row
  49. consists of Width gray values, in order from left to right. Each gray
  50. value is a number from 0 through Maxval, with 0 being black and Maxval
  51. being white. Each gray value is represented in pure binary by either
  52. 1 or 2 bytes. If the Maxval is less than 256, it is 1 byte.
  53. Otherwise, it is 2 bytes. The most significant byte is first.
  54. <p>A row of an image is horizontal. A column is vertical. The pixels
  55. in the image are square and contiguous.
  56. <p>Each gray value is a number proportional to the intensity of the
  57. pixel, adjusted by the ITU-R Recommendation BT.709 gamma transfer
  58. function. (That transfer function specifies a gamma number of 2.2 and
  59. has a linear section for small intensities). A value of zero is
  60. therefore black. A value of Maxval represents CIE D65 white and the
  61. most intense value in the image and any other image to which the image
  62. might be compared.
  63. <p>BT.709's range of channel values (16-240) is irrelevant to PGM.
  64. <p>Note that a common variation from the PGM format is to have the
  65. gray value be "linear," i.e. as specified above except
  66. without the gamma adjustment. <b>pnmgamma</b> takes such a PGM
  67. variant as input and produces a true PGM as output.
  68. <p>Another popular variation from PGM is to substitute the newer sRGB transfer
  69. function for the BT.709 one. You can use <b>pnmgamma</b> to convert between
  70. this variation and true PGM.
  71. <p>In the transparency mask variation from PGM, the value represents
  72. opaqueness. It is proportional to the fraction of intensity of a
  73. pixel that would show in place of an underlying pixel. So what
  74. normally means white represents total opaqueness and what normally
  75. means black represents total transparency. In between, you would
  76. compute the intensity of a composite pixel of an "under" and
  77. "over" pixel as under * (1-(alpha/alpha_maxval)) + over *
  78. (alpha/alpha_maxval). Note that there is no gamma transfer function
  79. in the transparency mask.
  80. </ol>
  81. <p>Strings starting with "#" may be comments, the same as
  82. with <a href="pbm.html">PBM</a>.
  83. <p>Note that you can use <b>pamdepth</b> to convert between a the
  84. format with 1 byte per gray value and the one with 2 bytes per gray
  85. value.
  86. <p>All characters referred to herein are encoded in ASCII.
  87. "newline" refers to the character known in ASCII as Line
  88. Feed or LF. A "white space" character is space, CR, LF,
  89. TAB, VT, or FF (I.e. what the ANSI standard C isspace() function
  90. calls white space).
  91. <h3 id="plainpgm">Plain PGM</h3>
  92. <p>There is actually another version of the PGM format that is fairly
  93. rare: "plain" PGM format. The format above, which generally
  94. considered the normal one, is known as the "raw" PGM format.
  95. See <b><a href="pbm.html">pbm</a></b> for some commentary on how plain
  96. and raw formats relate to one another and how to use them.
  97. <p>The difference in the plain format is:
  98. <ul>
  99. <li>
  100. There is exactly one image in a file.
  101. <li>
  102. The magic number is P2 instead of P5.
  103. <li>
  104. Each pixel in the raster is represented as an ASCII decimal number
  105. (of arbitrary size).
  106. <li>
  107. Each pixel in the raster has white space before and after it. There must
  108. be at least one character of white space between any two pixels, but there
  109. is no maximum.
  110. <li>
  111. No line should be longer than 70 characters.
  112. </ul>
  113. <p>Here is an example of a small image in the plain PGM format.
  114. <pre>
  115. P2
  116. # feep.pgm
  117. 24 7
  118. 15
  119. 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  120. 0 3 3 3 3 0 0 7 7 7 7 0 0 11 11 11 11 0 0 15 15 15 15 0
  121. 0 3 0 0 0 0 0 7 0 0 0 0 0 11 0 0 0 0 0 15 0 0 15 0
  122. 0 3 3 3 0 0 0 7 7 7 0 0 0 11 11 11 0 0 0 15 15 15 15 0
  123. 0 3 0 0 0 0 0 7 0 0 0 0 0 11 0 0 0 0 0 15 0 0 0 0
  124. 0 3 0 0 0 0 0 7 7 7 7 0 0 11 11 11 11 0 0 15 0 0 0 0
  125. 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  126. </pre>
  127. <p>There is a newline character at the end of each of these lines.
  128. <p>Programs that read this format should be as lenient as possible,
  129. accepting anything that looks remotely like a PGM.
  130. <h2 id="internetmediatype">INTERNET MEDIA TYPE</h2>
  131. <p>No Internet Media Type (aka MIME type, content type) for PGM has been
  132. registered with IANA, but the value <code>image/x-portable-graymap</code>
  133. is conventional.
  134. <p>Note that the PNM Internet Media Type <code>image/x-portable-anymap</code>
  135. also applies.
  136. <h2 id="filename">FILE NAME</h2>
  137. <p>There are no requirements on the name of a PGM file, but the convention is
  138. to use the suffix ".pgm". "pnm" is also conventional, for
  139. cases where distinguishing between the particular subformats of PNM is not
  140. convenient.
  141. <h2 id="compatibility">COMPATIBILITY</h2>
  142. <p>
  143. Before April 2000, a raw format PGM file could not have a maxval greater
  144. than 255. Hence, it could not have more than one byte per sample. Old
  145. programs may depend on this.
  146. <p>
  147. Before July 2000, there could be at most one image in a PGM file. As
  148. a result, most tools to process PGM files ignore (and don't read) any
  149. data after the first image.
  150. <h2 id="seealso">SEE ALSO</h2>
  151. <a href="pnm.html">pnm</a>,
  152. <a href="pbm.html">pbm</a>,
  153. <a href="ppm.html">ppm</a>,
  154. <a href="pam.html">pam</a>,
  155. <a href="libnetpbm.html">libnetpbm</a>,
  156. <a href="directory.html">programs that process PGM</a>,
  157. <h2 id="author">AUTHOR</h2>
  158. Copyright (C) 1989, 1991 by Jef Poskanzer.
  159. <hr>
  160. <h2 id="index">Table Of Contents</h2>
  161. <ul>
  162. <li><a href="#description">DESCRIPTION</a>
  163. <li><a href="#format">THE FORMAT</a>
  164. <ul>
  165. <li><a href="#plainpgm">Plain PGM</a>
  166. </ul>
  167. <li><a href="#internetmediatype">INTERNET MEDIA TYPE</a>
  168. <li><a href="#filename">FILE NAME</a>
  169. <li><a href="#compatibility">COMPATIBILITY</a>
  170. <li><a href="#seealso">SEE ALSO</a>
  171. <li><a href="#author">AUTHOR</a>
  172. </ul>
  173. </body>
  174. </html>