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}$$
A full-HD image
- 1920 × 1080 pixels, 24-bit colour.
- Size = 1920 × 1080 × 24 = 49 766 400 bits.
- ÷ 8 = 6 220 800 bytes ≈ 6.2 MB (before compression and metadata).
Check yourself
How many colours can a 4-bit colour depth show?
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.
| Bitmap | Vector |
|---|---|
| grid of pixels | list of objects and properties |
| suits photographs and complex images | suits logos, diagrams and fonts |
| becomes pixelated (blocky) when enlarged | scales to any size with no loss of quality |
| file size depends on resolution and colour depth | file size depends on the number of objects |
Check yourself
Why are logos often stored as vector graphics?
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}$$
A 3-minute stereo song at CD quality
- 44 100 × 16 × 180 × 2 = 254 016 000 bits.
- ÷ 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?
Show the answer
16 kHz
Nyquist: double.
4Lossy and lossless compression
Compression reduces file size, so files take less storage and transfer faster.
| Lossy | Lossless |
|---|---|
| permanently removes data people are unlikely to notice | the original can be rebuilt exactly |
| much smaller files | smaller savings |
| JPEG, MP3, MP4 | PNG, ZIP, FLAC |
| fine for photos, music, video | needed for text, program code, spreadsheets |
Check yourself
Why must a program file use lossless compression?
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.
RLE
- AAAABBBCCD becomes 4A3B2C1D.
- 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.
Dictionary encoding
- Dictionary: 1 = "the", 2 = "cat", 3 = "sat", 4 = "on", 5 = "mat".
- "the cat sat on the mat" becomes 1 2 3 4 1 5.
- The dictionary must be stored or sent too, so it only saves space when patterns repeat often.
Check yourself
What is WWWWWWBBBW in RLE?
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.
- Count each character's frequency.
- Repeatedly join the two smallest frequencies into a new node.
- 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.
ABRACADABRA
- Frequencies: A 5, B 2, R 2, C 1, D 1.
- Codes from the tree: A = 0, B = 10, R = 110, C = 1110, D = 1111.
- 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?
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