Understand image compression (lossless, lossy)

Graphics Creation – Image Compression

Learning Objectives (AO1‑AO3)

  • Explain why image compression is required and how resolution, colour depth, colour‑space and file format influence the amount of data.
  • Distinguish between lossless and lossy compression, name the principal algorithms and the formats that implement them.
  • Calculate compression ratios, PSNR and SSIM, and interpret these values to assess image quality.
  • Select the most appropriate compression method for a specific application (web, print, archival, medical, etc.) and justify the choice.

Why Compress Images?

Raw bitmap data grows rapidly with pixel count and colour depth. A 24‑bit colour image of 1920 × 1080 pixels needs almost 6 MB of storage. Transmitting or storing such data for web pages, mobile devices, cloud services or archival systems is impractical. Compression reduces the number of bits required while attempting to retain an acceptable visual appearance.

Key Concepts

  1. Compression Ratio (CR)

    $$CR = \frac{\text{Original size (bytes)}}{\text{Compressed size (bytes)}}$$

    A larger CR = greater size reduction.

  2. Bit Depth & Colour Models
    • Bit depth: bits per pixel (e.g., 8‑bit greyscale, 24‑bit RGB, 48‑bit RGB). Higher depth → more data.
    • Colour models: RGB, CMYK, HSV, YCbCr. JPEG, HEIF and most modern lossy formats first convert RGB → YCbCr to separate luminance (Y) from chrominance (Cb, Cr).
    • Colour‑space conversion enables chroma subsampling (e.g., 4:2:0), which reduces colour data with little perceived loss.
  3. Resolution & Scaling
    • Resolution = pixel dimensions (e.g., 1920 × 1080) or DPI/PPI for print.
    • Halving both width and height reduces the raw data to one‑quarter of the original.
    • Interpolation methods (nearest‑neighbour, bilinear, bicubic) affect the visual quality of a resized image and consequently its compressibility.
  4. Raster vs. Vector Graphics
    • Raster images store a colour value for each pixel; they are the primary target of pixel‑based compression.
    • Vector images store mathematical descriptions of shapes (paths, strokes). Because they contain no pixel grid, they are inherently lossless; optimisation focuses on path simplification and SVG‑specific compression (e.g., gzip).
  5. Redundancy – repeating patterns, identical colour values, or predictable frequency components that can be encoded more efficiently.
  6. Quality‑assessment metrics
    • Peak Signal‑to‑Noise Ratio (PSNR)** (dB) $$\text{PSNR}=10\log_{10}\!\left(\frac{MAX_I^{2}}{\text{MSE}}\right)$$ where MAX_I = 255 for 8‑bit data.
    • Structural Similarity Index (SSIM)** – value 0–1; closer to 1 means higher structural similarity.
    • Interpretation table (see below) links numerical ranges to qualitative judgments (“Excellent”, “Good”, “Poor”).

Interpretation of PSNR & SSIM

MetricRangeTypical Quality Assessment (AO3)
PSNR (dB)> 40Excellent – differences are imperceptible.
PSNR35 – 40Good – suitable for most photographic web use.
PSNR30 – 35Acceptable – noticeable artefacts may appear on close inspection.
PSNR< 30Poor – artefacts are obvious; not suitable for professional work.
SSIM> 0.95Excellent structural similarity.
SSIM0.90 – 0.95Good.
SSIM0.80 – 0.90Fair – some loss of detail.
SSIM< 0.80Poor – significant degradation.

Types of Image Compression

1. Lossless Compression

Data is reduced without any loss; the original bitmap can be reconstructed exactly.

  • Key Algorithms
    • Run‑Length Encoding (RLE)
    • Lempel‑Ziv‑Welch (LZW)
    • Huffman coding
    • DEFLATE (LZ77 + Huffman) – used in PNG and TIFF “Deflate” option
    • Predictive filters (PNG Sub, Up, Average, Paeth)
  • Common Formats
    • PNG – DEFLATE + predictive filters; supports 8‑/16‑bit colour, alpha transparency.
    • GIF – LZW, limited to 256 colours, supports simple animation.
    • TIFF – can store data uncompressed or losslessly compressed (LZW, Deflate, PackBits). Widely used in professional photography and archival work.
  • Typical Compression Ratio 2 : 1 – 3 : 1 (≈ 30‑50 % size reduction).
  • Best suited for line art, technical drawings, logos, medical images, archival storage where exact fidelity is required.

2. Lossy Compression

