RLE compression

RLE stands for Run Length Encoding. It is a lossless algorithm that only offers decent compression ratios for 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:
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 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:

38 thoughts on “RLE compression

  1. Hi there,

    if you encode a picture with RLE, how is the ‘linebreak’ encoded, ie how does the ‘system’ know the width and the length of a pic? ThX!

    1. I think that information is part of the file header and not included in the compressed imagedata. You’ll have to read the specs of the file format itself.

  2. I like this article. Thanks. I’m a Chinese student who is learning data compression. Can you tell me more about RLE algorithm? 谢谢!(which means thank you in Chinese.)

    1. Ok so Imma telling you i don’t know please stop asking me 谢谢!(Which means thank you in Chinese)

  3. Using RLE what would be the outcome of ABC000AAB00000000DEFAB00000?

    Do we perform it if the number of occurance of an element is more than a specified or pre-assumed value?

  4. Can anyone tell me how can i compress and decompress the below string using RLE.Please help me or suggest me any idea to solve.

    Original string: BMZR6(��—999000666///222

    1. Sorry but I am not a programmer. A forum dedicated to software development is a better place to ask for code.

  5. i want to know the compression ratio of test iamge when we compress it by RLE,Huffman because i want to compare it with my work
    >>> best regard

  6. Hello.. can you give me a way in comparing algorithms in data compression? given some criteria?

    criteria:
    -time of compression
    -time of reconstruction
    -size of compressed file

    thanks!

  7. can u send lossless image compression techniques and how to encode and decode an image plz…………………………………………………….

  8. tnx a lot to the contributor….
    but i really need the sourcecode in VB6 language…
    i hope somebody will upload here….

  9. One last note on the algorithms above, they only work with strings consisting of alpha values (letters) only. Add another controlcharacter to both methods (and don’t forget to change the regular expression) to ensure nummeric values, and other ASCII values can also be found.

  10. I’ll help you out a little then Laurens, here is some C# code that would help answering other programmers. However, this algorithm is very simple, they should be able to discover it by themselves based upon the text you provide above. Forgive me for the bad formatting…

    NOTE: Some strings (and integers for containing sizes) have been provided as class variables and have been left out.

    public string Compress(string stringToCompress)
    {
    string result = string.Empty;
    char previousLetter = ”;

    for (int i = 0; i l == currentLetter);
    if (currentLetter != previousLetter)
    {
    if ((int)count > 1)
    result += CONTROL_CHARACTER + count.ToString() + currentLetter;
    else result += currentLetter;
    }
    previousLetter = currentLetter;
    }
    CompressedSizeInBytes = result.Length;
    return result;
    }

    And a simple decompression:

    public string DeCompress(string stringToDeCompress)
    {
    string result = string.Empty;

    string pattern = @”(\d+)([A-Za-z])”;

    Regex r = new Regex(pattern);
    Match m = r.Match(stringToDeCompress);

    int oldIndex = 0;
    while ( m.Success )
    {
    int newIndex = m.Index;
    result += stringToDeCompress.Substring(oldIndex, (newIndex – oldIndex)-1);
    oldIndex = newIndex + m.Length;

    for (int i = 0; i < Int32.Parse(m.Groups[1].Value ); i++)
    result += m.Groups[2].Value;
    m = m.NextMatch();
    }
    //Capture the last known chars (will only occur if there are characters at the end with only 1 occurence…
    if (oldIndex < stringToDeCompress.Length)
    result += stringToDeCompress.Substring(oldIndex, stringToDeCompress.Length – oldIndex);
    return result;

    }

    I hope this will help others. Note that it's not written for effiency, only for testing purposes. I haven't tested this code on images.

    Regards,

    Robert.

  11. Can you send you send me the RLE image compression and decompression code in asp.net with c# or in matlab. Its very urgent. So, please help me.

    1. As already stated earlier in the comments: I am not a programmer and this site is not geared towards software development. I cannot help anyone asking for libraries, sample code,…

    1. Regarding this and your other question about RLE: this site is geared towards prepress operators, not programmers. You’ll have to look elsewhere for compression libraries, sample code, etc.

    1. They are both lossless so quality wise there is no difference. I have no idea which algorithm is the most efficient, in compression ratio or processing requirements. It might even be that there is no clear-cut answer to this and that it is different on a file-by-file basis. I have never done any comparisons so I cannot help you with this.

  12. nice article
    it helped me alot. thanks
    pls, i dont understand how to encode the control character or will it lead to an unavoidable increase in file size?

Leave a Reply

Your email address will not be published. Required fields are marked *