Browse Source

Merge pull request #73 from brogowski/master

Optimize FloodFill
Krzysztof Krysiński 4 years ago
parent
commit
fd7cce9a9d
1 changed files with 22 additions and 25 deletions
  1. 22 25
      PixiEditor/Models/Tools/Tools/FloodFill.cs

+ 22 - 25
PixiEditor/Models/Tools/Tools/FloodFill.cs

@@ -1,7 +1,5 @@
-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;
@@ -23,43 +21,42 @@ namespace PixiEditor.Models.Tools.Tools
             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;
 
+            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);
         }
     }
-}
+}