@@ -180,104 +180,88 @@ private String GenDrawCode_BW1BbppV(Image image)
180180 result += Environment . NewLine + "};" ;
181181 return result ;
182182 }
183-
184- private String GenDrawCode_RGB332 ( Image image )
183+
184+ private string GenCodeFromImage ( TransformColorFormats format )
185185 {
186- String result = "uint8_t image = {" + Environment . NewLine + "\t " ;
186+ StringBuilder result = new StringBuilder ( ) ;
187+ string codeFormat = "" ;
188+ int appendSize = 0 ;
189+ int Width = 0 , Height = 0 ;
190+ Image image = null ;
187191
188- using ( Bitmap bmp = new Bitmap ( image ) )
192+ this . Invoke ( new MethodInvoker ( delegate {
193+ image = imageBox . Image ;
194+ Width = ( int ) num_Width . Value ;
195+ Height = ( int ) num_Height . Value ;
196+ } ) ) ;
197+
198+ switch ( format )
189199 {
190- int x_step = 0 ;
200+ case TransformColorFormats . RGB332 :
201+ result . Append ( "uint8_t" ) ;
202+ codeFormat = "0x{0:x2}, " ;
203+ appendSize = 6 ; // "0xAB, "
204+ break ;
205+ case TransformColorFormats . RGB565 :
206+ result . Append ( "uint16_t" ) ;
207+ codeFormat = "0x{0:x4}, " ;
208+ appendSize = 8 ; // "0xABCD, "
209+ break ;
210+ }
211+ result . Append ( " image = {" + Environment . NewLine + "\t " ) ;
212+
213+ using ( Bitmap bmp = new Bitmap ( image , Width , Height ) )
214+ {
215+ int rowPos = 0 ;
191216 Color pixelColor ;
192217
193- // 6 = size of "0x12, ", 16 - every 16 lines we add newline and tab
194- StringBuilder str = new StringBuilder ( ( bmp . Width * bmp . Height * 6 ) + ( bmp . Width * bmp . Height / 16 * ( Environment . NewLine . Length + 1 ) ) ) ;
195-
196218 int pixelsTotal = bmp . Width * bmp . Height ;
197219 int pixelsCurrent = 0 ;
220+ int ColorByte = 0 ;
198221
199222 for ( int y = 0 ; y < bmp . Height ; y ++ )
200223 {
201224 for ( int x = 0 ; x < bmp . Width ; x ++ )
202225 {
203226 pixelColor = bmp . GetPixel ( x , y ) ;
204- // byte = 8 bit per color
205- // RRRG GGBB
206- int ColorByte =
207- ( pixelColor . R & 0xE0 ) |
208- ( pixelColor . G & 0xE0 ) >> 3 |
209- ( pixelColor . B & 0xC0 ) >> 6 ;
210-
211- //result += "0x" + ColorByte.ToString("X2") + ", ";
212- str . AppendFormat ( "0x{0:x2}, " , ColorByte ) ;
213227
214- pixelsCurrent ++ ;
215-
216- x_step ++ ;
217- if ( x_step == 16 )
228+ switch ( format )
218229 {
219- x_step = 0 ;
220- //result += Environment.NewLine + "\t";
221- str . Append ( Environment . NewLine + "\t " ) ;
230+ case TransformColorFormats . RGB332 :
231+ ColorByte =
232+ ( pixelColor . R & 0xE0 ) |
233+ ( pixelColor . G & 0xE0 ) >> 3 |
234+ ( pixelColor . B & 0xC0 ) >> 6 ;
235+ break ;
236+ case TransformColorFormats . RGB565 :
237+ // byte = 16 bit per color
238+ // RRRR RGGG GGGB BBBB
239+ ColorByte =
240+ ( pixelColor . R & 0xF8 ) << 8 |
241+ ( pixelColor . G & 0xFC ) << 3 |
242+ ( pixelColor . B & 0xF8 ) >> 3 ;
243+ break ;
222244 }
223245
224- bgWork . ReportProgress ( ( int ) ( ( double ) pixelsCurrent / ( double ) pixelsTotal * 100 ) ) ;
225- }
226- }
227- result += str . ToString ( ) ;
228- }
229- result += Environment . NewLine + "};" ;
230- return result ;
231- }
232-
233-
234- private String GenDrawCode_RGB565 ( Image image )
235- {
236- String result = "uint16_t image = {" + Environment . NewLine + "\t " ;
237-
238- using ( Bitmap bmp = new Bitmap ( image ) )
239- {
240- int x_step = 0 ;
241- Color pixelColor ;
242-
243- int pixelsTotal = bmp . Width * bmp . Height ;
244- int pixelsCurrent = 0 ;
245-
246- // 8 = size of "0x1234, ", 16 - every 16 lines we add newline and tab
247- StringBuilder str = new StringBuilder ( ( bmp . Width * bmp . Height * 8 ) + ( bmp . Width * bmp . Height / 16 * ( Environment . NewLine . Length + 1 ) ) ) ;
248-
249- for ( int y = 0 ; y < bmp . Height ; y ++ )
250- {
251- for ( int x = 0 ; x < bmp . Width ; x ++ )
252- {
253- pixelColor = bmp . GetPixel ( x , y ) ;
254- // byte = 16 bit per color
255- // RRRR RGGG GGGB BBBB
256- int ColorByte = ( pixelColor . R & 0xF8 ) << 8 |
257- ( pixelColor . G & 0xFC ) << 3 |
258- ( pixelColor . B & 0xF8 ) >> 3 ;
259-
260- str . AppendFormat ( "0x{0:x4}, " , ColorByte ) ;
261- //result += "0x" + ColorByte.ToString("X4") + ", ";
246+ result . AppendFormat ( codeFormat , ColorByte ) ;
262247 pixelsCurrent ++ ;
263248
264- x_step ++ ;
265- if ( x_step == 16 )
249+ rowPos ++ ;
250+ if ( rowPos == 16 )
266251 {
267- x_step = 0 ;
268- //result += Environment.NewLine + "\t";
269- str . Append ( Environment . NewLine + "\t " ) ;
252+ rowPos = 0 ;
253+ result . Append ( Environment . NewLine + "\t " ) ;
270254 }
271255
272256 bgWork . ReportProgress ( ( int ) ( ( double ) pixelsCurrent / ( double ) pixelsTotal * 100 ) ) ;
273257 }
274258 }
275- result += str . ToString ( ) ;
276259 }
277-
278- result += Environment . NewLine + "};" ;
279- return result ;
260+
261+ result . Append ( Environment . NewLine + "};" ) ;
262+ return result . ToString ( ) ;
280263 }
264+
281265 #endregion
282266
283267 private void ImageToCode ( object sender , DoWorkEventArgs e )
@@ -291,11 +275,13 @@ private void ImageToCode(object sender, DoWorkEventArgs e)
291275 e . Result = GenDrawCode_BW1BbppV ( OriginalImage ) ;
292276 break ;
293277 case TransformColorFormats . RGB332 :
294- e . Result = GenDrawCode_RGB332 ( OriginalImage ) ;
295- break ;
296278 case TransformColorFormats . RGB565 :
297- e . Result = GenDrawCode_RGB565 ( OriginalImage ) ;
279+ e . Result = GenCodeFromImage ( ( TransformColorFormats ) e . Argument ) ;
280+ //e.Result = GenDrawCode_RGB332(OriginalImage);
298281 break ;
282+ //case TransformColorFormats.RGB565:
283+ //e.Result = GenDrawCode_RGB565(OriginalImage);
284+ //break;
299285 default :
300286 MessageBox . Show ( "Sorry, unsupported." ) ;
301287 break ;
@@ -405,6 +391,7 @@ private void BgConvertProgress(object sender, ProgressChangedEventArgs e)
405391
406392 private void ConvertDone ( object sender , RunWorkerCompletedEventArgs e )
407393 {
394+ GC . Collect ( ) ;
408395 convertProgress . Value = 100 ;
409396 switch ( tabBox . SelectedIndex )
410397 {
@@ -420,6 +407,7 @@ private void ConvertDone(object sender, RunWorkerCompletedEventArgs e)
420407 break ;
421408 }
422409 btn_Convert . Enabled = true ;
410+ GC . Collect ( ) ;
423411 }
424412
425413 private void Btn_Convert_Click ( object sender , EventArgs e )
@@ -449,15 +437,13 @@ private void Btn_Convert_Click(object sender, EventArgs e)
449437 break ;
450438 }
451439
452- /*
453440 while ( bgWork . IsBusy )
454441 {
455442 convertProgress . Increment ( 1 ) ;
456443 // Keep UI messages moving, so the form remains
457444 // responsive during the asynchronous operation.
458445 Application . DoEvents ( ) ;
459446 }
460- */
461447 }
462448
463449 private void Txt_ZoomMode_LinkClicked ( object sender , LinkLabelLinkClickedEventArgs e )
0 commit comments