Secondary Computer Science / 1.4 Storage Size and Efficient Files

1.4.3 Reducing Files Without Losing Their Purpose

🔒 Lesson slides are available to signed-in users. Sign in

1.4.3 Reducing Files Without Losing Their Purpose

A community exhibition is preparing photographs, recorded interviews, posters, program files and visitor records. Sending and storing the original files requires more capacity than the organisers have available.

Compression can reduce the number of bits required. The method chosen depends on what the file must still do afterwards. A program file must remain exact, while a photograph may still serve its purpose after a small amount of visual detail has been removed.

By the end of this section, you should be able to:

  • explain why files are compressed;
  • describe how smaller files affect storage, bandwidth and transfer time;
  • distinguish lossless and lossy compression;
  • explain why some file types require exact reconstruction;
  • describe how image and sound data can be reduced using lossy methods;
  • explain how run-length encoding stores repeated data;
  • encode and decode simple sequences using RLE;
  • identify when RLE is likely to be effective or ineffective;
  • justify a suitable compression type for a given situation.

Why reduce a file?

Large files require more storage space and contain more bits to transmit. Compression represents the file using fewer bits so that storage and communication resources can be used more efficiently.

Compression 压缩: reducing the number of bits required to store or transmit data.

Compression algorithm 压缩算法: a defined sequence of steps used to create a smaller representation of data.

Decompression 解压缩: processing compressed data so that it can be used again.
Resource or activity Effect of a smaller file
Storage capacity More files can be stored in the same available space.
Network bandwidth Less data must travel through the connection.
Upload and download The transfer may finish in less time on the same connection.
Streaming A lower data rate may be required to deliver the media.
Backup and synchronisation Less data must be copied between devices.

Develop the purpose

Do not stop at “compression makes a file smaller”. Link the smaller file to an effect, such as requiring less storage, less bandwidth or a shorter transmission time.

Compression changes the representation

Compression does not simply delete the whole file or remove its meaning. It creates a more compact representation that can still be used for its intended purpose.

1 Original data

The file contains its original image, sound, text or other data.

2 Compression

An algorithm creates a smaller representation.

3 Compressed file

Fewer bits need to be stored or transmitted.

4 Decompression

The data are reconstructed for use.

Common mistake

Decompression does not always recover the exact original. Exact reconstruction is possible after lossless compression, but not after lossy compression.

Two approaches to compression

The most important decision is whether every original detail must be reconstructed exactly.

Lossless compression 无损压缩: compression that allows the exact original data to be reconstructed.

Lossy compression 有损压缩: compression that permanently removes or simplifies some data, so the exact original cannot be reconstructed.
Feature Lossless compression Lossy compression
Is data permanently removed? No Yes
Can the exact original be recovered? Yes No
Main priority Preserve every value exactly Achieve a smaller file while retaining acceptable usefulness
Typical use Text, source code, records and exact diagrams Photographs, sound and other media where some detail may be sacrificed
Possible consequence The reduction may be limited if the data contain few useful patterns Image or sound quality may decrease

Preserve what the file needs

A suitable method depends on the purpose of the file rather than only its extension. The same broad file type may need different treatment in different situations.

File and purpose Suitable type Reason
Source code being submitted for assessment Lossless Every character and symbol must be reconstructed correctly.
Visitor attendance spreadsheet Lossless Changing a value could make the record inaccurate.
Small preview photograph on an exhibition website Lossy may be suitable A smaller file may load faster while still looking acceptable.
Original photograph kept for future editing Lossless may be preferred Future editing may require all original detail.
Spoken introduction streamed to visitors Lossy may be suitable The recording may remain understandable after some detail is removed.
Exact black-and-white symbol used for printing Lossless The original edges and pattern should remain unchanged.

Justify, do not only name

A complete answer identifies a compression type and links it to the file’s purpose. For example: “Use lossless compression because every character of the program must be recovered exactly.”

Reducing image data with lossy compression

A bitmap image stores colour information for many pixels. A lossy method can reduce the amount of image data by storing fewer pixels or fewer bits for each pixel.

Reducing image resolution

Reducing the resolution means storing fewer pixels. This lowers the file size because fewer pixel colour values are required.

Reducing colour depth

Reducing the colour depth means using fewer bits for each pixel. The number of possible colours decreases, so similar colours may be represented using the same available colour.

Change Why the file becomes smaller Possible effect
Lower resolution Fewer pixels are stored Fine detail and sharp edges may be reduced
Lower colour depth Fewer bits are stored for each pixel Colour variation may be reduced

Permanent change

Increasing the resolution or colour depth after lossy compression cannot restore the original removed information. The software can create new estimated data, but it cannot recover the exact discarded pixels or colours.

Reducing sound data with lossy compression

A digital sound file stores sample values. The amount of stored data can be reduced by taking fewer samples each second or using fewer bits for each sample.

