RLE compression

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
1 January 2017

37 responses to “RLE compression”

  1. powturbo says:

    you or other readers might be interested to have a look at TURBRLE: Efficient and fast Run Length Encoding incl. automatic escape determination, SIMD and zero byte overhead:

  2. Hiho says:

    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!

    • Laurens says:

      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.

  3. yidi says:

    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.)

  4. shalini says:

    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?

  5. Channu says:

    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

  6. m.s.farhana says:

    plz give me the code for run length encoding using VHDL

    • Laurens says:

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

  7. hari says:

    how can i implement RLE compression algorithm in vhdl

  8. Hema says:

    Please send me the any recent 2D to 3D reconstruction algorithm coding

  9. sadegh says:

    it’s nice!!

  10. sarah says:

    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

  11. japs says:

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

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


  12. Gaurav Gupta says:

    Hi can u suggest me how can i implement dis in vhdl?

  13. fahad says:

    How can I use RLE for :

    Thank you for all

  14. sathishkumar says:

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

  15. yogita says:

    Thanks a lot , article helped me a lot…..
    Can you please send me the algorithm of RLE and it…

  16. ahmet says:

    can u send RLE algorithm matlab code please

  17. Janette says:

    Anyone who has the source code in Java language? Thanks!

  18. joe says:

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

  19. Robert says:

    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.

  20. Robert says:

    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.



  21. Sudipta says:

    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.

    • Laurens says:

      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,…

  22. rainie_vu says:

    i find the code RLE compression for text in VB6.
    but i don’t find the code for BITMAP…
    help me! please!

  23. GNANI says:

    can u send RLE algorithm C code

    • Laurens says:

      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.

  24. SUSHMA says:


  25. maya says:

    which compression algo is better LWZ or RLE??

    • Laurens says:

      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.

  26. rainie_vu says:

    I want to Use RLE compession is not in VB6?
    Can you help me!
    I need the code RLE trong VB6!!!

  27. Jivi says:

    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 *