diff --git a/OpenRA.Game/Graphics/Sheet.cs b/OpenRA.Game/Graphics/Sheet.cs index f07e845622..70c79dc8d3 100644 --- a/OpenRA.Game/Graphics/Sheet.cs +++ b/OpenRA.Game/Graphics/Sheet.cs @@ -14,6 +14,7 @@ using System.Drawing; using System.Drawing.Imaging; using System.IO; using System.Runtime.InteropServices; +using OpenRA.FileFormats; namespace OpenRA.Graphics { @@ -50,13 +51,10 @@ namespace OpenRA.Graphics public Sheet(SheetType type, Stream stream) { - using (var bitmap = (Bitmap)Image.FromStream(stream)) - { - Size = bitmap.Size; - data = new byte[4 * Size.Width * Size.Height]; - - Util.FastCopyIntoSprite(new Sprite(this, bitmap.Bounds(), TextureChannel.Red), bitmap); - } + var png = new Png(stream); + Size = new Size(png.Width, png.Height); + data = new byte[4 * Size.Width * Size.Height]; + Util.FastCopyIntoSprite(new Sprite(this, new Rectangle(0, 0, png.Width, png.Height), TextureChannel.Red), png); Type = type; ReleaseBuffer(); diff --git a/OpenRA.Game/Graphics/Util.cs b/OpenRA.Game/Graphics/Util.cs index 1429e7b541..a2af5ed27e 100644 --- a/OpenRA.Game/Graphics/Util.cs +++ b/OpenRA.Game/Graphics/Util.cs @@ -12,6 +12,7 @@ using System; using System.Drawing; using System.Drawing.Imaging; +using OpenRA.FileFormats; namespace OpenRA.Graphics { @@ -81,6 +82,46 @@ namespace OpenRA.Graphics } } + public static void FastCopyIntoSprite(Sprite dest, Png src) + { + var destData = dest.Sheet.GetData(); + var destStride = dest.Sheet.Size.Width; + var width = dest.Bounds.Width; + var height = dest.Bounds.Height; + + unsafe + { + // Cast the data to an int array so we can copy the src data directly + fixed (byte* bd = &destData[0]) + { + var data = (int*)bd; + var x = dest.Bounds.Left; + var y = dest.Bounds.Top; + + var k = 0; + for (var j = 0; j < height; j++) + { + for (var i = 0; i < width; i++) + { + Color cc; + if (src.Palette == null) + { + var r = src.Data[k++]; + var g = src.Data[k++]; + var b = src.Data[k++]; + var a = src.Data[k++]; + cc = Color.FromArgb(a, r, g, b); + } + else + cc = src.Palette[src.Data[k++]]; + + data[(y + j) * destStride + x + i] = PremultiplyAlpha(cc).ToArgb(); + } + } + } + } + } + public static void FastCopyIntoSprite(Sprite dest, Bitmap src) { var createdTempBitmap = false;