Skip to content

Commit 99fec66

Browse files
committed
add automatic outline option
1 parent bbc14dc commit 99fec66

File tree

2 files changed

+83
-3
lines changed

2 files changed

+83
-3
lines changed

PixelArtTool/MainWindow.xaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,11 @@
8686
<Border BorderBrush="Black" BorderThickness="1" HorizontalAlignment="Left" Height="256" Margin="89,50,0,0" VerticalAlignment="Top" Width="256">
8787
<Image x:Name="imgCanvas" HorizontalAlignment="Left" Height="256" Margin="-1" VerticalAlignment="Top" Width="256" Stretch="Fill"/>
8888
</Border>
89+
<Border BorderBrush="Black" BorderThickness="1" HorizontalAlignment="Left" Height="256" Margin="89,50,0,0" VerticalAlignment="Top" Width="256">
90+
<Image x:Name="imgOutline" IsHitTestVisible="False" HorizontalAlignment="Left" Height="256" Margin="-1" VerticalAlignment="Top" Width="256" Stretch="Fill" IsEnabled="False"/>
91+
</Border>
8992
</Grid>
93+
<CheckBox x:Name="chkOutline" Content="Outline" HorizontalAlignment="Left" Margin="648,50,0,0" VerticalAlignment="Top" Width="64"/>
9094
<Rectangle x:Name="rectCurrentColor" Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="24" Margin="10,181,0,0" Stroke="Black" VerticalAlignment="Top" Width="24"/>
9195
</Grid>
9296
</Window>

PixelArtTool/MainWindow.xaml.cs

Lines changed: 79 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,13 @@ public partial class MainWindow : Window
2424
{
2525
WriteableBitmap canvasBitmap;
2626
WriteableBitmap gridBitmap;
27+
WriteableBitmap outlineBitmap;
2728
WriteableBitmap paletteBitmap;
2829
Window w;
2930

3031
Image drawingImage;
3132
Image gridImage;
33+
Image outlineImage;
3234
Image paletteImage;
3335

3436
// bitmap settings
@@ -81,6 +83,15 @@ void Start()
8183
gridImage.Source = gridBitmap;
8284
DrawBackgroundGrid();
8385

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+
8495

8596
// build drawing area
8697
drawingImage = imgCanvas;
@@ -458,6 +469,11 @@ void DrawingLeftButtonDown(object sender, MouseButtonEventArgs e)
458469
int x = (int)(e.GetPosition(drawingImage).X / canvasScaleX);
459470
int y = (int)(e.GetPosition(drawingImage).Y / canvasScaleX);
460471
DrawPixel(x, y);
472+
473+
if (chkOutline.IsChecked == true)
474+
{
475+
UpdateOutline();
476+
}
461477
}
462478

463479
void DrawingMouseUp(object sender, MouseButtonEventArgs e)
@@ -486,6 +502,10 @@ void DrawingAreaMouseMoved(object sender, MouseEventArgs e)
486502

487503
ShowMousePos(x, y);
488504
ShowMousePixelColor(x, y);
505+
if (chkOutline.IsChecked == true)
506+
{
507+
UpdateOutline();
508+
}
489509
}
490510

491511
void ShowMousePos(int x, int y)
@@ -647,11 +667,11 @@ public void CanExecute_Undo(object sender, CanExecuteRoutedEventArgs e)
647667

648668
void DrawBackgroundGrid()
649669
{
650-
for (int x = 0; x < 16; x++)
670+
PixelColor c = new PixelColor();
671+
for (int x = 0; x < canvasResolutionX; x++)
651672
{
652-
for (int y = 0; y < 16; y++)
673+
for (int y = 0; y < canvasResolutionY; y++)
653674
{
654-
PixelColor c = new PixelColor();
655675
c.Alpha = gridAlpha;
656676
byte v = (byte)(((x % 2) == (y % 2)) ? 255 : 0);
657677
c.Red = v;
@@ -662,5 +682,61 @@ void DrawBackgroundGrid()
662682
}
663683
}
664684

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+
665741
} // class
666742
} // namespace

0 commit comments

Comments
 (0)