img-converter.in logo
img-converter.inOffline
HomeGuidesUnderstanding Image Compression

Understanding Image Compression: How Lossy & Lossless Algorithms Work

Algorithmic MathPublished: May 30, 2026By img-converter.in Engineering Team

At its most basic level, an uncompressed digital image is a three-dimensional grid of numbers. For a standard 24-bit color photo measuring 4000 x 3000 pixels, storing the raw values of red, green, and blue for every coordinate requires 4000 * 3000 * 3 bytes (approximately 36 Megabytes) of raw disk memory.

To make such files usable over standard web protocols, computer scientists designed image compression algorithms. These algorithms fall into two structural paradigms: **Lossless** (where the original bit-grid is reconstructed perfectly) and **Lossy** (where human visual perception thresholds are used to selectively discard color details). Below, we analyze the exact mechanics of these systems.

1. JPEG Compression & The Discrete Cosine Transform (DCT)

JPEG is a lossy standard developed specifically for photographic content. Rather than compressing individual pixels, it converts visual grids into spatial frequency spectra.

Step A: Color Space Transform & Chroma Subsampling

The original RGB pixel grid is converted to **YCbCr** (Y represents Luma/brightness, while Cb and Cr represent blue-difference and red-difference chroma channels). Because the human eye recognizes changes in brightness much more sharply than color variations, the Cb and Cr channels are downsampled to half their resolution (often $4:2:0$ subsampling), immediately stripping half the color data with virtually zero visible difference.

Step B: Discrete Cosine Transform (DCT)

The luma and chroma grids are divided into $8 \times 8$ pixel matrices. Each block undergoes a **Discrete Cosine Transform (DCT)**, shifting the spatial coordinates into a set of 64 frequency coefficients. The top-left value represents the average brightness of the block (the DC coefficient), while the remaining 63 values represent horizontal and vertical pattern frequencies (AC coefficients).

Step C: Quantization (The Lossy Step)

The 64 DCT coefficients are divided by values in a predefined **Quantization Table** and rounded to the nearest integer. High-frequency coefficients (which represent tiny pixel details) are divided by large numbers, rounding most of them to zero. **This rounding operation is the only source of loss in the JPEG standard**; raising or lowering the JPEG "Quality Slider" directly scales the values in this quantization table.

Step D: Run-Length & Huffman Coding

The remaining coefficients are read in a zig-zag pattern, gathering long sequences of zeroes together. These sequences are compressed using Huffman run-length encoding to output the final binary JPEG file.

2. PNG Compression & The DEFLATE Engine

Unlike JPEG, PNG is designed for complete accuracy. It must preserve every pixel exactly. It accomplishes this through a two-step process: Predictive Filtering followed by DEFLATE byte compression.

Step A: Predictive Pixel Filtering

Before actual compression, PNG operates a predictor filter on every row of pixels. For each pixel, the encoder predicts the values of color channels based on the values of the immediate left pixel (A), the top pixel (B), and the diagonal top-left pixel (C). Instead of storing the absolute color numbers, PNG stores the difference value (residual) between the predicted and actual pixel. This makes the data sequence highly repetitive and zero-heavy.

Step B: The DEFLATE Compression Algorithm

The filtered residual bytes are passed into the **DEFLATE** compression pipeline, which utilizes:

  • LZ77: A sliding window dictionary match. If a byte pattern has appeared recently, it is replaced by an offset pointer referencing the prior occurrence.
  • Huffman Coding: Frequent symbols are assigned shorter bit-lengths, while rare symbols are assigned longer bit-lengths.

3. WebP Compression & VP8 Predictive Architecture

WebP combines modern lossy video compression math with an extremely advanced lossless system, resulting in massive size reductions for web graphics.

Lossy WebP: VP8 Intra-Frame Prediction

Derived from Google's VP8 video codec, lossy WebP splits images into macroblocks ($16 \times 16$ luma pixels). For each block, the encoder predicts the structure of the pixel grid by extending edges and colors from previously decoded neighboring blocks (left and above). Four intra-prediction modes are calculated: Vertical, Horizontal, DC (flat average), and TrueMotion. The difference matrix (residual) is then compressed using a 2D DCT-like transform, quantized, and processed.

Lossless WebP: Advanced Spatial Coding

Lossless WebP is an entirely separate engine from lossy VP8. It optimizes PNG's principles by using several custom spatial transforms:

  • Color Index Transform: If the image contains fewer than 256 unique colors, it automatically creates a palette index, reducing 32-bit pixel weights to 8-bit registers.
  • Color Cache: Stores recently used colors in a dynamic hash cache table to replace pixel declarations with index references.
  • Spatial Predictor: Utilizes 14 separate mathematical prediction modes to flatten complex geometric lines and solid color spaces.

4. HEIC (High Efficiency Image Container) & HEVC Math

Adopted by Apple in iOS 11, HEIC is a container format utilizing High Efficiency Video Coding (HEVC / H.265) compression.

HEIC improves on JPEG DCT by using variable block partition sizes (from $4 \times 4$ up to $64 \times 64$ pixels, known as Coding Tree Units). It supports highly advanced directional prediction models (35 distinct prediction directions compared to JPEG's 0 and WebP's 4), allowing it to store photos at half the size of equivalent JPEG structures while supporting 10-bit and 12-bit wide-gamut colors.

However, because HEIC is heavily patent-restricted, modern web browsers do not support HEIC rendering natively. This is why tools like img-converter.in are necessary; we decode the HEVC stream client-side in JS/WASM and draw it to a standard canvas to export browser-friendly JPEG or WebP files.

Advanced Compression FAQs

What is "generation loss" in image files?

Generation loss occurs when you repeatedly open, modify, and save a lossy file (like JPEG). Each save cycle reapplies the DCT transform and performs a fresh round of rounding (quantization) on the already compressed blocks, magnifying mathematical errors. This causes visual patterns to degrade and blocky artifacts to expand.

How does client-side compression maintain absolute file privacy?

Because we run these mathematical operations (quantization, predictor matching, and Huffmann table generation) locally inside the browser thread using JavaScript modules, no file bytes leave your local hardware environment. You can compile large batches completely offline.