Computing Mike
Computing Mike
With: Mr Mike
Run-length encoding (RLE)
Run-length encoding (RLE) can be used for lossless compression of a number of
different file formats:
If we assume ASCII code is being used, then the string can be coded as follows:
This means we have five characters with ASCII code 97, four characters with
ASCII code 98, two characters with ASCII code 99 and five characters with
ASCII
code 100.
A flag preceding data indicates that what follows are the number of repeating
units.
When a flag is not used, the next byte(s) are taken with their face value and a run
of 1 (for example, 01 99 means one character with ASCII code 99 follows).
The original string contains 32 characters and would occupy 32 bytes of storage.
The coded version contains 18 values and would require 18 bytes of storage.
This has 15 values and would, therefore, require 15 bytes of storage. This is a
reduction in file size of about 53% when compared to the original string.
Using RLE with images
Example 1: Black and white image
Using RLE with images
Example 2: Coloured images
The original image (8 × 8 square) would need 3 bytes per square (to include
all three RGB values). Therefore, the uncompressed file for this image is
8 × 8 × 3 = 192 bytes.
The RLE code has 92 values, which means the compressed file will be 92bytes in
size. This gives a file reduction of about 52%. It should be noted that the file
reductions in reality will not be as large as this due to other data which needs to
be stored with the compressed file (e.g. a file header).
Pop Quiz
1. What are the five stages of the program life cycle?
» analysis
» design
» coding
» testing