1.3 Compression

2026 Syllabus Objectives

By the end of this topic, you should be able to:

  • understand why compression is needed and give examples of where it is used
  • explain the difference between lossy and lossless compression
  • justify which compression method should be used in a given situation
  • explain how a text file, bitmap image, vector graphic and sound file can be compressed
  • understand and use run-length encoding (RLE) as a compression method

The need for compression

Compression means reducing the size of a file so that it needs less storage space and can be sent more quickly.

Files can become very large. This is especially true for sound, images and video. Large files cause problems because they take up more space on storage devices and take longer to upload, download or stream across a network.

Compression is needed for two main reasons.

First, it helps to save storage space. If files are smaller, more of them can be stored on a hard drive, SSD, memory card, phone or tablet. For example, a music player can store far more songs if the songs are compressed.

Second, it helps to reduce transfer time. Smaller files move faster across a network because there is less data to send. This is useful when sending files by email, downloading music, streaming video, or loading images on a website.

So, in simple terms, compression is used when we want files to be:

  • smaller to store
  • faster to send
  • quicker to stream
  • easier to manage on devices with limited space

Examples of where compression is used

Compression is used in many everyday situations.

A music file may be compressed so that it can be stored on a phone and streamed over the internet without using too much data.

A photo may be compressed so that it takes up less storage and can be uploaded quickly to a website or sent in a message.

A video file may be compressed so it can be streamed online more smoothly.

A document may be compressed so that it uses less space and is easier to share.

A bitmap image may be compressed to reduce file size, especially when the same colour appears many times.

A vector graphic may be compressed because it is stored as text-based instructions, which can often be reduced efficiently.

Lossy and lossless compression

There are two main types of compression: lossless and lossy.

Lossless compression

Lossless compression reduces file size without losing any original data. After the file is decompressed, it returns to exactly the same form as before compression.

This means nothing is missing. Every character, every value and every part of the original file can be recovered.

Lossless compression is important when losing data would cause problems. For example, in text files and documents, even a tiny missing part could change the meaning of the file. That is why lossless compression is used when accuracy must stay perfect.

Lossless compression usually does not reduce file size as dramatically as lossy compression, but it keeps all the data safe.

Lossy compression

Lossy compression reduces file size by removing some of the original data. After decompression, the file is not exactly the same as the original because some detail has been permanently lost.

This method works when a small drop in quality is acceptable. For example, in music, images and video, some missing detail may not be noticed easily by people.

Lossy compression can make files much smaller than lossless compression. However, it cannot restore the removed data.

Comparing lossy and lossless compression

The easiest way to remember the difference is this:

  • Lossless = smaller file, but all original data stays
  • Lossy = much smaller file, but some original data is removed

Here is the key idea:

  • Use lossless when the file must stay exact
  • Use lossy when a small loss of quality is acceptable in exchange for a much smaller file

Choosing the correct method

In an exam, you may be asked to justify which method should be used. That means you must explain why one type is better for that situation.

When to use lossless compression

Lossless compression should be used when the original file must be recovered perfectly.

This is suitable for:

  • text files
  • documents
  • files where every symbol or value matters exactly

Why? Because even one missing letter, number or symbol could make the file incorrect or harder to use.

When to use lossy compression

Lossy compression should be used when reducing file size is more important than keeping every bit of original detail.

This is suitable for:

  • sound files
  • bitmap images such as photographs
  • video and multimedia files

Why? Because people may not notice a small drop in quality, but the much smaller file size gives big benefits for storage and transfer.

How sound files can be compressed

Sound files can be very large, especially if they store high-quality audio. One common way to compress sound is by using MP3 compression.

MP3 is a lossy compression method. It reduces file size by removing sounds that people are less likely to hear.

This includes:

  • frequencies outside the normal human hearing range
  • quiet sounds that are hidden by louder sounds

This method is sometimes described as perceptual music shaping. That means the compression is based on what the human ear is likely to notice and what it is unlikely to notice.

Because some sound data is removed, the original file cannot be fully recovered. That is why MP3 is lossy.

A typical music file can be reduced by about 90%. For example, a file of 80 MB may be reduced to about 8 MB. This makes it much easier to store on a device or stream over the internet.

The bit rate of a sound file affects its quality. Bit rate means the number of bits used each second. A higher bit rate usually means better quality, but also a larger file size. A lower bit rate gives a smaller file, but quality may fall.

So, sound compression is a trade-off between:

  • quality
  • file size
  • speed of transfer

How bitmap images can be compressed

A bitmap image is made from pixels. Each pixel stores colour data. Because there can be many thousands or millions of pixels, bitmap files can become large.

One common bitmap compression format is JPEG. JPEG uses lossy compression.

JPEG works well for photographs because it reduces file size by removing some image detail that the human eye may not notice easily. For example, similar colours may be grouped together so that less data needs to be stored.

This gives a much smaller file, but the original image cannot be restored perfectly. That is why JPEG is lossy.

