private IGIFBlock ReadImage(BinaryReader aReader, GIFImage aImage)
        {
            var res = new GIFImageBlock();

            res.xPos   = aReader.ReadUInt16();
            res.yPos   = aReader.ReadUInt16();
            res.width  = aReader.ReadUInt16();
            res.height = aReader.ReadUInt16();
            res.flags  = (EImageDescriptorFlags)aReader.ReadByte();
            if (res.HasLocalColorTable)
            {
                Log("Read local color table: " + res.SizeOfLocalColorTable);
                res.usedColorTable =
                    res.colorTable = ReadColorTable(aReader, res.SizeOfLocalColorTable);
            }
            else
            {
                res.colorTable     = null;
                res.usedColorTable = aImage.screen.globalColorTable;
            }
            if (lastGrCtrl != null)
            {
                res.graphicControl = lastGrCtrl;
                lastGrCtrl         = null;
            }
            Log("Start LZW");
            ReadLZWImage(aReader, res);
            return(res);
        }
        private IGIFBlock ReadGraphicControlBlock(BinaryReader aReader)
        {
            byte blockSize = aReader.ReadByte();

            if (blockSize != 4)
            {
                throw new Exception("GIF: GraphicControl extension block size wrong: " + blockSize + " != 4");
            }
            var res = new GIFGraphicControlExt();

            res.flags = (EGraphicControlFlags)aReader.ReadByte();
            res.delay = aReader.ReadUInt16();
            res.transparentColorIndex = aReader.ReadByte();
            byte nullByte = aReader.ReadByte();

            if (nullByte != 0)
            {
                throw new Exception("GIF: GraphicControl extension block not terminated with '0x00' (found:0x" + nullByte.ToString("xx") + ")");
            }
            lastGrCtrl = res;
            return(res);
        }
        private IGIFBlock ReadPlainTextBlock(BinaryReader aReader)
        {
            byte blockSize = aReader.ReadByte();

            if (blockSize != 12)
            {
                throw new Exception("GIF: PlainText extension block size wrong: " + blockSize + " != 12");
            }
            var res = new GIFTextBlock();

            res.xPos         = aReader.ReadUInt16();
            res.yPos         = aReader.ReadUInt16();
            res.width        = aReader.ReadUInt16();
            res.height       = aReader.ReadUInt16();
            res.charWidth    = aReader.ReadByte();
            res.charHeight   = aReader.ReadByte();
            res.colorIndex   = aReader.ReadByte();
            res.bgColorIndex = aReader.ReadByte();
            using (MemoryStream ms = new MemoryStream())
                using (BinaryWriter bw = new BinaryWriter(ms))
                {
                    byte size = aReader.ReadByte();
                    while (size > 0)
                    {
                        int read = aReader.Read(buf, 0, size);
                        bw.Write(buf, 0, read);
                        size = aReader.ReadByte();
                    }
                    bw.Flush();
                    res.text = System.Text.Encoding.ASCII.GetString(ms.ToArray());
                }
            if (lastGrCtrl != null)
            {
                res.graphicControl = lastGrCtrl;
                lastGrCtrl         = null;
            }
            return(res);
        }