Teachers' Portal  /  IGCSE Computer Science 0478  /  Data representation  /  Lossy and lossless compression
Lesson pack · IGCSE Computer Science 0478 · 1.3 · IGCSE

Lossy and lossless compression

Smaller files. Photos, songs and videos are big, and big files are slow to send and fill up storage.

Student lesson Lab, full screen
At a glance

The shape of the lesson

Topic
Data representation
Syllabus reference
Cambridge IGCSE Computer Science 0478, 1.3
Level
IGCSE
Duration
45 minutes, lab-led; stretches to a 60 minute block (see the contingency notes)
Position in topic
Lesson 10 of 10 in Data representation; follows Measuring data storage and calculating file sizes
Central model
Fig. 1: RLE stores each run of identical values as a count and the value
Cooperative structure
Assessment
Stage 1 check and the exam-style plenary from the student lesson, plus the worksheet as the exit task
Learning objectives

By the end of the lesson, learners can

Section by section
  • Why compress?: Smaller files help everyone who stores or sends them.
  • Lossless: run length encoding: In the lab, type some text or paint a 1-bit image and watch RLE replace each run of identical values with a count and the value.
  • Lossy: data is gone: Switch the lab to lossy mode and reduce the resolution, colour depth or sample rate.
Key points to leave on the board
  • Why: less storage, less bandwidth, shorter transmission time.
  • Lossy: data permanently removed (resolution, colour depth, sample rate, sample resolution).
  • Lossless: no permanent loss; the original is rebuilt exactly (RLE).
  • RLE: each run of identical values becomes a count and the value.
The core visual model

One picture every idea returns to

Compression reduces the size of a file, so it needs less storage space, less bandwidth and a shorter transmission time. Lossy compression permanently removes data, for example by reducing the resolution or colour depth of an image, or the sample rate or resolution of a sound; the original cannot be recovered. Lossless compression reduces the size without permanent loss of data, so the original file can be rebuilt exactly; run length encoding (RLE) is an example.

run length encoding (RLE) of one row of 15 pixels 8 white3 black4 white 8 W3 B4 W 15 values become 3 pairs: lossless, the row can be rebuilt exactly
Fig. 1: RLE stores each run of identical values as a count and the value
Lesson sequence

Forty-five minutes, phase by phase

TimePhaseWhat happens in the roomGrouping
0 to 5 minHook: predictOpen with the question in the title, Smaller files.. Photos, songs and videos are big, and big files are slow to send and fill up storage. Put the figure on the board: it is the one picture every idea in the lesson returns to.Whole class, then pairs
5 to 13 minTeach and check: Why compress?Smaller files help everyone who stores or sends them.Whole class, then pairs
13 to 21 minTeach and check: Lossless: run length encodingIn the lab, type some text or paint a 1-bit image and watch RLE replace each run of identical values with a count and the value. Project tab 5 of the lab (Run length encoding) and let a student drive it while the class predicts. Model the worked example Encode and count on the board, one line at a time. Your turn: Decode the RLE data 3R 2G 5B.Pairs
21 to 30 minTeach and check: Lossy: data is goneSwitch the lab to lossy mode and reduce the resolution, colour depth or sample rate. Project tab 5 of the lab (Lossy compression) and let a student drive it while the class predicts.Anticipation / Reaction Guide
30 to 36 minCheckThe four Stage 1 questions from the student lesson, answered on mini whiteboards or by show of fingers. Re-teach the one most get wrong.Individual, then whole class
36 to 43 minPlenaryThe exam-style questions from Stage 2, one at a time: thirty seconds alone, then a random call. The feedback lines in the key give the examiner's wording.Individual, random call
43 to 45 minExit and homeworkSet the worksheet below (6 questions, 13 marks). Learners self-assess against the objectives before they leave.Individual
Timing and contingency

Protect the parts that carry the learning

The sequence carries about fifty minutes of material, so the lab is driven by one student while the class predicts rather than explored freely. Times are a guide.

Protected: the check at 30 minutes. It is the only individual read on whether the key idea landed, and it is not cut.

Cut first if the build overruns: the last check in Lossy: data is gone, never the check or the plenary.

In a 60 minute block: run worksheet questions 1 to 3 in class as a Rally Coach set and model both the hardest worked example and its check live.

Running the cooperative task

Anticipation / Reaction Guide

Before the lesson, students mark five statements true or false. After the teaching they mark them again and justify any change of mind in one sentence each. The statements come from the examiner traps below.

The full structure, with its variations and the other twelve, is on the cooperative-learning page.

Examiner traps to pre-empt

What to head off, and how

Trap learners fall intoTeaching move that pre-empts it
"It makes the file smaller" only restates compression.For a mark, give the effect: less storage, less bandwidth or a shorter transmission time.
RLE does not always make a file smaller.ABCD encodes as 1A 1B 1C 1D, which is twice as long.
Never choose lossy compression for text or program files: a lost character or instruction changes the meaning or stops the program working.Return to Lossy: data is gone: ask a pair to state the correct version and show it on the lab before moving on.
Worksheet

Lossy and lossless compression: worksheet

6 questions, 13 marks. Written in the style of the papers; no past paper question is reproduced.

NameClassDate
Q1 · explain[2 marks]

Give two reasons why files are compressed before they are sent over the internet.

Q2 · explain[2 marks]

Describe the difference between lossy and lossless compression.

Q3 · apply[2 marks]

Use run length encoding to compress the text PPPPPQQRRRRRRR.

