Aqa 8525 TG Rle
Aqa 8525 TG Rle
Aqa 8525 TG Rle
This teaching guide is designed to help you teach Run-length encoding from our GCSE
Computer Science specification (8525). It is not prescriptive; it simply gives you some
teaching ideas that you can adapt to the needs of your students.
Example as ASCII:
97 97 97 97 98 98 98 98 98 98 99 100 100 100 100 100
© AQA 2020 1 of 6
RLE can be used to store that same data using fewer bytes. There are four consecutive
occurrences of the character 'a' followed by six consecutive occurrences of 'b', one 'c' and
finally five consecutive occurrences of 'd'. This could be written as the following sequence of
count-character pairs:
Run-length encoding for the above example:
(4, a)(6, b)(1, c)(5, d)
So, if the text aaaabbbbbbcddddd is compressed using RLE, storing each count in binary
in a single byte, we would end up with:
04 97 06 98 01 99 05 100
As we can see, this compressed version only requires 8 bytes – a reduction from the original
16 bytes.
Text that contains characters with a high frequency of repetition can be compressed efficiently.
Text with few repetitions, ie more random text, will not compress well and can even result in
the compressed version using more storage space than the uncompressed version.
A possible solution to this is to use a special byte value that ‘flags’ when a run is going to
occur. This does mean though that any run of bytes automatically has an additional byte
added to it. When there is no flag, the next byte(s) are taken as their face value and with a run
of 1.
© AQA 2020 2 of 6
Example
Uncompressed text
aaaaaaaaaabbbbbbececececececdddddddddddddddecb (46 bytes)
RLE count-character pairs
(10, a)(6, b)(1, e)(1, c)(1, e)(1, c)(1, e)(1, c)(1, e)
(1, c)(1, e)(1, c)(1, e)(1, c)(15, d)(1, e)(1, c)(1, b)
No Flag RLE
10 97 06 98 01 101 01 99 01 101 01 99 01 101 01 99 01 101 01
99 01 101 01 99 01 101 01 99 15 100 01 101 01 99 01 98(36 bytes)
Flag (value 255) RLE
255 10 97 255 06 98 101 99 101 99 101 99 101 99 101 99 101 99
255 15 100 101 99 98 (24 bytes)
An algorithm to decompress an RLE sequence stored in the array RLE would look like this:
index ← 0
WHILE index < LEN(RLE)
IF RLE[index] = 255 THEN
count ← RLE[index + 1]
character ← CODE_TO_CHAR(RLE[index + 2])
FOR i ← 1 TO count
OUTPUT character
ENDFOR
index ← index + 3
ELSE
OUTPUT CODE_TO_CHAR(RLE[index])
index ← index + 1
ENDIF
ENDWHILE
© AQA 2020 3 of 6
The example below shows a section of 32 pixels from a monochrome bitmap:
© AQA 2020 4 of 6
Pixel level RLE (16.7 million colour images)
Each pixel is represented by three bytes, for an RGB bitmap. Each pair would consist of a run
length byte, followed by the three bytes that represent the pixel colour.
The metadata for a bitmap will include the number of rows and number of columns (in this
case 8x8), so it is safe for the RLE to 'run over' a row, ie reading from the top-left pixel, the
image can be encoded as two blue pixels, four black pixels, three blue pixels, and so on.
This image uses three bytes per pixel and so the total UNCOMPRESSED file size (ignoring
metadata) is 8 x 8 x 3 = 192 bytes.
Each cell in the table requires one byte hence the total bytes required for the RLE of the bitmap
is 11 x 12 = 132 bytes. This results in a file size that is about two-thirds (actually 68.75%) of
the original size.
© AQA 2020 5 of 6
Best and worst cases for RLE
Best case The longer the run lengths the better the compression,
therefore if we only had one colour we could specify
the entire 64 pixel image with just 4 bytes.
Count R G B
64 0 0 255
Total bytes required = 4 bytes
Worst case The shorter the run lengths the less compression we
achieve. The worst case is that the colour changes
every pixel.
Each pixel takes exactly 4 bytes EVERY time,
because there are no runs (or alternatively you can
think of all the runs as being of length 1).
Total bytes required = 8 x 8 x 4 = 256 bytes
File sizes
The actual file size of compressed data will often be larger than indicated by the examples shown
in this document as there will also be metadata stored in the file (along with the character/pixel
data).