|
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace Endogine.Serialization
{
public class RleCodec
{
/// <summary>
///
/// </summary>
/// <param name="reader"></param>
/// <param name="destinationBuffer"></param>
/// <param name="bufferOffset"></param>
/// <returns>the number of uncompressed bytes written to the destination buffer</returns>
public static int DecodeChunk(Stream reader, byte[] destinationBuffer, int bufferOffset)
{
int numUncompressedWritten = 0;
int len = (int)reader.ReadByte();
if (len < 128)
{
//less than 128: uncompressed pixels
int numUncompressedToRead = len + 1;
numUncompressedWritten = numUncompressedToRead;
while (numUncompressedToRead != 0)
{
destinationBuffer[bufferOffset] = (byte)reader.ReadByte();
bufferOffset++;
numUncompressedToRead--;
}
}
else if (len > 128)
{
//more than 128: RLE-compressed pixels
int numCompressedToRead = (len ^ 0xff) + 2;
numUncompressedWritten = numCompressedToRead;
// Next -len+1 bytes in the dest are replicated from next source byte.
// (Interpret len as a negative 8-bit int.)
// len ^= 0x0FF;
// len += 2;
byte byteValue = (byte)reader.ReadByte();
while (numCompressedToRead != 0)
{
destinationBuffer[bufferOffset] = byteValue;
bufferOffset++;
numCompressedToRead--;
}
}
else if (len == 128)
{
// Do nothing
}
return numUncompressedWritten;
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.
A list of licenses authors might use can be found here
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.