Information representation

Information representation

Compression: lossy, lossless and RLE

Compression AS Level 23 min read

On this page
  1. What you need to be able to do
  2. Why compress?
  3. Lossy and lossless compression
  4. Run-length encoding (RLE)
  5. How each kind of file is compressed
  6. Choosing lossy or lossless: case by case
  7. Mark-scheme focus
  8. Worked examples
  9. Summary
  10. Can I do this?
  11. Quiz

What you need to be able to do

Syllabus 1.3 Compression
  • 1.3Explain the need for compression and give examples of its use.
  • 1.3Describe lossy and lossless compression and justify using one in a given situation.
  • 1.3Explain how a text file, a bitmap image, a vector graphic and a sound file can be compressed, including run-length encoding (RLE).

You need: how text, bitmaps, vectors and sound are stored (pages 3-6 of this chapter). This page ties them together.

Why compress?

Compression reduces the file size. On its own that earns little; every mark comes from the consequence in the context given:

Smaller file means…so (the context)
less storage space usedmore files fit on the device / the web server / the user's storage; the email account's limit is not exceeded
faster to upload / download / transmitthe customer waits less; a teacher receives the attachment sooner
less bandwidth usedother transmissions on the network are not slowed; users with low bandwidth can still take part
less data allowance usedcheaper for users on a mobile data plan
less buffering when streaminga video call / live stream plays smoothly, people are not behind in the conversation
fits a maximum attachment sizeemail providers often limit attachment size
“Easier to transmit” and “takes less time” without saying what takes less time were not credited (November 2021). Write “faster to upload/download” or “less bandwidth”.

Lossy and lossless compression

