home *** CD-ROM | disk | FTP | other *** search
/ Unix System Administration Handbook 1997 October / usah_oct97.iso / rfc / 2000s / rfc2083.txt < prev    next >
Text File  |  1997-03-26  |  243KB  |  5,716 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7. Network Working Group                                T. Boutell, et. al.
  8. Request for Comments: 2083                             Boutell.Com, Inc.
  9. Category: Informational                                       March 1997
  10.  
  11.  
  12.              PNG (Portable Network Graphics) Specification
  13.                               Version 1.0
  14.  
  15. Status of this Memo
  16.  
  17.    This memo provides information for the Internet community.  This memo
  18.    does not specify an Internet standard of any kind.  Distribution of
  19.    this memo is unlimited.
  20.  
  21. IESG Note:
  22.  
  23.    The IESG takes no position on the validity of any Intellectual
  24.    Property Rights statements contained in this document.
  25.  
  26. Abstract
  27.  
  28.    This document describes PNG (Portable Network Graphics), an
  29.    extensible file format for the lossless, portable, well-compressed
  30.    storage of raster images.  PNG provides a patent-free replacement for
  31.    GIF and can also replace many common uses of TIFF.  Indexed-color,
  32.    grayscale, and truecolor images are supported, plus an optional alpha
  33.    channel.  Sample depths range from 1 to 16 bits.
  34.  
  35.    PNG is designed to work well in online viewing applications, such as
  36.    the World Wide Web, so it is fully streamable with a progressive
  37.    display option.  PNG is robust, providing both full file integrity
  38.    checking and simple detection of common transmission errors.  Also,
  39.    PNG can store gamma and chromaticity data for improved color matching
  40.    on heterogeneous platforms.
  41.  
  42.    This specification defines the Internet Media Type image/png.
  43.  
  44. Table of Contents
  45.  
  46.    1. Introduction ..................................................  4
  47.    2. Data Representation ...........................................  5
  48.       2.1. Integers and byte order ..................................  5
  49.       2.2. Color values .............................................  6
  50.       2.3. Image layout .............................................  6
  51.       2.4. Alpha channel ............................................  7
  52.       2.5. Filtering ................................................  8
  53.       2.6. Interlaced data order ....................................  8
  54.       2.7. Gamma correction ......................................... 10
  55.  
  56.  
  57.  
  58. Boutell, et. al.             Informational                      [Page 1]
  59.  
  60. RFC 2083            PNG: Portable Network Graphics            March 1997
  61.  
  62.  
  63.       2.8. Text strings ............................................. 10
  64.    3. File Structure ................................................ 11
  65.       3.1. PNG file signature ....................................... 11
  66.       3.2. Chunk layout ............................................. 11
  67.       3.3. Chunk naming conventions ................................. 12
  68.       3.4. CRC algorithm ............................................ 15
  69.    4. Chunk Specifications .......................................... 15
  70.       4.1. Critical chunks .......................................... 15
  71.           4.1.1. IHDR Image header .................................. 15
  72.           4.1.2. PLTE Palette ....................................... 17
  73.           4.1.3. IDAT Image data .................................... 18
  74.           4.1.4. IEND Image trailer ................................. 19
  75.       4.2. Ancillary chunks ......................................... 19
  76.           4.2.1. bKGD Background color .............................. 19
  77.           4.2.2. cHRM Primary chromaticities and white point ........ 20
  78.           4.2.3. gAMA Image gamma ................................... 21
  79.           4.2.4. hIST Image histogram ............................... 21
  80.           4.2.5. pHYs Physical pixel dimensions ..................... 22
  81.           4.2.6. sBIT Significant bits .............................. 22
  82.           4.2.7. tEXt Textual data .................................. 24
  83.           4.2.8. tIME Image last-modification time .................. 25
  84.           4.2.9. tRNS Transparency .................................. 26
  85.           4.2.10. zTXt Compressed textual data ...................... 27
  86.       4.3. Summary of standard chunks ............................... 28
  87.       4.4. Additional chunk types ................................... 29
  88.    5. Deflate/Inflate Compression ................................... 29
  89.    6. Filter Algorithms ............................................. 31
  90.       6.1. Filter types ............................................. 31
  91.       6.2. Filter type 0: None ...................................... 32
  92.       6.3. Filter type 1: Sub ....................................... 33
  93.       6.4. Filter type 2: Up ........................................ 33
  94.       6.5. Filter type 3: Average ................................... 34
  95.       6.6. Filter type 4: Paeth...................................... 35
  96.    7. Chunk Ordering Rules .......................................... 36
  97.       7.1. Behavior of PNG editors .................................. 37
  98.       7.2. Ordering of ancillary chunks ............................. 38
  99.       7.3. Ordering of critical chunks .............................. 38
  100.    8. Miscellaneous Topics .......................................... 39
  101.       8.1. File name extension ...................................... 39
  102.       8.2. Internet media type ...................................... 39
  103.       8.3. Macintosh file layout .................................... 39
  104.       8.4. Multiple-image extension ................................. 39
  105.       8.5. Security considerations .................................. 40
  106.    9. Recommendations for Encoders .................................. 41
  107.       9.1. Sample depth scaling ..................................... 41
  108.       9.2. Encoder gamma handling ................................... 42
  109.       9.3. Encoder color handling ................................... 45
  110.       9.4. Alpha channel creation ................................... 47
  111.  
  112.  
  113.  
  114. Boutell, et. al.             Informational                      [Page 2]
  115.  
  116. RFC 2083            PNG: Portable Network Graphics            March 1997
  117.  
  118.  
  119.       9.5. Suggested palettes ....................................... 48
  120.       9.6. Filter selection ......................................... 49
  121.       9.7. Text chunk processing .................................... 49
  122.       9.8. Use of private chunks .................................... 50
  123.       9.9. Private type and method codes ............................ 51
  124.    10. Recommendations for Decoders ................................. 51
  125.       10.1. Error checking .......................................... 52
  126.       10.2. Pixel dimensions ........................................ 52
  127.       10.3. Truecolor image handling ................................ 52
  128.       10.4. Sample depth rescaling .................................. 53
  129.       10.5. Decoder gamma handling .................................. 54
  130.       10.6. Decoder color handling .................................. 56
  131.       10.7. Background color ........................................ 57
  132.       10.8. Alpha channel processing ................................ 58
  133.       10.9. Progressive display ..................................... 62
  134.       10.10. Suggested-palette and histogram usage .................. 63
  135.       10.11. Text chunk processing .................................. 64
  136.    11. Glossary ..................................................... 65
  137.    12. Appendix: Rationale .......................................... 69
  138.       12.1. Why a new file format? .................................. 69
  139.       12.2. Why these features? ..................................... 70
  140.       12.3. Why not these features? ................................. 70
  141.       12.4. Why not use format X? ................................... 72
  142.       12.5. Byte order .............................................. 73
  143.       12.6. Interlacing ............................................. 73
  144.       12.7. Why gamma? .............................................. 73
  145.       12.8. Non-premultiplied alpha ................................. 75
  146.       12.9. Filtering ............................................... 75
  147.       12.10. Text strings ........................................... 76
  148.       12.11. PNG file signature ..................................... 77
  149.       12.12. Chunk layout ........................................... 77
  150.       12.13. Chunk naming conventions ............................... 78
  151.       12.14. Palette histograms ..................................... 80
  152.    13. Appendix: Gamma Tutorial ..................................... 81
  153.    14. Appendix: Color Tutorial ..................................... 89
  154.    15. Appendix: Sample CRC Code .................................... 94
  155.    16. Appendix: Online Resources ................................... 96
  156.    17. Appendix: Revision History ................................... 96
  157.    18. References ................................................... 97
  158.    19. Credits ......................................................100
  159.  
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170. Boutell, et. al.             Informational                      [Page 3]
  171.  
  172. RFC 2083            PNG: Portable Network Graphics            March 1997
  173.  
  174.  
  175. 1. Introduction
  176.  
  177.    The PNG format provides a portable, legally unencumbered, well-
  178.    compressed, well-specified standard for lossless bitmapped image
  179.    files.
  180.  
  181.    Although the initial motivation for developing PNG was to replace
  182.    GIF, the design provides some useful new features not available in
  183.    GIF, with minimal cost to developers.
  184.  
  185.    GIF features retained in PNG include:
  186.  
  187.        * Indexed-color images of up to 256 colors.
  188.        * Streamability: files can be read and written serially, thus
  189.          allowing the file format to be used as a communications
  190.          protocol for on-the-fly generation and display of images.
  191.        * Progressive display: a suitably prepared image file can be
  192.          displayed as it is received over a communications link,
  193.          yielding a low-resolution image very quickly followed by
  194.          gradual improvement of detail.
  195.        * Transparency: portions of the image can be marked as
  196.          transparent, creating the effect of a non-rectangular image.
  197.        * Ancillary information: textual comments and other data can be
  198.          stored within the image file.
  199.        * Complete hardware and platform independence.
  200.        * Effective, 100% lossless compression.
  201.  
  202.    Important new features of PNG, not available in GIF, include:
  203.  
  204.        * Truecolor images of up to 48 bits per pixel.
  205.        * Grayscale images of up to 16 bits per pixel.
  206.        * Full alpha channel (general transparency masks).
  207.        * Image gamma information, which supports automatic display of
  208.          images with correct brightness/contrast regardless of the
  209.          machines used to originate and display the image.
  210.        * Reliable, straightforward detection of file corruption.
  211.        * Faster initial presentation in progressive display mode.
  212.  
  213.    PNG is designed to be:
  214.  
  215.        * Simple and portable: developers should be able to implement PNG
  216.          easily.
  217.        * Legally unencumbered: to the best knowledge of the PNG authors,
  218.          no algorithms under legal challenge are used.  (Some
  219.          considerable effort has been spent to verify this.)
  220.        * Well compressed: both indexed-color and truecolor images are
  221.          compressed as effectively as in any other widely used lossless
  222.          format, and in most cases more effectively.
  223.  
  224.  
  225.  
  226. Boutell, et. al.             Informational                      [Page 4]
  227.  
  228. RFC 2083            PNG: Portable Network Graphics            March 1997
  229.  
  230.  
  231.        * Interchangeable: any standard-conforming PNG decoder must read
  232.          all conforming PNG files.
  233.        * Flexible: the format allows for future extensions and private
  234.          add-ons, without compromising interchangeability of basic PNG.
  235.        * Robust: the design supports full file integrity checking as
  236.          well as simple, quick detection of common transmission errors.
  237.  
  238.    The main part of this specification gives the definition of the file
  239.    format and recommendations for encoder and decoder behavior.  An
  240.    appendix gives the rationale for many design decisions.  Although the
  241.    rationale is not part of the formal specification, reading it can
  242.    help implementors understand the design.  Cross-references in the
  243.    main text point to relevant parts of the rationale.  Additional
  244.    appendixes, also not part of the formal specification, provide
  245.    tutorials on gamma and color theory as well as other supporting
  246.    material.
  247.  
  248.    In this specification, the word "must" indicates a mandatory
  249.    requirement, while "should" indicates recommended behavior.
  250.  
  251.    See Rationale: Why a new file format? (Section 12.1), Why these
  252.    features? (Section 12.2), Why not these features? (Section 12.3), Why
  253.    not use format X? (Section 12.4).
  254.  
  255.    Pronunciation
  256.  
  257.       PNG is pronounced "ping".
  258.  
  259. 2. Data Representation
  260.  
  261.    This chapter discusses basic data representations used in PNG files,
  262.    as well as the expected representation of the image data.
  263.  
  264.    2.1. Integers and byte order
  265.  
  266.       All integers that require more than one byte must be in network
  267.       byte order: the most significant byte comes first, then the less
  268.       significant bytes in descending order of significance (MSB LSB for
  269.       two-byte integers, B3 B2 B1 B0 for four-byte integers).  The
  270.       highest bit (value 128) of a byte is numbered bit 7; the lowest
  271.       bit (value 1) is numbered bit 0. Values are unsigned unless
  272.       otherwise noted. Values explicitly noted as signed are represented
  273.       in two's complement notation.
  274.  
  275.       See Rationale: Byte order (Section 12.5).
  276.  
  277.  
  278.  
  279.  
  280.  
  281.  
  282. Boutell, et. al.             Informational                      [Page 5]
  283.  
  284. RFC 2083            PNG: Portable Network Graphics            March 1997
  285.  
  286.  
  287.    2.2. Color values
  288.  
  289.       Colors can be represented by either grayscale or RGB (red, green,
  290.       blue) sample data.  Grayscale data represents luminance; RGB data
  291.       represents calibrated color information (if the cHRM chunk is
  292.       present) or uncalibrated device-dependent color (if cHRM is
  293.       absent).  All color values range from zero (representing black) to
  294.       most intense at the maximum value for the sample depth.  Note that
  295.       the maximum value at a given sample depth is (2^sampledepth)-1,
  296.       not 2^sampledepth.
  297.  
  298.       Sample values are not necessarily linear; the gAMA chunk specifies
  299.       the gamma characteristic of the source device, and viewers are
  300.       strongly encouraged to compensate properly.  See Gamma correction
  301.       (Section 2.7).
  302.  
  303.       Source data with a precision not directly supported in PNG (for
  304.       example, 5 bit/sample truecolor) must be scaled up to the next
  305.       higher supported bit depth.  This scaling is reversible with no
  306.       loss of data, and it reduces the number of cases that decoders
  307.       have to cope with.  See Recommendations for Encoders: Sample depth
  308.       scaling (Section 9.1) and Recommendations for Decoders: Sample
  309.       depth rescaling (Section 10.4).
  310.  
  311.    2.3. Image layout
  312.  
  313.       Conceptually, a PNG image is a rectangular pixel array, with
  314.       pixels appearing left-to-right within each scanline, and scanlines
  315.       appearing top-to-bottom.  (For progressive display purposes, the
  316.       data may actually be transmitted in a different order; see
  317.       Interlaced data order, Section 2.6.) The size of each pixel is
  318.       determined by the bit depth, which is the number of bits per
  319.       sample in the image data.
  320.  
  321.       Three types of pixel are supported:
  322.  
  323.           * An indexed-color pixel is represented by a single sample
  324.             that is an index into a supplied palette.  The image bit
  325.             depth determines the maximum number of palette entries, but
  326.             not the color precision within the palette.
  327.           * A grayscale pixel is represented by a single sample that is
  328.             a grayscale level, where zero is black and the largest value
  329.             for the bit depth is white.
  330.           * A truecolor pixel is represented by three samples: red (zero
  331.             = black, max = red) appears first, then green (zero = black,
  332.             max = green), then blue (zero = black, max = blue).  The bit
  333.             depth specifies the size of each sample, not the total pixel
  334.             size.
  335.  
  336.  
  337.  
  338. Boutell, et. al.             Informational                      [Page 6]
  339.  
  340. RFC 2083            PNG: Portable Network Graphics            March 1997
  341.  
  342.  
  343.       Optionally, grayscale and truecolor pixels can also include an
  344.       alpha sample, as described in the next section.
  345.  
  346.       Pixels are always packed into scanlines with no wasted bits
  347.       between pixels.  Pixels smaller than a byte never cross byte
  348.       boundaries; they are packed into bytes with the leftmost pixel in
  349.       the high-order bits of a byte, the rightmost in the low-order
  350.       bits.  Permitted bit depths and pixel types are restricted so that
  351.       in all cases the packing is simple and efficient.
  352.  
  353.       PNG permits multi-sample pixels only with 8- and 16-bit samples,
  354.       so multiple samples of a single pixel are never packed into one
  355.       byte.  16-bit samples are stored in network byte order (MSB
  356.       first).
  357.  
  358.       Scanlines always begin on byte boundaries.  When pixels have fewer
  359.       than 8 bits and the scanline width is not evenly divisible by the
  360.       number of pixels per byte, the low-order bits in the last byte of
  361.       each scanline are wasted.  The contents of these wasted bits are
  362.       unspecified.
  363.  
  364.       An additional "filter type" byte is added to the beginning of
  365.       every scanline (see Filtering, Section 2.5).  The filter type byte
  366.       is not considered part of the image data, but it is included in
  367.       the datastream sent to the compression step.
  368.  
  369.    2.4. Alpha channel
  370.  
  371.       An alpha channel, representing transparency information on a per-
  372.       pixel basis, can be included in grayscale and truecolor PNG
  373.       images.
  374.  
  375.       An alpha value of zero represents full transparency, and a value
  376.       of (2^bitdepth)-1 represents a fully opaque pixel.  Intermediate
  377.       values indicate partially transparent pixels that can be combined
  378.       with a background image to yield a composite image.  (Thus, alpha
  379.       is really the degree of opacity of the pixel.  But most people
  380.       refer to alpha as providing transparency information, not opacity
  381.       information, and we continue that custom here.)
  382.  
  383.       Alpha channels can be included with images that have either 8 or
  384.       16 bits per sample, but not with images that have fewer than 8
  385.       bits per sample.  Alpha samples are represented with the same bit
  386.       depth used for the image samples.  The alpha sample for each pixel
  387.       is stored immediately following the grayscale or RGB samples of
  388.       the pixel.
  389.  
  390.  
  391.  
  392.  
  393.  
  394. Boutell, et. al.             Informational                      [Page 7]
  395.  
  396. RFC 2083            PNG: Portable Network Graphics            March 1997
  397.  
  398.  
  399.       The color values stored for a pixel are not affected by the alpha
  400.       value assigned to the pixel.  This rule is sometimes called
  401.       "unassociated" or "non-premultiplied" alpha.  (Another common
  402.       technique is to store sample values premultiplied by the alpha
  403.       fraction; in effect, such an image is already composited against a
  404.       black background.  PNG does not use premultiplied alpha.)
  405.  
  406.       Transparency control is also possible without the storage cost of
  407.       a full alpha channel.  In an indexed-color image, an alpha value
  408.       can be defined for each palette entry.  In grayscale and truecolor
  409.       images, a single pixel value can be identified as being
  410.       "transparent".  These techniques are controlled by the tRNS
  411.       ancillary chunk type.
  412.  
  413.       If no alpha channel nor tRNS chunk is present, all pixels in the
  414.       image are to be treated as fully opaque.
  415.  
  416.       Viewers can support transparency control partially, or not at all.
  417.  
  418.       See Rationale: Non-premultiplied alpha (Section 12.8),
  419.       Recommendations for Encoders: Alpha channel creation (Section
  420.       9.4), and Recommendations for Decoders: Alpha channel processing
  421.       (Section 10.8).
  422.  
  423.    2.5. Filtering
  424.  
  425.       PNG allows the image data to be filtered before it is compressed.
  426.       Filtering can improve the compressibility of the data.  The filter
  427.       step itself does not reduce the size of the data.  All PNG filters
  428.       are strictly lossless.
  429.  
  430.       PNG defines several different filter algorithms, including "None"
  431.       which indicates no filtering.  The filter algorithm is specified
  432.       for each scanline by a filter type byte that precedes the filtered
  433.       scanline in the precompression datastream.  An intelligent encoder
  434.       can switch filters from one scanline to the next.  The method for
  435.       choosing which filter to employ is up to the encoder.
  436.  
  437.       See Filter Algorithms (Chapter 6) and Rationale: Filtering
  438.       (Section 12.9).
  439.  
  440.    2.6. Interlaced data order
  441.  
  442.       A PNG image can be stored in interlaced order to allow progressive
  443.       display.  The purpose of this feature is to allow images to "fade
  444.       in" when they are being displayed on-the-fly.  Interlacing
  445.       slightly expands the file size on average, but it gives the user a
  446.       meaningful display much more rapidly.  Note that decoders are
  447.  
  448.  
  449.  
  450. Boutell, et. al.             Informational                      [Page 8]
  451.  
  452. RFC 2083            PNG: Portable Network Graphics            March 1997
  453.  
  454.  
  455.       required to be able to read interlaced images, whether or not they
  456.       actually perform progressive display.
  457.  
  458.       With interlace method 0, pixels are stored sequentially from left
  459.       to right, and scanlines sequentially from top to bottom (no
  460.       interlacing).
  461.  
  462.       Interlace method 1, known as Adam7 after its author, Adam M.
  463.       Costello, consists of seven distinct passes over the image.  Each
  464.       pass transmits a subset of the pixels in the image.  The pass in
  465.       which each pixel is transmitted is defined by replicating the
  466.       following 8-by-8 pattern over the entire image, starting at the
  467.       upper left corner:
  468.  
  469.          1 6 4 6 2 6 4 6
  470.          7 7 7 7 7 7 7 7
  471.          5 6 5 6 5 6 5 6
  472.          7 7 7 7 7 7 7 7
  473.          3 6 4 6 3 6 4 6
  474.          7 7 7 7 7 7 7 7
  475.          5 6 5 6 5 6 5 6
  476.          7 7 7 7 7 7 7 7
  477.  
  478.       Within each pass, the selected pixels are transmitted left to
  479.       right within a scanline, and selected scanlines sequentially from
  480.       top to bottom.  For example, pass 2 contains pixels 4, 12, 20,
  481.       etc. of scanlines 0, 8, 16, etc. (numbering from 0,0 at the upper
  482.       left corner).  The last pass contains the entirety of scanlines 1,
  483.       3, 5, etc.
  484.  
  485.       The data within each pass is laid out as though it were a complete
  486.       image of the appropriate dimensions.  For example, if the complete
  487.       image is 16 by 16 pixels, then pass 3 will contain two scanlines,
  488.       each containing four pixels.  When pixels have fewer than 8 bits,
  489.       each such scanline is padded as needed to fill an integral number
  490.       of bytes (see Image layout, Section 2.3).  Filtering is done on
  491.       this reduced image in the usual way, and a filter type byte is
  492.       transmitted before each of its scanlines (see Filter Algorithms,
  493.       Chapter 6).  Notice that the transmission order is defined so that
  494.       all the scanlines transmitted in a pass will have the same number
  495.       of pixels; this is necessary for proper application of some of the
  496.       filters.
  497.  
  498.       Caution: If the image contains fewer than five columns or fewer
  499.       than five rows, some passes will be entirely empty.  Encoders and
  500.       decoders must handle this case correctly.  In particular, filter
  501.       type bytes are only associated with nonempty scanlines; no filter
  502.       type bytes are present in an empty pass.
  503.  
  504.  
  505.  
  506. Boutell, et. al.             Informational                      [Page 9]
  507.  
  508. RFC 2083            PNG: Portable Network Graphics            March 1997
  509.  
  510.  
  511.       See Rationale: Interlacing (Section 12.6) and Recommendations for
  512.       Decoders: Progressive display (Section 10.9).
  513.  
  514.    2.7. Gamma correction
  515.  
  516.       PNG images can specify, via the gAMA chunk, the gamma
  517.       characteristic of the image with respect to the original scene.
  518.       Display programs are strongly encouraged to use this information,
  519.       plus information about the display device they are using and room
  520.       lighting, to present the image to the viewer in a way that
  521.       reproduces what the image's original author saw as closely as
  522.       possible.  See Gamma Tutorial (Chapter 13) if you aren't already
  523.       familiar with gamma issues.
  524.  
  525.       Gamma correction is not applied to the alpha channel, if any.
  526.       Alpha samples always represent a linear fraction of full opacity.
  527.  
  528.       For high-precision applications, the exact chromaticity of the RGB
  529.       data in a PNG image can be specified via the cHRM chunk, allowing
  530.       more accurate color matching than gamma correction alone will
  531.       provide.  See Color Tutorial (Chapter 14) if you aren't already
  532.       familiar with color representation issues.
  533.  
  534.       See Rationale: Why gamma? (Section 12.7), Recommendations for
  535.       Encoders: Encoder gamma handling (Section 9.2), and
  536.       Recommendations for Decoders: Decoder gamma handling (Section
  537.       10.5).
  538.  
  539.    2.8. Text strings
  540.  
  541.       A PNG file can store text associated with the image, such as an
  542.       image description or copyright notice.  Keywords are used to
  543.       indicate what each text string represents.
  544.  
  545.       ISO 8859-1 (Latin-1) is the character set recommended for use in
  546.       text strings [ISO-8859].  This character set is a superset of 7-
  547.       bit ASCII.
  548.  
  549.       Character codes not defined in Latin-1 should not be used, because
  550.       they have no platform-independent meaning.  If a non-Latin-1 code
  551.       does appear in a PNG text string, its interpretation will vary
  552.       across platforms and decoders.  Some systems might not even be
  553.       able to display all the characters in Latin-1, but most modern
  554.       systems can.
  555.  
  556.       Provision is also made for the storage of compressed text.
  557.  
  558.       See Rationale: Text strings (Section 12.10).
  559.  
  560.  
  561.  
  562. Boutell, et. al.             Informational                     [Page 10]
  563.  
  564. RFC 2083            PNG: Portable Network Graphics            March 1997
  565.  
  566.  
  567. 3. File Structure
  568.  
  569.    A PNG file consists of a PNG signature followed by a series of
  570.    chunks.  This chapter defines the signature and the basic properties
  571.    of chunks.  Individual chunk types are discussed in the next chapter.
  572.  
  573.    3.1. PNG file signature
  574.  
  575.       The first eight bytes of a PNG file always contain the following
  576.       (decimal) values:
  577.  
  578.          137 80 78 71 13 10 26 10
  579.  
  580.       This signature indicates that the remainder of the file contains a
  581.       single PNG image, consisting of a series of chunks beginning with
  582.       an IHDR chunk and ending with an IEND chunk.
  583.  
  584.       See Rationale: PNG file signature (Section 12.11).
  585.  
  586.    3.2. Chunk layout
  587.  
  588.       Each chunk consists of four parts:
  589.  
  590.       Length
  591.          A 4-byte unsigned integer giving the number of bytes in the
  592.          chunk's data field. The length counts only the data field, not
  593.          itself, the chunk type code, or the CRC.  Zero is a valid
  594.          length.  Although encoders and decoders should treat the length
  595.          as unsigned, its value must not exceed (2^31)-1 bytes.
  596.  
  597.       Chunk Type
  598.          A 4-byte chunk type code.  For convenience in description and
  599.          in examining PNG files, type codes are restricted to consist of
  600.          uppercase and lowercase ASCII letters (A-Z and a-z, or 65-90
  601.          and 97-122 decimal).  However, encoders and decoders must treat
  602.          the codes as fixed binary values, not character strings.  For
  603.          example, it would not be correct to represent the type code
  604.          IDAT by the EBCDIC equivalents of those letters.  Additional
  605.          naming conventions for chunk types are discussed in the next
  606.          section.
  607.  
  608.       Chunk Data
  609.          The data bytes appropriate to the chunk type, if any.  This
  610.          field can be of zero length.
  611.  
  612.  
  613.  
  614.  
  615.  
  616.  
  617.  
  618. Boutell, et. al.             Informational                     [Page 11]
  619.  
  620. RFC 2083            PNG: Portable Network Graphics            March 1997
  621.  
  622.  
  623.       CRC
  624.          A 4-byte CRC (Cyclic Redundancy Check) calculated on the
  625.          preceding bytes in the chunk, including the chunk type code and
  626.          chunk data fields, but not including the length field. The CRC
  627.          is always present, even for chunks containing no data.  See CRC
  628.          algorithm (Section 3.4).
  629.  
  630.       The chunk data length can be any number of bytes up to the
  631.       maximum; therefore, implementors cannot assume that chunks are
  632.       aligned on any boundaries larger than bytes.
  633.  
  634.       Chunks can appear in any order, subject to the restrictions placed
  635.       on each chunk type.  (One notable restriction is that IHDR must
  636.       appear first and IEND must appear last; thus the IEND chunk serves
  637.       as an end-of-file marker.)  Multiple chunks of the same type can
  638.       appear, but only if specifically permitted for that type.
  639.  
  640.       See Rationale: Chunk layout (Section 12.12).
  641.  
  642.    3.3. Chunk naming conventions
  643.  
  644.       Chunk type codes are assigned so that a decoder can determine some
  645.       properties of a chunk even when it does not recognize the type
  646.       code.  These rules are intended to allow safe, flexible extension
  647.       of the PNG format, by allowing a decoder to decide what to do when
  648.       it encounters an unknown chunk.  The naming rules are not normally
  649.       of interest when the decoder does recognize the chunk's type.
  650.  
  651.       Four bits of the type code, namely bit 5 (value 32) of each byte,
  652.       are used to convey chunk properties.  This choice means that a
  653.       human can read off the assigned properties according to whether
  654.       each letter of the type code is uppercase (bit 5 is 0) or
  655.       lowercase (bit 5 is 1).  However, decoders should test the
  656.       properties of an unknown chunk by numerically testing the
  657.       specified bits; testing whether a character is uppercase or
  658.       lowercase is inefficient, and even incorrect if a locale-specific
  659.       case definition is used.
  660.  
  661.       It is worth noting that the property bits are an inherent part of
  662.       the chunk name, and hence are fixed for any chunk type.  Thus,
  663.       TEXT and Text would be unrelated chunk type codes, not the same
  664.       chunk with different properties.  Decoders must recognize type
  665.       codes by a simple four-byte literal comparison; it is incorrect to
  666.       perform case conversion on type codes.
  667.  
  668.  
  669.  
  670.  
  671.  
  672.  
  673.  
  674. Boutell, et. al.             Informational                     [Page 12]
  675.  
  676. RFC 2083            PNG: Portable Network Graphics            March 1997
  677.  
  678.  
  679.       The semantics of the property bits are:
  680.  
  681.       Ancillary bit: bit 5 of first byte
  682.          0 (uppercase) = critical, 1 (lowercase) = ancillary.
  683.  
  684.          Chunks that are not strictly necessary in order to meaningfully
  685.          display the contents of the file are known as "ancillary"
  686.          chunks.  A decoder encountering an unknown chunk in which the
  687.          ancillary bit is 1 can safely ignore the chunk and proceed to
  688.          display the image. The time chunk (tIME) is an example of an
  689.          ancillary chunk.
  690.  
  691.          Chunks that are necessary for successful display of the file's
  692.          contents are called "critical" chunks. A decoder encountering
  693.          an unknown chunk in which the ancillary bit is 0 must indicate
  694.          to the user that the image contains information it cannot
  695.          safely interpret.  The image header chunk (IHDR) is an example
  696.          of a critical chunk.
  697.  
  698.       Private bit: bit 5 of second byte
  699.          0 (uppercase) = public, 1 (lowercase) = private.
  700.  
  701.          A public chunk is one that is part of the PNG specification or
  702.          is registered in the list of PNG special-purpose public chunk
  703.          types.  Applications can also define private (unregistered)
  704.          chunks for their own purposes.  The names of private chunks
  705.          must have a lowercase second letter, while public chunks will
  706.          always be assigned names with uppercase second letters.  Note
  707.          that decoders do not need to test the private-chunk property
  708.          bit, since it has no functional significance; it is simply an
  709.          administrative convenience to ensure that public and private
  710.          chunk names will not conflict.  See Additional chunk types
  711.          (Section 4.4) and Recommendations for Encoders: Use of private
  712.          chunks (Section 9.8).
  713.  
  714.       Reserved bit: bit 5 of third byte
  715.          Must be 0 (uppercase) in files conforming to this version of
  716.          PNG.
  717.  
  718.          The significance of the case of the third letter of the chunk
  719.          name is reserved for possible future expansion.  At the present
  720.          time all chunk names must have uppercase third letters.
  721.          (Decoders should not complain about a lowercase third letter,
  722.          however, as some future version of the PNG specification could
  723.          define a meaning for this bit.  It is sufficient to treat a
  724.          chunk with a lowercase third letter in the same way as any
  725.          other unknown chunk type.)
  726.  
  727.  
  728.  
  729.  
  730. Boutell, et. al.             Informational                     [Page 13]
  731.  
  732. RFC 2083            PNG: Portable Network Graphics            March 1997
  733.  
  734.  
  735.       Safe-to-copy bit: bit 5 of fourth byte
  736.          0 (uppercase) = unsafe to copy, 1 (lowercase) = safe to copy.
  737.  
  738.          This property bit is not of interest to pure decoders, but it
  739.          is needed by PNG editors (programs that modify PNG files).
  740.          This bit defines the proper handling of unrecognized chunks in
  741.          a file that is being modified.
  742.  
  743.          If a chunk's safe-to-copy bit is 1, the chunk may be copied to
  744.          a modified PNG file whether or not the software recognizes the
  745.          chunk type, and regardless of the extent of the file
  746.          modifications.
  747.  
  748.          If a chunk's safe-to-copy bit is 0, it indicates that the chunk
  749.          depends on the image data.  If the program has made any changes
  750.          to critical chunks, including addition, modification, deletion,
  751.          or reordering of critical chunks, then unrecognized unsafe
  752.          chunks must not be copied to the output PNG file.  (Of course,
  753.          if the program does recognize the chunk, it can choose to
  754.          output an appropriately modified version.)
  755.  
  756.          A PNG editor is always allowed to copy all unrecognized chunks
  757.          if it has only added, deleted, modified, or reordered ancillary
  758.          chunks.  This implies that it is not permissible for ancillary
  759.          chunks to depend on other ancillary chunks.
  760.  
  761.          PNG editors that do not recognize a critical chunk must report
  762.          an error and refuse to process that PNG file at all. The
  763.          safe/unsafe mechanism is intended for use with ancillary
  764.          chunks.  The safe-to-copy bit will always be 0 for critical
  765.          chunks.
  766.  
  767.          Rules for PNG editors are discussed further in Chunk Ordering
  768.          Rules (Chapter 7).
  769.  
  770.       For example, the hypothetical chunk type name "bLOb" has the
  771.       property bits:
  772.  
  773.          bLOb  <-- 32 bit chunk type code represented in text form
  774.          ||||
  775.          |||+- Safe-to-copy bit is 1 (lower case letter; bit 5 is 1)
  776.          ||+-- Reserved bit is 0     (upper case letter; bit 5 is 0)
  777.          |+--- Private bit is 0      (upper case letter; bit 5 is 0)
  778.          +---- Ancillary bit is 1    (lower case letter; bit 5 is 1)
  779.  
  780.       Therefore, this name represents an ancillary, public, safe-to-copy
  781.       chunk.
  782.  
  783.  
  784.  
  785.  
  786. Boutell, et. al.             Informational                     [Page 14]
  787.  
  788. RFC 2083            PNG: Portable Network Graphics            March 1997
  789.  
  790.  
  791.       See Rationale: Chunk naming conventions (Section 12.13).
  792.  
  793.    3.4. CRC algorithm
  794.  
  795.       Chunk CRCs are calculated using standard CRC methods with pre and
  796.       post conditioning, as defined by ISO 3309 [ISO-3309] or ITU-T V.42
  797.       [ITU-V42].  The CRC polynomial employed is
  798.  
  799.          x^32+x^26+x^23+x^22+x^16+x^12+x^11+x^10+x^8+x^7+x^5+x^4+x^2+x+1
  800.  
  801.       The 32-bit CRC register is initialized to all 1's, and then the
  802.       data from each byte is processed from the least significant bit
  803.       (1) to the most significant bit (128).  After all the data bytes
  804.       are processed, the CRC register is inverted (its ones complement
  805.       is taken).  This value is transmitted (stored in the file) MSB
  806.       first.  For the purpose of separating into bytes and ordering, the
  807.       least significant bit of the 32-bit CRC is defined to be the
  808.       coefficient of the x^31 term.
  809.  
  810.       Practical calculation of the CRC always employs a precalculated
  811.       table to greatly accelerate the computation. See Sample CRC Code
  812.       (Chapter 15).
  813.  
  814. 4. Chunk Specifications
  815.  
  816.    This chapter defines the standard types of PNG chunks.
  817.  
  818.    4.1. Critical chunks
  819.  
  820.       All implementations must understand and successfully render the
  821.       standard critical chunks.  A valid PNG image must contain an IHDR
  822.       chunk, one or more IDAT chunks, and an IEND chunk.
  823.  
  824.       4.1.1. IHDR Image header
  825.  
  826.          The IHDR chunk must appear FIRST.  It contains:
  827.  
  828.             Width:              4 bytes
  829.             Height:             4 bytes
  830.             Bit depth:          1 byte
  831.             Color type:         1 byte
  832.             Compression method: 1 byte
  833.             Filter method:      1 byte
  834.             Interlace method:   1 byte
  835.  
  836.  
  837.  
  838.  
  839.  
  840.  
  841.  
  842. Boutell, et. al.             Informational                     [Page 15]
  843.  
  844. RFC 2083            PNG: Portable Network Graphics            March 1997
  845.  
  846.  
  847.          Width and height give the image dimensions in pixels.  They are
  848.          4-byte integers. Zero is an invalid value. The maximum for each
  849.          is (2^31)-1 in order to accommodate languages that have
  850.          difficulty with unsigned 4-byte values.
  851.  
  852.          Bit depth is a single-byte integer giving the number of bits
  853.          per sample or per palette index (not per pixel).  Valid values
  854.          are 1, 2, 4, 8, and 16, although not all values are allowed for
  855.          all color types.
  856.  
  857.          Color type is a single-byte integer that describes the
  858.          interpretation of the image data.  Color type codes represent
  859.          sums of the following values: 1 (palette used), 2 (color used),
  860.          and 4 (alpha channel used). Valid values are 0, 2, 3, 4, and 6.
  861.  
  862.          Bit depth restrictions for each color type are imposed to
  863.          simplify implementations and to prohibit combinations that do
  864.          not compress well.  Decoders must support all legal
  865.          combinations of bit depth and color type.  The allowed
  866.          combinations are:
  867.  
  868.             Color    Allowed    Interpretation
  869.             Type    Bit Depths
  870.  
  871.             0       1,2,4,8,16  Each pixel is a grayscale sample.
  872.  
  873.             2       8,16        Each pixel is an R,G,B triple.
  874.  
  875.             3       1,2,4,8     Each pixel is a palette index;
  876.                                 a PLTE chunk must appear.
  877.  
  878.             4       8,16        Each pixel is a grayscale sample,
  879.                                 followed by an alpha sample.
  880.  
  881.             6       8,16        Each pixel is an R,G,B triple,
  882.                                 followed by an alpha sample.
  883.  
  884.          The sample depth is the same as the bit depth except in the
  885.          case of color type 3, in which the sample depth is always 8
  886.          bits.
  887.  
  888.          Compression method is a single-byte integer that indicates the
  889.          method used to compress the image data.  At present, only
  890.          compression method 0 (deflate/inflate compression with a 32K
  891.          sliding window) is defined.  All standard PNG images must be
  892.          compressed with this scheme.  The compression method field is
  893.          provided for possible future expansion or proprietary variants.
  894.          Decoders must check this byte and report an error if it holds
  895.  
  896.  
  897.  
  898. Boutell, et. al.             Informational                     [Page 16]
  899.  
  900. RFC 2083            PNG: Portable Network Graphics            March 1997
  901.  
  902.  
  903.          an unrecognized code.  See Deflate/Inflate Compression (Chapter
  904.          5) for details.
  905.  
  906.          Filter method is a single-byte integer that indicates the
  907.          preprocessing method applied to the image data before
  908.          compression.  At present, only filter method 0 (adaptive
  909.          filtering with five basic filter types) is defined.  As with
  910.          the compression method field, decoders must check this byte and
  911.          report an error if it holds an unrecognized code.  See Filter
  912.          Algorithms (Chapter 6) for details.
  913.  
  914.          Interlace method is a single-byte integer that indicates the
  915.          transmission order of the image data.  Two values are currently
  916.          defined: 0 (no interlace) or 1 (Adam7 interlace).  See
  917.          Interlaced data order (Section 2.6) for details.
  918.  
  919.       4.1.2. PLTE Palette
  920.  
  921.          The PLTE chunk contains from 1 to 256 palette entries, each a
  922.          three-byte series of the form:
  923.  
  924.             Red:   1 byte (0 = black, 255 = red)
  925.             Green: 1 byte (0 = black, 255 = green)
  926.             Blue:  1 byte (0 = black, 255 = blue)
  927.  
  928.          The number of entries is determined from the chunk length.  A
  929.          chunk length not divisible by 3 is an error.
  930.  
  931.          This chunk must appear for color type 3, and can appear for
  932.          color types 2 and 6; it must not appear for color types 0 and
  933.          4. If this chunk does appear, it must precede the first IDAT
  934.          chunk.  There must not be more than one PLTE chunk.
  935.  
  936.          For color type 3 (indexed color), the PLTE chunk is required.
  937.          The first entry in PLTE is referenced by pixel value 0, the
  938.          second by pixel value 1, etc.  The number of palette entries
  939.          must not exceed the range that can be represented in the image
  940.          bit depth (for example, 2^4 = 16 for a bit depth of 4).  It is
  941.          permissible to have fewer entries than the bit depth would
  942.          allow.  In that case, any out-of-range pixel value found in the
  943.          image data is an error.
  944.  
  945.          For color types 2 and 6 (truecolor and truecolor with alpha),
  946.          the PLTE chunk is optional.  If present, it provides a
  947.          suggested set of from 1 to 256 colors to which the truecolor
  948.          image can be quantized if the viewer cannot display truecolor
  949.          directly.  If PLTE is not present, such a viewer will need to
  950.          select colors on its own, but it is often preferable for this
  951.  
  952.  
  953.  
  954. Boutell, et. al.             Informational                     [Page 17]
  955.  
  956. RFC 2083            PNG: Portable Network Graphics            March 1997
  957.  
  958.  
  959.          to be done once by the encoder.  (See Recommendations for
  960.          Encoders: Suggested palettes, Section 9.5.)
  961.  
  962.          Note that the palette uses 8 bits (1 byte) per sample
  963.          regardless of the image bit depth specification.  In
  964.          particular, the palette is 8 bits deep even when it is a
  965.          suggested quantization of a 16-bit truecolor image.
  966.  
  967.          There is no requirement that the palette entries all be used by
  968.          the image, nor that they all be different.
  969.  
  970.       4.1.3. IDAT Image data
  971.  
  972.          The IDAT chunk contains the actual image data.  To create this
  973.          data:
  974.  
  975.              * Begin with image scanlines represented as described in
  976.                Image layout (Section 2.3); the layout and total size of
  977.                this raw data are determined by the fields of IHDR.
  978.              * Filter the image data according to the filtering method
  979.                specified by the IHDR chunk.  (Note that with filter
  980.                method 0, the only one currently defined, this implies
  981.                prepending a filter type byte to each scanline.)
  982.              * Compress the filtered data using the compression method
  983.                specified by the IHDR chunk.
  984.  
  985.          The IDAT chunk contains the output datastream of the
  986.          compression algorithm.
  987.  
  988.          To read the image data, reverse this process.
  989.  
  990.          There can be multiple IDAT chunks; if so, they must appear
  991.          consecutively with no other intervening chunks.  The compressed
  992.          datastream is then the concatenation of the contents of all the
  993.          IDAT chunks.  The encoder can divide the compressed datastream
  994.          into IDAT chunks however it wishes.  (Multiple IDAT chunks are
  995.          allowed so that encoders can work in a fixed amount of memory;
  996.          typically the chunk size will correspond to the encoder's
  997.          buffer size.) It is important to emphasize that IDAT chunk
  998.          boundaries have no semantic significance and can occur at any
  999.          point in the compressed datastream.  A PNG file in which each
  1000.          IDAT chunk contains only one data byte is legal, though
  1001.          remarkably wasteful of space.  (For that matter, zero-length
  1002.          IDAT chunks are legal, though even more wasteful.)
  1003.  
  1004.          See Filter Algorithms (Chapter 6) and Deflate/Inflate
  1005.          Compression (Chapter 5) for details.
  1006.  
  1007.  
  1008.  
  1009.  
  1010. Boutell, et. al.             Informational                     [Page 18]
  1011.  
  1012. RFC 2083            PNG: Portable Network Graphics            March 1997
  1013.  
  1014.  
  1015.       4.1.4. IEND Image trailer
  1016.  
  1017.          The IEND chunk must appear LAST.  It marks the end of the PNG
  1018.          datastream.  The chunk's data field is empty.
  1019.  
  1020.    4.2. Ancillary chunks
  1021.  
  1022.       All ancillary chunks are optional, in the sense that encoders need
  1023.       not write them and decoders can ignore them.  However, encoders
  1024.       are encouraged to write the standard ancillary chunks when the
  1025.       information is available, and decoders are encouraged to interpret
  1026.       these chunks when appropriate and feasible.
  1027.  
  1028.       The standard ancillary chunks are listed in alphabetical order.
  1029.       This is not necessarily the order in which they would appear in a
  1030.       file.
  1031.  
  1032.       4.2.1. bKGD Background color
  1033.  
  1034.          The bKGD chunk specifies a default background color to present
  1035.          the image against.  Note that viewers are not bound to honor
  1036.          this chunk; a viewer can choose to use a different background.
  1037.  
  1038.          For color type 3 (indexed color), the bKGD chunk contains:
  1039.  
  1040.             Palette index:  1 byte
  1041.  
  1042.          The value is the palette index of the color to be used as
  1043.          background.
  1044.  
  1045.          For color types 0 and 4 (grayscale, with or without alpha),
  1046.          bKGD contains:
  1047.  
  1048.             Gray:  2 bytes, range 0 .. (2^bitdepth)-1
  1049.  
  1050.          (For consistency, 2 bytes are used regardless of the image bit
  1051.          depth.)  The value is the gray level to be used as background.
  1052.  
  1053.          For color types 2 and 6 (truecolor, with or without alpha),
  1054.          bKGD contains:
  1055.  
  1056.             Red:   2 bytes, range 0 .. (2^bitdepth)-1
  1057.             Green: 2 bytes, range 0 .. (2^bitdepth)-1
  1058.             Blue:  2 bytes, range 0 .. (2^bitdepth)-1
  1059.  
  1060.          (For consistency, 2 bytes per sample are used regardless of the
  1061.          image bit depth.)  This is the RGB color to be used as
  1062.          background.
  1063.  
  1064.  
  1065.  
  1066. Boutell, et. al.             Informational                     [Page 19]
  1067.  
  1068. RFC 2083            PNG: Portable Network Graphics            March 1997
  1069.  
  1070.  
  1071.          When present, the bKGD chunk must precede the first IDAT chunk,
  1072.          and must follow the PLTE chunk, if any.
  1073.  
  1074.          See Recommendations for Decoders: Background color (Section
  1075.          10.7).
  1076.  
  1077.       4.2.2. cHRM Primary chromaticities and white point
  1078.  
  1079.          Applications that need device-independent specification of
  1080.          colors in a PNG file can use the cHRM chunk to specify the 1931
  1081.          CIE x,y chromaticities of the red, green, and blue primaries
  1082.          used in the image, and the referenced white point. See Color
  1083.          Tutorial (Chapter 14) for more information.
  1084.  
  1085.          The cHRM chunk contains:
  1086.  
  1087.             White Point x: 4 bytes
  1088.             White Point y: 4 bytes
  1089.             Red x:         4 bytes
  1090.             Red y:         4 bytes
  1091.             Green x:       4 bytes
  1092.             Green y:       4 bytes
  1093.             Blue x:        4 bytes
  1094.             Blue y:        4 bytes
  1095.  
  1096.          Each value is encoded as a 4-byte unsigned integer,
  1097.          representing the x or y value times 100000.  For example, a
  1098.          value of 0.3127 would be stored as the integer 31270.
  1099.  
  1100.          cHRM is allowed in all PNG files, although it is of little
  1101.          value for grayscale images.
  1102.  
  1103.          If the encoder does not know the chromaticity values, it should
  1104.          not write a cHRM chunk; the absence of a cHRM chunk indicates
  1105.          that the image's primary colors are device-dependent.
  1106.  
  1107.          If the cHRM chunk appears, it must precede the first IDAT
  1108.          chunk, and it must also precede the PLTE chunk if present.
  1109.  
  1110.          See Recommendations for Encoders: Encoder color handling
  1111.          (Section 9.3), and Recommendations for Decoders: Decoder color
  1112.          handling (Section 10.6).
  1113.  
  1114.  
  1115.  
  1116.  
  1117.  
  1118.  
  1119.  
  1120.  
  1121.  
  1122. Boutell, et. al.             Informational                     [Page 20]
  1123.  
  1124. RFC 2083            PNG: Portable Network Graphics            March 1997
  1125.  
  1126.  
  1127.       4.2.3. gAMA Image gamma
  1128.  
  1129.          The gAMA chunk specifies the gamma of the camera (or simulated
  1130.          camera) that produced the image, and thus the gamma of the
  1131.          image with respect to the original scene.  More precisely, the
  1132.          gAMA chunk encodes the file_gamma value, as defined in Gamma
  1133.          Tutorial (Chapter 13).
  1134.  
  1135.          The gAMA chunk contains:
  1136.  
  1137.             Image gamma: 4 bytes
  1138.  
  1139.          The value is encoded as a 4-byte unsigned integer, representing
  1140.          gamma times 100000.  For example, a gamma of 0.45 would be
  1141.          stored as the integer 45000.
  1142.  
  1143.          If the encoder does not know the image's gamma value, it should
  1144.          not write a gAMA chunk; the absence of a gAMA chunk indicates
  1145.          that the gamma is unknown.
  1146.  
  1147.          If the gAMA chunk appears, it must precede the first IDAT
  1148.          chunk, and it must also precede the PLTE chunk if present.
  1149.  
  1150.          See Gamma correction (Section 2.7), Recommendations for
  1151.          Encoders: Encoder gamma handling (Section 9.2), and
  1152.          Recommendations for Decoders: Decoder gamma handling (Section
  1153.          10.5).
  1154.  
  1155.       4.2.4. hIST Image histogram
  1156.  
  1157.          The hIST chunk gives the approximate usage frequency of each
  1158.          color in the color palette.  A histogram chunk can appear only
  1159.          when a palette chunk appears.  If a viewer is unable to provide
  1160.          all the colors listed in the palette, the histogram may help it
  1161.          decide how to choose a subset of the colors for display.
  1162.  
  1163.          The hIST chunk contains a series of 2-byte (16 bit) unsigned
  1164.          integers.  There must be exactly one entry for each entry in
  1165.          the PLTE chunk.  Each entry is proportional to the fraction of
  1166.          pixels in the image that have that palette index; the exact
  1167.          scale factor is chosen by the encoder.
  1168.  
  1169.          Histogram entries are approximate, with the exception that a
  1170.          zero entry specifies that the corresponding palette entry is
  1171.          not used at all in the image.  It is required that a histogram
  1172.          entry be nonzero if there are any pixels of that color.
  1173.  
  1174.  
  1175.  
  1176.  
  1177.  
  1178. Boutell, et. al.             Informational                     [Page 21]
  1179.  
  1180. RFC 2083            PNG: Portable Network Graphics            March 1997
  1181.  
  1182.  
  1183.          When the palette is a suggested quantization of a truecolor
  1184.          image, the histogram is necessarily approximate, since a
  1185.          decoder may map pixels to palette entries differently than the
  1186.          encoder did.  In this situation, zero entries should not
  1187.          appear.
  1188.  
  1189.          The hIST chunk, if it appears, must follow the PLTE chunk, and
  1190.          must precede the first IDAT chunk.
  1191.  
  1192.          See Rationale: Palette histograms (Section 12.14), and
  1193.          Recommendations for Decoders: Suggested-palette and histogram
  1194.          usage (Section 10.10).
  1195.  
  1196.       4.2.5. pHYs Physical pixel dimensions
  1197.  
  1198.          The pHYs chunk specifies the intended pixel size or aspect
  1199.          ratio for display of the image.  It contains:
  1200.  
  1201.             Pixels per unit, X axis: 4 bytes (unsigned integer)
  1202.             Pixels per unit, Y axis: 4 bytes (unsigned integer)
  1203.             Unit specifier:          1 byte
  1204.  
  1205.          The following values are legal for the unit specifier:
  1206.  
  1207.             0: unit is unknown
  1208.             1: unit is the meter
  1209.  
  1210.          When the unit specifier is 0, the pHYs chunk defines pixel
  1211.          aspect ratio only; the actual size of the pixels remains
  1212.          unspecified.
  1213.  
  1214.          Conversion note: one inch is equal to exactly 0.0254 meters.
  1215.  
  1216.          If this ancillary chunk is not present, pixels are assumed to
  1217.          be square, and the physical size of each pixel is unknown.
  1218.  
  1219.          If present, this chunk must precede the first IDAT chunk.
  1220.  
  1221.          See Recommendations for Decoders: Pixel dimensions (Section
  1222.          10.2).
  1223.  
  1224.       4.2.6. sBIT Significant bits
  1225.  
  1226.          To simplify decoders, PNG specifies that only certain sample
  1227.          depths can be used, and further specifies that sample values
  1228.          should be scaled to the full range of possible values at the
  1229.          sample depth.  However, the sBIT chunk is provided in order to
  1230.          store the original number of significant bits.  This allows
  1231.  
  1232.  
  1233.  
  1234. Boutell, et. al.             Informational                     [Page 22]
  1235.  
  1236. RFC 2083            PNG: Portable Network Graphics            March 1997
  1237.  
  1238.  
  1239.          decoders to recover the original data losslessly even if the
  1240.          data had a sample depth not directly supported by PNG.  We
  1241.          recommend that an encoder emit an sBIT chunk if it has
  1242.          converted the data from a lower sample depth.
  1243.  
  1244.          For color type 0 (grayscale), the sBIT chunk contains a single
  1245.          byte, indicating the number of bits that were significant in
  1246.          the source data.
  1247.  
  1248.          For color type 2 (truecolor), the sBIT chunk contains three
  1249.          bytes, indicating the number of bits that were significant in
  1250.          the source data for the red, green, and blue channels,
  1251.          respectively.
  1252.  
  1253.          For color type 3 (indexed color), the sBIT chunk contains three
  1254.          bytes, indicating the number of bits that were significant in
  1255.          the source data for the red, green, and blue components of the
  1256.          palette entries, respectively.
  1257.  
  1258.          For color type 4 (grayscale with alpha channel), the sBIT chunk
  1259.          contains two bytes, indicating the number of bits that were
  1260.          significant in the source grayscale data and the source alpha
  1261.          data, respectively.
  1262.  
  1263.          For color type 6 (truecolor with alpha channel), the sBIT chunk
  1264.          contains four bytes, indicating the number of bits that were
  1265.          significant in the source data for the red, green, blue and
  1266.          alpha channels, respectively.
  1267.  
  1268.          Each depth specified in sBIT must be greater than zero and less
  1269.          than or equal to the sample depth (which is 8 for indexed-color
  1270.          images, and the bit depth given in IHDR for other color types).
  1271.  
  1272.          A decoder need not pay attention to sBIT: the stored image is a
  1273.          valid PNG file of the sample depth indicated by IHDR.  However,
  1274.          if the decoder wishes to recover the original data at its
  1275.          original precision, this can be done by right-shifting the
  1276.          stored samples (the stored palette entries, for an indexed-
  1277.          color image).  The encoder must scale the data in such a way
  1278.          that the high-order bits match the original data.
  1279.  
  1280.          If the sBIT chunk appears, it must precede the first IDAT
  1281.          chunk, and it must also precede the PLTE chunk if present.
  1282.  
  1283.          See Recommendations for Encoders: Sample depth scaling (Section
  1284.          9.1) and Recommendations for Decoders: Sample depth rescaling
  1285.          (Section 10.4).
  1286.  
  1287.  
  1288.  
  1289.  
  1290. Boutell, et. al.             Informational                     [Page 23]
  1291.  
  1292. RFC 2083            PNG: Portable Network Graphics            March 1997
  1293.  
  1294.  
  1295.       4.2.7. tEXt Textual data
  1296.  
  1297.          Textual information that the encoder wishes to record with the
  1298.          image can be stored in tEXt chunks.  Each tEXt chunk contains a
  1299.          keyword and a text string, in the format:
  1300.  
  1301.             Keyword:        1-79 bytes (character string)
  1302.             Null separator: 1 byte
  1303.             Text:           n bytes (character string)
  1304.  
  1305.          The keyword and text string are separated by a zero byte (null
  1306.          character).  Neither the keyword nor the text string can
  1307.          contain a null character.  Note that the text string is not
  1308.          null-terminated (the length of the chunk is sufficient
  1309.          information to locate the ending).  The keyword must be at
  1310.          least one character and less than 80 characters long.  The text
  1311.          string can be of any length from zero bytes up to the maximum
  1312.          permissible chunk size less the length of the keyword and
  1313.          separator.
  1314.  
  1315.          Any number of tEXt chunks can appear, and more than one with
  1316.          the same keyword is permissible.
  1317.  
  1318.          The keyword indicates the type of information represented by
  1319.          the text string.  The following keywords are predefined and
  1320.          should be used where appropriate:
  1321.  
  1322.             Title            Short (one line) title or caption for image
  1323.             Author           Name of image's creator
  1324.             Description      Description of image (possibly long)
  1325.             Copyright        Copyright notice
  1326.             Creation Time    Time of original image creation
  1327.             Software         Software used to create the image
  1328.             Disclaimer       Legal disclaimer
  1329.             Warning          Warning of nature of content
  1330.             Source           Device used to create the image
  1331.             Comment          Miscellaneous comment; conversion from
  1332.                              GIF comment
  1333.  
  1334.          For the Creation Time keyword, the date format defined in
  1335.          section 5.2.14 of RFC 1123 is suggested, but not required
  1336.          [RFC-1123].  Decoders should allow for free-format text
  1337.          associated with this or any other keyword.
  1338.  
  1339.          Other keywords may be invented for other purposes.  Keywords of
  1340.          general interest can be registered with the maintainers of the
  1341.          PNG specification.  However, it is also permitted to use
  1342.          private unregistered keywords.  (Private keywords should be
  1343.  
  1344.  
  1345.  
  1346. Boutell, et. al.             Informational                     [Page 24]
  1347.  
  1348. RFC 2083            PNG: Portable Network Graphics            March 1997
  1349.  
  1350.  
  1351.          reasonably self-explanatory, in order to minimize the chance
  1352.          that the same keyword will be used for incompatible purposes by
  1353.          different people.)
  1354.  
  1355.          Both keyword and text are interpreted according to the ISO
  1356.          8859-1 (Latin-1) character set [ISO-8859].  The text string can
  1357.          contain any Latin-1 character.  Newlines in the text string
  1358.          should be represented by a single linefeed character (decimal
  1359.          10); use of other control characters in the text is
  1360.          discouraged.
  1361.  
  1362.          Keywords must contain only printable Latin-1 characters and
  1363.          spaces; that is, only character codes 32-126 and 161-255
  1364.          decimal are allowed.  To reduce the chances for human
  1365.          misreading of a keyword, leading and trailing spaces are
  1366.          forbidden, as are consecutive spaces.  Note also that the non-
  1367.          breaking space (code 160) is not permitted in keywords, since
  1368.          it is visually indistinguishable from an ordinary space.
  1369.  
  1370.          Keywords must be spelled exactly as registered, so that
  1371.          decoders can use simple literal comparisons when looking for
  1372.          particular keywords.  In particular, keywords are considered
  1373.          case-sensitive.
  1374.  
  1375.          See Recommendations for Encoders: Text chunk processing
  1376.          (Section 9.7) and Recommendations for Decoders: Text chunk
  1377.          processing (Section 10.11).
  1378.  
  1379.       4.2.8. tIME Image last-modification time
  1380.  
  1381.          The tIME chunk gives the time of the last image modification
  1382.          (not the time of initial image creation).  It contains:
  1383.  
  1384.             Year:   2 bytes (complete; for example, 1995, not 95)
  1385.             Month:  1 byte (1-12)
  1386.             Day:    1 byte (1-31)
  1387.             Hour:   1 byte (0-23)
  1388.             Minute: 1 byte (0-59)
  1389.             Second: 1 byte (0-60)    (yes, 60, for leap seconds; not 61,
  1390.                                       a common error)
  1391.  
  1392.          Universal Time (UTC, also called GMT) should be specified
  1393.          rather than local time.
  1394.  
  1395.  
  1396.  
  1397.  
  1398.  
  1399.  
  1400.  
  1401.  
  1402. Boutell, et. al.             Informational                     [Page 25]
  1403.  
  1404. RFC 2083            PNG: Portable Network Graphics            March 1997
  1405.  
  1406.  
  1407.          The tIME chunk is intended for use as an automatically-applied
  1408.          time stamp that is updated whenever the image data is changed.
  1409.          It is recommended that tIME not be changed by PNG editors that
  1410.          do not change the image data.  See also the Creation Time tEXt
  1411.          keyword, which can be used for a user-supplied time.
  1412.  
  1413.       4.2.9. tRNS Transparency
  1414.  
  1415.          The tRNS chunk specifies that the image uses simple
  1416.          transparency: either alpha values associated with palette
  1417.          entries (for indexed-color images) or a single transparent
  1418.          color (for grayscale and truecolor images).  Although simple
  1419.          transparency is not as elegant as the full alpha channel, it
  1420.          requires less storage space and is sufficient for many common
  1421.          cases.
  1422.  
  1423.          For color type 3 (indexed color), the tRNS chunk contains a
  1424.          series of one-byte alpha values, corresponding to entries in
  1425.          the PLTE chunk:
  1426.  
  1427.             Alpha for palette index 0:  1 byte
  1428.             Alpha for palette index 1:  1 byte
  1429.             ... etc ...
  1430.  
  1431.          Each entry indicates that pixels of the corresponding palette
  1432.          index must be treated as having the specified alpha value.
  1433.          Alpha values have the same interpretation as in an 8-bit full
  1434.          alpha channel: 0 is fully transparent, 255 is fully opaque,
  1435.          regardless of image bit depth. The tRNS chunk must not contain
  1436.          more alpha values than there are palette entries, but tRNS can
  1437.          contain fewer values than there are palette entries.  In this
  1438.          case, the alpha value for all remaining palette entries is
  1439.          assumed to be 255.  In the common case in which only palette
  1440.          index 0 need be made transparent, only a one-byte tRNS chunk is
  1441.          needed.
  1442.  
  1443.          For color type 0 (grayscale), the tRNS chunk contains a single
  1444.          gray level value, stored in the format:
  1445.  
  1446.             Gray:  2 bytes, range 0 .. (2^bitdepth)-1
  1447.  
  1448.          (For consistency, 2 bytes are used regardless of the image bit
  1449.          depth.) Pixels of the specified gray level are to be treated as
  1450.          transparent (equivalent to alpha value 0); all other pixels are
  1451.          to be treated as fully opaque (alpha value (2^bitdepth)-1).
  1452.  
  1453.  
  1454.  
  1455.  
  1456.  
  1457.  
  1458. Boutell, et. al.             Informational                     [Page 26]
  1459.  
  1460. RFC 2083            PNG: Portable Network Graphics            March 1997
  1461.  
  1462.  
  1463.          For color type 2 (truecolor), the tRNS chunk contains a single
  1464.          RGB color value, stored in the format:
  1465.  
  1466.             Red:   2 bytes, range 0 .. (2^bitdepth)-1
  1467.             Green: 2 bytes, range 0 .. (2^bitdepth)-1
  1468.             Blue:  2 bytes, range 0 .. (2^bitdepth)-1
  1469.  
  1470.          (For consistency, 2 bytes per sample are used regardless of the
  1471.          image bit depth.) Pixels of the specified color value are to be
  1472.          treated as transparent (equivalent to alpha value 0); all other
  1473.          pixels are to be treated as fully opaque (alpha value
  1474.          (2^bitdepth)-1).
  1475.  
  1476.          tRNS is prohibited for color types 4 and 6, since a full alpha
  1477.          channel is already present in those cases.
  1478.  
  1479.          Note: when dealing with 16-bit grayscale or truecolor data, it
  1480.          is important to compare both bytes of the sample values to
  1481.          determine whether a pixel is transparent.  Although decoders
  1482.          may drop the low-order byte of the samples for display, this
  1483.          must not occur until after the data has been tested for
  1484.          transparency.  For example, if the grayscale level 0x0001 is
  1485.          specified to be transparent, it would be incorrect to compare
  1486.          only the high-order byte and decide that 0x0002 is also
  1487.          transparent.
  1488.  
  1489.          When present, the tRNS chunk must precede the first IDAT chunk,
  1490.          and must follow the PLTE chunk, if any.
  1491.  
  1492.       4.2.10. zTXt Compressed textual data
  1493.  
  1494.          The zTXt chunk contains textual data, just as tEXt does;
  1495.          however, zTXt takes advantage of compression.  zTXt and tEXt
  1496.          chunks are semantically equivalent, but zTXt is recommended for
  1497.          storing large blocks of text.
  1498.  
  1499.          A zTXt chunk contains:
  1500.  
  1501.             Keyword:            1-79 bytes (character string)
  1502.             Null separator:     1 byte
  1503.             Compression method: 1 byte
  1504.             Compressed text:    n bytes
  1505.  
  1506.          The keyword and null separator are exactly the same as in the
  1507.          tEXt chunk.  Note that the keyword is not compressed.  The
  1508.          compression method byte identifies the compression method used
  1509.          in this zTXt chunk.  The only value presently defined for it is
  1510.          0 (deflate/inflate compression). The compression method byte is
  1511.  
  1512.  
  1513.  
  1514. Boutell, et. al.             Informational                     [Page 27]
  1515.  
  1516. RFC 2083            PNG: Portable Network Graphics            March 1997
  1517.  
  1518.  
  1519.          followed by a compressed datastream that makes up the remainder
  1520.          of the chunk.  For compression method 0, this datastream
  1521.          adheres to the zlib datastream format (see Deflate/Inflate
  1522.          Compression, Chapter 5).  Decompression of this datastream
  1523.          yields Latin-1 text that is identical to the text that would be
  1524.          stored in an equivalent tEXt chunk.
  1525.  
  1526.          Any number of zTXt and tEXt chunks can appear in the same file.
  1527.          See the preceding definition of the tEXt chunk for the
  1528.          predefined keywords and the recommended format of the text.
  1529.  
  1530.          See Recommendations for Encoders: Text chunk processing
  1531.          (Section 9.7), and Recommendations for Decoders: Text chunk
  1532.          processing (Section 10.11).
  1533.  
  1534.    4.3. Summary of standard chunks
  1535.  
  1536.       This table summarizes some properties of the standard chunk types.
  1537.  
  1538.          Critical chunks (must appear in this order, except PLTE
  1539.                           is optional):
  1540.  
  1541.                  Name  Multiple  Ordering constraints
  1542.                          OK?
  1543.  
  1544.                  IHDR    No      Must be first
  1545.                  PLTE    No      Before IDAT
  1546.                  IDAT    Yes     Multiple IDATs must be consecutive
  1547.                  IEND    No      Must be last
  1548.  
  1549.          Ancillary chunks (need not appear in this order):
  1550.  
  1551.                  Name  Multiple  Ordering constraints
  1552.                          OK?
  1553.  
  1554.                  cHRM    No      Before PLTE and IDAT
  1555.                  gAMA    No      Before PLTE and IDAT
  1556.                  sBIT    No      Before PLTE and IDAT
  1557.                  bKGD    No      After PLTE; before IDAT
  1558.                  hIST    No      After PLTE; before IDAT
  1559.                  tRNS    No      After PLTE; before IDAT
  1560.                  pHYs    No      Before IDAT
  1561.                  tIME    No      None
  1562.                  tEXt    Yes     None
  1563.                  zTXt    Yes     None
  1564.  
  1565.  
  1566.  
  1567.  
  1568.  
  1569.  
  1570. Boutell, et. al.             Informational                     [Page 28]
  1571.  
  1572. RFC 2083            PNG: Portable Network Graphics            March 1997
  1573.  
  1574.  
  1575.       Standard keywords for tEXt and zTXt chunks:
  1576.  
  1577.          Title            Short (one line) title or caption for image
  1578.          Author           Name of image's creator
  1579.          Description      Description of image (possibly long)
  1580.          Copyright        Copyright notice
  1581.          Creation Time    Time of original image creation
  1582.          Software         Software used to create the image
  1583.          Disclaimer       Legal disclaimer
  1584.          Warning          Warning of nature of content
  1585.          Source           Device used to create the image
  1586.          Comment          Miscellaneous comment; conversion from
  1587.                           GIF comment
  1588.  
  1589.    4.4. Additional chunk types
  1590.  
  1591.       Additional public PNG chunk types are defined in the document "PNG
  1592.       Special-Purpose Public Chunks" [PNG-EXTENSIONS].  Chunks described
  1593.       there are expected to be less widely supported than those defined
  1594.       in this specification.  However, application authors are
  1595.       encouraged to use those chunk types whenever appropriate for their
  1596.       applications.  Additional chunk types can be proposed for
  1597.       inclusion in that list by contacting the PNG specification
  1598.       maintainers at png-info@uunet.uu.net or at png-group@w3.org.
  1599.  
  1600.       New public chunks will only be registered if they are of use to
  1601.       others and do not violate the design philosophy of PNG. Chunk
  1602.       registration is not automatic, although it is the intent of the
  1603.       authors that it be straightforward when a new chunk of potentially
  1604.       wide application is needed.  Note that the creation of new
  1605.       critical chunk types is discouraged unless absolutely necessary.
  1606.  
  1607.       Applications can also use private chunk types to carry data that
  1608.       is not of interest to other applications.  See Recommendations for
  1609.       Encoders: Use of private chunks (Section 9.8).
  1610.  
  1611.       Decoders must be prepared to encounter unrecognized public or
  1612.       private chunk type codes.  Unrecognized chunk types must be
  1613.       handled as described in Chunk naming conventions (Section 3.3).
  1614.  
  1615. 5. Deflate/Inflate Compression
  1616.  
  1617.    PNG compression method 0 (the only compression method presently
  1618.    defined for PNG) specifies deflate/inflate compression with a 32K
  1619.    sliding window.  Deflate compression is an LZ77 derivative used in
  1620.    zip, gzip, pkzip and related programs.  Extensive research has been
  1621.    done supporting its patent-free status.  Portable C implementations
  1622.    are freely available.
  1623.  
  1624.  
  1625.  
  1626. Boutell, et. al.             Informational                     [Page 29]
  1627.  
  1628. RFC 2083            PNG: Portable Network Graphics            March 1997
  1629.  
  1630.  
  1631.    Deflate-compressed datastreams within PNG are stored in the "zlib"
  1632.    format, which has the structure:
  1633.  
  1634.       Compression method/flags code: 1 byte
  1635.       Additional flags/check bits:   1 byte
  1636.       Compressed data blocks:        n bytes
  1637.       Check value:                   4 bytes
  1638.  
  1639.    Further details on this format are given in the zlib specification
  1640.    [RFC-1950].
  1641.  
  1642.    For PNG compression method 0, the zlib compression method/flags code
  1643.    must specify method code 8 ("deflate" compression) and an LZ77 window
  1644.    size of not more than 32K.  Note that the zlib compression method
  1645.    number is not the same as the PNG compression method number.  The
  1646.    additional flags must not specify a preset dictionary.
  1647.  
  1648.    The compressed data within the zlib datastream is stored as a series
  1649.    of blocks, each of which can represent raw (uncompressed) data,
  1650.    LZ77-compressed data encoded with fixed Huffman codes, or LZ77-
  1651.    compressed data encoded with custom Huffman codes.  A marker bit in
  1652.    the final block identifies it as the last block, allowing the decoder
  1653.    to recognize the end of the compressed datastream.  Further details
  1654.    on the compression algorithm and the encoding are given in the
  1655.    deflate specification [RFC-1951].
  1656.  
  1657.    The check value stored at the end of the zlib datastream is
  1658.    calculated on the uncompressed data represented by the datastream.
  1659.    Note that the algorithm used is not the same as the CRC calculation
  1660.    used for PNG chunk check values.  The zlib check value is useful
  1661.    mainly as a cross-check that the deflate and inflate algorithms are
  1662.    implemented correctly.  Verifying the chunk CRCs provides adequate
  1663.    confidence that the PNG file has been transmitted undamaged.
  1664.  
  1665.    In a PNG file, the concatenation of the contents of all the IDAT
  1666.    chunks makes up a zlib datastream as specified above.  This
  1667.    datastream decompresses to filtered image data as described elsewhere
  1668.    in this document.
  1669.  
  1670.    It is important to emphasize that the boundaries between IDAT chunks
  1671.    are arbitrary and can fall anywhere in the zlib datastream.  There is
  1672.    not necessarily any correlation between IDAT chunk boundaries and
  1673.    deflate block boundaries or any other feature of the zlib data.  For
  1674.    example, it is entirely possible for the terminating zlib check value
  1675.    to be split across IDAT chunks.
  1676.  
  1677.  
  1678.  
  1679.  
  1680.  
  1681.  
  1682. Boutell, et. al.             Informational                     [Page 30]
  1683.  
  1684. RFC 2083            PNG: Portable Network Graphics            March 1997
  1685.  
  1686.  
  1687.    In the same vein, there is no required correlation between the
  1688.    structure of the image data (i.e., scanline boundaries) and deflate
  1689.    block boundaries or IDAT chunk boundaries.  The complete image data
  1690.    is represented by a single zlib datastream that is stored in some
  1691.    number of IDAT chunks; a decoder that assumes any more than this is
  1692.    incorrect.  (Of course, some encoder implementations may emit files
  1693.    in which some of these structures are indeed related.  But decoders
  1694.    cannot rely on this.)
  1695.  
  1696.    PNG also uses zlib datastreams in zTXt chunks.  In a zTXt chunk, the
  1697.    remainder of the chunk following the compression method byte is a
  1698.    zlib datastream as specified above.  This datastream decompresses to
  1699.    the user-readable text described by the chunk's keyword.  Unlike the
  1700.    image data, such datastreams are not split across chunks; each zTXt
  1701.    chunk contains an independent zlib datastream.
  1702.  
  1703.    Additional documentation and portable C code for deflate and inflate
  1704.    are available from the Info-ZIP archives at
  1705.    <URL:ftp://ftp.uu.net/pub/archiving/zip/>.
  1706.  
  1707. 6. Filter Algorithms
  1708.  
  1709.    This chapter describes the filter algorithms that can be applied
  1710.    before compression.  The purpose of these filters is to prepare the
  1711.    image data for optimum compression.
  1712.  
  1713.    6.1. Filter types
  1714.  
  1715.       PNG filter method 0 defines five basic filter types:
  1716.  
  1717.          Type    Name
  1718.  
  1719.          0       None
  1720.          1       Sub
  1721.          2       Up
  1722.          3       Average
  1723.          4       Paeth
  1724.  
  1725.       (Note that filter method 0 in IHDR specifies exactly this set of
  1726.       five filter types.  If the set of filter types is ever extended, a
  1727.       different filter method number will be assigned to the extended
  1728.       set, so that decoders need not decompress the data to discover
  1729.       that it contains unsupported filter types.)
  1730.  
  1731.       The encoder can choose which of these filter algorithms to apply
  1732.       on a scanline-by-scanline basis.  In the image data sent to the
  1733.       compression step, each scanline is preceded by a filter type byte
  1734.       that specifies the filter algorithm used for that scanline.
  1735.  
  1736.  
  1737.  
  1738. Boutell, et. al.             Informational                     [Page 31]
  1739.  
  1740. RFC 2083            PNG: Portable Network Graphics            March 1997
  1741.  
  1742.  
  1743.       Filtering algorithms are applied to bytes, not to pixels,
  1744.       regardless of the bit depth or color type of the image.  The
  1745.       filtering algorithms work on the byte sequence formed by a
  1746.       scanline that has been represented as described in Image layout
  1747.       (Section 2.3).  If the image includes an alpha channel, the alpha
  1748.       data is filtered in the same way as the image data.
  1749.  
  1750.       When the image is interlaced, each pass of the interlace pattern
  1751.       is treated as an independent image for filtering purposes.  The
  1752.       filters work on the byte sequences formed by the pixels actually
  1753.       transmitted during a pass, and the "previous scanline" is the one
  1754.       previously transmitted in the same pass, not the one adjacent in
  1755.       the complete image.  Note that the subimage transmitted in any one
  1756.       pass is always rectangular, but is of smaller width and/or height
  1757.       than the complete image.  Filtering is not applied when this
  1758.       subimage is empty.
  1759.  
  1760.       For all filters, the bytes "to the left of" the first pixel in a
  1761.       scanline must be treated as being zero.  For filters that refer to
  1762.       the prior scanline, the entire prior scanline must be treated as
  1763.       being zeroes for the first scanline of an image (or of a pass of
  1764.       an interlaced image).
  1765.  
  1766.       To reverse the effect of a filter, the decoder must use the
  1767.       decoded values of the prior pixel on the same line, the pixel
  1768.       immediately above the current pixel on the prior line, and the
  1769.       pixel just to the left of the pixel above.  This implies that at
  1770.       least one scanline's worth of image data will have to be stored by
  1771.       the decoder at all times.  Even though some filter types do not
  1772.       refer to the prior scanline, the decoder will always need to store
  1773.       each scanline as it is decoded, since the next scanline might use
  1774.       a filter that refers to it.
  1775.  
  1776.       PNG imposes no restriction on which filter types can be applied to
  1777.       an image.  However, the filters are not equally effective on all
  1778.       types of data.  See Recommendations for Encoders: Filter selection
  1779.       (Section 9.6).
  1780.  
  1781.       See also Rationale: Filtering (Section 12.9).
  1782.  
  1783.    6.2. Filter type 0: None
  1784.  
  1785.       With the None filter, the scanline is transmitted unmodified; it
  1786.       is only necessary to insert a filter type byte before the data.
  1787.  
  1788.  
  1789.  
  1790.  
  1791.  
  1792.  
  1793.  
  1794. Boutell, et. al.             Informational                     [Page 32]
  1795.  
  1796. RFC 2083            PNG: Portable Network Graphics            March 1997
  1797.  
  1798.  
  1799.    6.3. Filter type 1: Sub
  1800.  
  1801.       The Sub filter transmits the difference between each byte and the
  1802.       value of the corresponding byte of the prior pixel.
  1803.  
  1804.       To compute the Sub filter, apply the following formula to each
  1805.       byte of the scanline:
  1806.  
  1807.          Sub(x) = Raw(x) - Raw(x-bpp)
  1808.  
  1809.       where x ranges from zero to the number of bytes representing the
  1810.       scanline minus one, Raw(x) refers to the raw data byte at that
  1811.       byte position in the scanline, and bpp is defined as the number of
  1812.       bytes per complete pixel, rounding up to one. For example, for
  1813.       color type 2 with a bit depth of 16, bpp is equal to 6 (three
  1814.       samples, two bytes per sample); for color type 0 with a bit depth
  1815.       of 2, bpp is equal to 1 (rounding up); for color type 4 with a bit
  1816.       depth of 16, bpp is equal to 4 (two-byte grayscale sample, plus
  1817.       two-byte alpha sample).
  1818.  
  1819.       Note this computation is done for each byte, regardless of bit
  1820.       depth.  In a 16-bit image, each MSB is predicted from the
  1821.       preceding MSB and each LSB from the preceding LSB, because of the
  1822.       way that bpp is defined.
  1823.  
  1824.       Unsigned arithmetic modulo 256 is used, so that both the inputs
  1825.       and outputs fit into bytes.  The sequence of Sub values is
  1826.       transmitted as the filtered scanline.
  1827.  
  1828.       For all x < 0, assume Raw(x) = 0.
  1829.  
  1830.       To reverse the effect of the Sub filter after decompression,
  1831.       output the following value:
  1832.  
  1833.          Sub(x) + Raw(x-bpp)
  1834.  
  1835.       (computed mod 256), where Raw refers to the bytes already decoded.
  1836.  
  1837.    6.4. Filter type 2: Up
  1838.  
  1839.       The Up filter is just like the Sub filter except that the pixel
  1840.       immediately above the current pixel, rather than just to its left,
  1841.       is used as the predictor.
  1842.  
  1843.       To compute the Up filter, apply the following formula to each byte
  1844.       of the scanline:
  1845.  
  1846.          Up(x) = Raw(x) - Prior(x)
  1847.  
  1848.  
  1849.  
  1850. Boutell, et. al.             Informational                     [Page 33]
  1851.  
  1852. RFC 2083            PNG: Portable Network Graphics            March 1997
  1853.  
  1854.  
  1855.       where x ranges from zero to the number of bytes representing the
  1856.       scanline minus one, Raw(x) refers to the raw data byte at that
  1857.       byte position in the scanline, and Prior(x) refers to the
  1858.       unfiltered bytes of the prior scanline.
  1859.  
  1860.       Note this is done for each byte, regardless of bit depth.
  1861.       Unsigned arithmetic modulo 256 is used, so that both the inputs
  1862.       and outputs fit into bytes.  The sequence of Up values is
  1863.       transmitted as the filtered scanline.
  1864.  
  1865.       On the first scanline of an image (or of a pass of an interlaced
  1866.       image), assume Prior(x) = 0 for all x.
  1867.  
  1868.       To reverse the effect of the Up filter after decompression, output
  1869.       the following value:
  1870.  
  1871.          Up(x) + Prior(x)
  1872.  
  1873.       (computed mod 256), where Prior refers to the decoded bytes of the
  1874.       prior scanline.
  1875.  
  1876.    6.5. Filter type 3: Average
  1877.  
  1878.       The Average filter uses the average of the two neighboring pixels
  1879.       (left and above) to predict the value of a pixel.
  1880.  
  1881.       To compute the Average filter, apply the following formula to each
  1882.       byte of the scanline:
  1883.  
  1884.          Average(x) = Raw(x) - floor((Raw(x-bpp)+Prior(x))/2)
  1885.  
  1886.       where x ranges from zero to the number of bytes representing the
  1887.       scanline minus one, Raw(x) refers to the raw data byte at that
  1888.       byte position in the scanline, Prior(x) refers to the unfiltered
  1889.       bytes of the prior scanline, and bpp is defined as for the Sub
  1890.       filter.
  1891.  
  1892.       Note this is done for each byte, regardless of bit depth.  The
  1893.       sequence of Average values is transmitted as the filtered
  1894.       scanline.
  1895.  
  1896.       The subtraction of the predicted value from the raw byte must be
  1897.       done modulo 256, so that both the inputs and outputs fit into
  1898.       bytes.  However, the sum Raw(x-bpp)+Prior(x) must be formed
  1899.       without overflow (using at least nine-bit arithmetic).  floor()
  1900.       indicates that the result of the division is rounded to the next
  1901.       lower integer if fractional; in other words, it is an integer
  1902.       division or right shift operation.
  1903.  
  1904.  
  1905.  
  1906. Boutell, et. al.             Informational                     [Page 34]
  1907.  
  1908. RFC 2083            PNG: Portable Network Graphics            March 1997
  1909.  
  1910.  
  1911.       For all x < 0, assume Raw(x) = 0.  On the first scanline of an
  1912.       image (or of a pass of an interlaced image), assume Prior(x) = 0
  1913.       for all x.
  1914.  
  1915.       To reverse the effect of the Average filter after decompression,
  1916.       output the following value:
  1917.  
  1918.          Average(x) + floor((Raw(x-bpp)+Prior(x))/2)
  1919.  
  1920.       where the result is computed mod 256, but the prediction is
  1921.       calculated in the same way as for encoding.  Raw refers to the
  1922.       bytes already decoded, and Prior refers to the decoded bytes of
  1923.       the prior scanline.
  1924.  
  1925.    6.6. Filter type 4: Paeth
  1926.  
  1927.       The Paeth filter computes a simple linear function of the three
  1928.       neighboring pixels (left, above, upper left), then chooses as
  1929.       predictor the neighboring pixel closest to the computed value.
  1930.       This technique is due to Alan W. Paeth [PAETH].
  1931.  
  1932.       To compute the Paeth filter, apply the following formula to each
  1933.       byte of the scanline:
  1934.  
  1935.          Paeth(x) = Raw(x) - PaethPredictor(Raw(x-bpp), Prior(x),
  1936.                                             Prior(x-bpp))
  1937.  
  1938.       where x ranges from zero to the number of bytes representing the
  1939.       scanline minus one, Raw(x) refers to the raw data byte at that
  1940.       byte position in the scanline, Prior(x) refers to the unfiltered
  1941.       bytes of the prior scanline, and bpp is defined as for the Sub
  1942.       filter.
  1943.  
  1944.       Note this is done for each byte, regardless of bit depth.
  1945.       Unsigned arithmetic modulo 256 is used, so that both the inputs
  1946.       and outputs fit into bytes.  The sequence of Paeth values is
  1947.       transmitted as the filtered scanline.
  1948.  
  1949.  
  1950.  
  1951.  
  1952.  
  1953.  
  1954.  
  1955.  
  1956.  
  1957.  
  1958.  
  1959.  
  1960.  
  1961.  
  1962. Boutell, et. al.             Informational                     [Page 35]
  1963.  
  1964. RFC 2083            PNG: Portable Network Graphics            March 1997
  1965.  
  1966.  
  1967.       The PaethPredictor function is defined by the following
  1968.       pseudocode:
  1969.  
  1970.          function PaethPredictor (a, b, c)
  1971.          begin
  1972.               ; a = left, b = above, c = upper left
  1973.               p := a + b - c        ; initial estimate
  1974.               pa := abs(p - a)      ; distances to a, b, c
  1975.               pb := abs(p - b)
  1976.               pc := abs(p - c)
  1977.               ; return nearest of a,b,c,
  1978.               ; breaking ties in order a,b,c.
  1979.               if pa <= pb AND pa <= pc then return a
  1980.               else if pb <= pc then return b
  1981.               else return c
  1982.          end
  1983.  
  1984.       The calculations within the PaethPredictor function must be
  1985.       performed exactly, without overflow.  Arithmetic modulo 256 is to
  1986.       be used only for the final step of subtracting the function result
  1987.       from the target byte value.
  1988.  
  1989.       Note that the order in which ties are broken is critical and must
  1990.       not be altered.  The tie break order is: pixel to the left, pixel
  1991.       above, pixel to the upper left.  (This order differs from that
  1992.       given in Paeth's article.)
  1993.  
  1994.       For all x < 0, assume Raw(x) = 0 and Prior(x) = 0.  On the first
  1995.       scanline of an image (or of a pass of an interlaced image), assume
  1996.       Prior(x) = 0 for all x.
  1997.  
  1998.       To reverse the effect of the Paeth filter after decompression,
  1999.       output the following value:
  2000.  
  2001.          Paeth(x) + PaethPredictor(Raw(x-bpp), Prior(x), Prior(x-bpp))
  2002.  
  2003.       (computed mod 256), where Raw and Prior refer to bytes already
  2004.       decoded.  Exactly the same PaethPredictor function is used by both
  2005.       encoder and decoder.
  2006.  
  2007. 7. Chunk Ordering Rules
  2008.  
  2009.    To allow new chunk types to be added to PNG, it is necessary to
  2010.    establish rules about the ordering requirements for all chunk types.
  2011.    Otherwise a PNG editing program cannot know what to do when it
  2012.    encounters an unknown chunk.
  2013.  
  2014.  
  2015.  
  2016.  
  2017.  
  2018. Boutell, et. al.             Informational                     [Page 36]
  2019.  
  2020. RFC 2083            PNG: Portable Network Graphics            March 1997
  2021.  
  2022.  
  2023.    We define a "PNG editor" as a program that modifies a PNG file and
  2024.    wishes to preserve as much as possible of the ancillary information
  2025.    in the file.  Two examples of PNG editors are a program that adds or
  2026.    modifies text chunks, and a program that adds a suggested palette to
  2027.    a truecolor PNG file.  Ordinary image editors are not PNG editors in
  2028.    this sense, because they usually discard all unrecognized information
  2029.    while reading in an image.  (Note: we strongly encourage programs
  2030.    handling PNG files to preserve ancillary information whenever
  2031.    possible.)
  2032.  
  2033.    As an example of possible problems, consider a hypothetical new
  2034.    ancillary chunk type that is safe-to-copy and is required to appear
  2035.    after PLTE if PLTE is present.  If our program to add a suggested
  2036.    PLTE does not recognize this new chunk, it may insert PLTE in the
  2037.    wrong place, namely after the new chunk.  We could prevent such
  2038.    problems by requiring PNG editors to discard all unknown chunks, but
  2039.    that is a very unattractive solution.  Instead, PNG requires
  2040.    ancillary chunks not to have ordering restrictions like this.
  2041.  
  2042.    To prevent this type of problem while allowing for future extension,
  2043.    we put some constraints on both the behavior of PNG editors and the
  2044.    allowed ordering requirements for chunks.
  2045.  
  2046.    7.1. Behavior of PNG editors
  2047.  
  2048.       The rules for PNG editors are:
  2049.  
  2050.           * When copying an unknown unsafe-to-copy ancillary chunk, a
  2051.             PNG editor must not move the chunk relative to any critical
  2052.             chunk.  It can relocate the chunk freely relative to other
  2053.             ancillary chunks that occur between the same pair of
  2054.             critical chunks.  (This is well defined since the editor
  2055.             must not add, delete, modify, or reorder critical chunks if
  2056.             it is preserving unknown unsafe-to-copy chunks.)
  2057.           * When copying an unknown safe-to-copy ancillary chunk, a PNG
  2058.             editor must not move the chunk from before IDAT to after
  2059.             IDAT or vice versa.  (This is well defined because IDAT is
  2060.             always present.)  Any other reordering is permitted.
  2061.           * When copying a known ancillary chunk type, an editor need
  2062.             only honor the specific chunk ordering rules that exist for
  2063.             that chunk type.  However, it can always choose to apply the
  2064.             above general rules instead.
  2065.           * PNG editors must give up on encountering an unknown critical
  2066.             chunk type, because there is no way to be certain that a
  2067.             valid file will result from modifying a file containing such
  2068.             a chunk.  (Note that simply discarding the chunk is not good
  2069.             enough, because it might have unknown implications for the
  2070.             interpretation of other chunks.)
  2071.  
  2072.  
  2073.  
  2074. Boutell, et. al.             Informational                     [Page 37]
  2075.  
  2076. RFC 2083            PNG: Portable Network Graphics            March 1997
  2077.  
  2078.  
  2079.       These rules are expressed in terms of copying chunks from an input
  2080.       file to an output file, but they apply in the obvious way if a PNG
  2081.       file is modified in place.
  2082.  
  2083.       See also Chunk naming conventions (Section 3.3).
  2084.  
  2085.    7.2. Ordering of ancillary chunks
  2086.  
  2087.       The ordering rules for an ancillary chunk type cannot be any
  2088.       stricter than this:
  2089.  
  2090.           * Unsafe-to-copy chunks can have ordering requirements
  2091.             relative to critical chunks.
  2092.           * Safe-to-copy chunks can have ordering requirements relative
  2093.             to IDAT.
  2094.  
  2095.       The actual ordering rules for any particular ancillary chunk type
  2096.       may be weaker.  See for example the ordering rules for the
  2097.       standard ancillary chunk types (Summary of standard chunks,
  2098.       Section 4.3).
  2099.  
  2100.       Decoders must not assume more about the positioning of any
  2101.       ancillary chunk than is specified by the chunk ordering rules.  In
  2102.       particular, it is never valid to assume that a specific ancillary
  2103.       chunk type occurs with any particular positioning relative to
  2104.       other ancillary chunks.  (For example, it is unsafe to assume that
  2105.       your private ancillary chunk occurs immediately before IEND.  Even
  2106.       if your application always writes it there, a PNG editor might
  2107.       have inserted some other ancillary chunk after it.  But you can
  2108.       safely assume that your chunk will remain somewhere between IDAT
  2109.       and IEND.)
  2110.  
  2111.    7.3. Ordering of critical chunks
  2112.  
  2113.       Critical chunks can have arbitrary ordering requirements, because
  2114.       PNG editors are required to give up if they encounter unknown
  2115.       critical chunks.  For example, IHDR has the special ordering rule
  2116.       that it must always appear first.  A PNG editor, or indeed any
  2117.       PNG-writing program, must know and follow the ordering rules for
  2118.       any critical chunk type that it can emit.
  2119.  
  2120.  
  2121.  
  2122.  
  2123.  
  2124.  
  2125.  
  2126.  
  2127.  
  2128.  
  2129.  
  2130. Boutell, et. al.             Informational                     [Page 38]
  2131.  
  2132. RFC 2083            PNG: Portable Network Graphics            March 1997
  2133.  
  2134.  
  2135. 8. Miscellaneous Topics
  2136.  
  2137.    8.1. File name extension
  2138.  
  2139.       On systems where file names customarily include an extension
  2140.       signifying file type, the extension ".png" is recommended for PNG
  2141.       files.  Lower case ".png" is preferred if file names are case-
  2142.       sensitive.
  2143.  
  2144.    8.2. Internet media type
  2145.  
  2146.       The Internet Assigned Numbers Authority (IANA) has registered
  2147.       "image/png" as the Internet Media Type for PNG [RFC-2045, RFC-
  2148.       2048].  For robustness, decoders may choose to also support the
  2149.       interim media type "image/x-png" which was in use before
  2150.       registration was complete.
  2151.  
  2152.    8.3. Macintosh file layout
  2153.  
  2154.       In the Apple Macintosh system, the following conventions are
  2155.       recommended:
  2156.  
  2157.           * The four-byte file type code for PNG files is "PNGf".  (This
  2158.             code has been registered with Apple for PNG files.) The
  2159.             creator code will vary depending on the creating
  2160.             application.
  2161.           * The contents of the data fork must be a PNG file exactly as
  2162.             described in the rest of this specification.
  2163.           * The contents of the resource fork are unspecified.  It may
  2164.             be empty or may contain application-dependent resources.
  2165.           * When transferring a Macintosh PNG file to a non-Macintosh
  2166.             system, only the data fork should be transferred.
  2167.  
  2168.    8.4. Multiple-image extension
  2169.  
  2170.       PNG itself is strictly a single-image format.  However, it may be
  2171.       necessary to store multiple images within one file; for example,
  2172.       this is needed to convert some GIF files.  In the future, a
  2173.       multiple-image format based on PNG may be defined.  Such a format
  2174.       will be considered a separate file format and will have a
  2175.       different signature.  PNG-supporting applications may or may not
  2176.       choose to support the multiple-image format.
  2177.  
  2178.       See Rationale: Why not these features? (Section 12.3).
  2179.  
  2180.  
  2181.  
  2182.  
  2183.  
  2184.  
  2185.  
  2186. Boutell, et. al.             Informational                     [Page 39]
  2187.  
  2188. RFC 2083            PNG: Portable Network Graphics            March 1997
  2189.  
  2190.  
  2191.    8.5. Security considerations
  2192.  
  2193.       A PNG file or datastream is composed of a collection of explicitly
  2194.       typed "chunks".  Chunks whose contents are defined by the
  2195.       specification could actually contain anything, including malicious
  2196.       code.  But there is no known risk that such malicious code could
  2197.       be executed on the recipient's computer as a result of decoding
  2198.       the PNG image.
  2199.  
  2200.       The possible security risks associated with future chunk types
  2201.       cannot be specified at this time.  Security issues will be
  2202.       considered when evaluating chunks proposed for registration as
  2203.       public chunks.  There is no additional security risk associated
  2204.       with unknown or unimplemented chunk types, because such chunks
  2205.       will be ignored, or at most be copied into another PNG file.
  2206.  
  2207.       The tEXt and zTXt chunks contain data that is meant to be
  2208.       displayed as plain text.  It is possible that if the decoder
  2209.       displays such text without filtering out control characters,
  2210.       especially the ESC (escape) character, certain systems or
  2211.       terminals could behave in undesirable and insecure ways.  We
  2212.       recommend that decoders filter out control characters to avoid
  2213.       this risk; see Recommendations for Decoders: Text chunk processing
  2214.       (Section 10.11).
  2215.  
  2216.       Because every chunk's length is available at its beginning, and
  2217.       because every chunk has a CRC trailer, there is a very robust
  2218.       defense against corrupted data and against fraudulent chunks that
  2219.       attempt to overflow the decoder's buffers.  Also, the PNG
  2220.       signature bytes provide early detection of common file
  2221.       transmission errors.
  2222.  
  2223.       A decoder that fails to check CRCs could be subject to data
  2224.       corruption.  The only likely consequence of such corruption is
  2225.       incorrectly displayed pixels within the image.  Worse things might
  2226.       happen if the CRC of the IHDR chunk is not checked and the width
  2227.       or height fields are corrupted.  See Recommendations for Decoders:
  2228.       Error checking (Section 10.1).
  2229.  
  2230.       A poorly written decoder might be subject to buffer overflow,
  2231.       because chunks can be extremely large, up to (2^31)-1 bytes long.
  2232.       But properly written decoders will handle large chunks without
  2233.       difficulty.
  2234.  
  2235.  
  2236.  
  2237.  
  2238.  
  2239.  
  2240.  
  2241.  
  2242. Boutell, et. al.             Informational                     [Page 40]
  2243.  
  2244. RFC 2083            PNG: Portable Network Graphics            March 1997
  2245.  
  2246.  
  2247. 9. Recommendations for Encoders
  2248.  
  2249.    This chapter gives some recommendations for encoder behavior.  The
  2250.    only absolute requirement on a PNG encoder is that it produce files
  2251.    that conform to the format specified in the preceding chapters.
  2252.    However, best results will usually be achieved by following these
  2253.    recommendations.
  2254.  
  2255.    9.1. Sample depth scaling
  2256.  
  2257.       When encoding input samples that have a sample depth that cannot
  2258.       be directly represented in PNG, the encoder must scale the samples
  2259.       up to a sample depth that is allowed by PNG.  The most accurate
  2260.       scaling method is the linear equation
  2261.  
  2262.          output = ROUND(input * MAXOUTSAMPLE / MAXINSAMPLE)
  2263.  
  2264.       where the input samples range from 0 to MAXINSAMPLE and the
  2265.       outputs range from 0 to MAXOUTSAMPLE (which is (2^sampledepth)-1).
  2266.  
  2267.       A close approximation to the linear scaling method can be achieved
  2268.       by "left bit replication", which is shifting the valid bits to
  2269.       begin in the most significant bit and repeating the most
  2270.       significant bits into the open bits.  This method is often faster
  2271.       to compute than linear scaling.  As an example, assume that 5-bit
  2272.       samples are being scaled up to 8 bits.  If the source sample value
  2273.       is 27 (in the range from 0-31), then the original bits are:
  2274.  
  2275.          4 3 2 1 0
  2276.          ---------
  2277.          1 1 0 1 1
  2278.  
  2279.       Left bit replication gives a value of 222:
  2280.  
  2281.          7 6 5 4 3  2 1 0
  2282.          ----------------
  2283.          1 1 0 1 1  1 1 0
  2284.          |=======|  |===|
  2285.              |      Leftmost Bits Repeated to Fill Open Bits
  2286.              |
  2287.          Original Bits
  2288.  
  2289.       which matches the value computed by the linear equation.  Left bit
  2290.       replication usually gives the same value as linear scaling, and is
  2291.       never off by more than one.
  2292.  
  2293.  
  2294.  
  2295.  
  2296.  
  2297.  
  2298. Boutell, et. al.             Informational                     [Page 41]
  2299.  
  2300. RFC 2083            PNG: Portable Network Graphics            March 1997
  2301.  
  2302.  
  2303.       A distinctly less accurate approximation is obtained by simply
  2304.       left-shifting the input value and filling the low order bits with
  2305.       zeroes.  This scheme cannot reproduce white exactly, since it does
  2306.       not generate an all-ones maximum value; the net effect is to
  2307.       darken the image slightly.  This method is not recommended in
  2308.       general, but it does have the effect of improving compression,
  2309.       particularly when dealing with greater-than-eight-bit sample
  2310.       depths.  Since the relative error introduced by zero-fill scaling
  2311.       is small at high sample depths, some encoders may choose to use
  2312.       it.  Zero-fill must not be used for alpha channel data, however,
  2313.       since many decoders will special-case alpha values of all zeroes
  2314.       and all ones.  It is important to represent both those values
  2315.       exactly in the scaled data.
  2316.  
  2317.       When the encoder writes an sBIT chunk, it is required to do the
  2318.       scaling in such a way that the high-order bits of the stored
  2319.       samples match the original data.  That is, if the sBIT chunk
  2320.       specifies a sample depth of S, the high-order S bits of the stored
  2321.       data must agree with the original S-bit data values.  This allows
  2322.       decoders to recover the original data by shifting right.  The
  2323.       added low-order bits are not constrained.  Note that all the above
  2324.       scaling methods meet this restriction.
  2325.  
  2326.       When scaling up source data, it is recommended that the low-order
  2327.       bits be filled consistently for all samples; that is, the same
  2328.       source value should generate the same sample value at any pixel
  2329.       position.  This improves compression by reducing the number of
  2330.       distinct sample values.  However, this is not a requirement, and
  2331.       some encoders may choose not to follow it.  For example, an
  2332.       encoder might instead dither the low-order bits, improving
  2333.       displayed image quality at the price of increasing file size.
  2334.  
  2335.       In some applications the original source data may have a range
  2336.       that is not a power of 2.  The linear scaling equation still works
  2337.       for this case, although the shifting methods do not.  It is
  2338.       recommended that an sBIT chunk not be written for such images,
  2339.       since sBIT suggests that the original data range was exactly
  2340.       0..2^S-1.
  2341.  
  2342.    9.2. Encoder gamma handling
  2343.  
  2344.       See Gamma Tutorial (Chapter 13) if you aren't already familiar
  2345.       with gamma issues.
  2346.  
  2347.       Proper handling of gamma encoding and the gAMA chunk in an encoder
  2348.       depends on the prior history of the sample values and on whether
  2349.       these values have already been quantized to integers.
  2350.  
  2351.  
  2352.  
  2353.  
  2354. Boutell, et. al.             Informational                     [Page 42]
  2355.  
  2356. RFC 2083            PNG: Portable Network Graphics            March 1997
  2357.  
  2358.  
  2359.       If the encoder has access to sample intensity values in floating-
  2360.       point or high-precision integer form (perhaps from a computer
  2361.       image renderer), then it is recommended that the encoder perform
  2362.       its own gamma encoding before quantizing the data to integer
  2363.       values for storage in the file.  Applying gamma encoding at this
  2364.       stage results in images with fewer banding artifacts at a given
  2365.       sample depth, or allows smaller samples while retaining the same
  2366.       visual quality.
  2367.  
  2368.       A linear intensity level, expressed as a floating-point value in
  2369.       the range 0 to 1, can be converted to a gamma-encoded sample value
  2370.       by
  2371.  
  2372.          sample = ROUND((intensity ^ encoder_gamma) * MAXSAMPLE)
  2373.  
  2374.       The file_gamma value to be written in the PNG gAMA chunk is the
  2375.       same as encoder_gamma in this equation, since we are assuming the
  2376.       initial intensity value is linear (in effect, camera_gamma is
  2377.       1.0).
  2378.  
  2379.       If the image is being written to a file only, the encoder_gamma
  2380.       value can be selected somewhat arbitrarily.  Values of 0.45 or 0.5
  2381.       are generally good choices because they are common in video
  2382.       systems, and so most PNG decoders should do a good job displaying
  2383.       such images.
  2384.  
  2385.       Some image renderers may simultaneously write the image to a PNG
  2386.       file and display it on-screen.  The displayed pixels should be
  2387.       gamma corrected for the display system and viewing conditions in
  2388.       use, so that the user sees a proper representation of the intended
  2389.       scene.  An appropriate gamma correction value is
  2390.  
  2391.          screen_gc = viewing_gamma / display_gamma
  2392.  
  2393.       If the renderer wants to write the same gamma-corrected sample
  2394.       values to the PNG file, avoiding a separate gamma-encoding step
  2395.       for file output, then this screen_gc value should be written in
  2396.       the gAMA chunk.  This will allow a PNG decoder to reproduce what
  2397.       the file's originator saw on screen during rendering (provided the
  2398.       decoder properly supports arbitrary values in a gAMA chunk).
  2399.  
  2400.       However, it is equally reasonable for a renderer to apply gamma
  2401.       correction for screen display using a gamma appropriate to the
  2402.       viewing conditions, and to separately gamma-encode the sample
  2403.       values for file storage using a standard value of gamma such as
  2404.       0.5.  In fact, this is preferable, since some PNG decoders may not
  2405.       accurately display images with unusual gAMA values.
  2406.  
  2407.  
  2408.  
  2409.  
  2410. Boutell, et. al.             Informational                     [Page 43]
  2411.  
  2412. RFC 2083            PNG: Portable Network Graphics            March 1997
  2413.  
  2414.  
  2415.       Computer graphics renderers often do not perform gamma encoding,
  2416.       instead making sample values directly proportional to scene light
  2417.       intensity.  If the PNG encoder receives sample values that have
  2418.       already been quantized into linear-light integer values, there is
  2419.       no point in doing gamma encoding on them; that would just result
  2420.       in further loss of information.  The encoder should just write the
  2421.       sample values to the PNG file.  This "linear" sample encoding is
  2422.       equivalent to gamma encoding with a gamma of 1.0, so graphics
  2423.       programs that produce linear samples should always emit a gAMA
  2424.       chunk specifying a gamma of 1.0.
  2425.  
  2426.       When the sample values come directly from a piece of hardware, the
  2427.       correct gAMA value is determined by the gamma characteristic of
  2428.       the hardware.  In the case of video digitizers ("frame grabbers"),
  2429.       gAMA should be 0.45 or 0.5 for NTSC (possibly less for PAL or
  2430.       SECAM) since video camera transfer functions are standardized.
  2431.       Image scanners are less predictable.  Their output samples may be
  2432.       linear (gamma 1.0) since CCD sensors themselves are linear, or the
  2433.       scanner hardware may have already applied gamma correction
  2434.       designed to compensate for dot gain in subsequent printing (gamma
  2435.       of about 0.57), or the scanner may have corrected the samples for
  2436.       display on a CRT (gamma of 0.4-0.5).  You will need to refer to
  2437.       the scanner's manual, or even scan a calibrated gray wedge, to
  2438.       determine what a particular scanner does.
  2439.  
  2440.       File format converters generally should not attempt to convert
  2441.       supplied images to a different gamma.  Store the data in the PNG
  2442.       file without conversion, and record the source gamma if it is
  2443.       known.  Gamma alteration at file conversion time causes re-
  2444.       quantization of the set of intensity levels that are represented,
  2445.       introducing further roundoff error with little benefit.  It's
  2446.       almost always better to just copy the sample values intact from
  2447.       the input to the output file.
  2448.  
  2449.       In some cases, the supplied image may be in an image format (e.g.,
  2450.       TIFF) that can describe the gamma characteristic of the image.  In
  2451.       such cases, a file format converter is strongly encouraged to
  2452.       write a PNG gAMA chunk that corresponds to the known gamma of the
  2453.       source image.  Note that some file formats specify the gamma of
  2454.       the display system, not the camera.  If the input file's gamma
  2455.       value is greater than 1.0, it is almost certainly a display system
  2456.       gamma, and you should use its reciprocal for the PNG gAMA.
  2457.  
  2458.  
  2459.  
  2460.  
  2461.  
  2462.  
  2463.  
  2464.  
  2465.  
  2466. Boutell, et. al.             Informational                     [Page 44]
  2467.  
  2468. RFC 2083            PNG: Portable Network Graphics            March 1997
  2469.  
  2470.  
  2471.       If the encoder or file format converter does not know how an image
  2472.       was originally created, but does know that the image has been
  2473.       displayed satisfactorily on a display with gamma display_gamma
  2474.       under lighting conditions where a particular viewing_gamma is
  2475.       appropriate, then the image can be marked as having the
  2476.       file_gamma:
  2477.  
  2478.          file_gamma = viewing_gamma / display_gamma
  2479.  
  2480.       This will allow viewers of the PNG file to see the same image that
  2481.       the person running the file format converter saw.  Although this
  2482.       may not be precisely the correct value of the image gamma, it's
  2483.       better to write a gAMA chunk with an approximately right value
  2484.       than to omit the chunk and force PNG decoders to guess at an
  2485.       appropriate gamma.
  2486.  
  2487.       On the other hand, if the image file is being converted as part of
  2488.       a "bulk" conversion, with no one looking at each image, then it is
  2489.       better to omit the gAMA chunk entirely.  If the image gamma has to
  2490.       be guessed at, leave it to the decoder to do the guessing.
  2491.  
  2492.       Gamma does not apply to alpha samples; alpha is always represented
  2493.       linearly.
  2494.  
  2495.       See also Recommendations for Decoders: Decoder gamma handling
  2496.       (Section 10.5).
  2497.  
  2498.    9.3. Encoder color handling
  2499.  
  2500.       See Color Tutorial (Chapter 14) if you aren't already familiar
  2501.       with color issues.
  2502.  
  2503.       If it is possible for the encoder to determine the chromaticities
  2504.       of the source display primaries, or to make a strong guess based
  2505.       on the origin of the image or the hardware running it, then the
  2506.       encoder is strongly encouraged to output the cHRM chunk.  If it
  2507.       does so, the gAMA chunk should also be written; decoders can do
  2508.       little with cHRM if gAMA is missing.
  2509.  
  2510.  
  2511.  
  2512.  
  2513.  
  2514.  
  2515.  
  2516.  
  2517.  
  2518.  
  2519.  
  2520.  
  2521.  
  2522. Boutell, et. al.             Informational                     [Page 45]
  2523.  
  2524. RFC 2083            PNG: Portable Network Graphics            March 1997
  2525.  
  2526.  
  2527.       Video created with recent video equipment probably uses the CCIR
  2528.       709 primaries and D65 white point [ITU-BT709], which are:
  2529.  
  2530.                   R           G           B         White
  2531.          x      0.640       0.300       0.150       0.3127
  2532.          y      0.330       0.600       0.060       0.3290
  2533.  
  2534.       An older but still very popular video standard is SMPTE-C [SMPTE-
  2535.       170M]:
  2536.  
  2537.                   R           G           B         White
  2538.          x      0.630       0.310       0.155       0.3127
  2539.          y      0.340       0.595       0.070       0.3290
  2540.  
  2541.       The original NTSC color primaries have not been used in decades.
  2542.       Although you may still find the NTSC numbers listed in standards
  2543.       documents, you won't find any images that actually use them.
  2544.  
  2545.       Scanners that produce PNG files as output should insert the filter
  2546.       chromaticities into a cHRM chunk and the camera_gamma into a gAMA
  2547.       chunk.
  2548.  
  2549.       In the case of hand-drawn or digitally edited images, you have to
  2550.       determine what monitor they were viewed on when being produced.
  2551.       Many image editing programs allow you to specify what type of
  2552.       monitor you are using.  This is often because they are working in
  2553.       some device-independent space internally.  Such programs have
  2554.       enough information to write valid cHRM and gAMA chunks, and should
  2555.       do so automatically.
  2556.  
  2557.       If the encoder is compiled as a portion of a computer image
  2558.       renderer that performs full-spectral rendering, the monitor values
  2559.       that were used to convert from the internal device-independent
  2560.       color space to RGB should be written into the cHRM chunk. Any
  2561.       colors that are outside the gamut of the chosen RGB device should
  2562.       be clipped or otherwise constrained to be within the gamut; PNG
  2563.       does not store out of gamut colors.
  2564.  
  2565.       If the computer image renderer performs calculations directly in
  2566.       device-dependent RGB space, a cHRM chunk should not be written
  2567.       unless the scene description and rendering parameters have been
  2568.       adjusted to look good on a particular monitor.  In that case, the
  2569.       data for that monitor (if known) should be used to construct a
  2570.       cHRM chunk.
  2571.  
  2572.  
  2573.  
  2574.  
  2575.  
  2576.  
  2577.  
  2578. Boutell, et. al.             Informational                     [Page 46]
  2579.  
  2580. RFC 2083            PNG: Portable Network Graphics            March 1997
  2581.  
  2582.  
  2583.       There are often cases where an image's exact origins are unknown,
  2584.       particularly if it began life in some other format.  A few image
  2585.       formats store calibration information, which can be used to fill
  2586.       in the cHRM chunk.  For example, all PhotoCD images use the CCIR
  2587.       709 primaries and D65 whitepoint, so these values can be written
  2588.       into the cHRM chunk when converting a PhotoCD file.  PhotoCD also
  2589.       uses the SMPTE-170M transfer function, which is closely
  2590.       approximated by a gAMA of 0.5.  (PhotoCD can store colors outside
  2591.       the RGB gamut, so the image data will require gamut mapping before
  2592.       writing to PNG format.)  TIFF 6.0 files can optionally store
  2593.       calibration information, which if present should be used to
  2594.       construct the cHRM chunk.  GIF and most other formats do not store
  2595.       any calibration information.
  2596.  
  2597.       It is not recommended that file format converters attempt to
  2598.       convert supplied images to a different RGB color space.  Store the
  2599.       data in the PNG file without conversion, and record the source
  2600.       primary chromaticities if they are known.  Color space
  2601.       transformation at file conversion time is a bad idea because of
  2602.       gamut mismatches and rounding errors.  As with gamma conversions,
  2603.       it's better to store the data losslessly and incur at most one
  2604.       conversion when the image is finally displayed.
  2605.  
  2606.       See also Recommendations for Decoders: Decoder color handling
  2607.       (Section 10.6).
  2608.  
  2609.    9.4. Alpha channel creation
  2610.  
  2611.       The alpha channel can be regarded either as a mask that
  2612.       temporarily hides transparent parts of the image, or as a means
  2613.       for constructing a non-rectangular image.  In the first case, the
  2614.       color values of fully transparent pixels should be preserved for
  2615.       future use.  In the second case, the transparent pixels carry no
  2616.       useful data and are simply there to fill out the rectangular image
  2617.       area required by PNG.  In this case, fully transparent pixels
  2618.       should all be assigned the same color value for best compression.
  2619.  
  2620.       Image authors should keep in mind the possibility that a decoder
  2621.       will ignore transparency control.  Hence, the colors assigned to
  2622.       transparent pixels should be reasonable background colors whenever
  2623.       feasible.
  2624.  
  2625.       For applications that do not require a full alpha channel, or
  2626.       cannot afford the price in compression efficiency, the tRNS
  2627.       transparency chunk is also available.
  2628.  
  2629.  
  2630.  
  2631.  
  2632.  
  2633.  
  2634. Boutell, et. al.             Informational                     [Page 47]
  2635.  
  2636. RFC 2083            PNG: Portable Network Graphics            March 1997
  2637.  
  2638.  
  2639.       If the image has a known background color, this color should be
  2640.       written in the bKGD chunk.  Even decoders that ignore transparency
  2641.       may use the bKGD color to fill unused screen area.
  2642.  
  2643.       If the original image has premultiplied (also called "associated")
  2644.       alpha data, convert it to PNG's non-premultiplied format by
  2645.       dividing each sample value by the corresponding alpha value, then
  2646.       multiplying by the maximum value for the image bit depth, and
  2647.       rounding to the nearest integer.  In valid premultiplied data, the
  2648.       sample values never exceed their corresponding alpha values, so
  2649.       the result of the division should always be in the range 0 to 1.
  2650.       If the alpha value is zero, output black (zeroes).
  2651.  
  2652.    9.5. Suggested palettes
  2653.  
  2654.       A PLTE chunk can appear in truecolor PNG files.  In such files,
  2655.       the chunk is not an essential part of the image data, but simply
  2656.       represents a suggested palette that viewers may use to present the
  2657.       image on indexed-color display hardware.  A suggested palette is
  2658.       of no interest to viewers running on truecolor hardware.
  2659.  
  2660.       If an encoder chooses to provide a suggested palette, it is
  2661.       recommended that a hIST chunk also be written to indicate the
  2662.       relative importance of the palette entries.  The histogram values
  2663.       are most easily computed as "nearest neighbor" counts, that is,
  2664.       the approximate usage of each palette entry if no dithering is
  2665.       applied.  (These counts will often be available for free as a
  2666.       consequence of developing the suggested palette.)
  2667.  
  2668.       For images of color type 2 (truecolor without alpha channel), it
  2669.       is recommended that the palette and histogram be computed with
  2670.       reference to the RGB data only, ignoring any transparent-color
  2671.       specification.  If the file uses transparency (has a tRNS chunk),
  2672.       viewers can easily adapt the resulting palette for use with their
  2673.       intended background color.  They need only replace the palette
  2674.       entry closest to the tRNS color with their background color (which
  2675.       may or may not match the file's bKGD color, if any).
  2676.  
  2677.       For images of color type 6 (truecolor with alpha channel), it is
  2678.       recommended that a bKGD chunk appear and that the palette and
  2679.       histogram be computed with reference to the image as it would
  2680.       appear after compositing against the specified background color.
  2681.       This definition is necessary to ensure that useful palette entries
  2682.       are generated for pixels having fractional alpha values.  The
  2683.       resulting palette will probably only be useful to viewers that
  2684.       present the image against the same background color.  It is
  2685.       recommended that PNG editors delete or recompute the palette if
  2686.       they alter or remove the bKGD chunk in an image of color type 6.
  2687.  
  2688.  
  2689.  
  2690. Boutell, et. al.             Informational                     [Page 48]
  2691.  
  2692. RFC 2083            PNG: Portable Network Graphics            March 1997
  2693.  
  2694.  
  2695.       If PLTE appears without bKGD in an image of color type 6, the
  2696.       circumstances under which the palette was computed are
  2697.       unspecified.
  2698.  
  2699.    9.6. Filter selection
  2700.  
  2701.       For images of color type 3 (indexed color), filter type 0 (None)
  2702.       is usually the most effective.  Note that color images with 256 or
  2703.       fewer colors should almost always be stored in indexed color
  2704.       format; truecolor format is likely to be much larger.
  2705.  
  2706.       Filter type 0 is also recommended for images of bit depths less
  2707.       than 8.  For low-bit-depth grayscale images, it may be a net win
  2708.       to expand the image to 8-bit representation and apply filtering,
  2709.       but this is rare.
  2710.  
  2711.       For truecolor and grayscale images, any of the five filters may
  2712.       prove the most effective.  If an encoder uses a fixed filter, the
  2713.       Paeth filter is most likely to be the best.
  2714.  
  2715.       For best compression of truecolor and grayscale images, we
  2716.       recommend an adaptive filtering approach in which a filter is
  2717.       chosen for each scanline.  The following simple heuristic has
  2718.       performed well in early tests: compute the output scanline using
  2719.       all five filters, and select the filter that gives the smallest
  2720.       sum of absolute values of outputs.  (Consider the output bytes as
  2721.       signed differences for this test.)  This method usually
  2722.       outperforms any single fixed filter choice.  However, it is likely
  2723.       that much better heuristics will be found as more experience is
  2724.       gained with PNG.
  2725.  
  2726.       Filtering according to these recommendations is effective on
  2727.       interlaced as well as noninterlaced images.
  2728.  
  2729.    9.7. Text chunk processing
  2730.  
  2731.       A nonempty keyword must be provided for each text chunk.  The
  2732.       generic keyword "Comment" can be used if no better description of
  2733.       the text is available.  If a user-supplied keyword is used, be
  2734.       sure to check that it meets the restrictions on keywords.
  2735.  
  2736.       PNG text strings are expected to use the Latin-1 character set.
  2737.       Encoders should avoid storing characters that are not defined in
  2738.       Latin-1, and should provide character code remapping if the local
  2739.       system's character set is not Latin-1.
  2740.  
  2741.       Encoders should discourage the creation of single lines of text
  2742.       longer than 79 characters, in order to facilitate easy reading.
  2743.  
  2744.  
  2745.  
  2746. Boutell, et. al.             Informational                     [Page 49]
  2747.  
  2748. RFC 2083            PNG: Portable Network Graphics            March 1997
  2749.  
  2750.  
  2751.       It is recommended that text items less than 1K (1024 bytes) in
  2752.       size should be output using uncompressed tEXt chunks. In
  2753.       particular, it is recommended that the basic title and author
  2754.       keywords should always be output using uncompressed tEXt chunks.
  2755.       Lengthy disclaimers, on the other hand, are ideal candidates for
  2756.       zTXt.
  2757.  
  2758.       Placing large tEXt and zTXt chunks after the image data (after
  2759.       IDAT) can speed up image display in some situations, since the
  2760.       decoder won't have to read over the text to get to the image data.
  2761.       But it is recommended that small text chunks, such as the image
  2762.       title, appear before IDAT.
  2763.  
  2764.    9.8. Use of private chunks
  2765.  
  2766.       Applications can use PNG private chunks to carry information that
  2767.       need not be understood by other applications.  Such chunks must be
  2768.       given names with lowercase second letters, to ensure that they can
  2769.       never conflict with any future public chunk definition.  Note,
  2770.       however, that there is no guarantee that some other application
  2771.       will not use the same private chunk name.  If you use a private
  2772.       chunk type, it is prudent to store additional identifying
  2773.       information at the beginning of the chunk data.
  2774.  
  2775.       Use an ancillary chunk type (lowercase first letter), not a
  2776.       critical chunk type, for all private chunks that store information
  2777.       that is not absolutely essential to view the image.  Creation of
  2778.       private critical chunks is discouraged because they render PNG
  2779.       files unportable.  Such chunks should not be used in publicly
  2780.       available software or files.  If private critical chunks are
  2781.       essential for your application, it is recommended that one appear
  2782.       near the start of the file, so that a standard decoder need not
  2783.       read very far before discovering that it cannot handle the file.
  2784.  
  2785.       If you want others outside your organization to understand a chunk
  2786.       type that you invent, contact the maintainers of the PNG
  2787.       specification to submit a proposed chunk name and definition for
  2788.       addition to the list of special-purpose public chunks (see
  2789.       Additional chunk types, Section 4.4).  Note that a proposed public
  2790.       chunk name (with uppercase second letter) must not be used in
  2791.       publicly available software or files until registration has been
  2792.       approved.
  2793.  
  2794.       If an ancillary chunk contains textual information that might be
  2795.       of interest to a human user, you should not create a special chunk
  2796.       type for it.  Instead use a tEXt chunk and define a suitable
  2797.       keyword.  That way, the information will be available to users not
  2798.       using your software.
  2799.  
  2800.  
  2801.  
  2802. Boutell, et. al.             Informational                     [Page 50]
  2803.  
  2804. RFC 2083            PNG: Portable Network Graphics            March 1997
  2805.  
  2806.  
  2807.       Keywords in tEXt chunks should be reasonably self-explanatory,
  2808.       since the idea is to let other users figure out what the chunk
  2809.       contains.  If of general usefulness, new keywords can be
  2810.       registered with the maintainers of the PNG specification.  But it
  2811.       is permissible to use keywords without registering them first.
  2812.  
  2813.    9.9. Private type and method codes
  2814.  
  2815.       This specification defines the meaning of only some of the
  2816.       possible values of some fields.  For example, only compression
  2817.       method 0 and filter types 0 through 4 are defined.  Numbers
  2818.       greater than 127 must be used when inventing experimental or
  2819.       private definitions of values for any of these fields.  Numbers
  2820.       below 128 are reserved for possible future public extensions of
  2821.       this specification.  Note that use of private type codes may
  2822.       render a file unreadable by standard decoders.  Such codes are
  2823.       strongly discouraged except for experimental purposes, and should
  2824.       not appear in publicly available software or files.
  2825.  
  2826. 10. Recommendations for Decoders
  2827.  
  2828.    This chapter gives some recommendations for decoder behavior.  The
  2829.    only absolute requirement on a PNG decoder is that it successfully
  2830.    read any file conforming to the format specified in the preceding
  2831.    chapters.  However, best results will usually be achieved by
  2832.    following these recommendations.
  2833.  
  2834.    10.1. Error checking
  2835.  
  2836.       To ensure early detection of common file-transfer problems,
  2837.       decoders should verify that all eight bytes of the PNG file
  2838.       signature are correct.  (See Rationale: PNG file signature,
  2839.       Section 12.11.) A decoder can have additional confidence in the
  2840.       file's integrity if the next eight bytes are an IHDR chunk header
  2841.       with the correct chunk length.
  2842.  
  2843.       Unknown chunk types must be handled as described in Chunk naming
  2844.       conventions (Section 3.3).  An unknown chunk type is not to be
  2845.       treated as an error unless it is a critical chunk.
  2846.  
  2847.       It is strongly recommended that decoders should verify the CRC on
  2848.       each chunk.
  2849.  
  2850.       In some situations it is desirable to check chunk headers (length
  2851.       and type code) before reading the chunk data and CRC.  The chunk
  2852.       type can be checked for plausibility by seeing whether all four
  2853.       bytes are ASCII letters (codes 65-90 and 97-122); note that this
  2854.       need only be done for unrecognized type codes.  If the total file
  2855.  
  2856.  
  2857.  
  2858. Boutell, et. al.             Informational                     [Page 51]
  2859.  
  2860. RFC 2083            PNG: Portable Network Graphics            March 1997
  2861.  
  2862.  
  2863.       size is known (from file system information, HTTP protocol, etc),
  2864.       the chunk length can be checked for plausibility as well.
  2865.  
  2866.       If CRCs are not checked, dropped/added data bytes or an erroneous
  2867.       chunk length can cause the decoder to get out of step and
  2868.       misinterpret subsequent data as a chunk header.  Verifying that
  2869.       the chunk type contains letters is an inexpensive way of providing
  2870.       early error detection in this situation.
  2871.  
  2872.       For known-length chunks such as IHDR, decoders should treat an
  2873.       unexpected chunk length as an error.  Future extensions to this
  2874.       specification will not add new fields to existing chunks; instead,
  2875.       new chunk types will be added to carry new information.
  2876.  
  2877.       Unexpected values in fields of known chunks (for example, an
  2878.       unexpected compression method in the IHDR chunk) must be checked
  2879.       for and treated as errors.  However, it is recommended that
  2880.       unexpected field values be treated as fatal errors only in
  2881.       critical chunks.  An unexpected value in an ancillary chunk can be
  2882.       handled by ignoring the whole chunk as though it were an unknown
  2883.       chunk type.  (This recommendation assumes that the chunk's CRC has
  2884.       been verified.  In decoders that do not check CRCs, it is safer to
  2885.       treat any unexpected value as indicating a corrupted file.)
  2886.  
  2887.    10.2. Pixel dimensions
  2888.  
  2889.       Non-square pixels can be represented (see the pHYs chunk), but
  2890.       viewers are not required to account for them; a viewer can present
  2891.       any PNG file as though its pixels are square.
  2892.  
  2893.       Conversely, viewers running on display hardware with non-square
  2894.       pixels are strongly encouraged to rescale images for proper
  2895.       display.
  2896.  
  2897.    10.3. Truecolor image handling
  2898.  
  2899.       To achieve PNG's goal of universal interchangeability, decoders
  2900.       are required to accept all types of PNG image: indexed-color,
  2901.       truecolor, and grayscale.  Viewers running on indexed-color
  2902.       display hardware need to be able to reduce truecolor images to
  2903.       indexed format for viewing.  This process is usually called "color
  2904.       quantization".
  2905.  
  2906.  
  2907.  
  2908.  
  2909.  
  2910.  
  2911.  
  2912.  
  2913.  
  2914. Boutell, et. al.             Informational                     [Page 52]
  2915.  
  2916. RFC 2083            PNG: Portable Network Graphics            March 1997
  2917.  
  2918.  
  2919.       A simple, fast way of doing this is to reduce the image to a fixed
  2920.       palette.  Palettes with uniform color spacing ("color cubes") are
  2921.       usually used to minimize the per-pixel computation.  For
  2922.       photograph-like images, dithering is recommended to avoid ugly
  2923.       contours in what should be smooth gradients; however, dithering
  2924.       introduces graininess that can be objectionable.
  2925.  
  2926.       The quality of rendering can be improved substantially by using a
  2927.       palette chosen specifically for the image, since a color cube
  2928.       usually has numerous entries that are unused in any particular
  2929.       image.  This approach requires more work, first in choosing the
  2930.       palette, and second in mapping individual pixels to the closest
  2931.       available color.  PNG allows the encoder to supply a suggested
  2932.       palette in a PLTE chunk, but not all encoders will do so, and the
  2933.       suggested palette may be unsuitable in any case (it may have too
  2934.       many or too few colors).  High-quality viewers will therefore need
  2935.       to have a palette selection routine at hand.  A large lookup table
  2936.       is usually the most feasible way of mapping individual pixels to
  2937.       palette entries with adequate speed.
  2938.  
  2939.       Numerous implementations of color quantization are available.  The
  2940.       PNG reference implementation, libpng, includes code for the
  2941.       purpose.
  2942.  
  2943.    10.4. Sample depth rescaling
  2944.  
  2945.       Decoders may wish to scale PNG data to a lesser sample depth (data
  2946.       precision) for display.  For example, 16-bit data will need to be
  2947.       reduced to 8-bit depth for use on most present-day display
  2948.       hardware.  Reduction of 8-bit data to 5-bit depth is also common.
  2949.  
  2950.       The most accurate scaling is achieved by the linear equation
  2951.  
  2952.          output = ROUND(input * MAXOUTSAMPLE / MAXINSAMPLE)
  2953.  
  2954.       where
  2955.  
  2956.          MAXINSAMPLE = (2^sampledepth)-1
  2957.          MAXOUTSAMPLE = (2^desired_sampledepth)-1
  2958.  
  2959.       A slightly less accurate conversion is achieved by simply shifting
  2960.       right by sampledepth-desired_sampledepth places.  For example, to
  2961.       reduce 16-bit samples to 8-bit, one need only discard the low-
  2962.       order byte.  In many situations the shift method is sufficiently
  2963.       accurate for display purposes, and it is certainly much faster.
  2964.       (But if gamma correction is being done, sample rescaling can be
  2965.       merged into the gamma correction lookup table, as is illustrated
  2966.       in Decoder gamma handling, Section 10.5.)
  2967.  
  2968.  
  2969.  
  2970. Boutell, et. al.             Informational                     [Page 53]
  2971.  
  2972. RFC 2083            PNG: Portable Network Graphics            March 1997
  2973.  
  2974.  
  2975.       When an sBIT chunk is present, the original pre-PNG data can be
  2976.       recovered by shifting right to the sample depth specified by sBIT.
  2977.       Note that linear scaling will not necessarily reproduce the
  2978.       original data, because the encoder is not required to have used
  2979.       linear scaling to scale the data up.  However, the encoder is
  2980.       required to have used a method that preserves the high-order bits,
  2981.       so shifting always works.  This is the only case in which shifting
  2982.       might be said to be more accurate than linear scaling.
  2983.  
  2984.       When comparing pixel values to tRNS chunk values to detect
  2985.       transparent pixels, it is necessary to do the comparison exactly.
  2986.       Therefore, transparent pixel detection must be done before
  2987.       reducing sample precision.
  2988.  
  2989.    10.5. Decoder gamma handling
  2990.  
  2991.       See Gamma Tutorial (Chapter 13) if you aren't already familiar
  2992.       with gamma issues.
  2993.  
  2994.       To produce correct tone reproduction, a good image display program
  2995.       should take into account the gammas of the image file and the
  2996.       display device, as well as the viewing_gamma appropriate to the
  2997.       lighting conditions near the display.  This can be done by
  2998.       calculating
  2999.  
  3000.          gbright = insample / MAXINSAMPLE
  3001.          bright = gbright ^ (1.0 / file_gamma)
  3002.          vbright = bright ^ viewing_gamma
  3003.          gcvideo = vbright ^ (1.0 / display_gamma)
  3004.          fbval = ROUND(gcvideo * MAXFBVAL)
  3005.  
  3006.       where MAXINSAMPLE is the maximum sample value in the file (255 for
  3007.       8-bit, 65535 for 16-bit, etc), MAXFBVAL is the maximum value of a
  3008.       frame buffer sample (255 for 8-bit, 31 for 5-bit, etc), insample
  3009.       is the value of the sample in the PNG file, and fbval is the value
  3010.       to write into the frame buffer. The first line converts from
  3011.       integer samples into a normalized 0 to 1 floating point value, the
  3012.       second undoes the gamma encoding of the image file to produce a
  3013.       linear intensity value, the third adjusts for the viewing
  3014.       conditions, the fourth corrects for the display system's gamma
  3015.       value, and the fifth converts to an integer frame buffer sample.
  3016.       In practice, the second through fourth lines can be merged into
  3017.  
  3018.          gcvideo = gbright^(viewing_gamma / (file_gamma*display_gamma))
  3019.  
  3020.       so as to perform only one power calculation. For color images, the
  3021.       entire calculation is performed separately for R, G, and B values.
  3022.  
  3023.  
  3024.  
  3025.  
  3026. Boutell, et. al.             Informational                     [Page 54]
  3027.  
  3028. RFC 2083            PNG: Portable Network Graphics            March 1997
  3029.  
  3030.  
  3031.       It is not necessary to perform transcendental math for every
  3032.       pixel.  Instead, compute a lookup table that gives the correct
  3033.       output value for every possible sample value. This requires only
  3034.       256 calculations per image (for 8-bit accuracy), not one or three
  3035.       calculations per pixel.  For an indexed-color image, a one-time
  3036.       correction of the palette is sufficient, unless the image uses
  3037.       transparency and is being displayed against a nonuniform
  3038.       background.
  3039.  
  3040.       In some cases even the cost of computing a gamma lookup table may
  3041.       be a concern.  In these cases, viewers are encouraged to have
  3042.       precomputed gamma correction tables for file_gamma values of 1.0
  3043.       and 0.5 with some reasonable choice of viewing_gamma and
  3044.       display_gamma, and to use the table closest to the gamma indicated
  3045.       in the file. This will produce acceptable results for the majority
  3046.       of real files.
  3047.  
  3048.       When the incoming image has unknown gamma (no gAMA chunk), choose
  3049.       a likely default file_gamma value, but allow the user to select a
  3050.       new one if the result proves too dark or too light.
  3051.  
  3052.       In practice, it is often difficult to determine what value of
  3053.       display_gamma should be used. In systems with no built-in gamma
  3054.       correction, the display_gamma is determined entirely by the CRT.
  3055.       Assuming a CRT_gamma of 2.5 is recommended, unless you have
  3056.       detailed calibration measurements of this particular CRT
  3057.       available.
  3058.  
  3059.       However, many modern frame buffers have lookup tables that are
  3060.       used to perform gamma correction, and on these systems the
  3061.       display_gamma value should be the gamma of the lookup table and
  3062.       CRT combined. You may not be able to find out what the lookup
  3063.       table contains from within an image viewer application, so you may
  3064.       have to ask the user what the system's gamma value is.
  3065.       Unfortunately, different manufacturers use different ways of
  3066.       specifying what should go into the lookup table, so interpretation
  3067.       of the system gamma value is system-dependent.  Gamma Tutorial
  3068.       (Chapter 13) gives some examples.
  3069.  
  3070.       The response of real displays is actually more complex than can be
  3071.       described by a single number (display_gamma). If actual
  3072.       measurements of the monitor's light output as a function of
  3073.       voltage input are available, the fourth and fifth lines of the
  3074.       computation above can be replaced by a lookup in these
  3075.       measurements, to find the actual frame buffer value that most
  3076.       nearly gives the desired brightness.
  3077.  
  3078.  
  3079.  
  3080.  
  3081.  
  3082. Boutell, et. al.             Informational                     [Page 55]
  3083.  
  3084. RFC 2083            PNG: Portable Network Graphics            March 1997
  3085.  
  3086.  
  3087.       The value of viewing_gamma depends on lighting conditions; see
  3088.       Gamma Tutorial (Chapter 13) for more detail.  Ideally, a viewer
  3089.       would allow the user to specify viewing_gamma, either directly
  3090.       numerically, or via selecting from "bright surround", "dim
  3091.       surround", and "dark surround" conditions.  Viewers that don't
  3092.       want to do this should just assume a value for viewing_gamma of
  3093.       1.0, since most computer displays live in brightly-lit rooms.
  3094.  
  3095.       When viewing images that are digitized from video, or that are
  3096.       destined to become video frames, the user might want to set the
  3097.       viewing_gamma to about 1.25 regardless of the actual level of room
  3098.       lighting.  This value of viewing_gamma is "built into" NTSC video
  3099.       practice, and displaying an image with that viewing_gamma allows
  3100.       the user to see what a TV set would show under the current room
  3101.       lighting conditions.  (This is not the same thing as trying to
  3102.       obtain the most accurate rendition of the content of the scene,
  3103.       which would require adjusting viewing_gamma to correspond to the
  3104.       room lighting level.)  This is another reason viewers might want
  3105.       to allow users to adjust viewing_gamma directly.
  3106.  
  3107.    10.6. Decoder color handling
  3108.  
  3109.       See Color Tutorial (Chapter 14) if you aren't already familiar
  3110.       with color issues.
  3111.  
  3112.       In many cases, decoders will treat image data in PNG files as
  3113.       device-dependent RGB data and display it without modification
  3114.       (except for appropriate gamma correction). This provides the
  3115.       fastest display of PNG images.  But unless the viewer uses exactly
  3116.       the same display hardware as the original image author used, the
  3117.       colors will not be exactly the same as the original author saw,
  3118.       particularly for darker or near-neutral colors.  The cHRM chunk
  3119.       provides information that allows closer color matching than that
  3120.       provided by gamma correction alone.
  3121.  
  3122.       Decoders can use the cHRM data to transform the image data from
  3123.       RGB to XYZ and thence into a perceptually linear color space such
  3124.       as CIE LAB.  They can then partition the colors to generate an
  3125.       optimal palette, because the geometric distance between two colors
  3126.       in CIE LAB is strongly related to how different those colors
  3127.       appear (unlike, for example, RGB or XYZ spaces).  The resulting
  3128.       palette of colors, once transformed back into RGB color space,
  3129.       could be used for display or written into a PLTE chunk.
  3130.  
  3131.       Decoders that are part of image processing applications might also
  3132.       transform image data into CIE LAB space for analysis.
  3133.  
  3134.  
  3135.  
  3136.  
  3137.  
  3138. Boutell, et. al.             Informational                     [Page 56]
  3139.  
  3140. RFC 2083            PNG: Portable Network Graphics            March 1997
  3141.  
  3142.  
  3143.       In applications where color fidelity is critical, such as product
  3144.       design, scientific visualization, medicine, architecture, or
  3145.       advertising, decoders can transform the image data from source_RGB
  3146.       to the display_RGB space of the monitor used to view the image.
  3147.       This involves calculating the matrix to go from source_RGB to XYZ
  3148.       and the matrix to go from XYZ to display_RGB, then combining them
  3149.       to produce the overall transformation.  The decoder is responsible
  3150.       for implementing gamut mapping.
  3151.  
  3152.       Decoders running on platforms that have a Color Management System
  3153.       (CMS) can pass the image data, gAMA and cHRM values to the CMS for
  3154.       display or further processing.
  3155.  
  3156.       Decoders that provide color printing facilities can use the
  3157.       facilities in Level 2 PostScript to specify image data in
  3158.       calibrated RGB space or in a device-independent color space such
  3159.       as XYZ.  This will provide better color fidelity than a simple RGB
  3160.       to CMYK conversion.  The PostScript Language Reference manual
  3161.       gives examples of this process [POSTSCRIPT].  Such decoders are
  3162.       responsible for implementing gamut mapping between source_RGB
  3163.       (specified in the cHRM chunk) and the target printer. The
  3164.       PostScript interpreter is then responsible for producing the
  3165.       required colors.
  3166.  
  3167.       Decoders can use the cHRM data to calculate an accurate grayscale
  3168.       representation of a color image.  Conversion from RGB to gray is
  3169.       simply a case of calculating the Y (luminance) component of XYZ,
  3170.       which is a weighted sum of the R G and B values.  The weights
  3171.       depend on the monitor type, i.e., the values in the cHRM chunk.
  3172.       Decoders may wish to do this for PNG files with no cHRM chunk.  In
  3173.       that case, a reasonable default would be the CCIR 709 primaries
  3174.       [ITU-BT709].  Do not use the original NTSC primaries, unless you
  3175.       really do have an image color-balanced for such a monitor.  Few
  3176.       monitors ever used the NTSC primaries, so such images are probably
  3177.       nonexistent these days.
  3178.  
  3179.    10.7. Background color
  3180.  
  3181.       The background color given by bKGD will typically be used to fill
  3182.       unused screen space around the image, as well as any transparent
  3183.       pixels within the image.  (Thus, bKGD is valid and useful even
  3184.       when the image does not use transparency.)  If no bKGD chunk is
  3185.       present, the viewer will need to make its own decision about a
  3186.       suitable background color.
  3187.  
  3188.  
  3189.  
  3190.  
  3191.  
  3192.  
  3193.  
  3194. Boutell, et. al.             Informational                     [Page 57]
  3195.  
  3196. RFC 2083            PNG: Portable Network Graphics            March 1997
  3197.  
  3198.  
  3199.       Viewers that have a specific background against which to present
  3200.       the image (such as Web browsers) should ignore the bKGD chunk, in
  3201.       effect overriding bKGD with their preferred background color or
  3202.       background image.
  3203.  
  3204.       The background color given by bKGD is not to be considered
  3205.       transparent, even if it happens to match the color given by tRNS
  3206.       (or, in the case of an indexed-color image, refers to a palette
  3207.       index that is marked as transparent by tRNS).  Otherwise one would
  3208.       have to imagine something "behind the background" to composite
  3209.       against.  The background color is either used as background or
  3210.       ignored; it is not an intermediate layer between the PNG image and
  3211.       some other background.
  3212.  
  3213.       Indeed, it will be common that bKGD and tRNS specify the same
  3214.       color, since then a decoder that does not implement transparency
  3215.       processing will give the intended display, at least when no
  3216.       partially-transparent pixels are present.
  3217.  
  3218.    10.8. Alpha channel processing
  3219.  
  3220.       In the most general case, the alpha channel can be used to
  3221.       composite a foreground image against a background image; the PNG
  3222.       file defines the foreground image and the transparency mask, but
  3223.       not the background image.  Decoders are not required to support
  3224.       this most general case.  It is expected that most will be able to
  3225.       support compositing against a single background color, however.
  3226.  
  3227.       The equation for computing a composited sample value is
  3228.  
  3229.          output = alpha * foreground + (1-alpha) * background
  3230.  
  3231.       where alpha and the input and output sample values are expressed
  3232.       as fractions in the range 0 to 1.  This computation should be
  3233.       performed with linear (non-gamma-encoded) sample values.  For
  3234.       color images, the computation is done separately for R, G, and B
  3235.       samples.
  3236.  
  3237.       The following code illustrates the general case of compositing a
  3238.       foreground image over a background image.  It assumes that you
  3239.       have the original pixel data available for the background image,
  3240.       and that output is to a frame buffer for display.  Other variants
  3241.       are possible; see the comments below the code.  The code allows
  3242.       the sample depths and gamma values of foreground image, background
  3243.       image, and frame buffer/CRT all to be different.  Don't assume
  3244.       they are the same without checking.
  3245.  
  3246.  
  3247.  
  3248.  
  3249.  
  3250. Boutell, et. al.             Informational                     [Page 58]
  3251.  
  3252. RFC 2083            PNG: Portable Network Graphics            March 1997
  3253.  
  3254.  
  3255.       This code is standard C, with line numbers added for reference in
  3256.       the comments below.
  3257.  
  3258.          01  int foreground[4];  /* image pixel: R, G, B, A */
  3259.          02  int background[3];  /* background pixel: R, G, B */
  3260.          03  int fbpix[3];       /* frame buffer pixel */
  3261.          04  int fg_maxsample;   /* foreground max sample */
  3262.          05  int bg_maxsample;   /* background max sample */
  3263.          06  int fb_maxsample;   /* frame buffer max sample */
  3264.          07  int ialpha;
  3265.          08  float alpha, compalpha;
  3266.          09  float gamfg, linfg, gambg, linbg, comppix, gcvideo;
  3267.  
  3268.              /* Get max sample values in data and frame buffer */
  3269.          10  fg_maxsample = (1 << fg_sample_depth) - 1;
  3270.          11  bg_maxsample = (1 << bg_sample_depth) - 1;
  3271.          12  fb_maxsample = (1 << frame_buffer_sample_depth) - 1;
  3272.              /*
  3273.               * Get integer version of alpha.
  3274.               * Check for opaque and transparent special cases;
  3275.               * no compositing needed if so.
  3276.               *
  3277.               * We show the whole gamma decode/correct process in
  3278.               * floating point, but it would more likely be done
  3279.               * with lookup tables.
  3280.               */
  3281.          13  ialpha = foreground[3];
  3282.  
  3283.          14  if (ialpha == 0) {
  3284.                  /*
  3285.                   * Foreground image is transparent here.
  3286.                   * If the background image is already in the frame
  3287.                   * buffer, there is nothing to do.
  3288.                   */
  3289.          15      ;
  3290.          16  } else if (ialpha == fg_maxsample) {
  3291.                  /*
  3292.                   * Copy foreground pixel to frame buffer.
  3293.                   */
  3294.          17      for (i = 0; i < 3; i++) {
  3295.          18          gamfg = (float) foreground[i] / fg_maxsample;
  3296.          19          linfg = pow(gamfg, 1.0/fg_gamma);
  3297.          20          comppix = linfg;
  3298.          21          gcvideo = pow(comppix,viewing_gamma/display_gamma);
  3299.          22          fbpix[i] = (int) (gcvideo * fb_maxsample + 0.5);
  3300.          23      }
  3301.  
  3302.  
  3303.  
  3304.  
  3305.  
  3306. Boutell, et. al.             Informational                     [Page 59]
  3307.  
  3308. RFC 2083            PNG: Portable Network Graphics            March 1997
  3309.  
  3310.  
  3311.          24  } else {
  3312.                  /*
  3313.                   * Compositing is necessary.
  3314.                   * Get floating-point alpha and its complement.
  3315.                   * Note: alpha is always linear; gamma does not
  3316.                   * affect it.
  3317.                   */
  3318.          25      alpha = (float) ialpha / fg_maxsample;
  3319.          26      compalpha = 1.0 - alpha;
  3320.          27      for (i = 0; i < 3; i++) {
  3321.                      /*
  3322.                       * Convert foreground and background to floating
  3323.                       * point, then linearize (undo gamma encoding).
  3324.                       */
  3325.          28          gamfg = (float) foreground[i] / fg_maxsample;
  3326.          29          linfg = pow(gamfg, 1.0/fg_gamma);
  3327.          30          gambg = (float) background[i] / bg_maxsample;
  3328.          31          linbg = pow(gambg, 1.0/bg_gamma);
  3329.                      /*
  3330.                       * Composite.
  3331.                       */
  3332.          32          comppix = linfg * alpha + linbg * compalpha;
  3333.                      /*
  3334.                       * Gamma correct for display.
  3335.                       * Convert to integer frame buffer pixel.
  3336.                       */
  3337.          33          gcvideo = pow(comppix,viewing_gamma/display_gamma);
  3338.          34          fbpix[i] = (int) (gcvideo * fb_maxsample + 0.5);
  3339.          35      }
  3340.          36  }
  3341.  
  3342.       Variations:
  3343.  
  3344.           * If output is to another PNG image file instead of a frame
  3345.             buffer, lines 21, 22, 33, and 34 should be changed to be
  3346.             something like
  3347.  
  3348.                /*
  3349.                 * Gamma encode for storage in output file.
  3350.                 * Convert to integer sample value.
  3351.                 */
  3352.                gamout = pow(comppix, outfile_gamma);
  3353.                outpix[i] = (int) (gamout * out_maxsample + 0.5);
  3354.  
  3355.             Also, it becomes necessary to process background pixels when
  3356.             alpha is zero, rather than just skipping pixels.  Thus, line
  3357.             15 will need to be replaced by copies of lines 17-23, but
  3358.             processing background instead of foreground pixel values.
  3359.  
  3360.  
  3361.  
  3362. Boutell, et. al.             Informational                     [Page 60]
  3363.  
  3364. RFC 2083            PNG: Portable Network Graphics            March 1997
  3365.  
  3366.  
  3367.           * If the sample depths of the output file, foreground file,
  3368.             and background file are all the same, and the three gamma
  3369.             values also match, then the no-compositing code in lines
  3370.             14-23 reduces to nothing more than copying pixel values from
  3371.             the input file to the output file if alpha is one, or
  3372.             copying pixel values from background to output file if alpha
  3373.             is zero.  Since alpha is typically either zero or one for
  3374.             the vast majority of pixels in an image, this is a great
  3375.             savings.  No gamma computations are needed for most pixels.
  3376.           * When the sample depths and gamma values all match, it may
  3377.             appear attractive to skip the gamma decoding and encoding
  3378.             (lines 28-31, 33-34) and just perform line 32 using gamma-
  3379.             encoded sample values. Although this doesn't hurt image
  3380.             quality too badly, the time savings are small if alpha
  3381.             values of zero and one are special-cased as recommended
  3382.             here.
  3383.           * If the original pixel values of the background image are no
  3384.             longer available, only processed frame buffer pixels left by
  3385.             display of the background image, then lines 30 and 31 need
  3386.             to extract intensity from the frame buffer pixel values
  3387.             using code like
  3388.  
  3389.                /*
  3390.                 * Decode frame buffer value back into linear space.
  3391.                 */
  3392.                gcvideo = (float) fbpix[i] / fb_maxsample;
  3393.                linbg = pow(gcvideo, display_gamma / viewing_gamma);
  3394.  
  3395.             However, some roundoff error can result, so it is better to
  3396.             have the original background pixels available if at all
  3397.             possible.
  3398.           * Note that lines 18-22 are performing exactly the same gamma
  3399.             computation that is done when no alpha channel is present.
  3400.             So, if you handle the no-alpha case with a lookup table, you
  3401.             can use the same lookup table here.  Lines 28-31 and 33-34
  3402.             can also be done with (different) lookup tables.
  3403.           * Of course, everything here can be done in integer
  3404.             arithmetic.  Just be careful to maintain sufficient
  3405.             precision all the way through.
  3406.  
  3407.       Note: in floating point, no overflow or underflow checks are
  3408.       needed, because the input sample values are guaranteed to be
  3409.       between 0 and 1, and compositing always yields a result that is in
  3410.       between the input values (inclusive).  With integer arithmetic,
  3411.       some roundoff-error analysis might be needed to guarantee no
  3412.       overflow or underflow.
  3413.  
  3414.  
  3415.  
  3416.  
  3417.  
  3418. Boutell, et. al.             Informational                     [Page 61]
  3419.  
  3420. RFC 2083            PNG: Portable Network Graphics            March 1997
  3421.  
  3422.  
  3423.       When displaying a PNG image with full alpha channel, it is
  3424.       important to be able to composite the image against some
  3425.       background, even if it's only black.  Ignoring the alpha channel
  3426.       will cause PNG images that have been converted from an
  3427.       associated-alpha representation to look wrong.  (Of course, if the
  3428.       alpha channel is a separate transparency mask, then ignoring alpha
  3429.       is a useful option: it allows the hidden parts of the image to be
  3430.       recovered.)
  3431.  
  3432.       Even if the decoder author does not wish to implement true
  3433.       compositing logic, it is simple to deal with images that contain
  3434.       only zero and one alpha values.  (This is implicitly true for
  3435.       grayscale and truecolor PNG files that use a tRNS chunk; for
  3436.       indexed-color PNG files, it is easy to check whether tRNS contains
  3437.       any values other than 0 and 255.)  In this simple case,
  3438.       transparent pixels are replaced by the background color, while
  3439.       others are unchanged.  If a decoder contains only this much
  3440.       transparency capability, it should deal with a full alpha channel
  3441.       by treating all nonzero alpha values as fully opaque; that is, do
  3442.       not replace partially transparent pixels by the background.  This
  3443.       approach will not yield very good results for images converted
  3444.       from associated-alpha formats, but it's better than doing nothing.
  3445.  
  3446.    10.9. Progressive display
  3447.  
  3448.       When receiving images over slow transmission links, decoders can
  3449.       improve perceived performance by displaying interlaced images
  3450.       progressively.  This means that as each pass is received, an
  3451.       approximation to the complete image is displayed based on the data
  3452.       received so far.  One simple yet pleasing effect can be obtained
  3453.       by expanding each received pixel to fill a rectangle covering the
  3454.       yet-to-be-transmitted pixel positions below and to the right of
  3455.       the received pixel.  This process can be described by the
  3456.       following pseudocode:
  3457.  
  3458.          Starting_Row [1..7] =  { 0, 0, 4, 0, 2, 0, 1 }
  3459.          Starting_Col [1..7] =  { 0, 4, 0, 2, 0, 1, 0 }
  3460.          Row_Increment [1..7] = { 8, 8, 8, 4, 4, 2, 2 }
  3461.          Col_Increment [1..7] = { 8, 8, 4, 4, 2, 2, 1 }
  3462.          Block_Height [1..7] =  { 8, 8, 4, 4, 2, 2, 1 }
  3463.          Block_Width [1..7] =   { 8, 4, 4, 2, 2, 1, 1 }
  3464.  
  3465.          pass := 1
  3466.          while pass <= 7
  3467.          begin
  3468.              row := Starting_Row[pass]
  3469.  
  3470.              while row < height
  3471.  
  3472.  
  3473.  
  3474. Boutell, et. al.             Informational                     [Page 62]
  3475.  
  3476. RFC 2083            PNG: Portable Network Graphics            March 1997
  3477.  
  3478.  
  3479.              begin
  3480.                  col := Starting_Col[pass]
  3481.  
  3482.                  while col < width
  3483.                  begin
  3484.                      visit (row, col,
  3485.                             min (Block_Height[pass], height - row),
  3486.                             min (Block_Width[pass], width - col))
  3487.                      col := col + Col_Increment[pass]
  3488.                  end
  3489.                  row := row + Row_Increment[pass]
  3490.              end
  3491.  
  3492.              pass := pass + 1
  3493.          end
  3494.  
  3495.       Here, the function "visit(row,column,height,width)" obtains the
  3496.       next transmitted pixel and paints a rectangle of the specified
  3497.       height and width, whose upper-left corner is at the specified row
  3498.       and column, using the color indicated by the pixel.  Note that row
  3499.       and column are measured from 0,0 at the upper left corner.
  3500.  
  3501.       If the decoder is merging the received image with a background
  3502.       image, it may be more convenient just to paint the received pixel
  3503.       positions; that is, the "visit()" function sets only the pixel at
  3504.       the specified row and column, not the whole rectangle.  This
  3505.       produces a "fade-in" effect as the new image gradually replaces
  3506.       the old.  An advantage of this approach is that proper alpha or
  3507.       transparency processing can be done as each pixel is replaced.
  3508.       Painting a rectangle as described above will overwrite
  3509.       background-image pixels that may be needed later, if the pixels
  3510.       eventually received for those positions turn out to be wholly or
  3511.       partially transparent.  Of course, this is only a problem if the
  3512.       background image is not stored anywhere offscreen.
  3513.  
  3514.    10.10. Suggested-palette and histogram usage
  3515.  
  3516.       In truecolor PNG files, the encoder may have provided a suggested
  3517.       PLTE chunk for use by viewers running on indexed-color hardware.
  3518.  
  3519.       If the image has a tRNS chunk, the viewer will need to adapt the
  3520.       suggested palette for use with its desired background color.  To
  3521.       do this, replace the palette entry closest to the tRNS color with
  3522.       the desired background color; or just add a palette entry for the
  3523.       background color, if the viewer can handle more colors than there
  3524.       are PLTE entries.
  3525.  
  3526.  
  3527.  
  3528.  
  3529.  
  3530. Boutell, et. al.             Informational                     [Page 63]
  3531.  
  3532. RFC 2083            PNG: Portable Network Graphics            March 1997
  3533.  
  3534.  
  3535.       For images of color type 6 (truecolor with alpha channel), any
  3536.       suggested palette should have been designed for display of the
  3537.       image against a uniform background of the color specified by bKGD.
  3538.       Viewers should probably ignore the palette if they intend to use a
  3539.       different background, or if the bKGD chunk is missing.  Viewers
  3540.       can use a suggested palette for display against a different
  3541.       background than it was intended for, but the results may not be
  3542.       very good.
  3543.  
  3544.       If the viewer presents a transparent truecolor image against a
  3545.       background that is more complex than a single color, it is
  3546.       unlikely that the suggested palette will be optimal for the
  3547.       composite image.  In this case it is best to perform a truecolor
  3548.       compositing step on the truecolor PNG image and background image,
  3549.       then color-quantize the resulting image.
  3550.  
  3551.       The histogram chunk is useful when the viewer cannot provide as
  3552.       many colors as are used in the image's palette.  If the viewer is
  3553.       only short a few colors, it is usually adequate to drop the
  3554.       least-used colors from the palette.  To reduce the number of
  3555.       colors substantially, it's best to choose entirely new
  3556.       representative colors, rather than trying to use a subset of the
  3557.       existing palette.  This amounts to performing a new color
  3558.       quantization step; however, the existing palette and histogram can
  3559.       be used as the input data, thus avoiding a scan of the image data.
  3560.  
  3561.       If no palette or histogram chunk is provided, a decoder can
  3562.       develop its own, at the cost of an extra pass over the image data.
  3563.       Alternatively, a default palette (probably a color cube) can be
  3564.       used.
  3565.  
  3566.       See also Recommendations for Encoders: Suggested palettes (Section
  3567.       9.5).
  3568.  
  3569.    10.11. Text chunk processing
  3570.  
  3571.       If practical, decoders should have a way to display to the user
  3572.       all tEXt and zTXt chunks found in the file.  Even if the decoder
  3573.       does not recognize a particular text keyword, the user might be
  3574.       able to understand it.
  3575.  
  3576.       PNG text is not supposed to contain any characters outside the ISO
  3577.       8859-1 "Latin-1" character set (that is, no codes 0-31 or 127-
  3578.       159), except for the newline character (decimal 10).  But decoders
  3579.       might encounter such characters anyway.  Some of these characters
  3580.       can be safely displayed (e.g., TAB, FF, and CR, decimal 9, 12, and
  3581.       13, respectively), but others, especially the ESC character
  3582.       (decimal 27), could pose a security hazard because unexpected
  3583.  
  3584.  
  3585.  
  3586. Boutell, et. al.             Informational                     [Page 64]
  3587.  
  3588. RFC 2083            PNG: Portable Network Graphics            March 1997
  3589.  
  3590.  
  3591.       actions may be taken by display hardware or software.  To prevent
  3592.       such hazards, decoders should not attempt to directly display any
  3593.       non-Latin-1 characters (except for newline and perhaps TAB, FF,
  3594.       CR) encountered in a tEXt or zTXt chunk.  Instead, ignore them or
  3595.       display them in a visible notation such as "\nnn".  See Security
  3596.       considerations (Section 8.5).
  3597.  
  3598.       Even though encoders are supposed to represent newlines as LF, it
  3599.       is recommended that decoders not rely on this; it's best to
  3600.       recognize all the common newline combinations (CR, LF, and CR-LF)
  3601.       and display each as a single newline.  TAB can be expanded to the
  3602.       proper number of spaces needed to arrive at a column multiple of
  3603.       8.
  3604.  
  3605.       Decoders running on systems with non-Latin-1 character set
  3606.       encoding should provide character code remapping so that Latin-1
  3607.       characters are displayed correctly.  Some systems may not provide
  3608.       all the characters defined in Latin-1.  Mapping unavailable
  3609.       characters to a visible notation such as "\nnn" is a good
  3610.       fallback.  In particular, character codes 127-255 should be
  3611.       displayed only if they are printable characters on the decoding
  3612.       system.  Some systems may interpret such codes as control
  3613.       characters; for security, decoders running on such systems should
  3614.       not display such characters literally.
  3615.  
  3616.       Decoders should be prepared to display text chunks that contain
  3617.       any number of printing characters between newline characters, even
  3618.       though encoders are encouraged to avoid creating lines in excess
  3619.       of 79 characters.
  3620.  
  3621. 11. Glossary
  3622.  
  3623.    a^b
  3624.       Exponentiation; a raised to the power b.  C programmers should be
  3625.       careful not to misread this notation as exclusive-or.  Note that
  3626.       in gamma-related calculations, zero raised to any power is valid
  3627.       and must give a zero result.
  3628.  
  3629.    Alpha
  3630.       A value representing a pixel's degree of transparency.  The more
  3631.       transparent a pixel, the less it hides the background against
  3632.       which the image is presented.  In PNG, alpha is really the degree
  3633.       of opacity: zero alpha represents a completely transparent pixel,
  3634.       maximum alpha represents a completely opaque pixel.  But most
  3635.       people refer to alpha as providing transparency information, not
  3636.       opacity information, and we continue that custom here.
  3637.  
  3638.  
  3639.  
  3640.  
  3641.  
  3642. Boutell, et. al.             Informational                     [Page 65]
  3643.  
  3644. RFC 2083            PNG: Portable Network Graphics            March 1997
  3645.  
  3646.  
  3647.    Ancillary chunk
  3648.       A chunk that provides additional information.  A decoder can still
  3649.       produce a meaningful image, though not necessarily the best
  3650.       possible image, without processing the chunk.
  3651.  
  3652.    Bit depth
  3653.       The number of bits per palette index (in indexed-color PNGs) or
  3654.       per sample (in other color types).  This is the same value that
  3655.       appears in IHDR.
  3656.  
  3657.    Byte
  3658.       Eight bits; also called an octet.
  3659.  
  3660.    Channel
  3661.       The set of all samples of the same kind within an image; for
  3662.       example, all the blue samples in a truecolor image.  (The term
  3663.       "component" is also used, but not in this specification.)  A
  3664.       sample is the intersection of a channel and a pixel.
  3665.  
  3666.    Chromaticity
  3667.       A pair of values x,y that precisely specify the hue, though not
  3668.       the absolute brightness, of a perceived color.
  3669.  
  3670.    Chunk
  3671.       A section of a PNG file.  Each chunk has a type indicated by its
  3672.       chunk type name.  Most types of chunks also include some data.
  3673.       The format and meaning of the data within the chunk are determined
  3674.       by the type name.
  3675.  
  3676.    Composite
  3677.       As a verb, to form an image by merging a foreground image and a
  3678.       background image, using transparency information to determine
  3679.       where the background should be visible.  The foreground image is
  3680.       said to be "composited against" the background.
  3681.  
  3682.    CRC
  3683.       Cyclic Redundancy Check.  A CRC is a type of check value designed
  3684.       to catch most transmission errors.  A decoder calculates the CRC
  3685.       for the received data and compares it to the CRC that the encoder
  3686.       calculated, which is appended to the data.  A mismatch indicates
  3687.       that the data was corrupted in transit.
  3688.  
  3689.    Critical chunk
  3690.       A chunk that must be understood and processed by the decoder in
  3691.       order to produce a meaningful image from a PNG file.
  3692.  
  3693.    CRT
  3694.       Cathode Ray Tube: a common type of computer display hardware.
  3695.  
  3696.  
  3697.  
  3698. Boutell, et. al.             Informational                     [Page 66]
  3699.  
  3700. RFC 2083            PNG: Portable Network Graphics            March 1997
  3701.  
  3702.  
  3703.    Datastream
  3704.       A sequence of bytes.  This term is used rather than "file" to
  3705.       describe a byte sequence that is only a portion of a file.  We
  3706.       also use it to emphasize that a PNG image might be generated and
  3707.       consumed "on the fly", never appearing in a stored file at all.
  3708.  
  3709.    Deflate
  3710.       The name of the compression algorithm used in standard PNG files,
  3711.       as well as in zip, gzip, pkzip, and other compression programs.
  3712.       Deflate is a member of the LZ77 family of compression methods.
  3713.  
  3714.    Filter
  3715.       A transformation applied to image data in hopes of improving its
  3716.       compressibility.  PNG uses only lossless (reversible) filter
  3717.       algorithms.
  3718.  
  3719.    Frame buffer
  3720.       The final digital storage area for the image shown by a computer
  3721.       display.  Software causes an image to appear onscreen by loading
  3722.       it into the frame buffer.
  3723.  
  3724.    Gamma
  3725.       The brightness of mid-level tones in an image.  More precisely, a
  3726.       parameter that describes the shape of the transfer function for
  3727.       one or more stages in an imaging pipeline.  The transfer function
  3728.       is given by the expression
  3729.  
  3730.          output = input ^ gamma
  3731.  
  3732.       where both input and output are scaled to the range 0 to 1.
  3733.  
  3734.    Grayscale
  3735.       An image representation in which each pixel is represented by a
  3736.       single sample value representing overall luminance (on a scale
  3737.       from black to white).  PNG also permits an alpha sample to be
  3738.       stored for each pixel of a grayscale image.
  3739.  
  3740.    Indexed color
  3741.       An image representation in which each pixel is represented by a
  3742.       single sample that is an index into a palette or lookup table.
  3743.       The selected palette entry defines the actual color of the pixel.
  3744.  
  3745.    Lossless compression
  3746.       Any method of data compression that guarantees the original data
  3747.       can be reconstructed exactly, bit-for-bit.
  3748.  
  3749.  
  3750.  
  3751.  
  3752.  
  3753.  
  3754. Boutell, et. al.             Informational                     [Page 67]
  3755.  
  3756. RFC 2083            PNG: Portable Network Graphics            March 1997
  3757.  
  3758.  
  3759.    Lossy compression
  3760.       Any method of data compression that reconstructs the original data
  3761.       approximately, rather than exactly.
  3762.  
  3763.    LSB
  3764.       Least Significant Byte of a multi-byte value.
  3765.  
  3766.    Luminance
  3767.       Perceived brightness, or grayscale level, of a color.  Luminance
  3768.       and chromaticity together fully define a perceived color.
  3769.  
  3770.    LUT
  3771.       Look Up Table.  In general, a table used to transform data.  In
  3772.       frame buffer hardware, a LUT can be used to map indexed-color
  3773.       pixels into a selected set of truecolor values, or to perform
  3774.       gamma correction.  In software, a LUT can be used as a fast way of
  3775.       implementing any one-variable mathematical function.
  3776.  
  3777.    MSB
  3778.       Most Significant Byte of a multi-byte value.
  3779.  
  3780.    Palette
  3781.       The set of colors available in an indexed-color image.  In PNG, a
  3782.       palette is an array of colors defined by red, green, and blue
  3783.       samples.  (Alpha values can also be defined for palette entries,
  3784.       via the tRNS chunk.)
  3785.  
  3786.    Pixel
  3787.       The information stored for a single grid point in the image.  The
  3788.       complete image is a rectangular array of pixels.
  3789.  
  3790.    PNG editor
  3791.       A program that modifies a PNG file and preserves ancillary
  3792.       information, including chunks that it does not recognize.  Such a
  3793.       program must obey the rules given in Chunk Ordering Rules (Chapter
  3794.       7).
  3795.  
  3796.    Sample
  3797.       A single number in the image data; for example, the red value of a
  3798.       pixel.  A pixel is composed of one or more samples.  When
  3799.       discussing physical data layout (in particular, in Image layout,
  3800.       Section 2.3), we use "sample" to mean a number stored in the image
  3801.       array.  It would be more precise but much less readable to say
  3802.       "sample or palette index" in that context.  Elsewhere in the
  3803.       specification, "sample" means a color value or alpha value.  In
  3804.       the indexed-color case, these are palette entries not palette
  3805.       indexes.
  3806.  
  3807.  
  3808.  
  3809.  
  3810. Boutell, et. al.             Informational                     [Page 68]
  3811.  
  3812. RFC 2083            PNG: Portable Network Graphics            March 1997
  3813.  
  3814.  
  3815.    Sample depth
  3816.       The precision, in bits, of color values and alpha values.  In
  3817.       indexed-color PNGs the sample depth is always 8 by definition of
  3818.       the PLTE chunk.  In other color types it is the same as the bit
  3819.       depth.
  3820.  
  3821.    Scanline
  3822.       One horizontal row of pixels within an image.
  3823.  
  3824.    Truecolor
  3825.       An image representation in which pixel colors are defined by
  3826.       storing three samples for each pixel, representing red, green, and
  3827.       blue intensities respectively.  PNG also permits an alpha sample
  3828.       to be stored for each pixel of a truecolor image.
  3829.  
  3830.    White point
  3831.       The chromaticity of a computer display's nominal white value.
  3832.  
  3833.    zlib
  3834.       A particular format for data that has been compressed using
  3835.       deflate-style compression.  Also the name of a library
  3836.       implementing this method.  PNG implementations need not use the
  3837.       zlib library, but they must conform to its format for compressed
  3838.       data.
  3839.  
  3840. 12. Appendix: Rationale
  3841.  
  3842.    (This appendix is not part of the formal PNG specification.)
  3843.  
  3844.    This appendix gives the reasoning behind some of the design decisions
  3845.    in PNG.  Many of these decisions were the subject of considerable
  3846.    debate.  The authors freely admit that another group might have made
  3847.    different decisions; however, we believe that our choices are
  3848.    defensible and consistent.
  3849.  
  3850.    12.1. Why a new file format?
  3851.  
  3852.       Does the world really need yet another graphics format?  We
  3853.       believe so.  GIF is no longer freely usable, but no other commonly
  3854.       used format can directly replace it, as is discussed in more
  3855.       detail below.  We might have used an adaptation of an existing
  3856.       format, for example GIF with an unpatented compression scheme.
  3857.       But this would require new code anyway; it would not be all that
  3858.       much easier to implement than a whole new file format.  (PNG is
  3859.       designed to be simple to implement, with the exception of the
  3860.       compression engine, which would be needed in any case.)  We feel
  3861.       that this is an excellent opportunity to design a new format that
  3862.       fixes some of the known limitations of GIF.
  3863.  
  3864.  
  3865.  
  3866. Boutell, et. al.             Informational                     [Page 69]
  3867.  
  3868. RFC 2083            PNG: Portable Network Graphics            March 1997
  3869.  
  3870.  
  3871.    12.2. Why these features?
  3872.  
  3873.       The features chosen for PNG are intended to address the needs of
  3874.       applications that previously used the special strengths of GIF.
  3875.       In particular, GIF is well adapted for online communications
  3876.       because of its streamability and progressive display capability.
  3877.       PNG shares those attributes.
  3878.  
  3879.       We have also addressed some of the widely known shortcomings of
  3880.       GIF.  In particular, PNG supports truecolor images.  We know of no
  3881.       widely used image format that losslessly compresses truecolor
  3882.       images as effectively as PNG does.  We hope that PNG will make use
  3883.       of truecolor images more practical and widespread.
  3884.  
  3885.       Some form of transparency control is desirable for applications in
  3886.       which images are displayed against a background or together with
  3887.       other images.  GIF provided a simple transparent-color
  3888.       specification for this purpose.  PNG supports a full alpha channel
  3889.       as well as transparent-color specifications.  This allows both
  3890.       highly flexible transparency and compression efficiency.
  3891.  
  3892.       Robustness against transmission errors has been an important
  3893.       consideration.  For example, images transferred across Internet
  3894.       are often mistakenly processed as text, leading to file
  3895.       corruption.  PNG is designed so that such errors can be detected
  3896.       quickly and reliably.
  3897.  
  3898.       PNG has been expressly designed not to be completely dependent on
  3899.       a single compression technique. Although deflate/inflate
  3900.       compression is mentioned in this document, PNG would still exist
  3901.       without it.
  3902.  
  3903.    12.3. Why not these features?
  3904.  
  3905.       Some features have been deliberately omitted from PNG.  These
  3906.       choices were made to simplify implementation of PNG, promote
  3907.       portability and interchangeability, and make the format as simple
  3908.       and foolproof as possible for users.  In particular:
  3909.  
  3910.  
  3911.  
  3912.  
  3913.  
  3914.  
  3915.  
  3916.  
  3917.  
  3918.  
  3919.  
  3920.  
  3921.  
  3922. Boutell, et. al.             Informational                     [Page 70]
  3923.  
  3924. RFC 2083            PNG: Portable Network Graphics            March 1997
  3925.  
  3926.  
  3927.           * There is no uncompressed variant of PNG.  It is possible to
  3928.             store uncompressed data by using only uncompressed deflate
  3929.             blocks (a feature normally used to guarantee that deflate
  3930.             does not make incompressible data much larger).  However,
  3931.             PNG software must support full deflate/inflate; any software
  3932.             that does not is not compliant with the PNG standard. The
  3933.             two most important features of PNG---portability and
  3934.             compression---are absolute requirements for online
  3935.             applications, and users demand them. Failure to support full
  3936.             deflate/inflate compromises both of these objectives.
  3937.           * There is no lossy compression in PNG.  Existing formats such
  3938.             as JFIF already handle lossy compression well.  Furthermore,
  3939.             available lossy compression methods (e.g., JPEG) are far
  3940.             from foolproof --- a poor choice of quality level can ruin
  3941.             an image.  To avoid user confusion and unintentional loss of
  3942.             information, we feel it is best to keep lossy and lossless
  3943.             formats strictly separate.  Also, lossy compression is
  3944.             complex to implement.  Adding JPEG support to a PNG decoder
  3945.             might increase its size by an order of magnitude.  This
  3946.             would certainly cause some decoders to omit support for the
  3947.             feature, which would destroy our goal of interchangeability.
  3948.           * There is no support for CMYK or other unusual color spaces.
  3949.             Again, this is in the name of promoting portability.  CMYK,
  3950.             in particular, is far too device-dependent to be useful as a
  3951.             portable image representation.
  3952.           * There is no standard chunk for thumbnail views of images.
  3953.             In discussions with software vendors who use thumbnails in
  3954.             their products, it has become clear that most would not use
  3955.             a "standard" thumbnail chunk.  For one thing, every vendor
  3956.             has a different idea of what the dimensions and
  3957.             characteristics of a thumbnail ought to be.  Also, some
  3958.             vendors keep thumbnails in separate files to accommodate
  3959.             varied image formats; they are not going to stop doing that
  3960.             simply because of a thumbnail chunk in one new format.
  3961.             Proprietary chunks containing vendor-specific thumbnails
  3962.             appear to be more practical than a common thumbnail format.
  3963.  
  3964.       It is worth noting that private extensions to PNG could easily add
  3965.       these features.  We will not, however, include them as part of the
  3966.       basic PNG standard.
  3967.  
  3968.  
  3969.  
  3970.  
  3971.  
  3972.  
  3973.  
  3974.  
  3975.  
  3976.  
  3977.  
  3978. Boutell, et. al.             Informational                     [Page 71]
  3979.  
  3980. RFC 2083            PNG: Portable Network Graphics            March 1997
  3981.  
  3982.  
  3983.       PNG also does not support multiple images in one file.  This
  3984.       restriction is a reflection of the reality that many applications
  3985.       do not need and will not support multiple images per file.  In any
  3986.       case, single images are a fundamentally different sort of object
  3987.       from sequences of images.  Rather than make false promises of
  3988.       interchangeability, we have drawn a clear distinction between
  3989.       single-image and multi-image formats.  PNG is a single-image
  3990.       format.  (But see Multiple-image extension, Section 8.4.)
  3991.  
  3992.    12.4. Why not use format X?
  3993.  
  3994.       Numerous existing formats were considered before deciding to
  3995.       develop PNG.  None could meet the requirements we felt were
  3996.       important for PNG.
  3997.  
  3998.       GIF is no longer suitable as a universal standard because of legal
  3999.       entanglements.  Although just replacing GIF's compression method
  4000.       would avoid that problem, GIF does not support truecolor images,
  4001.       alpha channels, or gamma correction.  The spec has more subtle
  4002.       problems too.  Only a small subset of the GIF89 spec is actually
  4003.       portable across a variety of implementations, but there is no
  4004.       codification of the most portable part of the spec.
  4005.  
  4006.       TIFF is far too complex to meet our goals of simplicity and
  4007.       interchangeability.  Defining a TIFF subset would meet that
  4008.       objection, but would frustrate users making the reasonable
  4009.       assumption that a file saved as TIFF from their existing software
  4010.       would load into a program supporting our flavor of TIFF.
  4011.       Furthermore, TIFF is not designed for stream processing, has no
  4012.       provision for progressive display, and does not currently provide
  4013.       any good, legally unencumbered, lossless compression method.
  4014.  
  4015.       IFF has also been suggested, but is not suitable in detail:
  4016.       available image representations are too machine-specific or not
  4017.       adequately compressed.  The overall chunk structure of IFF is a
  4018.       useful concept that PNG has liberally borrowed from, but we did
  4019.       not attempt to be bit-for-bit compatible with IFF chunk structure.
  4020.       Again this is due to detailed issues, notably the fact that IFF
  4021.       FORMs are not designed to be serially writable.
  4022.  
  4023.       Lossless JPEG is not suitable because it does not provide for the
  4024.       storage of indexed-color images.  Furthermore, its lossless
  4025.       truecolor compression is often inferior to that of PNG.
  4026.  
  4027.  
  4028.  
  4029.  
  4030.  
  4031.  
  4032.  
  4033.  
  4034. Boutell, et. al.             Informational                     [Page 72]
  4035.  
  4036. RFC 2083            PNG: Portable Network Graphics            March 1997
  4037.  
  4038.  
  4039.    12.5. Byte order
  4040.  
  4041.       It has been asked why PNG uses network byte order.  We have
  4042.       selected one byte ordering and used it consistently. Which order
  4043.       in particular is of little relevance, but network byte order has
  4044.       the advantage that routines to convert to and from it are already
  4045.       available on any platform that supports TCP/IP networking,
  4046.       including all PC platforms.  The functions are trivial and will be
  4047.       included in the reference implementation.
  4048.  
  4049.    12.6. Interlacing
  4050.  
  4051.       PNG's two-dimensional interlacing scheme is more complex to
  4052.       implement than GIF's line-wise interlacing.  It also costs a
  4053.       little more in file size.  However, it yields an initial image
  4054.       eight times faster than GIF (the first pass transmits only 1/64th
  4055.       of the pixels, compared to 1/8th for GIF).  Although this initial
  4056.       image is coarse, it is useful in many situations.  For example, if
  4057.       the image is a World Wide Web imagemap that the user has seen
  4058.       before, PNG's first pass is often enough to determine where to
  4059.       click.  The PNG scheme also looks better than GIF's, because
  4060.       horizontal and vertical resolution never differ by more than a
  4061.       factor of two; this avoids the odd "stretched" look seen when
  4062.       interlaced GIFs are filled in by replicating scanlines.
  4063.       Preliminary results show that small text in an interlaced PNG
  4064.       image is typically readable about twice as fast as in an
  4065.       equivalent GIF, i.e., after PNG's fifth pass or 25% of the image
  4066.       data, instead of after GIF's third pass or 50%.  This is again due
  4067.       to PNG's more balanced increase in resolution.
  4068.  
  4069.    12.7. Why gamma?
  4070.  
  4071.       It might seem natural to standardize on storing sample values that
  4072.       are linearly proportional to light intensity (that is, have gamma
  4073.       of 1.0).  But in fact, it is common for images to have a gamma of
  4074.       less than 1.  There are three good reasons for this:
  4075.  
  4076.           * For reasons detailed in Gamma Tutorial (Chapter 13), all
  4077.             video cameras apply a "gamma correction" function to the
  4078.             intensity information.  This causes the video signal to have
  4079.             a gamma of about 0.5 relative to the light intensity in the
  4080.             original scene.  Thus, images obtained by frame-grabbing
  4081.             video already have a gamma of about 0.5.
  4082.           * The human eye has a nonlinear response to intensity, so
  4083.             linear encoding of samples either wastes sample codes in
  4084.             bright areas of the image, or provides too few sample codes
  4085.             to avoid banding artifacts in dark areas of the image, or
  4086.             both.  At least 12 bits per sample are needed to avoid
  4087.  
  4088.  
  4089.  
  4090. Boutell, et. al.             Informational                     [Page 73]
  4091.  
  4092. RFC 2083            PNG: Portable Network Graphics            March 1997
  4093.  
  4094.  
  4095.             visible artifacts in linear encoding with a 100:1 image
  4096.             intensity range.  An image gamma in the range 0.3 to 0.5
  4097.             allocates sample values in a way that roughly corresponds to
  4098.             the eye's response, so that 8 bits/sample are enough to
  4099.             avoid artifacts caused by insufficient sample precision in
  4100.             almost all images.  This makes "gamma encoding" a much
  4101.             better way of storing digital images than the simpler linear
  4102.             encoding.
  4103.           * Many images are created on PCs or workstations with no gamma
  4104.             correction hardware and no software willing to provide gamma
  4105.             correction either.  In these cases, the images have had
  4106.             their lighting and color chosen to look best on this
  4107.             platform --- they can be thought of as having "manual" gamma
  4108.             correction built in.  To see what the image author intended,
  4109.             it is necessary to treat such images as having a file_gamma
  4110.             value in the range 0.4-0.6, depending on the room lighting
  4111.             level that the author was working in.
  4112.  
  4113.       In practice, image gamma values around 1.0 and around 0.5 are both
  4114.       widely found.  Older image standards such as GIF often do not
  4115.       account for this fact.  The JFIF standard specifies that images in
  4116.       that format should use linear samples, but many JFIF images found
  4117.       on the Internet actually have a gamma somewhere near 0.4 or 0.5.
  4118.       The variety of images found and the variety of systems that people
  4119.       display them on have led to widespread problems with images
  4120.       appearing "too dark" or "too light".
  4121.  
  4122.       PNG expects viewers to compensate for image gamma at the time that
  4123.       the image is displayed. Another possible approach is to expect
  4124.       encoders to convert all images to a uniform gamma at encoding
  4125.       time. While that method would speed viewers slightly, it has
  4126.       fundamental flaws:
  4127.  
  4128.           * Gamma correction is inherently lossy due to quantization and
  4129.             roundoff error.  Requiring conversion at encoding time thus
  4130.             causes irreversible loss. Since PNG is intended to be a
  4131.             lossless storage format, this is undesirable; we should
  4132.             store unmodified source data.
  4133.           * The encoder might not know the source gamma value. If the
  4134.             decoder does gamma correction at viewing time, it can adjust
  4135.             the gamma (change the displayed brightness) in response to
  4136.             feedback from a human user. The encoder has no such
  4137.             recourse.
  4138.           * Whatever "standard" gamma we settled on would be wrong for
  4139.             some displays. Hence viewers would still need gamma
  4140.             correction capability.
  4141.  
  4142.  
  4143.  
  4144.  
  4145.  
  4146. Boutell, et. al.             Informational                     [Page 74]
  4147.  
  4148. RFC 2083            PNG: Portable Network Graphics            March 1997
  4149.  
  4150.  
  4151.       Since there will always be images with no gamma or an incorrect
  4152.       recorded gamma, good viewers will need to incorporate gamma
  4153.       adjustment code anyway. Gamma correction at viewing time is thus
  4154.       the right way to go.
  4155.  
  4156.       See Gamma Tutorial (Chapter 13) for more information.
  4157.  
  4158.    12.8. Non-premultiplied alpha
  4159.  
  4160.       PNG uses "unassociated" or "non-premultiplied" alpha so that
  4161.       images with separate transparency masks can be stored losslessly.
  4162.       Another common technique, "premultiplied alpha", stores pixel
  4163.       values premultiplied by the alpha fraction; in effect, the image
  4164.       is already composited against a black background.  Any image data
  4165.       hidden by the transparency mask is irretrievably lost by that
  4166.       method, since multiplying by a zero alpha value always produces
  4167.       zero.
  4168.  
  4169.       Some image rendering techniques generate images with premultiplied
  4170.       alpha (the alpha value actually represents how much of the pixel
  4171.       is covered by the image).  This representation can be converted to
  4172.       PNG by dividing the sample values by alpha, except where alpha is
  4173.       zero.  The result will look good if displayed by a viewer that
  4174.       handles alpha properly, but will not look very good if the viewer
  4175.       ignores the alpha channel.
  4176.  
  4177.       Although each form of alpha storage has its advantages, we did not
  4178.       want to require all PNG viewers to handle both forms.  We
  4179.       standardized on non-premultiplied alpha as being the lossless and
  4180.       more general case.
  4181.  
  4182.    12.9. Filtering
  4183.  
  4184.       PNG includes filtering capability because filtering can
  4185.       significantly reduce the compressed size of truecolor and
  4186.       grayscale images.  Filtering is also sometimes of value on
  4187.       indexed-color images, although this is less common.
  4188.  
  4189.       The filter algorithms are defined to operate on bytes, rather than
  4190.       pixels; this gains simplicity and speed with very little cost in
  4191.       compression performance.  Tests have shown that filtering is
  4192.       usually ineffective for images with fewer than 8 bits per sample,
  4193.       so providing pixelwise filtering for such images would be
  4194.       pointless.  For 16 bit/sample data, bytewise filtering is nearly
  4195.       as effective as pixelwise filtering, because MSBs are predicted
  4196.       from adjacent MSBs, and LSBs are predicted from adjacent LSBs.
  4197.  
  4198.  
  4199.  
  4200.  
  4201.  
  4202. Boutell, et. al.             Informational                     [Page 75]
  4203.  
  4204. RFC 2083            PNG: Portable Network Graphics            March 1997
  4205.  
  4206.  
  4207.       The encoder is allowed to change filters for each new scanline.
  4208.       This creates no additional complexity for decoders, since a
  4209.       decoder is required to contain defiltering logic for every filter
  4210.       type anyway.  The only cost is an extra byte per scanline in the
  4211.       pre-compression datastream.  Our tests showed that when the same
  4212.       filter is selected for all scanlines, this extra byte compresses
  4213.       away to almost nothing, so there is little storage cost compared
  4214.       to a fixed filter specified for the whole image.  And the
  4215.       potential benefits of adaptive filtering are too great to ignore.
  4216.       Even with the simplistic filter-choice heuristics so far
  4217.       discovered, adaptive filtering usually outperforms fixed filters.
  4218.       In particular, an adaptive filter can change behavior for
  4219.       successive passes of an interlaced image; a fixed filter cannot.
  4220.  
  4221.    12.10. Text strings
  4222.  
  4223.       Most graphics file formats include the ability to store some
  4224.       textual information along with the image.  But many applications
  4225.       need more than that: they want to be able to store several
  4226.       identifiable pieces of text.  For example, a database using PNG
  4227.       files to store medical X-rays would likely want to include
  4228.       patient's name, doctor's name, etc.  A simple way to do this in
  4229.       PNG would be to invent new private chunks holding text.  The
  4230.       disadvantage of such an approach is that other applications would
  4231.       have no idea what was in those chunks, and would simply ignore
  4232.       them.  Instead, we recommend that textual information be stored in
  4233.       standard tEXt chunks with suitable keywords.  Use of tEXt tells
  4234.       any PNG viewer that the chunk contains text that might be of
  4235.       interest to a human user.  Thus, a person looking at the file with
  4236.       another viewer will still be able to see the text, and even
  4237.       understand what it is if the keywords are reasonably self-
  4238.       explanatory.  (To this end, we recommend spelled-out keywords, not
  4239.       abbreviations that will be hard for a person to understand.
  4240.       Saving a few bytes on a keyword is false economy.)
  4241.  
  4242.       The ISO 8859-1 (Latin-1) character set was chosen as a compromise
  4243.       between functionality and portability.  Some platforms cannot
  4244.       display anything more than 7-bit ASCII characters, while others
  4245.       can handle characters beyond the Latin-1 set.  We felt that
  4246.       Latin-1 represents a widely useful and reasonably portable
  4247.       character set.  Latin-1 is a direct subset of character sets
  4248.       commonly used on popular platforms such as Microsoft Windows and X
  4249.       Windows.  It can also be handled on Macintosh systems with a
  4250.       simple remapping of characters.
  4251.  
  4252.       There is presently no provision for text employing character sets
  4253.       other than Latin-1. We recognize that the need for other character
  4254.       sets will increase.  However, PNG already requires that
  4255.  
  4256.  
  4257.  
  4258. Boutell, et. al.             Informational                     [Page 76]
  4259.  
  4260. RFC 2083            PNG: Portable Network Graphics            March 1997
  4261.  
  4262.  
  4263.       programmers implement a number of new and unfamiliar features, and
  4264.       text representation is not PNG's primary purpose. Since PNG
  4265.       provides for the creation and public registration of new ancillary
  4266.       chunks of general interest, we expect that text chunks for other
  4267.       character sets, such as Unicode, eventually will be registered and
  4268.       increase gradually in popularity.
  4269.  
  4270.    12.11. PNG file signature
  4271.  
  4272.       The first eight bytes of a PNG file always contain the following
  4273.       values:
  4274.  
  4275.          (decimal)              137  80  78  71  13  10  26  10
  4276.          (hexadecimal)           89  50  4e  47  0d  0a  1a  0a
  4277.          (ASCII C notation)    \211   P   N   G  \r  \n \032 \n
  4278.  
  4279.       This signature both identifies the file as a PNG file and provides
  4280.       for immediate detection of common file-transfer problems.  The
  4281.       first two bytes distinguish PNG files on systems that expect the
  4282.       first two bytes to identify the file type uniquely.  The first
  4283.       byte is chosen as a non-ASCII value to reduce the probability that
  4284.       a text file may be misrecognized as a PNG file; also, it catches
  4285.       bad file transfers that clear bit 7.  Bytes two through four name
  4286.       the format.  The CR-LF sequence catches bad file transfers that
  4287.       alter newline sequences.  The control-Z character stops file
  4288.       display under MS-DOS.  The final line feed checks for the inverse
  4289.       of the CR-LF translation problem.
  4290.  
  4291.       A decoder may further verify that the next eight bytes contain an
  4292.       IHDR chunk header with the correct chunk length; this will catch
  4293.       bad transfers that drop or alter null (zero) bytes.
  4294.  
  4295.       Note that there is no version number in the signature, nor indeed
  4296.       anywhere in the file.  This is intentional: the chunk mechanism
  4297.       provides a better, more flexible way to handle format extensions,
  4298.       as explained in Chunk naming conventions (Section 12.13).
  4299.  
  4300.    12.12. Chunk layout
  4301.  
  4302.       The chunk design allows decoders to skip unrecognized or
  4303.       uninteresting chunks: it is simply necessary to skip the
  4304.       appropriate number of bytes, as determined from the length field.
  4305.  
  4306.       Limiting chunk length to (2^31)-1 bytes avoids possible problems
  4307.       for implementations that cannot conveniently handle 4-byte
  4308.       unsigned values.  In practice, chunks will usually be much shorter
  4309.       than that anyway.
  4310.  
  4311.  
  4312.  
  4313.  
  4314. Boutell, et. al.             Informational                     [Page 77]
  4315.  
  4316. RFC 2083            PNG: Portable Network Graphics            March 1997
  4317.  
  4318.  
  4319.       A separate CRC is provided for each chunk in order to detect
  4320.       badly-transferred images as quickly as possible.  In particular,
  4321.       critical data such as the image dimensions can be validated before
  4322.       being used.
  4323.  
  4324.       The chunk length is excluded from the CRC so that the CRC can be
  4325.       calculated as the data is generated; this avoids a second pass
  4326.       over the data in cases where the chunk length is not known in
  4327.       advance.  Excluding the length from the CRC does not create any
  4328.       extra risk of failing to discover file corruption, since if the
  4329.       length is wrong, the CRC check will fail: the CRC will be computed
  4330.       on the wrong set of bytes and then be tested against the wrong
  4331.       value from the file.
  4332.  
  4333.    12.13. Chunk naming conventions
  4334.  
  4335.       The chunk naming conventions allow safe, flexible extension of the
  4336.       PNG format.  This mechanism is much better than a format version
  4337.       number, because it works on a feature-by-feature basis rather than
  4338.       being an overall indicator.  Decoders can process newer files if
  4339.       and only if the files use no unknown critical features (as
  4340.       indicated by finding unknown critical chunks).  Unknown ancillary
  4341.       chunks can be safely ignored.  We decided against having an
  4342.       overall format version number because experience has shown that
  4343.       format version numbers hurt portability as much as they help.
  4344.       Version numbers tend to be set unnecessarily high, leading to
  4345.       older decoders rejecting files that they could have processed
  4346.       (this was a serious problem for several years after the GIF89 spec
  4347.       came out, for example).  Furthermore, private extensions can be
  4348.       made either critical or ancillary, and standard decoders should
  4349.       react appropriately; overall version numbers are no help for
  4350.       private extensions.
  4351.  
  4352.       A hypothetical chunk for vector graphics would be a critical
  4353.       chunk, since if ignored, important parts of the intended image
  4354.       would be missing.  A chunk carrying the Mandelbrot set coordinates
  4355.       for a fractal image would be ancillary, since other applications
  4356.       could display the image without understanding what the image
  4357.       represents.  In general, a chunk type should be made critical only
  4358.       if it is impossible to display a reasonable representation of the
  4359.       intended image without interpreting that chunk.
  4360.  
  4361.  
  4362.  
  4363.  
  4364.  
  4365.  
  4366.  
  4367.  
  4368.  
  4369.  
  4370. Boutell, et. al.             Informational                     [Page 78]
  4371.  
  4372. RFC 2083            PNG: Portable Network Graphics            March 1997
  4373.  
  4374.  
  4375.       The public/private property bit ensures that any newly defined
  4376.       public chunk type name cannot conflict with proprietary chunks
  4377.       that could be in use somewhere.  However, this does not protect
  4378.       users of private chunk names from the possibility that someone
  4379.       else may use the same chunk name for a different purpose.  It is a
  4380.       good idea to put additional identifying information at the start
  4381.       of the data for any private chunk type.
  4382.  
  4383.       When a PNG file is modified, certain ancillary chunks may need to
  4384.       be changed to reflect changes in other chunks. For example, a
  4385.       histogram chunk needs to be changed if the image data changes.  If
  4386.       the file editor does not recognize histogram chunks, copying them
  4387.       blindly to a new output file is incorrect; such chunks should be
  4388.       dropped.  The safe/unsafe property bit allows ancillary chunks to
  4389.       be marked appropriately.
  4390.  
  4391.       Not all possible modification scenarios are covered by the
  4392.       safe/unsafe semantics.  In particular, chunks that are dependent
  4393.       on the total file contents are not supported.  (An example of such
  4394.       a chunk is an index of IDAT chunk locations within the file:
  4395.       adding a comment chunk would inadvertently break the index.)
  4396.       Definition of such chunks is discouraged.  If absolutely necessary
  4397.       for a particular application, such chunks can be made critical
  4398.       chunks, with consequent loss of portability to other applications.
  4399.       In general, ancillary chunks can depend on critical chunks but not
  4400.       on other ancillary chunks.  It is expected that mutually dependent
  4401.       information should be put into a single chunk.
  4402.  
  4403.       In some situations it may be unavoidable to make one ancillary
  4404.       chunk dependent on another.  Although the chunk property bits are
  4405.       insufficient to represent this case, a simple solution is
  4406.       available: in the dependent chunk, record the CRC of the chunk
  4407.       depended on.  It can then be determined whether that chunk has
  4408.       been changed by some other program.
  4409.  
  4410.       The same technique can be useful for other purposes.  For example,
  4411.       if a program relies on the palette being in a particular order, it
  4412.       can store a private chunk containing the CRC of the PLTE chunk.
  4413.       If this value matches when the file is again read in, then it
  4414.       provides high confidence that the palette has not been tampered
  4415.       with.  Note that it is not necessary to mark the private chunk
  4416.       unsafe-to-copy when this technique is used; thus, such a private
  4417.       chunk can survive other editing of the file.
  4418.  
  4419.  
  4420.  
  4421.  
  4422.  
  4423.  
  4424.  
  4425.  
  4426. Boutell, et. al.             Informational                     [Page 79]
  4427.  
  4428. RFC 2083            PNG: Portable Network Graphics            March 1997
  4429.  
  4430.  
  4431.    12.14. Palette histograms
  4432.  
  4433.       A viewer may not be able to provide as many colors as are listed
  4434.       in the image's palette.  (For example, some colors could be
  4435.       reserved by a window system.)  To produce the best results in this
  4436.       situation, it is helpful to have information about the frequency
  4437.       with which each palette index actually appears, in order to choose
  4438.       the best palette for dithering or to drop the least-used colors.
  4439.       Since images are often created once and viewed many times, it
  4440.       makes sense to calculate this information in the encoder, although
  4441.       it is not mandatory for the encoder to provide it.
  4442.  
  4443.       Other image formats have usually addressed this problem by
  4444.       specifying that the palette entries should appear in order of
  4445.       frequency of use.  That is an inferior solution, because it
  4446.       doesn't give the viewer nearly as much information: the viewer
  4447.       can't determine how much damage will be done by dropping the last
  4448.       few colors.  Nor does a sorted palette give enough information to
  4449.       choose a target palette for dithering, in the case that the viewer
  4450.       needs to reduce the number of colors substantially.  A palette
  4451.       histogram provides the information needed to choose such a target
  4452.       palette without making a pass over the image data.
  4453.  
  4454.  
  4455.  
  4456.  
  4457.  
  4458.  
  4459.  
  4460.  
  4461.  
  4462.  
  4463.  
  4464.  
  4465.  
  4466.  
  4467.  
  4468.  
  4469.  
  4470.  
  4471.  
  4472.  
  4473.  
  4474.  
  4475.  
  4476.  
  4477.  
  4478.  
  4479.  
  4480.  
  4481.  
  4482. Boutell, et. al.             Informational                     [Page 80]
  4483.  
  4484. RFC 2083            PNG: Portable Network Graphics            March 1997
  4485.  
  4486.  
  4487. 13. Appendix: Gamma Tutorial
  4488.  
  4489.    (This appendix is not part of the formal PNG specification.)
  4490.  
  4491.    It would be convenient for graphics programmers if all of the
  4492.    components of an imaging system were linear.  The voltage coming from
  4493.    an electronic camera would be directly proportional to the intensity
  4494.    (power) of light in the scene, the light emitted by a CRT would be
  4495.    directly proportional to its input voltage, and so on.  However,
  4496.    real-world devices do not behave in this way.  All CRT displays,
  4497.    almost all photographic film, and many electronic cameras have
  4498.    nonlinear signal-to-light-intensity or intensity-to-signal
  4499.    characteristics.
  4500.  
  4501.    Fortunately, all of these nonlinear devices have a transfer function
  4502.    that is approximated fairly well by a single type of mathematical
  4503.    function: a power function.  This power function has the general
  4504.    equation
  4505.  
  4506.       output = input ^ gamma
  4507.  
  4508.    where ^ denotes exponentiation, and "gamma" (often printed using the
  4509.    Greek letter gamma, thus the name) is simply the exponent of the
  4510.    power function.
  4511.  
  4512.    By convention, "input" and "output" are both scaled to the range
  4513.    0..1, with 0 representing black and 1 representing maximum white (or
  4514.    red, etc).  Normalized in this way, the power function is completely
  4515.    described by a single number, the exponent "gamma".
  4516.  
  4517.    So, given a particular device, we can measure its output as a
  4518.    function of its input, fit a power function to this measured transfer
  4519.    function, extract the exponent, and call it gamma.  We often say
  4520.    "this device has a gamma of 2.5" as a shorthand for "this device has
  4521.    a power-law response with an exponent of 2.5".  We can also talk
  4522.    about the gamma of a mathematical transform, or of a lookup table in
  4523.    a frame buffer, so long as the input and output of the thing are
  4524.    related by the power-law expression above.
  4525.  
  4526.    How do gammas combine?
  4527.  
  4528.       Real imaging systems will have several components, and more than
  4529.       one of these can be nonlinear.  If all of the components have
  4530.       transfer characteristics that are power functions, then the
  4531.       transfer function of the entire system is also a power function.
  4532.       The exponent (gamma) of the whole system's transfer function is
  4533.       just the product of all of the individual exponents (gammas) of
  4534.       the separate stages in the system.
  4535.  
  4536.  
  4537.  
  4538. Boutell, et. al.             Informational                     [Page 81]
  4539.  
  4540. RFC 2083            PNG: Portable Network Graphics            March 1997
  4541.  
  4542.  
  4543.       Also, stages that are linear pose no problem, since a power
  4544.       function with an exponent of 1.0 is really a linear function.  So
  4545.       a linear transfer function is just a special case of a power
  4546.       function, with a gamma of 1.0.
  4547.  
  4548.       Thus, as long as our imaging system contains only stages with
  4549.       linear and power-law transfer functions, we can meaningfully talk
  4550.       about the gamma of the entire system.  This is indeed the case
  4551.       with most real imaging systems.
  4552.  
  4553.    What should overall gamma be?
  4554.  
  4555.       If the overall gamma of an imaging system is 1.0, its output is
  4556.       linearly proportional to its input.  This means that the ratio
  4557.       between the intensities of any two areas in the reproduced image
  4558.       will be the same as it was in the original scene.  It might seem
  4559.       that this should always be the goal of an imaging system: to
  4560.       accurately reproduce the tones of the original scene.  Alas, that
  4561.       is not the case.
  4562.  
  4563.       When the reproduced image is to be viewed in "bright surround"
  4564.       conditions, where other white objects nearby in the room have
  4565.       about the same brightness as white in the image, then an overall
  4566.       gamma of 1.0 does indeed give real-looking reproduction of a
  4567.       natural scene.  Photographic prints viewed under room light and
  4568.       computer displays in bright room light are typical "bright
  4569.       surround" viewing conditions.
  4570.  
  4571.       However, sometimes images are intended to be viewed in "dark
  4572.       surround" conditions, where the room is substantially black except
  4573.       for the image.  This is typical of the way movies and slides
  4574.       (transparencies) are viewed by projection.  Under these
  4575.       circumstances, an accurate reproduction of the original scene
  4576.       results in an image that human viewers judge as "flat" and lacking
  4577.       in contrast.  It turns out that the projected image needs to have
  4578.       a gamma of about 1.5 relative to the original scene for viewers to
  4579.       judge it "natural".  Thus, slide film is designed to have a gamma
  4580.       of about 1.5, not 1.0.
  4581.  
  4582.       There is also an intermediate condition called "dim surround",
  4583.       where the rest of the room is still visible to the viewer, but is
  4584.       noticeably darker than the reproduced image itself.  This is
  4585.       typical of television viewing, at least in the evening, as well as
  4586.       subdued-light computer work areas.  In dim surround conditions,
  4587.       the reproduced image needs to have a gamma of about 1.25 relative
  4588.       to the original scene in order to look natural.
  4589.  
  4590.  
  4591.  
  4592.  
  4593.  
  4594. Boutell, et. al.             Informational                     [Page 82]
  4595.  
  4596. RFC 2083            PNG: Portable Network Graphics            March 1997
  4597.  
  4598.  
  4599.       The requirement for boosted contrast (gamma) in dark surround
  4600.       conditions is due to the way the human visual system works, and
  4601.       applies equally well to computer monitors.  Thus, a PNG viewer
  4602.       trying to achieve the maximum realism for the images it displays
  4603.       really needs to know what the room lighting conditions are, and
  4604.       adjust the gamma of the displayed image accordingly.
  4605.  
  4606.       If asking the user about room lighting conditions is inappropriate
  4607.       or too difficult, just assume that the overall gamma
  4608.       (viewing_gamma as defined below) should be 1.0 or 1.25.  That's
  4609.       all that most systems that implement gamma correction do.
  4610.  
  4611.    What is a CRT's gamma?
  4612.  
  4613.       All CRT displays have a power-law transfer characteristic with a
  4614.       gamma of about 2.5.  This is due to the physical processes
  4615.       involved in controlling the electron beam in the electron gun, and
  4616.       has nothing to do with the phosphor.
  4617.  
  4618.       An exception to this rule is fancy "calibrated" CRTs that have
  4619.       internal electronics to alter their transfer function.  If you
  4620.       have one of these, you probably should believe what the
  4621.       manufacturer tells you its gamma is.  But in all other cases,
  4622.       assuming 2.5 is likely to be pretty accurate.
  4623.  
  4624.       There are various images around that purport to measure gamma,
  4625.       usually by comparing the intensity of an area containing
  4626.       alternating white and black with a series of areas of continuous
  4627.       gray of different intensity.  These are usually not reliable.
  4628.       Test images that use a "checkerboard" pattern of black and white
  4629.       are the worst, because a single white pixel will be reproduced
  4630.       considerably darker than a large area of white.  An image that
  4631.       uses alternating black and white horizontal lines (such as the
  4632.       "gamma.png" test image at
  4633.       ftp://ftp.uu.net/graphics/png/images/suite/gamma.png) is much
  4634.       better, but even it may be inaccurate at high "picture" settings
  4635.       on some CRTs.
  4636.  
  4637.       If you have a good photometer, you can measure the actual light
  4638.       output of a CRT as a function of input voltage and fit a power
  4639.       function to the measurements.  However, note that this procedure
  4640.       is very sensitive to the CRT's black level adjustment, somewhat
  4641.       sensitive to its picture adjustment, and also affected by ambient
  4642.       light.  Furthermore, CRTs spread some light from bright areas of
  4643.       an image into nearby darker areas; a single bright spot against a
  4644.       black background may be seen to have a "halo".  Your measuring
  4645.       technique will need to minimize the effects of this.
  4646.  
  4647.  
  4648.  
  4649.  
  4650. Boutell, et. al.             Informational                     [Page 83]
  4651.  
  4652. RFC 2083            PNG: Portable Network Graphics            March 1997
  4653.  
  4654.  
  4655.       Because of the difficulty of measuring gamma, using either test
  4656.       images or measuring equipment, you're usually better off just
  4657.       assuming gamma is 2.5 rather than trying to measure it.
  4658.  
  4659.    What is gamma correction?
  4660.  
  4661.       A CRT has a gamma of 2.5, and we can't change that.  To get an
  4662.       overall gamma of 1.0 (or somewhere near that) for an imaging
  4663.       system, we need to have at least one other component of the "image
  4664.       pipeline" that is nonlinear.  If, in fact, there is only one
  4665.       nonlinear stage in addition to the CRT, then it's traditional to
  4666.       say that the CRT has a certain gamma, and that the other nonlinear
  4667.       stage provides "gamma correction" to compensate for the CRT.
  4668.       However, exactly where the "correction" is done depends on
  4669.       circumstance.
  4670.  
  4671.       In all broadcast video systems, gamma correction is done in the
  4672.       camera.  This choice was made in the days when television
  4673.       electronics were all analog, and a good gamma-correction circuit
  4674.       was expensive to build.  The original NTSC video standard required
  4675.       cameras to have a transfer function with a gamma of 1/2.2, or
  4676.       about 0.45.  Recently, a more complex two-part transfer function
  4677.       has been adopted [SMPTE-170M], but its behavior can be well
  4678.       approximated by a power function with a gamma of 0.5.  When the
  4679.       resulting image is displayed on a CRT with a gamma of 2.5, the
  4680.       image on screen ends up with a gamma of about 1.25 relative to the
  4681.       original scene, which is appropriate for "dim surround" viewing.
  4682.  
  4683.       These days, video signals are often digitized and stored in
  4684.       computer frame buffers.  This works fine, but remember that gamma
  4685.       correction is "built into" the video signal, and so the digitized
  4686.       video has a gamma of about 0.5 relative to the original scene.
  4687.  
  4688.       Computer rendering programs often produce linear samples.  To
  4689.       display these correctly, intensity on the CRT needs to be directly
  4690.       proportional to the sample values in the frame buffer.  This can
  4691.       be done with a special hardware lookup table between the frame
  4692.       buffer and the CRT hardware.  The lookup table (often called LUT)
  4693.       is loaded with a mapping that implements a power function with a
  4694.       gamma of 0.4, thus providing "gamma correction" for the CRT gamma.
  4695.  
  4696.       Thus, gamma correction sometimes happens before the frame buffer,
  4697.       sometimes after.  As long as images created in a particular
  4698.       environment are always displayed in that environment, everything
  4699.       is fine.  But when people try to exchange images, differences in
  4700.       gamma correction conventions often result in images that seem far
  4701.       too bright and washed out, or far too dark and contrasty.
  4702.  
  4703.  
  4704.  
  4705.  
  4706. Boutell, et. al.             Informational                     [Page 84]
  4707.  
  4708. RFC 2083            PNG: Portable Network Graphics            March 1997
  4709.  
  4710.  
  4711.    Gamma-encoded samples are good
  4712.  
  4713.       So, is it better to do gamma correction before or after the frame
  4714.       buffer?
  4715.  
  4716.       In an ideal world, sample values would be stored in floating
  4717.       point, there would be lots of precision, and it wouldn't really
  4718.       matter much.  But in reality, we're always trying to store images
  4719.       in as few bits as we can.
  4720.  
  4721.       If we decide to use samples that are linearly proportional to
  4722.       intensity, and do the gamma correction in the frame buffer LUT, it
  4723.       turns out that we need to use at least 12 bits for each of red,
  4724.       green, and blue to have enough precision in intensity.  With any
  4725.       less than that, we will sometimes see "contour bands" or "Mach
  4726.       bands" in the darker areas of the image, where two adjacent sample
  4727.       values are still far enough apart in intensity for the difference
  4728.       to be visible.
  4729.  
  4730.       However, through an interesting coincidence, the human eye's
  4731.       subjective perception of brightness is related to the physical
  4732.       stimulation of light intensity in a manner that is very much like
  4733.       the power function used for gamma correction.  If we apply gamma
  4734.       correction to measured (or calculated) light intensity before
  4735.       quantizing to an integer for storage in a frame buffer, we can get
  4736.       away with using many fewer bits to store the image.  In fact, 8
  4737.       bits per color is almost always sufficient to avoid contouring
  4738.       artifacts.  This is because, since gamma correction is so closely
  4739.       related to human perception, we are assigning our 256 available
  4740.       sample codes to intensity values in a manner that approximates how
  4741.       visible those intensity changes are to the eye.  Compared to a
  4742.       linear-sample image, we allocate fewer sample values to brighter
  4743.       parts of the tonal range and more sample values to the darker
  4744.       portions of the tonal range.
  4745.  
  4746.       Thus, for the same apparent image quality, images using gamma-
  4747.       encoded sample values need only about two-thirds as many bits of
  4748.       storage as images using linear samples.
  4749.  
  4750.  
  4751.  
  4752.  
  4753.  
  4754.  
  4755.  
  4756.  
  4757.  
  4758.  
  4759.  
  4760.  
  4761.  
  4762. Boutell, et. al.             Informational                     [Page 85]
  4763.  
  4764. RFC 2083            PNG: Portable Network Graphics            March 1997
  4765.  
  4766.  
  4767.    General gamma handling
  4768.  
  4769.       When more than two nonlinear transfer functions are involved in
  4770.       the image pipeline, the term "gamma correction" becomes too vague.
  4771.       If we consider a pipeline that involves capturing (or calculating)
  4772.       an image, storing it in an image file, reading the file, and
  4773.       displaying the image on some sort of display screen, there are at
  4774.       least 5 places in the pipeline that could have nonlinear transfer
  4775.       functions.  Let's give each a specific name for their
  4776.       characteristic gamma:
  4777.  
  4778.       camera_gamma
  4779.          the characteristic of the image sensor
  4780.  
  4781.       encoding_gamma
  4782.          the gamma of any transformation performed by the software
  4783.          writing the image file
  4784.  
  4785.       decoding_gamma
  4786.          the gamma of any transformation performed by the software
  4787.          reading the image file
  4788.  
  4789.       LUT_gamma
  4790.          the gamma of the frame buffer LUT, if present
  4791.  
  4792.       CRT_gamma
  4793.          the gamma of the CRT, generally 2.5
  4794.  
  4795.       In addition, let's add a few other names:
  4796.  
  4797.       file_gamma
  4798.          the gamma of the image in the file, relative to the original
  4799.          scene.  This is
  4800.  
  4801.             file_gamma = camera_gamma * encoding_gamma
  4802.  
  4803.       display_gamma
  4804.          the gamma of the "display system" downstream of the frame
  4805.          buffer.  This is
  4806.  
  4807.             display_gamma = LUT_gamma * CRT_gamma
  4808.  
  4809.       viewing_gamma
  4810.          the overall gamma that we want to obtain to produce pleasing
  4811.          images --- generally 1.0 to 1.5.
  4812.  
  4813.  
  4814.  
  4815.  
  4816.  
  4817.  
  4818. Boutell, et. al.             Informational                     [Page 86]
  4819.  
  4820. RFC 2083            PNG: Portable Network Graphics            March 1997
  4821.  
  4822.  
  4823.       The file_gamma value, as defined above, is what goes in the gAMA
  4824.       chunk in a PNG file.  If file_gamma is not 1.0, we know that gamma
  4825.       correction has been done on the sample values in the file, and we
  4826.       could call them "gamma corrected" samples.  However, since there
  4827.       can be so many different values of gamma in the image display
  4828.       chain, and some of them are not known at the time the image is
  4829.       written, the samples are not really being "corrected" for a
  4830.       specific display condition.  We are really using a power function
  4831.       in the process of encoding an intensity range into a small integer
  4832.       field, and so it is more correct to say "gamma encoded" samples
  4833.       instead of "gamma corrected" samples.
  4834.  
  4835.       When displaying an image file, the image decoding program is
  4836.       responsible for making the overall gamma of the system equal to
  4837.       the desired viewing_gamma, by selecting the decoding_gamma
  4838.       appropriately.  When displaying a PNG file, the gAMA chunk
  4839.       provides the file_gamma value.  The display_gamma may be known for
  4840.       this machine, or it might be obtained from the system software, or
  4841.       the user might have to be asked what it is.  The correct
  4842.       viewing_gamma depends on lighting conditions, and that will
  4843.       generally have to come from the user.
  4844.  
  4845.       Ultimately, you should have
  4846.  
  4847.          file_gamma * decoding_gamma * display_gamma = viewing_gamma
  4848.  
  4849.    Some specific examples
  4850.  
  4851.       In digital video systems, camera_gamma is about 0.5 by declaration
  4852.       of the various video standards documents.  CRT_gamma is 2.5 as
  4853.       usual, while encoding_gamma, decoding_gamma, and LUT_gamma are all
  4854.       1.0.  As a result, viewing_gamma ends up being about 1.25.
  4855.  
  4856.       On frame buffers that have hardware gamma correction tables, and
  4857.       that are calibrated to display linear samples correctly,
  4858.       display_gamma is 1.0.
  4859.  
  4860.       Many workstations and X terminals and PC displays lack gamma
  4861.       correction lookup tables.  Here, LUT_gamma is always 1.0, so
  4862.       display_gamma is 2.5.
  4863.  
  4864.  
  4865.  
  4866.  
  4867.  
  4868.  
  4869.  
  4870.  
  4871.  
  4872.  
  4873.  
  4874. Boutell, et. al.             Informational                     [Page 87]
  4875.  
  4876. RFC 2083            PNG: Portable Network Graphics            March 1997
  4877.  
  4878.  
  4879.       On the Macintosh, there is a LUT.  By default, it is loaded with a
  4880.       table whose gamma is about 0.72, giving a display_gamma (LUT and
  4881.       CRT combined) of about 1.8.  Some Macs have a "Gamma" control
  4882.       panel that allows gamma to be changed to 1.0, 1.2, 1.4, 1.8, or
  4883.       2.2.  These settings load alternate LUTs that are designed to give
  4884.       a display_gamma that is equal to the label on the selected button.
  4885.       Thus, the "Gamma" control panel setting can be used directly as
  4886.       display_gamma in decoder calculations.
  4887.  
  4888.       On recent SGI systems, there is a hardware gamma-correction table
  4889.       whose contents are controlled by the (privileged) "gamma" program.
  4890.       The gamma of the table is actually the reciprocal of the number
  4891.       that "gamma" prints, and it does not include the CRT gamma. To
  4892.       obtain the display_gamma, you need to find the SGI system gamma
  4893.       (either by looking in a file, or asking the user) and then
  4894.       calculating
  4895.  
  4896.          display_gamma = 2.5 / SGI_system_gamma
  4897.  
  4898.       You will find SGI systems with the system gamma set to 1.0 and 2.2
  4899.       (or higher), but the default when machines are shipped is 1.7.
  4900.  
  4901.    A note about video gamma
  4902.  
  4903.       The original NTSC video standards specified a simple power-law
  4904.       camera transfer function with a gamma of 1/2.2 or 0.45.  This is
  4905.       not possible to implement exactly in analog hardware because the
  4906.       function has infinite slope at x=0, so all cameras deviated to
  4907.       some degree from this ideal.  More recently, a new camera transfer
  4908.       function that is physically realizable has been accepted as a
  4909.       standard [SMPTE-170M].  It is
  4910.  
  4911.          Vout = 4.5 * Vin                    if Vin < 0.018
  4912.          Vout = 1.099 * (Vin^0.45) - 0.099   if Vin >= 0.018
  4913.  
  4914.       where Vin and Vout are measured on a scale of 0 to 1.  Although
  4915.       the exponent remains 0.45, the multiplication and subtraction
  4916.       change the shape of the transfer function, so it is no longer a
  4917.       pure power function.  If you want to perform extremely precise
  4918.       calculations on video signals, you should use the expression above
  4919.       (or its inverse, as required).
  4920.  
  4921.       However, PNG does not provide a way to specify that an image uses
  4922.       this exact transfer function; the gAMA chunk always assumes a pure
  4923.       power-law function. If we plot the two-part transfer function
  4924.       above along with the family of pure power functions, we find that
  4925.       a power function with a gamma of about 0.5 to 0.52 (not 0.45) most
  4926.       closely approximates the transfer function.  Thus, when writing a
  4927.  
  4928.  
  4929.  
  4930. Boutell, et. al.             Informational                     [Page 88]
  4931.  
  4932. RFC 2083            PNG: Portable Network Graphics            March 1997
  4933.  
  4934.  
  4935.       PNG file with data obtained from digitizing the output of a modern
  4936.       video camera, the gAMA chunk should contain 0.5 or 0.52, not 0.45.
  4937.       The remaining difference between the true transfer function and
  4938.       the power function is insignificant for almost all purposes.  (In
  4939.       fact, the alignment errors in most cameras are likely to be larger
  4940.       than the difference between these functions.)  The designers of
  4941.       PNG deemed the simplicity and flexibility of a power-law
  4942.       definition of gAMA to be more important than being able to
  4943.       describe the SMPTE-170M transfer curve exactly.
  4944.  
  4945.       The PAL and SECAM video standards specify a power-law camera
  4946.       transfer function with a gamma of 1/2.8 or 0.36 --- not the 1/2.2
  4947.       of NTSC.  However, this is too low in practice, so real cameras
  4948.       are likely to have their gamma set close to NTSC practice.  Just
  4949.       guessing 0.45 or 0.5 is likely to give you viewable results, but
  4950.       if you want precise values you'll probably have to measure the
  4951.       particular camera.
  4952.  
  4953.    Further reading
  4954.  
  4955.       If you have access to the World Wide Web, read Charles Poynton's
  4956.       excellent "Gamma FAQ" [GAMMA-FAQ] for more information about
  4957.       gamma.
  4958.  
  4959. 14. Appendix: Color Tutorial
  4960.  
  4961.    (This appendix is not part of the formal PNG specification.)
  4962.  
  4963.    About chromaticity
  4964.  
  4965.       The cHRM chunk is used, together with the gAMA chunk, to convey
  4966.       precise color information so that a PNG image can be displayed or
  4967.       printed with better color fidelity than is possible without this
  4968.       information.  The preceding chapters state how this information is
  4969.       encoded in a PNG image.  This tutorial briefly outlines the
  4970.       underlying color theory for those who might not be familiar with
  4971.       it.
  4972.  
  4973.       Note that displaying an image with incorrect gamma will produce
  4974.       much larger color errors than failing to use the chromaticity
  4975.       data.  First be sure the monitor set-up and gamma correction are
  4976.       right, then worry about chromaticity.
  4977.  
  4978.    The problem
  4979.  
  4980.       The color of an object depends not only on the precise spectrum of
  4981.       light emitted or reflected from it, but also on the observer ---
  4982.       their species, what else they can see at the same time, even what
  4983.  
  4984.  
  4985.  
  4986. Boutell, et. al.             Informational                     [Page 89]
  4987.  
  4988. RFC 2083            PNG: Portable Network Graphics            March 1997
  4989.  
  4990.  
  4991.       they have recently looked at!  Furthermore, two very different
  4992.       spectra can produce exactly the same color sensation.  Color is
  4993.       not an objective property of real-world objects; it is a
  4994.       subjective, biological sensation.  However, by making some
  4995.       simplifying assumptions (such as: we are talking about human
  4996.       vision) it is possible to produce a mathematical model of color
  4997.       and thereby obtain good color accuracy.
  4998.  
  4999.    Device-dependent color
  5000.  
  5001.       Display the same RGB data on three different monitors, side by
  5002.       side, and you will get a noticeably different color balance on
  5003.       each display.  This is because each monitor emits a slightly
  5004.       different shade and intensity of red, green, and blue light.  RGB
  5005.       is an example of a device-dependent color model --- the color you
  5006.       get depends on the device.  This also means that a particular
  5007.       color --- represented as say RGB 87, 146, 116 on one monitor ---
  5008.       might have to be specified as RGB 98, 123, 104 on another to
  5009.       produce the same color.
  5010.  
  5011.    Device-independent color
  5012.  
  5013.       A full physical description of a color would require specifying
  5014.       the exact spectral power distribution of the light source.
  5015.       Fortunately, the human eye and brain are not so sensitive as to
  5016.       require exact reproduction of a spectrum.  Mathematical, device-
  5017.       independent color models exist that describe fairly well how a
  5018.       particular color will be seen by humans.  The most important
  5019.       device-independent color model, to which all others can be
  5020.       related, was developed by the International Lighting Committee
  5021.       (CIE, in French) and is called XYZ.
  5022.  
  5023.       In XYZ, X is the sum of a weighted power distribution over the
  5024.       whole visible spectrum.  So are Y and Z, each with different
  5025.       weights.  Thus any arbitrary spectral power distribution is
  5026.       condensed down to just three floating point numbers.  The weights
  5027.       were derived from color matching experiments done on human
  5028.       subjects in the 1920s.  CIE XYZ has been an International Standard
  5029.       since 1931, and it has a number of useful properties:
  5030.  
  5031.           * two colors with the same XYZ values will look the same to
  5032.             humans
  5033.           * two colors with different XYZ values will not look the same
  5034.           * the Y value represents all the brightness information
  5035.             (luminance)
  5036.           * the XYZ color of any object can be objectively measured
  5037.  
  5038.  
  5039.  
  5040.  
  5041.  
  5042. Boutell, et. al.             Informational                     [Page 90]
  5043.  
  5044. RFC 2083            PNG: Portable Network Graphics            March 1997
  5045.  
  5046.  
  5047.       Color models based on XYZ have been used for many years by people
  5048.       who need accurate control of color --- lighting engineers for film
  5049.       and TV, paint and dyestuffs manufacturers, and so on.  They are
  5050.       thus proven in industrial use.  Accurate, device-independent color
  5051.       started to spread from high-end, specialized areas into the
  5052.       mainstream during the late 1980s and early 1990s, and PNG takes
  5053.       notice of that trend.
  5054.  
  5055.    Calibrated, device-dependent color
  5056.  
  5057.       Traditionally, image file formats have used uncalibrated, device-
  5058.       dependent color.  If the precise details of the original display
  5059.       device are known, it becomes possible to convert the device-
  5060.       dependent colors of a particular image to device-independent ones.
  5061.       Making simplifying assumptions, such as working with CRTs (which
  5062.       are much easier than printers), all we need to know are the XYZ
  5063.       values of each primary color and the CRT_gamma.
  5064.  
  5065.       So why does PNG not store images in XYZ instead of RGB?  Well, two
  5066.       reasons.  First, storing images in XYZ would require more bits of
  5067.       precision, which would make the files bigger.  Second, all
  5068.       programs would have to convert the image data before viewing it.
  5069.       Whether calibrated or not, all variants of RGB are close enough
  5070.       that undemanding viewers can get by with simply displaying the
  5071.       data without color correction.  By storing calibrated RGB, PNG
  5072.       retains compatibility with existing programs that expect RGB data,
  5073.       yet provides enough information for conversion to XYZ in
  5074.       applications that need precise colors.  Thus, we get the best of
  5075.       both worlds.
  5076.  
  5077.    What are chromaticity and luminance?
  5078.  
  5079.       Chromaticity is an objective measurement of the color of an
  5080.       object, leaving aside the brightness information.  Chromaticity
  5081.       uses two parameters x and y, which are readily calculated from
  5082.       XYZ:
  5083.  
  5084.          x = X / (X + Y + Z)
  5085.          y = Y / (X + Y + Z)
  5086.  
  5087.       XYZ colors having the same chromaticity values will appear to have
  5088.       the same hue but can vary in absolute brightness.  Notice that x,y
  5089.       are dimensionless ratios, so they have the same values no matter
  5090.       what units we've used for X,Y,Z.
  5091.  
  5092.  
  5093.  
  5094.  
  5095.  
  5096.  
  5097.  
  5098. Boutell, et. al.             Informational                     [Page 91]
  5099.  
  5100. RFC 2083            PNG: Portable Network Graphics            March 1997
  5101.  
  5102.  
  5103.       The Y value of an XYZ color is directly proportional to its
  5104.       absolute brightness and is called the luminance of the color.  We
  5105.       can describe a color either by XYZ coordinates or by chromaticity
  5106.       x,y plus luminance Y.  The XYZ form has the advantage that it is
  5107.       linearly related to (linear, gamma=1.0) RGB color spaces.
  5108.  
  5109.    How are computer monitor colors described?
  5110.  
  5111.       The "white point" of a monitor is the chromaticity x,y of the
  5112.       monitor's nominal white, that is, the color produced when
  5113.       R=G=B=maximum.
  5114.  
  5115.       It's customary to specify monitor colors by giving the
  5116.       chromaticities of the individual phosphors R, G, and B, plus the
  5117.       white point.  The white point allows one to infer the relative
  5118.       brightnesses of the three phosphors, which isn't determined by
  5119.       their chromaticities alone.
  5120.  
  5121.       Note that the absolute brightness of the monitor is not specified.
  5122.       For computer graphics work, we generally don't care very much
  5123.       about absolute brightness levels.  Instead of dealing with
  5124.       absolute XYZ values (in which X,Y,Z are expressed in physical
  5125.       units of radiated power, such as candelas per square meter), it is
  5126.       convenient to work in "relative XYZ" units, where the monitor's
  5127.       nominal white is taken to have a luminance (Y) of 1.0.  Given this
  5128.       assumption, it's simple to compute XYZ coordinates for the
  5129.       monitor's white, red, green, and blue from their chromaticity
  5130.       values.
  5131.  
  5132.       Why does cHRM use x,y rather than XYZ?  Simply because that is how
  5133.       manufacturers print the information in their spec sheets!
  5134.       Usually, the first thing a program will do is convert the cHRM
  5135.       chromaticities into relative XYZ space.
  5136.  
  5137.    What can I do with it?
  5138.  
  5139.       If a PNG file has the gAMA and cHRM chunks, the source_RGB values
  5140.       can be converted to XYZ.  This lets you:
  5141.  
  5142.           * do accurate grayscale conversion (just use the Y component)
  5143.           * convert to RGB for your own monitor (to see the original
  5144.             colors)
  5145.           * print the image in Level 2 PostScript with better color
  5146.             fidelity than a simple RGB to CMYK conversion could provide
  5147.           * calculate an optimal color palette
  5148.           * pass the image data to a color management system
  5149.           * etc.
  5150.  
  5151.  
  5152.  
  5153.  
  5154. Boutell, et. al.             Informational                     [Page 92]
  5155.  
  5156. RFC 2083            PNG: Portable Network Graphics            March 1997
  5157.  
  5158.  
  5159.    How do I convert from source_RGB to XYZ?
  5160.  
  5161.       Make a few simplifying assumptions first, like the monitor really
  5162.       is jet black with no input and the guns don't interfere with one
  5163.       another.  Then, given that you know the CIE XYZ values for each of
  5164.       red, green, and blue for a particular monitor, you put them into a
  5165.       matrix m:
  5166.  
  5167.                  Xr Xg Xb
  5168.             m =  Yr Yg Yb
  5169.                  Zr Zg Zb
  5170.  
  5171.       Here we assume we are working with linear RGB floating point data
  5172.       in the range 0..1.  If the gamma is not 1.0, make it so on the
  5173.       floating point data.  Then convert source_RGB to XYZ by matrix
  5174.       multiplication:
  5175.  
  5176.             X     R
  5177.             Y = m G
  5178.             Z     B
  5179.  
  5180.       In other words, X = Xr*R + Xg*G + Xb*B, and similarly for Y and Z.
  5181.       You can go the other way too:
  5182.  
  5183.             R      X
  5184.             G = im Y
  5185.             B      Z
  5186.  
  5187.       where im is the inverse of the matrix m.
  5188.  
  5189.    What is a gamut?
  5190.  
  5191.       The gamut of a device is the subset of visible colors which that
  5192.       device can display.  (It has nothing to do with gamma.)  The gamut
  5193.       of an RGB device can be visualized as a polyhedron in XYZ space;
  5194.       the vertices correspond to the device's black, blue, red, green,
  5195.       magenta, cyan, yellow and white.
  5196.  
  5197.       Different devices have different gamuts, in other words one device
  5198.       will be able to display certain colors (usually highly saturated
  5199.       ones) that another device cannot.  The gamut of a particular RGB
  5200.       device can be determined from its R, G, and B chromaticities and
  5201.       white point (the same values given in the cHRM chunk).  The gamut
  5202.       of a color printer is more complex and can only be determined by
  5203.       measurement.  However, printer gamuts are typically smaller than
  5204.       monitor gamuts, meaning that there can be many colors in a
  5205.       displayable image that cannot physically be printed.
  5206.  
  5207.  
  5208.  
  5209.  
  5210. Boutell, et. al.             Informational                     [Page 93]
  5211.  
  5212. RFC 2083            PNG: Portable Network Graphics            March 1997
  5213.  
  5214.  
  5215.       Converting image data from one device to another generally results
  5216.       in gamut mismatches --- colors that cannot be represented exactly
  5217.       on the destination device.  The process of making the colors fit,
  5218.       which can range from a simple clip to elaborate nonlinear scaling
  5219.       transformations, is termed gamut mapping.  The aim is to produce a
  5220.       reasonable visual representation of the original image.
  5221.  
  5222.    Further reading
  5223.  
  5224.       References [COLOR-1] through [COLOR-5] provide more detail about
  5225.       color theory.
  5226.  
  5227. 15. Appendix: Sample CRC Code
  5228.  
  5229.    The following sample code represents a practical implementation of
  5230.    the CRC (Cyclic Redundancy Check) employed in PNG chunks.  (See also
  5231.    ISO 3309 [ISO-3309] or ITU-T V.42 [ITU-V42] for a formal
  5232.    specification.)
  5233.  
  5234.    The sample code is in the ANSI C programming language.  Non C users
  5235.    may find it easier to read with these hints:
  5236.  
  5237.    &
  5238.       Bitwise AND operator.
  5239.  
  5240.    ^
  5241.       Bitwise exclusive-OR operator.  (Caution: elsewhere in this
  5242.       document, ^ represents exponentiation.)
  5243.  
  5244.    >>
  5245.       Bitwise right shift operator.  When applied to an unsigned
  5246.       quantity, as here, right shift inserts zeroes at the left.
  5247.  
  5248.    !
  5249.       Logical NOT operator.
  5250.  
  5251.    ++
  5252.       "n++" increments the variable n.
  5253.  
  5254.    0xNNN
  5255.       0x introduces a hexadecimal (base 16) constant.  Suffix L
  5256.       indicates a long value (at least 32 bits).
  5257.  
  5258.       /* Table of CRCs of all 8-bit messages. */
  5259.       unsigned long crc_table[256];
  5260.  
  5261.       /* Flag: has the table been computed? Initially false. */
  5262.       int crc_table_computed = 0;
  5263.  
  5264.  
  5265.  
  5266. Boutell, et. al.             Informational                     [Page 94]
  5267.  
  5268. RFC 2083            PNG: Portable Network Graphics            March 1997
  5269.  
  5270.  
  5271.       /* Make the table for a fast CRC. */
  5272.       void make_crc_table(void)
  5273.       {
  5274.         unsigned long c;
  5275.         int n, k;
  5276.         for (n = 0; n < 256; n++) {
  5277.           c = (unsigned long) n;
  5278.           for (k = 0; k < 8; k++) {
  5279.             if (c & 1)
  5280.               c = 0xedb88320L ^ (c >> 1);
  5281.             else
  5282.               c = c >> 1;
  5283.           }
  5284.           crc_table[n] = c;
  5285.         }
  5286.         crc_table_computed = 1;
  5287.       }
  5288.  
  5289.       /* Update a running CRC with the bytes buf[0..len-1]--the CRC
  5290.          should be initialized to all 1's, and the transmitted value
  5291.          is the 1's complement of the final running CRC (see the
  5292.          crc() routine below)). */
  5293.  
  5294.       unsigned long update_crc(unsigned long crc, unsigned char *buf,
  5295.                                int len)
  5296.       {
  5297.         unsigned long c = crc;
  5298.         int n;
  5299.  
  5300.         if (!crc_table_computed)
  5301.           make_crc_table();
  5302.         for (n = 0; n < len; n++) {
  5303.           c = crc_table[(c ^ buf[n]) & 0xff] ^ (c >> 8);
  5304.         }
  5305.         return c;
  5306.       }
  5307.  
  5308.       /* Return the CRC of the bytes buf[0..len-1]. */
  5309.       unsigned long crc(unsigned char *buf, int len)
  5310.       {
  5311.         return update_crc(0xffffffffL, buf, len) ^ 0xffffffffL;
  5312.       }
  5313.  
  5314.  
  5315.  
  5316.  
  5317.  
  5318.  
  5319.  
  5320.  
  5321.  
  5322. Boutell, et. al.             Informational                     [Page 95]
  5323.  
  5324. RFC 2083            PNG: Portable Network Graphics            March 1997
  5325.  
  5326.  
  5327. 16. Appendix: Online Resources
  5328.  
  5329.    (This appendix is not part of the formal PNG specification.)
  5330.  
  5331.    This appendix gives the locations of some Internet resources for PNG
  5332.    software developers.  By the nature of the Internet, the list is
  5333.    incomplete and subject to change.
  5334.  
  5335.    Archive sites
  5336.  
  5337.       The latest released versions of this document and related
  5338.       information can always be found at the PNG FTP archive site,
  5339.       ftp://ftp.uu.net/graphics/png/.  The PNG specification is
  5340.       available in several formats, including HTML, plain text, and
  5341.       PostScript.
  5342.  
  5343.    Reference implementation and test images
  5344.  
  5345.       A reference implementation in portable C is available from the PNG
  5346.       FTP archive site, ftp://ftp.uu.net/graphics/png/src/.  The
  5347.       reference implementation is freely usable in all applications,
  5348.       including commercial applications.
  5349.  
  5350.       Test images are available from
  5351.       ftp://ftp.uu.net/graphics/png/images/.
  5352.  
  5353.    Electronic mail
  5354.  
  5355.       The maintainers of the PNG specification can be contacted by e-
  5356.       mail at png-info@uunet.uu.net or at png-group@w3.org.
  5357.  
  5358.    PNG home page
  5359.  
  5360.       There is a World Wide Web home page for PNG at
  5361.       http://quest.jpl.nasa.gov/PNG/.  This page is a central location
  5362.       for current information about PNG and PNG-related tools.
  5363.  
  5364. 17. Appendix: Revision History
  5365.  
  5366.    (This appendix is not part of the formal PNG specification.)
  5367.  
  5368.    The PNG format has been frozen since the Ninth Draft of March 7,
  5369.    1995, and all future changes are intended to be backwards compatible.
  5370.    The revisions since the Ninth Draft are simply clarifications,
  5371.    improvements in presentation, and additions of supporting material.
  5372.  
  5373.    On 1 October 1996, the PNG specification was approved as a W3C (World
  5374.    Wide Web Consortium) Recommendation.
  5375.  
  5376.  
  5377.  
  5378. Boutell, et. al.             Informational                     [Page 96]
  5379.  
  5380. RFC 2083            PNG: Portable Network Graphics            March 1997
  5381.  
  5382.  
  5383.    Changes since the Tenth Draft of 5 May, 1995
  5384.  
  5385.           * Clarified meaning of a suggested-palette PLTE chunk in a
  5386.             truecolor image that uses transparency
  5387.           * Clarified exact semantics of sBIT and allowed sample depth
  5388.             scaling procedures
  5389.           * Clarified status of spaces in tEXt chunk keywords
  5390.           * Distinguished private and public extension values in type
  5391.             and method fields
  5392.           * Added a "Creation Time" tEXt keyword
  5393.           * Macintosh representation of PNG specified
  5394.           * Added discussion of security issues
  5395.           * Added more extensive discussion of gamma and chromaticity
  5396.             handling, including tutorial appendixes
  5397.           * Clarified terminology, notably sample depth vs. bit depth
  5398.           * Added a glossary
  5399.           * Editing and reformatting
  5400.  
  5401. 18. References
  5402.  
  5403.    [COLOR-1]
  5404.       Hall, Roy, Illumination and Color in Computer Generated Imagery.
  5405.       Springer-Verlag, New York, 1989.  ISBN 0-387-96774-5.
  5406.  
  5407.    [COLOR-2]
  5408.       Kasson, J., and W. Plouffe, "An Analysis of Selected Computer
  5409.       Interchange Color Spaces", ACM Transactions on Graphics, vol 11 no
  5410.       4 (1992), pp 373-405.
  5411.  
  5412.    [COLOR-3]
  5413.       Lilley, C., F. Lin, W.T. Hewitt, and T.L.J. Howard, Colour in
  5414.       Computer Graphics. CVCP, Sheffield, 1993.  ISBN 1-85889-022-5.
  5415.       Also available from
  5416.       <URL:http://info.mcc.ac.uk/CGU/ITTI/Col/colour_announce.html>
  5417.  
  5418.    [COLOR-4]
  5419.       Stone, M.C., W.B. Cowan, and J.C. Beatty, "Color gamut mapping and
  5420.       the printing of digital images", ACM Transactions on Graphics, vol
  5421.       7 no 3 (1988), pp 249-292.
  5422.  
  5423.    [COLOR-5]
  5424.       Travis, David, Effective Color Displays --- Theory and Practice.
  5425.       Academic Press, London, 1991.  ISBN 0-12-697690-2.
  5426.  
  5427.    [GAMMA-FAQ]
  5428.       Poynton, C., "Gamma FAQ".
  5429.       <URL:http://www.inforamp.net/%7Epoynton/Poynton-colour.html>
  5430.  
  5431.  
  5432.  
  5433.  
  5434. Boutell, et. al.             Informational                     [Page 97]
  5435.  
  5436. RFC 2083            PNG: Portable Network Graphics            March 1997
  5437.  
  5438.  
  5439.    [ISO-3309]
  5440.       International Organization for Standardization, "Information
  5441.       Processing Systems --- Data Communication High-Level Data Link
  5442.       Control Procedure --- Frame Structure", IS 3309, October 1984, 3rd
  5443.       Edition.
  5444.  
  5445.    [ISO-8859]
  5446.       International Organization for Standardization, "Information
  5447.       Processing --- 8-bit Single-Byte Coded Graphic Character Sets ---
  5448.       Part 1: Latin Alphabet No. 1", IS 8859-1, 1987.
  5449.       Also see sample files at
  5450.       ftp://ftp.uu.net/graphics/png/documents/iso_8859-1.*
  5451.  
  5452.    [ITU-BT709]
  5453.       International Telecommunications Union, "Basic Parameter Values
  5454.       for the HDTV Standard for the Studio and for International
  5455.       Programme Exchange", ITU-R Recommendation BT.709 (formerly CCIR
  5456.       Rec. 709), 1990.
  5457.  
  5458.    [ITU-V42]
  5459.       International Telecommunications Union, "Error-correcting
  5460.       Procedures for DCEs Using Asynchronous-to-Synchronous Conversion",
  5461.       ITU-T Recommendation V.42, 1994, Rev. 1.
  5462.  
  5463.    [PAETH]
  5464.       Paeth, A.W., "Image File Compression Made Easy", in Graphics Gems
  5465.       II, James Arvo, editor.  Academic Press, San Diego, 1991.  ISBN
  5466.       0-12-064480-0.
  5467.  
  5468.    [POSTSCRIPT]
  5469.       Adobe Systems Incorporated, PostScript Language Reference Manual,
  5470.       2nd edition. Addison-Wesley, Reading, 1990.  ISBN 0-201-18127-4.
  5471.  
  5472.    [PNG-EXTENSIONS]
  5473.       PNG Group, "PNG Special-Purpose Public Chunks".  Available in
  5474.       several formats from
  5475.       ftp://ftp.uu.net/graphics/png/documents/pngextensions.*
  5476.  
  5477.    [RFC-1123]
  5478.       Braden, R., Editor, "Requirements for Internet Hosts ---
  5479.       Application and Support", STD 3, RFC 1123, USC/Information
  5480.       Sciences Institute, October 1989.
  5481.       <URL:ftp://ds.internic.net/rfc/rfc1123.txt>
  5482.  
  5483.  
  5484.  
  5485.  
  5486.  
  5487.  
  5488.  
  5489.  
  5490. Boutell, et. al.             Informational                     [Page 98]
  5491.  
  5492. RFC 2083            PNG: Portable Network Graphics            March 1997
  5493.  
  5494.  
  5495.    [RFC-2045]
  5496.       Freed, N., and N. Borenstein, "Multipurpose Internet Mail
  5497.       Extensions (MIME) Part One: Format of Internet Message Bodies",
  5498.       RFC 2045, Innosoft, First Virtual, November 1996.
  5499.       <URL:ftp://ds.internic.net/rfc/rfc2045.txt>
  5500.  
  5501.    [RFC-2048]
  5502.       Freed, N., Klensin, J., and J. Postel, "Multipurpose Internet Mail
  5503.       Extensions (MIME) Part Four: Registration Procedures", RFC 2048,
  5504.       Innosoft, MCI, USC/Information Sciences Institute, November 1996.
  5505.       <URL:ftp://ds.internic.net/rfc/rfc2048.txt>
  5506.  
  5507.    [RFC-1950]
  5508.       Deutsch, P. and J-L. Gailly, "ZLIB Compressed Data Format
  5509.       Specification version 3.3", RFC 1950, Aladdin Enterprises, May
  5510.       1996.
  5511.       <URL:ftp://ds.internic.net/rfc/rfc1950.txt>
  5512.  
  5513.    [RFC-1951]
  5514.       Deutsch, P., "DEFLATE Compressed Data Format Specification version
  5515.       1.3", RFC 1951, Aladdin Enterprises, May 1996.
  5516.       <URL:ftp://ds.internic.net/rfc/rfc1951.txt>
  5517.  
  5518.    [SMPTE-170M]
  5519.       Society of Motion Picture and Television Engineers, "Television
  5520.       --- Composite Analog Video Signal --- NTSC for Studio
  5521.       Applications", SMPTE-170M, 1994.
  5522.  
  5523.  
  5524.  
  5525.  
  5526.  
  5527.  
  5528.  
  5529.  
  5530.  
  5531.  
  5532.  
  5533.  
  5534.  
  5535.  
  5536.  
  5537.  
  5538.  
  5539.  
  5540.  
  5541.  
  5542.  
  5543.  
  5544.  
  5545.  
  5546. Boutell, et. al.             Informational                     [Page 99]
  5547.  
  5548. RFC 2083            PNG: Portable Network Graphics            March 1997
  5549.  
  5550.  
  5551. 19. Credits
  5552.  
  5553.    Editor
  5554.  
  5555.       Thomas Boutell, boutell@boutell.com
  5556.  
  5557.    Contributing Editor
  5558.  
  5559.       Tom Lane, tgl@sss.pgh.pa.us
  5560.  
  5561.    Authors
  5562.  
  5563.       Authors' names are presented in alphabetical order.
  5564.  
  5565.           * Mark Adler, madler@alumni.caltech.edu
  5566.           * Thomas Boutell, boutell@boutell.com
  5567.           * Christian Brunschen, cb@df.lth.se
  5568.           * Adam M. Costello, amc@cs.berkeley.edu
  5569.           * Lee Daniel Crocker, lee@piclab.com
  5570.           * Andreas Dilger, adilger@enel.ucalgary.ca
  5571.           * Oliver Fromme, fromme@rz.tu-clausthal.de
  5572.           * Jean-loup Gailly, gzip@prep.ai.mit.edu
  5573.           * Chris Herborth, chrish@qnx.com
  5574.           * Alex Jakulin, Aleks.Jakulin@snet.fri.uni-lj.si
  5575.           * Neal Kettler, kettler@cs.colostate.edu
  5576.           * Tom Lane, tgl@sss.pgh.pa.us
  5577.           * Alexander Lehmann, alex@hal.rhein-main.de
  5578.           * Chris Lilley, chris@w3.org
  5579.           * Dave Martindale, davem@cs.ubc.ca
  5580.           * Owen Mortensen, 104707.650@compuserve.com
  5581.           * Keith S. Pickens, ksp@swri.edu
  5582.           * Robert P. Poole, lionboy@primenet.com
  5583.           * Glenn Randers-Pehrson, glennrp@arl.mil or
  5584.             randeg@alumni.rpi.edu
  5585.           * Greg Roelofs, newt@pobox.com
  5586.           * Willem van Schaik, willem@gintic.gov.sg
  5587.           * Guy Schalnat
  5588.           * Paul Schmidt, pschmidt@photodex.com
  5589.           * Tim Wegner, 71320.675@compuserve.com
  5590.           * Jeremy Wohl, jeremyw@anders.com
  5591.  
  5592.       The authors wish to acknowledge the contributions of the Portable
  5593.       Network Graphics mailing list, the readers of comp.graphics, and
  5594.       the members of the World Wide Web Consortium (W3C).
  5595.  
  5596.  
  5597.  
  5598.  
  5599.  
  5600.  
  5601.  
  5602. Boutell, et. al.             Informational                    [Page 100]
  5603.  
  5604. RFC 2083            PNG: Portable Network Graphics            March 1997
  5605.  
  5606.  
  5607.       The Adam7 interlacing scheme is not patented and it is not the
  5608.       intention of the originator of that scheme to patent it. The
  5609.       scheme may be freely used by all PNG implementations. The name
  5610.       "Adam7" may be freely used to describe interlace method 1 of the
  5611.       PNG specification.
  5612.  
  5613.    Trademarks
  5614.  
  5615.       GIF is a service mark of CompuServe Incorporated.  IBM PC is a
  5616.       trademark of International Business Machines Corporation.
  5617.       Macintosh is a trademark of Apple Computer, Inc.  Microsoft and
  5618.       MS-DOS are trademarks of Microsoft Corporation.  PhotoCD is a
  5619.       trademark of Eastman Kodak Company.  PostScript and TIFF are
  5620.       trademarks of Adobe Systems Incorporated.  SGI is a trademark of
  5621.       Silicon Graphics, Inc.  X Window System is a trademark of the
  5622.       Massachusetts Institute of Technology.
  5623.  
  5624. COPYRIGHT NOTICE
  5625.  
  5626.    Copyright (c) 1996 by: Massachusetts Institute of Technology (MIT)
  5627.  
  5628.    This W3C specification is being provided by the copyright holders
  5629.    under the following license. By obtaining, using and/or copying this
  5630.    specification, you agree that you have read, understood, and will
  5631.    comply with the following terms and conditions:
  5632.  
  5633.    Permission to use, copy, and distribute this specification for any
  5634.    purpose and without fee or royalty is hereby granted, provided that
  5635.    the full text of this NOTICE appears on ALL copies of the
  5636.    specification or portions thereof, including modifications, that you
  5637.    make.
  5638.  
  5639.    THIS SPECIFICATION IS PROVIDED "AS IS," AND COPYRIGHT HOLDERS MAKE NO
  5640.    REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED.  BY WAY OF
  5641.    EXAMPLE, BUT NOT LIMITATION, COPYRIGHT HOLDERS MAKE NO
  5642.    REPRESENTATIONS OR WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY
  5643.    PARTICULAR PURPOSE OR THAT THE USE OF THE SPECIFICATION WILL NOT
  5644.    INFRINGE ANY THIRD PARTY PATENTS, COPYRIGHTS, TRADEMARKS OR OTHER
  5645.    RIGHTS.  COPYRIGHT HOLDERS WILL BEAR NO LIABILITY FOR ANY USE OF THIS
  5646.    SPECIFICATION.
  5647.  
  5648.  
  5649.  
  5650.  
  5651.  
  5652.  
  5653.  
  5654.  
  5655.  
  5656.  
  5657.  
  5658. Boutell, et. al.             Informational                    [Page 101]
  5659.  
  5660. RFC 2083            PNG: Portable Network Graphics            March 1997
  5661.  
  5662.  
  5663.    The name and trademarks of copyright holders may NOT be used in
  5664.    advertising or publicity pertaining to the specification without
  5665.    specific, written prior permission.  Title to copyright in this
  5666.    specification and any associated documentation will at all times
  5667.    remain with copyright holders.
  5668.  
  5669. Security Considerations
  5670.  
  5671.    Security issues are discussed in Security considerations (Section
  5672.    8.5).
  5673.  
  5674. Author's Address
  5675.  
  5676.    Thomas Boutell
  5677.    PO Box 20837
  5678.    Seattle, WA  98102
  5679.  
  5680.    Phone: (206) 329-4969
  5681.    EMail: boutell@boutell.com
  5682.  
  5683.  
  5684.  
  5685.  
  5686.  
  5687.  
  5688.  
  5689.  
  5690.  
  5691.  
  5692.  
  5693.  
  5694.  
  5695.  
  5696.  
  5697.  
  5698.  
  5699.  
  5700.  
  5701.  
  5702.  
  5703.  
  5704.  
  5705.  
  5706.  
  5707.  
  5708.  
  5709.  
  5710.  
  5711.  
  5712.  
  5713.  
  5714. Boutell, et. al.             Informational                    [Page 102]
  5715.  
  5716.