Blame doc/webp-lossless-bitstream-spec.txt

Packit 9c6abc
Packit 9c6abc
Packit 9c6abc
Although you may be viewing an alternate representation, this document
Packit 9c6abc
is sourced in Markdown, a light-duty markup scheme, and is optimized for
Packit 9c6abc
the [kramdown](http://kramdown.rubyforge.org/) transformer.
Packit 9c6abc
Packit 9c6abc
See the accompanying README. External link targets are referenced at the
Packit 9c6abc
end of this file.
Packit 9c6abc
Packit 9c6abc
-->
Packit 9c6abc
Packit 9c6abc
Specification for WebP Lossless Bitstream
Packit 9c6abc
=========================================
Packit 9c6abc
Packit 9c6abc
_Jyrki Alakuijala, Ph.D., Google, Inc., 2012-06-19_
Packit 9c6abc
Packit 9c6abc
Paragraphs marked as \[AMENDED\] were amended on 2014-09-16.
Packit 9c6abc
Packit 9c6abc
Abstract
Packit 9c6abc
--------
Packit 9c6abc
Packit 9c6abc
WebP lossless is an image format for lossless compression of ARGB
Packit 9c6abc
images. The lossless format stores and restores the pixel values
Packit 9c6abc
exactly, including the color values for zero alpha pixels. The
Packit 9c6abc
format uses subresolution images, recursively embedded into the format
Packit 9c6abc
itself, for storing statistical data about the images, such as the used
Packit 9c6abc
entropy codes, spatial predictors, color space conversion, and color
Packit 9c6abc
table. LZ77, Huffman coding, and a color cache are used for compression
Packit 9c6abc
of the bulk data. Decoding speeds faster than PNG have been
Packit 9c6abc
demonstrated, as well as 25% denser compression than can be achieved
Packit 9c6abc
using today's PNG format.
Packit 9c6abc
Packit 9c6abc
Packit 9c6abc
* TOC placeholder
Packit 9c6abc
{:toc}
Packit 9c6abc
Packit 9c6abc
Packit 9c6abc
Nomenclature
Packit 9c6abc
------------
Packit 9c6abc
Packit 9c6abc
ARGB
Packit 9c6abc
: A pixel value consisting of alpha, red, green, and blue values.
Packit 9c6abc
Packit 9c6abc
ARGB image
Packit 9c6abc
: A two-dimensional array containing ARGB pixels.
Packit 9c6abc
Packit 9c6abc
color cache
Packit 9c6abc
: A small hash-addressed array to store recently used colors, to be able
Packit 9c6abc
  to recall them with shorter codes.
Packit 9c6abc
Packit 9c6abc
color indexing image
Packit 9c6abc
: A one-dimensional image of colors that can be indexed using a small
Packit 9c6abc
  integer (up to 256 within WebP lossless).
Packit 9c6abc
Packit 9c6abc
color transform image
Packit 9c6abc
: A two-dimensional subresolution image containing data about
Packit 9c6abc
  correlations of color components.
Packit 9c6abc
Packit 9c6abc
distance mapping
Packit 9c6abc
: Changes LZ77 distances to have the smallest values for pixels in 2D
Packit 9c6abc
  proximity.
Packit 9c6abc
Packit 9c6abc
entropy image
Packit 9c6abc
: A two-dimensional subresolution image indicating which entropy coding
Packit 9c6abc
  should be used in a respective square in the image, i.e., each pixel
Packit 9c6abc
  is a meta Huffman code.
Packit 9c6abc
Packit 9c6abc
Huffman code
Packit 9c6abc
: A classic way to do entropy coding where a smaller number of bits are
Packit 9c6abc
  used for more frequent codes.
Packit 9c6abc
Packit 9c6abc
LZ77
Packit 9c6abc
: Dictionary-based sliding window compression algorithm that either
Packit 9c6abc
  emits symbols or describes them as sequences of past symbols.
Packit 9c6abc
Packit 9c6abc
meta Huffman code
Packit 9c6abc
: A small integer (up to 16 bits) that indexes an element in the meta
Packit 9c6abc
  Huffman table.
Packit 9c6abc
Packit 9c6abc
predictor image
Packit 9c6abc
: A two-dimensional subresolution image indicating which spatial
Packit 9c6abc
  predictor is used for a particular square in the image.
Packit 9c6abc
Packit 9c6abc
prefix coding
Packit 9c6abc
: A way to entropy code larger integers that codes a few bits of the
Packit 9c6abc
  integer using an entropy code and codifies the remaining bits raw.
Packit 9c6abc
  This allows for the descriptions of the entropy codes to remain
Packit 9c6abc
  relatively small even when the range of symbols is large.
Packit 9c6abc
Packit 9c6abc
scan-line order
Packit 9c6abc
: A processing order of pixels, left-to-right, top-to-bottom, starting
Packit 9c6abc
  from the left-hand-top pixel, proceeding to the right. Once a row is
Packit 9c6abc
  completed, continue from the left-hand column of the next row.
Packit 9c6abc
Packit 9c6abc
Packit 9c6abc
1 Introduction
Packit 9c6abc
--------------
Packit 9c6abc
Packit 9c6abc
This document describes the compressed data representation of a WebP
Packit 9c6abc
lossless image. It is intended as a detailed reference for WebP lossless
Packit 9c6abc
encoder and decoder implementation.
Packit 9c6abc
Packit 9c6abc
In this document, we extensively use C programming language syntax to
Packit 9c6abc
describe the bitstream, and assume the existence of a function for
Packit 9c6abc
reading bits, `ReadBits(n)`. The bytes are read in the natural order of
Packit 9c6abc
the stream containing them, and bits of each byte are read in
Packit 9c6abc
least-significant-bit-first order. When multiple bits are read at the
Packit 9c6abc
same time, the integer is constructed from the original data in the
Packit 9c6abc
original order. The most significant bits of the returned integer are
Packit 9c6abc
also the most significant bits of the original data. Thus the statement
Packit 9c6abc
Packit 9c6abc
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Packit 9c6abc
b = ReadBits(2);
Packit 9c6abc
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Packit 9c6abc
Packit 9c6abc
is equivalent with the two statements below:
Packit 9c6abc
Packit 9c6abc
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Packit 9c6abc
b = ReadBits(1);
Packit 9c6abc
b |= ReadBits(1) << 1;
Packit 9c6abc
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Packit 9c6abc
Packit 9c6abc
We assume that each color component (e.g. alpha, red, blue and green) is
Packit 9c6abc
represented using an 8-bit byte. We define the corresponding type as
Packit 9c6abc
uint8. A whole ARGB pixel is represented by a type called uint32, an
Packit 9c6abc
unsigned integer consisting of 32 bits. In the code showing the behavior
Packit 9c6abc
of the transformations, alpha value is codified in bits 31..24, red in
Packit 9c6abc
bits 23..16, green in bits 15..8 and blue in bits 7..0, but
Packit 9c6abc
implementations of the format are free to use another representation
Packit 9c6abc
internally.
Packit 9c6abc
Packit 9c6abc
Broadly, a WebP lossless image contains header data, transform
Packit 9c6abc
information and actual image data. Headers contain width and height of
Packit 9c6abc
the image. A WebP lossless image can go through four different types of
Packit 9c6abc
transformation before being entropy encoded. The transform information
Packit 9c6abc
in the bitstream contains the data required to apply the respective
Packit 9c6abc
inverse transforms.
Packit 9c6abc
Packit 9c6abc
Packit 9c6abc
2 RIFF Header
Packit 9c6abc
-------------
Packit 9c6abc
Packit 9c6abc
The beginning of the header has the RIFF container. This consists of the
Packit 9c6abc
following 21 bytes:
Packit 9c6abc
Packit 9c6abc
   1. String "RIFF"
Packit 9c6abc
   2. A little-endian 32 bit value of the block length, the whole size
Packit 9c6abc
      of the block controlled by the RIFF header. Normally this equals
Packit 9c6abc
      the payload size (file size minus 8 bytes: 4 bytes for the 'RIFF'
Packit 9c6abc
      identifier and 4 bytes for storing the value itself).
Packit 9c6abc
   3. String "WEBP" (RIFF container name).
Packit 9c6abc
   4. String "VP8L" (chunk tag for lossless encoded image data).
Packit 9c6abc
   5. A little-endian 32-bit value of the number of bytes in the
Packit 9c6abc
      lossless stream.
Packit 9c6abc
   6. One byte signature 0x2f.
Packit 9c6abc
Packit 9c6abc
The first 28 bits of the bitstream specify the width and height of the
Packit 9c6abc
image. Width and height are decoded as 14-bit integers as follows:
Packit 9c6abc
Packit 9c6abc
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Packit 9c6abc
int image_width = ReadBits(14) + 1;
Packit 9c6abc
int image_height = ReadBits(14) + 1;
Packit 9c6abc
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Packit 9c6abc
Packit 9c6abc
The 14-bit dynamics for image size limit the maximum size of a WebP
Packit 9c6abc
lossless image to 16384✕16384 pixels.
Packit 9c6abc
Packit 9c6abc
The alpha_is_used bit is a hint only, and should not impact decoding.
Packit 9c6abc
It should be set to 0 when all alpha values are 255 in the picture, and
Packit 9c6abc
1 otherwise.
Packit 9c6abc
Packit 9c6abc
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Packit 9c6abc
int alpha_is_used = ReadBits(1);
Packit 9c6abc
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Packit 9c6abc
Packit 9c6abc
The version_number is a 3 bit code that must be set to 0. Any other value
Packit 9c6abc
should be treated as an error. \[AMENDED\]
Packit 9c6abc
Packit 9c6abc
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Packit 9c6abc
int version_number = ReadBits(3);
Packit 9c6abc
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Packit 9c6abc
Packit 9c6abc
Packit 9c6abc
3 Transformations
Packit 9c6abc
-----------------
Packit 9c6abc
Packit 9c6abc
Transformations are reversible manipulations of the image data that can
Packit 9c6abc
reduce the remaining symbolic entropy by modeling spatial and color
Packit 9c6abc
correlations. Transformations can make the final compression more dense.
Packit 9c6abc
Packit 9c6abc
An image can go through four types of transformation. A 1 bit indicates
Packit 9c6abc
the presence of a transform. Each transform is allowed to be used only
Packit 9c6abc
once. The transformations are used only for the main level ARGB image:
Packit 9c6abc
the subresolution images have no transforms, not even the 0 bit
Packit 9c6abc
indicating the end-of-transforms.
Packit 9c6abc
Packit 9c6abc
Typically an encoder would use these transforms to reduce the Shannon
Packit 9c6abc
entropy in the residual image. Also, the transform data can be decided
Packit 9c6abc
based on entropy minimization.
Packit 9c6abc
Packit 9c6abc
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Packit 9c6abc
while (ReadBits(1)) {  // Transform present.
Packit 9c6abc
  // Decode transform type.
Packit 9c6abc
  enum TransformType transform_type = ReadBits(2);
Packit 9c6abc
  // Decode transform data.
Packit 9c6abc
  ...
Packit 9c6abc
}
Packit 9c6abc
Packit 9c6abc
// Decode actual image data (Section 4).
Packit 9c6abc
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Packit 9c6abc
Packit 9c6abc
If a transform is present then the next two bits specify the transform
Packit 9c6abc
type. There are four types of transforms.
Packit 9c6abc
Packit 9c6abc
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Packit 9c6abc
enum TransformType {
Packit 9c6abc
  PREDICTOR_TRANSFORM             = 0,
Packit 9c6abc
  COLOR_TRANSFORM                 = 1,
Packit 9c6abc
  SUBTRACT_GREEN                  = 2,
Packit 9c6abc
  COLOR_INDEXING_TRANSFORM        = 3,
Packit 9c6abc
};
Packit 9c6abc
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Packit 9c6abc
Packit 9c6abc
The transform type is followed by the transform data. Transform data
Packit 9c6abc
contains the information required to apply the inverse transform and
Packit 9c6abc
depends on the transform type. Next we describe the transform data for
Packit 9c6abc
different types.
Packit 9c6abc
Packit 9c6abc
Packit 9c6abc
### Predictor Transform
Packit 9c6abc
Packit 9c6abc
The predictor transform can be used to reduce entropy by exploiting the
Packit 9c6abc
fact that neighboring pixels are often correlated. In the predictor
Packit 9c6abc
transform, the current pixel value is predicted from the pixels already
Packit 9c6abc
decoded (in scan-line order) and only the residual value (actual -
Packit 9c6abc
predicted) is encoded. The _prediction mode_ determines the type of
Packit 9c6abc
prediction to use. We divide the image into squares and all the pixels
Packit 9c6abc
in a square use same prediction mode.
Packit 9c6abc
Packit 9c6abc
The first 3 bits of prediction data define the block width and height in
Packit 9c6abc
number of bits. The number of block columns, `block_xsize`, is used in
Packit 9c6abc
indexing two-dimensionally.
Packit 9c6abc
Packit 9c6abc
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Packit 9c6abc
int size_bits = ReadBits(3) + 2;
Packit 9c6abc
int block_width = (1 << size_bits);
Packit 9c6abc
int block_height = (1 << size_bits);
Packit 9c6abc
#define DIV_ROUND_UP(num, den) ((num) + (den) - 1) / (den))
Packit 9c6abc
int block_xsize = DIV_ROUND_UP(image_width, 1 << size_bits);
Packit 9c6abc
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Packit 9c6abc
Packit 9c6abc
The transform data contains the prediction mode for each block of the
Packit 9c6abc
image. All the `block_width * block_height` pixels of a block use same
Packit 9c6abc
prediction mode. The prediction modes are treated as pixels of an image
Packit 9c6abc
and encoded using the same techniques described in
Packit 9c6abc
[Chapter 4](#image-data).
Packit 9c6abc
Packit 9c6abc
For a pixel _x, y_, one can compute the respective filter block address
Packit 9c6abc
by:
Packit 9c6abc
Packit 9c6abc
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Packit 9c6abc
int block_index = (y >> size_bits) * block_xsize +
Packit 9c6abc
                  (x >> size_bits);
Packit 9c6abc
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Packit 9c6abc
Packit 9c6abc
There are 14 different prediction modes. In each prediction mode, the
Packit 9c6abc
current pixel value is predicted from one or more neighboring pixels
Packit 9c6abc
whose values are already known.
Packit 9c6abc
Packit 9c6abc
We choose the neighboring pixels (TL, T, TR, and L) of the current pixel
Packit 9c6abc
(P) as follows:
Packit 9c6abc
Packit 9c6abc
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Packit 9c6abc
O    O    O    O    O    O    O    O    O    O    O
Packit 9c6abc
O    O    O    O    O    O    O    O    O    O    O
Packit 9c6abc
O    O    O    O    TL   T    TR   O    O    O    O
Packit 9c6abc
O    O    O    O    L    P    X    X    X    X    X
Packit 9c6abc
X    X    X    X    X    X    X    X    X    X    X
Packit 9c6abc
X    X    X    X    X    X    X    X    X    X    X
Packit 9c6abc
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Packit 9c6abc
Packit 9c6abc
where TL means top-left, T top, TR top-right, L left pixel.
Packit 9c6abc
At the time of predicting a value for P, all pixels O, TL, T, TR and L
Packit 9c6abc
have been already processed, and pixel P and all pixels X are unknown.
Packit 9c6abc
Packit 9c6abc
Given the above neighboring pixels, the different prediction modes are
Packit 9c6abc
defined as follows.
Packit 9c6abc
Packit 9c6abc
| Mode   | Predicted value of each channel of the current pixel    |
Packit 9c6abc
| ------ | ------------------------------------------------------- |
Packit 9c6abc
|  0     | 0xff000000 (represents solid black color in ARGB)       |
Packit 9c6abc
|  1     | L                                                       |
Packit 9c6abc
|  2     | T                                                       |
Packit 9c6abc
|  3     | TR                                                      |
Packit 9c6abc
|  4     | TL                                                      |
Packit 9c6abc
|  5     | Average2(Average2(L, TR), T)                            |
Packit 9c6abc
|  6     | Average2(L, TL)                                         |
Packit 9c6abc
|  7     | Average2(L, T)                                          |
Packit 9c6abc
|  8     | Average2(TL, T)                                         |
Packit 9c6abc
|  9     | Average2(T, TR)                                         |
Packit 9c6abc
| 10     | Average2(Average2(L, TL), Average2(T, TR))              |
Packit 9c6abc
| 11     | Select(L, T, TL)                                        |
Packit 9c6abc
| 12     | ClampAddSubtractFull(L, T, TL)                          |
Packit 9c6abc
| 13     | ClampAddSubtractHalf(Average2(L, T), TL)                |
Packit 9c6abc
Packit 9c6abc
Packit 9c6abc
`Average2` is defined as follows for each ARGB component:
Packit 9c6abc
Packit 9c6abc
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Packit 9c6abc
uint8 Average2(uint8 a, uint8 b) {
Packit 9c6abc
  return (a + b) / 2;
Packit 9c6abc
}
Packit 9c6abc
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Packit 9c6abc
Packit 9c6abc
The Select predictor is defined as follows:
Packit 9c6abc
Packit 9c6abc
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Packit 9c6abc
uint32 Select(uint32 L, uint32 T, uint32 TL) {
Packit 9c6abc
  // L = left pixel, T = top pixel, TL = top left pixel.
Packit 9c6abc
Packit 9c6abc
  // ARGB component estimates for prediction.
Packit 9c6abc
  int pAlpha = ALPHA(L) + ALPHA(T) - ALPHA(TL);
Packit 9c6abc
  int pRed = RED(L) + RED(T) - RED(TL);
Packit 9c6abc
  int pGreen = GREEN(L) + GREEN(T) - GREEN(TL);
Packit 9c6abc
  int pBlue = BLUE(L) + BLUE(T) - BLUE(TL);
Packit 9c6abc
Packit 9c6abc
  // Manhattan distances to estimates for left and top pixels.
Packit 9c6abc
  int pL = abs(pAlpha - ALPHA(L)) + abs(pRed - RED(L)) +
Packit 9c6abc
           abs(pGreen - GREEN(L)) + abs(pBlue - BLUE(L));
Packit 9c6abc
  int pT = abs(pAlpha - ALPHA(T)) + abs(pRed - RED(T)) +
Packit 9c6abc
           abs(pGreen - GREEN(T)) + abs(pBlue - BLUE(T));
Packit 9c6abc
Packit 9c6abc
  // Return either left or top, the one closer to the prediction.
Packit 9c6abc
  if (pL < pT) {     // \[AMENDED\]
Packit 9c6abc
    return L;
Packit 9c6abc
  } else {
Packit 9c6abc
    return T;
Packit 9c6abc
  }
Packit 9c6abc
}
Packit 9c6abc
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Packit 9c6abc
Packit 9c6abc
The functions `ClampAddSubtractFull` and `ClampAddSubtractHalf` are
Packit 9c6abc
performed for each ARGB component as follows:
Packit 9c6abc
Packit 9c6abc
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Packit 9c6abc
// Clamp the input value between 0 and 255.
Packit 9c6abc
int Clamp(int a) {
Packit 9c6abc
  return (a < 0) ? 0 : (a > 255) ?  255 : a;
Packit 9c6abc
}
Packit 9c6abc
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Packit 9c6abc
Packit 9c6abc
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Packit 9c6abc
int ClampAddSubtractFull(int a, int b, int c) {
Packit 9c6abc
  return Clamp(a + b - c);
Packit 9c6abc
}
Packit 9c6abc
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Packit 9c6abc
Packit 9c6abc
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Packit 9c6abc
int ClampAddSubtractHalf(int a, int b) {
Packit 9c6abc
  return Clamp(a + (a - b) / 2);
Packit 9c6abc
}
Packit 9c6abc
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Packit 9c6abc
Packit 9c6abc
There are special handling rules for some border pixels. If there is a
Packit 9c6abc
prediction transform, regardless of the mode \[0..13\] for these pixels,
Packit 9c6abc
the predicted value for the left-topmost pixel of the image is
Packit 9c6abc
0xff000000, L-pixel for all pixels on the top row, and T-pixel for all
Packit 9c6abc
pixels on the leftmost column.
Packit 9c6abc
Packit 9c6abc
Addressing the TR-pixel for pixels on the rightmost column is
Packit 9c6abc
exceptional. The pixels on the rightmost column are predicted by using
Packit 9c6abc
the modes \[0..13\] just like pixels not on border, but by using the
Packit 9c6abc
leftmost pixel on the same row as the current TR-pixel. The TR-pixel
Packit 9c6abc
offset in memory is the same for border and non-border pixels.
Packit 9c6abc
Packit 9c6abc
Packit 9c6abc
### Color Transform
Packit 9c6abc
Packit 9c6abc
The goal of the color transform is to decorrelate the R, G and B values
Packit 9c6abc
of each pixel. Color transform keeps the green (G) value as it is,
Packit 9c6abc
transforms red (R) based on green and transforms blue (B) based on green
Packit 9c6abc
and then based on red.
Packit 9c6abc
Packit 9c6abc
As is the case for the predictor transform, first the image is divided
Packit 9c6abc
into blocks and the same transform mode is used for all the pixels in a
Packit 9c6abc
block. For each block there are three types of color transform elements.
Packit 9c6abc
Packit 9c6abc
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Packit 9c6abc
typedef struct {
Packit 9c6abc
  uint8 green_to_red;
Packit 9c6abc
  uint8 green_to_blue;
Packit 9c6abc
  uint8 red_to_blue;
Packit 9c6abc
} ColorTransformElement;
Packit 9c6abc
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Packit 9c6abc
Packit 9c6abc
The actual color transformation is done by defining a color transform
Packit 9c6abc
delta. The color transform delta depends on the `ColorTransformElement`,
Packit 9c6abc
which is the same for all the pixels in a particular block. The delta is
Packit 9c6abc
added during color transform. The inverse color transform then is just
Packit 9c6abc
subtracting those deltas.
Packit 9c6abc
Packit 9c6abc
The color transform function is defined as follows:
Packit 9c6abc
Packit 9c6abc
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Packit 9c6abc
void ColorTransform(uint8 red, uint8 blue, uint8 green,
Packit 9c6abc
                    ColorTransformElement *trans,
Packit 9c6abc
                    uint8 *new_red, uint8 *new_blue) {
Packit 9c6abc
  // Transformed values of red and blue components
Packit 9c6abc
  uint32 tmp_red = red;
Packit 9c6abc
  uint32 tmp_blue = blue;
Packit 9c6abc
Packit 9c6abc
  // Applying transform is just adding the transform deltas
Packit 9c6abc
  tmp_red  += ColorTransformDelta(trans->green_to_red, green);
Packit 9c6abc
  tmp_blue += ColorTransformDelta(trans->green_to_blue, green);
Packit 9c6abc
  tmp_blue += ColorTransformDelta(trans->red_to_blue, red);
Packit 9c6abc
Packit 9c6abc
  *new_red = tmp_red & 0xff;
Packit 9c6abc
  *new_blue = tmp_blue & 0xff;
Packit 9c6abc
}
Packit 9c6abc
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Packit 9c6abc
Packit 9c6abc
`ColorTransformDelta` is computed using a signed 8-bit integer
Packit 9c6abc
representing a 3.5-fixed-point number, and a signed 8-bit RGB color
Packit 9c6abc
channel (c) \[-128..127\] and is defined as follows:
Packit 9c6abc
Packit 9c6abc
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Packit 9c6abc
int8 ColorTransformDelta(int8 t, int8 c) {
Packit 9c6abc
  return (t * c) >> 5;
Packit 9c6abc
}
Packit 9c6abc
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Packit 9c6abc
Packit 9c6abc
A conversion from the 8-bit unsigned representation (uint8) to the 8-bit
Packit 9c6abc
signed one (int8) is required before calling ColorTransformDelta().
Packit 9c6abc
It should be performed using 8-bit two's complement (that is: uint8 range
Packit 9c6abc
\[128-255\] is mapped to the \[-128, -1\] range of its converted int8 value).
Packit 9c6abc
Packit 9c6abc
The multiplication is to be done using more precision (with at least
Packit 9c6abc
16-bit dynamics). The sign extension property of the shift operation
Packit 9c6abc
does not matter here: only the lowest 8 bits are used from the result,
Packit 9c6abc
and there the sign extension shifting and unsigned shifting are
Packit 9c6abc
consistent with each other.
Packit 9c6abc
Packit 9c6abc
Now we describe the contents of color transform data so that decoding
Packit 9c6abc
can apply the inverse color transform and recover the original red and
Packit 9c6abc
blue values. The first 3 bits of the color transform data contain the
Packit 9c6abc
width and height of the image block in number of bits, just like the
Packit 9c6abc
predictor transform:
Packit 9c6abc
Packit 9c6abc
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Packit 9c6abc
int size_bits = ReadBits(3) + 2;
Packit 9c6abc
int block_width = 1 << size_bits;
Packit 9c6abc
int block_height = 1 << size_bits;
Packit 9c6abc
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Packit 9c6abc
Packit 9c6abc
The remaining part of the color transform data contains
Packit 9c6abc
`ColorTransformElement` instances corresponding to each block of the
Packit 9c6abc
image. `ColorTransformElement` instances are treated as pixels of an
Packit 9c6abc
image and encoded using the methods described in
Packit 9c6abc
[Chapter 4](#image-data).
Packit 9c6abc
Packit 9c6abc
During decoding, `ColorTransformElement` instances of the blocks are
Packit 9c6abc
decoded and the inverse color transform is applied on the ARGB values of
Packit 9c6abc
the pixels. As mentioned earlier, that inverse color transform is just
Packit 9c6abc
subtracting `ColorTransformElement` values from the red and blue
Packit 9c6abc
channels.
Packit 9c6abc
Packit 9c6abc
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Packit 9c6abc
void InverseTransform(uint8 red, uint8 green, uint8 blue,
Packit 9c6abc
                      ColorTransformElement *p,
Packit 9c6abc
                      uint8 *new_red, uint8 *new_blue) {
Packit 9c6abc
  // Applying inverse transform is just subtracting the
Packit 9c6abc
  // color transform deltas
Packit 9c6abc
  red  -= ColorTransformDelta(p->green_to_red_,  green);
Packit 9c6abc
  blue -= ColorTransformDelta(p->green_to_blue_, green);
Packit 9c6abc
  blue -= ColorTransformDelta(p->red_to_blue_, red & 0xff);
Packit 9c6abc
Packit 9c6abc
  *new_red = red & 0xff;
Packit 9c6abc
  *new_blue = blue & 0xff;
Packit 9c6abc
}
Packit 9c6abc
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Packit 9c6abc
Packit 9c6abc
Packit 9c6abc
### Subtract Green Transform
Packit 9c6abc
Packit 9c6abc
The subtract green transform subtracts green values from red and blue
Packit 9c6abc
values of each pixel. When this transform is present, the decoder needs
Packit 9c6abc
to add the green value to both red and blue. There is no data associated
Packit 9c6abc
with this transform. The decoder applies the inverse transform as
Packit 9c6abc
follows:
Packit 9c6abc
Packit 9c6abc
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Packit 9c6abc
void AddGreenToBlueAndRed(uint8 green, uint8 *red, uint8 *blue) {
Packit 9c6abc
  *red  = (*red  + green) & 0xff;
Packit 9c6abc
  *blue = (*blue + green) & 0xff;
Packit 9c6abc
}
Packit 9c6abc
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Packit 9c6abc
Packit 9c6abc
This transform is redundant as it can be modeled using the color
Packit 9c6abc
transform, but it is still often useful. Since it can extend the
Packit 9c6abc
dynamics of the color transform and there is no additional data here,
Packit 9c6abc
the subtract green transform can be coded using fewer bits than a
Packit 9c6abc
full-blown color transform.
Packit 9c6abc
Packit 9c6abc
Packit 9c6abc
### Color Indexing Transform
Packit 9c6abc
Packit 9c6abc
If there are not many unique pixel values, it may be more efficient to
Packit 9c6abc
create a color index array and replace the pixel values by the array's
Packit 9c6abc
indices. The color indexing transform achieves this. (In the context of
Packit 9c6abc
WebP lossless, we specifically do not call this a palette transform
Packit 9c6abc
because a similar but more dynamic concept exists in WebP lossless
Packit 9c6abc
encoding: color cache.)
Packit 9c6abc
Packit 9c6abc
The color indexing transform checks for the number of unique ARGB values
Packit 9c6abc
in the image. If that number is below a threshold (256), it creates an
Packit 9c6abc
array of those ARGB values, which is then used to replace the pixel
Packit 9c6abc
values with the corresponding index: the green channel of the pixels are
Packit 9c6abc
replaced with the index; all alpha values are set to 255; all red and
Packit 9c6abc
blue values to 0.
Packit 9c6abc
Packit 9c6abc
The transform data contains color table size and the entries in the
Packit 9c6abc
color table. The decoder reads the color indexing transform data as
Packit 9c6abc
follows:
Packit 9c6abc
Packit 9c6abc
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Packit 9c6abc
// 8 bit value for color table size
Packit 9c6abc
int color_table_size = ReadBits(8) + 1;
Packit 9c6abc
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Packit 9c6abc
Packit 9c6abc
The color table is stored using the image storage format itself. The
Packit 9c6abc
color table can be obtained by reading an image, without the RIFF
Packit 9c6abc
header, image size, and transforms, assuming a height of one pixel and
Packit 9c6abc
a width of `color_table_size`. The color table is always
Packit 9c6abc
subtraction-coded to reduce image entropy. The deltas of palette colors
Packit 9c6abc
contain typically much less entropy than the colors themselves, leading
Packit 9c6abc
to significant savings for smaller images. In decoding, every final
Packit 9c6abc
color in the color table can be obtained by adding the previous color
Packit 9c6abc
component values by each ARGB component separately, and storing the
Packit 9c6abc
least significant 8 bits of the result.
Packit 9c6abc
Packit 9c6abc
The inverse transform for the image is simply replacing the pixel values
Packit 9c6abc
(which are indices to the color table) with the actual color table
Packit 9c6abc
values. The indexing is done based on the green component of the ARGB
Packit 9c6abc
color.
Packit 9c6abc
Packit 9c6abc
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Packit 9c6abc
// Inverse transform
Packit 9c6abc
argb = color_table[GREEN(argb)];
Packit 9c6abc
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Packit 9c6abc
Packit 9c6abc
If the index is equal or larger than color_table_size, the argb color value
Packit 9c6abc
should be set to 0x00000000 (transparent black).  \[AMENDED\]
Packit 9c6abc
Packit 9c6abc
When the color table is small (equal to or less than 16 colors), several
Packit 9c6abc
pixels are bundled into a single pixel. The pixel bundling packs several
Packit 9c6abc
(2, 4, or 8) pixels into a single pixel, reducing the image width
Packit 9c6abc
respectively. Pixel bundling allows for a more efficient joint
Packit 9c6abc
distribution entropy coding of neighboring pixels, and gives some
Packit 9c6abc
arithmetic coding-like benefits to the entropy code, but it can only be
Packit 9c6abc
used when there are a small number of unique values.
Packit 9c6abc
Packit 9c6abc
`color_table_size` specifies how many pixels are combined together:
Packit 9c6abc
Packit 9c6abc
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Packit 9c6abc
int width_bits;
Packit 9c6abc
if (color_table_size <= 2) {
Packit 9c6abc
  width_bits = 3;
Packit 9c6abc
} else if (color_table_size <= 4) {
Packit 9c6abc
  width_bits = 2;
Packit 9c6abc
} else if (color_table_size <= 16) {
Packit 9c6abc
  width_bits = 1;
Packit 9c6abc
} else {
Packit 9c6abc
  width_bits = 0;
Packit 9c6abc
}
Packit 9c6abc
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Packit 9c6abc
Packit 9c6abc
`width_bits` has a value of 0, 1, 2 or 3. A value of 0 indicates no
Packit 9c6abc
pixel bundling to be done for the image. A value of 1 indicates that two
Packit 9c6abc
pixels are combined together, and each pixel has a range of \[0..15\]. A
Packit 9c6abc
value of 2 indicates that four pixels are combined together, and each
Packit 9c6abc
pixel has a range of \[0..3\]. A value of 3 indicates that eight pixels
Packit 9c6abc
are combined together and each pixel has a range of \[0..1\], i.e., a
Packit 9c6abc
binary value.
Packit 9c6abc
Packit 9c6abc
The values are packed into the green component as follows:
Packit 9c6abc
Packit 9c6abc
  * `width_bits` = 1: for every x value where x ≡ 0 (mod 2), a green
Packit 9c6abc
    value at x is positioned into the 4 least-significant bits of the
Packit 9c6abc
    green value at x / 2, a green value at x + 1 is positioned into the
Packit 9c6abc
    4 most-significant bits of the green value at x / 2.
Packit 9c6abc
  * `width_bits` = 2: for every x value where x ≡ 0 (mod 4), a green
Packit 9c6abc
    value at x is positioned into the 2 least-significant bits of the
Packit 9c6abc
    green value at x / 4, green values at x + 1 to x + 3 in order to the
Packit 9c6abc
    more significant bits of the green value at x / 4.
Packit 9c6abc
  * `width_bits` = 3: for every x value where x ≡ 0 (mod 8), a green
Packit 9c6abc
    value at x is positioned into the least-significant bit of the green
Packit 9c6abc
    value at x / 8, green values at x + 1 to x + 7 in order to the more
Packit 9c6abc
    significant bits of the green value at x / 8.
Packit 9c6abc
Packit 9c6abc
Packit 9c6abc
4 Image Data
Packit 9c6abc
------------
Packit 9c6abc
Packit 9c6abc
Image data is an array of pixel values in scan-line order.
Packit 9c6abc
Packit 9c6abc
### 4.1 Roles of Image Data
Packit 9c6abc
Packit 9c6abc
We use image data in five different roles:
Packit 9c6abc
Packit 9c6abc
  1. ARGB image: Stores the actual pixels of the image.
Packit 9c6abc
  1. Entropy image: Stores the
Packit 9c6abc
     [meta Huffman codes](#decoding-of-meta-huffman-codes). The red and green
Packit 9c6abc
     components of a pixel define the meta Huffman code used in a particular
Packit 9c6abc
     block of the ARGB image.
Packit 9c6abc
  1. Predictor image: Stores the metadata for [Predictor
Packit 9c6abc
     Transform](#predictor-transform). The green component of a pixel defines
Packit 9c6abc
     which of the 14 predictors is used within a particular block of the
Packit 9c6abc
     ARGB image.
Packit 9c6abc
  1. Color transform image. It is created by `ColorTransformElement` values
Packit 9c6abc
     (defined in [Color Transform](#color-transform)) for different blocks of
Packit 9c6abc
     the image. Each `ColorTransformElement` `'cte'` is treated as a pixel whose
Packit 9c6abc
     alpha component is `255`, red component is `cte.red_to_blue`, green
Packit 9c6abc
     component is `cte.green_to_blue` and blue component is `cte.green_to_red`.
Packit 9c6abc
  1. Color indexing image: An array of of size `color_table_size` (up to 256
Packit 9c6abc
     ARGB values) storing the metadata for the
Packit 9c6abc
     [Color Indexing Transform](#color-indexing-transform). This is stored as an
Packit 9c6abc
     image of width `color_table_size` and height `1`.
Packit 9c6abc
Packit 9c6abc
### 4.2 Encoding of Image data
Packit 9c6abc
Packit 9c6abc
The encoding of image data is independent of its role.
Packit 9c6abc
Packit 9c6abc
The image is first divided into a set of fixed-size blocks (typically 16x16
Packit 9c6abc
blocks). Each of these blocks are modeled using their own entropy codes. Also,
Packit 9c6abc
several blocks may share the same entropy codes.
Packit 9c6abc
Packit 9c6abc
**Rationale:** Storing an entropy code incurs a cost. This cost can be minimized
Packit 9c6abc
if statistically similar blocks share an entropy code, thereby storing that code
Packit 9c6abc
only once. For example, an encoder can find similar blocks by clustering them
Packit 9c6abc
using their statistical properties, or by repeatedly joining a pair of randomly
Packit 9c6abc
selected clusters when it reduces the overall amount of bits needed to encode
Packit 9c6abc
the image.
Packit 9c6abc
Packit 9c6abc
Each pixel is encoded using one of the three possible methods:
Packit 9c6abc
Packit 9c6abc
  1. Huffman coded literal: each channel (green, red, blue and alpha) is
Packit 9c6abc
     entropy-coded independently;
Packit 9c6abc
  2. LZ77 backward reference: a sequence of pixels are copied from elsewhere
Packit 9c6abc
     in the image; or
Packit 9c6abc
  3. Color cache code: using a short multiplicative hash code (color cache
Packit 9c6abc
     index) of a recently seen color.
Packit 9c6abc
Packit 9c6abc
The following sub-sections describe each of these in detail.
Packit 9c6abc
Packit 9c6abc
#### 4.2.1 Huffman Coded Literals
Packit 9c6abc
Packit 9c6abc
The pixel is stored as Huffman coded values of green, red, blue and alpha (in
Packit 9c6abc
that order). See [this section](#decoding-entropy-coded-image-data) for details.
Packit 9c6abc
Packit 9c6abc
#### 4.2.2 LZ77 Backward Reference
Packit 9c6abc
Packit 9c6abc
Backward references are tuples of _length_ and _distance code_:
Packit 9c6abc
Packit 9c6abc
  * Length indicates how many pixels in scan-line order are to be copied.
Packit 9c6abc
  * Distance code is a number indicating the position of a previously seen
Packit 9c6abc
    pixel, from which the pixels are to be copied. The exact mapping is
Packit 9c6abc
    described [below](#distance-mapping).
Packit 9c6abc
Packit 9c6abc
The length and distance values are stored using **LZ77 prefix coding**.
Packit 9c6abc
Packit 9c6abc
LZ77 prefix coding divides large integer values into two parts: the _prefix
Packit 9c6abc
code_ and the _extra bits_: the prefix code is stored using an entropy code,
Packit 9c6abc
while the extra bits are stored as they are (without an entropy code).
Packit 9c6abc
Packit 9c6abc
**Rationale**: This approach reduces the storage requirement for the entropy
Packit 9c6abc
code. Also, large values are usually rare, and so extra bits would be used for
Packit 9c6abc
very few values in the image. Thus, this approach results in a better
Packit 9c6abc
compression overall.
Packit 9c6abc
Packit 9c6abc
The following table denotes the prefix codes and extra bits used for storing
Packit 9c6abc
different range of values.
Packit 9c6abc
Packit 9c6abc
Note: The maximum backward reference length is limited to 4096. Hence, only the
Packit 9c6abc
first 24 prefix codes (with the respective extra bits) are meaningful for length
Packit 9c6abc
values. For distance values, however, all the 40 prefix codes are valid.
Packit 9c6abc
Packit 9c6abc
| Value range     | Prefix code | Extra bits |
Packit 9c6abc
| --------------- | ----------- | ---------- |
Packit 9c6abc
| 1               | 0           | 0          |
Packit 9c6abc
| 2               | 1           | 0          |
Packit 9c6abc
| 3               | 2           | 0          |
Packit 9c6abc
| 4               | 3           | 0          |
Packit 9c6abc
| 5..6            | 4           | 1          |
Packit 9c6abc
| 7..8            | 5           | 1          |
Packit 9c6abc
| 9..12           | 6           | 2          |
Packit 9c6abc
| 13..16          | 7           | 2          |
Packit 9c6abc
| ...             | ...         | ...        |
Packit 9c6abc
| 3072..4096      | 23          | 10         |
Packit 9c6abc
| ...             | ...         | ...        |
Packit 9c6abc
| 524289..786432  | 38          | 18         |
Packit 9c6abc
| 786433..1048576 | 39          | 18         |
Packit 9c6abc
Packit 9c6abc
The pseudocode to obtain a (length or distance) value from the prefix code is
Packit 9c6abc
as follows:
Packit 9c6abc
Packit 9c6abc
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Packit 9c6abc
if (prefix_code < 4) {
Packit 9c6abc
  return prefix_code + 1;
Packit 9c6abc
}
Packit 9c6abc
int extra_bits = (prefix_code - 2) >> 1;
Packit 9c6abc
int offset = (2 + (prefix_code & 1)) << extra_bits;
Packit 9c6abc
return offset + ReadBits(extra_bits) + 1;
Packit 9c6abc
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Packit 9c6abc
Packit 9c6abc
**Distance Mapping:**
Packit 9c6abc
{:#distance-mapping}
Packit 9c6abc
Packit 9c6abc
As noted previously, distance code is a number indicating the position of a
Packit 9c6abc
previously seen pixel, from which the pixels are to be copied. This sub-section
Packit 9c6abc
defines the mapping between a distance code and the position of a previous
Packit 9c6abc
pixel.
Packit 9c6abc
Packit 9c6abc
The distance codes larger than 120 denote the pixel-distance in scan-line
Packit 9c6abc
order, offset by 120.
Packit 9c6abc
Packit 9c6abc
The smallest distance codes \[1..120\] are special, and are reserved for a close
Packit 9c6abc
neighborhood of the current pixel. This neighborhood consists of 120 pixels:
Packit 9c6abc
Packit 9c6abc
  * Pixels that are 1 to 7 rows above the current pixel, and are up to 8 columns
Packit 9c6abc
    to the left or up to 7 columns to the right of the current pixel. \[Total
Packit 9c6abc
    such pixels = `7 * (8 + 1 + 7) = 112`\].
Packit 9c6abc
  * Pixels that are in same row as the current pixel, and are up to 8 columns to
Packit 9c6abc
    the left of the current pixel. \[`8` such pixels\].
Packit 9c6abc
Packit 9c6abc
The mapping between distance code `i` and the neighboring pixel offset
Packit 9c6abc
`(xi, yi)` is as follows:
Packit 9c6abc
Packit 9c6abc
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Packit 9c6abc
(0, 1),  (1, 0),  (1, 1),  (-1, 1), (0, 2),  (2, 0),  (1, 2),  (-1, 2),
Packit 9c6abc
(2, 1),  (-2, 1), (2, 2),  (-2, 2), (0, 3),  (3, 0),  (1, 3),  (-1, 3),
Packit 9c6abc
(3, 1),  (-3, 1), (2, 3),  (-2, 3), (3, 2),  (-3, 2), (0, 4),  (4, 0),
Packit 9c6abc
(1, 4),  (-1, 4), (4, 1),  (-4, 1), (3, 3),  (-3, 3), (2, 4),  (-2, 4),
Packit 9c6abc
(4, 2),  (-4, 2), (0, 5),  (3, 4),  (-3, 4), (4, 3),  (-4, 3), (5, 0),
Packit 9c6abc
(1, 5),  (-1, 5), (5, 1),  (-5, 1), (2, 5),  (-2, 5), (5, 2),  (-5, 2),
Packit 9c6abc
(4, 4),  (-4, 4), (3, 5),  (-3, 5), (5, 3),  (-5, 3), (0, 6),  (6, 0),
Packit 9c6abc
(1, 6),  (-1, 6), (6, 1),  (-6, 1), (2, 6),  (-2, 6), (6, 2),  (-6, 2),
Packit 9c6abc
(4, 5),  (-4, 5), (5, 4),  (-5, 4), (3, 6),  (-3, 6), (6, 3),  (-6, 3),
Packit 9c6abc
(0, 7),  (7, 0),  (1, 7),  (-1, 7), (5, 5),  (-5, 5), (7, 1),  (-7, 1),
Packit 9c6abc
(4, 6),  (-4, 6), (6, 4),  (-6, 4), (2, 7),  (-2, 7), (7, 2),  (-7, 2),
Packit 9c6abc
(3, 7),  (-3, 7), (7, 3),  (-7, 3), (5, 6),  (-5, 6), (6, 5),  (-6, 5),
Packit 9c6abc
(8, 0),  (4, 7),  (-4, 7), (7, 4),  (-7, 4), (8, 1),  (8, 2),  (6, 6),
Packit 9c6abc
(-6, 6), (8, 3),  (5, 7),  (-5, 7), (7, 5),  (-7, 5), (8, 4),  (6, 7),
Packit 9c6abc
(-6, 7), (7, 6),  (-7, 6), (8, 5),  (7, 7),  (-7, 7), (8, 6),  (8, 7)
Packit 9c6abc
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Packit 9c6abc
Packit 9c6abc
For example, distance code `1` indicates offset of `(0, 1)` for the neighboring
Packit 9c6abc
pixel, that is, the pixel above the current pixel (0-pixel difference in
Packit 9c6abc
X-direction and 1 pixel difference in Y-direction). Similarly, distance code
Packit 9c6abc
`3` indicates left-top pixel.
Packit 9c6abc
Packit 9c6abc
The decoder can convert a distances code 'i' to a scan-line order distance
Packit 9c6abc
'dist' as follows:
Packit 9c6abc
Packit 9c6abc
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Packit 9c6abc
(xi, yi) = distance_map[i]
Packit 9c6abc
dist = x + y * xsize
Packit 9c6abc
if (dist < 1) {
Packit 9c6abc
  dist = 1
Packit 9c6abc
}
Packit 9c6abc
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Packit 9c6abc
Packit 9c6abc
where 'distance_map' is the mapping noted above and `xsize` is the width of the
Packit 9c6abc
image in pixels.
Packit 9c6abc
Packit 9c6abc
Packit 9c6abc
#### 4.2.3 Color Cache Coding
Packit 9c6abc
Packit 9c6abc
Color cache stores a set of colors that have been recently used in the image.
Packit 9c6abc
Packit 9c6abc
**Rationale:** This way, the recently used colors can sometimes be referred to
Packit 9c6abc
more efficiently than emitting them using other two methods (described in
Packit 9c6abc
[4.2.1](#huffman-coded-literals) and [4.2.2](#lz77-backward-reference)).
Packit 9c6abc
Packit 9c6abc
Color cache codes are stored as follows. First, there is a 1-bit value that
Packit 9c6abc
indicates if the color cache is used. If this bit is 0, no color cache codes
Packit 9c6abc
exist, and they are not transmitted in the Huffman code that decodes the green
Packit 9c6abc
symbols and the length prefix codes. However, if this bit is 1, the color cache
Packit 9c6abc
size is read next:
Packit 9c6abc
Packit 9c6abc
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Packit 9c6abc
int color_cache_code_bits = ReadBits(4);
Packit 9c6abc
int color_cache_size = 1 << color_cache_code_bits;
Packit 9c6abc
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Packit 9c6abc
Packit 9c6abc
`color_cache_code_bits` defines the size of the color_cache by (1 <<
Packit 9c6abc
`color_cache_code_bits`). The range of allowed values for
Packit 9c6abc
`color_cache_code_bits` is \[1..11\]. Compliant decoders must indicate a
Packit 9c6abc
corrupted bitstream for other values.
Packit 9c6abc
Packit 9c6abc
A color cache is an array of size `color_cache_size`. Each entry
Packit 9c6abc
stores one ARGB color. Colors are looked up by indexing them by
Packit 9c6abc
(0x1e35a7bd * `color`) >> (32 - `color_cache_code_bits`). Only one
Packit 9c6abc
lookup is done in a color cache; there is no conflict resolution.
Packit 9c6abc
Packit 9c6abc
In the beginning of decoding or encoding of an image, all entries in all
Packit 9c6abc
color cache values are set to zero. The color cache code is converted to
Packit 9c6abc
this color at decoding time. The state of the color cache is maintained
Packit 9c6abc
by inserting every pixel, be it produced by backward referencing or as
Packit 9c6abc
literals, into the cache in the order they appear in the stream.
Packit 9c6abc
Packit 9c6abc
Packit 9c6abc
5 Entropy Code
Packit 9c6abc
--------------
Packit 9c6abc
Packit 9c6abc
### 5.1 Overview
Packit 9c6abc
Packit 9c6abc
Most of the data is coded using [canonical Huffman code][canonical_huff]. Hence,
Packit 9c6abc
the codes are transmitted by sending the _Huffman code lengths_, as opposed to
Packit 9c6abc
the actual _Huffman codes_.
Packit 9c6abc
Packit 9c6abc
In particular, the format uses **spatially-variant Huffman coding**. In other
Packit 9c6abc
words, different blocks of the image can potentially use different entropy
Packit 9c6abc
codes.
Packit 9c6abc
Packit 9c6abc
**Rationale**: Different areas of the image may have different characteristics. So, allowing them to use different entropy codes provides more flexibility and
Packit 9c6abc
potentially a better compression.
Packit 9c6abc
Packit 9c6abc
### 5.2 Details
Packit 9c6abc
Packit 9c6abc
The encoded image data consists of two parts:
Packit 9c6abc
Packit 9c6abc
  1. Meta Huffman codes
Packit 9c6abc
  1. Entropy-coded image data
Packit 9c6abc
Packit 9c6abc
#### 5.2.1 Decoding of Meta Huffman Codes
Packit 9c6abc
Packit 9c6abc
As noted earlier, the format allows the use of different Huffman codes for
Packit 9c6abc
different blocks of the image. _Meta Huffman codes_ are indexes identifying
Packit 9c6abc
which Huffman codes to use in different parts of the image.
Packit 9c6abc
Packit 9c6abc
Meta Huffman codes may be used _only_ when the image is being used in the
Packit 9c6abc
[role](#roles-of-image-data) of an _ARGB image_.
Packit 9c6abc
Packit 9c6abc
There are two possibilities for the meta Huffman codes, indicated by a 1-bit
Packit 9c6abc
value:
Packit 9c6abc
Packit 9c6abc
  * If this bit is zero, there is only one meta Huffman code used everywhere in
Packit 9c6abc
    the image. No more data is stored.
Packit 9c6abc
  * If this bit is one, the image uses multiple meta Huffman codes. These meta
Packit 9c6abc
    Huffman codes are stored as an _entropy image_ (described below).
Packit 9c6abc
Packit 9c6abc
**Entropy image:**
Packit 9c6abc
Packit 9c6abc
The entropy image defines which Huffman codes are used in different parts of the
Packit 9c6abc
image, as described below.
Packit 9c6abc
Packit 9c6abc
The first 3-bits contain the `huffman_bits` value. The dimensions of the entropy
Packit 9c6abc
image are derived from 'huffman_bits'.
Packit 9c6abc
Packit 9c6abc
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Packit 9c6abc
int huffman_bits = ReadBits(3) + 2;
Packit 9c6abc
int huffman_xsize = DIV_ROUND_UP(xsize, 1 << huffman_bits);
Packit 9c6abc
int huffman_ysize = DIV_ROUND_UP(ysize, 1 << huffman_bits);
Packit 9c6abc
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Packit 9c6abc
Packit 9c6abc
where `DIV_ROUND_UP` is as defined [earlier](#predictor-transform).
Packit 9c6abc
Packit 9c6abc
Next bits contain an entropy image of width `huffman_xsize` and height
Packit 9c6abc
`huffman_ysize`.
Packit 9c6abc
Packit 9c6abc
**Interpretation of Meta Huffman Codes:**
Packit 9c6abc
Packit 9c6abc
For any given pixel (x, y), there is a set of five Huffman codes associated with
Packit 9c6abc
it. These codes are (in bitstream order):
Packit 9c6abc
Packit 9c6abc
  * **Huffman code #1**: used for green channel, backward-reference length and
Packit 9c6abc
    color cache
Packit 9c6abc
  * **Huffman code #2, #3 and #4**: used for red, blue and alpha channels
Packit 9c6abc
    respectively.
Packit 9c6abc
  * **Huffman code #5**: used for backward-reference distance.
Packit 9c6abc
Packit 9c6abc
From here on, we refer to this set as a **Huffman code group**.
Packit 9c6abc
Packit 9c6abc
The number of Huffman code groups in the ARGB image can be obtained by finding
Packit 9c6abc
the _largest meta Huffman code_ from the entropy image:
Packit 9c6abc
Packit 9c6abc
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Packit 9c6abc
int num_huff_groups = max(entropy image) + 1;
Packit 9c6abc
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Packit 9c6abc
where `max(entropy image)` indicates the largest Huffman code stored in the
Packit 9c6abc
entropy image.
Packit 9c6abc
Packit 9c6abc
As each Huffman code groups contains five Huffman codes, the total number of
Packit 9c6abc
Huffman codes is:
Packit 9c6abc
Packit 9c6abc
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Packit 9c6abc
int num_huff_codes = 5 * num_huff_groups;
Packit 9c6abc
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Packit 9c6abc
Packit 9c6abc
Given a pixel (x, y) in the ARGB image, we can obtain the corresponding Huffman
Packit 9c6abc
codes to be used as follows:
Packit 9c6abc
Packit 9c6abc
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Packit 9c6abc
int position = (y >> huffman_bits) * huffman_xsize + (x >> huffman_bits);
Packit 9c6abc
int meta_huff_code = (entropy_image[pos] >> 8) & 0xffff;
Packit 9c6abc
HuffmanCodeGroup huff_group = huffman_code_groups[meta_huff_code];
Packit 9c6abc
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Packit 9c6abc
Packit 9c6abc
where, we have assumed the existence of `HuffmanCodeGroup` structure, which
Packit 9c6abc
represents a set of five Huffman codes. Also, `huffman_code_groups` is an array
Packit 9c6abc
of `HuffmanCodeGroup` (of size `num_huff_groups`).
Packit 9c6abc
Packit 9c6abc
The decoder then uses Huffman code group `huff_group` to decode the pixel
Packit 9c6abc
(x, y) as explained in the [next section](#decoding-entropy-coded-image-data).
Packit 9c6abc
Packit 9c6abc
#### 5.2.2 Decoding Entropy-coded Image Data
Packit 9c6abc
Packit 9c6abc
For the current position (x, y) in the image, the decoder first identifies the
Packit 9c6abc
corresponding Huffman code group (as explained in the last section). Given the
Packit 9c6abc
Huffman code group, the pixel is read and decoded as follows:
Packit 9c6abc
Packit 9c6abc
Read next symbol S from the bitstream using Huffman code #1. \[See
Packit 9c6abc
[next section](#decoding-the-code-lengths) for details on decoding the Huffman
Packit 9c6abc
code lengths\]. Note that S is any integer in the range `0` to
Packit 9c6abc
`(256 + 24 + ` [`color_cache_size`](#color-cache-code)`- 1)`.
Packit 9c6abc
Packit 9c6abc
The interpretation of S depends on its value:
Packit 9c6abc
Packit 9c6abc
  1. if S < 256
Packit 9c6abc
     1. Use S as the green component
Packit 9c6abc
     1. Read red from the bitstream using Huffman code #2
Packit 9c6abc
     1. Read blue from the bitstream using Huffman code #3
Packit 9c6abc
     1. Read alpha from the bitstream using Huffman code #4
Packit 9c6abc
  1. if S < 256 + 24
Packit 9c6abc
     1. Use S - 256 as a length prefix code
Packit 9c6abc
     1. Read extra bits for length from the bitstream
Packit 9c6abc
     1. Determine backward-reference length L from length prefix code and the
Packit 9c6abc
        extra bits read.
Packit 9c6abc
     1. Read distance prefix code from the bitstream using Huffman code #5
Packit 9c6abc
     1. Read extra bits for distance from the bitstream
Packit 9c6abc
     1. Determine backward-reference distance D from distance prefix code and
Packit 9c6abc
        the extra bits read.
Packit 9c6abc
     1. Copy the L pixels (in scan-line order) from the sequence of pixels
Packit 9c6abc
        prior to them by D pixels.
Packit 9c6abc
  1. if S >= 256 + 24
Packit 9c6abc
     1. Use S - (256 + 24) as the index into the color cache.
Packit 9c6abc
     1. Get ARGB color from the color cache at that index.
Packit 9c6abc
Packit 9c6abc
Packit 9c6abc
**Decoding the Code Lengths:**
Packit 9c6abc
{:#decoding-the-code-lengths}
Packit 9c6abc
Packit 9c6abc
This section describes the details about reading a symbol from the bitstream by
Packit 9c6abc
decoding the Huffman code length.
Packit 9c6abc
Packit 9c6abc
The Huffman code lengths can be coded in two ways. The method used is specified
Packit 9c6abc
by a 1-bit value.
Packit 9c6abc
Packit 9c6abc
  * If this bit is 1, it is a _simple code length code_, and
Packit 9c6abc
  * If this bit is 0, it is a _normal code length code_.
Packit 9c6abc
Packit 9c6abc
**(i) Simple Code Length Code:**
Packit 9c6abc
Packit 9c6abc
This variant is used in the special case when only 1 or 2 Huffman code lengths
Packit 9c6abc
are non-zero, and are in the range of \[0, 255\]. All other Huffman code lengths
Packit 9c6abc
are implicitly zeros.
Packit 9c6abc
Packit 9c6abc
The first bit indicates the number of non-zero code lengths:
Packit 9c6abc
Packit 9c6abc
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Packit 9c6abc
int num_code_lengths = ReadBits(1) + 1;
Packit 9c6abc
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Packit 9c6abc
Packit 9c6abc
The first code length is stored either using a 1-bit code for values of 0 and 1,
Packit 9c6abc
or using an 8-bit code for values in range \[0, 255\]. The second code length,
Packit 9c6abc
when present, is coded as an 8-bit code.
Packit 9c6abc
Packit 9c6abc
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Packit 9c6abc
int is_first_8bits = ReadBits(1);
Packit 9c6abc
code_lengths[0] = ReadBits(1 + 7 * is_first_8bits);
Packit 9c6abc
if (num_code_lengths == 2) {
Packit 9c6abc
  code_lengths[1] = ReadBits(8);
Packit 9c6abc
}
Packit 9c6abc
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Packit 9c6abc
Packit 9c6abc
**Note:** Another special case is when _all_ Huffman code lengths are _zeros_
Packit 9c6abc
(an empty Huffman code). For example, a Huffman code for distance can be empty
Packit 9c6abc
if there are no backward references. Similarly, Huffman codes for alpha, red,
Packit 9c6abc
and blue can be empty if all pixels within the same meta Huffman code are
Packit 9c6abc
produced using the color cache. However, this case doesn't need a special
Packit 9c6abc
handling, as empty Huffman codes can be coded as those containing a single
Packit 9c6abc
symbol `0`.
Packit 9c6abc
Packit 9c6abc
**(ii) Normal Code Length Code:**
Packit 9c6abc
Packit 9c6abc
The code lengths of a Huffman code are read as follows: `num_code_lengths`
Packit 9c6abc
specifies the number of code lengths; the rest of the code lengths
Packit 9c6abc
(according to the order in `kCodeLengthCodeOrder`) are zeros.
Packit 9c6abc
Packit 9c6abc
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Packit 9c6abc
int kCodeLengthCodes = 19;
Packit 9c6abc
int kCodeLengthCodeOrder[kCodeLengthCodes] = {
Packit 9c6abc
  17, 18, 0, 1, 2, 3, 4, 5, 16, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
Packit 9c6abc
};
Packit 9c6abc
int code_lengths[kCodeLengthCodes] = { 0 };  // All zeros.
Packit 9c6abc
int num_code_lengths = 4 + ReadBits(4);
Packit 9c6abc
for (i = 0; i < num_code_lengths; ++i) {
Packit 9c6abc
  code_lengths[kCodeLengthCodeOrder[i]] = ReadBits(3);
Packit 9c6abc
}
Packit 9c6abc
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Packit 9c6abc
Packit 9c6abc
  * Code length code \[0..15\] indicates literal code lengths.
Packit 9c6abc
    * Value 0 means no symbols have been coded.
Packit 9c6abc
    * Values \[1..15\] indicate the bit length of the respective code.
Packit 9c6abc
  * Code 16 repeats the previous non-zero value \[3..6\] times, i.e.,
Packit 9c6abc
    3 + `ReadBits(2)` times.  If code 16 is used before a non-zero
Packit 9c6abc
    value has been emitted, a value of 8 is repeated.
Packit 9c6abc
  * Code 17 emits a streak of zeros \[3..10\], i.e., 3 + `ReadBits(3)`
Packit 9c6abc
    times.
Packit 9c6abc
  * Code 18 emits a streak of zeros of length \[11..138\], i.e.,
Packit 9c6abc
    11 + `ReadBits(7)` times.
Packit 9c6abc
Packit 9c6abc
Packit 9c6abc
6 Overall Structure of the Format
Packit 9c6abc
---------------------------------
Packit 9c6abc
Packit 9c6abc
Below is a view into the format in Backus-Naur form. It does not cover
Packit 9c6abc
all details. End-of-image (EOI) is only implicitly coded into the number
Packit 9c6abc
of pixels (xsize * ysize).
Packit 9c6abc
Packit 9c6abc
Packit 9c6abc
#### Basic Structure
Packit 9c6abc
Packit 9c6abc
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Packit 9c6abc
<format> ::= <RIFF header><image size><image stream>
Packit 9c6abc
<image stream> ::= <optional-transform><spatially-coded image>
Packit 9c6abc
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Packit 9c6abc
Packit 9c6abc
Packit 9c6abc
#### Structure of Transforms
Packit 9c6abc
Packit 9c6abc
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Packit 9c6abc
<optional-transform> ::= (1-bit value 1; <transform> <optional-transform>) |
Packit 9c6abc
                         1-bit value 0
Packit 9c6abc
<transform> ::= <predictor-tx> | <color-tx> | <subtract-green-tx> |
Packit 9c6abc
                <color-indexing-tx>
Packit 9c6abc
<predictor-tx> ::= 2-bit value 0; <predictor image>
Packit 9c6abc
<predictor image> ::= 3-bit sub-pixel code ; <entropy-coded image>
Packit 9c6abc
<color-tx> ::= 2-bit value 1; <color image>
Packit 9c6abc
<color image> ::= 3-bit sub-pixel code ; <entropy-coded image>
Packit 9c6abc
<subtract-green-tx> ::= 2-bit value 2
Packit 9c6abc
<color-indexing-tx> ::= 2-bit value 3; <color-indexing image>
Packit 9c6abc
<color-indexing image> ::= 8-bit color count; <entropy-coded image>
Packit 9c6abc
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Packit 9c6abc
Packit 9c6abc
Packit 9c6abc
#### Structure of the Image Data
Packit 9c6abc
Packit 9c6abc
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Packit 9c6abc
<spatially-coded image> ::= <meta huffman><entropy-coded image>
Packit 9c6abc
<entropy-coded image> ::= <color cache info><huffman codes><lz77-coded image>
Packit 9c6abc
<meta huffman> ::= 1-bit value 0 |
Packit 9c6abc
                   (1-bit value 1; <entropy image>)
Packit 9c6abc
<entropy image> ::= 3-bit subsample value; <entropy-coded image>
Packit 9c6abc
<color cache info> ::= 1 bit value 0 |
Packit 9c6abc
                       (1-bit value 1; 4-bit value for color cache size)
Packit 9c6abc
<huffman codes> ::= <huffman code group> | <huffman code group><huffman codes>
Packit 9c6abc
<huffman code group> ::= <huffman code><huffman code><huffman code>
Packit 9c6abc
                         <huffman code><huffman code>
Packit 9c6abc
                         See "Interpretation of Meta Huffman codes" to
Packit 9c6abc
                         understand what each of these five Huffman codes are
Packit 9c6abc
                         for.
Packit 9c6abc
<huffman code> ::= <simple huffman code> | <normal huffman code>
Packit 9c6abc
<simple huffman code> ::= see "Simple code length code" for details
Packit 9c6abc
<normal huffman code> ::= ; encoded code lengths
Packit 9c6abc
 ::= see section "Normal code length code"
Packit 9c6abc
<lz77-coded image> ::= ((<argb-pixel> | <lz77-copy> | <color-cache-code>)
Packit 9c6abc
                       <lz77-coded image>) | ""
Packit 9c6abc
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Packit 9c6abc
Packit 9c6abc
A possible example sequence:
Packit 9c6abc
Packit 9c6abc
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Packit 9c6abc
<RIFF header><image size>1-bit value 1<subtract-green-tx>
Packit 9c6abc
1-bit value 1<predictor-tx>1-bit value 0<meta huffman>
Packit 9c6abc
<color cache info><huffman codes>
Packit 9c6abc
<lz77-coded image>
Packit 9c6abc
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Packit 9c6abc
Packit 9c6abc
[canonical_huff]: http://en.wikipedia.org/wiki/Canonical_Huffman_code