Text, images, sound and compression
IGCSE Computer Science · Data Representation
Representing text
Every character is stored as a binary code from a character set.
- ASCII — $7$ or $8$ bits per character, giving $128$ or $256$ characters. Enough for English letters, digits and punctuation, but not for other alphabets;
- Unicode — uses more bits per character, so it covers far more characters: Chinese, Arabic, Cyrillic, emoji.
The trade-off is direct: Unicode supports every writing system, but each character takes more storage, so files are larger.
Representing images
A bitmap image is a grid of pixels, each storing a colour as a binary number.
- resolution — the number of pixels, given as width $\times$ height;
- colour depth — the number of bits used for each pixel.
These two are constantly confused, and the file size formula needs both:
$$\text{file size (bits)}=\text{width}\times\text{height}\times\text{colour depth}$$Colour depth decides how many colours are available: $n$ bits give $2^n$ colours. So $1$ bit gives $2$ colours, $8$ bits give $256$, and $24$ bits give over $16$ million.
Increasing either resolution or colour depth improves quality but increases file size.
Representing sound
Sound is analogue, so it must be sampled: its amplitude is measured at regular intervals and each measurement stored as a binary number.
- sample rate — the number of samples taken per second;
- sample resolution — the number of bits per sample.
A higher sample rate captures the sound more accurately, and a higher sample resolution records each amplitude more precisely. Both improve quality and both increase file size — the same trade-off as with images.
Storage units
| $8$ bits | $=1$ byte |
| $1\,000$ bytes | $=1$ kilobyte (kB) |
| $1\,000$ kB | $=1$ megabyte (MB) |
| $1\,000$ MB | $=1$ gigabyte (GB) |
| $1\,000$ GB | $=1$ terabyte (TB) |
Note the distinction in symbols: a lower-case b means bit and an upper-case B means byte. File size formulas give an answer in bits, so divide by $8$ to get bytes.
Compression
| lossless | no data is permanently removed; the original file can be restored exactly |
| lossy | some data is permanently removed; the original cannot be restored |
Run-length encoding is the standard lossless example: a run of identical values is replaced by the value and a count, so $\text{WWWWWWBB}$ becomes $\text{6W2B}$. Nothing is lost, and the original is rebuilt exactly.
Lossy methods — MP3 for audio, JPEG for images — discard detail the human ear or eye is least likely to notice. They achieve much greater size reduction, which is why they are used for streaming and for photographs.
Choosing between them: use lossless where every bit matters — text documents, program code, spreadsheets, medical images. Use lossy where a small quality loss is acceptable in exchange for a much smaller file.
Worked example: calculating image and sound file sizes
(a) An image has a resolution of $800\times 600$ and a colour depth of $24$ bits. Calculate the file size in megabytes. (b) A sound clip is $30$ seconds long, with a sample rate of $44\,100$ Hz and a sample resolution of $16$ bits. Calculate the file size in megabytes. (c) State two ways to reduce an image file size and the cost of each.
Solution
(a) Image file size.
$$\text{file size}=800\times 600\times 24=11\,520\,000\ \text{bits}$$Convert to bytes (divide by $8$):
$$\frac{11\,520\,000}{8}=1\,440\,000\ \text{bytes}$$Convert to megabytes (divide by $1\,000$ twice):
$$\frac{1\,440\,000}{1\,000\,000}=1.44\ \text{MB}$$(b) Sound file size.
$$\text{file size}=44\,100\times 16\times 30=21\,168\,000\ \text{bits}$$$$\frac{21\,168\,000}{8}=2\,646\,000\ \text{bytes}=2.646\ \text{MB}$$A general note for both calculations. The formula gives the result in bits, so the step of dividing by $8$ is essential. Forgetting this step makes the result eight times too large — this is the most common way to lose marks.
(c) Two ways to reduce an image file size.
Way one — reduce the resolution. For example from $800\times 600$ down to $400\times 300$. The number of pixels falls by a factor of four, so the file size also falls by a factor of four, to $0.36$ MB.
The cost: the image has less detail and looks pixelated when enlarged.
Way two — reduce the colour depth. For example from $24$ bits down to $8$ bits. The file size falls by a factor of three, to $0.48$ MB.
The cost: the number of usable colours drops from over $16$ million to only $256$, so smooth colour gradients will break up into bands.
A third way — compression. Using lossy compression such as JPEG reduces the size a great deal with changes the naked eye can barely notice, but the original cannot be restored.
Worked example: choosing the right compression type for each kind of file
For each case, state whether to use lossy or lossless compression and explain: (a) a program's source code file; (b) a song for online streaming; (c) an X-ray image used to diagnose disease.
Solution
General principle: ask “may any bits be lost?” If no bit may be lost → lossless. If losing a little detail is acceptable in exchange for a much smaller file → lossy.
(a) Program source code → LOSSLESS COMPRESSION.
Reason. In source code, every single character matters. Losing or changing one semicolon or one bracket makes the program unable to run.
Lossy compression here makes no sense at all: it would destroy the very thing that needs to be preserved. Lossless compression allows every character to be restored exactly.
(b) Song for online streaming → LOSSY COMPRESSION.
Reason. The top priority is a small file size so it transmits quickly over the network and does not use up bandwidth.
Lossy compression such as MP3 removes the frequencies that the human ear is least likely to hear, so it reduces the file size by a great deal while an ordinary listener can hardly tell the difference.
That the original cannot be restored is acceptable, because the listener only needs to listen and does not need to edit the original.
(c) Diagnostic X-ray image → LOSSLESS COMPRESSION.
Reason. This is the case that is most dangerous if the wrong choice is made.
Lossy compression removes details that the human eye is unlikely to notice — but those are exactly the kind of details the doctor is looking for: a very thin bone fracture, an early-stage tumour.
The compression algorithm does not know which details have medical significance; it only knows which details are less visually prominent. Losing exactly those details could lead to a wrong diagnosis.
So lossless compression must be used, accepting the larger file.
Comment. Parts (b) and (c) are both multimedia files, but the answers are opposite — because what decides is not the type of file but the consequence of losing detail.