Lossy bitmap compression is useful when:

  • some loss of detail is acceptable
  • storage space matters
  • images need to be shared quickly online

However, if exact image data must be kept, then a lossless method would be better.

How vector graphics can be compressed

A vector graphic is not stored as pixels. Instead, it is stored as instructions, such as lines, shapes, curves and their positions.

Many vector graphics, such as SVG files, are stored in a text-based format. Because of this, they can often be compressed efficiently.

This is closer to the way text is compressed, because repeated words, symbols or patterns in the file can be reduced.

Vector graphics can be compressed, but the success may be more limited than with some other file types. Still, since the file is based on structured text instructions, compression is possible.

For this syllabus, the important point is that vector graphics can be compressed because they are often stored as text-based data.

How text files can be compressed

Text files can be compressed using lossless methods.

This is because text must stay exact. If a letter, punctuation mark or number is changed or removed, the meaning may change.

One important method you need to know is run-length encoding (RLE).

Run-length encoding (RLE)

Run-length encoding (RLE) is a lossless compression method.

It works by looking for runs, which means repeated items that appear one after another.

Instead of storing every repeated item separately, RLE stores:

  • the count of how many times it repeats
  • the value being repeated

So if a letter appears many times in a row, RLE stores one number and one value instead of writing the same thing again and again.

RLE works best when there are long runs of repeated data. If the data changes all the time, RLE may not help much.

RLE with text

Suppose we have this text:

AAAABBBCCDAA

Instead of storing every character separately, RLE stores each group as a count and a character:

  • 4A
  • 3B
  • 2C
  • 1D
  • 2A

So the compressed version is:

4A3B2C1D2A

This is more efficient because repeated letters are grouped together.

Another example is:

aaaaabbbbccddddd

This can be represented as runs of repeated characters. The count tells us how many times a character appears in a row, and the value tells us which character it is.

In computer storage, the count may be stored in binary and the character may be stored using its ASCII code.

For example, the run 4A could be stored as:

  • count 4 in binary
  • character A as its ASCII value

This shows that RLE can be used at a lower machine level, not just in simple written form.

Why RLE is lossless

RLE is lossless because no data is removed. The repeated values are still all represented. They are just written in a shorter form.

When the file is decompressed, the computer reads the count and the value, then rebuilds the original sequence exactly.

So if the original data was:

AAAA

the computer can expand 4A back into AAAA with no change at all.

When RLE works well

RLE works well when the file contains many repeated values in a row.

This happens in:

  • simple text with repeated characters
  • bitmap images with large areas of the same colour
  • simple black-and-white images
  • images with repeated pixel values

For example, a row of pixels such as:

0001001001000

contains repeated 0s and 1s. RLE can group these into runs, such as:

  • 3 zeros
  • 1 one
  • 2 zeros
  • 1 one
  • 2 zeros
  • 1 one
  • 3 zeros

This usually needs less storage than storing each pixel separately.

When RLE does not work well

RLE is not effective if the data changes often and does not contain long runs.

For example, if every character or pixel is different from the next one, then storing count-value pairs may not reduce size much. In some cases, it can even make the file larger.

So RLE is best when the data has lots of repetition.

RLE with bitmap images

RLE is very useful for some bitmap images, especially simple ones.

In a black-and-white bitmap image, each pixel may be represented using values such as:

  • 0 for black
  • 1 for white

If many pixels of the same colour appear in a row, RLE can store the run length and the pixel value instead of storing every single pixel.

For example, if a row begins with:

0001111000

this can be stored as:

  • 3 zeros
  • 4 ones
  • 3 zeros

That is more compact than writing all ten bits separately.

The same idea can be used across an image. If long runs continue from the end of one line to the start of the next, the runs can still be grouped.

This makes RLE useful for images with:

  • simple shapes
  • large blocks of one colour
  • repeated patterns

Black-and-white bitmap example

A black-and-white 8 × 8 image has 64 pixels.

If each pixel uses 1 byte, the uncompressed image needs:

64 bytes

Using RLE, the same image can be stored in fewer bytes if many neighbouring pixels are the same colour. In the example given, the image size falls to 30 bytes.

That shows how repeated pixel values can save storage space.

Colour bitmap example

Colour images can also use RLE.

In a colour bitmap, each pixel may store RGB values:

  • Red
  • Green
  • Blue

If one pixel uses 3 bytes, then an 8 × 8 colour image needs:

[ 8 \times 8 \times 3 = 192 \text{ bytes} ]

If many neighbouring pixels are the same colour, RLE can store:

  • the count
  • the RGB value of that colour

For example:

  • 10 black pixels could be stored as count 10 plus RGB 0, 0, 0
  • 5 red pixels could be stored as count 5 plus RGB 255, 0, 0
  • 3 green pixels could be stored as count 3 plus RGB 0, 255, 0

In the example given, the 8 × 8 colour image is reduced from 192 bytes to 92 bytes.

