Computer ScienceFoundation20 min read

Text, Sound and Images

How a machine that stores only numbers holds a photograph

This topic appears in:

01

Everything becomes a number

A computer stores nothing but binary. Text, sound, pictures and video all have to be converted into numbers first, and the rule for doing so is a code agreed in advance so that two machines interpret the same bits identically.

Each kind of data has its own conversion, and each involves a trade-off between how faithful the result is and how much space it takes. That trade-off is the examinable idea running through the whole topic.

DataConverted byQuality controlled by
Texta character set — ASCII or Unicodehow many characters the set covers
Imagea grid of pixels, each a colour numberresolution and colour depth
Soundmeasuring the wave at intervalssample rate and sample resolution
02

Text

A character set assigns a number to every character. ASCII uses 7 bits, giving 128 characters — enough for English letters, digits, punctuation and a few control codes. Extended ASCII uses 8 bits for 256.

That is not enough for the world's writing systems, so Unicode assigns numbers to over a million characters, covering Urdu, Arabic, Chinese, emoji and much else. Its first 128 code points are deliberately identical to ASCII, so old text remains readable.

The cost is size: a Unicode character may take up to four bytes where an ASCII one takes a single byte. UTF-8, the encoding used by almost every web page, keeps common English characters at one byte and spends more only where needed.

Letters are in order for a reason

In ASCII, A is 65 and B is 66, running consecutively to Z at 90; a is 97. Because the codes are consecutive, sorting alphabetically is just sorting numerically, and converting between upper and lower case is adding or subtracting 32. Neither would work if the assignments were arbitrary, and exam questions test exactly these consequences.

03

Images

A bitmap image is a grid of pixels, each storing a colour as a number. Two properties decide both the quality and the file size.

Resolution is the number of pixels — more pixels means finer detail. Colour depth is the number of bits per pixel — more bits means more distinguishable colours. 1 bit per pixel gives black and white only; 8 bits give 256 colours; 24 bits give about 16.7 million, which is more than the eye can distinguish and is why it is the common choice.

number of colours = 2^(colour depth)file size (bits) = width × height × colour depthfile size (bytes) = that ÷ 8the answer comes out in bits — dividing by 8 for bytes is the step most often forgotten
Worked example

An image is 800 by 600 pixels with a colour depth of 24 bits. Calculate its file size in megabytes.

  1. Pixels = 800 × 600 = 480 000.Resolution first — this is the number of colour values to be stored.
  2. Bits = 480 000 × 24 = 11 520 000.Each pixel needs 24 bits to record its colour.
  3. Bytes = 11 520 000 ÷ 8 = 1 440 000.Eight bits to the byte. Omitting this gives an answer eight times too large.
  4. Megabytes = 1 440 000 ÷ 1 048 576 = 1.37 MB.Dividing by 1024 twice. Using 1 000 000 gives 1.44 MB, which is accepted if the convention is stated.

About 1.37 MB

04

Sound

Sound is a continuously varying wave, and a computer cannot store something continuous. Instead it measures the amplitude at regular instants — sampling — and stores each measurement as a number.

Two settings control the result. The sample rate is how many measurements are taken per second, in hertz; a higher rate follows the wave more closely in time. The sample resolution is the number of bits per measurement; more bits allow a closer match in amplitude. Both improve accuracy and both increase the file size, in direct proportion.

sound file size (bits) = sample rate × sample resolution × seconds× 2 for stereoCD quality is 44 100 Hz at 16 bits, which is 1.4 million bits per second in stereo

Drag sample rate alone and watch the samples follow the wave more closely in time. Now drag resolution alone — the dashed levels multiply and each sample lands nearer the true height. Two independent controls, both costing file size.

Rate and resolution are not the same thing

Sample rate is how often you measure; sample resolution is how precisely each measurement is recorded. A high rate with low resolution gives a signal measured frequently but crudely; the reverse gives precise measurements too far apart to capture the shape. Questions ask about them separately, and using one term for the other loses the mark.

Before you leave this chapter

  1. A character set assigns a number to each character: ASCII 7-bit, Unicode over a million.
  2. ASCII letters are consecutive, so sorting and case conversion are arithmetic.
  3. Image size = width × height × colour depth, in bits. Divide by 8 for bytes.
  4. Colour depth of n bits gives 2ⁿ colours.
  5. Sound size = sample rate × resolution × seconds. Rate is how often, resolution is how precisely.
05

Working out a file size

Almost every calculation question in this topic is the same chain, and writing it out as a chain is what keeps the marks even when one step goes wrong.

Find the number of items — pixels for an image, samples for a sound. Multiply by the bits each one takes. That gives bits. Divide by 8 for bytes, then by 1024 for kilobytes and again for megabytes.

