RLE stands for Run Length Encoding. It is a lossless algorithm that only offers decent compression ratios in specific types of data.
How RLE works
RLE is probably the easiest compression algorithm there is. It replaces sequences of the same data values within a file by a count number and a single value. Suppose the following string of data (17 bytes) has to be compressed:
Using RLE compression, the compressed file takes up 10 bytes and could look like this:
A *8B C D *4E F
As you can see, repetitive strings of data are replaced by a control character (*) followed by the number of repeated characters and the repetitive character itself. The control character is not fixed, it can differ from implementation to implementation.
If the control character itself appears in the file then one extra character is coded.
Als you can see, RLE encoding is only effective if there are sequences of 4 or more repeating characters because three characters are used to conduct RLE so coding two repeating characters would even lead to an increase in file size.
It is important to know that there are many different run-length encoding schemes. The above example has just been used to demonstrate the basic principle of RLE encoding. Sometimes the implementation of RLE is adapted to the type of data that is being compressed.
Advantages and disadvantages
This algorithm is very easy to implement and does not require much CPU horsepower. RLE compression is only efficient with files that contain lots of repetitive data. These can be text files if they contain lots of spaces for indenting but line-art images that contain large white or black areas are far more suitable. Computer-generated color images (e.g. architectural drawings) can also give fair compression ratios.
Where is RLE compression used?
RLE compression can be used in the following file formats:
- TIFF files
- PDF files