Inside PNG Compression: Pre-filtering Algorithms and DEFLATE Optimization Explained
PNG 무손실 압축의 내부 동작: 5가지 사전 필터링과 DEFLATE 알고리즘 원리
By OmniTool Codec Engineering Team (Lossless Compression & Data Structure Specialist)
Executive Summary
A deep architectural dive into why PNG uses pre-filtering passes (None, Sub, Up, Average, Paeth) before LZ77 and Huffman coding, and how stripping auxiliary chunks minimizes file size.
PNG 포맷이 무손실 압축임에도 뛰어난 용량 절감을 보이는 비결은 5가지 공간 필터링 기법에 있습니다. IHDR, IDAT, IEND 청크 구조와 불필요한 메타데이터 제거 기술을 설명합니다.
1. Why Pure LZ77 Compression Fails on Raw 2D Bitmaps
Bitmap graphics are inherently two-dimensional arrays of RGB or RGBA color bytes. Standard general-purpose compression algorithms like LZ77 rely on finding repeating byte sequences across a linear stream.
However, in natural photographs and shaded illustrations, neighboring pixels along the vertical axis (e.g., pixel (X, Y) and pixel (X, Y+1)) are nearly identical, but in a linear 1D memory scanline, they are separated by thousands of bytes (the entire width of the image). A naive compression algorithm fails to spot this 2D correlation.
To overcome this structural limitation, the Portable Network Graphics (PNG) specification introduced an ingenious mathematical step: **Scanline Pre-Filtering**.
2. The 5 PNG Filter Types: None, Sub, Up, Average, and Paeth
Before compressing a scanline, the PNG encoder evaluates five mathematical filters on a per-row basis:
- **Type 0 (None):** The raw scanline bytes are passed directly to DEFLATE without modification.
- **Type 1 (Sub):** Replaces each byte with the difference between itself and the corresponding byte of the pixel immediately to its left.
- **Type 2 (Up):** Replaces each byte with the difference between itself and the byte directly above it in the previous row.
- **Type 3 (Average):** Predicts the current byte as the integer average of the left and above pixels, recording only the difference.
- **Type 4 (Paeth):** Evaluates a linear function of left, above, and upper-left pixels, picking the neighbor closest to the linear estimate.
By recording only differences (residuals), high-magnitude pixel values (e.g., 240, 242, 241) are transformed into long sequences of zeroes and small integers. This dramatic reduction in entropy allows the subsequent DEFLATE (LZ77 + Huffman) engine to compress the data by up to 80% without losing a single bit of visual fidelity.
3. Stripping Ancillary Chunks to Shave Off Unnecessary Bytes
A production PNG file is composed of mandatory chunks (`IHDR` header, `IDAT` compressed pixel streams, `IEND` termination) and optional ancillary chunks (`tEXt`, `zTXt`, `iTXt`, `pHYs` DPI tags, `cHRM` color gamuts).
Digital cameras and vector design applications like Illustrator often stuff dozens of kilobytes of proprietary application data, thumbnails, and color profiles into these ancillary chunks.
OmniTool Pro cleanly extracts pure pixel payloads, discarding obsolete metadata chunks and optimizing filter heuristics, ensuring your PNG assets are featherweight and lightning-fast on the web.
Lossless PNG optimization never changes the RGB values of your pixels; it merely reorganizes byte entropy to achieve tighter mathematical compression.
Frequently Asked Questions
Is PNG better than WebP for logos and icons?▼
WebP Lossless generally achieves 20% to 26% smaller file sizes than optimized PNG for transparent logos and vector icons. However, PNG retains universal software compatibility for offline printing.
What is the Paeth predictor named after?▼
The Paeth filter algorithm is named after Alan W. Paeth, who originally devised the linear predictive interpolation formula for computer graphics in 1991.
Recommended In-Browser Tool
Lossless Image Resizer
Apply what you just read directly in your browser. Fast, 100% private, and zero file uploads required.