📑 Contents

Chapter 1.3: Compression

AS Level Computer Science (9618)

📚 Learning Objectives

1. The Need for Compression

📖 Definition

Compression is the process of reducing the size of a file so that it takes up less space on secondary storage.

Why Do We Need Compression?

⚠️ Key Point

2. Lossy vs Lossless Compression

2.1 Lossy Compression

📖 Definition

Lossy compression is a compression method where data is permanently removed to reduce file size. The process is irreversible.

Example: Lossy Compression in Photographs

2.2 Lossless Compression

📖 Definition

Lossless compression is a compression method where data is encoded to reduce file size, but no information is lost. The process is reversible.

2. Lossy vs Lossless (Continued)

Feature Lossy Compression Lossless Compression
Data Loss Some data permanently removed No data lost
Reversibility Irreversible Fully reversible
Compression Ratio High (up to 90% reduction) Lower (typically 50-80%)
Quality Reduced quality Original quality maintained
Best For Multimedia (images, audio, video) Text, documents, executables
Examples JPEG, MP3, MP4 PNG, ZIP, RLE, Huffman
💡 Exam Tip: When to Use Which Method?
🧠 Memory Trick

3. Run-Length Encoding (RLE)

📖 Definition

Run-Length Encoding (RLE) is a form of lossless data compression that condenses identical consecutive elements into a single value with a count.

How RLE Works

RLE = Count + Value
⚠️ Important Limitation

3.1 RLE with Text Data

📝 Example: Compressing Text

Original string: 'aaaaabbbbccddddd'

4. RLE with Images

4.1 Black and White Images

📝 Example: Letter F in 8×8 Grid

4.2 Multi-Colour Images

For images with multiple colours, RLE stores RGB values along with the count:

Colour Red Green Blue
Black 0 0 0
White 255 255 255
Red 255 0 0
Green 0 255 0
RLE Encoding Example:
❌ Common Mistake

5. Huffman Coding

📖 Definition

Huffman coding is a lossless data compression algorithm that assigns variable-length codes to characters based on their frequency of occurrence.

How Huffman Coding Works

Example: Text with 8 Different Letters
Feature Fixed-Length Code Huffman Code
Code Length Same for all characters Variable based on frequency
Most Frequent Char Same length as others Shortest code
Compression No compression Efficient compression

6. File Compression Formats

6.1 MP3 (MPEG-3) - Audio Compression

🌟 Perceptual Music Shaping

6.2 MP4 (MPEG-4) - Multimedia Format

7. JPEG and Image Formats

7.1 JPEG - Lossy Image Compression

⚠️ Important JPEG Facts

7.2 PNG - Lossless Image Compression

💡 Choosing the Right Image Format

8. Vector Graphics Compression

8.1 What are Vector Graphics?

8.2 SVG (Scalable Vector Graphics)

🌟 SVG Compression
File Type Compression Method Effectiveness
Text Files Lossless (Huffman, ZIP) High — patterns detected
Bitmap Images Lossy (JPEG) or Lossless (PNG) Variable — depends on content
Vector Graphics Lossless (text compression) Limited — already efficient
Sound Files Lossy (MP3) or Lossless (FLAC) High — removes inaudible data
Video Files Lossy (MP4) Very High — temporal redundancy

9. Exam-Style Questions (1-5)

1. Photographs are compressed before they are uploaded to a web server. Customers download photographs from this web server. Explain reasons why compressing photographs will benefit customers. [4 marks]

Answer:

  • Customers can download photographs in less time
  • Photographs take less bandwidth to transfer
  • Photographs take less storage space on customer's device
  • Customers can store more images on their devices
2. An image can be compressed using RLE. Explain reasons why RLE may not reduce the file size of a bitmap image. Give one example in your answer. [3 marks]

Answer:

  • RLE stores a colour and count for consecutive pixels
  • An image may not have many sequences of the same colour
  • RLE would need to store each colour with count 1, adding data
  • Example: Red-Green-Blue becomes Red-1 Green-1 Blue-1 (larger than original)
3. When storing music tracks in a computer, MP3 format is used. This reduces file size by about 90%. Explain how music quality is apparently retained. [4 marks]

Answer:

  • MP3 uses perceptual music shaping
  • Removes frequencies outside human hearing range (below 20 Hz or above 20,000 Hz)
  • Removes masked sounds (softer sounds hidden by louder ones)
  • Parts of music removed without affecting perceived quality
4. Explain the difference between lossy and lossless compression. Give an example of when each would be appropriate. [4 marks]

Answer:

  • Lossy: Data permanently removed; original cannot be recovered; high compression ratio
  • Lossless: No data lost; original can be perfectly reconstructed; lower compression ratio
  • Lossy example: Photographs (JPEG), music (MP3), video (MP4)
  • Lossless example: Text documents, spreadsheets, program files
5. A bitmap image contains the following pattern of pixels (W = White, B = Black):
WWWWWWWWWWWWBBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWW
Show how this would be encoded using Run-Length Encoding (RLE). [3 marks]

Answer:

  • RLE encoding: 12W2B12W3B20W
  • 12 Ws, 2 Bs, 12 Ws, 3 Bs, 20 Ws
  • Original: 49 characters → RLE: 14 characters

9. Exam-Style Questions (6-10)

6. Describe how Run-Length Encoding (RLE) compresses data. Explain why RLE would be suitable for compressing a screenshot of a document with mostly white background. [4 marks]

Answer:

  • RLE stores consecutive identical values as count + value pairs
  • Instead of storing each pixel individually, stores how many in a row
  • Screenshot has large areas of white pixels (background)
  • These long runs compress very efficiently with RLE
7. A text file contains the string: AAAABBBCCDAA
Calculate the size of the original string and the RLE compressed version. Show your working. [4 marks]

Answer:

  • Original: 12 characters × 1 byte = 12 bytes
  • RLE encoding: 4A3B2C1D2A
  • RLE has 4 count values + 5 character values = 9 values
  • Compressed size = 9 bytes (or 8 bytes if we use: 4,3,2,1,2 and A,B,C,D,A)
8. Explain two differences between JPEG and PNG image formats. State when each format would be the better choice. [4 marks]

Answer:

  • JPEG uses lossy compression; PNG uses lossless compression
  • JPEG cannot preserve transparency; PNG supports transparency
  • JPEG better for: Photographs, complex images with many colours
  • PNG better for: Screenshots, logos, diagrams, images with text
9. A company wants to store high-quality master copies of photographs for professional printing. Should they use lossy or lossless compression? Justify your answer. [3 marks]

Answer:

  • Should use lossless compression
  • Professional printing requires highest quality with no data loss
  • Lossy compression would permanently remove image data
  • Any quality loss would be visible in professional prints
10. Explain how Huffman coding achieves compression. Why might Huffman coding be more effective than RLE for compressing a text document? [4 marks]

Answer:

  • Huffman assigns shorter codes to more frequent characters
  • Assigns longer codes to less frequent characters
  • RLE is only effective for consecutive repeated characters
  • Text documents rarely have long runs of identical characters
  • Huffman works well even without consecutive repetitions

10. Summary & Key Takeaways

📌 Chapter Summary
🧠 Quick Reference Table
Scenario Best Compression Reason
Photographs JPEG (lossy) Similar colours grouped, small loss acceptable
Screenshots PNG (lossless) Sharp edges must be preserved
Music files MP3 (lossy) Removes inaudible frequencies
Text documents ZIP/Huffman (lossless) Any data loss corrupts file
Simple graphics RLE (lossless) Long runs of same colour
💡 Final Exam Tips