Teachers' Portal  /  A-Level Computer Science 9618  /  Information representation  /  Lossy and lossless compression, run-length encoding
Lesson pack · A-Level Computer Science 9618 · 1.3 · AS Level

Lossy and lossless compression, run-length encoding

Smaller files. Images, sound and video files are huge, and every one of them has to be stored and sent somewhere.

Student lesson Lab, full screen
At a glance

The shape of the lesson

Topic
Information representation
Syllabus reference
Cambridge International AS and A Level Computer Science 9618, 1.3
Level
AS Level
Duration
45 minutes, lab-led; stretches to a 60 minute block (see the contingency notes)
Position in topic
Lesson 7 of 7 in Information representation; follows Sampled sound
Central model
Fig. 1: run-length encoding replaces each run of identical pixels with a count and a colour
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
  • Lossy or lossless?: In the lab, try the lossy mode on the image and turn the amount of compression up.
  • Run-length encoding: Switch the lab to RLE and encode the image row by row.
  • Compressing each type of file: The syllabus expects you to say how text, bitmaps, vectors and sound can each be compressed.
Key points to leave on the board
  • Why: less storage, less bandwidth, faster transmission.
  • Lossless: original rebuilt exactly (RLE, dictionary methods); for text, programs and critical data.
  • Lossy: data permanently removed (perceptual coding, lower resolution or colour depth); for media.
  • RLE: count and value for each run; only saves space when there are long runs.
The core visual model

One picture every idea returns to

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
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.. Images, sound and video files are huge, and every one of them has to be stored and sent somewhere. 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: Lossy or lossless?In the lab, try the lossy mode on the image and turn the amount of compression up. Project tab 5 of the lab (Lossless and lossy compression) and let a student drive it while the class predicts.Whole class, then pairs
13 to 21 minTeach and check: Run-length encodingSwitch the lab to RLE and encode the image row by row. Project tab 5 of the lab (Run-length encoding) and let a student drive it while the class predicts. Model the worked example Text on the board, one line at a time. Your turn: Encode GGGGGGGGHHHHKKKKKKKKKKKK with RLE and give the saving if every count and character takes 1 byte.Pairs
21 to 30 minTeach and check: Compressing each type of fileThe syllabus expects you to say how text, bitmaps, vectors and sound can each be compressed.Corners
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, 18 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 Compressing each type of file, 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

Corners

Put one answer or viewpoint in each corner of the room. Students move to the corner they agree with, justify the choice to a partner there, then hear one voice from each corner before the reveal.

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
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".Return to Lossy or lossless?: ask a pair to state the correct version and show it on the lab before moving on.
Never suggest lossy compression for a text file or program.Losing even one character changes the meaning, and a program would no longer run.
Worksheet

Lossy and lossless compression, run-length encoding: worksheet

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

NameClassDate
Q1 · explain[2 marks]

Explain why files are often compressed before they are sent across the internet.

Q2 · apply[3 marks]

A text file contains XXXXXXYYZZZZZZZZZW. Each character and each count is stored in 1 byte. (a) Show the result of run-length encoding. (b) Calculate the number of bytes saved.

Q3 · apply[4 marks]

A music streaming service and a hospital both need to compress image and sound files. Justify the type of compression each should use.

Q4 · explain[3 marks]

Describe how a sound file can be compressed using lossy compression.

Q5 · explain[3 marks]

Explain how a text file can be compressed, and why only lossless compression is suitable.

Q6 · analysis[3 marks]

A 1-bit image row is 10101010. It is compressed with RLE, storing each run as a (count, colour) pair. Explain the effect on the size of the data and state what type of image RLE suits.

Answer key and marking notes
Q1 · 2 marks
  • The file is smaller, so less bandwidth is needed.
  • It is transmitted / downloaded faster (also accept: uses less storage on the server).
Q2 · 3 marks
  • (a) 6X 2Y 9Z 1W.
  • Original 18 bytes; encoded 4 pairs = 8 bytes.
  • (b) 18 − 8 = 10 bytes saved.
Q3 · 4 marks
  • Streaming service: lossy.
  • Much smaller files stream quickly with less bandwidth; the removed data is not noticed by most listeners.
  • Hospital: lossless.
  • Medical images and recordings must be restored exactly, as lost detail could affect a diagnosis.
