示例#1
0
        public void Decode()
        {
            UsedIndexes = new List <int>();

            _currentLine   = 0;
            _currentColumn = 0;

            var bitStreamManager = new BitStreamManager(_imageData.Data);

            _resultBitmap = new Bitmap(_width, _height);

            //if (_pictureData.ImageData.Length == 0
            //    || (_pictureData.ImageData.Length == 1 && _pictureData.ImageData[0] == 0)) return; //Algumas imagens são vazias!!

            /*
             * Each line start with a 16le storing the size of the encoded line (without the size header itself) followed by the RLE data.
             * lines
             *  encoded size : 16le
             *  line data    : size bytes
             */
            bool finishDecode = false;

            while (!finishDecode)
            {
                uint lineSize       = bitStreamManager.ReadUInt16();
                int  streamPosition = bitStreamManager.Position;

                while ((bitStreamManager.Position - streamPosition) < (lineSize * 8))
                {
                    bool repeatSameColor = bitStreamManager.ReadBit();
                    int  count           = bitStreamManager.ReadValue(7) + 1;

                    if (count > _width)
                    {
                        count = _width;
                    }

                    if (repeatSameColor)
                    {
                        byte colorIndex = bitStreamManager.ReadByte();
                        var  color      = GetColor(colorIndex);
                        for (int j = 0; j < count; j++)
                        {
                            DrawNextPixel(color);
                        }
                    }
                    else
                    {
                        for (int j = 0; j < count; j++)
                        {
                            byte colorIndex = bitStreamManager.ReadByte();
                            var  color      = GetColor(colorIndex);
                            DrawNextPixel(color);
                        }
                    }

                    if ((_currentColumn == 0 && _currentLine == _imageData.Height) || bitStreamManager.EndOfStream)
                    {
                        finishDecode = true;
                    }
                }
            }

            UsedIndexes.Sort();
        }