Faster bitmap editing

This commit is contained in:
Paul Chote
2010-07-15 21:25:02 +12:00
parent 45061cdf37
commit b3413dc783

View File

@@ -70,15 +70,22 @@ namespace OpenRA.Graphics
public static Bitmap RenderTerrainBitmap(Map map, TileSet tileset) public static Bitmap RenderTerrainBitmap(Map map, TileSet tileset)
{ {
var terrain = new Bitmap(map.MapSize.X, map.MapSize.Y); var terrain = new Bitmap(map.MapSize.X, map.MapSize.Y);
var bitmapData = terrain.LockBits(new Rectangle(0, 0, terrain.Width, terrain.Height),
ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
unsafe
{
int* c = (int*)bitmapData.Scan0;
for (var x = 0; x < map.MapSize.X; x++) for (var x = 0; x < map.MapSize.X; x++)
for (var y = 0; y < map.MapSize.Y; y++) for (var y = 0; y < map.MapSize.Y; y++)
{ {
var type = tileset.GetTerrainType(map.MapTiles[x, y]); var type = tileset.GetTerrainType(map.MapTiles[x, y]);
terrain.SetPixel(x, y, map.IsInMap(x, y) *(c + (y * bitmapData.Stride >> 2) + x) = map.IsInMap(x, y)
? Color.FromArgb(alpha, tileset.Terrain[type].Color) ? Color.FromArgb(alpha, tileset.Terrain[type].Color).ToArgb()
: shroudColor); : shroudColor.ToArgb();
} }
}
terrain.UnlockBits(bitmapData);
return terrain; return terrain;
} }
@@ -182,6 +189,13 @@ namespace OpenRA.Graphics
var size = NextPowerOf2(Math.Max(map.Width, map.Height)); var size = NextPowerOf2(Math.Max(map.Width, map.Height));
Bitmap terrain = new Bitmap(size, size); Bitmap terrain = new Bitmap(size, size);
var bitmapData = terrain.LockBits(new Rectangle(0, 0, terrain.Width, terrain.Height),
ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
unsafe
{
int* c = (int*)bitmapData.Scan0;
for (var x = 0; x < map.Width; x++) for (var x = 0; x < map.Width; x++)
for (var y = 0; y < map.Height; y++) for (var y = 0; y < map.Height; y++)
{ {
@@ -196,8 +210,11 @@ namespace OpenRA.Graphics
if (res != null) if (res != null)
type = res; type = res;
terrain.SetPixel(x, y, tileset.Terrain[type].Color); *(c + (y * bitmapData.Stride >> 2) + x) = tileset.Terrain[type].Color.ToArgb();
} }
}
terrain.UnlockBits(bitmapData);
return terrain; return terrain;
} }
} }