StepImageSound
Count the itemswidth × height pixelssample rate × seconds
Bits per itemcolour depthsample resolution
Multiplypixels × depthsamples × resolution
Then÷ 8 for bytes÷ 8, and × 2 for stereo
Then÷ 1024 for KB, again for MBthe same

The answer comes out in bits

Every one of these formulas produces a number of bits, because colour depth and sample resolution are both measured in bits. Forgetting to divide by 8 gives an answer exactly eight times too large, and it is the single most common error in the topic. Write the unit next to every intermediate value and the mistake becomes visible.

Practice questions

6 questions · 20 marks · full working on every one

Try each one on paper first, then open the working. The marks are shown where they are actually awarded, because that is where they are actually lost.

Short questions

3 · 6 marks

Two marks each, in the style of the short-question section of the paper. Answer in two or three lines.

SQ1[2 marks]
State one advantage and one disadvantage of Unicode compared with ASCII.
Model answer

Advantage: it represents over a million characters, covering every writing system including Urdu and Chinese, where ASCII covers only 128. Disadvantage: each character may need up to four bytes rather than one, so files are larger.

Examiner tip. The size cost is the disadvantage the mark scheme wants. "It is more complicated" earns nothing.

SQ2[2 marks]
An image has a colour depth of 8 bits. How many different colours can each pixel be?
Model answer

2⁸ = 256 colours.

Examiner tip. Colour depth is a number of bits, so the number of colours is 2 raised to it. Answering 8 confuses the two.

SQ3[2 marks]
Define sample rate and sample resolution.
Model answer

Sample rate is the number of measurements taken per second, in hertz. Sample resolution is the number of bits used to store each measurement. The first controls accuracy in time, the second accuracy in amplitude.

Examiner tip. Give the units for the rate. Saying "how good the sound is" for both is what loses the marks here.

Solved numericals

2 · 8 marks

Full working, one step per line, with the marks shown where they are awarded.

N1[4 marks]
A bitmap image is 1024 pixels wide and 768 pixels high with a colour depth of 16 bits. Calculate the file size in kilobytes.
Full working
  1. Total pixels = 1024 × 768 = 786 432[1]
  2. Total bits = 786 432 × 16 = 12 582 912[1]
  3. Bytes = 12 582 912 ÷ 8 = 1 572 864dividing by 8 is the step most often missed[1]
  4. Kilobytes = 1 572 864 ÷ 1024 = 1536 KB[1]

1536 KB (1.5 MB)

Examiner tip. Write each conversion on its own line — pixels, bits, bytes, kilobytes. The marks follow the chain, so a slip at one stage still leaves the earlier ones.

N2[4 marks]
A 30-second mono sound clip is recorded at 44 100 Hz with a resolution of 16 bits. Calculate the file size in megabytes, and state the effect of recording in stereo.
Full working
  1. Bits = 44 100 × 16 × 30rate × resolution × seconds[1]
  2. = 21 168 000 bits[1]
  3. Bytes = 2 646 000, so ÷ 1 048 576 = 2.52 MBaccept 2.65 MB using 1 000 000[1]
  4. Stereo records two channels, so the file size doubles to about 5 MB[1]

About 2.5 MB mono, doubling to about 5 MB in stereo

Examiner tip. Stereo is simply two of everything. The multiplication by 2 comes at the end and is worth a mark on its own.

Long questions

1 · 6 marks

Theory and numerical together, as they appear in the long-question section.

LQ1[6 marks]
A photographer must email a set of images but the files are too large.
  1. Explain two ways the file size of a bitmap image could be reduced, and the cost of each.
  2. Explain why increasing the colour depth from 8 to 24 bits triples the file size.
  3. Explain why the letters of the alphabet were given consecutive codes in ASCII.
Mark scheme
  1. Reduce the resolution — fewer pixels means a smaller file, at the cost of visible detail[1]
  2. Reduce the colour depth — fewer bits per pixel, at the cost of colour accuracy and possible banding in smooth gradientsaccept compression as a third method[1]
  3. The file size is width × height × colour depth, and only the depth has changed[1]
  4. 24 is three times 8, and the size is directly proportional to the depth, so the file is three times as largethe proportionality is the mark[1]
  5. Consecutive codes mean sorting alphabetically is the same as sorting numerically[1]
  6. And converting between upper and lower case is a fixed addition or subtraction of 32, both of which would be far harder with arbitrary assignments[1]

(a) fewer pixels or fewer bits per pixel (b) size is proportional to depth, and 24 is 3 × 8 (c) sorting and case conversion become arithmetic

Examiner tip. Every "reduce the file size" answer needs its cost stating. A smaller file is easy; the question is what you gave up for it.