1.3 Compression
A file is compressed when its data is represented using fewer bits. The aim is not simply to produce a smaller number: the chosen method must still preserve everything that matters for the file's purpose.
By the end of this section, you should be able to:
- Explain why reducing file size can improve storage and data transfer.
- Distinguish between lossless and lossy compression.
- Encode and decode simple data using run-length encoding.
- Explain how text, bitmap, vector and sound files can be compressed.
- Justify a suitable method from the file type, data pattern and intended use.
Exam tip: answer beyond “the file becomes smaller”
State a practical consequence. A smaller file can require less storage, take less time to transmit, use less network capacity, or make streaming more reliable on a limited connection.
Why Reduce File Size?
Consider a wildlife sensor that sends photographs from a remote island. Its connection is slow and its battery is limited. Sending fewer bits means each upload can finish sooner, so less transmission time and energy are needed.
| Situation | Benefit of compression |
|---|---|
| Archiving project files | More files fit within the available storage capacity. |
| Downloading a software package | Fewer bits have to cross the network. |
| Streaming audio | A lower data rate can reduce interruptions. |
| Backing up data to cloud storage | Upload time and storage use can both be reduced. |
Common mistake: compression and capacity are not the same
Compression does not increase the physical capacity of a drive. It reduces the number of bits used by the files, allowing more data to fit within the same capacity.
Choose What Must Be Preserved
The first decision is whether the exact original must be reconstructed. This separates the two main categories of compression.
| Question | Lossless compression | Lossy compression |
|---|---|---|
| Can decompression restore every original bit? | Yes | No |
| What creates the reduction? | Repeated or predictable data is coded more efficiently. | Some detail is discarded or represented less precisely. |
| When is it appropriate? | When every symbol, value or instruction must remain unchanged. | When a small quality change is acceptable in return for a smaller file. |
| Typical examples | Program code, documents, database exports, vector drawings. | Photographs and audio prepared for delivery or streaming. |
A practical decision test
- Would one altered character, value or drawing instruction matter?
- Does the data contain repeated runs or other patterns?
- Is the file a delivery copy where a small perceptual change is acceptable?
- Which matters more in this situation: exact recovery, quality or file size?
Common mistake: choosing by file extension alone
The intended use matters. A photograph kept as a scientific record may need a lossless master, while a smaller lossy copy may be suitable for a web preview.
Exam tip: use a three-link justification
Name the method, identify the important property of the file, and state the consequence. For example: “Use lossless compression because every database value must be restored exactly; losing a digit could make the records incorrect.”
Run-Length Encoding
Run-length encoding (RLE) is a lossless method for data that contains consecutive repetitions. A run is replaced by a compact token containing the repeated value and the number of times it occurs.
classroom notation: count + value
Worked example
A simple image row uses letters to represent colours:
RRRRRRBBBBBGGYYYY
| Detected run | Count | Value | Token |
|---|---|---|---|
| RRRRRR | 6 | R | 6R |
| BBBBB | 5 | B | 5B |
| GG | 2 | G | 2G |
| YYYY | 4 | Y | 4Y |
RLE result: 6R 5B 2G 4Y
Decoding reverses the process: output six R values, then five B values, then two G values and finally four Y values. The original row is recovered exactly.
Exam tip: show both encoding and reconstruction
An RLE explanation is stronger when it states that the decoder expands each token back into the stated number of repeated values.
When RLE helps—and when it does not
| Data pattern | Likely result | Reason |
|---|---|---|
| Large areas of one colour in a diagram | Effective | Many pixels form long runs. |
| Repeated spaces or symbols in structured text | Sometimes effective | Only the repeated sections produce useful runs. |
| A detailed photograph | Often ineffective | Neighbouring pixel values change frequently. |
| QWERTYUI | May grow | Each one-symbol run needs an extra count. |
Common mistake: RLE always reduces the data
If most runs contain only one value, storing a count beside every value introduces overhead. The encoded result can be as large as, or larger than, the original.
Technical note: 6R is simplified notation
A real format needs an agreed way to distinguish counts from data values and may store each field in a fixed number of bits. For exam questions, follow the representation defined in the question.
Interactive: Run-Length Encoding Visualiser
Enter a sequence of characters. The widget groups repeated values into runs and shows the compressed representation.
Compression by File Type
Different files contain different patterns. The compression method should match both the structure of the data and the purpose of the output.
| File type | Possible lossless approach | Possible lossy approach |
|---|---|---|
| Text | Replace repeated sequences with shorter references, or give frequent symbols shorter codes. | Normally unsuitable because omitted or changed characters alter the content. |
| Bitmap image | Use RLE where neighbouring pixels repeat, especially in flat-colour graphics. | Reduce colour precision or approximate fine detail. |
| Vector graphic | Compress repeated drawing commands, attribute names and coordinate patterns without changing the objects. | Usually avoid removing instructions when the drawing must remain editable and exact. |
| Sound | Store predictable sample patterns or differences more compactly while retaining every sample. | Discard or represent less precisely details that are unlikely to be noticed. |
Text: preserve every character
Text, source code and structured data usually require lossless compression. A compressor can detect repeated words, byte sequences or character patterns and store short references to them. Another approach assigns shorter bit patterns to symbols that appear frequently. In both cases, the decoder must reproduce the exact original sequence.
Common mistake: “text cannot be compressed”
Text can be compressed, but the method should normally be lossless. The characters are represented more efficiently rather than deleted.
Bitmap images: exploit pixels or reduce detail
A simple icon may contain long horizontal bands of identical pixels, making RLE useful. A photograph normally changes colour from pixel to pixel, so a lossy method can instead group similar colours or simplify detail. The second approach can produce a much smaller delivery copy, but it cannot recreate the exact original pixels.
Vector graphics: compress the instructions
A vector file stores objects and their properties rather than a grid of pixels. Repeated command names, style descriptions and coordinate patterns can be compressed losslessly. A text-based vector description, such as an SVG-style file, is especially suitable for general lossless pattern compression.
Common mistake: a vector must become a bitmap before compression
Vector instructions can be compressed directly. Converting to a bitmap changes how the graphic is represented and can remove the advantages of editable, scalable objects.
Sound: exact samples or perceptual quality
Lossless audio compression uses relationships between samples to represent the same sample sequence with fewer bits. Lossy audio compression may simplify or remove information judged less noticeable to a listener. The choice depends on whether the recording is an exact master or a smaller listening copy.
Exam tip: “how” is different from “why”
A complete answer should identify the mechanism as well as the benefit. For example, “RLE replaces a run of identical pixels with a count and a pixel value, so fewer values are stored when the run is long.”
Interactive: Bitmap RLE Compression Grid
This widget scans a tiny bitmap row by row. Repeated colours in the same row are compressed using run-length encoding.
Interactive: Lossy Colour Reduction Visualiser
Reduce the number of colours in the image. The image becomes smaller to store, but some detail is permanently lost.
Interactive: JPEG Lossy Compression Simulator
Upload an image or use the sample image. Change the JPEG quality to see how lossy compression reduces file size by removing or simplifying some image detail.
Optional Extension: Frequency-Based Coding
Huffman coding is useful enrichment because it shows another way a text or sound file can be compressed without losing information.
Suppose an original data set contains four symbols with these frequencies:
| Symbol | Frequency | One possible code |
|---|---|---|
| S | 13 | 0 |
| A | 6 | 10 |
| Q | 1 | 110 |
| N | 4 | 111 |
The common symbol S receives a one-bit code. Less frequent symbols use longer codes. The exact codes depend on the tree produced from the frequencies; equivalent valid trees can assign different bit patterns.
Click the original diagram to enlarge it.
Why the codes can be decoded
The codes are prefix-free: no complete symbol code is the beginning of another complete code. The stream below therefore has one left-to-right reading:
010110111 → 0 | 10 | 110 | 111 → S A Q N
Common mistake: “short codes” are automatically safe
Variable-length codes need a rule that marks each boundary unambiguously. Prefix-free codes achieve this without inserting a separator between every symbol.
Exam Tip
Learn RLE and file-type choices first. Treat Huffman trees and the prefix property as extension knowledge unless your teacher or assessment materials require them.
Interactive: Huffman Coding / Prefix Code Visualiser
Type a short text. The widget counts character frequencies, gives shorter codes to common characters, and demonstrates decoding using the prefix property.
Building a Strong Justification
Compression questions often ask for a decision rather than a definition. Use this chain:
method → mechanism → effect → suitability
Example scenario
A museum keeps a master scan of a fragile poster and also places a preview on its website.
| File | Choice | Reasoning |
|---|---|---|
| Preservation master | Lossless | Every original pixel value remains recoverable for future analysis and reproduction. |
| Website preview | Lossy may be suitable | A small reduction in fine detail may be acceptable, while the smaller file loads faster. |
Improve vague statements
Replace “lossy is better for images” with a conditional explanation: “Lossy compression is suitable for this web preview because exact pixels are not required, and removing some detail reduces the transfer size.”
Practice
Core questions
- Explain two practical benefits of reducing a file's size.
- Distinguish lossless compression from lossy compression.
- Compress TTTTTSSUUUUUUUVV using count-then-value RLE.
- Decode 3C 1A 4T 2S.
- Explain why RLE is likely to work better on a simple flag graphic than on a photograph.
- Explain how a text file can be compressed while preserving every character.
- Describe one lossless way to reduce the size of a vector graphic.
- Explain one way a sound file can be made smaller using lossy compression.
Decision questions
- A hospital sends a diagnostic image to a specialist. Recommend a compression category and justify your answer.
- A school publishes a background photograph on its homepage. Explain why a different compression choice may be appropriate from the hospital example.
- A pixel row is RGBRGBRGBRGB. Evaluate whether simple RLE is likely to reduce its size.
- A logo is stored as vector objects. Explain why converting it to a low-resolution bitmap is not the same as losslessly compressing the vector file.
Optional extension
- Using S = 0, A = 10, Q = 110 and N = 111, encode SANSA.
- Explain why the four codes above are prefix-free.