RLE Decompress
RLE (Run-length encoding) is a very simple form of lossless 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 RLE Compress algorithm click here.
public static void Decompress(byte[] input, byte[] output, uint inputSize)
{
byte marker, symbol;
uint i, inputPosition, outputPosition, count;
if (inputSize < 1)
{
return;
}
inputPosition = 0;
marker = input[inputPosition++];
outputPosition = 0;
do
{
symbol = input[inputPosition++];
if (symbol == marker)
{
count = input[inputPosition++];
if (count <= 2)
{
for (i = 0; i <= count; ++i)
{
output[outputPosition++] = marker;
}
}
else
{
if (Convert.ToBoolean(count & 0x80))
{
count = ((count & 0x7f) << 8) + input[inputPosition++];
}
symbol = input[inputPosition++];
for (i = 0; i <= count; ++i)
{
output[outputPosition++] = symbol;
}
}
}
else
{
output[outputPosition++] = symbol;
}
} while (inputPosition < inputSize);
}
Example
byte[] decompressedData = new byte[originalDataSize];
Decompress(compressedData, decompressedData, (uint)compressedDataSize);