home *** CD-ROM | disk | FTP | other *** search
/ Compressed Image File Formats / CompressedImageFileFormatsJohnMiano.iso / pc / Specs / pngext-1.1.1-pdg.txt < prev    next >
Encoding:
Text File  |  1999-02-12  |  52.9 KB  |  1,351 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.          Extensions to the PNG 1.1 Specification, Version 1.1.1
  7.  
  8. The latest versions of this document, the PNG specification, and related
  9. information can always be found at the PNG FTP archive site,
  10. ftp://ftp.uu.net/graphics/png/.  The maintainers of the PNG
  11. specification can be contacted by e-mail at png-info@uunet.uu.net.
  12.  
  13. Abstract
  14.  
  15.    This document is an extension to the Portable Network Graphics (PNG)
  16.    specification, version 1.1.  It describes additional public chunk
  17.    types and contains additional information for use in PNG images.
  18.  
  19.    This document, together with the PNG specification, contains the
  20.    entire list of registered "public" PNG chunks.  The additional
  21.    registered chunks appearing in this document are the oFFs, pCAL,
  22.    sCAL, gIFg, gIFs, fRAc, and iTXt chunks, plus the deprecated gIFt
  23.    chunk.  Additional chunk types may be proposed for inclusion in this
  24.    list by contacting the PNG specification maintainers at
  25.    png-info@uunet.uu.net.  Chunks described here are expected to be less
  26.    widely supported than those defined in the basic specification.
  27.    However, application authors are encouraged to use these chunk types
  28.    whenever appropriate for their applications.
  29.  
  30.    This document also describes data representations that do not occur
  31.    in the core PNG format, but are used in one or more special-purpose
  32.    chunks.  New chunks should use these representations whenever
  33.    applicable, in order to maximize portability and simplify decoders.
  34.  
  35. Table of Contents
  36.  
  37.    1. Data Representation 
  38.       1.1. Integer values 
  39.       1.2. Floating-point values 
  40.    2. Summary of Special-Purpose Chunks 
  41.    3. Chunk Descriptions 
  42.       3.1. oFFs Image offset 
  43.       3.2. pCAL Calibration of pixel values 
  44.       3.3. sCAL Physical scale of image subject 
  45.       3.4. gIFg GIF Graphic Control Extension 
  46.       3.5. gIFx GIF Application Extension 
  47.       3.6. iTXt International Textual Data 
  48.    4. Chunks Not Described Here 
  49.       4.1. fRAc Fractal image parameters 
  50.    5. Text Chunk Keywords 
  51.    6. Deprecated Chunks 
  52.       6.1. gIFt GIF Plain Text Extension 
  53.    7. Security Considerations 
  54.    8. Appendix: Sample code 
  55.       8.1. pCAL 
  56.       8.2. Fixed-point gamma correction 
  57.    9. Appendix: Rationale 
  58.       9.1. iTXt 
  59.       9.2. pCAL 
  60.    10. Appendix: Revision History 
  61.    11. References 
  62.    12. Credits 
  63.  
  64. 1. Data Representation
  65.  
  66.    1.1. Integer values
  67.  
  68.       Refer to Section 2.1 of the PNG specification for the format and
  69.       range of integer values.
  70.  
  71.    1.2. Floating-point values
  72.  
  73.       The core of PNG does not use floating-point numbers anywhere; it
  74.       uses integers or, where applicable, fixed-point fractional values.
  75.       However, special-purpose chunks may need to represent values that
  76.       do not fit comfortably in fixed-point notation.  The textual
  77.       floating-point notation defined here is recommended for use in all
  78.       such cases.  This representation is simple, has no a priori limits
  79.       on range or precision, and is portable across all machines.
  80.  
  81.       A floating-point value in this notation is represented by an ASCII
  82.       text string in a standardized decimal floating-point format.  The
  83.       string is variable-length and must be terminated by a null (zero)
  84.       character unless it is the last item in its chunk.  The string
  85.       consists of an optional sign ("+" or "-"), an integer part, a
  86.       fraction part beginning with a decimal point ("."), and an
  87.       exponent part beginning with an "E" or "e" and optional sign.  The
  88.       integer, fraction, and exponent parts each contain one or more
  89.       digits (ASCII "0" to "9").  Either the integer part or the
  90.       fraction part, but not both, may be omitted.  A decimal point is
  91.       allowed, but not required, if there is no fraction part.  The
  92.       exponent part may be omitted.  No spaces or any other character
  93.       besides those specified may appear.
  94.  
  95.       Note in particular that C-language "F" and "L" suffixes are not
  96.       allowed, the string "." is not allowed as a shorthand for 0 as in
  97.       some other programming languages, and no commas or underscores are
  98.       allowed.  This format ought to be easily readable in all
  99.       programming environments.
  100.  
  101. 2. Summary of Special-Purpose Chunks
  102.  
  103.    This table summarizes some properties of the chunks described in this
  104.    document.
  105.  
  106.       Name  Multiple  Ordering constraints
  107.               OK?
  108.  
  109.       oFFs    No      Before IDAT
  110.       pCAL    No      Before IDAT
  111.       sCAL    No      Before IDAT
  112.       gIFg    Yes     None
  113.       gIFt    Yes     None (this chunk is deprecated)
  114.       gIFx    Yes     None
  115.       fRAc    Yes     None
  116.       iTXt    Yes     None
  117.  
  118. 3. Chunk Descriptions
  119.  
  120.    3.1. oFFs Image offset
  121.  
  122.       The oFFs chunk gives the position on a printed page at which the
  123.       image should be output when printed alone.  It can also be used to
  124.       define the image's location with respect to a larger screen or
  125.       other application-specific coordinate system.
  126.  
  127.       The oFFs chunk contains:
  128.  
  129.          X position:     4 bytes (signed integer)
  130.          Y position:     4 bytes (signed integer)
  131.          Unit specifier: 1 byte
  132.  
  133.       Both position values are signed.  The following values are legal
  134.       for the unit specifier:
  135.  
  136.          0: unit is the pixel (true dimensions unspecified)
  137.          1: unit is the micrometer
  138.  
  139.       Conversion note: one inch is equal to exactly 25400 micrometers.
  140.       A micrometer (also called a micron) is 10^-6 meter.
  141.  
  142.       The X position is measured rightwards from the left edge of the
  143.       page to the left edge of the image; the Y position is measured
  144.       downwards from the top edge of the page to the top edge of the
  145.       image.  Note that negative values are permitted, and denote
  146.       displacement in the opposite directions.  Although oFFs can
  147.       specify an image placement that is partially or wholly outside the
  148.       page boundaries, the result of such placement is application-
  149.       dependent.
  150.  
  151.       If present, this chunk must precede the first IDAT chunk.
  152.  
  153.    3.2. pCAL Calibration of pixel values
  154.  
  155.       When a PNG file is being used to store physical data other than
  156.       color values, such as a two-dimensional temperature field, the
  157.       pCAL chunk can be used to record the relationship (mapping)
  158.       between stored pixel samples, original samples, and actual
  159.       physical values.  The pCAL data might be used to construct a
  160.       reference color bar beside the image, or to extract the original
  161.       physical data values from the file.  It is not expected to affect
  162.       the way the pixels are displayed.  Another method should be used
  163.       if the encoder wants the decoder to modify the sample values for
  164.       display purposes.
  165.  
  166.       The pCAL chunk contains:
  167.  
  168.          Calibration name:     1-79 bytes (character string)
  169.          Null separator:       1 byte
  170.          Original zero (x0):   4 bytes (signed integer)
  171.          Original max  (x1):   4 bytes (signed integer)
  172.          Equation type:        1 byte
  173.          Number of parameters: 1 byte
  174.          Unit name:            0 or more bytes (character string)
  175.          Null separator:       1 byte
  176.          Parameter 0 (p0):     1 or more bytes (ASCII floating-point)
  177.          Null separator:       1 byte
  178.          Parameter 1 (p1):     1 or more bytes (ASCII floating-point)
  179.          ...etc...
  180.  
  181.       There is no null separator after the final parameter (or after the
  182.       unit name, if there are zero parameters).  The number of
  183.       parameters field must agree with the actual number of parameters
  184.       present in the chunk, and must be correct for the specified
  185.       equation type (see below).
  186.  
  187.       The calibration name can be any convenient name for referring to
  188.       the mapping, and is subject to the same restrictions as a tEXt
  189.       keyword: it must contain only printable Latin-1 [ISO/IEC-8859-1]
  190.       characters (33-126 and 161-255) and spaces (32), but no leading,
  191.       trailing, or consecutive spaces.  The calibration name can permit
  192.       applications or people to choose the appropriate pCAL chunk when
  193.       more than one is present (this could occur in a multiple-image
  194.       file, but not in a PNG file).  For example, a calibration name of
  195.       "SI" or "English" could be used to identify the system of units in
  196.       the pCAL chunk as well as in other chunk types, to permit a
  197.       decoder to select an appropriate set of chunks based on their
  198.       names.
  199.  
  200.       The pCAL chunk defines two mappings:
  201.  
  202.          1. A mapping from the stored samples, which are unsigned
  203.             integers in the range 0..max, where max=(2^bitdepth)-1, to
  204.             the original samples, which are signed integers.  The x0 and
  205.             x1 fields, together with the bit depth for the image, define
  206.             this mapping.
  207.  
  208.          2. A mapping from the original samples to the physical values,
  209.             which are usually real numbers with units.  This mapping is
  210.             defined by x0, x1, the equation type, parameters, and unit
  211.             name.
  212.  
  213.       The mapping between the stored samples and the original samples is
  214.       given by the following equations:
  215.  
  216.          original_sample =
  217.             (stored_sample * (x1-x0) + max/2) / max + x0
  218.  
  219.          stored_sample =
  220.              ((original_sample - x0) * max + (x1-x0)/2) / (x1-x0)
  221.              clipped to the range 0..max
  222.  
  223.       In these equations, "/" means integer division that rounds toward
  224.       negative infinity, so n/d = integer(floor(real(a)/real(b)))).
  225.       Note that this is the same as the "/" operator in the C
  226.       programming language when n and d are nonnegative, but not
  227.       necessarily when n or d is negative.
  228.  
  229.       Notice that x0 and x1 are the original samples that correspond to
  230.       the stored samples 0 and max, respectively.  Encoders will usually
  231.       set x0=0 and x1=max to indicate that the stored samples are equal
  232.       to the original samples.  Note that x0 is not constrained to be
  233.       less than x1, and neither is constrained to be positive, but they
  234.       must be different from each other.
  235.  
  236.       This mapping is lossless and reversible when abs(x1-x0) <= max and
  237.       the original sample is in the range x0..x1.  If abs(x1-x0) > max
  238.       then there can be no lossless reversible mapping, but the
  239.       functions provide the best integer approximations to floating-
  240.       point affine transformations.
  241.  
  242.       The mapping between the original samples and the physical values
  243.       is given by one of several equations, depending on the equation
  244.       type, which may have the following values:
  245.  
  246.          0: Linear mapping
  247.          1: Base-e exponential mapping
  248.          2: Arbitrary-base exponential mapping
  249.          3: Hyperbolic mapping
  250.  
  251.       For equation type 0:
  252.  
  253.          physical_value = p0 + p1 * original_sample / (x1-x0)
  254.  
  255.       For equation type 1:
  256.  
  257.          physical_value =
  258.             p0 + p1 * exp(p2 * original_sample / (x1-x0))
  259.  
  260.       For equation type 2:
  261.  
  262.          physical_value =
  263.             p0 + p1 * pow(p2, (original_sample / (x1-x0)))
  264.  
  265.       For equation type 3:
  266.  
  267.          physical_value =
  268.             p0 + p1 * sinh(p2 * (original_sample - p3) / (x1-x0))
  269.  
  270.       For these physical value equations, "/" means floating-point
  271.       division.
  272.  
  273.       The function exp(x) is e raised to the power of x, where e is the
  274.       base of the natural logarithms, approximately 2.71828182846.  The
  275.       exponential function exp() is the inverse the natural logarithm
  276.       function ln().
  277.  
  278.       The function pow(x,y) is x raised to the power of y.
  279.  
  280.          pow(x,y) = exp(y * ln(x))
  281.  
  282.       The function sinh(x) is the hyperbolic sine of x.
  283.  
  284.          sinh(x) = 0.5 * (exp(x) - exp(-x))
  285.  
  286.       The units for the physical values are given by the unit name,
  287.       which may contain any number of printable Latin-1 characters, with
  288.       no limitation on the number and position of blanks.  For example,
  289.       "K", "population density", "MPa".  A zero-length string can be
  290.       used for dimensionless data.
  291.  
  292.       For color types 0 (gray) and 4 (gray-alpha), the mappings apply to
  293.       the gray sample values (but not to the alpha sample).  For color
  294.       types 2 (RGB), 3 (indexed RGB), and 6 (RGBA), the mappings apply
  295.       independently to each of the red, green, and blue sample values
  296.       (but not the alpha sample).  In the case of color type 3 (indexed
  297.       RGB), the mapping refers to the RGB samples and not to the index
  298.       values.
  299.  
  300.       Linear data can be expressed with equation type 0.
  301.  
  302.       Pure logarithmic data can be expressed with either equation type 1
  303.       or 2:
  304.  
  305.          Equation type 1       Equation type 2
  306.          x0 = 0                x0 = 0
  307.          x1 = max              x1 = max
  308.          p0 = 0                p0 = 0
  309.          p1 = bottom           p1 = bottom
  310.          p2 = ln(top/bottom)   p2 = top/bottom
  311.  
  312.       Equation types 1 and 2 are functionally equivalent; both are
  313.       defined because authors may find one or the other more convenient.
  314.  
  315.       Using equation type 3, floating-point data can be reduced (with
  316.       loss) to a set of integer samples such that the resolution of the
  317.       stored data is roughly proportional to its magnitude.  For
  318.       example, floating-point data ranging from -10^31 to 10^31 (the
  319.       usual range of 32-bit floating-point numbers) can be represented
  320.       with:
  321.  
  322.          Equation type 3
  323.          x0 = 0
  324.          x1 = 65535
  325.          p0 = 0.0
  326.          p1 = 1.0e-30
  327.          p2 = 280.0
  328.          p3 = 32767.0
  329.  
  330.       The resolution near zero is about 10^-33, while the resolution
  331.       near 10^31 or -10^31 is about 10^28.  Everywhere the resolution is
  332.       about 0.4 percent of the magnitude.
  333.  
  334.       Note that those floating-point paramaters could be stored in the
  335.       chunk more compactly as follows:
  336.  
  337.          p0 = 0
  338.          p1 = 1e-30
  339.          p2 = 280
  340.          p3 = 32767
  341.  
  342.       Applications should use double precision arithmetic (or take other
  343.       precautions) while performing the mappings for equation types 1,
  344.       2, and 3, to prevent overflow of intermediate results when p1 is
  345.       small and the exp(), pow(), or sinh() function is large.
  346.  
  347.       If present, the pCAL chunk must appear before the first IDAT
  348.       chunk.  Only one instance of the pCAL chunk is permitted in a PNG
  349.       datastream.
  350.  
  351.    3.3. sCAL Physical scale of image subject
  352.  
  353.       While the pHYs chunk is used to record the physical size of the
  354.       image itself as it was scanned or as it should be printed, certain
  355.       images (such as maps, photomicrographs, astronomical surveys,
  356.       floor plans, and others) may benefit from knowing the actual
  357.       physical dimensions of the image's subject for remote measurement
  358.       and other purposes.  The sCAL chunk serves this need.  It
  359.       contains:
  360.  
  361.          Unit specifier: 1 byte
  362.          Pixel width:    1 or more bytes (ASCII floating-point)
  363.          Null separator: 1 byte
  364.          Pixel height:   1 or more bytes (ASCII floating-point)
  365.  
  366.       The following values are legal for the unit specifier:
  367.  
  368.          1: unit is the meter
  369.          2: unit is the radian
  370.  
  371.       Following the unit specifier are two ASCII strings.  The first
  372.       string defines the physical width represented by one image pixel;
  373.       the second string defines the physical height represented by one
  374.       pixel.  The two strings are separated by a zero byte (null
  375.       character).  As in the tEXt chunk, there is no trailing zero byte
  376.       for the final string.  Each of these strings contains a floating-
  377.       point constant in the format specified above (Floating-point
  378.       values, Section 1.2).  Both values are required to be greater than
  379.       zero.
  380.  
  381.       If present, this chunk must precede the first IDAT chunk.
  382.  
  383.    3.4. gIFg GIF Graphic Control Extension
  384.  
  385.       The gIFg chunk is provided for backward compatibility with the
  386.       GIF89a Graphic Control Extension.  It contains:
  387.  
  388.          Disposal Method: 1 byte
  389.          User Input Flag: 1 byte
  390.          Delay Time:      2 bytes (byte order converted from GIF)
  391.  
  392.       The Disposal Method indicates the way in which the graphic is to
  393.       be treated after being displayed.  The User Input Flag indicates
  394.       whether user input is required before continuing.  The Delay Time
  395.       specifies the number of hundredths (1/100) of a second to delay
  396.       before continuing with the processing of the datastream.  Note
  397.       that this field is to be byte-order-converted.
  398.  
  399.       The "Transparent Color Flag" and "Transparent Color Index" fields
  400.       found in the GIF89a Graphic Control Extension are omitted from
  401.       gIFg.  These fields should be converted using the transparency
  402.       features of basic PNG.
  403.  
  404.       The GIF specification allows at most one Graphic Control Extension
  405.       to preceed each graphic rendering block.  Because each PNG file
  406.       holds only one image, it is expected that gIFg will appear at most
  407.       once, before IDAT, but there is no strict requirement.
  408.  
  409.    3.5. gIFx GIF Application Extension
  410.  
  411.       The gIFx chunk is provided for backward compatibility with the
  412.       GIF89a Application Extension.  The Application Extension contains
  413.       application-specific information.  This chunk contains:
  414.  
  415.          Application Identifier: 8 bytes
  416.          Authentication Code:    3 bytes
  417.          Application Data:       n bytes
  418.  
  419.       The Application Identifier is a sequence of eight printable ASCII
  420.       characters used to identify the application creating the
  421.       Application Extension.  The Authentication Code is three
  422.       additional bytes that the application may use to further validate
  423.       the Application Extension.  The remainder of the chunk is
  424.       application-specific data whose content is not defined by the GIF
  425.       specification.
  426.  
  427.       Note that GIF-to-PNG converters should not attempt to perform byte
  428.       reordering on the contents of the Application Extension.  The data
  429.       is simply transcribed without any processing except for de-
  430.       blocking GIF sub-blocks.
  431.  
  432.       Applications that formerly used GIF Application Extensions may
  433.       define special-purpose PNG chunks to replace their application
  434.       extensions.  If a GIF-to-PNG converter recognizes the Application
  435.       Identifier and is aware of a corresponding PNG chunk, it may
  436.       choose to convert the Application Extension into that PNG chunk
  437.       type rather than using gIFx.
  438.  
  439.    3.6. iTXt International Textual Data
  440.  
  441.       Note: This chunk definition will be moved into the next version of
  442.       the core PNG specification.
  443.  
  444.       This chunk is semantically equivalent to the tEXt and zTXt chunks,
  445.       but the textual data is in the UTF-8 encoding of the Unicode
  446.       character set instead of Latin-1.  This chunk contains:
  447.  
  448.          Keyword:             1-79 bytes (character string)
  449.          Null separator:      1 byte
  450.          Compression flag:    1 byte
  451.          Compression method:  1 byte
  452.          Language tag:        0 or more bytes (character string)
  453.          Null separator:      1 byte
  454.          Translated keyword:  0 or more bytes
  455.          Null separator:      1 byte
  456.          Text:                0 or more bytes
  457.  
  458.       The keyword is case-sensitive and subject to the same restrictions
  459.       as a tEXt keyword: it must contain only printable Latin-1
  460.       [ISO/IEC-8859-1] characters (33-126 and 161-255) and spaces (32),
  461.       but no leading, trailing, or consecutive spaces.
  462.  
  463.       The compression flag is 0 for uncompressed text, 1 for compressed
  464.       text.  Only the text field may be compressed.  The only value
  465.       presently defined for the compression method byte is 0, meaning
  466.       zlib datastream with deflate compression.  For uncompressed text,
  467.       encoders should set the compression method to 0 and decoders
  468.       should ignore it.
  469.  
  470.       The language tag [RFC-1766] indicates the human language used by
  471.       the translated keyword and the text.  Unlike the keyword, the
  472.       language tag is case-insensitive.  It is an ASCII [ISO-646] string
  473.       consisting of hyphen-separated words of 1-8 letters each (for
  474.       example: cn, en-uk, no-bok, x-klingon).  If the first word is two
  475.       letters long, it is an ISO language code [ISO-639].  If the
  476.       language tag is empty, the language is unspecified.
  477.  
  478.       The translated keyword and text both use the UTF-8 encoding of the
  479.       Unicode character set [ISO/IEC-10646-1], and neither may contain a
  480.       zero byte (null character).  The text, unlike the other strings,
  481.       is not null-terminated; its length is implied by the chunk length.
  482.  
  483.       Line breaks should not appear in the translated keyword.  In the
  484.       text, a newline should be represented by a single line feed
  485.       character (decimal 10).  The remaining control characters (1-9,
  486.       11-31, 127-159) are discouraged in both the translated keyword and
  487.       the text.  Note that in UTF-8 there is a difference between the
  488.       characters 128-159 (which are discouraged) and the bytes 128-159
  489.       (which are often necessary).
  490.  
  491.       The translated keyword, if not empty, should contain a translation
  492.       of the keyword into the language indicated by the language tag,
  493.       and applications displaying the keyword should display the
  494.       translated keyword in addition.
  495.  
  496.       Any number of iTXt, tEXt, and zTXt chunks can appear in the same
  497.       file.  See the definition of the tEXt chunk in the core PNG
  498.       specification for the predefined keywords.
  499.  
  500. 4. Chunks Not Described Here
  501.  
  502.    The definitions of some public chunks are being maintained by groups
  503.    other than the core PNG group.  In general, these are chunks that are
  504.    useful to more than one application (and thus are not private
  505.    chunks), but are considered too specialized to list in the core PNG
  506.    documentation.
  507.  
  508.    4.1. fRAc Fractal image parameters
  509.  
  510.       The fRAc chunk will describe the parameters used to generate a
  511.       fractal image.  The specification for the contents of the fRAc
  512.       chunk is being developed by Tim Wegner, twegner@phoenix.net.
  513.  
  514.       In the future, chunks will be fully specified before they are
  515.       registered.
  516.  
  517. 5. Text Chunk Keywords
  518.  
  519.    It is expected that special-purpose keywords for iTXt, tEXt and zTXt
  520.    chunks will be registered and will appear in this document.  However,
  521.    no such keywords have yet been assigned.
  522.  
  523.    All registered textual keywords in iTXt and all other chunk types are
  524.    limited to the ASCII characters A-Z, a-z, 0-9, space, and the
  525.    following 20 symbols:
  526.  
  527.       ! " % & ' ( ) * + , - . / : ; < = > ? _
  528.  
  529.    but not the remaining 12 symbols:
  530.  
  531.       # $ @ [ \ ] ^ ` { | } ~
  532.  
  533.    This restricted set is the ISO-646 "invariant" character set
  534.    [ISO-646].  These characters have the same numeric codes in all ISO
  535.    character sets, including all national variants of ASCII.
  536.  
  537. 6. Deprecated Chunks
  538.  
  539.    The chunks listed in this section are registered, but deprecated.
  540.    Encoders are discouraged from using them, and decoders are not
  541.    encouraged to support them.
  542.  
  543.    6.1. gIFt GIF Plain Text Extension
  544.  
  545.       The gIFt chunk was originally provided for backward compatibility
  546.       with the GIF89a Plain Text Extension, but gIFt is now deprecated
  547.       because it suffers from some fundamental design flaws.
  548.  
  549.           * GIF considers a Plain Text Extension to be a Graphic
  550.             Rendering Block, just like an image, so a GIF datastream
  551.             containing an image and a Plain Text Extension is really a
  552.             multi-image datastream with ordering issues (like
  553.             associating each Graphic Control Extension with the proper
  554.             Graphic Rendering Block).  PNG, being a single-image format
  555.             with no provisions for handling these ordering issues, is
  556.             not equipped to contain both IDAT and gIFt simultaneously.
  557.             Since IDAT is required, gIFt must be discouraged.
  558.  
  559.           * The Text Foreground Color and Text Background Color fields
  560.             of the Plain Text Extension are converted to RGB, rather
  561.             than being converted to RGBA or left as palette indexes.
  562.             Therefore, transparency information can be lost.
  563.  
  564.       The gIFt chunk contains:
  565.  
  566.          Text Grid Left Position: 4 bytes (signed integer,
  567.                                      byte order and size converted)
  568.          Text Grid Top Position:  4 bytes (signed integer,
  569.                                      byte order and size converted)
  570.          Text Grid Width:         4 bytes (unsigned integer,
  571.                                      byte order and size converted)
  572.          Text Grid Height:        4 bytes (unsigned integer,
  573.                                      byte order and size converted)
  574.          Character Cell Width:    1 byte
  575.          Character Cell Height:   1 byte
  576.          Text Foreground Color:   3 bytes (R,G,B samples)
  577.          Text Background Color:   3 bytes (R,G,B samples)
  578.          Plain Text Data:         n bytes
  579.  
  580.       Text Grid Left Position, Top Position, Width, and Height specify
  581.       the text area position and size in pixels.  The converter must
  582.       reformat these fields from 2-byte LSB-first unsigned integers to
  583.       4-byte MSB-first signed or unsigned integers.  Note that GIF
  584.       defines the position to be relative to the upper left corner of
  585.       the logical screen.  If an oFFs chunk is also present, a decoder
  586.       should assume that the oFFs chunk defines the offset of the image
  587.       relative to the GIF logical screen; hence subtracting the oFFs
  588.       values (converted from micrometers to pixels if necessary) from
  589.       the Text Grid Left and Top Positions gives the text area position
  590.       relative to the main PNG image.
  591.  
  592.       Character Cell Width and Height give the dimensions of each
  593.       character in pixels.
  594.  
  595.       Text Foreground and Background Color give the colors to be used to
  596.       render text foreground and background.  Note that the GIF-to-PNG
  597.       converter must replace the palette index values found in the GIF
  598.       Plain Text Extension block with the corresponding palette entry.
  599.  
  600.       The remainder of the chunk is the text to be displayed.  Note that
  601.       this data is not in GIF sub-block format, but is a continuous
  602.       datastream.
  603.  
  604. 7. Security Considerations
  605.  
  606.    The normal precautions (see the Security considerations section of
  607.    the PNG specification) should be taken when displaying text contained
  608.    in the sCAL calibration name, pCAL unit name, or any ASCII floating-
  609.    point fields.
  610.  
  611.    Applications must take care to avoid underflow and overflow of
  612.    intermediate results when converting data from one form to another
  613.    according to the pCAL mappings.
  614.  
  615. 8. Appendix: Sample code
  616.  
  617.    This appendix provides some sample code that can be used in encoding
  618.    and decoding PNG chunks.  It does not form a part of the
  619.    specification.  In the event of a discrepancy between the sample code
  620.    in this appendix and the chunk definition, the chunk definition
  621.    prevails.
  622.  
  623.    8.1. pCAL
  624.  
  625.       The latest version of this code, including test routines not shown
  626.       here, is available at ftp://ftp.uu.net/graphics/png/src/pcal.c.
  627.  
  628.          #if 0
  629.          pcal.c 0.2.2 (Sat 19 Dec 1998)
  630.          Adam M. Costello <amc @ cs.berkeley.edu>
  631.  
  632.          This is public domain example code for computing
  633.          the mappings defined for the PNG pCAL chunk.
  634.  
  635.          #endif
  636.          #if __STDC__ != 1
  637.          #error This code relies on ANSI C conformance.
  638.          #endif
  639.  
  640.          #include <limits.h>
  641.          #include <math.h>
  642.          #include <stdio.h>
  643.          #include <stdlib.h>
  644.  
  645.          /* In this program a type named uintN denotes an unsigned    */
  646.          /* type that handles at least all values 0 through (2^N)-1.  */
  647.          /* A type named intN denotes a signed type that handles at   */
  648.          /* least all values 1-2^(N-1) through 2^(N-1)-1.  It is not  */
  649.          /* necessarily the smallest such type; we are more concerned */
  650.          /* with speed.                                               */
  651.  
  652.          typedef unsigned int uint16;
  653.  
  654.          #if UINT_MAX >= 0xffffffff
  655.            typedef unsigned int uint32;
  656.          #else
  657.            typedef unsigned long uint32;
  658.          #endif
  659.  
  660.          #if INT_MAX >= 0x7fffffff && INT_MIN + 0x7fffffff <= 0
  661.            typedef int int32;
  662.          #else
  663.            typedef long int32;
  664.          #endif
  665.  
  666.          /* Testing for 48-bit integers is tricky because we cannot */
  667.          /* safely use constants greater than 0xffffffff.  Also,    */
  668.          /* shifting by the entire width of a type is undefined, so */
  669.          /* for unsigned int, which might be only 16 bits wide, we  */
  670.          /* must shift in two steps.                                */
  671.  
  672.          #if (UINT_MAX - 0xffff) >> 8 >> 8 >= 0xffffffff
  673.            typedef unsigned int uint48;
  674.            #define HAVE_UINT48 1
  675.          #elif (ULONG_MAX - 0xffff) >> 16 >= 0xffffffff
  676.            typedef unsigned long uint48;
  677.            #define HAVE_UINT48 1
  678.          #elif defined(ULLONG_MAX)
  679.            #if (ULLONG_MAX - 0xffff) >> 16 >= 0xffffffff
  680.              typedef unsigned long long uint48;
  681.              #define HAVE_UINT48 1
  682.            #endif
  683.          #else
  684.            #define HAVE_UINT48 0
  685.          #endif
  686.  
  687.          /*******************/
  688.          /* Program failure */
  689.  
  690.          void
  691.          fail(const char *msg)
  692.          {
  693.            fputs(msg,stderr);
  694.            fputc('\n', stderr);
  695.            exit(EXIT_FAILURE);
  696.          }
  697.  
  698.          /*************************/
  699.          /* Check max, x0, and x1 */
  700.  
  701.          int
  702.          samp_params_ok(uint16 max, int32 x0, int32 x1)
  703.  
  704.          /* Returns 1 if max, x0, and x1 have */
  705.          /* allowed values, 0 otherwise.      */
  706.  
  707.          {
  708.            const int32 xlimit = 0x7fffffff;
  709.  
  710.            return    max >  0      && max <=  0xffff
  711.                   &&  x0 <= xlimit &&  x0 >= -xlimit
  712.                   &&  x1 <= xlimit &&  x1 >= -xlimit
  713.                   &&  x0 != x1;
  714.          }
  715.  
  716.          /***********************************************/
  717.          /* Map from stored samples to original samples */
  718.  
  719.          int32
  720.          stored_to_orig(uint16 stored, uint16 max, int32 x0, int32 x1)
  721.  
  722.          #if 0
  723.  
  724.            Returns the original sample corresponding to the given stored
  725.            sample, which must be <= max.  The parameters max, x0, and x1
  726.            must have been approved by samp_params_ok().
  727.  
  728.            The pCAL spec says:
  729.  
  730.                orig = (stored * (x1-x0) + max/2) / max + x0           [1]
  731.  
  732.            Equivalently:
  733.  
  734.                orig = (stored * (x1-x0) + max/2) / max
  735.                       + (x0-x1) - (x0-x1) + x0
  736.                orig = (stored * (x1-x0) + max * (x0-x1) + max/2) / max
  737.                       - (x0-x1) + x0
  738.                orig = ((max - stored) * (x0-x1) + max/2) / max + x1
  739.  
  740.            So we can check whether x0 < x1 and coerce the formula so that
  741.            the numerators and denominators are always nonnegative:
  742.  
  743.                orig = (offset * xspan + max/2) / max + xbottom        [2]
  744.  
  745.            This will come in handy later.
  746.  
  747.            But the multiplication and the subtraction can overflow, so we
  748.            have to be trickier.  For the subtraction, we can convert to
  749.            unsigned integers.  For the multiplication, we can use 48-bit
  750.            integers if we have them, otherwise observe that:
  751.  
  752.                   b    =   (b/c)*c +     b%c
  753.                 a*b    = a*(b/c)*c +  a*(b%c)  ; let d = a*(b%c)
  754.                (a*b)/c = a*(b/c)   +     d/c   remainder d%c          [3]
  755.  
  756.            These are true no matter which way the division rounds.  If
  757.            (a*b)/c is in-range, a*(b/c) is guaranteed to be in-range if
  758.            b/c rounds toward zero.  Here is another observation:
  759.  
  760.                sum{x_i} / c = sum{x_i / c} + sum{x_i % c} / c         [4]
  761.  
  762.            This one also avoids overflow if the division rounds toward
  763.            zero.  The pCAL spec requires rounding toward -infinity.  ANSI
  764.            C leaves the rounding direction implementation-defined except
  765.            when both the numerator and denominator are nonnegative, in
  766.            which case it rounds downward.  So if we arrange for all
  767.            numerators and denominators to be nonnegative, everything
  768.            works.  Starting with equation 2 and applying identity 4, then
  769.            3, we obtain the final formula:
  770.  
  771.                      d = offset * (xspan % max)
  772.                xoffset = offset * (xspan / max) + d/max
  773.                          + (d%max + max/2) / max
  774.                   orig = xoffset + xbottom
  775.  
  776.          #endif
  777.  
  778.          {
  779.            uint16 offset;
  780.            uint32 xspan, q, r, d, xoffset;
  781.            int32 xbottom;
  782.  
  783.            if (stored > max) fail("stored_to_orig: stored > max");
  784.  
  785.            if (x1 >= x0) {
  786.              xbottom = x0;
  787.              xspan = (uint32)x1 - (uint32)x0;
  788.              offset = stored;
  789.            }
  790.            else {
  791.              xbottom = x1;
  792.              xspan = (uint32)x0 - (uint32)x1;
  793.              offset = max - stored;
  794.            }
  795.  
  796.            /* We knew xspan would fit in a uint32, but we needed to   */
  797.            /* cast x0 and x1 before subtracting because otherwise the */
  798.            /* subtraction could overflow, and ANSI doesn't say what   */
  799.            /* the result will be in that case.                        */
  800.  
  801.            /* Let's optimize two common simple cases */
  802.            /* before handling the general case:      */
  803.  
  804.            if (xspan == max) {
  805.              xoffset = offset;
  806.            }
  807.            else if (xspan <= 0xffff) {
  808.              /* Equation 2 won't overflow and does only one division. */
  809.              xoffset = (offset * xspan + (max>>1)) / max;
  810.            }
  811.            else {
  812.              #if HAVE_UINT48
  813.                /* We can use equation 2 and do one uint48     */
  814.                /* division instead of three uint32 divisions. */
  815.                xoffset = (offset * (uint48)xspan + (max>>1)) / max;
  816.              #else
  817.                q = xspan / max;
  818.                r = xspan % max;
  819.                /* Hopefully those were compiled into one instruction. */
  820.                d = offset * r;
  821.                xoffset = offset * q + d/max + (d%max + (max>>1)) / max;
  822.              #endif
  823.            }
  824.  
  825.            /* xoffset might not fit in an int32, but we know the sum */
  826.            /* xbottom + xoffset will, so we can do the addition on   */
  827.            /* unsigned integers and then cast.                       */
  828.  
  829.            return (int32)((uint32)xbottom + xoffset);
  830.          }
  831.  
  832.          /***********************************************/
  833.          /* Map from original samples to stored samples */
  834.  
  835.          uint16
  836.          orig_to_stored(int32 orig, uint16 max, int32 x0, int32 x1)
  837.  
  838.          #if 0
  839.  
  840.            Returns the stored sample corresponding to the given original
  841.            sample.  The parameters max, x0, and x1 must have been
  842.            approved by samp_params_ok().
  843.  
  844.            The pCAL spec says:
  845.  
  846.            stored = ((orig - x0) * max + (x1-x0)/2) / (x1-x0)
  847.                     clipped to the range 0..max
  848.  
  849.            Notice that all three terms are nonnegative, or else all
  850.            are nonpositive.  Just as in stored_to_orig(), we can avoid
  851.            overflow and rounding problems by transforming the equation to
  852.            use unsigned quantities:
  853.  
  854.            stored = (xoffset * max + xspan/2) / xspan
  855.  
  856.          #endif
  857.  
  858.          {
  859.            uint32 xoffset, xspan;
  860.  
  861.            if (x0 < x1) {
  862.              if (orig < x0) return 0;
  863.              if (orig > x1) return max;
  864.              xspan = (uint32)x1 - (uint32)x0;
  865.              xoffset = (uint32)orig - (uint32)x0;
  866.            }
  867.            else {
  868.              if (orig < x1) return 0;
  869.              if (orig > x0) return max;
  870.              xspan = (uint32)x0 - (uint32)x1;
  871.              xoffset = (uint32)x0 - (uint32)orig;
  872.            }
  873.  
  874.            /* For 16-bit xspan the calculation is straightforward: */
  875.  
  876.            if (xspan <= 0xffff)
  877.              return (xoffset * max + (xspan>>1)) / xspan;
  878.  
  879.            /* Otherwise, the numerator is more than 32 bits and the   */
  880.            /* denominator is more than 16 bits.  The tricks we played */
  881.            /* in stored_to_orig() depended on the denominator being   */
  882.            /* 16-bit, so they won't help us here.                     */
  883.  
  884.            #if HAVE_UINT48
  885.              return ((uint48)xoffset * max + (xspan>>1)) / xspan;
  886.            #else
  887.  
  888.              /* Doing the exact integer calculation with 32-bit         */
  889.              /* arithmetic would be very difficult.  But xspan > 0xffff */
  890.              /* implies xspan > max, in which case the pCAL spec says   */
  891.              /* "there can be no lossless reversible mapping, but the   */
  892.              /* functions provide the best integer approximations to    */
  893.              /* floating-point affine transformations."  So why insist  */
  894.              /* on using the integer calculation?  Let's just use       */
  895.              /* floating-point.                                         */
  896.  
  897.              return ((double)xoffset * max + (xspan>>1)) / xspan;
  898.  
  899.            #endif
  900.          }
  901.  
  902.          /*********************************************/
  903.          /* Check x0, x1, eqtype, n, and p[0]..p[n-1] */
  904.  
  905.          int
  906.          phys_params_ok(int32 x0, int32 x1, int eqtype, int n, double *p)
  907.  
  908.          /* Returns 1 if x0, x1, eqtype, n, and p[0]..p[n-1] */
  909.          /* have allowed values, 0 otherwise.                */
  910.  
  911.          {
  912.            if (!samp_params_ok(1,x0,x1)) return 0;
  913.  
  914.            switch (eqtype) {
  915.              case 0: return n == 2;
  916.              case 1: return n == 3;
  917.              case 2: break;
  918.              case 3: return n == 4;
  919.            }
  920.  
  921.            /* eqtype is 2, check for pow() domain error: */
  922.  
  923.            if (p[2] > 0) return 1;
  924.            if (p[2] < 0) return 0;
  925.            return (x0 <= x1) ? (x0 > 0 && x1 > 0) : (x0 < 0 && x1 < 0);
  926.          }
  927.  
  928.          /************************************************/
  929.          /* Map from original samples to physical values */
  930.  
  931.          double
  932.          orig_to_phys(int32 orig, int32 x0, int32 x1,
  933.                       int eqtype, double *p)
  934.  
  935.          /* Returns the physical value corresponding to the given    */
  936.          /* original sample.  The parameters x0, x1, eqtype, and p[] */
  937.          /* must have been approved by phys_params_ok().  The array  */
  938.          /* p[] must hold enough parameters for the equation type.   */
  939.  
  940.          {
  941.            double xdiff, f;
  942.  
  943.            xdiff = (double)x1 - x0;
  944.  
  945.            switch (eqtype) {
  946.              case 0:  f = orig / xdiff;
  947.                       break;
  948.              case 1:  f = exp(p[2] * orig / xdiff);
  949.                       break;
  950.              case 2:  f = pow(p[2], orig / xdiff);
  951.                       break;
  952.              case 3:  f = sinh(p[2] * (orig - p[3]) / xdiff);
  953.                       break;
  954.              default: fail("orig_to_phys: unknown equation type");
  955.            }
  956.  
  957.            return p[0] + p[1] * f;
  958.          }
  959.  
  960.    8.2. Fixed-point gamma correction
  961.  
  962.       The latest version of this code, including test routines not shown
  963.       here, is available at ftp://ftp.uu.net/graphics/png/src/gamma-
  964.       lookup.c.
  965.  
  966.          #if 0
  967.          gamma-lookup.c 0.1.4 (Sat 19 Dec 1998)
  968.          by Adam M. Costello <amc @ cs.berkeley.edu>
  969.  
  970.          This is public domain example code for computing gamma
  971.          correction lookup tables using integer arithmetic.
  972.  
  973.          #endif
  974.          #if __STDC__ != 1
  975.          #error This code relies on ANSI C conformance.
  976.          #endif
  977.  
  978.          #include <limits.h>
  979.          #include <math.h>
  980.  
  981.          /* In this program a type named uintN denotes the  */
  982.          /* smallest unsigned type we can find that handles */
  983.          /* at least all values 0 through (2^N)-1.          */
  984.  
  985.          typedef unsigned char uint8;
  986.  
  987.          #if UCHAR_MAX >= 0xffff
  988.            typedef unsigned char uint16;
  989.          #else
  990.            typedef unsigned short uint16;
  991.          #endif
  992.  
  993.          #if UCHAR_MAX >= 0xffffffff
  994.            typedef unsigned char uint32;
  995.          #elif USHRT_MAX >= 0xffffffff
  996.            typedef unsigned short uint32;
  997.          #elif UINT_MAX >= 0xffffffff
  998.            typedef unsigned int uint32;
  999.          #else
  1000.            typedef unsigned long uint32;
  1001.          #endif
  1002.  
  1003.          /*********************/
  1004.          /* 16-bit arithmetic */
  1005.  
  1006.          void
  1007.          precompute16(uint16 L[511])
  1008.  
  1009.          /* Precomputes the log table (this requires floating point). */
  1010.  
  1011.          {
  1012.            int j;
  1013.            double f;
  1014.  
  1015.            /* L[j] will hold an integer representation of -log(j / */
  1016.            /* 510.0).  Knowing that L[1] (the largest) is 0xfe00   */
  1017.            /* will help avoid overflow later, so we set the scale  */
  1018.            /* factor accordingly.                                  */
  1019.  
  1020.            f = 0xfe00 / log(1 / 510.0);
  1021.  
  1022.            for (j = 1;  j <= 510;  ++j)
  1023.              L[j] = log(j / 510.0) * f + 0.5;
  1024.          }
  1025.  
  1026.          void
  1027.          gamma16(uint16 L[511], uint8 G[256], uint16 g)
  1028.  
  1029.          /* Makes a 256-entry gamma correction lookup table G[] with */
  1030.          /* exponent g/pow(2,14), where g must not exceed 0xffff.    */
  1031.  
  1032.          {
  1033.            int i, j;
  1034.            uint16 x, y, xhi, ghi, xlo, glo;
  1035.  
  1036.            j = 1;
  1037.            G[0] = 0;
  1038.  
  1039.            for (i = 1;  i <= 255;  ++i) {
  1040.              x = L[i << 1];
  1041.              xhi = x >> 8;
  1042.              ghi = g >> 8;
  1043.              y = xhi * ghi;
  1044.  
  1045.              if (y > 0x3f80) {
  1046.                /* We could have overflowed later. */
  1047.                /* But now we know y << 2 > L[1].  */
  1048.                G[i] = 0;
  1049.                continue;
  1050.              }
  1051.  
  1052.              xlo = x & 0xff;
  1053.              glo = g & 0xff;
  1054.              y = (y << 2) + ((xhi * glo) >> 6) + ((xlo * ghi) >> 6);
  1055.              while (L[j] > y) ++j;
  1056.              G[i] = j >> 1;
  1057.            }
  1058.          }
  1059.  
  1060.          /*********************/
  1061.          /* 32-bit arithmetic */
  1062.  
  1063.          void
  1064.          precompute32(uint32 L[511])
  1065.  
  1066.          /* Precomputes the log table (this requires floating point). */
  1067.  
  1068.          {
  1069.            int j;
  1070.            double f;
  1071.  
  1072.            /* L[j] will hold an integer representation of -log(j /   */
  1073.            /* 510.0).  Knowing that L[1] (the largest) is 0x3fffffff */
  1074.            /* will help avoid overflow later, so we set the scale    */
  1075.            /* factor accordingly.                                    */
  1076.  
  1077.            f = 0x3fffffff / log(1 / 510.0);
  1078.  
  1079.            for (j = 1;  j <= 510;  ++j)
  1080.              L[j] = log(j / 510.0) * f + 0.5;
  1081.          }
  1082.  
  1083.          void
  1084.          gamma32(uint32 L[511], uint8 G[256], uint16 g)
  1085.  
  1086.          /* Makes a 256-entry gamma correction lookup table G[] with */
  1087.          /* exponent g/pow(2,14), where g must not exceed 0xffff.    */
  1088.  
  1089.          {
  1090.            int i, j;
  1091.            uint32 x, y;
  1092.  
  1093.            j = 1;
  1094.            G[0] = 0;
  1095.  
  1096.            for (i = 1;  i <= 255;  ++i) {
  1097.              x = L[i << 1];
  1098.              y = (x >> 14) * g;
  1099.              while (L[j] > y) ++j;
  1100.              G[i] = j >> 1;
  1101.            }
  1102.          }
  1103.  
  1104.          /**********************************************/
  1105.          /* Floating-point arithmetic (for comparison) */
  1106.  
  1107.          void
  1108.          gamma_fp(uint8 G[256], double g)
  1109.  
  1110.          /* Makes a 256-entry gamma correction */
  1111.          /* lookup table G[i] with exponent g. */
  1112.  
  1113.          {
  1114.            int i;
  1115.  
  1116.            G[0] = 0;
  1117.  
  1118.            for (i = 1;  i <= 255;  ++i)
  1119.              G[i] = pow(i/255.0, g) * 255 + 0.5;
  1120.          }
  1121.  
  1122. 9. Appendix: Rationale
  1123.  
  1124.    This appendix gives the reasoning behind some of the design decisions
  1125.    in the PNG extension chunks.  It does not form a part of the
  1126.    specification.
  1127.  
  1128.    9.1. iTXt
  1129.  
  1130.       This section gives the reasoning behind some of the design
  1131.       decisions in the iTXt chunk.  It does not form a part of the
  1132.       specification.
  1133.  
  1134.       Keyword: Why not Unicode?
  1135.  
  1136.          Unicode is too fancy for the keyword, which is intended for
  1137.          both machine and human consumption.  Even applications without
  1138.          Unicode support should at least be able to understand the
  1139.          keyword (to selectively delete chunks, for example).
  1140.  
  1141.       Keyword: Latin-1 vs. ASCII
  1142.  
  1143.          UTF-8 is used elsewhere in this chunk, and ASCII, unlike
  1144.          Latin-1, is compatible with UTF-8.  There is a translated
  1145.          keyword, so restricting the keyword to ASCII would not be a
  1146.          hardship.  So why use Latin-1?  Because all other existing
  1147.          chunks containing keywords use Latin-1, so applications can
  1148.          reuse code they already contain.
  1149.  
  1150.       Compression flag and compression method: Why not combine them?
  1151.  
  1152.          We have deliberately avoided defining a null compression method
  1153.          in the past (for tEXt/zTXt), so that there would be no
  1154.          temptation to use it in IHDR.
  1155.  
  1156.       Language tag:
  1157.  
  1158.          It is not always clear how to render Unicode text unless it is
  1159.          known what language is represented by the text.  Also, multiple
  1160.          iTXt chunks containing the same message in different languages
  1161.          could be present, and a decoder could automatically select the
  1162.          one most appropriate for its user.
  1163.  
  1164.       Translated keyword:
  1165.  
  1166.          Registered keywords, like "Description", are registered only
  1167.          once, in a single language (probably English), so that they can
  1168.          be recognized automatically.  To be intelligible to speakers of
  1169.          another language, a translation must be provided.
  1170.  
  1171.       Text: Unicode vs. MIME charset name
  1172.  
  1173.          Including a MIME charset name would be more general, and allow
  1174.          the use of legacy character sets.  But support for Unicode is
  1175.          growing, and allowing only Unicode is conceptually simpler and
  1176.          likely to eventually lead to greater interoperability.
  1177.  
  1178.       UTF-8 vs. UCS-2 vs. UCS-4
  1179.  
  1180.          UCS-2 is short-sighted.  Neither UCS-2 nor UCS-4 is compatible
  1181.          with ASCII.  UTF-8 is both backward compatible with ASCII and
  1182.          forward compatible with UCS-4, and is generally the preferred
  1183.          encoding for interchange (as opposed to internal
  1184.          representation).
  1185.  
  1186.    9.2. pCAL
  1187.  
  1188.       This section gives the reasoning behind some of the design
  1189.       decisions in the pCAL chunk.  It does not form a part of the
  1190.       specification.
  1191.  
  1192.       Redundant equation types
  1193.  
  1194.          Equation types 1 and 2 seem to be equivalent.  Why have both?
  1195.  
  1196.              * We don't want to force people to do the exponentiation
  1197.                using ln() and exp(), since pow() may provide better
  1198.                accuracy in some floating-point math libraries.  We also
  1199.                don't want to force people using base-10 logs to store a
  1200.                sufficiently accurate value of ln(10) in the pCAL chunk.
  1201.  
  1202.              * When the base is e, we don't want to force people to
  1203.                encode a sufficiently accurate value of e in the pCAL
  1204.                chunk, or to use pow() when exp() is sufficient.
  1205.  
  1206.       What are x0 and x1 for?
  1207.  
  1208.              * First, x0 and x1 provide a way to recover the original
  1209.                data, losslessly, when the original range is not a power
  1210.                of two.  Sometimes the digitized values do not have a
  1211.                range that fills the full depth of a PNG.  For example,
  1212.                if the original samples range from 0 (corresponding to
  1213.                black) to 800 (corresponding to white), PNG requires that
  1214.                these samples be scaled to the range 0 to 65535.  By
  1215.                recording x0=0 and x1=800 we can recover the original
  1216.                samples, and we indicate the precision of the data.
  1217.  
  1218.              * Even if the original data had a range identical to a
  1219.                valid PNG image sample, like 0 (black) to 65535 (white),
  1220.                one might want to create a derived image by stretching
  1221.                the contrast in a limited intensity range containing the
  1222.                important details.  For example, we might want to scale
  1223.                the samples so that 46000 becomes 0 (black) and 47000
  1224.                becomes 65535 (white).  As in the previous case, by
  1225.                recording x0=46000 and x1=47000, we can recover the
  1226.                original data samples that fell between 46000 and 47000.
  1227.  
  1228.       Integer division
  1229.  
  1230.          Why define integer divison to round toward negative infinity?
  1231.          This is different from many C implementations and from all
  1232.          Fortran implementations, which round toward zero.
  1233.  
  1234.          We cannot leave the choice unspecified.  If we were to specify
  1235.          rounding toward zero, we'd have to account for a discontinuity
  1236.          at zero.  A division by positive d would map the 2d-1 values
  1237.          from -(d-1) through d-1 to zero, but would map only d values to
  1238.          any other value; for example, 3d through 4d-1 would be mapped
  1239.          to 3.  Achieving lossless mappings in spite of this anomaly
  1240.          would be difficult.
  1241.  
  1242. 10. Appendix: Revision History
  1243.  
  1244.        * 9 February 1999:
  1245.  
  1246.              * Added the iTXt chunk
  1247.              * Limited the character set for future registered keywords
  1248.        * 30 December 1998:
  1249.  
  1250.              * Added pCAL chunk and related sample code
  1251.              * Deprecated the gIFT chunk
  1252.              * Added sample gamma-correction code that uses integer
  1253.                arithmetic
  1254.        * 11 March 1997: First public release
  1255.  
  1256. 11. References
  1257.  
  1258.    [ISO/IEC-8859-1]
  1259.       International Organization for Standardization and International
  1260.       Electrotechnical Commission, "Information Technology--8-bit
  1261.       Single-Byte Coded Graphic Character Sets--Part 1: Latin Alphabet
  1262.       No. 1", IS 8859-1, 1998.
  1263.       Also see sample files at
  1264.       ftp://ftp.uu.net/graphics/png/documents/iso_8859-1.*
  1265.  
  1266.    [ISO-639]
  1267.       International Organization for Standardization, "Code for the
  1268.       representation of names of languages", 1st edition, 1988.
  1269.  
  1270.    [ISO-646]
  1271.       International Organization for Standardization and International
  1272.       Electrotechnical Commission, "Information Technology--ISO 7-bit
  1273.       Coded Character Set for Information Exchange", 1991.
  1274.  
  1275.    [ISO/IEC-10646-1]
  1276.       International Organization for Standardization and International
  1277.       Electrotechnical Commission, "Information Technology--Universal
  1278.       Multiple-Octet Coded Character Set (UCS)--Part 1: Architecture and
  1279.       Basic Multilingual Plane", 1993.
  1280.       See also "The Unicode Standard" published by the Unicode
  1281.       Consortium
  1282.       http://www.unicode.org
  1283.       and RFC 2279 (F. Yergeau, "UTF-8, a transformation format of ISO
  1284.       10646", January 1998)
  1285.       ftp://ftp.isi.edu/in-notes/rfc2279.txt
  1286.  
  1287.    [RFC-1766]
  1288.       Harald T. Alvestrand, "Tags for the Identification of Languages",
  1289.       RFC 1766, UNINETT, March 1995.
  1290.       ftp://ftp.isi.edu/in-notes/rfc1766.txt
  1291.  
  1292. 12. Credits
  1293.  
  1294.    Editors
  1295.  
  1296.           * Glenn Randers-Pehrson, randeg @ alumni.rpi.edu
  1297.           * Tom Lane, tgl @ sss.pgh.pa.us (edited the first release of
  1298.             this document)
  1299.  
  1300.    Contributors
  1301.  
  1302.       Names of contributors not already listed in the PNG specification
  1303.       are presented in alphabetical order:
  1304.  
  1305.           * Todd French, tfrench @ sandia.gov
  1306.           * Alaric B. Williams, png @ abwillms.demon.co.uk
  1307.  
  1308.    Trademarks
  1309.  
  1310.       GIF is a service mark of CompuServe Incorporated.  PostScript is a
  1311.       trademark of Adobe Systems.
  1312.  
  1313.    Document source
  1314.  
  1315.       This document was built from the file pngext-master-19990209 on
  1316.       9 February 1999.
  1317.  
  1318.    Copyright Notice
  1319.  
  1320.       Copyright (c) 1998, 1999 by:  Glenn Randers-Pehrson
  1321.  
  1322.       This specification is being provided by the copyright holder under
  1323.       the following license.  By obtaining, using and/or copying this
  1324.       specification, you agree that you have read, understood, and will
  1325.       comply with the following terms and conditions:
  1326.  
  1327.       Permission to use, copy, and distribute this specification for any
  1328.       purpose and without fee or royalty is hereby granted, provided
  1329.       that the full text of this NOTICE appears on ALL copies of the
  1330.       specification or portions thereof, including modifications, that
  1331.       you make.
  1332.  
  1333.       THIS SPECIFICATION IS PROVIDED "AS IS," AND COPYRIGHT HOLDER MAKES
  1334.       NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED.  BY WAY OF
  1335.       EXAMPLE, BUT NOT LIMITATION, COPYRIGHT HOLDERS MAKE NO
  1336.       REPRESENTATIONS OR WARRANTIES OF MERCHANTABILITY OR FITNESS FOR
  1337.       ANY PARTICULAR PURPOSE OR THAT THE USE OF THE SPECIFICATION WILL
  1338.       NOT INFRINGE ANY THIRD PARTY PATENTS, COPYRIGHTS, TRADEMARKS OR
  1339.       OTHER RIGHTS.  COPYRIGHT HOLDER WILL BEAR NO LIABILITY FOR ANY USE
  1340.       OF THIS SPECIFICATION.
  1341.  
  1342.       The name and trademarks of copyright holder may NOT be used in
  1343.       advertising or publicity pertaining to the specification without
  1344.       specific, written prior permission.  Title to copyright in this
  1345.       specification and any associated documentation will at all times
  1346.       remain with copyright holder.
  1347.  
  1348. End of Extensions to the PNG Specification
  1349.  
  1350.  
  1351.