Skip to content

Commit

Permalink
Optimize FloodFill
Browse files Browse the repository at this point in the history
  • Loading branch information
brogowski committed Oct 10, 2020
1 parent 5442807 commit 77c83ce
Showing 1 changed file with 23 additions and 27 deletions.
50 changes: 23 additions & 27 deletions PixiEditor/Models/Tools/Tools/FloodFill.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using PixiEditor.Models.DataHolders;
using PixiEditor.Models.Layers;
using PixiEditor.Models.Position;
using PixiEditor.ViewModels;

namespace PixiEditor.Models.Tools.Tools
{
Expand All @@ -23,42 +20,41 @@ public override LayerChange[] Use(Layer layer, Coordinates[] coordinates, Color
return Only(ForestFire(layer, coordinates[0], color), layer);
}

public BitmapPixelChanges ForestFire(Layer layer, Coordinates startingCoords, Color newColor)
private BitmapPixelChanges ForestFire(Layer layer, Coordinates startingCoords, Color newColor)
{
List<Coordinates> changedCoords = new List<Coordinates>();

Layer clone = layer.Clone();
int width = ViewModelMain.Current.BitmapManager.ActiveDocument.Width;
int height = ViewModelMain.Current.BitmapManager.ActiveDocument.Height;
int width = layer.Width;
int height = layer.Height;

var visited = new bool[width, height];

Color colorToReplace = layer.GetPixelWithOffset(startingCoords.X, startingCoords.Y);

var stack = new Stack<Coordinates>();
stack.Push(new Coordinates(startingCoords.X, startingCoords.Y));
using(clone.LayerBitmap.GetBitmapContext(ReadWriteMode.ReadWrite))

while (stack.Count > 0)
{
while (stack.Count > 0)
{
var cords = stack.Pop();
var relativeCords = clone.GetRelativePosition(cords);
var cords = stack.Pop();
var relativeCords = layer.GetRelativePosition(cords);

if (cords.X < 0 || cords.X > width - 1) continue;
if (cords.Y < 0 || cords.Y > height - 1) continue;
if (clone.GetPixel(relativeCords.X, relativeCords.Y) == newColor) continue;
if (cords.X < 0 || cords.X > width - 1) continue;
if (cords.Y < 0 || cords.Y > height - 1) continue;
if (visited[cords.X, cords.Y]) continue;
if (layer.GetPixel(relativeCords.X, relativeCords.Y) == newColor) continue;

if (clone.GetPixel(relativeCords.X, relativeCords.Y) == colorToReplace)
{
changedCoords.Add(new Coordinates(cords.X, cords.Y));
clone.SetPixel(new Coordinates(cords.X, cords.Y), newColor);
stack.Push(new Coordinates(cords.X, cords.Y - 1));
stack.Push(new Coordinates(cords.X + 1, cords.Y));
stack.Push(new Coordinates(cords.X, cords.Y + 1));
stack.Push(new Coordinates(cords.X - 1, cords.Y));
}
if (layer.GetPixel(relativeCords.X, relativeCords.Y) == colorToReplace)
{
changedCoords.Add(new Coordinates(cords.X, cords.Y));
visited[cords.X, cords.Y] = true;
stack.Push(new Coordinates(cords.X, cords.Y - 1));
stack.Push(new Coordinates(cords.X, cords.Y + 1));
stack.Push(new Coordinates(cords.X - 1, cords.Y));
stack.Push(new Coordinates(cords.X + 1, cords.Y));
}

}

return BitmapPixelChanges.FromSingleColoredArray(changedCoords, newColor);
}
}
Expand Down

0 comments on commit 77c83ce

Please sign in to comment.