@@ -24,11 +24,13 @@ public partial class MainWindow : Window
24
24
{
25
25
WriteableBitmap canvasBitmap ;
26
26
WriteableBitmap gridBitmap ;
27
+ WriteableBitmap outlineBitmap ;
27
28
WriteableBitmap paletteBitmap ;
28
29
Window w ;
29
30
30
31
Image drawingImage ;
31
32
Image gridImage ;
33
+ Image outlineImage ;
32
34
Image paletteImage ;
33
35
34
36
// bitmap settings
@@ -81,6 +83,15 @@ void Start()
81
83
gridImage . Source = gridBitmap ;
82
84
DrawBackgroundGrid ( ) ;
83
85
86
+ // setup outline bitmap
87
+ outlineImage = imgOutline ;
88
+ RenderOptions . SetBitmapScalingMode ( outlineImage , BitmapScalingMode . NearestNeighbor ) ;
89
+ RenderOptions . SetEdgeMode ( outlineImage , EdgeMode . Aliased ) ;
90
+ w = ( MainWindow ) Application . Current . MainWindow ;
91
+ //var gridScaleX = (int)gridImage.Width / canvasResolutionX;
92
+ outlineBitmap = new WriteableBitmap ( canvasResolutionX , canvasResolutionY , dpiX , dpiY , PixelFormats . Bgra32 , null ) ;
93
+ outlineImage . Source = outlineBitmap ;
94
+
84
95
85
96
// build drawing area
86
97
drawingImage = imgCanvas ;
@@ -458,6 +469,11 @@ void DrawingLeftButtonDown(object sender, MouseButtonEventArgs e)
458
469
int x = ( int ) ( e . GetPosition ( drawingImage ) . X / canvasScaleX ) ;
459
470
int y = ( int ) ( e . GetPosition ( drawingImage ) . Y / canvasScaleX ) ;
460
471
DrawPixel ( x , y ) ;
472
+
473
+ if ( chkOutline . IsChecked == true )
474
+ {
475
+ UpdateOutline ( ) ;
476
+ }
461
477
}
462
478
463
479
void DrawingMouseUp ( object sender , MouseButtonEventArgs e )
@@ -486,6 +502,10 @@ void DrawingAreaMouseMoved(object sender, MouseEventArgs e)
486
502
487
503
ShowMousePos ( x , y ) ;
488
504
ShowMousePixelColor ( x , y ) ;
505
+ if ( chkOutline . IsChecked == true )
506
+ {
507
+ UpdateOutline ( ) ;
508
+ }
489
509
}
490
510
491
511
void ShowMousePos ( int x , int y )
@@ -647,11 +667,11 @@ public void CanExecute_Undo(object sender, CanExecuteRoutedEventArgs e)
647
667
648
668
void DrawBackgroundGrid ( )
649
669
{
650
- for ( int x = 0 ; x < 16 ; x ++ )
670
+ PixelColor c = new PixelColor ( ) ;
671
+ for ( int x = 0 ; x < canvasResolutionX ; x ++ )
651
672
{
652
- for ( int y = 0 ; y < 16 ; y ++ )
673
+ for ( int y = 0 ; y < canvasResolutionY ; y ++ )
653
674
{
654
- PixelColor c = new PixelColor ( ) ;
655
675
c . Alpha = gridAlpha ;
656
676
byte v = ( byte ) ( ( ( x % 2 ) == ( y % 2 ) ) ? 255 : 0 ) ;
657
677
c . Red = v ;
@@ -662,5 +682,61 @@ void DrawBackgroundGrid()
662
682
}
663
683
}
664
684
685
+ // draw automatic outlines
686
+ void UpdateOutline ( )
687
+ {
688
+ PixelColor c = new PixelColor ( ) ;
689
+ for ( int x = 0 ; x < canvasResolutionX ; x ++ )
690
+ {
691
+ for ( int y = 0 ; y < canvasResolutionY ; y ++ )
692
+ {
693
+ int centerPix = GetPixelColor ( x , y , canvasBitmap ) . Alpha > 0 ? 1 : 0 ;
694
+
695
+ int yy = ( y + 1 ) > ( canvasResolutionY - 1 ) ? y : y ;
696
+ int upPix = GetPixelColor ( x , yy + 1 , canvasBitmap ) . Alpha > 0 ? 1 : 0 ;
697
+ int xx = ( x + 1 ) > ( canvasResolutionX - 1 ) ? x : x + 1 ;
698
+ int rightPix = GetPixelColor ( xx , y , canvasBitmap ) . Alpha > 0 ? 1 : 0 ;
699
+ yy = ( y - 1 ) < 0 ? y : y - 1 ;
700
+ int downPix = GetPixelColor ( x , yy , canvasBitmap ) . Alpha > 0 ? 1 : 0 ;
701
+ xx = ( x - 1 ) < 0 ? x : x - 1 ;
702
+ int leftPix = GetPixelColor ( xx , y , canvasBitmap ) . Alpha > 0 ? 1 : 0 ;
703
+
704
+ /*
705
+ // decrease count if black color founded
706
+ if (!automaticOutlineForBlack)
707
+ {
708
+ if (upPix > 0) upPix -= canvas.GetPixel(x, y + 1).grayscale == 0 ? 1 : 0;
709
+ if (rightPix > 0) rightPix -= canvas.GetPixel(x + 1, y).grayscale == 0 ? 1 : 0;
710
+ if (downPix > 0) downPix -= canvas.GetPixel(x, y - 1).grayscale == 0 ? 1 : 0;
711
+ if (leftPix > 0) leftPix -= canvas.GetPixel(x - 1, y).grayscale == 0 ? 1 : 0;
712
+ }*/
713
+
714
+ c . Red = 0 ;
715
+ c . Green = 0 ;
716
+ c . Blue = 0 ;
717
+ c . Alpha = 0 ;
718
+
719
+ int neighbourAlphas = upPix + rightPix + downPix + leftPix ;
720
+ if ( neighbourAlphas > 0 )
721
+ {
722
+ if ( centerPix == 0 )
723
+ {
724
+ c . Alpha = 255 ;
725
+ }
726
+ else
727
+ {
728
+ c . Alpha = 0 ;
729
+ }
730
+ }
731
+ else
732
+ {
733
+ c . Alpha = 0 ;
734
+ }
735
+
736
+ SetPixel ( outlineBitmap , x , y , ( int ) c . ColorBGRA ) ;
737
+ }
738
+ }
739
+ }
740
+
665
741
} // class
666
742
} // namespace
0 commit comments