Q4 · 3 marks
  • Perceptual coding / psychoacoustics is used.
  • Frequencies outside the range of human hearing are removed.
  • Quieter sounds that are masked by louder sounds at the same time are removed (also accept: reduce the sampling rate or resolution).
Q5 · 3 marks
  • Runs of repeated characters can be replaced by a count and the character (RLE).
  • Repeated words or phrases can be replaced by short codes, with a dictionary of codes stored in the file.
  • Lossless is needed because every character must be restored exactly; losing any would change the meaning.
Q6 · 3 marks
  • Every run has length 1, so the row becomes 8 pairs: (1,1)(1,0)(1,1)(1,0)(1,1)(1,0)(1,1)(1,0).
  • 16 values are stored instead of 8, so the data gets larger.
  • RLE suits images with large areas of the same colour (long runs), such as simple diagrams or logos.
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 statement describes lossless compression?

  • AThe file is made smaller and the original data can be rebuilt exactly
  • BDetail that people are unlikely to notice is permanently removed
  • CThe file is always reduced to the smallest possible size
  • DThe resolution of the image is reduced so fewer pixels are stored
Q21 mark

Which file must be compressed with a lossless method?

  • AA spreadsheet of students' exam marks
  • BA holiday photograph for a social media post
  • CA music track for a streaming service
  • DA video trailer embedded on a website
Q31 mark

The string WWWWWWBBBWWWWWWWW is compressed with run-length encoding. Each count and each character is stored in 1 byte. How many bytes are saved?

  • A11
  • B6
  • C14
  • D9
Q41 mark

A row of a black-and-white bitmap is stored with run-length encoding as the (count, colour) pairs 3 0, 5 1, 2 0. Which row of pixels does this decode to?

  • A0001111100
  • B1110000011
  • C000111110
  • D00011111000
Q51 mark

The string ABCABCAB is compressed with run-length encoding, storing each count and each character in 1 byte. What is the result?

  • A16 bytes, 8 more than the original
  • B8 bytes, the same as the original
  • C3 bytes, a saving of 5 bytes
  • D6 bytes, a saving of 2 bytes
Q61 mark

How does perceptual coding, as used in MP3 files, reduce the size of a sound file?

  • AIt removes frequencies outside the range of human hearing and quiet sounds masked by louder ones
  • BIt replaces runs of identical samples with a count and a value, so no data is lost
  • CIt increases the sampling rate so that each sample needs fewer bits
  • DIt stores the waveform as a drawing list of curves that can be rescaled
Quiz key
Q1: A

Lossless compression removes only redundancy, so decompressing restores the original bit for bit. Removing detail people will not notice describes lossy compression, which usually gives smaller files than lossless, and lowering the resolution permanently discards data, which is also lossy.

Q2: A

Every number in the spreadsheet must be recovered exactly, so only lossless compression is acceptable. A photo, a streamed music track and a web video are for people to see or hear, where a small loss of quality is not noticed and the smaller file loads faster, so lossy compression is suitable.

Q3: A

The runs are 6 W, 3 B and 8 W, encoded as 6W 3B 8W: 3 pairs = 6 bytes. The original is 17 bytes, so 17 − 6 = 11 bytes are saved. 6 is the size of the encoded data, not the saving; 14 counts each run as a single byte; 9 wrongly merges the two runs of W into one run of 14.

Q4: A

Three 0s, then five 1s, then two 0s gives 0001111100 (10 pixels). 1110000011 swaps the colours, 000111110 ends with only one 0 instead of two, and 00011111000 has three 0s at the end instead of two.

Q5: A

No two neighbouring characters are the same, so there are 8 runs of length 1: 1A 1B 1C 1A 1B 1C 1A 1B, which is 16 bytes, double the original 8. RLE cannot detect the repeating pattern ABC, so the answers that store the pattern once (3 or 6 bytes) are wrong, and the encoded data is not the same size as the original.

Q6: A

Perceptual coding discards sounds that human ears cannot hear or would not notice, so the data is lost permanently but quality seems almost unchanged. Replacing runs with counts is run-length encoding, which is lossless and rarely helps with sound. Raising the sampling rate makes files bigger, and a drawing list is how vector graphics are stored.

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 lessonSampled sound