11using System ;
2+ using System . Collections . Generic ;
23using System . Drawing ;
34using System . Drawing . Imaging ;
4- using System . Reflection ;
5+ using System . Linq ;
56using System . Runtime . InteropServices ;
67
7- namespace ArcanumTextureSlicer
8+ namespace ArcanumTextureSlicer . Core
89{
910 public static class BitmapExtensions
1011 {
11- public const int TileWidth = 78 ;
12- public const int HalfTileWidth = 39 ;
13- public const int TileHeight = 40 ;
14- public const int HalfTileHeight = 20 ;
15- public const int TileXSpace = 2 ;
16- public const int HalfTileXSpace = 1 ;
17- private static Bitmap _sampleTile ;
18-
19- public static Bitmap SampleTile =>
20- _sampleTile ??
21- ( _sampleTile = new Bitmap ( Assembly . GetExecutingAssembly ( )
22- . GetManifestResourceStream ( "ArcanumTextureSlicer.Resources.SampleTile.png" ) ) ) ;
23-
2412 public static Point GetStartTileCenter ( this Bitmap source )
2513 {
2614 var point = new Point ( ) ;
@@ -34,34 +22,33 @@ public static Point GetStartTileCenter(this Bitmap source)
3422 Console . WriteLine ( $ "Start tile color { Color . Black } is not found in palette.") ;
3523 return point ;
3624 }
37- var tileRows = GetTileRows ( ) ;
3825 var sourceData = source . LockBits ( new Rectangle ( 0 , 0 , source . Width , source . Height ) ,
3926 ImageLockMode . ReadOnly , source . PixelFormat ) ;
4027 try
4128 {
4229 var sourceBytes = new byte [ sourceData . Height * sourceData . Stride ] ;
4330 Marshal . Copy ( sourceData . Scan0 , sourceBytes , 0 , sourceBytes . Length ) ;
4431
45- for ( var y = 0 ; y < sourceData . Height - HalfTileHeight + 1 ; y ++ )
32+ for ( var y = 0 ; y < sourceData . Height - Tile . HalfHeight + 1 ; y ++ )
4633 {
47- for ( var x = HalfTileWidth - 1 ; x < sourceData . Width - HalfTileWidth ; x ++ )
34+ for ( var x = Tile . HalfWidth - 1 ; x < sourceData . Width - Tile . HalfWidth ; x ++ )
4835 {
4936 var index = y * sourceData . Stride + x ;
5037 if ( sourceBytes [ index ] == colorIndex )
5138 {
52- for ( var r = 0 ; r < tileRows . Length ; r ++ )
39+ for ( var r = 0 ; r < Tile . Rows . Length ; r ++ )
5340 {
54- for ( var p = 0 ; p < tileRows [ r ] ; p ++ )
41+ for ( var p = 0 ; p < Tile . Rows [ r ] ; p ++ )
5542 {
56- var i = index + p + r * sourceData . Stride - ( tileRows [ r ] - 2 ) / 2 ;
43+ var i = index + p + r * sourceData . Stride - ( Tile . Rows [ r ] - 2 ) / 2 ;
5744 if ( sourceBytes [ i ] != colorIndex )
5845 {
5946 goto Continue ;
6047 }
6148 }
6249 }
6350 point . X = x + 1 ;
64- point . Y = y + HalfTileHeight ;
51+ point . Y = y + Tile . HalfHeight ;
6552 goto Finish ;
6653 }
6754 Continue :
@@ -79,26 +66,11 @@ public static Point GetStartTileCenter(this Bitmap source)
7966 return point ;
8067 }
8168
82- private static int [ ] GetTileRows ( )
83- {
84- var rows = new int [ 40 ] ;
85- for ( var i = 0 ; i < 20 ; i ++ )
86- {
87- rows [ i ] = 2 + i * 4 ;
88- }
89- for ( var i = 19 ; i >= 0 ; i -- )
90- {
91- rows [ 39 - i ] = 2 + i * 4 ;
92- }
93- return rows ;
94- }
95-
96- //private static bool
97-
9869 public static Bitmap CreateTile ( this Bitmap source , int x , int y )
9970 {
100- var tile = CloneRegion ( source , new Rectangle ( x , y , SampleTile . Width , SampleTile . Height ) ) ;
101- tile . DrawAlpha ( SampleTile ) ;
71+ var tile = CloneRegion ( source ,
72+ new Rectangle ( x - Tile . HalfWidth , y - Tile . HalfHeight , Tile . Width , Tile . Height ) ) ;
73+ tile . DrawAlpha ( ) ;
10274 return tile ;
10375 }
10476
@@ -142,43 +114,40 @@ public static void SetColor(this Bitmap canvas, byte colorIndex)
142114 }
143115 }
144116
145- public static void DrawAlpha ( this Bitmap canvas , Bitmap sample )
117+ public static void DrawAlpha ( this Bitmap tile )
146118 {
147- var rect = new Rectangle ( 0 , 0 , canvas . Width , canvas . Height ) ;
148-
149- var sampleData = sample . LockBits ( rect , ImageLockMode . ReadOnly , sample . PixelFormat ) ;
150- var canvasData = canvas . LockBits ( rect , ImageLockMode . WriteOnly , canvas . PixelFormat ) ;
119+ var rect = new Rectangle ( 0 , 0 , tile . Width , tile . Height ) ;
120+ var data = tile . LockBits ( rect , ImageLockMode . WriteOnly , tile . PixelFormat ) ;
151121 try
152122 {
153- var sampleAlphaIndex = sample . GetColorIndex ( Color . Blue ) ;
154-
155- var sampleBytes = new byte [ sampleData . Height * sampleData . Stride ] ;
156- var canvasBytes = new byte [ canvasData . Height * canvasData . Stride ] ;
157- Marshal . Copy ( sampleData . Scan0 , sampleBytes , 0 , sampleBytes . Length ) ;
158- Marshal . Copy ( canvasData . Scan0 , canvasBytes , 0 , canvasBytes . Length ) ;
123+ var canvasBytes = new byte [ data . Height * data . Stride ] ;
124+ Marshal . Copy ( data . Scan0 , canvasBytes , 0 , canvasBytes . Length ) ;
159125
160- for ( var y = 0 ; y < canvasData . Height ; y ++ )
126+ for ( var y = 0 ; y < Tile . Height ; y ++ )
161127 {
162- for ( var x = 0 ; x < canvasData . Width ; x ++ )
128+ for ( var x = 0 ; x < Tile . Width ; x ++ )
163129 {
164- if ( sampleBytes [ x + y * sampleData . Stride ] == sampleAlphaIndex )
130+ if ( ! Tile . HitTest ( x - Tile . HalfWidth , y - Tile . HalfHeight ) )
165131 {
166- canvasBytes [ x + y * canvasData . Stride ] = 0 ;
132+ canvasBytes [ x + y * data . Stride ] = 0 ;
167133 }
168134 }
169135 }
170136
171- Marshal . Copy ( canvasBytes , 0 , canvasData . Scan0 , canvasBytes . Length ) ;
137+ Marshal . Copy ( canvasBytes , 0 , data . Scan0 , canvasBytes . Length ) ;
172138 }
173139 finally
174140 {
175- sample . UnlockBits ( sampleData ) ;
176- canvas . UnlockBits ( canvasData ) ;
141+ tile . UnlockBits ( data ) ;
177142 }
178143 }
179144
180145 public static void DrawImage ( this Bitmap canvas , Bitmap source , int canvasX , int canvasY , Rectangle sourceRect )
181146 {
147+ if ( sourceRect . Width == 0 || sourceRect . Height == 0 )
148+ {
149+ return ;
150+ }
182151 var sourceData = source . LockBits ( sourceRect , ImageLockMode . ReadOnly , source . PixelFormat ) ;
183152 var canvasData = canvas . LockBits ( new Rectangle ( canvasX , canvasY , sourceRect . Width , sourceRect . Height ) ,
184153 ImageLockMode . WriteOnly , canvas . PixelFormat ) ;
@@ -251,5 +220,26 @@ public static bool IsTransparent(this Bitmap canvas)
251220 }
252221 return true ;
253222 }
223+
224+ public static IEnumerable < Tile > ToTiles ( this Bitmap bitmap , int initTileX , int initTileY )
225+ {
226+ return Tile . Split ( bitmap . Width - initTileX , bitmap . Height - initTileY )
227+ . Select ( t => new Tile
228+ {
229+ Row = t . Row ,
230+ Column = t . Column ,
231+ X = t . X + initTileX ,
232+ Y = t . Y + initTileY
233+ } ) ;
234+ }
235+
236+ public static Color ToColor ( this uint value )
237+ {
238+ return Color . FromArgb (
239+ ( byte ) ( ( value >> 24 ) & 0xFF ) ,
240+ ( byte ) ( ( value >> 16 ) & 0xFF ) ,
241+ ( byte ) ( ( value >> 8 ) & 0xFF ) ,
242+ ( byte ) ( value & 0xFF ) ) ;
243+ }
254244 }
255245}
0 commit comments