AS Level · Topic 1.3
A-Level Computer Science 9618 / Topic 1 / AS

Smaller files.

Images, sound and video files are huge, and every one of them has to be stored and sent somewhere. Compression shrinks them. Lossless methods rebuild the original exactly; lossy methods throw away data you are unlikely to miss. Learn both, apply run-length encoding, and justify the right method for a task.

The key idea

Compression reduces file size so files need less storage and less bandwidth, and are transmitted faster. Lossless compression reduces the size with no loss of data: the original is rebuilt exactly. Lossy compression permanently removes data, usually detail that human senses notice least, giving much smaller files. Run-length encoding (RLE) is lossless: each run of identical values is replaced by a count and the value.

run-length encoding one row of 12 pixels W W W W W B B B W W W W 5 W3 B4 W 12 values become 3 (count, colour) pairs = 6 values, and decode back exactly
Fig. 1: run-length encoding replaces each run of identical pixels with a count and a colour
Section 01

Lossy or lossless?

In the lab, try the lossy mode on the image and turn the amount of compression up. Notice which details disappear first, and that they never come back.

LosslessLossy
DataNo data is lost; the original is restored exactlyData is permanently removed; the original cannot be restored
Size reductionSmaller reductionMuch larger reduction
ExamplesRLE, ZIP, PNG, FLACJPEG, MP3, most streamed video
Use forText, program code, spreadsheets, databases, medical or legal imagesPhotos, music and video for the web, streaming and social media
Examiner trap

A justification needs a reason linked to the scenario. "Lossy because it is smaller" earns less than "lossy, because the photo only needs to load quickly on a web page and the lost detail will not be noticed".

Section 02

Run-length encoding.

Switch the lab to RLE and encode the image row by row. The more long runs there are, the bigger the saving.

Worked example Text
Use RLE to compress AAAAABBBCCCCCCCCD. Each character and each count is stored in 1 byte. How many bytes are saved?
  1. Runs: 5 A, 3 B, 8 C, 1 D.
  2. Encoded: 5A 3B 8C 1D, which is 4 pairs = 8 bytes.
  3. Original: 17 characters = 17 bytes. 17 − 8 = 9 bytes saved.
Answer: 5A 3B 8C 1D, saving 9 bytes
Worked example Decode a bitmap row
A 1-bit image row is stored as the (count, colour) pairs 4 1, 3 0, 2 1. Write out the row.
  1. 4 1 means four pixels of colour 1: 1111.
  2. 3 0 means three of colour 0: 000. 2 1 means two of colour 1: 11.
Answer: 111100011 (9 pixels)
Your turn
Encode GGGGGGGGHHHHKKKKKKKKKKKK with RLE and give the saving if every count and character takes 1 byte.
Stage 1 · Learn

Check what the sim just showed you

Four quick checks. Each correct answer earns XP and records this skill on your profile.

Quick check+10 XP

What is the main feature of lossless compression?

Quick check+10 XP

In the lab, when does RLE give the biggest saving?

Quick check+10 XP

What is WWWWWBBBWWWW after run-length encoding?

Quick check+10 XP

Which is a lossy technique?

Section 03

Compressing each type of file.

The syllabus expects you to say how text, bitmaps, vectors and sound can each be compressed.

FileHow it can be compressedType
Text fileRLE for repeated characters; replacing repeated words or phrases with short codes held in a dictionary stored with the fileLossless only: every character must be recovered
Bitmap imageRLE on runs of identical pixels; or reduce the resolution or colour depth, or merge similar colours into one (as JPEG does)RLE is lossless; the others are lossy
Vector graphicRemove redundant data in the drawing list and apply a lossless method such as RLE or ZIP to the object propertiesLossless: the objects must be redrawn exactly
Sound filePerceptual coding: remove frequencies outside the range of human hearing and quieter sounds masked by louder ones (as MP3 does); or reduce the sampling rate or resolutionLossy (lossless formats such as FLAC also exist)
Examiner trap

Never suggest lossy compression for a text file or program. Losing even one character changes the meaning, and a program would no longer run.

Stage 2 · Exam

Exam-style questions

Unlocks once the four checks above are done. Worth more XP, written to AS Paper 1 and Paper 2 standard.

Finish the four checks above to unlock the exam questions
Exam style+20 XP

What is PPPPQQQQQQRR after run-length encoding?

Exam style+20 XP

A hospital stores X-ray images that doctors use for diagnosis. Which compression should be used?

Exam style+20 XP

Why does RLE make the data ABCDEF larger?

Exam style+20 XP

How is a sound file typically compressed using lossy compression?

Skill unlocked

Compression, mastered.

This skill is now recorded on your profile. Keep the chain going.

-Rank -Level -Score -Topics
Go deeper · practice
Six original Cambridge-style questions
The need for compression, lossy and lossless methods, run-length encoding and compressing each type of file.