PNG Compression Techniques: How Deflate and Byte Filtering Work
Deep dive into PNG internals: scanline delta filtering, Paeth prediction, and LZ77 Deflate compression for ultra-efficient lossless images. In this comprehensive technical guide, we examine the underlying mechanics, practical implementation workflows, and industry best practices.
The Secret Weapon: 2D Scanline Pre-Filtering
Before compression begins, every horizontal row of pixels (scanline) is transformed using one of five filtering algorithms:
- Filter 0 (None): Raw pixel bytes transmitted untouched.
- Filter 1 (Sub): Difference between current pixel and its left neighbor.
- Filter 2 (Up): Difference between current pixel and the pixel directly above.
- Filter 3 (Average): Average of left and above pixels.
- Filter 4 (Paeth): Linear prediction of three neighboring pixels.
Why Filtering Makes Deflate More Powerful
By replacing raw color bytes with tiny delta differences, long sequences of near-zero values are produced. The subsequent LZ77 and Huffman stages in Deflate can compress these repetitive sequences with extraordinary efficiency.
Conclusion
Understanding PNG byte filtering reveals why clean graphics and flat UI screenshots compress with extraordinary efficiency when prepared properly.