From 84e965835ba1dd57d9c57cc5b60fb56ec3f1355a Mon Sep 17 00:00:00 2001 From: Paul Chote Date: Sat, 22 Dec 2018 08:41:10 +0000 Subject: [PATCH] Remove System.Bitmap from badge and mod icons. --- OpenRA.Game/ExternalMods.cs | 4 +-- OpenRA.Game/Exts.cs | 19 ----------- OpenRA.Game/Graphics/SheetBuilder.cs | 8 ----- OpenRA.Game/Graphics/Util.cs | 50 ---------------------------- OpenRA.Game/InstalledMods.cs | 4 +-- OpenRA.Game/PlayerDatabase.cs | 3 +- 6 files changed, 6 insertions(+), 82 deletions(-) diff --git a/OpenRA.Game/ExternalMods.cs b/OpenRA.Game/ExternalMods.cs index bbcc4c7ad3..43910f6e68 100644 --- a/OpenRA.Game/ExternalMods.cs +++ b/OpenRA.Game/ExternalMods.cs @@ -15,6 +15,7 @@ using System.Collections.Generic; using System.Drawing; using System.IO; using System.Linq; +using OpenRA.FileFormats; using OpenRA.Graphics; namespace OpenRA @@ -77,8 +78,7 @@ namespace OpenRA if (iconNode != null && !string.IsNullOrEmpty(iconNode.Value.Value)) { using (var stream = new MemoryStream(Convert.FromBase64String(iconNode.Value.Value))) - using (var bitmap = new Bitmap(stream)) - mod.Icon = sheetBuilder.Add(bitmap); + mod.Icon = sheetBuilder.Add(new Png(stream)); } // Avoid possibly overwriting a valid mod with an obviously bogus one diff --git a/OpenRA.Game/Exts.cs b/OpenRA.Game/Exts.cs index 3cdaffdbeb..685a033004 100644 --- a/OpenRA.Game/Exts.cs +++ b/OpenRA.Game/Exts.cs @@ -450,25 +450,6 @@ namespace OpenRA public static Rectangle Bounds(this Bitmap b) { return new Rectangle(0, 0, b.Width, b.Height); } - public static Bitmap CloneWith32bbpArgbPixelFormat(this Bitmap original) - { - // Note: We would use original.Clone(original.Bounds(), PixelFormat.Format32bppArgb) - // but this doesn't work on mono. - var clone = new Bitmap(original.Width, original.Height, PixelFormat.Format32bppArgb); - try - { - using (var g = System.Drawing.Graphics.FromImage(clone)) - g.DrawImage(original, original.Bounds()); - } - catch (Exception) - { - clone.Dispose(); - throw; - } - - return clone; - } - public static int ToBits(this IEnumerable bits) { var i = 0; diff --git a/OpenRA.Game/Graphics/SheetBuilder.cs b/OpenRA.Game/Graphics/SheetBuilder.cs index 8df23d7f8a..ac1947fab5 100644 --- a/OpenRA.Game/Graphics/SheetBuilder.cs +++ b/OpenRA.Game/Graphics/SheetBuilder.cs @@ -76,14 +76,6 @@ namespace OpenRA.Graphics return rect; } - public Sprite Add(Bitmap src) - { - var rect = Allocate(src.Size); - Util.FastCopyIntoSprite(rect, src); - current.CommitBufferedData(); - return rect; - } - public Sprite Add(Png src) { var rect = Allocate(new Size(src.Width, src.Height)); diff --git a/OpenRA.Game/Graphics/Util.cs b/OpenRA.Game/Graphics/Util.cs index a2af5ed27e..e2867d7c99 100644 --- a/OpenRA.Game/Graphics/Util.cs +++ b/OpenRA.Game/Graphics/Util.cs @@ -122,56 +122,6 @@ namespace OpenRA.Graphics } } - public static void FastCopyIntoSprite(Sprite dest, Bitmap src) - { - var createdTempBitmap = false; - if (src.PixelFormat != PixelFormat.Format32bppArgb) - { - src = src.CloneWith32bbpArgbPixelFormat(); - createdTempBitmap = true; - } - - try - { - var destData = dest.Sheet.GetData(); - var destStride = dest.Sheet.Size.Width; - var width = dest.Bounds.Width; - var height = dest.Bounds.Height; - - var srcData = src.LockBits(src.Bounds(), - ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb); - - unsafe - { - var c = (int*)srcData.Scan0; - - // 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; - - for (var j = 0; j < height; j++) - { - for (var i = 0; i < width; i++) - { - var cc = Color.FromArgb(*(c + (j * srcData.Stride >> 2) + i)); - data[(y + j) * destStride + x + i] = PremultiplyAlpha(cc).ToArgb(); - } - } - } - } - - src.UnlockBits(srcData); - } - finally - { - if (createdTempBitmap) - src.Dispose(); - } - } - public static Color PremultiplyAlpha(Color c) { if (c.A == byte.MaxValue) diff --git a/OpenRA.Game/InstalledMods.cs b/OpenRA.Game/InstalledMods.cs index 4d817bd4c8..4c2da70575 100644 --- a/OpenRA.Game/InstalledMods.cs +++ b/OpenRA.Game/InstalledMods.cs @@ -15,6 +15,7 @@ using System.Collections.Generic; using System.Drawing; using System.IO; using System.Linq; +using OpenRA.FileFormats; using OpenRA.FileSystem; using OpenRA.Graphics; using OpenRA.Primitives; @@ -83,8 +84,7 @@ namespace OpenRA { using (var stream = package.GetStream("icon.png")) if (stream != null) - using (var bitmap = new Bitmap(stream)) - icons[id] = sheetBuilder.Add(bitmap); + icons[id] = sheetBuilder.Add(new Png(stream)); } else if (!manifest.Metadata.Hidden) Log.Write("debug", "Mod '{0}' is missing 'icon.png'.".F(path)); diff --git a/OpenRA.Game/PlayerDatabase.cs b/OpenRA.Game/PlayerDatabase.cs index cc00c66574..78206c3f49 100644 --- a/OpenRA.Game/PlayerDatabase.cs +++ b/OpenRA.Game/PlayerDatabase.cs @@ -15,6 +15,7 @@ using System.Drawing; using System.IO; using System.Linq; using System.Net; +using OpenRA.FileFormats; using OpenRA.Graphics; namespace OpenRA @@ -64,7 +65,7 @@ namespace OpenRA try { - var icon = new Bitmap(new MemoryStream(i.Result)); + var icon = new Png(new MemoryStream(i.Result)); if (icon.Width == 24 && icon.Height == 24) { Game.RunAfterTick(() =>