Q4 · calculate[2 marks]

The text PPPPPQQRRRRRRR is stored using 1 byte per character. In the RLE version each count and each character uses 1 byte. Calculate the number of bytes saved.

Q5 · apply[3 marks]

A school uploads photos to its website and also sends spreadsheets of exam marks. State which type of compression should be used for each and justify your answers.

Q6 · analysis[2 marks]

A student says "run length encoding always makes a file smaller." Explain, with an example, why this is not true.

Answer key and marking notes
Q1 · 2 marks
  • Less bandwidth is needed.
  • The transmission time is shorter (also accept: less storage space is needed).
Q2 · 2 marks
  • Lossy compression permanently removes data, so the original file cannot be recovered.
  • Lossless compression reduces the file size without permanent loss of data, so the original can be rebuilt exactly.
Q3 · 2 marks
  • Runs: 5 P, 2 Q, 7 R.
  • 5P 2Q 7R.
Q4 · 2 marks
  • Original 14 bytes; RLE 3 pairs = 6 bytes.
  • 14 − 6 = 8 bytes saved.
Q5 · 3 marks
  • Photos: lossy, because the file is much smaller so the page loads faster.
  • The data lost from a photo is unlikely to be noticed.
  • Spreadsheets: lossless, because every value must be kept exactly; losing data would change the marks.
Q6 · 2 marks
  • RLE only saves space when there are runs of repeated values.
  • e.g. ABCD becomes 1A 1B 1C 1D, which is 8 values instead of 4, so the file is larger.
Quiz

6 multiple-choice questions on this lesson

From the topic question bank. Assign them in the app for a marked score in your gradebook, or run the topic as a class game.

Q11 mark

Which is a benefit of compressing a file before it is sent over the internet?

  • AIt uses less bandwidth, so it is transmitted in a shorter time
  • BIt always improves the quality of the file
  • CIt stops the file from being intercepted
  • DIt removes any viruses in the file
Q21 mark

Which statement describes lossy compression?

  • AData is permanently removed, so the original file cannot be recovered
  • BThe file is made smaller and can be rebuilt exactly as it was
  • CRepeated values are stored as a count and the value
  • DThe file is split into packets before it is sent
Q31 mark

A school compresses four files before emailing them. Which file must be compressed using lossless compression?

  • AA spreadsheet of students' exam marks
  • BA photo of the school sports day
  • CA recording of the school choir
  • DA video of the school play
Q41 mark

The row of pixels shown is compressed using run length encoding (RLE). What is the encoded data?

12345678910W = white pixel, B = black pixel
  • A3W 5B 2W
  • B5W 5B
  • C3B 5W 2B
  • D3W 5B
Q51 mark

The text WWWWWWWWWWBBWWWWWW is stored with 1 byte per character. It is compressed using RLE, where each count and each character uses 1 byte. How many bytes are saved?

  • A12
  • B6
  • C15
  • D3
Q61 mark

The text ABABABAB (1 byte per character) is compressed using RLE with 1 byte per count and 1 byte per character. What happens?

  • AThe file grows from 8 bytes to 16 bytes, because every run has a length of 1
  • BThe file shrinks to 4 bytes, because the pattern AB repeats 4 times
  • CThe file stays at 8 bytes, because RLE never changes text
  • DThe file shrinks to 2 bytes, because only two different letters are used
Quiz key
Q1: A

A smaller file needs less bandwidth and takes less time to upload or download. Compression never improves quality; lossy compression lowers it. Protecting a file from interception is the job of encryption, and removing viruses is the job of anti-malware software.

Q2: A

Lossy compression permanently removes data, for example by lowering resolution or sample rate, so the original cannot be restored. Rebuilding the file exactly describes lossless compression. Storing runs as a count and value is run length encoding, a lossless method. Splitting into packets is about data transmission, not compression.

Q3: A

If any data in the spreadsheet were lost, a mark could be changed, so it must be rebuilt exactly: lossless. A photo, a recording and a video can lose a little detail without people noticing, so lossy compression is acceptable for them.

Q4: A

Pixels 1 to 3 are white, 4 to 8 black and 9 to 10 white, so the runs are 3W 5B 2W. 5W 5B adds the two white runs together, which loses where they are. 3B 5W 2B swaps the colours. 3W 5B misses the final run, so the image could not be rebuilt.

Q5: A

The original has 18 characters = 18 bytes. The runs are 10W 2B 6W: 3 pairs = 6 bytes. 18 − 6 = 12 bytes saved. 6 is the size of the compressed data, not the saving. 15 counts only 1 byte per run. 3 is the number of runs.

Q6: A

No character is next to a copy of itself, so each is a run of 1: 1A 1B 1A 1B 1A 1B 1A 1B, which is 16 bytes, twice the original. RLE does not spot repeating patterns such as AB. RLE does change text when it encodes it. Knowing only which letters appear is not enough to rebuild the text, so it cannot shrink to 2 bytes.

Lab and demo

What to project

The student lesson embeds these tabs. Open them full screen on the projector; every slider has a drag handle on the stage.

Slides

Slides for this lesson

A projectable deck for every pack is on the way. Until then, present the student lesson full screen: its sections, figure and lab tabs follow the same order as the plan above.

Also for this topic

Everything else you can use

Original work by the TheLucidSTEM team. The worksheet items are written in the style of the papers; no past paper question is reproduced.
Previous lessonMeasuring data storage and calculating file sizes