diff options
Diffstat (limited to 'Bitmap.cs')
| -rw-r--r-- | Bitmap.cs | 82 |
1 files changed, 41 insertions, 41 deletions
@@ -25,23 +25,19 @@ class Bitmap : IDisposable Texture = Raylib.LoadTextureFromImage(img); - //Get Data for Serialization - int imgSize = Raylib.GetPixelDataSize(img.Width, img.Height, PixelFormat.UncompressedR8G8B8A8); - Span<byte> imgData; - unsafe { - imgData = new(img.Data, imgSize); - } - using MemoryStream stream = new(); + //Write image info before compressed data + Span<byte> TexInfo = stackalloc byte[6]; + BinaryPrimitives.WriteInt16LittleEndian(TexInfo, (short)Width); + BinaryPrimitives.WriteInt16LittleEndian(TexInfo[2..], (short)Height); + TexInfo[4] = (byte)img.Format; + TexInfo[5] = (byte)img.Mipmaps; - //Write width and height before compressed data - Span<byte> WidthHeight = stackalloc byte[4]; - BinaryPrimitives.WriteInt16LittleEndian(WidthHeight, (short)Width); - BinaryPrimitives.WriteInt16LittleEndian(WidthHeight[2..], (short)Height); - stream.Write(WidthHeight); + using MemoryStream stream = new(); + stream.Write(TexInfo); - using DeflateStream compressor = new(stream, CompressionLevel.Optimal); - compressor.Write(imgData); - compressor.Close(); + DeflateStream compressor = new(stream, CompressionLevel.Optimal); + compressor.Write(img.DataSpan()); + compressor.Dispose(); SerializedData = stream.ToArray(); Raylib.UnloadImage(img); @@ -53,54 +49,58 @@ class Bitmap : IDisposable Texture = tex; } + public Bitmap Copy() + { + Image ImgCopy = Raylib.LoadImageFromTexture(Texture); + Bitmap Copy = new(Raylib.LoadTextureFromImage(ImgCopy), SerializedData.ToArray()); + Raylib.UnloadImage(ImgCopy); + return Copy; + } + public static Bitmap FromFile(string path, int MaxW = MaxWidth, int MaxH = MaxHeight) { if (!File.Exists(path) || new FileInfo(path).Length > 40000 || !path.Contains(".png")) return new Bitmap(Raylib.GenImageChecked(32, 32, 4, 4, Color.Black, Color.Pink)); - return new Bitmap(Raylib.LoadImage(path), MaxW, MaxH); + return new Bitmap(Raylib.LoadImageFromMemory(".png", File.ReadAllBytes(path)), MaxW, MaxH); } - public static Bitmap FromPNGMemory(byte[] memory, int MaxW = MaxWidth, int MaxH = MaxHeight) - { - return new Bitmap(Raylib.LoadImageFromMemory(".png", memory), MaxW, MaxH); - } + public static Bitmap FromPNGMemory(byte[] memory, int MaxW = MaxWidth, int MaxH = MaxHeight) => new(Raylib.LoadImageFromMemory(".png", memory), MaxW, MaxH); - public static Bitmap Deserialize(ReadOnlySpan<byte> span, int MaxW = MaxWidth, int MaxH = MaxHeight, byte[]? fallbackImage = null) + public static Bitmap Deserialize(ReadOnlySpan<byte> span, int MaxW = MaxWidth, int MaxH = MaxHeight, Image? fallbackImage = null) { int width = BinaryPrimitives.ReadInt16LittleEndian(span[..2]); int height = BinaryPrimitives.ReadInt16LittleEndian(span.Slice(2, 2)); + PixelFormat format = (PixelFormat)span[4]; + int mipmaps = span[5]; - bool Invalid = width <=0 || height <= 0 || width > MaxW || height > MaxH; - width = Math.Clamp(width, 1, MaxW); - height = Math.Clamp(height, 1, MaxH); + bool Invalid = width <= 0 || height <= 0 || width > MaxW || height > MaxH || format != PixelFormat.UncompressedR8G8B8A8; - //If the width and height are somehow wrong, then no use in loading the rest. - if (Invalid && fallbackImage != null) return FromPNGMemory(fallbackImage, MaxW, MaxH); - Image img = Raylib.GenImageChecked(width, height, 4, 4, Color.Pink, Color.Black); - Raylib.ImageFormat(ref img, PixelFormat.UncompressedR8G8B8A8); + //If the is somehow invalid, then no use in loading the rest. Either use fallback, or generate error checkerboard. + if (Invalid && fallbackImage != null) return new Bitmap(Raylib.ImageCopy(fallbackImage.Value), MaxW, MaxH); + Image img = Raylib.GenImageChecked(Math.Clamp(width, 8, MaxW), Math.Clamp(height, 8, MaxH), 4, 4, Color.Pink, Color.Black); if (Invalid) return new Bitmap(img); - byte[] compressed = span[4..].ToArray(); - byte[] data = new byte[Raylib.GetPixelDataSize(width, height, PixelFormat.UncompressedR8G8B8A8)]; + byte[] compressed = span[6..].ToArray(); + byte[] data = new byte[RaylibExt.TextureLength(width, height, format, mipmaps)]; { using MemoryStream input = new(compressed); using DeflateStream decompressor = new(input, CompressionMode.Decompress); decompressor.ReadExactly(data); } - - Texture2D tex = Raylib.LoadTextureFromImage(img); - Raylib.UpdateTexture(tex, data); - - Raylib.UnloadImage(img); - - return new Bitmap(tex, compressed); + unsafe {fixed (void* dataPtr = data) { + Texture2D tex = new() { + Id = Rlgl.LoadTexture(dataPtr, width, height, format, mipmaps), + Width = width, + Height = height, + Format = format, + Mipmaps = mipmaps + }; + return new Bitmap(tex, compressed); + }} } - public byte[] Serialize() - { - return SerializedData; - } + public byte[] Serialize() => SerializedData; bool disposed = false; |
