Convert to 32bbp internally in FastCopyIntoSprite.

This avoids the need for callers to ensure the pixel format is correct, but ensures that the copying succeeds when the format is different.
This commit is contained in:
RoosterDragon
2015-04-17 18:44:17 +01:00
parent f77d6f6044
commit 9c93001c84
3 changed files with 24 additions and 26 deletions

View File

@@ -64,21 +64,33 @@ namespace OpenRA.Graphics
public static void FastCopyIntoSprite(Sprite dest, Bitmap src) public static void FastCopyIntoSprite(Sprite dest, Bitmap src)
{ {
var createdTempBitmap = false;
if (src.PixelFormat != PixelFormat.Format32bppArgb) if (src.PixelFormat != PixelFormat.Format32bppArgb)
throw new ArgumentException("src must have a PixelFormat of Format32bppArgb", "src"); {
src = src.CloneWith32bbpArgbPixelFormat();
createdTempBitmap = true;
}
var data = dest.Sheet.GetData(); try
var dataStride = dest.Sheet.Size.Width * 4; {
var x = dest.Bounds.Left * 4; var data = dest.Sheet.GetData();
var width = dest.Bounds.Width * 4; var dataStride = dest.Sheet.Size.Width * 4;
var y = dest.Bounds.Top; var x = dest.Bounds.Left * 4;
var height = dest.Bounds.Height; var width = dest.Bounds.Width * 4;
var y = dest.Bounds.Top;
var height = dest.Bounds.Height;
var bd = src.LockBits(src.Bounds(), var bd = src.LockBits(src.Bounds(),
ImageLockMode.ReadWrite, src.PixelFormat); ImageLockMode.ReadWrite, src.PixelFormat);
for (var row = 0; row < height; row++) for (var row = 0; row < height; row++)
Marshal.Copy(IntPtr.Add(bd.Scan0, row * bd.Stride), data, (y + row) * dataStride + x, width); Marshal.Copy(IntPtr.Add(bd.Scan0, row * bd.Stride), data, (y + row) * dataStride + x, width);
src.UnlockBits(bd); src.UnlockBits(bd);
}
finally
{
if (createdTempBitmap)
src.Dispose();
}
} }
public static float[] IdentityMatrix() public static float[] IdentityMatrix()

View File

@@ -392,16 +392,8 @@ namespace OpenRA
DefaultSubCell = (SubCell)Game.ModData.Manifest.SubCellDefaultIndex; DefaultSubCell = (SubCell)Game.ModData.Manifest.SubCellDefaultIndex;
if (Container.Exists("map.png")) if (Container.Exists("map.png"))
{
using (var dataStream = Container.GetContent("map.png")) using (var dataStream = Container.GetContent("map.png"))
CustomPreview = new Bitmap(dataStream); CustomPreview = new Bitmap(dataStream);
if (CustomPreview.PixelFormat != PixelFormat.Format32bppArgb)
{
var original = CustomPreview;
CustomPreview = original.CloneWith32bbpArgbPixelFormat();
original.Dispose();
}
}
PostInit(); PostInit();

View File

@@ -156,12 +156,6 @@ namespace OpenRA
SpawnPoints = spawns; SpawnPoints = spawns;
CustomPreview = new Bitmap(new MemoryStream(Convert.FromBase64String(r.minimap))); CustomPreview = new Bitmap(new MemoryStream(Convert.FromBase64String(r.minimap)));
if (CustomPreview.PixelFormat != PixelFormat.Format32bppArgb)
{
var original = CustomPreview;
CustomPreview = original.CloneWith32bbpArgbPixelFormat();
original.Dispose();
}
} }
catch (Exception) { } catch (Exception) { }