This works because repeated colours are grouped together.

General ways file size can be reduced

The sources also describe some general ways of reducing file size. These are useful examples of compression in practice.

For movie or video files, file size can be reduced by:

  • lowering the sampling rate
  • lowering the sampling resolution
  • lowering the frame rate

For image files, file size can be reduced by:

  • cropping the image
  • reducing the colour depth or bit depth
  • reducing the image resolution

These changes make the file smaller, but they often reduce quality. So they fit best with situations where some loss of quality is acceptable.

Putting it all together

To answer compression questions well, remember these core ideas.

Compression is needed because it saves storage space and reduces the time needed to send files.

Lossless compression keeps all the original data, so it is used when accuracy matters.

Lossy compression removes some data, so it is used when smaller size matters more than perfect quality.

Text files are usually compressed with lossless methods because every character matters.

Bitmap images can be compressed using lossy methods such as JPEG, or lossless methods such as RLE when there are repeated pixel patterns.

Vector graphics can be compressed because they are often stored as text-based instructions.

Sound files such as MP3 use lossy compression by removing sounds people are less likely to notice.

RLE is a key lossless method you must know. It works by replacing repeated data with count-value pairs.

Key Terms

Compression — reducing the size of a file so it takes less storage space and can be sent faster.

Lossless compression — compression where the original file can be restored exactly after decompression.

Lossy compression — compression where some original data is removed permanently, so the exact original file cannot be restored.

Decompression — expanding a compressed file back into a usable form.

RLE (run-length encoding) — a lossless method that stores repeated data as a count and a value.

Run — a sequence of the same item repeated one after another.

Bitmap image — an image made from pixels.

Vector graphic — an image stored as drawing instructions, such as lines and curves, instead of pixels.

JPEG — a common lossy compression format for bitmap images, especially photographs.

MP3 — a lossy compression format for sound files.

MP4 — a compressed multimedia format that can store video, audio, images and animation.

Perceptual music shaping — removing sounds that people are unlikely to hear, in order to reduce sound file size.

Bit rate — the number of bits used or sent each second; in sound files, a higher bit rate usually means better quality and a larger file.

RGB — red, green and blue colour values used to represent colour in digital images.

Frame rate — the number of video frames shown or recorded each second.

Resolution — the amount of detail in an image, often linked to the number of pixels.

Bit depth / colour depth — the number of bits used to store the colour of each pixel.

Exam-Style Questions & Answers

Question 1 (4 marks)

Explain why compression is needed. Give two reasons.

Model Answer:

  1. Compression reduces the size of a file, so it uses less space on secondary storage such as a hard drive, phone or memory card. This means more files can be stored on the same device.
  2. Compression makes files faster to upload, download or stream because there is less data to send across a network. This reduces transfer time.
  3. Compression is especially useful for large files such as music, images and videos, which would otherwise take up a lot of space.
  4. It also helps when devices have limited storage or when internet speed is slow, because smaller files are easier to manage.

Question 2 (4 marks)

Describe the difference between lossy and lossless compression.

Model Answer:

  1. Lossless compression reduces file size without removing any original data, so the file can be decompressed back to exactly the same form.
  2. Lossy compression reduces file size by removing some data permanently, so the original file cannot be fully recovered.
  3. Lossless compression is used when accuracy is important, such as in text files or documents where every character matters.
  4. Lossy compression is used when a small drop in quality is acceptable, such as with sound, photographs or video, because it usually gives a much smaller file.

Question 3 (4 marks)

A student wants to compress a text document before sending it. State which method should be used and justify your answer.

Model Answer:

  1. The best method is lossless compression.
  2. A text document must stay exact, because every letter, symbol and number may be important.
  3. If lossy compression was used, some data could be removed permanently, which could change the meaning of the document.
  4. Lossless compression still reduces the file size for easier storage and faster sending, but it keeps all the original data.

Question 4 (4 marks)

Explain how run-length encoding (RLE) compresses a text file.

Model Answer:

  1. RLE looks for characters that are repeated one after another in the file. These repeated groups are called runs.
  2. Instead of storing each repeated character separately, it stores the count and the character value.
  3. For example, AAAABBBCCDAA can be stored as 4A3B2C1D2A, which is shorter because repeated letters are grouped together.
  4. RLE is lossless because the original text can be rebuilt exactly from the count-value pairs during decompression.

Question 5 (4 marks)

Explain how a sound file and a bitmap image can be compressed.

Model Answer:

  1. A sound file can be compressed using a lossy method such as MP3, where sounds people are less likely to hear are removed to reduce file size.
  2. This includes frequencies outside the normal hearing range and quiet sounds hidden by louder ones.
  3. A bitmap image can be compressed using a lossy method such as JPEG, which removes some image detail to make the file smaller.
  4. A bitmap image can also be compressed using RLE if it contains repeated pixel values, because runs of the same colour can be stored as a count and a value instead of storing every pixel separately.

Sign in to view full notes