Remove System.Bitmap from badge and mod icons.

This commit is contained in:
Paul Chote
2018-12-22 08:41:10 +00:00
parent f65a777366
commit 84e965835b
6 changed files with 6 additions and 82 deletions

View File

@@ -15,6 +15,7 @@ using System.Collections.Generic;
using System.Drawing; using System.Drawing;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using OpenRA.FileFormats;
using OpenRA.Graphics; using OpenRA.Graphics;
namespace OpenRA namespace OpenRA
@@ -77,8 +78,7 @@ namespace OpenRA
if (iconNode != null && !string.IsNullOrEmpty(iconNode.Value.Value)) if (iconNode != null && !string.IsNullOrEmpty(iconNode.Value.Value))
{ {
using (var stream = new MemoryStream(Convert.FromBase64String(iconNode.Value.Value))) using (var stream = new MemoryStream(Convert.FromBase64String(iconNode.Value.Value)))
using (var bitmap = new Bitmap(stream)) mod.Icon = sheetBuilder.Add(new Png(stream));
mod.Icon = sheetBuilder.Add(bitmap);
} }
// Avoid possibly overwriting a valid mod with an obviously bogus one // Avoid possibly overwriting a valid mod with an obviously bogus one

View File

@@ -450,25 +450,6 @@ namespace OpenRA
public static Rectangle Bounds(this Bitmap b) { return new Rectangle(0, 0, b.Width, b.Height); } 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<bool> bits) public static int ToBits(this IEnumerable<bool> bits)
{ {
var i = 0; var i = 0;

View File

@@ -76,14 +76,6 @@ namespace OpenRA.Graphics
return rect; 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) public Sprite Add(Png src)
{ {
var rect = Allocate(new Size(src.Width, src.Height)); var rect = Allocate(new Size(src.Width, src.Height));

View File

@@ -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) public static Color PremultiplyAlpha(Color c)
{ {
if (c.A == byte.MaxValue) if (c.A == byte.MaxValue)

View File

@@ -15,6 +15,7 @@ using System.Collections.Generic;
using System.Drawing; using System.Drawing;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using OpenRA.FileFormats;
using OpenRA.FileSystem; using OpenRA.FileSystem;
using OpenRA.Graphics; using OpenRA.Graphics;
using OpenRA.Primitives; using OpenRA.Primitives;
@@ -83,8 +84,7 @@ namespace OpenRA
{ {
using (var stream = package.GetStream("icon.png")) using (var stream = package.GetStream("icon.png"))
if (stream != null) if (stream != null)
using (var bitmap = new Bitmap(stream)) icons[id] = sheetBuilder.Add(new Png(stream));
icons[id] = sheetBuilder.Add(bitmap);
} }
else if (!manifest.Metadata.Hidden) else if (!manifest.Metadata.Hidden)
Log.Write("debug", "Mod '{0}' is missing 'icon.png'.".F(path)); Log.Write("debug", "Mod '{0}' is missing 'icon.png'.".F(path));

View File

@@ -15,6 +15,7 @@ using System.Drawing;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Net; using System.Net;
using OpenRA.FileFormats;
using OpenRA.Graphics; using OpenRA.Graphics;
namespace OpenRA namespace OpenRA
@@ -64,7 +65,7 @@ namespace OpenRA
try try
{ {
var icon = new Bitmap(new MemoryStream(i.Result)); var icon = new Png(new MemoryStream(i.Result));
if (icon.Width == 24 && icon.Height == 24) if (icon.Width == 24 && icon.Height == 24)
{ {
Game.RunAfterTick(() => Game.RunAfterTick(() =>