@@ -46,11 +46,76 @@ public ImageUnpacker(Bitmap image, string fileName)
4646 }
4747 this . original = new Bitmap ( ( Bitmap ) image . Clone ( ) ) ;
4848 this . originalSize = image . Size ;
49+ this . original = this . RemoveTransparencyFromImage ( this . original ) ;
4950 this . boxes = new List < Rectangle > ( ) ;
5051 this . FileName = fileName ;
5152 this . IsLarge = ( this . original . Width * this . original . Height ) > ( 800 * 800 ) ;
5253 }
5354
55+ private Bitmap RemoveTransparencyFromImage ( Bitmap image )
56+ {
57+ Dictionary < int , Color > coloursByArgb = new Dictionary < int , Color > ( ) ;
58+ Dictionary < Point , Color > transparentPixelsByCoord = new Dictionary < Point , Color > ( ) ;
59+ Dictionary < Color , Color > opaquedByTransparent = new Dictionary < Color , Color > ( ) ;
60+ Color pixel ;
61+ bool containsTransparency = false ;
62+ Color transparentPixel ;
63+ Color opaquedPixel ;
64+
65+ for ( int x = 0 ; x < image . Width ; x ++ )
66+ {
67+ for ( int y = 0 ; y < image . Height ; y ++ )
68+ {
69+ pixel = image . GetPixel ( x , y ) ;
70+
71+ if ( ! coloursByArgb . ContainsKey ( pixel . ToArgb ( ) ) )
72+ {
73+ coloursByArgb . Add ( pixel . ToArgb ( ) , pixel ) ;
74+ }
75+ if ( pixel . A < 255 )
76+ {
77+ containsTransparency = true ;
78+ transparentPixelsByCoord . Add ( new Point ( x , y ) , pixel ) ;
79+ }
80+ }
81+ }
82+
83+ if ( containsTransparency )
84+ {
85+ foreach ( Point coord in transparentPixelsByCoord . Keys )
86+ {
87+ transparentPixel = transparentPixelsByCoord [ coord ] ;
88+ if ( opaquedByTransparent . ContainsKey ( transparentPixel ) )
89+ {
90+ opaquedPixel = opaquedByTransparent [ transparentPixel ] ;
91+ }
92+ else
93+ {
94+ opaquedPixel = Color . FromArgb ( 255 , transparentPixel ) ;
95+
96+ do
97+ {
98+ if ( transparentPixel . R > ( 255 / 2 ) )
99+ {
100+ opaquedPixel = Color . FromArgb ( opaquedPixel . R - 1 , opaquedPixel . G , opaquedPixel . B ) ;
101+ }
102+ else
103+ {
104+ opaquedPixel = Color . FromArgb ( opaquedPixel . R + 1 , opaquedPixel . G , opaquedPixel . B ) ;
105+ }
106+ } while ( coloursByArgb . ContainsKey ( opaquedPixel . ToArgb ( ) ) ) ;
107+
108+ coloursByArgb . Remove ( transparentPixel . ToArgb ( ) ) ;
109+ coloursByArgb . Add ( opaquedPixel . ToArgb ( ) , opaquedPixel ) ;
110+ opaquedByTransparent . Add ( transparentPixel , opaquedPixel ) ;
111+ }
112+ image . SetPixel ( coord . X , coord . Y , opaquedPixel ) ;
113+ }
114+ }
115+
116+ return image ;
117+ }
118+
54119 public System . Drawing . Imaging . ColorPalette GetPallette ( )
55120 {
56121 return this . pallette ;
@@ -330,8 +395,8 @@ private void HandleDividedArea(Rectangle region, bool updateCounter, Bitmap imag
330395
331396 private void SetBackgroundColour ( Bitmap image )
332397 {
333- Dictionary < Color , int > colours = new Dictionary < Color , int > ( ) ;
334- Color presentColour = default ( Color ) ;
398+ Dictionary < int , int > colourCountsByArgb = new Dictionary < int , int > ( ) ;
399+ Color presentColour ;
335400 int maxCount = 0 ;
336401
337402 for ( int x = 0 ; x <= this . originalSize . Width - 1 ; x ++ )
@@ -340,13 +405,13 @@ private void SetBackgroundColour(Bitmap image)
340405 {
341406 presentColour = image . GetPixel ( x , y ) ;
342407
343- if ( ! colours . ContainsKey ( presentColour ) )
408+ if ( ! colourCountsByArgb . ContainsKey ( presentColour . ToArgb ( ) ) )
344409 {
345- colours . Add ( presentColour , 1 ) ;
410+ colourCountsByArgb . Add ( presentColour . ToArgb ( ) , 1 ) ;
346411 }
347412 else
348413 {
349- colours [ presentColour ] += 1 ;
414+ colourCountsByArgb [ presentColour . ToArgb ( ) ] += 1 ;
350415 }
351416
352417 if ( ( x + y ) % 100 == 0 )
@@ -359,17 +424,17 @@ private void SetBackgroundColour(Bitmap image)
359424 }
360425 }
361426
362- foreach ( Color colour in colours . Keys )
427+ foreach ( int colourArgb in colourCountsByArgb . Keys )
363428 {
364- if ( colours [ colour ] >= maxCount )
429+ if ( colourCountsByArgb [ colourArgb ] >= maxCount )
365430 {
366- maxCount = colours [ colour ] ;
367- this . backgroundColour = colour ;
431+ maxCount = colourCountsByArgb [ colourArgb ] ;
432+ this . backgroundColour = Color . FromArgb ( colourArgb ) ;
368433 }
369434 }
370435 image . Dispose ( ) ;
371436 this . _isBackgroundColourSet = true ;
372- this . ColoursCount = colours . Count - 1 ;
437+ this . ColoursCount = colourCountsByArgb . Count - 1 ;
373438 }
374439
375440 private void HandleUnpackComplete ( )
0 commit comments