Compression
In one sentence: Compression represents data using fewer bits, either preserving all original information or permanently discarding some detail.
What you will learn
You will distinguish lossless and lossy compression, trace run-length encoding, and describe how transform coding reduces data.
Before you begin
A computer represents data using bits. A shorter representation can take less storage and can reduce the time needed to transfer a file. Compression must still produce data suitable for its intended use.
Key words
| Term | Simple meaning |
|---|---|
| Compression | Encoding data in a form that uses fewer bits than the original. |
| Lossless | Compression from which the original data can be recovered exactly. |
| Lossy | Compression that permanently removes some information. |
| Run | A sequence of the same symbol repeated consecutively. |
| Run-length encoding (RLE) | A lossless method that represents a run by its symbol and repetition count. |
| Transform coding | A method that transforms data into components, then reduces their precision or detail before encoding. |
| Quantization | Reducing the precision of transformed values; in lossy compression, some original information is discarded here. |
The idea, step by step
Lossless and lossy compression
Lossless compression reduces the representation while preserving enough information to reconstruct the original exactly. It is appropriate when every detail must remain unchanged, such as source code, text, or a database file. If decompressed correctly, the result is identical to the input.
Lossy compression reduces the representation by permanently removing information judged less important for the intended use. The reconstructed result is not identical to the original. It is common for photographs, audio, and video when a smaller file is more useful and some loss in quality is acceptable.
| Question | Lossless | Lossy |
|---|---|---|
| Can the exact original be recovered? | Yes | No |
| What is traded for a smaller representation? | Repeated or statistically redundant patterns are encoded more efficiently. | Some information is discarded, often allowing a greater size reduction. |
| Suitable use | Text, program files, or data that must remain exact. | Images, audio, or video where a controlled quality loss is acceptable. |
Compression does not always make every file smaller. A method can add metadata or bookkeeping, and a file with little repeated or removable information may gain little or even become larger.
Run-length encoding
RLE is a lossless method that is useful when the same symbol appears repeatedly next to itself. It replaces each run with the symbol and the number of times it repeats.
For example, consider the original string:
RRRRRRYYTTTTQQ
Its runs are six R symbols, two Y symbols, four T symbols, and two Q symbols. Using the simple count-then-symbol format, it becomes:
6R2Y4T2Q
The original has 14 symbols; the encoded form has 8 symbols. Under the simplified assumption that each symbol occupies one byte and counts fit in one character, the representation is shorter. Real formats need rules for separating counts and symbols, so overhead affects the actual size.
RLE is helpful for data with long repeated runs, such as a simple image containing broad areas of the same colour. It is less useful for data with few consecutive repeats: encoding each separate symbol with a count can make the result longer.
Transform coding
Transform coding is used in lossy formats such as JPEG images. It changes how data is represented so that less important detail can be reduced before the result is encoded.
For a simplified JPEG-style process:
- Divide an image into small blocks of pixels.
- Apply a mathematical transform, such as the discrete cosine transform, to represent patterns in each block as frequency components.
- Quantize the components. The encoder reduces their precision, especially for fine detail that people are less likely to notice.
- Encode the resulting values more compactly, for example by scanning and encoding repeated values.
Quantization is the lossy step: discarded precision cannot be restored exactly. The later encoding can be lossless, but it cannot bring back information already removed. Stronger quantization can produce a smaller file while making the reconstructed image less faithful.
Transform coding differs from RLE. RLE finds consecutive repetitions directly in a sequence. Transform coding first changes the representation into components, then reduces selected detail before compact encoding.
Worked example
A student wants to share a long screen recording and also keep the exact text of a programming assignment.
- For the screen recording, lossy compression may be appropriate if a modest quality reduction is acceptable in return for a smaller file and faster transfer.
- For the assignment text, lossless compression is appropriate because every character must be restored exactly.
- If an image contains large regions of repeated pixel values, RLE may encode some sequences compactly. If its data has few repeated runs, RLE alone may not reduce its size.
The right choice depends on what must be preserved, how much size reduction is needed, and how the compressed data will be used.
Check your understanding
- What does lossless compression preserve that lossy compression does not?
- Encode
AAAAABBBCCusing count-then-symbol RLE. How many symbols are in the input and encoded output? - At what stage of transform coding is information discarded?
- Why might RLE be ineffective for a sequence with few repeated adjacent symbols?
Common mix-up
It is easy to think: Every compression method can recreate the original data.
Instead, remember: Lossless methods preserve enough information for exact recovery. Lossy methods discard information, so decompression can only produce an approximation.
Quick recap
- Compression uses fewer bits to represent data and can reduce storage and transfer requirements.
- Lossless compression allows exact reconstruction; lossy compression permanently removes information.
- RLE replaces consecutive runs with counts and symbols, so it works best when runs are common.
- Transform coding changes the representation and reduces selected precision; quantization makes the result lossy.
Textbook reading
Read Computer Science (Baumgarten, Ganea, Turland, 2025), A1 Computer fundamentals > A1.1 Computer hardware and operation > A1.1.8 Compression, Book pp. 19–21 (PDF pp. 29–31). The syllabus outcome is A1.1.8: “Describe the concept of compression” (Guide p. 29; PDF p. 35).
Sources and further learning
This lesson addresses the complete listed scope of IB Computer Science Guide 2027 outcome A1.1.8, including lossy and lossless methods, RLE, and transform coding. The worked examples are original.