LosslessLossy
What happensthe file is re-encoded more efficiently; no data is lostsome data is permanently removed (usually detail people won't notice)
Decompressed fileidentical to the original: it can be re-created exactlynot the same as the original: the removed data can't be got back
Size reductionsmallermuch smaller (a larger reduction than lossless)
Use it fortext, program code, spreadsheets, databases; images where every pixel matters (e.g. medical scans, line art)photographs, music, video, streaming: anything where a small loss of quality is not noticed
ExamplesRLE; ZIP; PNG; FLACJPEG; MP3; MP4 / most streaming video

Text must be lossless

No data can be lost: removing even one character changes the meaning; the file would be corrupted / not make sense / not open. Program code would no longer run.

Photos and video: usually lossy

The data removed is not noticed by the human eye (small changes of shade, fine detail), and the file becomes much smaller - important for real-time streaming where bandwidth is limited.

When lossless wins for media

When quality matters more than size: high-quality originals that will be edited again, or a fast dedicated connection that doesn't need the reduction.

No mark for the choice alone

“Tick lossy or lossless and justify” usually gives no mark for the tick: all the marks are for the justification, which must fit the situation.

Run-length encoding (RLE)

Run-length encoding is a lossless method: it finds runs of consecutive identical data (characters, pixel colours, sample values) and replaces each run with one copy of the value and the number of times it repeats.

RLE on text

  1. Read from the start. Count how many times the same character appears in a row (consecutively).
  2. Write the count, then the character.
  3. Start counting again at the next different character.
Original (12 characters)RLE
AAAABBBCCDAA4A3B2C1D2A (10 characters)

Note that the final “AA” is a new run: RLE only joins identical values that are next to each other.

RLE on a bitmap

Each pixel's colour code is the value. A row of 20 pixels, W = white, B = black:

PixelsRLE (count, colour)
WWWWWWWW BBB WWWWWWWWW8W 3B 9W: 3 pairs instead of 20 colour codes

Decoding

Read each pair as “this many of this value”: 3F2 = F2 F2 F2. In hex-coded images the count comes first and the colour is the last two hex digits: “11D” = one pixel of colour 1D.

When RLE does not help

RLE stores a value and a count for every run. If the data has few repeated runs, every run has length 1, so each value gains a count and the file gets bigger: Red Green Blue → Red 1 Green 1 Blue 1 (June 2023). Photographs, with many slightly different colours next to each other, often compress badly with RLE; simple graphics with large areas of one colour compress well.

1 Encode text

Compress “TTTTTOOOOP” with RLE.

AskedRLE stringKnowruns: 5 T, 4 O, 1 PRoutecount + character per run

  1. 5T4O1P: 10 characters → 6

2 Decode

Decode the RLE “3E5F2E”.

AskedoriginalKnowpairs (count, value)Routeexpand each pair

  1. 3E → EEE, 5F → FFFFF, 2E → EE
  2. EEEFFFFFEE

3 RLE makes it worse

Compress “ABCDABCD” with RLE. What happens?

Askedresult + effectKnowno two neighbours are equalRouteevery run has length 1

  1. 1A1B1C1D1A1B1C1D: 16 characters instead of 8
  2. The file doubles: RLE is not suitable for data without runs

4 A pixel row in hex codes

8-bit colour codes: 0F 0F 0F 0F A2 A2 0F. Write the RLE.

AskedRLEKnowruns of identical codesRoutecount before each code

  1. 40F 2A2 10F

How each kind of file is compressed

File typeLossless methodsLossy methods
TextRLE: repeated sequences of the same character replaced by one copy + a count; (dictionary methods: repeated words / patterns replaced by short codes)not used: no data can be lost
Bitmap imageRLE: runs of consecutive identical pixels stored as colour + countreduce the colour (bit) depth → fewer bits per pixel; reduce the colour palette → fewer colours, fewer bits each; reduce the image resolution → fewer pixels; remove detail / changes of shade the human eye can't see (JPEG)
Vector graphicgeneral lossless compression of the drawing list (repeated commands / values stored once); remove redundant data (hidden objects, duplicate properties)simplify shapes: fewer points on curves, store coordinates with less precision
SoundRLE of repeated sample values (e.g. silence); record the changes between samples instead of the actual values; reduce the amplitude range stored to only the range used → fewer bits per samplereduce the sampling rate → fewer samples per second; reduce the sampling resolution → fewer bits per sample; remove sounds outside the range of human hearing (and quiet sounds masked by louder ones: perceptual coding, MP3)
For each lossy method, say what you reduce and why that saves bits. The November 2021 report: “use JPEG” or “remove colours the eye cannot see” is not a description. “Reduce the colour depth … so each pixel is stored with fewer bits.”
Answer about the file in the question. Reports in June 2021, June 2022, June 2023 and November 2021: candidates asked about an image described text compression (repeated characters), or the other way round. Image → pixels / colours; text → characters; sound → samples.

Vector graphics compress much less than bitmaps: the drawing list has little redundant data to remove (November 2021 scheme).

Choosing lossy or lossless: case by case

1 Live video call

A video conference uses real-time bit streaming. Lossy or lossless? Justify.

Askedchoice + reasonsKnowreal time; limited bandwidthRoutesmallest file wins

  1. Lossy: reduces the file size more than lossless
  2. so significantly less bandwidth is needed and buffering is reduced
  3. data that can't be seen is removed (e.g. lower video resolution / audio sample rate), so quality loss is not noticed

2 A program's source code emailed

A program file is compressed before being emailed. Lossy or lossless?

Askedchoice + reasonKnowcode must runRouteno data loss allowed

  1. Lossless: every character is needed; losing any would corrupt the file / stop the program working

3 Photos on a website

Holiday photos are uploaded to a web server for customers to view. Lossy or lossless?

Askedchoice + reasonsKnowviewing only; many downloadsRoutemuch smaller files

  1. Lossy: much smaller files → faster for customers to download, less storage on the server, less bandwidth
  2. the detail removed is not noticed by the human eye

4 A medical scan

X-ray images are stored for doctors to examine. Lossy or lossless?

Askedchoice + reasonKnowevery detail may matterRouteaccuracy beats size

  1. Lossless: the original must be re-created exactly; a removed detail could change a diagnosis

5 Reducing the sampling rate

A sound file is compressed by reducing its sampling rate. Is this lossy or lossless?

Askedtype + reasonKnowsamples are discardedRoutecan the original come back?

  1. Lossy: there are fewer samples per second, so data is permanently lost / the original sound cannot be re-created

Mark-scheme focus

Lossless compression of a text file; why lossy must not be used (4 marks)

9618/13/M/J/22 Q1Paper 1 · Theory fundamentalsParts (a)(iii) and (iv)4 marksFull size
(iii) Describe how lossless compression can be used to reduce the file size of a text file. [2] (iv) Explain why lossy compression should not be used on a text file. [2]
Credited
  • (iii) Run-length encoding can be used B1; identifies groups of repeated characters … replaces them with one copy of the character and the number of times it occurs B1
  • (iv) None of the original data can be lost / deleted B1; the text file would be corrupted // cannot be opened B1
Not credited
  • (iii) describing image compression (pixels) for a text file
  • (iv) one point only, written twice in different words
Why

The June 2022 report: most said the file would be corrupted but missed the second mark. Two marks = two distinct points: no data may be lost, AND what happens if it is.

From: 9618/13/M/J/22 Q1(a)(iii), (iv)

Why RLE may not reduce the size of a bitmap, with an example (3 marks)

9618/11/M/J/23 Q1Paper 1 · Theory fundamentalsPart (c)(ii)3 marksFull size
(c)(ii) An image can be compressed using run-length encoding (RLE). Explain the reasons why RLE may not reduce the file size of a bitmap image. Give one example in your answer. [3]
Credited
  • RLE stores a colour and the number of times it occurs consecutively B1
  • An image may not have many sequences of the same colour B1
  • It would need to store each colour and then the count 1, which adds data B1 (max 2 for the explanation)
  • Example: Red-Green-Blue would become Red 1 Green 1 Blue 1 B1
Not credited
  • An example with characters (“ABC → 1A1B1C”) when the question is about a bitmap
  • “RLE doesn't work on photos” with no reason
Why

The example must be about colours / pixels: the June 2023 report noted examples using characters instead of colours.

From: 9618/11/M/J/23 Q1(c)(ii)

Why lossy compression is often suitable for a bitmap image (2 marks)

9618/13/O/N/24 Q6Paper 1 · Theory fundamentalsPart (b)(iii)2 marksFull size
(b) The colour of each pixel in a bitmapped image is represented by 8 bits. (iii) A bitmap image can be compressed using lossy compression. Explain the reasons why lossy compression is often suitable for a bitmap image. [2]
Credited
  • The change may not be noticeable // data removed is usually not noticed by the human eye B1
  • … for example, changes in shade / detail B1
  • It produces a larger decrease in file size compared to lossless B1 (max 2)
Not credited
  • “It makes the file smaller” (true of all compression)
  • “The quality stays the same” (it doesn't - it just isn't noticed)
Why

The reason lossy suits images is human perception plus a bigger reduction; “smaller” alone doesn't separate lossy from lossless.

From: 9618/13/O/N/24 Q6(b)(iii)

Why compress a sound file for email; how lossless compression works on sound (4 marks)

9618/11/O/N/21 Q7Paper 1 · Theory fundamentalsPart (b)4 marksFull size
(b) Bobby wants to email the sound file to his school email address. He compresses the file before sending the email. (i) Explain the reasons why Bobby compresses the sound file. [2] (ii) Bobby uses lossless compression. Describe how lossless compression can compress the sound file. [2]
Credited
  • (i) Reduces the file size B1; faster to transmit / download // original file too large for email storage / attachment B1
  • (ii) e.g. RLE: where consecutive sounds are the same, record the value and the number of times it repeats B1 B1 // reduce the amplitude range to only the range used, so fewer bits per sample // record the changes instead of the actual sounds
Not credited
  • (i) “Easier to transmit” (the report wanted speed)
  • (ii) generic lossless descriptions with text or image examples
Why

(ii) must be about sound: the report said answers were generic and seldom referred to the sound file.

From: 9618/11/O/N/21 Q7(b)

  • Compression appears in most papers, usually attached to an image, sound or text question: 2-7 marks.
  • Most asked: benefits of compressing in a context (2-3), lossy vs lossless choice + justification (1-3), describe RLE for text or an image (2-3).
  • Lost marks: generic answers that ignore the file type or the context; one-word reasons (“smaller”, “easier”).

Worked examples

Example 1 · RLE on one row of pixels (Paper 1, 2 marks) ★ Easy
9618/12/M/J/22 Q1Paper 1 · Theory fundamentalsPart (d)2 marksFull size
(d) The first row of pixels in the image from part (b) is shown: B B B B B B B B B B (ten blue pixels). Explain how this row of pixels can be compressed using lossless compression. [2]
1 Read it with me what is actually happening?

Ten identical pixels in a row: the perfect case for RLE. The second mark is for applying it to this row.

  • this row of pixelsuse the row: 10 × B
  • losslessRLE
2 What is it asking for? the target, part by part
  • (d)name + apply2
3 What do we know? everything the question gives us
  • BBBBBBBBBB
4 Plan the route work backwards from what we want
FindKey ideaAlready have itWork out firstTrap
  • Compress
    • MethodRLE
    • Applycolour B, count 10
What would a generic answer miss?

The report: answers that didn't explain how the method applies to the row given lost the second mark.

5 Now solve it have a go on paper first, then open the worked solution
  1. Use run-length encoding B1
  2. Record the colour Blue and the number of times it occurs, 10 B1
6 Check it does the answer make sense?

10 colour codes become one colour + one count ✓.

Example 2 · Completing an RLE table, then text and lossless (Paper 1, 4 marks) ★★ Medium
9618/12/O/N/22 Q8Paper 1 · Theory fundamentalsPart (c)4 marksFull size
(c) A second bitmap image is stored using a colour depth of 8 bits per pixel. It is compressed using RLE. (i) Complete the table of uncompressed and RLE compressed values (hex colour codes): EA F1 F1 F2 F2 F2 EA ↔ 1EA 2F1 3F2 1EA (given); … ↔ 2AB 2FF 11D 167; 32 32 80 81 81 ↔ … [2] (ii) RLE is an example of lossless compression. Explain why lossless compression is more appropriate than lossy compression for a text file. [2]
1 Read it with me what is actually happening?

The first row shows the format: count, then the 2-digit hex colour. Row 2 is decoding; row 3 is encoding.

  • first row completedcopy its format exactly
  • each colour = hex valuecolour codes are 2 hex digits; anything before them is the count
2 What is it asking for? the target, part by part
  • (c)(i)two cells2
  • (c)(ii)why lossless for text2
3 What do we know? everything the question gives us
  • 2AB 2FF 11D 167decode
  • 32 32 80 81 81encode
4 Plan the route work backwards from what we want
FindKey ideaAlready have itWork out firstTrap
  • Row 2decode
    • Split2|AB, 2|FF, 1|1D, 1|67
    • Trapwriting FF once
  • Row 3encode
    • Runstwo 32s, one 80, two 81s
    • Trap80 instead of 180
How is “11D” split?

Count 1, colour 1D: the colour is always the last two hex digits.

5 Now solve it have a go on paper first, then open the worked solution
  1. (i) Row 2: AB AB FF FF 1D 67 B1
  2. Row 3: 232 180 281 B1
  3. (ii) All the data is required // no data can be lost B1 … otherwise the text file will be corrupted / not make sense B1
6 Check it does the answer make sense?

Re-encode row 2 to check: AB AB → 2AB, FF FF → 2FF, 1D → 11D, 67 → 167 ✓. The report saw “only one FF” and “80 instead of 180”.

Example 3 · Live streaming a concert: choose and justify (Paper 1, 3 marks) ★★ Medium
9618/12/O/N/23 Q6Paper 1 · Theory fundamentalsPart (a)3 marksFull size
(a) A real-time video of a music concert needs to be streamed to subscribers. Tick one box to identify the most appropriate type of compression (lossy or lossless) and justify your answer. [3]
1 Read it with me what is actually happening?

Three marks, all for the justification. Real-time streaming = the data must keep arriving as fast as it is watched.

  • real-timeno time to wait: buffering ruins it
  • subscribersmany viewers, many devices
  • justifyreasons linked to streaming
2 What is it asking for? the target, part by part
  • (a)choice + justification3
3 What do we know? everything the question gives us
    4 Plan the route work backwards from what we want
    FindKey ideaAlready have itWork out firstTrap
    • Lossy?
      • Sizemuch smaller → less bandwidth
      • Viewerloss not noticed; less buffering
      • Devicesphones don't need full resolution
    Could lossless be justified?

    Yes, the scheme also credits it (e.g. viewers don't want any loss; high-bandwidth devices). Lossy is the stronger case.

    5 Now solve it have a go on paper first, then open the worked solution
    1. Lossy (ticked)
    2. Loss of quality will not be noticed B1
    3. Needs to be viewed in real time, so less bandwidth needed if the file size is smaller B1
    4. Smaller file sizes reduce buffering so the video plays more smoothly // viewers on different devices may not need high resolution B1
    6 Check it does the answer make sense?

    Every point links the choice to real-time streaming ✓.

    Example 4 · Two lossy methods for a photograph (Paper 1, 4 marks) ★★★ Hard
    9618/12/O/N/21 Q5Paper 1 · Theory fundamentalsPart (b)(ii)4 marks

    Riya takes a photograph using a digital camera. The photograph is stored as a bitmap image.

    1. (b)(ii)
      Riya needs to email the photograph. She compresses the photograph before sending it using an email.
      Describe two lossy methods that Riya can use to compress the image.
      Method 1 ……   Method 2 ……
      [4]

    Why it is hard: the November 2021 report: answers “frequently described lossy compression and the effects of the compression on the image, not methods of lossy compression”.

    1 Read it with me what is actually happening?

    “Describe two methods”: 2 marks each = the method + how it reduces the data. Effects on the picture don't score.

    • lossy methodsways of removing data from a bitmap
    • describemethod + why fewer bits
    2 What is it asking for? the target, part by part
    • (b)(ii)two methods, each explained4
    3 What do we know? everything the question gives us
      4 Plan the route work backwards from what we want
      FindKey ideaAlready have itWork out firstTrap
      • Methods
        • Bit depthfewer bits per pixel
        • Palettefewer colours, fewer bits each
        • Resolutionfewer pixels
        • Trap“use JPEG”
      Is “remove colours the eye can't see” a method?

      Not as a description: the report wanted how it compresses the image.

      5 Now solve it have a go on paper first, then open the worked solution
      1. Method 1: reduce the bit (colour) depth B1 … reduces the number of bits per pixel, so each pixel needs fewer bits B1
      2. Method 2: reduce the image resolution B1 … fewer pixels, so less binary to store B1
      3. (also credited: reduce the colour palette … fewer colours mean fewer bits are needed to store each colour)
      6 Check it does the answer make sense?

      Both methods say what is reduced and why the data shrinks ✓.

      Summary

      • Compression reduces file size → less storage, faster transfer, less bandwidth / buffering: always tie it to the context.
      • Lossless: no data lost, original re-created exactly (text, code). Lossy: data permanently removed, much smaller (photos, audio, video, streaming).
      • RLE: runs of consecutive identical values → value + count. Great for long runs, makes data without runs bigger.
      • Image lossy: lower depth / palette / resolution. Sound lossy: lower rate / resolution, remove inaudible sounds.
      If you see…do this
      “explain why … compressed” in a contextsmaller → faster / less storage / bandwidth, for the people in the question
      “lossy or lossless?” + justifyno mark for the choice: give reasons that fit the situation
      “describe RLE” for an imageconsecutive identical pixels → colour + count
      “describe RLE” for textrepeated characters → one copy + count
      an RLE tablecopy the given row's format; count then value
      “describe a lossy method”what is reduced + why it means fewer bits

      Can I do this?

      Anything unticked: revise that section first. All ticked: take the quiz.

      Quiz

      The multiple-choice and guided questions need JavaScript. The concept checks and exam practice below work without it.

      Concept checks

      Answer each one in your head first, then open it.

      1Why can lossless compression make a file smaller without losing anything?Show answer

      Most files contain redundancy - repeated patterns. Lossless methods store the repeats more cleverly (e.g. once with a count) and can rebuild them exactly.

      2Why does compressing an already-compressed JPEG with ZIP barely help?Show answer

      The JPEG has already removed most of the redundancy; there is little repetition left for a lossless method to exploit.

      3Is reducing the sampling rate compression, or just a different recording?Show answer

      Both: it is a lossy way of reducing the size of an existing recording, because samples are thrown away and can't be recovered.

      4Why do vector graphics compress so little?Show answer

      Their drawing lists are already compact, with little redundant data to remove.

      Exam-style practice

      New questions, not used above. Do them on paper, then mark your work against the official mark scheme.

      Q19618/11/M/J/21 Q1Paper 1 · Theory fundamentalsPart (b)3 marksFull size
      (b) The image is compressed using lossless compression. Identify one method of lossless compression that can be used to compress the image and describe how the method will reduce the file size. [3]
      Show the mark scheme
      PartAnswer / workingMarksGuidance
      (b)Run-length encodingB1
      Replace sequences of the same colour pixel …B1must be consecutive / adjacent pixels
      … with colour code and number of identical pixelsB1
      Total: 3 marks

      Examiner tip The June 2021 report: descriptions did not always make it clear that the identical pixels were adjacent, and some described text instead of the image.

      Q29618/11/O/N/24 Q4Paper 1 · Theory fundamentalsPart (b)6 marksFull size
      (b) The student compresses the file (a program saved as a text file) before it is emailed to their teacher as an attachment. (i) Explain the benefits to the teacher of the attachment being a compressed file. [3] (ii) Describe one lossless method of compressing a text file. [3]
      Show the mark scheme
      PartAnswer / workingMarksGuidance
      (b)(i)Less of the teacher's storage space is used … so more student work can be storedB1
      Transmission time is reduced … so the teacher does not have to wait as long for it to arriveB1
      Bandwidth usage is reduced … so other transmissions are not adversely affected // less data allowance usedB1max 3, benefits to the teacher
      (b)(ii)Run-Length Encoding // RLEB1
      Repeated sequences of the same characters are replaced by …B1
      … a single copy of the character and a count of the number of charactersB1
      Total: 6 marks
      Q39618/11/M/J/25 Q2Paper 1 · Theory fundamentalsPart (b)7 marksFull size
      Programmers take part in live video conferences using real-time bit streaming; the video is compressed before it is transmitted. (b)(i) Explain the reasons why a video is compressed before it is transmitted using real-time bit streaming. [4] (ii) Identify whether the lossy or lossless compression method is more appropriate for real-time bit streaming. Justify your answer. [3]
      Show the mark scheme
      PartAnswer / workingMarksGuidance
      (b)(i)Video is data-intensiveB1
      The file size needs reducing in order to …B1
      … reduce the amount of bandwidth used … and reduce bufferingB1
      This means people are not behind in the conversation // people with lower bandwidth can still take partB1max 4
      (b)(ii)No mark for the choice; lossy is most appropriate
      Reduces file size more than lossless … so significantly less bandwidth / data is neededB1
      … so buffering is reduced even more than with losslessB1
      Data can be removed which cannot be seen, reducing quality without impacting the experience (e.g. lower video resolution / audio sample rate)B1max 3
      Total: 7 marks
      Q49618/12/O/N/25 Q6Paper 1 · Theory fundamentalsPart (a)1 markFull size
      (a) A sound file is compressed by reducing the sampling rate. State whether this is lossless or lossy compression. Justify your choice. [1]
      Show the mark scheme
      PartAnswer / workingMarksGuidance
      (a)Lossy: there will be fewer samples per second, so data will be permanently lost // the original sound cannot be re-createdB1type and justification both needed
      Total: 1 mark
      Q59618/11/O/N/23 Q1Paper 1 · Theory fundamentalsPart (c)2 marksFull size
      (c) Explain why a bitmap image is often compressed before it is attached to an email. [2]
      Show the mark scheme
      PartAnswer / workingMarksGuidance
      (c)Reduced bandwidth usage when transmitting the message // reduced transmission time to the email serverB1
      Reduced storage space on the email server // email accounts often have a maximum size for an attachmentB1max 2
      Total: 2 marks

      Your progress on this note is saved when JavaScript is on.