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.



									/*****Please include following header files*****/
// cstdint
/***********************************************/

static void Decompress(uint8_t* input, uint8_t* output, uint32_t inputSize) {
	uint8_t marker, symbol;
	uint32_t 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 (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

									uint8_t* decompressedData = new uint8_t[originalDataSize];

Decompress(compressedData, decompressedData, compressedDataSize);