Show understanding of how a text file, bitmap image, vector graphic and sound file can be compressed

1.3 Compression

Learning objective

Show understanding of how a text file, bitmap image, vector graphic and sound file can be compressed, and be able to choose an appropriate method for a given situation.


Why compression matters – real‑world scenarios

  • Web performance: A 2 MB photograph saved as a JPEG (≈ 300 KB) loads in seconds on a 3G connection, whereas the original BMP would take considerably longer.
  • Archiving: A folder of 500 MB of plain .txt research papers can be reduced to ≈ 150 MB with ZIP, saving storage without any loss of information.
  • Streaming media: Lossy audio (MP3, AAC) and video (H.264) enable smooth playback over limited bandwidth while keeping quality acceptable to most listeners/viewers.


Decision guide – choosing a compression method

Use the checklist below to decide which technique is appropriate. The final column indicates the relevant Cambridge Assessment Objectives (AO).

QuestionGuidanceTypical methodsAO(s) addressed
Is the original data required to be reproduced exactly?

  • Yes → lossless compression.
  • No → lossy compression (acceptable quality loss).

Lossless: ZIP, PNG, FLAC, SVG + GZIP.
Lossy: JPEG, MP3, AAC.
AO1 – recall facts; AO2 – analyse suitability.
What type of data are you compressing?

  • Text → character‑based algorithms.
  • Bitmap (photographs) → frequency‑domain methods.
  • Bitmap (simple graphics, icons) → palette‑based or run‑length.
  • Vector graphic → descriptive‑text compression.
  • Audio → time‑domain prediction or frequency‑domain masking.

See detailed sections below.AO1, AO2.
Do you need to balance size against quality?Adjust quality parameters (JPEG quality factor, MP3 bitrate, etc.) to reach the desired trade‑off.Quality sliders, variable‑bit‑rate (VBR) modes.AO2, AO3 – evaluate trade‑offs and justify choices.


2 Compression of Specific File Types

2.1 Text files

Purpose

Store sequences of characters (e.g., source code, documents). Redundancy appears as repeated characters, common words, and recurring substrings.

Lossless techniques (most common)

  • Run‑Length Encoding (RLE) – effective when the same character repeats many times.

    Example: AAAAABBBCC5A3B2C

  • Huffman coding – builds a variable‑length prefix code from character frequencies; high‑frequency symbols get short codes.
  • LZW (Lempel‑Ziv‑Welch) – constructs a dictionary of repeated substrings on‑the‑fly; the basis of the ZIP and GIF formats.

Lossy techniques

Rarely used for plain text because any loss changes meaning. In specialised contexts (e.g., speech‑to‑text transcripts) lossy text compression may be acceptable, but it is not part of the Cambridge syllabus.

Key idea

Exploit statistical frequency and repeated patterns to replace them with shorter codes.

Worked example – Huffman coding

Text: ABABABAA → frequencies: A = 5, B = 3.

Huffman tree gives codes: A → 0, B → 1.

Compressed bit‑stream: 01010100 (8 bits) instead of 8 × 8 = 64 bits in ASCII.


2.2 Bitmap (raster) images

Purpose

Store colour information for each pixel. Uncompressed size = width × height × bits‑per‑pixel.

Lossless techniques

  • PNG – filtering + LZ77 (deflate) + Huffman coding.
  • GIF – limited to 256 colours; uses LZW.
  • BMP with RLE – simple run‑length encoding for 8‑bit or 4‑bit BMP files (good for simple graphics).

Lossy technique (dominant for photographs)

  • JPEG – 8 × 8 block Discrete Cosine Transform (DCT) → quantisation → entropy coding (Huffman).

Key ideas

  • Lossless: Remove exact repetitions and predictable patterns (e.g., identical rows, palette repeats).
  • Lossy: Transform spatial redundancy into the frequency domain (DCT) and discard high‑frequency components that the human eye is less sensitive to.

Worked example – JPEG on a 2 × 2 block (illustrative only)

| 52 55 |

| 53 54 |

After padding to an 8 × 8 block and applying the DCT, the top‑left coefficient (the average) is ≈ 52, while the remaining 63 coefficients are close to 0. Quantisation rounds the small coefficients to 0, leaving only a few non‑zero values to be Huffman‑encoded – a dramatic size reduction.


2.3 Vector graphics

Purpose

Describe images with geometric primitives (lines, curves, shapes) and their attributes. File size depends on the number of objects, not on pixel count, giving inherent resolution‑independence.

Lossless techniques (standard)

  • SVG + GZIP (SVGZ) – the XML text of an SVG file is compressed with the generic Deflate algorithm.
  • PDF – may embed vector objects; uses Flate (deflate), LZW or other filters.

Lossy techniques

Usually unnecessary because vectors store mathematical definitions. If a vector is rasterised for a specific purpose, the resulting raster can be compressed with lossy image formats.

Key idea

Compress the descriptive text (remove whitespace, shorten attribute names) and any numeric data using standard lossless compressors.

Worked example – SVG → SVGZ

Original SVG snippet (≈ 120 bytes):

<svg width="100" height="100">

<circle cx="50" cy="50" r="40" stroke="black" fill="red"/>

</svg>

After GZIP compression the same graphic occupies ≈ 45 bytes – a 62 % reduction.


2.4 Sound files

Purpose

Store audio as a series of amplitude samples taken at a fixed sampling rate (e.g., 44.1 kHz) and bit depth (e.g., 16‑bit). Redundancy exists because successive samples are often similar.

