github.com/cellofellow/gopkg@v0.0.0-20140722061823-eec0544a62ad/image/webp/libwebp/doc/webp-container-spec.txt (about)

     1  <!--
     2  
     3  Although you may be viewing an alternate representation, this document
     4  is sourced in Markdown, a light-duty markup scheme, and is optimized for
     5  the [kramdown](http://kramdown.rubyforge.org/) transformer.
     6  
     7  See the accompanying README. External link targets are referenced at the
     8  end of this file.
     9  
    10  -->
    11  
    12  
    13  WebP Container Specification
    14  ============================
    15  
    16  * TOC placeholder
    17  {:toc}
    18  
    19  
    20  Introduction
    21  ------------
    22  
    23  WebP is an image format that uses either (i) the VP8 key frame encoding
    24  to compress image data in a lossy way, or (ii) the WebP lossless encoding
    25  (and possibly other encodings in the future). These encoding schemes should
    26  make it more efficient than currently used formats. It is optimized for fast
    27  image transfer over the network (e.g., for websites). The WebP format has
    28  feature parity (color profile, metadata, animation etc) with other formats as
    29  well. This document describes the structure of a WebP file.
    30  
    31  The WebP container (i.e., RIFF container for WebP) allows feature support over
    32  and above the basic use case of WebP (i.e., a file containing a single image
    33  encoded as a VP8 key frame). The WebP container provides additional support
    34  for:
    35  
    36    * **Lossless compression.** An image can be losslessly compressed, using the
    37      WebP Lossless Format.
    38  
    39    * **Metadata.** An image may have metadata stored in EXIF or XMP formats.
    40  
    41    * **Transparency.** An image may have transparency, i.e., an alpha channel.
    42  
    43    * **Color Profile.** An image may have an embedded ICC profile as described
    44      by the [International Color Consortium][iccspec].
    45  
    46    * **Animation.** An image may have multiple frames with pauses between them,
    47      making it an animation.
    48  
    49    * **Image Fragmentation.** A single bitstream in WebP has an inherent
    50      limitation for width or height of 2^14 pixels, and, when using VP8, a 512
    51      KiB limit on the size of the first compressed partition. To support larger
    52      images, the format supports images that are composed of multiple fragments,
    53      each encoded as a separate bitstream. All fragments logically form a single
    54      image: they have common metadata, color profile, etc. Image fragmentation
    55      may also improve efficiency for larger images, e.g., grass can be encoded
    56      differently than sky.
    57  
    58  The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT",
    59  "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this
    60  document are to be interpreted as described in [RFC 2119][].
    61  
    62  **Note:** Out of the features mentioned above, lossy compression, lossless
    63  compression, transparency, metadata, color profile and animation are finalized
    64  and are to be considered stable. On the other hand, image fragmentation is
    65  experimental as of now, and is open to discussion, feedback and comments.
    66  The same is indicated using annotation "_status: experimental_" in the relevant
    67  sections of this document.
    68  
    69  Terminology &amp; Basics
    70  ------------------------
    71  
    72  A WebP file contains either a still image (i.e., an encoded matrix of pixels)
    73  or an [animation](#animation). Optionally, it can also contain transparency
    74  information, color profile and metadata. In case we need to refer only to the
    75  matrix of pixels, we will call it the _canvas_ of the image.
    76  
    77  Below are additional terms used throughout this document:
    78  
    79  _Reader/Writer_
    80  
    81  : Code that reads WebP files is referred to as a _reader_, while code that
    82  writes them is referred to as a _writer_.
    83  
    84  _uint16_
    85  
    86  : A 16-bit, little-endian, unsigned integer.
    87  
    88  _uint24_
    89  
    90  : A 24-bit, little-endian, unsigned integer.
    91  
    92  _uint32_
    93  
    94  : A 32-bit, little-endian, unsigned integer.
    95  
    96  _FourCC_
    97  
    98  : A _FourCC_ (four-character code) is a _uint32_ created by concatenating four
    99    ASCII characters in little-endian order.
   100  
   101  _1-based_
   102  
   103  : An unsigned integer field storing values offset by `-1`. e.g., Such a field
   104  would store value _25_ as _24_.
   105  
   106  RIFF file format
   107  ----------------
   108  The WebP file format is based on the RIFF (resource interchange file format)
   109  document format.
   110  
   111  The basic element of a RIFF file is a _chunk_. It consists of:
   112  
   113       0                   1                   2                   3
   114       0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
   115      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   116      |                         Chunk FourCC                          |
   117      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   118      |                          Chunk Size                           |
   119      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   120      |                         Chunk Payload                         |
   121      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   122  
   123  Chunk FourCC: 32 bits
   124  
   125  : ASCII four-character code used for chunk identification.
   126  
   127  Chunk Size: 32 bits (_uint32_)
   128  
   129  : The size of the chunk not including this field, the chunk identifier or
   130    padding.
   131  
   132  Chunk Payload: _Chunk Size_ bytes
   133  
   134  : The data payload. If _Chunk Size_ is odd, a single padding byte -- that
   135    SHOULD be `0` -- is added.
   136  
   137  _ChunkHeader('ABCD')_
   138  
   139  : This is used to describe the _FourCC_ and _Chunk Size_ header of individual
   140    chunks, where 'ABCD' is the FourCC for the chunk. This element's
   141    size is 8 bytes.
   142  
   143  **Note:** RIFF has a convention that all-uppercase chunk FourCCs are standard
   144  chunks that apply to any RIFF file format, while FourCCs specific to a file
   145  format are all lowercase. WebP does not follow this convention.
   146  
   147  WebP file header
   148  ----------------
   149  
   150       0                   1                   2                   3
   151       0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
   152      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   153      |      'R'      |      'I'      |      'F'      |      'F'      |
   154      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   155      |                           File Size                           |
   156      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   157      |      'W'      |      'E'      |      'B'      |      'P'      |
   158      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   159  
   160  'RIFF': 32 bits
   161  
   162  : The ASCII characters 'R' 'I' 'F' 'F'.
   163  
   164  File Size: 32 bits (_uint32_)
   165  
   166  : The size of the file in bytes starting at offset 8. The maximum value of
   167  this field is 2^32 minus 10 bytes and thus the size of the whole file is at
   168  most 4GiB minus 2 bytes.
   169  
   170  'WEBP': 32 bits
   171  
   172  : The ASCII characters 'W' 'E' 'B' 'P'.
   173  
   174  A WebP file MUST begin with a RIFF header with the FourCC 'WEBP'. The file size
   175  in the header is the total size of the chunks that follow plus `4` bytes for
   176  the 'WEBP' FourCC. The file SHOULD NOT contain anything after it. As the size
   177  of any chunk is even, the size given by the RIFF header is also even. The
   178  contents of individual chunks will be described in the following sections.
   179  
   180  Simple file format (lossy)
   181  --------------------------
   182  
   183  This layout SHOULD be used if the image requires _lossy_ encoding and does not
   184  require transparency or other advanced features provided by the extended format.
   185  Files with this layout are smaller and supported by older software.
   186  
   187  Simple WebP (lossy) file format:
   188  
   189       0                   1                   2                   3
   190       0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
   191      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   192      |                    WebP file header (12 bytes)                |
   193      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   194      |                          VP8 chunk                            |
   195      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   196  
   197  VP8 chunk:
   198  
   199       0                   1                   2                   3
   200       0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
   201      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   202      |                      ChunkHeader('VP8 ')                      |
   203      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   204      |                           VP8 data                            |
   205      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   206  
   207  VP8 data: _Chunk Size_ bytes
   208  
   209  : VP8 bitstream data.
   210  
   211  The VP8 bitstream format specification can be found at [VP8 Data Format and
   212  Decoding Guide][vp8spec]. Note that the VP8 frame header contains the VP8 frame
   213  width and height. That is assumed to be the width and height of the canvas.
   214  
   215  The VP8 specification describes how to decode the image into Y'CbCr
   216  format. To convert to RGB, Rec. 601 SHOULD be used.
   217  
   218  Simple file format (lossless)
   219  -----------------------------
   220  
   221  **Note:** Older readers may not support files using the lossless format.
   222  
   223  This layout SHOULD be used if the image requires _lossless_ encoding (with an
   224  optional transparency channel) and does not require advanced features provided
   225  by the extended format.
   226  
   227  Simple WebP (lossless) file format:
   228  
   229       0                   1                   2                   3
   230       0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
   231      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   232      |                    WebP file header (12 bytes)                |
   233      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   234      |                          VP8L chunk                           |
   235      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   236  
   237  VP8L chunk:
   238  
   239       0                   1                   2                   3
   240       0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
   241      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   242      |                      ChunkHeader('VP8L')                      |
   243      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   244      |                           VP8L data                           |
   245      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   246  
   247  VP8L data: _Chunk Size_ bytes
   248  
   249  : VP8L bitstream data.
   250  
   251  The current specification of the VP8L bitstream can be found at
   252  [WebP Lossless Bitstream Format][webpllspec]. Note that the VP8L header
   253  contains the VP8L image width and height. That is assumed to be the width
   254  and height of the canvas.
   255  
   256  Extended file format
   257  --------------------
   258  
   259  **Note:** Older readers may not support files using the extended format.
   260  
   261  An extended format file consists of:
   262  
   263    * A 'VP8X' chunk with information about features used in the file.
   264  
   265    * An optional 'ICCP' chunk with color profile.
   266  
   267    * An optional 'ANIM' chunk with animation control data.
   268  
   269    * Image data.
   270  
   271    * An optional 'EXIF' chunk with EXIF metadata.
   272  
   273    * An optional 'XMP ' chunk with XMP metadata.
   274  
   275    * An optional list of [unknown chunks](#unknown-chunks). _\[status: experimental\]_
   276  
   277  For a _still image_, the _image data_ consists of a single frame, whereas for
   278  an _animated image_, it consists of multiple frames. More details about frames
   279  can be found in the [Animation](#animation) section.
   280  
   281  Moreover, each frame can be fragmented or non-fragmented, as will be described
   282  in the [Extended WebP file header](#extended_header) section. More details about
   283  fragments can be found in the [Fragments](#fragments) section.
   284  
   285  All chunks SHOULD be placed in the same order as listed above. If a chunk
   286  appears in the wrong place, the file is invalid, but readers MAY parse the
   287  file, ignoring the chunks that come too late.
   288  
   289  **Rationale:** Setting the order of chunks should allow quicker file
   290  parsing. For example, if an 'ALPH' chunk does not appear in its required
   291  position, a decoder can choose to stop searching for it. The rule of
   292  ignoring late chunks should make programs that need to do a full search
   293  give the same results as the ones stopping early.
   294  
   295  Extended WebP file header:
   296  {:#extended_header}
   297  
   298       0                   1                   2                   3
   299       0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
   300      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   301      |                   WebP file header (12 bytes)                 |
   302      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   303      |                      ChunkHeader('VP8X')                      |
   304      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   305      |Rsv|I|L|E|X|A|F|                   Reserved                    |
   306      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   307      |          Canvas Width Minus One               |             ...
   308      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   309      ...  Canvas Height Minus One    |
   310      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   311  
   312  Reserved (Rsv): 2 bits
   313  
   314  : SHOULD be `0`.
   315  
   316  ICC profile (I): 1 bit
   317  
   318  : Set if the file contains an ICC profile.
   319  
   320  Alpha (L): 1 bit
   321  
   322  : Set if any of the frames of the image contain transparency information
   323  ("alpha").
   324  
   325  EXIF metadata (E): 1 bit
   326  
   327  : Set if the file contains EXIF metadata.
   328  
   329  XMP metadata (X): 1 bit
   330  
   331  : Set if the file contains XMP metadata.
   332  
   333  Animation (A): 1 bit
   334  
   335  : Set if this is an animated image. Data in 'ANIM' and 'ANMF' chunks should be
   336  used to control the animation.
   337  
   338  Image Fragmentation (F): 1 bit _\[status: experimental\]_
   339  
   340  : Set if any of the frames in the image are represented by fragments.
   341  
   342  Reserved: 24 bits
   343  
   344  : SHOULD be `0`.
   345  
   346  Canvas Width Minus One: 24 bits
   347  
   348  : _1-based_ width of the canvas in pixels.
   349    The actual canvas width is '1 + Canvas Width Minus One'
   350  
   351  Canvas Height Minus One: 24 bits
   352  
   353  : _1-based_ height of the canvas in pixels.
   354    The actual canvas height is '1 + Canvas Height Minus One'
   355  
   356  The product of _Canvas Width_ and _Canvas Height_ MUST be at most `2^32 - 1`.
   357  
   358  Future specifications MAY add more fields.
   359  
   360  ### Chunks
   361  
   362  #### Animation
   363  
   364  An animation is controlled by ANIM and ANMF chunks.
   365  
   366  ANIM Chunk:
   367  {:#anim_chunk}
   368  
   369  For an animated image, this chunk contains the _global parameters_ of the
   370  animation.
   371  
   372       0                   1                   2                   3
   373       0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
   374      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   375      |                      ChunkHeader('ANIM')                      |
   376      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   377      |                       Background Color                        |
   378      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   379      |          Loop Count           |
   380      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   381  
   382  Background Color: 32 bits (_uint32_)
   383  
   384  : The default background color of the canvas in \[Blue, Green, Red, Alpha\]
   385  byte order. This color MAY be used to fill the unused space on the canvas around
   386  the frames, as well as the transparent pixels of the first frame. Background
   387  color is also used when disposal method is `1`.
   388  
   389  **Note**:
   390  
   391    * Background color MAY contain a transparency value (alpha), even if the
   392      _Alpha_ flag in [VP8X chunk](#extended_header) is unset.
   393  
   394    * Viewer applications SHOULD treat the background color value as a hint, and
   395      are not required to use it.
   396  
   397  Loop Count: 16 bits (_uint16_)
   398  
   399  : The number of times to loop the animation. `0` means infinitely.
   400  
   401  This chunk MUST appear if the _Animation_ flag in the VP8X chunk is set.
   402  If the _Animation_ flag is not set and this chunk is present, it
   403  SHOULD be ignored.
   404  
   405  
   406  ANMF chunk:
   407  
   408  For animated images, this chunk contains information about a _single_ frame.
   409  If the _Animation flag_ is not set, then this chunk SHOULD NOT be present.
   410  
   411       0                   1                   2                   3
   412       0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
   413      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   414      |                      ChunkHeader('ANMF')                      |
   415      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   416      |                        Frame X                |             ...
   417      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   418      ...          Frame Y            |   Frame Width Minus One     ...
   419      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   420      ...             |           Frame Height Minus One              |
   421      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   422      |                 Frame Duration                |  Reserved |B|D|
   423      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   424      |                         Frame Data                            |
   425      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   426  
   427  Frame X: 24 bits (_uint24_)
   428  
   429  : The X coordinate of the upper left corner of the frame is `Frame X * 2`
   430  
   431  Frame Y: 24 bits (_uint24_)
   432  
   433  : The Y coordinate of the upper left corner of the frame is `Frame Y * 2`
   434  
   435  Frame Width Minus One: 24 bits (_uint24_)
   436  
   437  : The _1-based_ width of the frame.
   438    The frame width is `1 + Frame Width Minus One`
   439  
   440  Frame Height Minus One: 24 bits (_uint24_)
   441  
   442  : The _1-based_ height of the frame.
   443    The frame height is `1 + Frame Height Minus One`
   444  
   445  Frame Duration: 24 bits (_uint24_)
   446  
   447  : The time to wait before displaying the next frame, in 1 millisecond units.
   448  In particular, frame duration of 0 is useful when one wants to update multiple
   449  areas of the canvas at once during the animation.
   450  
   451  Reserved: 6 bits
   452  
   453  : SHOULD be 0.
   454  
   455  Blending method (B): 1 bit
   456  
   457  : Indicates how transparent pixels of _the current frame_ are to be blended with
   458  corresponding pixels of the previous canvas:
   459  
   460    * `0`: Use alpha blending. After disposing of the previous frame, render the
   461      current frame on the canvas using [alpha-blending](#alpha-blending). If the
   462      current frame does not have an alpha channel, assume alpha value of 255,
   463      effectively replacing the rectangle.
   464  
   465    * `1`: Do not blend. After disposing of the previous frame, render the
   466      current frame on the canvas by overwriting the rectangle covered by the
   467      current frame.
   468  
   469  Disposal method (D): 1 bit
   470  
   471  : Indicates how _the current frame_ is to be treated after it has been displayed
   472  (before rendering the next frame) on the canvas:
   473  
   474    * `0`: Do not dispose. Leave the canvas as is.
   475  
   476    * `1`: Dispose to background color. Fill the _rectangle_ on the canvas covered
   477      by the _current frame_ with background color specified in the
   478      [ANIM chunk](#anim_chunk).
   479  
   480  **Notes**:
   481  
   482    * The frame disposal only applies to the _frame rectangle_, that is, the
   483      rectangle defined by _Frame X_, _Frame Y_, _frame width_ and _frame height_.
   484      It may or may not cover the whole canvas.
   485  
   486  {:#alpha-blending}
   487    * **Alpha-blending**:
   488  
   489      Given that each of the R, G, B and A channels is 8-bit, and the RGB
   490      channels are _not premultiplied_ by alpha, the formula for blending
   491      'dst' onto 'src' is:
   492  
   493  ~~~~~
   494      blend.A = src.A + dst.A * (1 - src.A / 255)
   495      if blend.A = 0 then
   496        blend.RGB = 0
   497      else
   498        blend.RGB = (src.RGB * src.A +
   499                     dst.RGB * dst.A * (1 - src.A / 255)) / blend.A
   500  ~~~~~
   501  
   502    * Alpha-blending SHOULD be done in linear color space, by taking into account
   503      the [color profile](#color-profile) of the image. If the color profile is
   504      not present, sRGB is to be assumed. (Note that sRGB also needs to be
   505      linearized due to a gamma of ~2.2).
   506  
   507  Frame Data: _Chunk Size_ - `16` bytes
   508  
   509  : For a fragmented frame, it consists of multiple [fragment chunks](#fragments).
   510  
   511  : For a non-fragmented frame, it consists of:
   512  
   513    * An optional [alpha subchunk](#alpha) for the frame.
   514  
   515    * A [bitstream subchunk](#bitstream-vp8vp8l) for the frame.
   516  
   517    * An optional list of [unknown chunks](#unknown-chunks).
   518  
   519  **Note**: The 'ANMF' payload, _Frame Data_ above, consists of individual
   520  _padded_ chunks as described by the [RIFF file format](#riff-file-format).
   521  
   522  #### Fragments _\[status: experimental\]_
   523  
   524  For images that are represented by fragments, this chunk contains data for
   525  a single fragment. If the _Image Fragmentation Flag_ is not set, then this chunk
   526  SHOULD NOT be present.
   527  
   528       0                   1                   2                   3
   529       0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
   530      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   531      |                      ChunkHeader('FRGM')                      |
   532      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   533      |                  Fragment X                   |             ...
   534      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   535      ...       Fragment Y            |         Fragment Data         |
   536      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   537  
   538  Fragment X: 24 bits (_uint24_)
   539  
   540  : The X coordinate of the upper left corner of the fragment is `Fragment X * 2`
   541  
   542  Fragment Y: 24 bits (_uint24_)
   543  
   544  : The Y coordinate of the upper left corner of the fragment is `Fragment Y * 2`
   545  
   546  Fragment Data: _Chunk Size_ - `6` bytes
   547  
   548  : It contains:
   549  
   550    * An optional [alpha subchunk](#alpha) for the fragment.
   551    * The [bitstream subchunk](#bitstream-vp8vp8l) for the fragment.
   552    * An optional list of [unknown chunks](#unknown-chunks).
   553  
   554  Note: The width and height of the fragment is obtained from the bitstream
   555  subchunk.
   556  
   557  The fragments of a frame SHOULD have the following properties:
   558  
   559    * They collectively cover the whole frame.
   560  
   561    * No pair of fragments have any overlapping region on the frame.
   562  
   563    * No portion of any fragment should be located outside of the canvas.
   564  
   565  #### Alpha
   566  
   567       0                   1                   2                   3
   568       0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
   569      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   570      |                      ChunkHeader('ALPH')                      |
   571      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   572      |Rsv| P | F | C |     Alpha Bitstream...                        |
   573      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   574  
   575  Reserved (Rsv): 2 bits
   576  
   577  : SHOULD be `0`.
   578  
   579  Pre-processing (P): 2 bits
   580  
   581  : These INFORMATIVE bits are used to signal the pre-processing that has
   582  been performed during compression. The decoder can use this information to
   583  e.g. dither the values or smooth the gradients prior to display.
   584  
   585    * `0`: no pre-processing
   586    * `1`: level reduction
   587  
   588  Filtering method (F): 2 bits
   589  
   590  : The filtering method used:
   591  
   592    * `0`: None.
   593    * `1`: Horizontal filter.
   594    * `2`: Vertical filter.
   595    * `3`: Gradient filter.
   596  
   597  For each pixel, filtering is performed using the following calculations.
   598  Assume the alpha values surrounding the current `X` position are labeled as:
   599  
   600       C | B |
   601      ---+---+
   602       A | X |
   603  
   604  We seek to compute the alpha value at position `X`. First, a prediction is
   605  made depending on the filtering method:
   606  
   607    * Method `0`: predictor = 0
   608    * Method `1`: predictor = A
   609    * Method `2`: predictor = B
   610    * Method `3`: predictor = clip(A + B - C)
   611  
   612  where `clip(v)` is equal to:
   613  
   614    * 0    if v < 0
   615    * 255  if v > 255
   616    * v    otherwise
   617  
   618  The final value is derived by adding the decompressed value `X` to the
   619  predictor and using modulo-256 arithmetic to wrap the \[256-511\] range
   620  into the \[0-255\] one:
   621  
   622  `alpha = (predictor + X) % 256`
   623  
   624  There are special cases for left-most and top-most pixel positions:
   625  
   626    * Top-left value at location (0,0) uses 0 as predictor value. Otherwise,
   627    * For horizontal or gradient filtering methods, the left-most pixels at
   628      location (0, y) are predicted using the location (0, y-1) just above.
   629    * For vertical or gradient filtering methods, the top-most pixels at
   630      location (x, 0) are predicted using the location (x-1, 0) on the left.
   631  
   632  
   633  Decoders are not required to use this information in any specified way.
   634  
   635  Compression method (C): 2 bits
   636  
   637  : The compression method used:
   638  
   639    * `0`: No compression.
   640    * `1`: Compressed using the WebP lossless format.
   641  
   642  Alpha bitstream: _Chunk Size_ - `1` bytes
   643  
   644  : Encoded alpha bitstream.
   645  
   646  This optional chunk contains encoded alpha data for this frame/fragment. A
   647  frame/fragment containing a 'VP8L' chunk SHOULD NOT contain this chunk.
   648  
   649  **Rationale**: The transparency information is already part of the 'VP8L'
   650  chunk.
   651  
   652  The alpha channel data is stored as uncompressed raw data (when
   653  compression method is '0') or compressed using the lossless format
   654  (when the compression method is '1').
   655  
   656    * Raw data: consists of a byte sequence of length width * height,
   657      containing all the 8-bit transparency values in scan order.
   658  
   659    * Lossless format compression: the byte sequence is a compressed
   660      image-stream (as described in the [WebP Lossless Bitstream Format]
   661      [webpllspec]) of implicit dimension width x height. That is, this
   662      image-stream does NOT contain any headers describing the image dimension.
   663  
   664      **Rationale**: the dimension is already known from other sources,
   665      so storing it again would be redundant and error-prone.
   666  
   667      Once the image-stream is decoded into ARGB color values, following
   668      the process described in the lossless format specification, the
   669      transparency information must be extracted from the *green* channel
   670      of the ARGB quadruplet.
   671  
   672      **Rationale**: the green channel is allowed extra transformation
   673      steps in the specification -- unlike the other channels -- that can
   674      improve compression.
   675  
   676  #### Bitstream (VP8/VP8L)
   677  
   678  This chunk contains compressed bitstream data for a single frame/fragment.
   679  
   680  A bitstream chunk may be either (i) a VP8 chunk, using "VP8 " (note the
   681  significant fourth-character space) as its tag _or_ (ii) a VP8L chunk, using
   682  "VP8L" as its tag.
   683  
   684  The formats of VP8 and VP8L chunks are as described in sections
   685  [Simple file format (lossy)](#simple-file-format-lossy)
   686  and [Simple file format (lossless)](#simple-file-format-lossless) respectively.
   687  
   688  #### Color profile
   689  
   690       0                   1                   2                   3
   691       0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
   692      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   693      |                      ChunkHeader('ICCP')                      |
   694      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   695      |                       Color Profile                           |
   696      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   697  
   698  Color Profile: _Chunk Size_ bytes
   699  
   700  : ICC profile.
   701  
   702  This chunk MUST appear before the image data.
   703  
   704  There SHOULD be at most one such chunk. If there are more such chunks, readers
   705  MAY ignore all except the first one.
   706  See the [ICC Specification][iccspec] for details.
   707  
   708  If this chunk is not present, sRGB SHOULD be assumed.
   709  
   710  #### Metadata
   711  
   712  Metadata can be stored in 'EXIF' or 'XMP ' chunks.
   713  
   714  There SHOULD be at most one chunk of each type ('EXIF' and 'XMP '). If there
   715  are more such chunks, readers MAY ignore all except the first one. Also, a file
   716  may possibly contain both 'EXIF' and 'XMP ' chunks.
   717  
   718  The chunks are defined as follows:
   719  
   720  EXIF chunk:
   721  
   722       0                   1                   2                   3
   723       0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
   724      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   725      |                      ChunkHeader('EXIF')                      |
   726      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   727      |                        EXIF Metadata                          |
   728      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   729  
   730  EXIF Metadata: _Chunk Size_ bytes
   731  
   732  : image metadata in EXIF format.
   733  
   734  
   735  XMP chunk:
   736  
   737       0                   1                   2                   3
   738       0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
   739      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   740      |                      ChunkHeader('XMP ')                      |
   741      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   742      |                        XMP Metadata                           |
   743      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   744  
   745  XMP Metadata: _Chunk Size_ bytes
   746  
   747  : image metadata in XMP format.
   748  
   749  Additional guidance about handling metadata can be found in the
   750  Metadata Working Group's [Guidelines for Handling Metadata][metadata].
   751  
   752  #### Unknown Chunks _\[status: experimental\]_
   753  
   754  A RIFF chunk (described in [this](#terminology-amp-basics) section) whose _chunk
   755  tag_ is different from any of the chunks described in this document, is
   756  considered an _unknown chunk_.
   757  
   758  **Rationale**: Allowing unknown chunks gives a provision for future extension
   759  of the format, and also allows storage of any application-specific data.
   760  
   761  A file MAY contain unknown chunks:
   762  
   763    * At the end of the file as described in [Extended WebP file
   764      header](#extended_header) section.
   765    * At the end of FRGM and ANMF chunks as described in [Fragments](#fragments)
   766      and [Animation](#animation) sections.
   767  
   768  Readers SHOULD ignore these chunks. Writers SHOULD preserve them in their
   769  original order (unless they specifically intend to modify these chunks).
   770  
   771  ### Assembling the Canvas from fragments/frames
   772  
   773  Here we provide an overview of how a reader should assemble a canvas in case
   774  of a fragmented-image and in case of an animated image. The notation
   775  _VP8X.field_ means the field in the 'VP8X' chunk with the same description.
   776  
   777  Displaying a _fragmented image_ canvas MUST be equivalent to the following
   778  pseudocode: _\[status: experimental\]_
   779  
   780  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   781  assert VP8X.flags.hasFragments
   782  canvas ← new black image of size VP8X.canvasWidth x VP8X.canvasHeight.
   783  frgm_params ← nil
   784  for chunk in image_data:
   785      assert chunk.tag is "FRGM"
   786      frgm_params.fragmentX = Fragment X
   787      frgm_params.fragmentY = Fragment Y
   788      for subchunk in 'Fragment Data':
   789          if subchunk.tag == "ALPH":
   790              assert alpha subchunks not found in 'Fragment Data' earlier
   791              frgm_params.alpha = alpha_data
   792          else if subchunk.tag == "VP8 " OR subchunk.tag == "VP8L":
   793              assert bitstream subchunks not found in 'Fragment Data' earlier
   794              frgm_params.bitstream = bitstream_data
   795      frgm_params.fragmentWidth = Width extracted from bitstream subchunk
   796      frgm_params.fragmentHeight = Height extracted from bitstream subchunk
   797      assert VP8X.canvasWidth >=
   798          frgm_params.fragmentX + frgm_params.fragmentWidth
   799      assert VP8X.canvasHeight >=
   800          frgm_params.fragmentY + frgm_params.fragmentHeight
   801      assert fragment has the properties mentioned in "Image Fragments" section.
   802      render fragment with frame_params.alpha and frame_params.bitstream on canvas
   803      with top-left corner in (frgm_params.fragmentX, frgm_params.fragmentY).
   804  canvas contains the decoded canvas.
   805  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   806  
   807  Displaying an _animated image_ canvas MUST be equivalent to the following
   808  pseudocode:
   809  
   810  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   811  assert VP8X.flags.hasAnimation
   812  canvas ← new image of size VP8X.canvasWidth x VP8X.canvasHeight with
   813  background color ANIM.background_color.
   814  loop_count ← ANIM.loopCount
   815  dispose_method ← ANIM.disposeMethod
   816  if loop_count == 0:
   817      loop_count = ∞
   818  frame_params ← nil
   819  for loop = 0, ..., loop_count - 1
   820      assert next chunk in image_data is ANMF
   821      frame_params.frameX = Frame X
   822      frame_params.frameY = Frame Y
   823      frame_params.frameWidth = Frame Width Minus One + 1
   824      frame_params.frameHeight = Frame Height Minus One + 1
   825      frame_params.frameDuration = Frame Duration
   826      assert VP8X.canvasWidth >= frame_params.frameX + frame_params.frameWidth
   827      assert VP8X.canvasHeight >= frame_params.frameY + frame_params.frameHeight
   828      if VP8X.flags.hasFragments and first subchunk in 'Frame Data' is FRGM
   829          // Fragmented frame.
   830          frame_params.{bitstream,alpha} = canvas decoded from subchunks in
   831                                           'Frame Data' as per the pseudocode for
   832                                           _fragmented image_ above.
   833      else
   834          // Non-fragmented frame.
   835          for subchunk in 'Frame Data':
   836              if subchunk.tag == "ALPH":
   837                  assert alpha subchunks not found in 'Frame Data' earlier
   838                  frame_params.alpha = alpha_data
   839              else if subchunk.tag == "VP8 " OR subchunk.tag == "VP8L":
   840                  assert bitstream subchunks not found in 'Frame Data' earlier
   841                  frame_params.bitstream = bitstream_data
   842      render frame with frame_params.alpha and frame_params.bitstream on canvas
   843      with top-left corner in (frame_params.frameX, frame_params.frameY), using
   844      dispose method dispose_method.
   845      Show the contents of the image for frame_params.frameDuration * 1ms.
   846  canvas contains the decoded canvas.
   847  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   848  
   849  Example file layouts
   850  --------------------
   851  
   852  A lossy encoded image with alpha may look as follows:
   853  
   854  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   855  RIFF/WEBP
   856  +- VP8X (descriptions of features used)
   857  +- ALPH (alpha bitstream)
   858  +- VP8 (bitstream)
   859  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   860  
   861  A losslessly encoded image may look as follows:
   862  
   863  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   864  RIFF/WEBP
   865  +- VP8X (descriptions of features used)
   866  +- XYZW (unknown chunk)
   867  +- VP8L (lossless bitstream)
   868  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   869  
   870  A lossless image with ICC profile and XMP metadata may
   871  look as follows:
   872  
   873  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   874  RIFF/WEBP
   875  +- VP8X (descriptions of features used)
   876  +- ICCP (color profile)
   877  +- VP8L (lossless bitstream)
   878  +- XMP  (metadata)
   879  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   880  
   881  A fragmented image may look as follows:
   882  
   883  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   884  RIFF/WEBP
   885  +- VP8X (descriptions of features used)
   886  +- FRGM (fragment1 parameters + data)
   887  +- FRGM (fragment2 parameters + data)
   888  +- FRGM (fragment3 parameters + data)
   889  +- FRGM (fragment4 parameters + data)
   890  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   891  
   892  An animated image with EXIF metadata may look as follows:
   893  
   894  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   895  RIFF/WEBP
   896  +- VP8X (descriptions of features used)
   897  +- ANIM (global animation parameters)
   898  +- ANMF (frame1 parameters + data)
   899  +- ANMF (frame2 parameters + data)
   900  +- ANMF (frame3 parameters + data)
   901  +- ANMF (frame4 parameters + data)
   902  +- EXIF (metadata)
   903  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   904  
   905  [vp8spec]:  http://tools.ietf.org/html/rfc6386
   906  [webpllspec]: https://gerrit.chromium.org/gerrit/gitweb?p=webm/libwebp.git;a=blob;f=doc/webp-lossless-bitstream-spec.txt;hb=master
   907  [iccspec]: http://www.color.org/icc_specs2.xalter
   908  [metadata]: http://www.metadataworkinggroup.org/pdf/mwg_guidance.pdf
   909  [rfc 2119]: http://tools.ietf.org/html/rfc2119