Reducing sample rate

A lower sample rate stores fewer measurements per second. This reduces the file size but may reduce the accuracy with which rapid changes in the sound are represented.

Reducing sample resolution

A lower sample resolution uses fewer bits for each sample. This reduces the number of possible amplitude levels.

Change Why the file becomes smaller Possible effect
Lower sample rate Fewer samples are stored each second The recording may represent the sound less accurately
Lower sample resolution Each sample uses fewer bits Amplitude values are represented less precisely

Name the data that change

For sound, connect sample rate to the number of samples per second and sample resolution to the number of bits per sample.

Reducing a file without permanent data loss

Lossless compression finds a more efficient way to represent the data. The compressed version and any information needed by the algorithm are sufficient to reconstruct the exact original.

One lossless method is run-length encoding. It is useful when the data contain consecutive repetitions of the same value.

Run-length encoding 运行长度编码(RLE): a lossless method that represents a consecutive run of the same value using the value and the number of times it occurs.

Run 连续重复段: a sequence of identical values appearing next to one another.

repeated run → count + value

The examples on this page use the convention count followed by value. An actual file format must also define clearly how counts and data values are distinguished.

Worked example: encoding repeated symbols

A simple image row is represented by colour initials:

MMMMMMNNNPPPPPPPPQQ

Step 1: identify each run

Run Value Count RLE token
MMMMMM M 6 6M
NNN N 3 3N
PPPPPPPP P 8 8P
QQ Q 2 2Q

Step 2: place the tokens in order

6M 3N 8P 2Q

No original symbol has been permanently removed. The decoder can repeat each value according to its count.

Do not combine separated values

RLE describes consecutive runs. In AAABBBAAA, the two groups of A are separate runs, so the encoding is 3A 3B 3A, not 6A 3B.

Decoding run-length data

To decompress an RLE sequence, read each count–value pair and output the value the stated number of times.

Decode:

4R 2S 5T 1U

Token Instruction Output
4R Write R four times RRRR
2S Write S twice SS
5T Write T five times TTTTT
1U Write U once U

RRRRSSTTTTTU

Preserve the order

Expand the tokens from left to right. Changing their order changes the original sequence.

When RLE reduces the data

RLE is most effective when there are long consecutive runs. A simple icon, diagram or bitmap area with large blocks of one colour may contain many such runs.

Original sequence Simple RLE form Likely result
AAAAAAAAAABBBBBBBB 10A 8B Useful reduction
WWWWWWWWWWWW 12W Very useful reduction
RRRGGGGBBBBB 3R 4G 5B Likely reduction

The exact number of stored bits depends on the real format used for counts, values and control information. The simplified notation is used here to understand the principle.

When RLE may not help

RLE adds a count to every run. If values change frequently, many runs have a length of one and the encoded representation may require more data than the original.

RGBYCMK

1R 1G 1B 1Y 1C 1M 1K

The second representation contains a count for every single value. In this simplified model, RLE has expanded the data rather than reducing it.

Compression does not guarantee a smaller result

A method is effective only when the structure of the data suits the method. RLE is not a strong choice for data containing many short and changing runs.

A decision process for choosing compression

Question 1 Must every original value be recovered exactly?

If yes, use a lossless approach.

Question 2 Can reduced image or sound detail still meet the purpose?

If yes, a lossy approach may be suitable.

Question 3 Does the data contain long repeated runs?

If yes, RLE may provide useful lossless compression.

Structure a justification

A strong justification can follow this pattern:

method → what happens to the data → why this suits the file’s purpose

Common mistakes and misconceptions

  • Saying compression increases the quality of a file.
  • Saying lossless compression permanently removes unimportant data.
  • Saying lossy data can be reconstructed exactly.
  • Choosing lossy compression for source code or exact numerical records.
  • Describing lower image resolution as “fewer colours”.
  • Describing lower colour depth as “fewer pixels”.
  • Describing lower sample rate as “fewer bits per sample”.
  • Describing lower sample resolution as “fewer samples per second”.
  • Combining identical values that are not consecutive when applying RLE.
  • Assuming RLE always makes a file smaller.
  • Naming a method without explaining why it suits the purpose.

Interactive: Compression Choice and RLE Laboratory

Build an RLE representation one run at a time. The laboratory compares the original sequence with the simplified encoded form and then asks you to select a suitable compression type for different files.

Interactive investigation

Find runs and preserve the original sequence

Simple RLE result 6A 3B 6C 2D The simplified encoding uses fewer items.
Use letters, numbers or spaces. Consecutive identical values form a run.
Select “Animate runs” to watch the encoder process each run.
Original sequence 17 symbols
Encoded sequence 8 simplified items

Original values

Count–value tokens

