Ejemplo n.º 1
0
        private static ReadBuffer UnpackRLE(ReadBuffer packedData)
        {
            int escCount  = packedData[0] & 0x7f;
            var escCodes  = packedData.Slice(1, escCount);
            var rleData   = packedData.Drop(escCount + 1);
            var escLookup = new byte[0x100];

            for (int i = 0; i < escCount; i++)
            {
                escLookup[escCodes[i]] = (byte)(i + 1);
            }

            bool compressed  = (packedData[0] & 0x80) == 0;
            var  encodedData = compressed ? UnpackSequences(rleData, escCodes[1]) : rleData;

            var result = new WriteBuffer();

            while (encodedData.Length > 0)
            {
                var code = escLookup[encodedData[0]];
                switch (code)
                {
                case 0:
                    result.Append(encodedData[0]);
                    encodedData = encodedData.Drop(1);
                    break;

                case 1:
                    result.AppendRepeat(encodedData[2], encodedData[1]);
                    encodedData = encodedData.Drop(3);
                    break;

                case 3:
                    result.AppendRepeat(encodedData[3], encodedData[1] + (((int)encodedData[2]) << 8));
                    encodedData = encodedData.Drop(4);
                    break;

                default:
                    result.AppendRepeat(encodedData[1], code - 1);
                    encodedData = encodedData.Drop(2);
                    break;
                }
            }

            return(result.ToReadBuffer());
        }