Browse Source

Some optimizations

Equbuxu 4 years ago
parent
commit
e1795e63c8
2 changed files with 25 additions and 13 deletions
  1. 4 3
      PixiEditor/Models/Layers/Layer.cs
  2. 21 10
      PixiEditor/Models/Tools/Tools/FloodFill.cs

+ 4 - 3
PixiEditor/Models/Layers/Layer.cs

@@ -29,6 +29,7 @@ namespace PixiEditor.Models.Layers
         private Thickness offset;
 
         private float opacity = 1f;
+        private readonly Color transparent = Color.FromArgb(0, 0, 0, 0);
 
         private string layerHighlightColor = "#666666";
 
@@ -267,8 +268,8 @@ namespace PixiEditor.Models.Layers
         /// <returns>Color of a pixel.</returns>
         public Color GetPixelWithOffset(int x, int y)
         {
-            Coordinates cords = GetRelativePosition(new Coordinates(x, y));
-            return GetPixel(cords.X, cords.Y);
+            //This does not use GetRelativePosition for better performance
+            return GetPixel(x - OffsetX, y - OffsetY);
         }
 
         /// <summary>
@@ -281,7 +282,7 @@ namespace PixiEditor.Models.Layers
         {
             if (x > Width - 1 || x < 0 || y > Height - 1 || y < 0)
             {
-                return Color.FromArgb(0, 0, 0, 0);
+                return transparent;
             }
 
             return LayerBitmap.GetPixel(x, y);

+ 21 - 10
PixiEditor/Models/Tools/Tools/FloodFill.cs

@@ -4,7 +4,8 @@ using PixiEditor.Models.Layers;
 using PixiEditor.Models.Position;
 using System.Collections.Generic;
 using System.Windows.Media;
-
+using System.Windows.Media.Imaging;
+
 namespace PixiEditor.Models.Tools.Tools
 {
     public class FloodFill : BitmapOperationTool
@@ -32,16 +33,23 @@ namespace PixiEditor.Models.Tools.Tools
             Color colorToReplace = layer.GetPixelWithOffset(startingCoords.X, startingCoords.Y);
             int width = BitmapManager.ActiveDocument.Width;
             int height = BitmapManager.ActiveDocument.Height;
-            var visited = new bool[width * height];
+            if (startingCoords.X < 0 || startingCoords.Y < 0 || startingCoords.X >= width || startingCoords.Y >= height)
+                return BitmapPixelChanges.Empty;
+            var visited = new bool[width * height];
+
+            using (BitmapContext ctx = layer.LayerBitmap.GetBitmapContext())
+            {
+                PerformLinearFill(layer, changedCoords, floodFillQueue, startingCoords, width, ref colorToReplace, visited);
+                PerformFloodFIll(layer, changedCoords, floodFillQueue, ref colorToReplace, width, height, visited);
+            }
 
-
-            PerformLinearFill(ref changedCoords, ref floodFillQueue, ref layer, startingCoords, width, ref colorToReplace, ref visited);
-            PerformFloodFIll(ref changedCoords, ref floodFillQueue, ref layer, ref colorToReplace, width, height, ref visited);
-
             return BitmapPixelChanges.FromSingleColoredArray(changedCoords, newColor);
         }
 
-        private void PerformLinearFill(ref List<Coordinates> changedCoords, ref Queue<FloodFillRange> floodFillQueue, ref Layer layer, Coordinates coords, int width, ref Color colorToReplace, ref bool[] visited)
+        private void PerformLinearFill(
+            Layer layer,
+            List<Coordinates> changedCoords, Queue<FloodFillRange> floodFillQueue,
+            Coordinates coords, int width, ref Color colorToReplace, bool[] visited)
         {
             // Find the Left Edge of the Color Area
             int fillXLeft = coords.X;
@@ -83,7 +91,10 @@ namespace PixiEditor.Models.Tools.Tools
             floodFillQueue.Enqueue(range);
         }
 
-        private void PerformFloodFIll(ref List<Coordinates> changedCords, ref Queue<FloodFillRange> floodFillQueue, ref Layer layer, ref Color colorToReplace, int width, int height, ref bool[] pixelsVisited)
+        private void PerformFloodFIll(
+            Layer layer,
+            List<Coordinates> changedCords, Queue<FloodFillRange> floodFillQueue,
+            ref Color colorToReplace, int width, int height, bool[] pixelsVisited)
         {
             while (floodFillQueue.Count > 0)
             {
@@ -99,10 +110,10 @@ namespace PixiEditor.Models.Tools.Tools
                     //START LOOP UPWARDS
                     //if we're not above the top of the bitmap and the pixel above this one is within the color tolerance
                     if (range.Y > 0 && (!pixelsVisited[upPixelIndex]) && layer.GetPixelWithOffset(i, upY) == colorToReplace)
-                        PerformLinearFill(ref changedCords, ref floodFillQueue, ref layer, new Coordinates(i, upY), width, ref colorToReplace, ref pixelsVisited);
+                        PerformLinearFill(layer, changedCords, floodFillQueue, new Coordinates(i, upY), width, ref colorToReplace, pixelsVisited);
                     //START LOOP DOWNWARDS
                     if (range.Y < (height - 1) && (!pixelsVisited[downPixelxIndex]) && layer.GetPixelWithOffset(i, downY) == colorToReplace)
-                        PerformLinearFill(ref changedCords, ref floodFillQueue, ref layer, new Coordinates(i, downY), width, ref colorToReplace, ref pixelsVisited);
+                        PerformLinearFill(layer, changedCords, floodFillQueue, new Coordinates(i, downY), width, ref colorToReplace, pixelsVisited);
                     downPixelxIndex++;
                     upPixelIndex++;
                 }