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 bool ReadFileHeader(BinaryReader aReader, GIFImage aImage)
        {
            aImage.header.magic = (uint)(aReader.ReadByte() | (aReader.ReadByte() << 8) | (aReader.ReadByte() << 16));
            var ver = aImage.header.version = (uint)(aReader.ReadByte() | (aReader.ReadByte() << 8) | (aReader.ReadByte() << 16));

            return(aImage.header.magic == GIFHeader.MAGIC && (ver == GIFHeader.VERSION_87a || ver == GIFHeader.VERSION_89a));
        }
        public GIFImage Load(BinaryReader aReader)
        {
            GIFImage img = new GIFImage();

            if (!ReadFileHeader(aReader, img))
            {
                Debug.LogWarning("Gif header magic wrong or unknown version: '" + img.header.Magic + "' / '" + img.header.Version + "'");
                return(null);
            }
            if (!ReadScreenDescriptor(aReader, img))
            {
                Debug.LogWarning("Error reading GIF screen descriptor or global color table");
                return(null);
            }
            Log("Header read completed");
            while (true)
            {
                var block = ReadBlock(aReader, img);
                if (block == null)
                {
                    break;
                }
                block.Parent = img;
                img.data.Add(block);
            }
            return(img);
        }
        private IGIFBlock ReadBlock(BinaryReader aReader, GIFImage aImage)
        {
            byte blockType = aReader.ReadByte();

            Log("New block: " + ((EBlockType)blockType));
            switch ((EBlockType)blockType)
            {
            case EBlockType.Extension: return(ReadExtension(aReader));

            case EBlockType.ImageDescriptor: return(ReadImage(aReader, aImage));

            case EBlockType.Trailer: return(null);

            default: throw new System.NotSupportedException("Encountered unknown GIF block type: 0x" + blockType.ToString("xx"));
            }
        }
 private bool ReadScreenDescriptor(BinaryReader aReader, GIFImage img)
 {
     img.screen.width            = aReader.ReadUInt16();
     img.screen.height           = aReader.ReadUInt16();
     img.screen.flags            = (EScreenDescriptorFlags)aReader.ReadByte();
     img.screen.bgColorIndex     = aReader.ReadByte();
     img.screen.pixelAspectRatio = aReader.ReadByte();
     if (img.screen.HasGlobalColorTable)
     {
         img.screen.globalColorTable = ReadColorTable(aReader, img.screen.SizeOfGlobalColorTable);
     }
     else
     {
         img.screen.globalColorTable = null;
     }
     return(img.screen.HasGlobalColorTable ^ img.screen.globalColorTable == null);
 }