Representing Images (Bitmaps)

Look really closely at a photo on a screen — press your nose to it, or zoom in until it goes blocky. Those little coloured squares are pixels (short for picture elements). A bitmap image is nothing more than a grid of pixels, and the computer stores it by writing down a number for the colour of every single pixel, row by row.

That's the whole idea: a picture becomes a long list of numbers. Because a computer only understands binary, each of those colour numbers is stored as a pattern of bits. Decode the numbers back into colours, lay them out on the grid, and the image reappears.

A grid of pixels, coloured by number

Here is a tiny 8\times 8 bitmap. Every square is a pixel, and a few of them have been filled in from their stored colour code. Step through it to watch the numbers turn into colour.

A real photo works exactly the same way — it just has millions of pixels instead of 64, so from a normal viewing distance your eye blends them into a smooth picture.

Resolution: how many pixels

The resolution of a bitmap is how many pixels it has, given as width \times height. A picture that is 1920 pixels wide and 1080 pixels tall has a resolution of 1920\times 1080 — that's 1920\times 1080 = 2{,}073{,}600 pixels altogether (just over two million, which is why it's nicknamed "2 megapixels").

More pixels means more detail: edges look sharper and curves look smoother, because there are more little squares to describe them. Fewer pixels means the same picture looks blocky, or pixelated.

Colour depth: how many colours

Each pixel's colour is stored as a binary number, and colour depth is the number of bits used per pixel. This is where binary really pays off: with d bits you can make 2^d different patterns, so a colour depth of d bits gives you

\text{number of colours} = 2^{d}

With just 1 bit per pixel you get 2^1 = 2 colours — black and white. Bump it up and the palette explodes:

24-bit colour is common on the web: it splits the 24 bits into 8 bits each for red, green and blue, mixing them to make any shade your eye can notice.

Colours from depth — Run it

Doubling d times is exactly 2^d. Change depth to any number of bits and press Run to see how many colours that buys you.

// How many colours does a given colour depth give? colours = 2 ^ depth const depth: number = 8; // bits per pixel — try 1, 2, 8, 24 … const colours: number = 2 ** depth; console.log(depth + "-bit colour = " + colours.toLocaleString() + " colours");

File size: pixels times depth

Now we can work out how big the file is. Every one of the width \times height pixels needs its colour stored, and each colour takes colour depth bits. So, counting in bits,

\text{file size} \approx \text{width} \times \text{height} \times \text{colour depth}

That answer is in bits. To get bytes, divide by 8 (there are 8 bits in a byte); divide by 8000 for kilobytes, and so on. This is an estimate of the raw pixel data — real image files also carry a little extra "header" information and are often compressed — but for GCSE it's the calculation you need.

// Estimate an image's file size: bits = width * height * colourDepth const width: number = 800; // pixels const height: number = 600; // pixels const colourDepth: number = 24; // bits per pixel const bits: number = width * height * colourDepth; const bytes: number = bits / 8; const kilobytes: number = bytes / 1000; console.log("Pixels: " + (width * height).toLocaleString()); console.log("Size (bits): " + bits.toLocaleString()); console.log("Size (bytes):" + bytes.toLocaleString()); console.log("Size (KB): " + kilobytes.toLocaleString() + " KB");

File size grows if you increase the resolution or the colour depth — because both are multiplied together in \text{width}\times\text{height}\times\text{depth}. It's tempting to think only "more pixels" costs storage, but going from 8-bit to 24-bit colour trebles the file even though the number of pixels hasn't changed at all. More detail — whether that's more pixels or more colours per pixel — always costs more storage.

By this formula a 4000\times 3000 photo at 24-bit colour would be about 36 MB of raw pixels — yet the JPEG on your phone might be only 3 MB. The difference is compression: clever algorithms spot patches of similar colour and repetition and store them far more compactly. Our formula gives the uncompressed size, which is the honest upper bound — and exactly what your exam is asking for.

Worked example

An image is 100 pixels wide, 50 pixels tall, with a colour depth of 4 bits. How big is it in bytes?

And a colour depth of 4 bits means the image can use up to 2^4 = 16 different colours.