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:
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:
https://github.com/powturbo/TurboRLE
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!
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.
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.)
Ok so Imma telling you i don’t know please stop asking me 谢谢!(Which means thank you in Chinese)
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?
what would be its compression ratio?
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
I think those people should try: http://www.pleasedomyhomeworkcosiamtoodumb.com
plz give me the code for run length encoding using VHDL
Sorry but I am not a programmer. A forum dedicated to software development is a better place to ask for code.
hi,
how can i implement RLE compression algorithm in vhdl
Hello,
Please send me the any recent 2D to 3D reconstruction algorithm coding
tnx
it’s nice!!
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
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!
Hi can u suggest me how can i implement dis in vhdl?
How can I use RLE for :
EEEE####WWWWWQ
Thank you for all
can u send lossless image compression techniques and how to encode and decode an image plz…………………………………………………….
Thanks a lot , article helped me a lot…..
Can you please send me the algorithm of RLE and it…
hi
can u send RLE algorithm matlab code please
plz fwd it if u get it on [email protected]
thanks
hi
plz fwd it if u get it on [email protected]
thanks
Anyone who has the source code in Java language? Thanks!
tnx a lot to the contributor….
but i really need the sourcecode in VB6 language…
i hope somebody will upload here….
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.
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.
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.
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,…
i find the code RLE compression for text in VB6.
but i don’t find the code for BITMAP…
help me! please!
can u send RLE algorithm C code
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.
VERY SIMPLE AND EASY TO UNDERSTAND .iF POSSIBLE TRY TO EXPLAIN THE ALGORITHM
which compression algo is better LWZ or RLE??
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.
I want to Use RLE compession is not in VB6?
Can you help me!
I need the code RLE trong VB6!!!
Thanhks!
I am not a programmer, nor is this site geared towards this. Sorry but I cannot help you, nor Jivi.
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?