Remove System.Bitmap from UI artwork loading.

This commit is contained in:
Paul Chote
2018-12-23 00:23:48 +00:00
parent 52be0192f6
commit 4f10d4a302
2 changed files with 46 additions and 7 deletions

View File

@@ -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();

View File

@@ -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;