Representing Images & Sound
How Computers Store Pictures
A computer can only hold data as binary — long strings of 0s and 1s — so a photograph must be turned into numbers before it can be saved. The most common way to do this is with a bitmap image, which is simply a grid of tiny coloured squares. Each square is a pixel (short for ‘picture element’), the smallest element of a digital image. Every pixel is given a binary code that stands for its colour, so the whole picture becomes a long list of colour codes that the computer reads row by row to rebuild the image.
Colour depth is the number of bits used to store the colour of each pixel. With a colour depth of \(n\) bits, an image can use \(2^n\) different colours — every extra bit doubles the number of colours available.
Colour Depth
The colour depth of an image is the number of bits used to represent the colour of a single pixel. Because each extra bit doubles the patterns available, an image with a colour depth of \(n\) bits can display \(2^n\) colours. A colour depth of just 1 bit gives only \(2^1 = 2\) colours, which are usually black and white.
Raising the colour depth quickly widens the range of colours. An 8-bit colour depth stores \(2^8 = 256\) colours, while 24-bit colour — often called ‘true colour’ — stores \(2^{24}\), which is over 16 million shades. That is more than the human eye can easily tell apart, so it produces smooth, realistic images. The catch is that more bits per pixel means every pixel takes up more storage.
Resolution
The resolution of a bitmap image is the number of pixels it contains, usually written as width times height, such as 1920 by 1080. A higher resolution packs in more pixels, so the image holds more detail and looks sharper. A low-resolution image stretched across a large screen looks blocky, because each pixel is enlarged until you can see the individual squares.
Colour depth and resolution each affect two things at once: the quality of the picture and the size of its file. Turning either one up makes the image look better but also makes the file bigger, because there is simply more information to record. Balancing that trade-off is a choice designers make every time they save an image.
Increasing the colour depth or the resolution improves image quality but increases the file size, because each change adds more binary data that has to be stored.
The size of a bitmap image in bits is found by multiplying the colour depth by the number of pixels:
$$\text{file size (bits)} = \text{colour depth} \times \text{width} \times \text{height}$$
For example, take an image that is 200 pixels wide and 300 pixels high with a colour depth of 8 bits. Its size is \(8 \times 200 \times 300\), which comes to 480,000 bits. To change bits into bytes you divide by 8, giving 60,000 bytes, or roughly 60 kilobytes. Notice that doubling either the colour depth or the resolution would double the file size.
Metadata
When an image is saved, the file also stores metadata, which means ‘data about the data’. Metadata is the extra information the computer needs in order to open and display the picture correctly, such as the image’s width, height and colour depth. It often records other details too, like the date the photo was taken and the camera settings, and many phones add the location where it was captured. Without the width and height, the computer would not know the shape of the pixel grid, so it could not rebuild the image at all.
Metadata is data stored alongside an image that describes it — for example its width, height and colour depth. The computer relies on this information to reconstruct and display the image correctly.
How Computers Store Sound
Sound in the real world is analogue: it is a continuous wave that changes smoothly and can take any value. A computer cannot store a continuous wave, because it works only in binary, so the sound must first be converted into numbers. This is done by sampling — measuring the height, or amplitude, of the wave at regular moments and recording each measurement as a binary number.
Two settings control how faithfully the wave is captured. The sample rate is how many samples are taken every second, measured in Hertz (Hz). Music on a CD, for instance, uses a sample rate of 44,100 Hz, meaning the wave is measured 44,100 times a second. Taking more samples each second captures quicker changes in the sound, so the recording stays closer to the original.
The bit depth is the number of bits used to store each individual sample, such as 16 bits. A larger bit depth lets each sample record the amplitude more precisely, giving a wider range of volumes and a cleaner sound. The duration is simply how many seconds of audio the file contains; a longer recording needs more samples and therefore more storage.
A higher sample rate and a greater bit depth both make a digital recording sound closer to the original, but both also increase the size of the sound file.
Quality versus File Size
Just like images, sound files involve a trade-off between quality and size. Raising the sample rate, raising the bit depth, or recording for longer all improve or lengthen the audio, yet each one increases the number of bits that must be stored. That is why a short, low-quality voice memo takes up far less space than a long, high-quality music track.
The size of a sound file in bits is found by multiplying its three settings together:
$$\text{file size (bits)} = \text{sample rate} \times \text{duration (s)} \times \text{bit depth}$$
For example, a 30-second clip recorded at a sample rate of 44,100 Hz with a bit depth of 16 bits has a size of \(44100 \times 30 \times 16\), which comes to 21,168,000 bits. Dividing by 8 gives 2,646,000 bytes, or about 2.6 megabytes.