Run Length Encoding (RLE)
Lesson 1 activities
Activity 4.1.1
This series of binary numbers represents a compressed image.
Line 1 00 0001 01 0011 00 1100
Line 2 00 0001 01 0011 00 1100
Line 3 00 0001 01 0011 00 0100 10 0010 00 0100 10 0001 00 0001
Line 4 00 0111 10 0010 00 0100 10 0010 00 0001
Line 5 01 0010 00 0101 11 0011 00 0011 10 0010 00 0001
Line 6 01 0010 00 0011 11 0110 00 0001 10 0011 00 0001
Line 7 00 0100 11 0010 01 0001 11 0101 10 0011 00 0001
Line 8 00 0001 01 0001 00 0001 11 1001 10 0011 00 0001
Line 9 00 0011 11 1001 10 0011 00 0001
Line 10 00 0001 01 0001 00 0010 11 1000 10 0011 00 0001
Line 11 00 0101 11 0110 00 0001 10 0011 00 0001
Line 12 00 0111 11 0011 00 0011 10 0010 00 0001
Line 13 00 0001 11 0001 00 0010 11 0001 00 0010 10 0010 00 0100 10 0010 00 0001
Line 14 00 0001 11 0001 00 0001 11 0001 00 0100 10 0010 00 0100 10 0001 00 0001
Line 15 00 0010 11 0001 00 1101
Line 16 10 10000
The first pair of binary numbers represents a colour code as follows:
00 = blue 01 = white 10 = yellow 11 = green
The second set of binary numbers represents the run length of the colour. So, for
example, 00, 1101 represents 13 blue pixels.
Assuming each of the squares in the grid below represents one pixel, shade in the
squares. The first line of pixels has been done for you.
Line 1: 00 0001 01 0011 00 1100
Activity 4.1.2
Use this image to complete these calculations.
How many bytes are required to
represent the uncompressed file?
Hint: number of squares in grid x 2 / 8
How many bytes are required to
represent the RLE encoded file?
Hint: number of codes x number of
bits in each code / 8
How much storage space have you
saved?
Extension
Imagine that an image contains:
1,024 pixels (a grid of 32 pixels by 32 pixels)
255 different colours (represented by 8 bits)
The image has been encoded using RLE, producing 116 binary codes comprising 8
bits (to represent the 255 colours) and 5 bits (to represent the colour run length).
Using this information, calculate the following.
How many bytes are required to
represent the uncompressed file?
How many bytes are required to
represent the RLE encoded file?
How much storage space have you
saved?
Activity 4.1.3
Encode these text strings using RLE.
Text string Answer
AAAABBBBBBBBBCADDDDEEFFFFFFFF
ABCABCABCABCABCABCABCABCABCS
BBGGYYAACCFFEEBBGGYYAACCFFEE
Which one compresses the most?
Why is this?
Describe in English the process the RLE
calculator follows to encode a piece of
text.
Activity 4.1.4 (homework)
1. Explain how the RLE compression algorithm works.
2. Here is some data used to represent an image. Each pixel is encoded as a
character.
AADACCEFABAAECFGBDGE
Explain why a RLE algorithm may not be appropriate for encoding this image.
3. Here is a partly completed RLE algorithm expressed as a written description.
Fill in the gaps to complete it.
1. Start with the first character in the string.
2. Write down the number 1.
3. Compare the first character with the next character on the right.
4. If they are the same, ___________________________________.
5. If they are not the same, ________________________________.
6. Move on to the next character on the right.
7. Go back to step 2 and repeat until you reach the end of the string.
8. _____________________________________________________.
Lesson 2 activities
Activity 4.2.1
Implement the RLE compression algorithm in Python.
Test out your program using these data strings:
o AAAABBBBBBBBBCADDDDEEFFFFFFFF
o ABCABCABCABCABCABCABCABCABCS
o BBGGYYAACCFFEEBBGGYYAACCFFEE