Encoding process

  1. Start at the first value.
  2. Count consecutive copies of A.
  3. Store the token 6A.
Original symbols 17
Number of runs 4
Simplified encoded items 8
Simple comparison 9 fewer
Method explanation The sequence contains four long runs, so a count–value representation uses fewer simplified items than the original.

Compression decision

Source code submission

A student must upload a program that will be compiled and tested. Every character must remain correct.

Select the method that best preserves the file’s purpose.

Separate the two decisions

First decide whether permanent data loss is acceptable. Then decide whether the structure of the data suits a particular lossless method such as RLE.

Practice

Core questions

  1. Define the term compression.
  2. Explain two benefits of reducing a file’s size.
  3. State the difference between lossless and lossy compression.
  4. Explain why source code should normally use lossless compression.
  5. Explain why lossy compression may be suitable for a preview photograph.
  6. Describe how reducing image resolution reduces file size.
  7. Describe how reducing image colour depth reduces file size.
  8. Describe how reducing sound sample rate reduces file size.
  9. Describe how reducing sound sample resolution reduces file size.
  10. Define run-length encoding.
  11. Encode the following sequence using count followed by value: HHHHHJJKKKKKKKLLL.
  12. Encode: AAAABBAAAAA.
  13. Decode: 3P 6Q 2R 4S.
  14. Decode: 1A 5B 3C 2D.
  15. Explain why the separate groups of A in AAABBBAAA cannot be combined into one run.
  16. Explain why RLE works well for data containing long repeated sequences.
  17. Explain why RLE may increase the size of ABCDEFGH.

Choosing and justifying a method

  1. A school stores a spreadsheet containing student attendance values. Choose lossless or lossy compression and justify your choice.
  2. A low-resolution photograph is needed as a small website preview. Choose a compression type and justify your choice.
  3. A sound clip must be streamed through a slow connection, and a small reduction in quality is acceptable. Choose a compression type and justify your choice.
  4. A black-and-white diagram contains large areas of the same colour. Explain why lossless RLE may be effective.
  5. A pattern contains almost no repeated adjacent values. Explain why another compression approach may be more suitable than RLE.
  6. An original photograph will later be used for detailed editing and printing. Explain why a lossy copy may be suitable for sharing but not as the only preserved version.

Extension questions

  1. Compare the likely effect of halving an image’s width and height with only halving its colour depth.
  2. A sound file’s sample rate and sample resolution are both halved. Explain how this affects the amount of sample data stored for the same duration.
  3. Create an original sequence for which RLE gives a substantial reduction. Show the original and encoded forms.
  4. Create an original sequence for which the simplified RLE form is larger. Explain why.
  5. Explain why “lossy is always better because it makes smaller files” is an incomplete statement.
Check selected answers
  1. Lossless compression permits exact reconstruction; lossy compression permanently removes or simplifies some data.
  2. HHHHHJJKKKKKKKLLL becomes 5H 2J 7K 3L.
  3. AAAABBAAAAA becomes 4A 2B 5A.
  4. 3P 6Q 2R 4S becomes PPPQQQQQQRRSSSS.
  5. 1A 5B 3C 2D becomes ABBBBBCCCDD.
  6. In AAABBBAAA, the B values interrupt the A values. The correct encoding is 3A 3B 3A.
  7. Lower image resolution stores fewer pixels. Lower colour depth stores fewer bits for each pixel.
  8. Lower sound sample rate stores fewer samples per second. Lower sample resolution stores fewer bits for each sample.
  9. A spreadsheet should use lossless compression because the original values must be recovered exactly.
  10. A website preview photograph may use lossy compression because a smaller file can load faster and a small quality reduction may be acceptable.

Review

Key ideas

  • Compression reduces the number of bits required to store or transmit a file.
  • Smaller files use less storage space and bandwidth.
  • Smaller files can take less time to upload, download or transmit.
  • Lossless compression allows the exact original data to be reconstructed.
  • Lossy compression permanently removes or simplifies some data.
  • Lossy image compression may reduce resolution or colour depth.
  • Lossy sound compression may reduce sample rate or sample resolution.
  • RLE is a lossless method based on consecutive repeated values.
  • RLE records each run using a count and a value.
  • RLE works best when data contain long repeated runs.
  • RLE may be ineffective when values change frequently.
  • The selected method must preserve the purpose of the file.

Quick self-check

  1. Can I explain why compression is needed?
  2. Can I distinguish lossless and lossy compression?
  3. Can I describe lossy changes to image data?
  4. Can I describe lossy changes to sound data?
  5. Can I encode and decode a simple RLE sequence?
  6. Can I identify when RLE is likely to help?
  7. Can I justify a method using the file’s purpose?

One-minute exit task

A museum needs to store:

  1. an exact visitor database;
  2. a small website preview of a large photograph.

Choose a compression type for each file and justify both choices.