public void FromBuffer(GR.Memory.ByteBuffer Data)
        {
            if (Data == null)
            {
                return;
            }
            GR.IO.MemoryReader memIn = Data.MemoryReader();

            int w  = memIn.ReadInt32();
            int h  = memIn.ReadInt32();
            var pf = (GR.Drawing.PixelFormat)memIn.ReadUInt32();

            Create(w, h, pf);

            int numEntries = memIn.ReadInt32();

            for (int i = 0; i < numEntries; ++i)
            {
                byte r = memIn.ReadUInt8();
                byte g = memIn.ReadUInt8();
                byte b = memIn.ReadUInt8();

                SetPaletteColor(i, r, g, b);
            }
        }
        public bool ReadFromBuffer(GR.Memory.ByteBuffer DataIn)
        {
            if (DataIn == null)
            {
                return(false);
            }
            SpriteLayers.Clear();
            Sprites.Clear();

            GR.IO.MemoryReader memIn = DataIn.MemoryReader();

            uint Version = memIn.ReadUInt32();

            if (Version == 2)
            {
                Colors.Palettes.Clear();

                GR.IO.FileChunk chunkMain = new GR.IO.FileChunk();

                while (chunkMain.ReadFromStream(memIn))
                {
                    switch (chunkMain.Type)
                    {
                    case FileChunkConstants.SPRITESET_PROJECT:
                    {
                        var chunkReader = chunkMain.MemoryReader();

                        GR.IO.FileChunk subChunk = new GR.IO.FileChunk();

                        while (subChunk.ReadFromStream(chunkReader))
                        {
                            var subChunkReader = subChunk.MemoryReader();

                            switch (subChunk.Type)
                            {
                            case FileChunkConstants.SPRITESET_INFO:
                                TotalNumberOfSprites = subChunkReader.ReadInt32();
                                Name              = subChunkReader.ReadString();
                                ExportFilename    = subChunkReader.ReadString();
                                ExportStartIndex  = subChunkReader.ReadInt32();
                                ExportSpriteCount = subChunkReader.ReadInt32();
                                break;

                            case FileChunkConstants.MULTICOLOR_DATA:
                                Mode = (SpriteProjectMode)subChunkReader.ReadInt32();
                                Colors.BackgroundColor = subChunkReader.ReadInt32();
                                Colors.MultiColor1     = subChunkReader.ReadInt32();
                                Colors.MultiColor2     = subChunkReader.ReadInt32();
                                Colors.ActivePalette   = 0;
                                break;

                            case FileChunkConstants.PALETTE:
                                Colors.Palettes.Add(Palette.Read(subChunkReader));
                                break;

                            case FileChunkConstants.SPRITESET_SPRITE:
                            {
                                var sprite = new SpriteData(new ColorSettings(Colors));

                                sprite.Mode             = (SpriteMode)subChunkReader.ReadInt32();
                                sprite.Tile.Mode        = (GraphicTileMode)subChunkReader.ReadInt32();
                                sprite.Tile.CustomColor = subChunkReader.ReadInt32();
                                sprite.Tile.Width       = subChunkReader.ReadInt32();
                                sprite.Tile.Height      = subChunkReader.ReadInt32();
                                int dataLength = subChunkReader.ReadInt32();
                                sprite.Tile.Data = new GR.Memory.ByteBuffer();
                                subChunkReader.ReadBlock(sprite.Tile.Data, (uint)dataLength);
                                if (sprite.Tile.CustomColor == -1)
                                {
                                    sprite.Tile.CustomColor = 1;
                                }

                                sprite.Tile.Colors.ActivePalette = subChunkReader.ReadInt32();
                                sprite.Tile.Image = new GR.Image.MemoryImage(sprite.Tile.Width, sprite.Tile.Height, GR.Drawing.PixelFormat.Format32bppRgb);

                                Sprites.Add(sprite);
                            }
                            break;

                            case FileChunkConstants.SPRITESET_LAYER:
                            {
                                Layer layer = new Layer();

                                SpriteLayers.Add(layer);

                                GR.IO.FileChunk subChunkL = new GR.IO.FileChunk();

                                while (subChunkL.ReadFromStream(subChunkReader))
                                {
                                    var subChunkReaderL = subChunkL.MemoryReader();

                                    if (subChunkL.Type == FileChunkConstants.SPRITESET_LAYER_ENTRY)
                                    {
                                        LayerSprite sprite = new LayerSprite();

                                        sprite.Index   = subChunkReaderL.ReadInt32();
                                        sprite.Color   = subChunkReaderL.ReadInt32();
                                        sprite.X       = subChunkReaderL.ReadInt32();
                                        sprite.Y       = subChunkReaderL.ReadInt32();
                                        sprite.ExpandX = (subChunkReaderL.ReadInt32() != 0);
                                        sprite.ExpandY = (subChunkReaderL.ReadInt32() != 0);

                                        layer.Sprites.Add(sprite);
                                    }
                                    else if (subChunkL.Type == FileChunkConstants.SPRITESET_LAYER_INFO)
                                    {
                                        layer.Name            = subChunkReaderL.ReadString();
                                        layer.BackgroundColor = subChunkReaderL.ReadInt32();
                                        layer.DelayMS         = subChunkReaderL.ReadInt32();
                                    }
                                }
                            }
                            break;
                            }
                        }
                    }
                    break;

                    default:
                        Debug.Log("SpriteProject.ReadFromBuffer unexpected chunk type " + chunkMain.Type.ToString("X"));
                        return(false);
                    }
                }

                return(true);
            }

            int numSprites = 256;

            if (Version >= 1)
            {
                numSprites = memIn.ReadInt32();
            }
            Sprites = new List <SpriteData>();
            for (int i = 0; i < numSprites; ++i)
            {
                Sprites.Add(new SpriteData(Colors));
                PaletteManager.ApplyPalette(Sprites[i].Tile.Image);
            }

            string name = memIn.ReadString();

            for (int i = 0; i < numSprites; ++i)
            {
                Sprites[i].Tile.CustomColor = memIn.ReadInt32();
            }
            for (int i = 0; i < numSprites; ++i)
            {
                Sprites[i].Mode      = (SpriteMode)memIn.ReadUInt8();
                Sprites[i].Tile.Mode = Lookup.GraphicTileModeFromSpriteMode(Sprites[i].Mode);
            }
            Colors.BackgroundColor = memIn.ReadInt32();
            Colors.MultiColor1     = memIn.ReadInt32();
            Colors.MultiColor2     = memIn.ReadInt32();

            bool genericMultiColor = (memIn.ReadUInt32() != 0);

            for (int i = 0; i < numSprites; ++i)
            {
                GR.Memory.ByteBuffer tempBuffer = new GR.Memory.ByteBuffer();

                memIn.ReadBlock(tempBuffer, 64);
                tempBuffer.CopyTo(Sprites[i].Tile.Data, 0, 63);
            }

            ExportSpriteCount = memIn.ReadInt32();

            ExportFilename = memIn.ReadString();
            string exportPathSpriteFile = memIn.ReadString();

            for (int i = 0; i < numSprites; ++i)
            {
                string desc = memIn.ReadString();
            }
            int spriteTestCount = memIn.ReadInt32();

            for (int i = 0; i < spriteTestCount; ++i)
            {
                int  spriteIndex      = memIn.ReadInt32();
                byte spriteColor      = memIn.ReadUInt8();
                bool spriteMultiColor = (memIn.ReadUInt8() != 0);
                int  spriteX          = memIn.ReadInt32();
                int  spriteY          = memIn.ReadInt32();
            }

            GR.IO.FileChunk chunk = new GR.IO.FileChunk();

            while (chunk.ReadFromStream(memIn))
            {
                switch (chunk.Type)
                {
                case FileChunkConstants.SPRITESET_LAYER:
                {
                    Layer layer = new Layer();

                    SpriteLayers.Add(layer);

                    var chunkReader = chunk.MemoryReader();

                    GR.IO.FileChunk subChunk = new GR.IO.FileChunk();

                    while (subChunk.ReadFromStream(chunkReader))
                    {
                        var subChunkReader = subChunk.MemoryReader();

                        if (subChunk.Type == FileChunkConstants.SPRITESET_LAYER_ENTRY)
                        {
                            LayerSprite sprite = new LayerSprite();

                            sprite.Index   = subChunkReader.ReadInt32();
                            sprite.Color   = subChunkReader.ReadUInt8();
                            sprite.X       = subChunkReader.ReadInt32();
                            sprite.Y       = subChunkReader.ReadInt32();
                            sprite.ExpandX = (subChunkReader.ReadUInt8() != 0);
                            sprite.ExpandY = (subChunkReader.ReadUInt8() != 0);

                            layer.Sprites.Add(sprite);
                        }
                        else if (subChunk.Type == FileChunkConstants.SPRITESET_LAYER_INFO)
                        {
                            layer.Name            = subChunkReader.ReadString();
                            layer.BackgroundColor = subChunkReader.ReadUInt8();
                            layer.DelayMS         = subChunkReader.ReadInt32();
                        }
                    }
                }
                break;
                }
            }

            while (Sprites.Count > 256)
            {
                Sprites.RemoveAt(256);
            }

            return(true);
        }