Information deemed less perceptible to the human eye is discarded, giving much higher size reductions.

  • Key Algorithms
    • Discrete Cosine Transform (DCT) – JPEG, JPEG‑2000 (wavelet variant), HEIF (HEVC‑based).
    • Wavelet transform – JPEG‑2000, JPEG‑XL.
    • Predictive coding – WebP (lossy), AVIF (AV1‑based).
  • Core Steps (with emphasis on quantisation)
    1. Colour‑space conversion (RGB → YCbCr) and chroma subsampling (e.g., 4:2:0) to reduce colour data.
    2. Block or tile division (e.g., 8 × 8 for JPEG, 4 × 4 or variable‑size for wavelets).
    3. Transform (DCT or wavelet) to obtain frequency coefficients.
    4. Quantisation – each coefficient is divided by a quantisation step size taken from a quantisation table; larger steps → more coefficients become zero → higher compression but greater loss. The “quality factor” in JPEG editors directly controls these step sizes.
    5. Zig‑zag ordering (for DCT) or coefficient ordering (for wavelets), followed by run‑length encoding of zeroes.
    6. Entropy coding (Huffman or arithmetic) to produce the final bit‑stream.
  • Common Formats
    • JPEG – DCT, 8 × 8 blocks, optional progressive mode; supports 4:2:0, 4:2:2, 4:4:4 chroma sampling.
    • JPEG‑2000 – wavelet transform, offers both lossy and lossless modes; higher quality at lower bit‑rates than JPEG.
    • WebP (lossy) – predictive coding + DCT; typically 25‑35 % smaller than JPEG at comparable quality.
    • HEIF/HEIC – based on HEVC intra‑prediction; excellent compression for high‑resolution photos and HDR content.
    • Emerging formats – AVIF (AV1‑based) and JPEG‑XL – promise even better compression efficiency while retaining support for lossless mode.
  • Typical Compression Ratio 10 : 1 – 30 : 1 (or higher) depending on the quality setting.
  • Best suited for photographs, web graphics, video frames, mobile app assets where a modest loss of fidelity is acceptable.

Comparison of Lossless and Lossy Compression

Aspect Lossless Lossy
Data Retention Exact reconstruction possible. Some data permanently discarded; reconstruction is approximate.
Typical CR 2 : 1 – 3 : 1 (≈ 30‑50 % reduction) 10 : 1 – 30 : 1 (or higher)
Image Quality No visual degradation. Possible artefacts (blocking, ringing, colour bleeding) that increase with higher compression.
Common Uses Icons, logos, line art, medical/archival images. Photographs, web pages, streaming video, mobile apps.
Key Format Features PNG – filter types, alpha channel; GIF – 256‑colour palette, animation; TIFF – multiple pages, selectable lossless algorithms. JPEG – DCT, chroma subsampling; JPEG‑2000 – wavelet, lossless option; WebP – predictive coding; HEIF – HEVC intra‑prediction, HDR support.

How Lossless Compression Works – Example: Run‑Length Encoding (RLE)

RLE replaces a run of identical pixels with a count and the pixel value.

Original row:  BBBBBBBB AAABBB
RLE output:   (8,B) (3,A) (3,B)

Effective for graphics with long uniform areas (e.g., simple icons, after colour‑space reduction).

How PNG Compression Works – Filters & DEFLATE

  1. Apply a predictive filter to each scanline (None, Sub, Up, Average, Paeth) to reduce entropy.
  2. Compress the filtered data with the DEFLATE algorithm (LZ77 sliding‑window + Huffman coding).
  3. Store the compressed stream together with metadata (bit depth, colour type, interlace, gamma, transparency).

The filter step is lossless; the decompressed file is identical to the original bitmap.

How JPEG Compression Works – DCT Example

  1. Block division – split the image into 8 × 8 pixel blocks.
  2. Colour‑space conversion & chroma subsampling – RGB → YCbCr; typical 4:2:0 halves the chroma resolution.
  3. Discrete Cosine Transform (DCT) – each block becomes 64 frequency coefficients.
  4. Quantisation – divide each coefficient by a quantisation value from a table; higher quality factor = smaller divisors, lower quality factor = larger divisors → more zeroes.
  5. Zig‑zag ordering & run‑length encoding – groups trailing zeroes for efficient entropy coding.
  6. Entropy coding – Huffman (or arithmetic) coding produces the final bit‑stream.

The quantisation step is the only source of data loss; adjusting the quality factor directly trades off file size against visual artefacts.

Practical Example – Compression Ratio, PSNR and SSIM

Original image: 1920 × 1080 pixels, 24‑bit colour (RGB).

Uncompressed size:

$$\text{Size}_{\text{orig}} = 1920 \times 1080 \times 24 \text{ bits} = 49{,}766{,}400 \text{ bits} \approx 5.97 \text{ MB}$$

Compressed JPEG (quality = 75): 850 KB (≈ 0.83 MB).

Compression ratio:

$$CR = \frac{5.97}{0.83} \approx 7.2$$

Assume the mean‑squared error after decompression is 12.5.

