@@ -35,6 +35,9 @@ public Form1()
3535 //selBox_Format.DataSource = Enum.GetValues(typeof(PixelFormat));
3636 selBox_Format . DataSource = Enum . GetValues ( typeof ( TransformColorFormats ) ) ;
3737 selBox_Format . SelectedIndex = 0 ;
38+
39+ num_Width . Enabled = false ;
40+ num_Height . Enabled = false ;
3841 }
3942
4043 private void Form1_Load ( object sender , EventArgs e )
@@ -300,7 +303,7 @@ private void ImageToCode(object sender, DoWorkEventArgs e)
300303 }
301304
302305 #region Code2Image
303- private Image GenImageFromCode_RGB332 ( )
306+ private Image GenImageFromCode ( TransformColorFormats format )
304307 {
305308 string Code = "" ;
306309 int Width = 0 , Height = 0 ;
@@ -321,10 +324,45 @@ private Image GenImageFromCode_RGB332()
321324 // Two ways re symbol-by-symbol, or use regex. What faster?
322325 // Read from symbos "{", ignore tabs and spaces, read to "}". Use "," as separator.
323326 // Use regex and search pattern "0x([0-9a-fA-F]{2}),"
324- foreach ( Match m in Regex . Matches ( Code , @"0x([0-9a-fA-F]{2})," ) )
327+ string regex = "" ;
328+ switch ( format )
329+ {
330+ case TransformColorFormats . BW1BppH :
331+ case TransformColorFormats . BW1BppV :
332+ case TransformColorFormats . RGB332 :
333+ regex = @"0x([0-9a-fA-F]{2})," ;
334+ break ;
335+ case TransformColorFormats . RGB565 :
336+ regex = @"0x([0-9a-fA-F]{4})," ;
337+ break ;
338+ }
339+
340+ foreach ( Match m in Regex . Matches ( Code , regex ) )
325341 {
326342 pixelColor = int . Parse ( m . Groups [ 1 ] . Value , System . Globalization . NumberStyles . HexNumber ) ;
327- result . SetPixel ( x , y , Color . FromArgb ( ( pixelColor & 0xE0 ) , ( pixelColor & 0x1C ) << 3 , ( pixelColor & 0x3 ) << 6 ) ) ;
343+ switch ( format )
344+ {
345+ //case TransformColorFormats.BW1BppH:
346+ // break;
347+ //case TransformColorFormats.BW1BppV:
348+ // break;
349+ case TransformColorFormats . RGB332 : // RRRG GGBB
350+ result . SetPixel ( x , y , Color . FromArgb (
351+ ( pixelColor & 0xE0 ) ,
352+ ( pixelColor & 0x1C ) << 3 ,
353+ ( pixelColor & 0x3 ) << 6 )
354+ ) ;
355+ break ;
356+ case TransformColorFormats . RGB565 : // RRRR RGGG GGGB BBBB
357+ result . SetPixel ( x , y , Color . FromArgb (
358+ ( pixelColor & 0xF800 ) >> 8 ,
359+ ( pixelColor & 0x7E0 ) >> 3 ,
360+ ( pixelColor & 0x1F ) << 3 )
361+ ) ;
362+ break ;
363+ default :
364+ break ;
365+ }
328366
329367 x ++ ;
330368 if ( x == Width )
@@ -349,8 +387,9 @@ private void CodeToImage(object sender, DoWorkEventArgs e)
349387 {
350388 switch ( e . Argument )
351389 {
390+ case TransformColorFormats . RGB565 :
352391 case TransformColorFormats . RGB332 :
353- e . Result = GenImageFromCode_RGB332 ( ) ;
392+ e . Result = GenImageFromCode ( ( TransformColorFormats ) e . Argument ) ;
354393 break ;
355394 default :
356395 MessageBox . Show ( "Sorry, unsupported." ) ;
@@ -425,11 +464,24 @@ private void Txt_ZoomMode_LinkClicked(object sender, LinkLabelLinkClickedEventAr
425464 {
426465 int mode = ( int ) imageBox . SizeMode ;
427466 mode ++ ;
428- if ( mode == sizeof ( PictureBoxSizeMode ) )
467+ if ( mode == sizeof ( PictureBoxSizeMode ) + 1 )
429468 mode = 0 ;
430469
431470 txt_ZoomMode . Text = "Zoom mode: " + Enum . GetName ( typeof ( PictureBoxSizeMode ) , ( PictureBoxSizeMode ) mode ) ;
432471 imageBox . SizeMode = ( PictureBoxSizeMode ) mode ;
433472 }
473+
474+ private void tabBox_SelectedIndexChanged ( object sender , EventArgs e )
475+ {
476+ if ( tabBox . SelectedIndex == 1 ) // Show size boxes only on "Code" tab
477+ {
478+ num_Width . Enabled = true ;
479+ num_Height . Enabled = true ;
480+ } else
481+ {
482+ num_Width . Enabled = false ;
483+ num_Height . Enabled = false ;
484+ }
485+ }
434486 }
435487}
0 commit comments