You are on page 1of 4

RLE compression

INTRODUCTION RLE stands for Run Length Encoding. It is a lossless algorithm that only offers decent compression ratios in specific types of data. Run-length encoding (RLE) is a very simple form of data compression in which runs of data (that is, sequences in which the same data value occurs in many consecutive data elements) are stored as a single data value and count, rather than as the original run. This is most useful on data that contains many such runs: for example, simple graphic images such as icons, line drawings, and animations. It is not useful with files that don't have many runs as it could greatly increase the file size.

How RLE works?


RLE is probably the easiest compression algorithm . 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: ABBBBBBBBBCDEEEEF 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 are being compressed.

Encoding Strings
Step 1. Set the previous symbol equal to an unmatchable value. Step 2. Read the next symbol from the input stream. Step 3. If the symbol is an EOF exit. Step 4. Write out the current symbol. Step 5. If the symbol is an does not match the previous symbol, set the previous symbol to the current symbol, and go to step 2. Step 6. Read and count additional symbols until a non-matching symbol is found. This is the run length. Step 7. Write out the run length. Step 8. Write out the non-matching symbol. Step 9. Set the previous symbol to the non-matching symbol, and go to step 2. When actually implementing traditional RLE, a little attention to detail is required in Step 6. The run length is stored in a finite number of bits (I used an unsigned char). Runs longer than the amount that can be counted need to be broken up into to smaller runs. When the maximum count is reached, just write the count value and start the process of looking for a run all over again. You

also need to handle the case where a run is ended by an EOF. When a run is ended by anEOF, write out the run length and exit.

Decoding Strings
Decoding traditionally encoded strings is even easier than encoding. Not only are there less steps, but there are no caveats. To decode a traditionally encoded stream: Step 1. Set the previous symbol equal to an unmatchable value. Step 2. Read the next symbol from the input stream. Step 3. If the symbol is an EOF exit. Step 4. Write out the current symbol. Step 5. If the symbol is an does not match the previous symbol, set the previous symbol to the current symbol, and go to step 2. Step 6. Read the run length. Step 7. Write out a run of the current symbol as long as indicated by the run length. Step 8. Go to step 1.

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 colour 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

RLE also refers to a little-used image format in Windows 3.x, with the extension .rle, which is a Run Length Encoded Bitmap, used to compress the Windows 3.x startup screen.

You might also like