Skip to content

Commit b9f2e74

Browse files
Update
1 parent ff585b0 commit b9f2e74

File tree

4 files changed

+63
-8
lines changed

4 files changed

+63
-8
lines changed

ArcanumTextureSlicer/ArcanumTextureSlicer/ArcanumTextureSlicer.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@
5151
<ItemGroup>
5252
<None Include="App.config" />
5353
</ItemGroup>
54+
<ItemGroup>
55+
<EmbeddedResource Include="Resources\SampleTile.png" />
56+
</ItemGroup>
5457
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
5558
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
5659
Other similar extension points exist, see Microsoft.Common.targets.

ArcanumTextureSlicer/ArcanumTextureSlicer/BitmapExtensions.cs

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,37 @@
11
using System;
22
using System.Drawing;
33
using System.Drawing.Imaging;
4+
using System.Reflection;
45
using System.Runtime.InteropServices;
56

67
namespace ArcanumTextureSlicer
78
{
89
public static class BitmapExtensions
910
{
11+
private static Bitmap _sampleTile;
12+
13+
public static readonly Color AlphaColor = Color.Blue;
14+
15+
public static Bitmap SampleTile =>
16+
_sampleTile ??
17+
(_sampleTile = new Bitmap(Assembly.GetExecutingAssembly()
18+
.GetManifestResourceStream("ArcanumTextureSlicer.Resources.SampleTile.png")));
19+
20+
public static Bitmap CreateTile(this Bitmap source, int x, int y)
21+
{
22+
var tile = CloneRegion(source, new Rectangle(x, y, SampleTile.Width, SampleTile.Height));
23+
tile.DrawAlpha(SampleTile);
24+
return tile;
25+
}
26+
1027
public static Bitmap CloneRegion(this Bitmap source, Rectangle rect)
1128
{
1229
if (rect.X >= 0 && rect.Y >= 0 && rect.Right <= source.Width && rect.Bottom <= source.Height)
1330
{
1431
return source.Clone(rect, source.PixelFormat);
1532
}
1633
var bitmap = source.Clone(new Rectangle(0, 0, rect.Width, rect.Height), source.PixelFormat);
17-
bitmap.SetColor(Color.Blue);
34+
bitmap.SetColor(AlphaColor);
1835
bitmap.DrawImage(source,
1936
Math.Max(-rect.X, 0),
2037
Math.Max(-rect.Y, 0),
@@ -38,6 +55,42 @@ public static void SetColor(this Bitmap canvas, Color color)
3855
canvas.UnlockBits(data);
3956
}
4057

58+
public static void DrawAlpha(this Bitmap canvas, Bitmap sample)
59+
{
60+
var rect = new Rectangle(0, 0, canvas.Width, canvas.Height);
61+
62+
var sampleData = sample.LockBits(rect, ImageLockMode.ReadOnly, sample.PixelFormat);
63+
var canvasData = canvas.LockBits(rect, ImageLockMode.WriteOnly, canvas.PixelFormat);
64+
try
65+
{
66+
var sampleAlphaIndex = sample.GetColorIndex(AlphaColor);
67+
var canvasAlphaIndex = canvas.GetColorIndex(AlphaColor);
68+
69+
var sampleBytes = new byte[sampleData.Height*sampleData.Stride];
70+
var canvasBytes = new byte[canvasData.Height*canvasData.Stride];
71+
Marshal.Copy(sampleData.Scan0, sampleBytes, 0, sampleBytes.Length);
72+
Marshal.Copy(canvasData.Scan0, canvasBytes, 0, canvasBytes.Length);
73+
74+
for (var y = 0; y < canvasData.Height; y++)
75+
{
76+
for (var x = 0; x < canvasData.Width; x++)
77+
{
78+
if (sampleBytes[x + y*sampleData.Stride] == sampleAlphaIndex)
79+
{
80+
canvasBytes[x + y*canvasData.Stride] = canvasAlphaIndex;
81+
}
82+
}
83+
}
84+
85+
Marshal.Copy(canvasBytes, 0, canvasData.Scan0, canvasBytes.Length);
86+
}
87+
finally
88+
{
89+
sample.UnlockBits(sampleData);
90+
canvas.UnlockBits(canvasData);
91+
}
92+
}
93+
4194
public static void DrawImage(this Bitmap canvas, Bitmap source, int canvasX, int canvasY, Rectangle sourceRect)
4295
{
4396
var sourceData = source.LockBits(sourceRect, ImageLockMode.ReadOnly, source.PixelFormat);

ArcanumTextureSlicer/ArcanumTextureSlicer/Program.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ public static void Main(string[] args)
1818
var inputFile = args[0];
1919
var outputFolder = args[1];
2020

21-
const int tileWidth = 78;
22-
const int tileHeight = 40;
23-
const int halfTileWidth = 39;
24-
const int halfTileHeight = 20;
21+
var tileWidth = BitmapExtensions.SampleTile.Width;
22+
var tileHeight = BitmapExtensions.SampleTile.Height;
23+
var halfTileWidth = tileWidth/2;
24+
var halfTileHeight = tileHeight/2;
2525

2626
try
2727
{
@@ -33,10 +33,9 @@ public static void Main(string[] args)
3333
{
3434
for (var j = 0; j < m; j++)
3535
{
36-
using (var outputBitmap = inputBitmap.CloneRegion(new Rectangle(
36+
using (var outputBitmap = inputBitmap.CreateTile(
3737
i*tileWidth - halfTileWidth*(1 - j%2),
38-
j*tileHeight/2,
39-
tileWidth, tileHeight)))
38+
j*halfTileHeight - halfTileHeight))
4039
{
4140
try
4241
{
4.99 KB
Loading

0 commit comments

Comments
 (0)