How Does WebP Lossless Compression Work?
WebP lossless compression reduces file sizes by combining reversible image transformations—most notably spatial prediction—with an entropy coding pipeline built on LZ77 dictionary coding and canonical Huffman encoding. Before encoding, the format transforms raw pixel values to eliminate correlation between neighboring pixels and color channels. The residual data is then compressed using a customized, backward-referencing dictionary search paired with color-indexed entropy codes, achieving significantly better compression density than traditional formats like PNG.
Spatial Prediction Transformations
Spatial prediction in WebP lossless removes spatial redundancy by estimating a pixel's color based on previously decoded neighbors (above, left, and top-left). Rather than storing raw RGBA values, the encoder records only the residual difference between the predicted value and the actual value.
WebP defines 14 distinct prediction modes evaluated on a block-by-block basis:
- Basic directional modes: Use simple copies of immediate neighbors (e.g., \(L\) for left, \(T\) for top, \(TL\) for top-left, or \(TR\) for top-right).
- Average modes: Compute arithmetic averages of adjacent pixels to handle smooth gradients (such as \(\text{Avg}(L, T)\) or \(\text{Avg}(L, TR)\)).
- Select predictor: Evaluates local horizontal and vertical gradients to avoid predicting across detected edges, dynamically choosing between \(L\), \(T\), and a clamp function.
- Clamped gradient mode: Uses the formula \(\text{Clamp}(L + T - TL)\) to extrapolate continuous two-dimensional color planes.
Because image characteristics vary across regions, the encoder divides the image into blocks (typically 16×16 pixels) and selects the optimal predictor mode for each block. These mode choices are stored in a low-resolution sub-image, which itself is compressed using entropy coding.
Additional Reversible Preprocessing Steps
Spatial prediction is one of several reversible transforms used in WebP lossless encoding:
- Color Transform: Decorrelates the green, red, and blue channels. Because the green channel typically contains the most structural information, red and blue values are predicted from green (and red for blue) using linear transform functions.
- Subtract Green Transform: Replaces raw red and blue values by subtracting the green value modulo 256, flattening channel variance with minimal overhead.
- Color Indexing (Palette) Transform: When an image contains fewer than 256 unique colors, the image is mapped to an indexed palette, converting 32-bit pixel data into compact 1-, 2-, 4-, or 8-bit index arrays.
Entropy Coding: LZ77 and Canonical Huffman Coding
After the spatial and channel transforms produce decorrelated residual values, the resulting data stream enters entropy coding. WebP adapts standard LZ77 and Huffman coding techniques with image-specific enhancements:
- Two-Dimensional LZ77: Standard LZ77 searches backwards linearly for repeated byte sequences. WebP extends this by allowing distance references that map to 2D image coordinates, enabling efficient reuse of repeating pixel clusters and textures across scanlines.
- Color Cache: WebP maintains an addressable cache of recently seen colors. When a pixel matches a cached entry, the encoder emits a cache-index token rather than a full literal or long distance vector.
- Multi-Prefix Canonical Huffman Trees: WebP uses five separate Huffman trees to encode the data stream: green channel (which also holds LZ77 literals and match lengths), red channel, blue channel, alpha channel, and LZ77 backward distances. Canonical Huffman coding ensures compact prefix codes with minimal header overhead.
- Spatial Entropy Partitioning: Similar to the spatial predictor, images can be partitioned into regions that each use separate sets of Huffman trees, ensuring that complex photographic areas and flat, uniform areas use entropy models optimized specifically for their distinct statistical distributions.