private void RLEDecompress()
{
if (RLECompressed) //decompressing image
{
MemoryStream oldImgData = new MemoryStream(imgData);
MemoryStream newImgData = new MemoryStream((int)(imgSize.width * imgSize.height * BPP));
byte[] buffer;
while (newImgData.Position < (imgSize.width * imgSize.height * BPP))
{
int count = oldImgData.ReadByte();
bool isCompressed = (count & 0x80) == 0x80; //if value > 128
if (isCompressed)
{
count -= 0x7F; //value - 127 = num of repetitions
buffer = new byte[(int)BPP];
oldImgData.Read(buffer, 0, buffer.Length);
for (int j = 0; j < count; j++) // write pixel for each count
{
newImgData.Write(buffer, 0, buffer.Length);
}
}
else // if it's not compressed copy the values
{
buffer = new byte[(int)BPP * (count + 1)];
oldImgData.Read(buffer, 0, buffer.Length);
newImgData.Write(buffer, 0, buffer.Length);
}
}
if (newImgData.Position != newImgData.Length)
throw new BadImageFormatException();
RLECompressed = false; //remove RLE flag
imgData = newImgData.ToArray();
oldImgData.Close();
newImgData.Close();
}
}