From 82fade25a6568255dfeeceae9b632541d717593a Mon Sep 17 00:00:00 2001 From: Paul Chote Date: Tue, 12 Feb 2019 12:50:10 +0000 Subject: [PATCH] Replace Sheet.AsBitmap with Sheet.AsPng. --- OpenRA.Game/Graphics/Sheet.cs | 45 +++++-------------- .../DumpSequenceSheetsCommand.cs | 2 +- 2 files changed, 12 insertions(+), 35 deletions(-) diff --git a/OpenRA.Game/Graphics/Sheet.cs b/OpenRA.Game/Graphics/Sheet.cs index 8617c2aa58..512ac47c03 100644 --- a/OpenRA.Game/Graphics/Sheet.cs +++ b/OpenRA.Game/Graphics/Sheet.cs @@ -11,9 +11,7 @@ using System; using System.Drawing; -using System.Drawing.Imaging; using System.IO; -using System.Runtime.InteropServices; using OpenRA.FileFormats; namespace OpenRA.Graphics @@ -79,48 +77,27 @@ namespace OpenRA.Graphics return texture; } - public Bitmap AsBitmap() + public Png AsPng() { - var d = GetData(); - var dataStride = 4 * Size.Width; - var bitmap = new Bitmap(Size.Width, Size.Height); - - var bd = bitmap.LockBits(bitmap.Bounds(), - ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb); - for (var y = 0; y < Size.Height; y++) - Marshal.Copy(d, y * dataStride, IntPtr.Add(bd.Scan0, y * bd.Stride), dataStride); - bitmap.UnlockBits(bd); - - return bitmap; + return new Png(GetData(), Size.Width, Size.Height); } - public Bitmap AsBitmap(TextureChannel channel, IPalette pal) + public Png AsPng(TextureChannel channel, IPalette pal) { var d = GetData(); + var plane = new byte[Size.Width * Size.Height]; var dataStride = 4 * Size.Width; - var bitmap = new Bitmap(Size.Width, Size.Height); var channelOffset = (int)channel; - var bd = bitmap.LockBits(bitmap.Bounds(), - ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb); - unsafe - { - var colors = (uint*)bd.Scan0; - for (var y = 0; y < Size.Height; y++) - { - var dataRowIndex = y * dataStride + channelOffset; - var bdRowIndex = y * bd.Stride / 4; - for (var x = 0; x < Size.Width; x++) - { - var paletteIndex = d[dataRowIndex + 4 * x]; - colors[bdRowIndex + x] = pal[paletteIndex]; - } - } - } + for (var y = 0; y < Size.Height; y++) + for (var x = 0; x < Size.Width; x++) + plane[y * Size.Width + x] = d[y * dataStride + channelOffset + 4 * x]; - bitmap.UnlockBits(bd); + var palColors = new Color[Palette.Size]; + for (var i = 0; i < Palette.Size; i++) + palColors[i] = pal.GetColor(i); - return bitmap; + return new Png(plane, Size.Width, Size.Height, palColors); } public void CreateBuffer() diff --git a/OpenRA.Mods.Common/UtilityCommands/DumpSequenceSheetsCommand.cs b/OpenRA.Mods.Common/UtilityCommands/DumpSequenceSheetsCommand.cs index 84b8949924..52764da75c 100644 --- a/OpenRA.Mods.Common/UtilityCommands/DumpSequenceSheetsCommand.cs +++ b/OpenRA.Mods.Common/UtilityCommands/DumpSequenceSheetsCommand.cs @@ -49,7 +49,7 @@ namespace OpenRA.Mods.Common.UtilityCommands { var max = s == sb.Current ? (int)sb.CurrentChannel + 1 : 4; for (var i = 0; i < max; i++) - s.AsBitmap((TextureChannel)ChannelMasks[i], palette).Save("{0}.png".F(count++)); + s.AsPng((TextureChannel)ChannelMasks[i], palette).Save("{0}.png".F(count++)); } Console.WriteLine("Saved [0..{0}].png", count - 1);