Text, images, sound and compression

IGCSE Computer Science · Data Representation

Representing text

Every character is stored as a binary code from a character set.

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.

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.

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

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

losslessno data is permanently removed; the original file can be restored exactly
lossysome 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.

Two DIFFERENT quantities — the formula needs BOTH RESOLUTION = NUMBER of pixels 6 × 3 = 18 pixels COLOUR DEPTH = BITS per pixel ONE pixel stored using n bits n bit → 2ⁿ màu 8 bit → 256 màu file size (bits) = width × height × colour depth Forget the colour depth → the result is many times too small. The result is in BITS — divide by 8 to convert to BYTES (lowercase b = bit, uppercase B = byte).
The same three-factor formula applies to sound: sample rate × sample resolution × duration. Recognizing this common pattern means remembering both.

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.

Common mistake

Confusing resolution with colour depth, and leaving one of them out of the file size formula. Resolution is the number of pixels (width $\times$ height); colour depth is the number of bits used for each pixel. The formula needs all three factors: width $\times$ height $\times$ colour depth. Forgetting the colour depth makes the result many times too small — for a $24$-bit image, exactly $24$ times too small. The second trap, and the most common way to lose marks in the whole chapter: forgetting to divide by $8$. The formula gives the result in bits, while the question usually asks for bytes, kB or MB. Forgetting this step makes the answer eight times too large. The notation is also distinct: a lowercase b is a bit, an uppercase B is a byte. The third trap: thinking lossless compression just means “compressing less”. The difference is not one of degree but of nature: lossless means the data can be restored exactly, while lossy means the original is gone permanently.

Key takeaway

ASCII is enough for English; Unicode covers every writing system but takes more bits. Images: resolution is the number of pixels, colour depth is the number of bits per pixel, and $n$ bits give $2^n$ colours. Image file size $=$ width $\times$ height $\times$ colour depth; sound file size $=$ sample rate $\times$ sample resolution $\times$ duration — the same three-factor pattern, and the result is in bits so you must divide by $8$. Lossless compression restores data exactly (run-length encoding); lossy compression deletes permanently but can reduce size far more.

← Number systems and binary arithmetic · All IGCSE Computer Science lessons · Practice IGCSE Computer Science questions (free, instant scoring) · Packets, transmission methods and direction →