@@ -76,116 +76,114 @@ private void Btn_open_Click(object sender, EventArgs e)
7676 }
7777 }
7878 }
79-
80- #region Image2Code
81- private String GenDrawCode_BW1BbppH ( Image image )
79+ #region Color code conversion
80+ private Color color_from332 ( int pixel )
8281 {
83- String result = "uint8_t image = {" + Environment . NewLine + "\t " ;
84- int correction = image . Width % 8 ;
85- if ( correction > 0 )
86- correction = - correction + 8 ;
87-
88- using ( Bitmap bmp = new Bitmap ( image . Width + correction , image . Height ) )
89- {
90- using ( Graphics canvas = Graphics . FromImage ( bmp ) )
91- {
92- canvas . Clear ( Color . White ) ;
93- canvas . DrawImage ( image , 0 , 0 ) ;
94-
95- int x_step = 0 ;
96- int pixelsTotal = bmp . Width * bmp . Height ;
97- int pixelsCurrent = 0 ;
98-
99- for ( int y = 0 ; y < bmp . Height ; y ++ )
100- {
101- for ( int block = 0 ; block < bmp . Width / 8 ; block ++ )
102- {
103- int ColorByte = 0 ;
104-
105- for ( int bitInBlock = 7 ; bitInBlock >= 0 ; bitInBlock -- )
106- {
107- Color pixelColor = bmp . GetPixel ( ( block * 8 + 7 - bitInBlock ) , y ) ;
108- if ( pixelColor . R > 248 || pixelColor . G > 248 || pixelColor . B > 248 )
109- ColorByte &= ~ ( 1 << bitInBlock ) ;
110- else
111- ColorByte |= 1 << bitInBlock ;
112- pixelsCurrent ++ ; // For progress bar
113- }
114- result += "0x" + ColorByte . ToString ( "X2" ) + ", " ;
115- x_step ++ ;
116- if ( x_step == 16 )
117- {
118- x_step = 0 ;
119- result += Environment . NewLine + "\t " ;
120- }
82+ // Format: RRRG GGBB (Hex) 8*8*4=256 colors
83+ // 1110 0000 = 0xE0
84+ // 0001 1100 = 0x1C
85+ // 0000 0011 = 0x03
86+ return Color . FromArgb ( ( pixel & 0xE0 ) , ( pixel & 0x1C ) << 3 , ( pixel & 0x3 ) << 6 ) ;
87+ }
12188
122- bgWork . ReportProgress ( ( int ) ( ( double ) pixelsCurrent / ( double ) pixelsTotal * 100 ) ) ;
123- }
124- }
125- }
126- }
89+ private Color color_from444 ( int pixel )
90+ {
91+ // Format: 0000 RRRR GGGG BBBB, 16*16*16=4096 colors
92+ // 0000 1111 0000 0000 = 0x0F00
93+ // 0000 0000 1111 0000 = 0x00F0
94+ // 0000 0000 0000 1111 = 0x000F
95+ return Color . FromArgb ( ( pixel & 0x0F00 ) >> 4 , ( pixel & 0x00F0 ) , ( pixel & 0x000F ) << 4 ) ;
96+ }
12797
128- result += Environment . NewLine + "};" ;
129- return result ;
98+ private Color color_from556 ( int pixel )
99+ {
100+ // Format: RRRR RGGG GGGB BBBB (Hex) 32*32*64=65536 colors
101+ // 1111 1000 0000 0000 = 0xF800
102+ // 0000 0111 1110 0000 = 0x07E0
103+ // 0000 0000 0001 1111 = 0x001F
104+ return Color . FromArgb ( ( pixel & 0xF800 ) >> 8 , ( pixel & 0x7E0 ) >> 3 , ( pixel & 0x1F ) << 3 ) ;
130105 }
131-
132- private String GenDrawCode_BW1BbppV ( Image image )
106+ #endregion
107+
108+ #region Image2Code
109+ private String CodeFromImage_1bpp ( Image image , bool horisontal = false )
133110 {
134111 String result = "uint8_t image = {" + Environment . NewLine + "\t " ;
135- int correction = image . Height % 8 ;
136- if ( correction > 0 )
137- correction = - correction + 8 ;
112+ int addW = 0 ;
113+ int addH = 0 ;
114+ if ( horisontal )
115+ {
116+ // Horisontal
117+ addW = image . Width % 8 ;
118+ if ( addW > 0 )
119+ addW = - addW + 8 ;
120+ } else
121+ {
122+ // Vertical
123+ addH = image . Height % 8 ;
124+ if ( addH > 0 )
125+ addH = - addH + 8 ;
126+ }
138127
139- using ( Bitmap bmp = new Bitmap ( image . Width , image . Height + correction ) )
128+ using ( Bitmap bmp = new Bitmap ( image . Width + addW , image . Height + addH ) )
140129 {
141130 using ( Graphics canvas = Graphics . FromImage ( bmp ) )
142131 {
143132 canvas . Clear ( Color . White ) ;
144- canvas . DrawImage ( image , 0 , 0 ) ;
145-
133+ canvas . DrawImage ( image , 0 , 0 , image . Width , image . Height ) ;
134+
146135 int x_step = 0 ;
147-
148136 int pixelsTotal = bmp . Width * bmp . Height ;
149137 int pixelsCurrent = 0 ;
138+ int bitInBlock = 7 ;
139+ int ColorByte = 0 ;
140+ Color pixelColor ;
150141
151- for ( int vBlock = 0 ; vBlock < bmp . Height / 8 ; vBlock ++ )
142+ for ( int y = 0 ; y < bmp . Height ; y ++ )
152143 {
153144 for ( int x = 0 ; x < bmp . Width ; x ++ )
154145 {
155- int ColorByte = 0 ;
156- //result += "B"; // Show in byte format: B11110000 (0xf0)
157- for ( int bitInBlock = 0 ; bitInBlock < 8 ; bitInBlock ++ )
146+ for ( int tmpY = 0 ; tmpY < ( ( horisontal ) ? 1 : 8 ) ; tmpY ++ )
158147 {
159- Color pixelColor = bmp . GetPixel ( x , vBlock * 8 + bitInBlock ) ;
148+ pixelColor = bmp . GetPixel ( x , ( ( horisontal ) ? y : y + tmpY ) ) ;
149+
160150 if ( pixelColor . R > 248 || pixelColor . G > 248 || pixelColor . B > 248 )
161- ColorByte &= ~ ( 1 << bitInBlock ) ; // result += "0";
151+ ColorByte &= ~ ( 1 << bitInBlock ) ; // White
162152 else
163- ColorByte |= 1 << bitInBlock ; // result += "1";
164- pixelsCurrent ++ ;
165- }
166- result += "0x" + ColorByte . ToString ( "X2" ) + ", " ;
167-
168- x_step ++ ;
169- if ( x_step == 16 )
170- {
171- x_step = 0 ;
172- result += Environment . NewLine + "\t " ;
173- }
153+ ColorByte |= 1 << bitInBlock ; // Black
154+
155+ if ( bitInBlock == 0 )
156+ {
157+ result += "0x" + ColorByte . ToString ( "X2" ) + ", " ;
158+ x_step ++ ;
159+ if ( x_step == 16 )
160+ {
161+ x_step = 0 ;
162+ result += Environment . NewLine + "\t " ;
163+ }
164+ ColorByte = 0 ;
165+ bitInBlock = 7 ;
166+ }
167+ else
168+ bitInBlock -- ;
174169
175- bgWork . ReportProgress ( ( int ) ( ( double ) pixelsCurrent / ( double ) pixelsTotal * 100 ) ) ;
170+ bgWork . ReportProgress ( ( int ) ( ( double ) pixelsCurrent ++ / ( double ) pixelsTotal * 100 ) ) ;
171+ }
176172 }
173+ if ( ! horisontal )
174+ y += 7 ;
177175 }
178176 }
179177 }
178+
180179 result += Environment . NewLine + "};" ;
181180 return result ;
182181 }
183182
184- private string GenCodeFromImage ( TransformColorFormats format )
183+ private string CodeFromImage ( TransformColorFormats format )
185184 {
186185 StringBuilder result = new StringBuilder ( ) ;
187186 string codeFormat = "" ;
188- int appendSize = 0 ;
189187 int Width = 0 , Height = 0 ;
190188 Image image = null ;
191189
@@ -200,12 +198,10 @@ private string GenCodeFromImage(TransformColorFormats format)
200198 case TransformColorFormats . RGB332 :
201199 result . Append ( "uint8_t" ) ;
202200 codeFormat = "0x{0:x2}, " ;
203- appendSize = 6 ; // "0xAB, "
204201 break ;
205202 case TransformColorFormats . RGB565 :
206203 result . Append ( "uint16_t" ) ;
207204 codeFormat = "0x{0:x4}, " ;
208- appendSize = 8 ; // "0xABCD, "
209205 break ;
210206 }
211207 result . Append ( " image = {" + Environment . NewLine + "\t " ) ;
@@ -269,14 +265,14 @@ private void ImageToCode(object sender, DoWorkEventArgs e)
269265 switch ( e . Argument )
270266 {
271267 case TransformColorFormats . BW1BppH :
272- e . Result = GenDrawCode_BW1BbppH ( OriginalImage ) ;
268+ e . Result = CodeFromImage_1bpp ( OriginalImage , true ) ;
273269 break ;
274270 case TransformColorFormats . BW1BppV :
275- e . Result = GenDrawCode_BW1BbppV ( OriginalImage ) ;
271+ e . Result = CodeFromImage_1bpp ( OriginalImage , false ) ;
276272 break ;
277273 case TransformColorFormats . RGB332 :
278274 case TransformColorFormats . RGB565 :
279- e . Result = GenCodeFromImage ( ( TransformColorFormats ) e . Argument ) ;
275+ e . Result = CodeFromImage ( ( TransformColorFormats ) e . Argument ) ;
280276 //e.Result = GenDrawCode_RGB332(OriginalImage);
281277 break ;
282278 //case TransformColorFormats.RGB565:
@@ -289,8 +285,11 @@ private void ImageToCode(object sender, DoWorkEventArgs e)
289285 }
290286
291287 #region Code2Image
292- private Image GenImageFromCode ( TransformColorFormats format )
288+
289+ private void CodeToImage ( object sender , DoWorkEventArgs e )
293290 {
291+ TransformColorFormats format = ( TransformColorFormats ) e . Argument ;
292+
294293 string Code = "" ;
295294 int Width = 0 , Height = 0 ;
296295 int x = 0 , y = 0 ;
@@ -301,7 +300,7 @@ private Image GenImageFromCode(TransformColorFormats format)
301300 Code = GeneratedCode . Text ;
302301 } ) ) ;
303302
304- Bitmap result = new Bitmap ( Width , Height , PixelFormat . Format16bppRgb565 ) ;
303+ Bitmap result = new Bitmap ( Width , Height ) ;
305304
306305 int pixelsCurrent = 0 ;
307306 int pixelsTotal = Width * Height ;
@@ -316,45 +315,50 @@ private Image GenImageFromCode(TransformColorFormats format)
316315 case TransformColorFormats . BW1BppH :
317316 case TransformColorFormats . BW1BppV :
318317 case TransformColorFormats . RGB332 :
319- regex = @"0x([0-9a-fA-F]{2})," ;
318+ regex = @"0x([0-9a-fA-F]{2})," ; // 0xAB (one byte)
320319 break ;
321320 case TransformColorFormats . RGB565 :
322- regex = @"0x([0-9a-fA-F]{4})," ;
321+ regex = @"0x([0-9a-fA-F]{4})," ; // 0xABCD (two bytes)
323322 break ;
324323 }
325324
326325 foreach ( Match m in Regex . Matches ( Code , regex ) )
327326 {
328327 pixelColor = int . Parse ( m . Groups [ 1 ] . Value , System . Globalization . NumberStyles . HexNumber ) ;
328+
329329 switch ( format )
330330 {
331- //case TransformColorFormats.BW1BppH:
332- // break;
333- //case TransformColorFormats.BW1BppV:
334- // break;
331+ case TransformColorFormats . BW1BppH :
332+ for ( int offset = 7 ; offset >= 0 ; offset -- )
333+ {
334+ result . SetPixel ( x , y , ( ( pixelColor & ( 1 << offset ) ) > 0 ) ? Color . Black : Color . White ) ;
335+ x ++ ;
336+ }
337+ x -- ;
338+ break ;
339+ case TransformColorFormats . BW1BppV :
340+ for ( int offset = 7 ; offset >= 0 ; offset -- )
341+ {
342+ result . SetPixel ( x , y + ( - offset + 7 ) , ( ( pixelColor & ( 1 << offset ) ) > 0 ) ? Color . Black : Color . White ) ;
343+ }
344+ break ;
335345 case TransformColorFormats . RGB332 : // RRRG GGBB
336- result . SetPixel ( x , y , Color . FromArgb (
337- ( pixelColor & 0xE0 ) ,
338- ( pixelColor & 0x1C ) << 3 ,
339- ( pixelColor & 0x3 ) << 6 )
340- ) ;
346+ result . SetPixel ( x , y , color_from332 ( pixelColor ) ) ;
341347 break ;
342348 case TransformColorFormats . RGB565 : // RRRR RGGG GGGB BBBB
343- result . SetPixel ( x , y , Color . FromArgb (
344- ( pixelColor & 0xF800 ) >> 8 ,
345- ( pixelColor & 0x7E0 ) >> 3 ,
346- ( pixelColor & 0x1F ) << 3 )
347- ) ;
348- break ;
349- default :
349+ result . SetPixel ( x , y , color_from556 ( pixelColor ) ) ;
350350 break ;
351351 }
352-
352+
353353 x ++ ;
354354 if ( x == Width )
355355 {
356356 x = 0 ;
357- y ++ ;
357+ if ( format == TransformColorFormats . BW1BppV )
358+ y += 8 ;
359+ else
360+ y += 1 ;
361+
358362 if ( y == Height )
359363 {
360364 break ; // All done. Stop conversion.
@@ -365,25 +369,11 @@ private Image GenImageFromCode(TransformColorFormats format)
365369 bgWork . ReportProgress ( ( int ) ( ( double ) pixelsCurrent / ( double ) pixelsTotal * 100 ) ) ;
366370 }
367371
368- return result ;
369- }
370- #endregion
371-
372- private void CodeToImage ( object sender , DoWorkEventArgs e )
373- {
374- switch ( e . Argument )
375- {
376- case TransformColorFormats . RGB565 :
377- case TransformColorFormats . RGB332 :
378- e . Result = GenImageFromCode ( ( TransformColorFormats ) e . Argument ) ;
379- break ;
380- default :
381- MessageBox . Show ( "Sorry, unsupported." ) ;
382- e . Result = OriginalImage ;
383- break ;
384- }
372+ e . Result = result ;
385373 }
386374
375+ #endregion
376+
387377 private void BgConvertProgress ( object sender , ProgressChangedEventArgs e )
388378 {
389379 convertProgress . Value = e . ProgressPercentage ;
0 commit comments