PSNR:

$$\text{PSNR}=10\log_{10}\!\left(\frac{255^{2}}{12.5}\right) \approx 38.1\ \text{dB}$$

SSIM (computed by software) = 0.96.

Interpretation (using the table above): PSNR ≈ 38 dB → “Good” quality; SSIM = 0.96 → “Excellent” structural similarity.

Decision Tree – Choosing the Right Compression Method

  1. Is exact pixel fidelity required?
    • Yes → Use a lossless format: PNG (8/16‑bit, alpha), TIFF (LZW/Deflate), or lossless JPEG‑2000.
    • No → Continue to step 2.
  2. What is the primary use?
    • Web / Mobile – Small file size, fast download.
      Recommended: JPEG (quality ≈ 70‑80) or WebP (lossy) for photographs; PNG for graphics with sharp edges or transparency.
    • Print (high‑resolution, colour‑accurate) – Larger files acceptable, colour fidelity important.
      Recommended: TIFF (lossless) or high‑quality JPEG (quality ≈ 90) if storage is limited.
    • Archival / Medical – No loss allowed, long‑term preservation.
      Recommended: PNG (16‑bit) or TIFF (LZW/Deflate). JPEG‑2000 lossless is also acceptable.
    • HDR / Advanced photography – Need wide colour gamut and high dynamic range.
      Recommended: HEIF/HEIC or JPEG‑XL (both support HDR and lossless modes).
  3. Do you need animation?
    • Yes → GIF (lossless, 256‑colour) for simple animations; APNG (PNG‑based) for higher colour depth.
    • No → Use the format chosen in steps 1‑2.

Impact of Resolution & Scaling on Compression

Example: A 4000 × 3000 pixel, 24‑bit image (≈ 34 MB uncompressed).

  • Scale to 2000 × 1500 (50 % of each dimension). New uncompressed size ≈ 8.5 MB (¼ of the original).
  • Compress both versions with JPEG at quality = 80:
    • Original size → 3.2 MB (CR ≈ 10.6).
    • Scaled size → 0.9 MB (CR ≈ 9.4).
  • Result: Reducing resolution dramatically lowers the amount of data that must be encoded, often allowing a higher visual quality for the same file‑size budget.

Emerging Image Formats (Brief Overview)

  • AVIF – Based on the AV1 video codec; offers 30‑50 % better compression than JPEG at comparable quality, supports HDR and lossless mode.
  • JPEG‑XL – Successor to JPEG‑2000; provides both efficient lossy compression and truly lossless compression in a single format.
  • Both are being adopted in modern browsers and mobile operating systems, illustrating the industry trend toward higher efficiency and broader colour‑gamut support.

Summary

Image compression balances file size against visual quality. Lossless methods (PNG, TIFF, lossless JPEG‑2000) preserve every pixel and are essential for graphics, medical images and archival work. Lossy methods (JPEG, WebP, HEIF, AVIF) discard perceptually less important information, achieving far higher compression ratios suitable for photographs and web delivery. Understanding the underlying algorithms, colour‑space handling, quantisation, and quality‑assessment metrics enables students to evaluate and justify the most appropriate format for any given application.

Suggested diagram: Side‑by‑side flowcharts showing the processing pipeline of a lossless format (PNG) versus a lossy format (JPEG). Highlight colour‑space conversion, filtering, DCT/wavelet transform, quantisation, and entropy coding steps.

Suggested Classroom Activities

  1. Format‑comparison lab – Save a high‑resolution photograph as PNG, JPEG (quality 100, 80, 60), WebP (lossy) and AVIF. Record file sizes, compute CR, PSNR and SSIM, and discuss visual differences and suitability for web use.
  2. RLE coding exercise – Using a spreadsheet or simple script, implement run‑length encoding on a 10 × 10 binary bitmap. Compare the compressed size with the original.
  3. PNG filter investigation – Re‑compress the same image with each PNG filter (None, Sub, Up, Average, Paeth). Observe how filter choice influences file size and why the optimal filter varies with image content.
  4. Artefact identification – Provide JPEG images at quality 90, 70, 50, 30. Students label the dominant artefacts (blocking, ringing, colour bleeding) and relate them to the quantisation step.
  5. Resolution & scaling task – Resize a 4000 × 3000 photograph to 50 % and 25 % using nearest‑neighbour, bilinear and bicubic interpolation. Compress each version with JPEG (quality 80) and analyse how resolution affects compression ratio and visual quality.
  6. Vector optimisation demo – Open an SVG file in an editor, simplify paths and gzip‑compress the file. Discuss why vector graphics are inherently lossless and how file size is reduced without affecting visual appearance.

Create an account or Login to take a Quiz

35 views
0 improvement suggestions

Log in to suggest improvements to this note.