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:
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.
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
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?
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.
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.
VERY SIMPLE AND EASY TO UNDERSTAND .iF POSSIBLE TRY TO EXPLAIN THE ALGORITHM
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.
i find the code RLE compression for text in VB6.
but i don’t find the code for BITMAP…
help me! please!
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’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.
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.
tnx a lot to the contributor….
but i really need the sourcecode in VB6 language…
i hope somebody will upload here….
Anyone who has the source code in Java language? Thanks!
hi
can u send RLE algorithm matlab code please
Thanks a lot , article helped me a lot…..
Can you please send me the algorithm of RLE and it…