public GR.Memory.ByteBuffer ToBuffer()
        {
            GR.Memory.ByteBuffer result = new GR.Memory.ByteBuffer();

            result.AppendI32(Width);
            result.AppendI32(Height);
            result.AppendU32((uint)PixelFormat);

            result.AppendI32(PaletteEntryCount);
            for (int i = 0; i < PaletteEntryCount; ++i)
            {
                result.AppendU8(PaletteRed(i));
                result.AppendU8(PaletteGreen(i));
                result.AppendU8(PaletteBlue(i));
            }
            result.Append(m_ImageData);
            return(result);
        }
        public GR.Memory.ByteBuffer SaveToBuffer()
        {
            GR.Memory.ByteBuffer projectFile = new GR.Memory.ByteBuffer();

            projectFile.AppendU32(2);

            var chunkProject = new GR.IO.FileChunk(FileChunkConstants.SPRITESET_PROJECT);

            var chunkInfo = new GR.IO.FileChunk(FileChunkConstants.SPRITESET_INFO);

            chunkInfo.AppendI32(TotalNumberOfSprites);
            chunkInfo.AppendString(Name);
            chunkInfo.AppendString(ExportFilename);
            chunkInfo.AppendI32(ExportStartIndex);
            chunkInfo.AppendI32(ExportSpriteCount);
            chunkProject.Append(chunkInfo.ToBuffer());

            GR.IO.FileChunk chunkScreenMultiColorData = new GR.IO.FileChunk(FileChunkConstants.MULTICOLOR_DATA);
            chunkScreenMultiColorData.AppendI32((byte)Mode);
            chunkScreenMultiColorData.AppendI32((byte)Colors.BackgroundColor);
            chunkScreenMultiColorData.AppendI32((byte)Colors.MultiColor1);
            chunkScreenMultiColorData.AppendI32((byte)Colors.MultiColor2);
            chunkProject.Append(chunkScreenMultiColorData.ToBuffer());

            foreach (var pal in Colors.Palettes)
            {
                chunkProject.Append(pal.ToBuffer());
            }

            foreach (var sprite in Sprites)
            {
                GR.IO.FileChunk chunkSprite = new GR.IO.FileChunk(FileChunkConstants.SPRITESET_SPRITE);
                chunkSprite.AppendI32((int)sprite.Mode);
                chunkSprite.AppendI32((int)sprite.Tile.Mode);
                chunkSprite.AppendI32((int)sprite.Tile.CustomColor);
                chunkSprite.AppendI32(sprite.Tile.Width);
                chunkSprite.AppendI32(sprite.Tile.Height);
                chunkSprite.AppendI32((int)sprite.Tile.Data.Length);
                chunkSprite.Append(sprite.Tile.Data);
                chunkSprite.AppendI32(sprite.Tile.Colors.ActivePalette);

                chunkProject.Append(chunkSprite.ToBuffer());
            }

            foreach (var layer in SpriteLayers)
            {
                GR.IO.FileChunk chunkLayer = new GR.IO.FileChunk(FileChunkConstants.SPRITESET_LAYER);

                GR.IO.FileChunk chunkLayerInfo = new GR.IO.FileChunk(FileChunkConstants.SPRITESET_LAYER_INFO);
                chunkLayerInfo.AppendString(layer.Name);
                chunkLayerInfo.AppendI32((byte)layer.BackgroundColor);
                chunkLayerInfo.AppendI32(layer.DelayMS);
                chunkLayer.Append(chunkLayerInfo.ToBuffer());

                foreach (var sprite in layer.Sprites)
                {
                    GR.IO.FileChunk chunkLayerSprite = new GR.IO.FileChunk(FileChunkConstants.SPRITESET_LAYER_ENTRY);
                    chunkLayerSprite.AppendI32(sprite.Index);
                    chunkLayerSprite.AppendI32((byte)sprite.Color);
                    chunkLayerSprite.AppendI32(sprite.X);
                    chunkLayerSprite.AppendI32(sprite.Y);
                    chunkLayerSprite.AppendI32((byte)(sprite.ExpandX ? 1 : 0));
                    chunkLayerSprite.AppendI32((byte)(sprite.ExpandY ? 1 : 0));

                    chunkLayer.Append(chunkLayerSprite.ToBuffer());
                }
                chunkProject.Append(chunkLayer.ToBuffer());
            }
            projectFile.Append(chunkProject.ToBuffer());

            /*
             * // version
             * projectFile.AppendU32( 1 );
             * projectFile.AppendI32( Sprites.Count );
             * // Name
             * projectFile.AppendString( Name );
             * for ( int i = 0; i < Sprites.Count; ++i )
             * {
             * projectFile.AppendI32( Sprites[i].Color );
             * }
             * for ( int i = 0; i < Sprites.Count; ++i )
             * {
             * projectFile.AppendU8( (byte)Sprites[i].Mode );
             * }
             * projectFile.AppendI32( Colors.BackgroundColor );
             * projectFile.AppendI32( Colors.MultiColor1 );
             * projectFile.AppendI32( Colors.MultiColor2 );
             * // generic MC
             * projectFile.AppendU32( 0 );
             * for ( int i = 0; i < Sprites.Count; ++i )
             * {
             * projectFile.Append( Sprites[i].Tile.Data );
             * projectFile.AppendU8( (byte)Sprites[i].Color );
             * }
             * projectFile.AppendU32( ExportSpriteCount );
             *
             * // export name
             * projectFile.AppendString( ExportFilename );
             *
             * // exportpath
             * projectFile.AppendString( "" );
             *
             * // desc
             * for ( int i = 0; i < Sprites.Count; ++i )
             * {
             * projectFile.AppendString( "" );
             * }
             *
             * // testbed (not used anymore, write 0 as number of sprites)
             * projectFile.AppendI32( 0 );
             *
             *
             * foreach ( var layer in SpriteLayers )
             * {
             * GR.IO.FileChunk   chunkLayer = new GR.IO.FileChunk( FileChunkConstants.SPRITESET_LAYER );
             *
             * GR.IO.FileChunk   chunkLayerInfo = new GR.IO.FileChunk( FileChunkConstants.SPRITESET_LAYER_INFO );
             * chunkLayerInfo.AppendString( layer.Name );
             * chunkLayerInfo.AppendU8( (byte)layer.BackgroundColor );
             * chunkLayerInfo.AppendI32( layer.DelayMS );
             * chunkLayer.Append( chunkLayerInfo.ToBuffer() );
             *
             * foreach ( var sprite in layer.Sprites )
             * {
             *  GR.IO.FileChunk   chunkLayerSprite = new GR.IO.FileChunk( FileChunkConstants.SPRITESET_LAYER_ENTRY );
             *  chunkLayerSprite.AppendI32( sprite.Index );
             *  chunkLayerSprite.AppendU8( (byte)sprite.Color );
             *  chunkLayerSprite.AppendI32( sprite.X );
             *  chunkLayerSprite.AppendI32( sprite.Y );
             *  chunkLayerSprite.AppendU8( (byte)( sprite.ExpandX ? 1 : 0 ) );
             *  chunkLayerSprite.AppendU8( (byte)( sprite.ExpandY ? 1 : 0 ) );
             *
             *  chunkLayer.Append( chunkLayerSprite.ToBuffer() );
             * }
             * projectFile.Append( chunkLayer.ToBuffer() );
             * }*/

            return(projectFile);
        }