png_optimization.html 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  2. "http://www.w3.org/TR/html4/loose.dtd">
  3. <html>
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  6. <meta name="Author" content="Cosmin Truţa">
  7. <title>A guide to PNG optimization</title>
  8. </head>
  9. <body>
  10. <h2>A guide to PNG optimization</h2>
  11. <h3>1. Background</h3>
  12. <h4>1.1 The PNG file format</h4>
  13. <p>
  14. The <a href="http://www.libpng.org/pub/png/">Portable Network Graphics</a>
  15. (<b><i>PNG</i></b>) is a format for storing compressed raster graphics. The
  16. compression engine is based on the <b><i>Deflate</i></b> method
  17. [<a href="http://www.ietf.org/rfc/rfc1951">RFC1951</a>],
  18. designed by
  19. <a href="http://www.pkware.com/">PKWare</a>
  20. and originally used in <b>PKZIP</b>.
  21. </p>
  22. <p>
  23. The PNG format is defined by the
  24. <a href="http://www.libpng.org/pub/png/spec/">PNG Specification</a>.
  25. This specification was developed by an ad-hoc group named the
  26. <a href="http://www.libpng.org/pub/png/">PNG Development Group</a>, and it is
  27. both an International Standard (published under the formal name
  28. ISO/IEC&nbsp;15948) and a W3C Recommendation.
  29. </p>
  30. <p>
  31. PNG was initially intended as a superior, patent-free replacement of GIF. The
  32. final outcome is a modern, extensible, reliable image format, capable to handle
  33. an impressive number of image types (from 1-bit black-and-white images up to
  34. 48-bit RGB images with a full 16-bit alpha channel), and geared by a
  35. significantly stronger lossless compression engine (typically 5-25% better than
  36. GIF).
  37. </p>
  38. <p>
  39. Unlike other lossless compression schemes, PNG compression does not depend
  40. solely on the statistics of the input, but it may vary within wide limits,
  41. depending on the compressor's implementation. A good PNG encoder must be
  42. able to take informed decisions about the factors that affect the size of the
  43. output. The purpose of this article is to provide information about these
  44. factors, and to give advice on implementing efficient PNG encoders.
  45. </p>
  46. <h4>1.2 The PNG compression</h4>
  47. <p>
  48. The PNG compression works in a pipeline manner.
  49. </p>
  50. <p>
  51. In the first stage, the image pixels are passed through a lossless arithmetic
  52. transformation named <b><i>delta filtering</i></b>, or simply
  53. <b><i>filtering</i></b>, and sent further as a (filtered) byte sequence.
  54. Filtering does not compress or otherwise reduce the size of the data, but it
  55. makes the data more compressible.
  56. </p>
  57. <p>
  58. In the second stage, the filtered byte sequence is passed through the
  59. Ziv-Lempel algorithm (LZ77), producing LZ77 codes that are further compressed
  60. by the Huffman algorithm in the third and final stage. The combination of the
  61. last two stages is referred to as the <b><i>Deflate compression</i></b>, a
  62. widely-used, patent-free algorithm for universal, lossless data compression.
  63. The maximum size of the LZ77 sliding window in Deflate is 32768 bytes, and the
  64. LZ77 matches can be between 3 and 258 bytes long.
  65. </p>
  66. <p>
  67. A complete description of the PNG compression is beyond the scope of this
  68. guide. The PNG Specification describes the format completely, and provides
  69. a complete list of references to the underlying technologies.
  70. </p>
  71. <h3>2. Factors that affect the PNG file size</h3>
  72. <div>
  73. Like any other compression scheme, PNG compression depends on the statistics
  74. of the input data. In addition, it depends on the following PNG-specific
  75. parameters:
  76. </div>
  77. <ol>
  78. <li>
  79. The PNG image type
  80. </li>
  81. <li>
  82. The PNG delta filters
  83. </li>
  84. <li>
  85. The strategy of searching LZ77 matches
  86. </li>
  87. <li>
  88. The size of the Huffman buffers inside the Deflate encoder
  89. </li>
  90. </ol>
  91. <p>
  92. Depending on how these parameters are chosen by the implementation, PNG
  93. compression may vary within wide limits. The process of selecting the best
  94. configuration is computationally infeasible, but heuristics to select a
  95. satisfactory configuration are available. The problem of improving these
  96. heuristics constitutes an interesting subject for research.
  97. </p>
  98. <h4>2.1 The PNG image type</h4>
  99. <p>
  100. The type of a PNG image is defined in the <b><code>IHDR</code></b> image
  101. header. The image has a certain bit depth, up to 16 bits per sample, and a
  102. certain color type, from Grayscale to RGB+Alpha. If two PNG files of different
  103. types represent exactly the same image, each file can be regarded as a lossless
  104. transformation of the other. A lossless transformation can reduce the
  105. <i>uncompressed</i> stream, and such a transformation is named <b><i>image
  106. reduction</i></b>. In most cases, image reductions are capable of reducing the
  107. <i>compressed</i> stream (which is, in fact, our interest), as an indirect
  108. effect of reducing the size of the compressor's input.
  109. </p>
  110. <div>
  111. The possible image reductions are:
  112. </div>
  113. <ul>
  114. <li>
  115. <i>Bit depth reduction</i>
  116. <br>
  117. The bit depth can be reduced to a minimum value that is acceptable for all
  118. samples. For example, if all sample values in a 16-bit image have the form
  119. (256+1)*<i>n</i>, (e.g. #0000, #2323, #FFFF), then the bit depth can be
  120. reduced to 8, and the new sample values will become <i>n</i>, (e.g. #00, #23,
  121. #FF).
  122. </li>
  123. <li>
  124. <i>Color type reduction</i>
  125. <br>
  126. - If an RGB image has 256 distinct colors or less, it can be reencoded as a
  127. Palette image.
  128. <br>
  129. - If an RGB or Palette image has only gray pixels, it can be reencoded as
  130. Grayscale.
  131. <br>
  132. A color type reduction can also enable a bit depth reduction.
  133. </li>
  134. <li>
  135. <i>Color palette reduction</i>
  136. <br>
  137. If the color palette contains redundant entries (i.e. duplicate entries that
  138. indicate the same RGB value) or sterile entries (i.e. entries that do not
  139. have a correspondent in the raw pixel data), these entries can be removed.
  140. <br>
  141. A color palette reduction can also enable a bit depth reduction.
  142. </li>
  143. <li>
  144. <i>Alpha channel reduction</i>
  145. <br>
  146. If all pixels in a Grayscale+Alpha or an RGB+Alpha image are fully opaque
  147. (i.e. all alpha components are equal to 2^<sup>bitdepth</sup>-1), or if the
  148. transparency information can be stored entirely in a (much cheaper)
  149. <b><code>tRNS</code></b> chunk, the alpha channel can be stripped.
  150. </li>
  151. </ul>
  152. <p>
  153. There are, however, a few cases when some image type reductions do not
  154. necessarily lead to the reduction of the compressed stream. The
  155. <a href="http://www.cs.toronto.edu/~cosmin/pngtech/">PNG-Tech</a> site contains
  156. experimental analyses of these possibilities; for example, see the article
  157. <a href="8bpp.html">8 bits per pixel in paletted images</a>.
  158. </p>
  159. <p>
  160. Interlacing, useful for a faster, progressive rendering, is another component
  161. of the PNG image type that affects compression. In an interlaced stream, the
  162. samples corresponding to neighboring pixels are stored far away, hence the data
  163. in it is less correlated and less compressible. Unlike JPEG, where interlacing
  164. may improve the compression slightly, the PNG interlacing degrades the
  165. compression significantly.
  166. </p>
  167. <h4>2.2 The PNG delta filters</h4>
  168. <p>
  169. The role of filtering can be illustrated in the following example. Assume the
  170. sequence 2, 3, 4, 5, 6, 7, 8, 9. Although it has much redundancy, the sequence
  171. is not compressible by a Ziv-Lempel compressor, nor by a Huffman compressor.
  172. However, if one makes a simple and reversible transformation, replacing each
  173. value with the numerical difference between it and the value to its left, the
  174. sequence becomes 2, 1, 1, 1, 1, 1, 1, 1, which is highly compressible.
  175. </p>
  176. <p>
  177. The PNG format employs five types of filters: <b><i>None</i></b>,
  178. <b><i>Left</i></b>, <b><i>Up</i></b>, <b><i>Average</i></b>, and
  179. <b><i>Paeth</i></b>. The first filter leaves the original data intact, and the
  180. other four are subtracting from each pixel a value that involves the
  181. neighbor pixels from the left, up, and/or the upper left.
  182. </p>
  183. <p>
  184. A certain filter is assigned to each row, and is applied to all pixels from
  185. that row. Therefore, an image can be delta-filtered in a huge number of
  186. possible configurations (5 ^ <sup><i>height</i></sup>), and each configuration
  187. leads to a different compressed output. Two different filter configurations may
  188. make a difference in the compressed file size by a couple of factors, so a
  189. careful choice of filters is of paramount importance.
  190. </p>
  191. <p>
  192. It is possible to apply a single filter to all rows, or to apply different
  193. filters to different rows. In the former case, the filtering process is
  194. <b><i>fixed</i></b>; in the latter, it is <b><i>adaptive</i></b>.
  195. </p>
  196. <div>
  197. While an exhaustive search is unfeasible, the PNG Specification suggests a
  198. heuristic filtering strategy:
  199. </div>
  200. <ul>
  201. <li>
  202. If the image type is Palette, or the bit depth is smaller than 8, then
  203. do not filter the image (i.e. use fixed filtering, with the filter
  204. <i>None</i>).
  205. </li>
  206. <li>
  207. (The other case) If the image type is Grayscale or RGB (with or without
  208. Alpha), and the bit depth is not smaller than 8, then use adaptive filtering
  209. as follows: <i>independently for each row</i>, apply all five filters and
  210. select the filter that produces the smallest sum of absolute values per row.
  211. </li>
  212. </ul>
  213. <p>
  214. Cases where the above heuristics are less than optimal are shown on the
  215. <a href="http://www.cs.toronto.edu/~cosmin/pngtech/">PNG-Tech</a>
  216. site; for example, see
  217. <a href="better-filtering.html">Brute-force vs. heuristic filtering</a>.
  218. </p>
  219. <h4>2.3 The strategy of searching LZ77 matches</h4>
  220. <p>
  221. The Ziv-Lempel algorithm works under the assumption that contiguous sequences
  222. appear repeatedly in the input stream. If the sequence to be encoded matches
  223. one or more sequences already present in the sliding history window, the
  224. encoder sends a LZ77 pair (<i>distance</i>, <i>length</i>) that points to the
  225. <i>closest</i> match. In most LZ77 incarnations, including Deflate, smaller
  226. distance codes are encoded more concisely.
  227. </p>
  228. <p>
  229. In Deflate, in particular, the regular (non-matched) symbols, and the match
  230. lengths, are sent to the same Huffman coder, while the match distances are sent
  231. to a separate Huffman coder. If the LZ77 matches fall between the accepted
  232. boundaries (i.e. they are not shorter than 3 and not longer than 258), a greedy
  233. strategy will accept them as a replacement for the symbols to which they
  234. correspond.
  235. </p>
  236. <p>
  237. The greedy strategy is preferable when compressing text files, or many types of
  238. binary files, but it may be suboptimal when compressing filtered data, such as
  239. the byte strings that come from a PNG filter. Filtered data consist mostly of
  240. small values with a pseudo-random distribution. Therefore, in certain
  241. situations, it may be desirable to favor the encoding of individual symbols,
  242. even if matches that may replace these symbols exist.
  243. </p>
  244. <p>
  245. The
  246. <a href="http://www.zlib.org/">zlib Reference Library</a>
  247. is a reference implementation of Deflate, which is further used by the
  248. <a href="http://www.libpng.org/pub/png/libpng.html">PNG Reference Library</a>.
  249. By default, <b>zlib</b> selects the greedy strategy, but the user is able to
  250. specify his or her custom preference via the <code>strategy</code> parameter.
  251. This parameter can take one of the following values:
  252. <br>
  253. - <code>Z_DEFAULT_STRATEGY = 0</code>, the default greedy search strategy.
  254. <br>
  255. - <code>Z_FILTERED = 1</code>, a strategy in which the matches are accepted
  256. only if their length is 6 or bigger.
  257. <br>
  258. - <code>Z_HUFFMAN_ONLY = 2</code>, a fast strategy in which the Ziv-Lempel
  259. algorithm is entirely bypassed, and all the symbols from the input are encoded
  260. directly by the Huffman coder.
  261. <br>
  262. - <code>Z_RLE = 3</code> (appeared in the <b>zlib-1.2.x</b> series), a fast
  263. strategy in which the LZ77 algorithm is essentially reduced to the Run-Length
  264. Encoding algorithm. In other words, the matches are accepted only if their
  265. distance is 1. For example, the 10-symbol sequence "<code>aaaaaaaaaa</code>"
  266. can be LZ77-encoded as
  267. ['<code>a</code>', (<i>distance</i>=1, <i>length</i>=9)];
  268. by removing <i>distance</i>=1 from the picture, this encoding can be regarded
  269. as a peculiar run-length encoding (which differs from the classic RLE by using
  270. <i>length</i>=9 instead of <i>count</i>=10).
  271. <br>
  272. The <code>strategy</code> parameter affects only the compression ratio. It does
  273. not affect the correctness of the compressed output, even if it is set to an
  274. inappropriate value.
  275. </p>
  276. <p>
  277. It was experimentally observed that the LZ77 search is occasionally capable of
  278. producing smaller PNGs if it is less exhaustive. The reason behind this act
  279. resides in the same category of "strategic searches" discussed here.
  280. Unfortunately, there is no known method of anticipating which search level
  281. (from the fastest and the least exhaustive, to the slowest and the most
  282. exhaustive) is better, other than assuming "the most exhaustive is better in
  283. most cases".
  284. </p>
  285. <p>
  286. Unfortunately, even a "filtered" strategy does not always produce better
  287. results than a "greedy" strategy on filtered input, and the only known method
  288. to obtain the best combination is by multiple trials. Experiments and
  289. measurements can, again, be found on the
  290. <a href="http://www.cs.toronto.edu/~cosmin/pngtech/">PNG-Tech</a>
  291. site; for example, see the original
  292. <a href="z_rle.html">Z_RLE strategy proposal</a>.
  293. </p>
  294. <h4>2.4 The size of Huffman buffers</h4>
  295. <p>
  296. As mentioned earlier, the entropy encoder inside the Deflate method is the
  297. static Huffman algorithm. The output of LZ77 is fed into a buffer which is
  298. occasionally flushed by sending a static Huffman tree followed by all the
  299. Huffman codes, to the output of Deflate. After this, both the buffer and the
  300. Huffman tree are reset, waiting for the subsequent LZ77 codes to come and
  301. refill the buffer.
  302. </p>
  303. <blockquote>
  304. The Deflate specification refers to <i>dynamic Huffman codes</i>. However, this
  305. is a misnomer, in which the term <i>dynamic</i> is used in contrast to the
  306. <i>fixed</i> Huffman codes. The fixed Huffman codes are simply built according
  307. to a predefined Huffman tree, without regard to the actual symbol frequencies.
  308. The dynamic Huffman codes referred to by the Deflate specification are NOT
  309. built by the dynamic Huffman algorithm, as defined, for example, by Faller,
  310. Gallager and Knuth (the FGK algorithm), or by Vitter (the V algorithm).
  311. The predefined Huffman tree was introduced in <b>PKZIP</b> as a fast
  312. compression alternative, but it produces poor results even on text, and it is
  313. almost useless in PNG compression. Still, a PNG stream that contains codes
  314. built by the fixed (predefined) Huffman tree, is a valid stream, and a
  315. compliant PNG reader must decode this stream correctly.
  316. </blockquote>
  317. <p>
  318. It is desirable to establish the buffer boundaries so that sequences conforming
  319. to the same probability model are fit in the same Huffman buffer. Methods for
  320. approaching these boundaries exist, but they are not used in the mainstream
  321. Deflate implementation(s). Instead, the buffers are flushed when a limit
  322. (typically, 16k LZ77 codes) is reached. This is, however, a fast approach, and
  323. the results are satisfactory.
  324. </p>
  325. <p>
  326. The size of Huffman buffers is indirectly determined by the encoder's memory
  327. (usage) level. For this reason, certain memory levels might be good for certain
  328. types of images.
  329. </p>
  330. <h3>3. PNG (lossless) optimization programs</h3>
  331. <p>
  332. The multitude of PNG encoding programs is listed at
  333. <a href="http://www.libpng.org/pub/png/pngapps.html">http://www.libpng.org/pub/png/pngapps.html</a>.
  334. Their performance varies as much as the range of possible compression ratios;
  335. the good encoders are at least applying the filtering heuristics, described
  336. briefly in the PNG Specification, and illustrated above.
  337. <br>
  338. Some programs gain extra compression by discarding some of the data in the
  339. input images (so these programs are <i>lossy</i>!)
  340. </p>
  341. <p>
  342. This section contains the small list of <b><i>PNG optimization programs</i></b>
  343. that show a particular concern towards obtaining a file size as small as
  344. possible. They work by performing repeated compression trials, applying various
  345. parameter sets, and selecting the parameter set that yields the smallest
  346. compressed output.
  347. </p>
  348. <ul>
  349. <li>
  350. <p>
  351. <b>pngrewrite</b> by Jason Summers, available at
  352. <a href="http://www.pobox.com/~jason1/pngrewrite/">http://www.pobox.com/~jason1/pngrewrite</a>,
  353. is an open-source program that performs lossless image reductions.
  354. It works best in conjunction with <b>pngcrush</b> (see below); the user
  355. should run <b>pngcrush</b> <i>after</i> <b>pngrewrite</b>.
  356. </p>
  357. </li>
  358. <li>
  359. <p>
  360. <b>pngcrush</b> by Glenn Randers-Pehrson, available at
  361. <a href="http://pmt.sourceforge.net/pngcrush/">http://pmt.sourceforge.net/pngcrush</a>,
  362. is an open-source program that iterates over PNG filters and zlib (Deflate)
  363. parameters, compresses the image repeatedly using each parameter
  364. configuration, and chooses the configuration that yields the smallest
  365. compressed (IDAT) output.
  366. At the user's option, the program can explore few (below 10) or many (a
  367. brute-force traversal over more than 100) configurations. The method of
  368. selecting the parameters for "few" trials is particularly effective, and the
  369. use of a brute-force traversal is generally not recommended.
  370. </p>
  371. <p>
  372. In addition, <b>pngcrush</b> offers a multitude of extra features, such as
  373. recovery of erroneous PNG files (e.g. files containing bad CRCs), and
  374. chunk-level editing of PNG meta-data.
  375. </p>
  376. </li>
  377. <li>
  378. <p>
  379. <b>OptiPNG</b> by Cosmin Truţa, available at
  380. <a href="http://www.cs.toronto.edu/~cosmin/pngtech/optipng/">http://www.cs.toronto.edu/pngtech/optipng</a>,
  381. is a newer open-source program, inspired from <b>pngcrush</b>, but designed
  382. to be more flexible and to run faster.
  383. Unlike <b>pngcrush</b>, <b>OptiPNG</b> performs the trials entirely in
  384. memory, and writes only the final output file on the disk. Moreover, it
  385. offers multiple optimization presets to the user, who can choose among a
  386. range of options from "very few trials" to "very many trials" (in contrast to
  387. the coarser "smart vs. brute" option offered by <b>pngcrush</b>).
  388. </p>
  389. <p>
  390. It is important to mention that the achieved compression ratio is less and
  391. less likely to improve when higher-level presets (trigerring more trials)
  392. are being used. Even if the program is capable of searching automatically
  393. over more than 200 configurations (and the advanced users have access to more
  394. than 1000 configurations!), a preset that selects around 10 trials should be
  395. satisfactory for most users. Furthermore, a preset that selects between
  396. 30-40 trials <i>should</i> be satisfactory for all users, for it is very,
  397. very unlikely to be beaten significantly by any wider search. The rest of the
  398. trial configurations are offered rather as a curiosity (but they were used in
  399. the experimentation from which we concluded they are indeed useless!)
  400. </p>
  401. </li>
  402. <li>
  403. <p>
  404. <b>AdvanceCOMP</b> by Andrea Mazzoleni is a set of tools for optimizing
  405. ZIP/GZIP, PNG and MNG files, based on the powerful <b>7-Zip</b> deflation
  406. engine. The name of the PNG optimization tool is <b>AdvPNG</b>. At the time
  407. of this writing, <b>AdvPNG</b> does not perform image reductions, so the use
  408. of <b>pngrewrite</b> or <b>OptiPNG</b> prior to optimiziation may be
  409. necessary. However, given the effectivenes of <b>7-Zip</b> deflation,
  410. <b>AdvanceCOMP</b> is a powerful contender.
  411. </p>
  412. <p>
  413. The <b>AdvanceCOMP</b> tool set is a part of the <b>AdvanceMAME</b> project,
  414. available at
  415. <a href="http://advancemame.sourceforge.net/">http://advancemame.sourceforge.net</a>.
  416. </p>
  417. </li>
  418. <li>
  419. <p>
  420. <b>PNGOut</b> by Ken Silverman, available at
  421. <a href="http://advsys.net/ken/utils.htm">http://advsys.net/ken/utils.htm</a>,
  422. is a freely-available compiled program (no source code), running on
  423. Windows and Linux. According to our tests, the compression ratio achieved by
  424. <b>PNGOut</b> is comparable to that of <b>AdvPNG</b>.
  425. Unfortunately, due to the lack of information, we cannot say much about this
  426. tool.
  427. </p>
  428. <p>
  429. A nice GUI frontend for <b>PNGOut</b>, named <b>PNGGauntlet</b>, is
  430. available at
  431. <a href="http://www.numbera.com/software/pnggauntlet.aspx">http://www.numbera.com/software/pnggauntlet.aspx</a>.
  432. </p>
  433. </li>
  434. </ul>
  435. <h3>4. An extra note on losslessness</h3>
  436. <p>
  437. What is lossless PNG optimization, after all? This is a straightforward
  438. question, whose answer is intuitive, yet not so straightforward.
  439. </p>
  440. <p>
  441. Losslessness in the strictest sense, where no information whatsoever is lost,
  442. can only be achieved by leaving the original file (<i>any</i> file) intact, or
  443. by transforming it (e.g. compressing it, encrypting it) in such a way that
  444. there is an inverse transformation which recovers it completely, bit by bit.
  445. </p>
  446. <p>
  447. In the case of PNG images, this condition of strict losslessness has little
  448. relevance to the casual graphics user, and is, therefore, too strong.
  449. There are instances where strict losslessness is required; for example, when
  450. handling certified PNG files whose integrity is guaranteed by an external
  451. checksum like <b>MD5</b> or <b>SHA</b>, or by a digital signature such as
  452. <b><code>dSIG</code></b>. Most of the time, however, it is desirable to relax
  453. the notion of PNG losslessness, to the extent of not losing any information
  454. that pertains to the <i>rendered image</i> and to the
  455. <i>semantic value of the metadata</i> that accompanies the image. This allows
  456. the user to concentrate on what is really important when it comes to preserving
  457. the contents of a PNG image, and enables the concept of PNG optimization tools.
  458. </p>
  459. <blockquote>
  460. A <b><i>lossless transform</i></b> of a PNG image file is a transform which
  461. fully preserves the <i>rendered</i> RGB triples (the RGB triples that come
  462. either directly, or from a palette index, or from a gray->RGB expansion), the
  463. <i>rendered</i> transparency (the alpha samples that come either directly, or
  464. from a <b><code>tRNS</code></b> chunk, or the implicit 100% opacity assumed due
  465. to the lack of any explicit transparency information), the <i>order of
  466. rendering</i> (sequential or interlaced), and the semantics contained by the
  467. ancillary chunks.
  468. </blockquote>
  469. <div>
  470. This definition allows the execution of the above-mentioned image reduction
  471. operations, and the recompression of <b><code>IDAT</code></b>. It also allows
  472. the alteration or the elimination of other pieces of information that are
  473. technically valid, but have no influence on any presentation of the image
  474. pixels:
  475. </div>
  476. <ul>
  477. <li>
  478. The information that pertains to <b><i>Deflate</i></b> streams, either inside
  479. <b><code>IDAT</code></b>, or in other compressed chunks like
  480. <b><code>zTXt</code></b>, <b><code>iTXt</code></b> or
  481. <b><code>iCCP</code></b>; e.g. the LZ77 window size, the type and size of
  482. <b><i>Deflate</i></b> blocks, etc. (The only thing that matters is that the
  483. decompressed byte sequence must remain the same.)
  484. </li>
  485. <li>
  486. The order of palette entries inside a <b><code>PLTE</code></b> chunk. (When
  487. changing this order, the information that depends on it, such as the
  488. palette-encoded pixels or the <b><code>tRNS</code></b> information, must be
  489. updated accordingly.)
  490. </li>
  491. <li>
  492. RGB triples that do not correspond to any pixel in the actual image, but are
  493. stored in a <b><code>tRNS</code></b> chunk.
  494. </li>
  495. <li>
  496. Fully opaque <b><code>tRNS</code></b> entries in a palette image.
  497. </li>
  498. <li>
  499. Gamma correction (<b><code>gAMA</code></b>) or significant bit
  500. (<b><code>sBIT</code></b>) information inside an image that consists
  501. exclusively of samples whose intensity is either minimum (0) or maximum
  502. (2^<sup>bitdepth</sup>-1).
  503. </li>
  504. <li>
  505. The fact that a textual comment is stored uncompressed in a
  506. <b><code>tEXt</code></b> chunk, or compressed in a <b><code>zTXt</code></b>
  507. chunk, or with no translation in an <b><code>iTXt</code></b> chunk.
  508. </li>
  509. <li>
  510. Etcetera.
  511. </li>
  512. </ul>
  513. <p>
  514. If any of the discardable information is important in a particular application,
  515. and lossless PNG optimization is still desirable, it is recommended to store
  516. this information in ancillary chunks, rather than hack it inside critical
  517. chunks. For example, if sterile palette entries are necessary (e.g. for later
  518. editing stages), it is recommended to store them inside a suggested palette
  519. (<b><code>sPLT</code></b>) chunk, rather than keeping them inside
  520. <b><code>PLTE</code></b>.
  521. </p>
  522. <h3>5. Selective bibliography</h3>
  523. <p>
  524. Besides the discussed specifications, the references below provide essential
  525. information necessary to comprehend the contents of this article.
  526. </p>
  527. <ul>
  528. <li>
  529. Thomas Boutell, Glenn Randers-Pehrson et al.
  530. <i>Portable Network Graphics (PNG) Specification, Second Edition</i>.
  531. ISO/IEC 15948:2003(E); W3C Recommendation 10 November 2003.
  532. </li>
  533. <li>
  534. David A. Huffman.
  535. A method for the construction of minimum redundancy codes.
  536. In <i>Proceedings of the Institute of Radio Engineers</i>,
  537. vol. 40, no. 9, pp. 1098-1101, September 1952.
  538. </li>
  539. <li>
  540. Jacob Ziv and Abraham Lempel.
  541. A universal algorithm for data compression.
  542. <i>IEEE Transactions on Information Theory</i>,
  543. vol. IT-23, no. 3, pp. 337-343, May 1977.
  544. <br>
  545. <font size="-1">
  546. Due to a historical accident, the famous algorithm is better-known as the
  547. "Lempel-Ziv (LZ) algorithm", even though the "Ziv-Lempel algorithm" is a
  548. more legitimate name.
  549. </font>
  550. </li>
  551. <li>
  552. Greg Roelofs.
  553. <i>PNG: The definitive guide</i>.
  554. O'Reilly and Associates, 1999.
  555. </li>
  556. </ul>
  557. <hr>
  558. <address>
  559. <font size="-1">
  560. Copyright &copy; 2003-2008 Cosmin Truţa. Permission to distribute freely.
  561. <br>
  562. Appeared: 7&nbsp;Apr&nbsp;2003.
  563. <br>
  564. Last updated: 10&nbsp;May&nbsp;2008.
  565. </font>
  566. </address>
  567. </body>
  568. </html>