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.
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.
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.
Lossless
Lossy
Data
No data is lost; the original is restored exactly
Data is permanently removed; the original cannot be restored
Size reduction
Smaller reduction
Much larger reduction
Examples
RLE, ZIP, PNG, FLAC
JPEG, MP3, most streamed video
Use for
Text, program code, spreadsheets, databases, medical or legal images
Photos, music and video for the web, streaming and social media
Why compress: less storage space on devices and servers; less bandwidth; faster upload, download and streaming; more files fit within email or upload limits.
Justifying a method: ask whether any data can be lost. If every bit matters (a program, a bank record, an X-ray used for diagnosis), choose lossless. If the file is for people to see or hear and size matters more than perfect quality, lossy is acceptable.
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.
Encode: scan the data; for each run of identical values store the count and the value.
Decode: write out each value the stated number of times. The result is identical to the original, so RLE is lossless.
Best for: data with long runs, such as simple images with large areas of one colour, or text with repeated characters.
Worst case: if values rarely repeat, every value becomes a pair and the file gets bigger.
Worked example Text
Use RLE to compress AAAAABBBCCCCCCCCD. Each character and each count is stored in 1 byte. How many bytes are saved?
A 1-bit image row is stored as the (count, colour) pairs 4 1, 3 0, 2 1. Write out the row.
4 1 means four pixels of colour 1: 1111.
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.
8G 4H 12K: 24 bytes become 6 bytes, a saving of 18 bytes
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.
File
How it can be compressed
Type
Text file
RLE for repeated characters; replacing repeated words or phrases with short codes held in a dictionary stored with the file
Lossless only: every character must be recovered
Bitmap image
RLE 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 graphic
Remove redundant data in the drawing list and apply a lossless method such as RLE or ZIP to the object properties
Lossless: the objects must be redrawn exactly
Sound file
Perceptual 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 resolution
Lossy (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.