Lossless techniques

  • FLAC – linear predictive coding (LPC) + residual coding + Rice (entropy) coding.
  • WavPack – hybrid predictive + Rice coding; supports fast decoding.
  • ALAC – Apple Lossless, similar predictive approach.

Lossy techniques (dominant for consumer audio)

  • MP3 – sub‑band filter bank → Modified DCT (MDCT) → psychoacoustic masking → quantisation → Huffman.
  • AAC – improved filter bank (MDCT + Perceptual Noise Substitution) and more efficient coding.

Key ideas

  • Lossless: Predict each sample from previous ones; encode only the small prediction error (residual).
  • Lossy: Transform to frequency domain, discard components masked by louder sounds (psychoacoustic masking), and quantise the rest.

Worked example – FLAC prediction (order‑1)

Original PCM samples (16‑bit): 1000, 1012, 1025, 1030, 1020 …

Predict each sample as the previous value. Prediction errors: 0, 12, 13, 5, ‑10 …

These small errors are entropy‑coded, giving roughly a 2:1 compression on typical music.


2.5 Comparison summary

File typeTypical formatsLossless method(s)Lossy method(s)Key ideaAO(s)
Text.txt, .csvRLE, Huffman, LZW (ZIP)– (rarely used)Exploit character frequency and repeated substrings.AO1, AO2
Bitmap image.bmp, .png, .gif, .jpgPNG (filter + deflate), GIF (LZW), BMP + RLEJPEG (DCT + quantisation + Huffman)Transform spatial redundancy (DCT) or encode exact repetitions.AO1, AO2, AO3
Vector graphic.svg, .pdfSVG + GZIP (deflate), PDF filters (Flate, LZW)– (normally lossless)Compress descriptive text; no pixel data to discard.AO1, AO2
Sound.wav, .flac, .wavpack, .mp3, .aacFLAC, WavPack, ALAC (predictive + Rice)MP3, AAC (sub‑band transform + psychoacoustic masking + Huffman)Predictive coding for redundancy; discard inaudible frequencies for lossy.AO1, AO2, AO3


2.6 Deeper dive (optional for higher‑ability students)

Click to expand

Entropy – the theoretical limit of lossless compression

The entropy of a source with symbol probabilities \(p_i\) is

\(H = -\sumi pi \log2 pi\) bits per symbol.

No lossless code can, on average, use fewer than \(H\) bits per symbol. Huffman coding approaches this limit when symbol probabilities are powers of ½.

JPEG DCT formula (simplified)

\(F(u,v)=\frac{1}{4}\alpha(u)\alpha(v)\sum{x=0}^{7}\sum{y=0}^{7} f(x,y)\cos\frac{(2x+1)u\pi}{16}\cos\frac{(2y+1)v\pi}{16}\)

where \(\alpha(0)=\frac{1}{\sqrt{2}}\) and \(\alpha(k)=1\) for \(k>0\).

MP3 psychoacoustic masking

The encoder builds a masking‑threshold curve based on the ear’s sensitivity. Any spectral component below this curve is considered inaudible and can be quantised more coarsely or discarded, yielding high compression with little perceived loss.


3 Link to the Cambridge IGCSE/A‑Level Syllabus

Assessment Objectives (AO) covered

  • AO1 – Knowledge and understanding: definitions of lossless vs. lossy, description of algorithms (RLE, Huffman, LZW, DCT, predictive coding).
  • AO2 – Application of knowledge: selecting an appropriate method for a given file type, interpreting compression ratios, analysing trade‑offs.
  • AO3 – Analysis and evaluation: evaluating the impact of quality settings, comparing different formats, justifying the choice of a compression method in a real‑world scenario.

How the notes map to exam tasks

Exam command wordRelevant note sectionSuggested answer points
Define2.1 – 2.4 (e.g., “Define lossless compression.”)Lossless = exact reconstruction; lossy = acceptable loss of detail.
ExplainKey ideas for each file typeState the principle (e.g., “JPEG uses DCT to separate low‑ and high‑frequency components.”)
CompareComparison summary tableContrast PNG (lossless) with JPEG (lossy) in terms of size, quality, typical use.
EvaluateDecision guide & quality‑trade‑off discussionDiscuss how a lower JPEG quality reduces size but may introduce blocking artefacts; weigh against storage constraints.


4 Practical Component (Paper 4) – Quick Checklist

  • Language & IDE: Choose a language supported by the exam (e.g., Python, Java, C++) and set up a simple IDE or text editor.
  • Program structure: Include a clear main method, functions for reading, compressing, and writing files, and comments describing each step.
  • Testing: Prepare at least two test cases – one lossless (e.g., text → ZIP) and one lossy (e.g., image → JPEG) – and record input size, output size, and any quality observations.
  • Documentation: Provide a brief user guide (what the program does, required arguments, and how to run it).
  • Submission format: Follow the exam board’s guidelines (source file, optional compiled executable, and a short report).


5 Further reading & resources

  • Cambridge International AS & A Level Computer Science – Specification (Section 1.3 Compression).
  • “Data Compression: The Complete Reference” by David Salomon – chapters on Huffman, LZW, JPEG, and MP3.
  • Online DCT visualiser: dsp.stackexchange.com
  • Free tools for experimentation: 7‑Zip (ZIP/LZW), GIMP (PNG/JPEG), Audacity (FLAC/MP3).