impera Open Impera

A levels Computer Science · Data representation

Images, sound and compression

1Bitmap images

A bitmap image is a grid of pixels. Each pixel stores a colour as a binary number.

  • Resolution: the number of pixels (width × height), or pixels per inch for printing.
  • Colour depth: the number of bits per pixel. With n bits you get 2ⁿ colours: 1 bit = 2 colours, 8 bits = 256, 24 bits = about 16.7 million.
  • Metadata stores extra information such as width, height and colour depth, so the image can be displayed correctly.

$$\text{file size (bits)} = \text{width} \times \text{height} \times \text{colour depth}$$

Worked example

A full-HD image

  1. 1920 × 1080 pixels, 24-bit colour.
  2. Size = 1920 × 1080 × 24 = 49 766 400 bits.
  3. ÷ 8 = 6 220 800 bytes ≈ 6.2 MB (before compression and metadata).
Check yourself

How many colours can a 4-bit colour depth show?

  • 8

  • 4

  • 16

  • 256

Show the answer

16

2⁴.

2Vector graphics

A vector image stores a list of objects (lines, circles, polygons, text) with their properties, such as position, size, line colour and fill colour.

BitmapVector
grid of pixelslist of objects and properties
suits photographs and complex imagessuits logos, diagrams and fonts
becomes pixelated (blocky) when enlargedscales to any size with no loss of quality
file size depends on resolution and colour depthfile size depends on the number of objects
Check yourself

Why are logos often stored as vector graphics?

  • They show photographs with more detail

  • They always use more colours than bitmaps

  • They can be scaled to any size without losing quality

  • They are made of pixels that can be edited

Show the answer

They can be scaled to any size without losing quality

Objects are redrawn.

3Sound

Sound is an analogue wave. To store it, an analogue-to-digital converter (ADC) measures its amplitude at regular intervals: this is sampling.

Sampling. The wave is measured at regular intervals and each measurement is rounded to the nearest of 8 levels (a 3-bit resolution).

  • Sample rate: samples per second (Hz). CD quality is 44 100 Hz.
  • Sample resolution (bit depth): bits per sample. CD quality is 16 bits.

$$\text{file size (bits)} = \text{sample rate} \times \text{resolution} \times \text{seconds} \times \text{channels}$$

Worked example

A 3-minute stereo song at CD quality

  1. 44 100 × 16 × 180 × 2 = 254 016 000 bits.
  2. ÷ 8 = 31 752 000 bytes ≈ 31.8 MB.

Nyquist's theorem: the sample rate must be at least twice the highest frequency in the sound. Human hearing goes up to about 20 kHz, which is why CDs use 44.1 kHz.

MIDI stores music as instructions (note, length, volume, instrument), not as samples, so files are tiny and easy to edit, but it cannot store speech or real recordings.

Check yourself

What is the minimum sample rate to record sound up to 8 kHz?

  • 4 kHz

  • 44.1 kHz

  • 16 kHz

  • 8 kHz

Show the answer

16 kHz

Nyquist: double.

4Lossy and lossless compression

Compression reduces file size, so files take less storage and transfer faster.

LossyLossless
permanently removes data people are unlikely to noticethe original can be rebuilt exactly
much smaller filessmaller savings
JPEG, MP3, MP4PNG, ZIP, FLAC
fine for photos, music, videoneeded for text, program code, spreadsheets
Check yourself

Why must a program file use lossless compression?

  • Programs are too small to need any compression at all

  • Losing any data would change the program and stop it working

  • Lossless files are always smaller than lossy ones

  • Lossy compression is not allowed for programs by law

Show the answer

Losing any data would change the program and stop it working

Every bit matters.

5Run-length and dictionary encoding

Run-length encoding (RLE) replaces runs of the same value with a count and the value.

Worked example

RLE

  1. AAAABBBCCD becomes 4A3B2C1D.
  2. It works well for images with large areas of one colour, but can make data with few repeats bigger (ABCD becomes 1A1B1C1D).

Dictionary encoding replaces repeated words or patterns with short references to a dictionary.

Worked example

Dictionary encoding

  1. Dictionary: 1 = "the", 2 = "cat", 3 = "sat", 4 = "on", 5 = "mat".
  2. "the cat sat on the mat" becomes 1 2 3 4 1 5.
  3. The dictionary must be stored or sent too, so it only saves space when patterns repeat often.
Check yourself

What is WWWWWWBBBW in RLE?

  • W6B3W1

  • 10W

  • 6W4B

  • 6W3B1W

Show the answer

6W3B1W

Count each run.

6Huffman coding

Huffman coding is lossless. It gives shorter codes to more frequent characters, built from a binary tree.

  1. Count each character's frequency.
  2. Repeatedly join the two smallest frequencies into a new node.
  3. Label each left branch 0 and each right branch 1; a character's code is the path from the root.

The Huffman tree for ABRACADABRA. The most frequent letter, A, gets a 1-bit code; the rarest, C and D, get 4-bit codes.

Worked example

ABRACADABRA

  1. Frequencies: A 5, B 2, R 2, C 1, D 1.
  2. Codes from the tree: A = 0, B = 10, R = 110, C = 1110, D = 1111.
  3. Bits: 5×1 + 2×2 + 2×3 + 1×4 + 1×4 = 23 bits, compared with 11 × 8 = 88 bits in 8-bit ASCII.

No code is the start of another (a prefix code), so the bit stream can be decoded without separators.

Check yourself

In Huffman coding, which characters get the shortest codes?

  • The least frequent ones

  • The most frequent ones

  • Characters early in the alphabet

  • Capital letters

Show the answer

The most frequent ones

Common ones save most.

Now test yourself

This lesson has a 8-question quiz and 5 exam-style questions with mark schemes in Impera. Free, with your progress saved.

Practise in Impera