Selaa lähdekoodia

Performance and memory improvements + changed to .NET 5

flabbet 4 vuotta sitten
vanhempi
commit
af88df2172

+ 14 - 9
PixiEditor/Models/Controllers/BitmapManager.cs

@@ -1,4 +1,5 @@
 using System;
+using System.Collections.Generic;
 using System.Linq;
 using System.Windows;
 using System.Windows.Input;
@@ -258,11 +259,12 @@ namespace PixiEditor.Models.Controllers
                 return;
             }
 
-            Coordinates[] highlightArea = CoordinatesCalculator.RectangleToCoordinates(
+            IEnumerable<Coordinates> highlightArea = CoordinatesCalculator.RectangleToCoordinates(
                 CoordinatesCalculator.CalculateThicknessCenter(newPosition, ToolSize));
             if (CanChangeHighlightOffset(highlightArea))
             {
-                PreviewLayer.Offset = new Thickness(highlightArea[0].X, highlightArea[0].Y, 0, 0);
+                Coordinates start = highlightArea.First();
+                PreviewLayer.Offset = new Thickness(start.X, start.Y, 0, 0);
             }
             else if (!IsInsideBounds(highlightArea))
             {
@@ -276,17 +278,20 @@ namespace PixiEditor.Models.Controllers
             }
         }
 
-        private bool CanChangeHighlightOffset(Coordinates[] highlightArea)
+        private bool CanChangeHighlightOffset(IEnumerable<Coordinates> highlightArea)
         {
-            return highlightArea.Length > 0 && PreviewLayer != null &&
-                   IsInsideBounds(highlightArea) && highlightArea.Length == PreviewLayer.Width * PreviewLayer.Height;
+            int count = highlightArea.Count();
+            return count > 0 && PreviewLayer != null &&
+                   IsInsideBounds(highlightArea) && count == PreviewLayer.Width * PreviewLayer.Height;
         }
 
-        private bool IsInsideBounds(Coordinates[] highlightArea)
+        private bool IsInsideBounds(IEnumerable<Coordinates> highlightArea)
         {
-            return highlightArea[0].X <= ActiveDocument.Width - 1 &&
-                    highlightArea[0].Y <= ActiveDocument.Height - 1 &&
-                   highlightArea[^1].X >= 0 && highlightArea[^1].Y >= 0;
+            Coordinates start = highlightArea.First();
+            Coordinates end = highlightArea.Last();
+            return start.X <= ActiveDocument.Width - 1 &&
+                    start.Y <= ActiveDocument.Height - 1 &&
+                   end.X >= 0 && end.Y >= 0;
         }
     }
 }

+ 3 - 3
PixiEditor/Models/Position/CoordinatesCalculator.cs

@@ -47,7 +47,7 @@ namespace PixiEditor.Models.Position
         /// <param name="y1">Top left y position.</param>
         /// <param name="x2">Bottom right x position.</param>
         /// <param name="y2">Bottom right Y position.</param>
-        public static Coordinates[] RectangleToCoordinates(int x1, int y1, int x2, int y2)
+        public static IEnumerable<Coordinates> RectangleToCoordinates(int x1, int y1, int x2, int y2)
         {
             x2++;
             y2++;
@@ -60,10 +60,10 @@ namespace PixiEditor.Models.Position
                 }
             }
 
-            return coordinates.ToArray();
+            return coordinates;
         }
 
-        public static Coordinates[] RectangleToCoordinates(DoubleCords coordinates)
+        public static IEnumerable<Coordinates> RectangleToCoordinates(DoubleCords coordinates)
         {
             return RectangleToCoordinates(coordinates.Coords1.X, coordinates.Coords1.Y, coordinates.Coords2.X, coordinates.Coords2.Y);
         }

+ 6 - 2
PixiEditor/Models/Tools/BitmapOperationTool.cs

@@ -10,17 +10,21 @@ namespace PixiEditor.Models.Tools
         public bool RequiresPreviewLayer { get; set; }
 
         public bool UseDefaultUndoMethod { get; set; } = true;
+
+        private LayerChange[] _onlyLayerArr = new LayerChange[] { new LayerChange(BitmapPixelChanges.Empty, 0) };
 
         public abstract LayerChange[] Use(Layer layer, Coordinates[] mouseMove, Color color);
 
         protected LayerChange[] Only(BitmapPixelChanges changes, Layer layer)
         {
-            return new[] { new LayerChange(changes, layer) };
+            _onlyLayerArr[0] = new LayerChange(changes, layer);
+            return _onlyLayerArr;
         }
 
         protected LayerChange[] Only(BitmapPixelChanges changes, int layerIndex)
         {
-            return new[] { new LayerChange(changes, layerIndex) };
+            _onlyLayerArr[0] = new LayerChange(changes, layerIndex);
+            return _onlyLayerArr;
         }
     }
 }

+ 6 - 6
PixiEditor/Models/Tools/Tools/BrightnessTool.cs

@@ -57,31 +57,31 @@ namespace PixiEditor.Models.Tools.Tools
         public BitmapPixelChanges ChangeBrightness(Layer layer, Coordinates coordinates, int toolSize, float correctionFactor)
         {
             DoubleCords centeredCoords = CoordinatesCalculator.CalculateThicknessCenter(coordinates, toolSize);
-            Coordinates[] rectangleCoordinates = CoordinatesCalculator.RectangleToCoordinates(
+            IEnumerable<Coordinates> rectangleCoordinates = CoordinatesCalculator.RectangleToCoordinates(
                 centeredCoords.Coords1.X,
                 centeredCoords.Coords1.Y,
                 centeredCoords.Coords2.X,
                 centeredCoords.Coords2.Y);
             BitmapPixelChanges changes = new BitmapPixelChanges(new Dictionary<Coordinates, Color>());
 
-            for (int i = 0; i < rectangleCoordinates.Length; i++)
+            foreach (Coordinates coordinate in rectangleCoordinates)
             {
                 if (Mode == BrightnessMode.Default)
                 {
-                    if (pixelsVisited.Contains(rectangleCoordinates[i]))
+                    if (pixelsVisited.Contains(coordinate))
                     {
                         continue;
                     }
 
-                    pixelsVisited.Add(rectangleCoordinates[i]);
+                    pixelsVisited.Add(coordinate);
                 }
 
-                Color pixel = layer.GetPixelWithOffset(rectangleCoordinates[i].X, rectangleCoordinates[i].Y);
+                Color pixel = layer.GetPixelWithOffset(coordinate.X, coordinate.Y);
                 Color newColor = ExColor.ChangeColorBrightness(
                     Color.FromArgb(pixel.A, pixel.R, pixel.G, pixel.B),
                     correctionFactor);
                 changes.ChangedPixels.Add(
-                    new Coordinates(rectangleCoordinates[i].X, rectangleCoordinates[i].Y),
+                    new Coordinates(coordinate.X, coordinate.Y),
                     newColor);
             }
 

+ 3 - 2
PixiEditor/Models/Tools/Tools/EraserTool.cs

@@ -9,6 +9,8 @@ namespace PixiEditor.Models.Tools.Tools
 {
     public class EraserTool : BitmapOperationTool
     {
+        private readonly PenTool pen = new PenTool();
+
         public EraserTool()
         {
             Tooltip = "Erasers color from pixel (E)";
@@ -25,9 +27,8 @@ namespace PixiEditor.Models.Tools.Tools
         public LayerChange[] Erase(Layer layer, Coordinates[] coordinates, int toolSize)
         {
             Coordinates startingCords = coordinates.Length > 1 ? coordinates[1] : coordinates[0];
-            PenTool pen = new PenTool();
             BitmapPixelChanges pixels = pen.Draw(startingCords, coordinates[0], System.Windows.Media.Colors.Transparent, toolSize);
-            return new[] { new LayerChange(pixels, layer) };
+            return Only(pixels, layer);
         }
     }
 }

+ 18 - 5
PixiEditor/Models/Tools/Tools/LineTool.cs

@@ -12,10 +12,13 @@ namespace PixiEditor.Models.Tools.Tools
 {
     public class LineTool : ShapeTool
     {
+        private readonly CircleTool circleTool;
+
         public LineTool()
         {
             Tooltip = "Draws line on canvas (L). Hold Shift to draw even line.";
             Toolbar = new BasicToolbar();
+            circleTool = new CircleTool();
         }
 
         public override ToolType ToolType => ToolType.Line;
@@ -34,7 +37,7 @@ namespace PixiEditor.Models.Tools.Tools
 
         public IEnumerable<Coordinates> CreateLine(Coordinates start, Coordinates end, int thickness)
         {
-            return CreateLine(new[] { end, start }, thickness, CapType.Square, CapType.Square);
+            return CreateLineFastest(start, end, thickness);
         }
 
         public IEnumerable<Coordinates> CreateLine(Coordinates start, Coordinates end, int thickness, CapType startCap, CapType endCap)
@@ -54,6 +57,17 @@ namespace PixiEditor.Models.Tools.Tools
             return GetLinePoints(startingCoordinates, latestCoordinates, thickness, startCap, endCap);
         }
 
+        private IEnumerable<Coordinates> CreateLineFastest(Coordinates start, Coordinates end, int thickness)
+        {
+            IEnumerable<Coordinates> line = BresenhamLine(start.X, start.Y, end.X, end.Y);
+            if (thickness == 1)
+            {
+                return line;
+            }
+
+            return GetThickShape(line, thickness);
+        }
+
         private IEnumerable<Coordinates> GetLinePoints(Coordinates start, Coordinates end, int thickness, CapType startCap, CapType endCap)
         {
             IEnumerable<Coordinates> startingCap = GetCapCoordinates(startCap, start, thickness);
@@ -69,7 +83,7 @@ namespace PixiEditor.Models.Tools.Tools
             output.AddRange(GetCapCoordinates(endCap, end, thickness));
             if (line.Count() > 2)
             {
-                output.AddRange(GetThickShape(line.Except(new[] { start, end }).ToArray(), thickness));
+                output.AddRange(GetThickShape(line.Except(new[] { start, end }), thickness));
             }
 
             return output.Distinct();
@@ -96,10 +110,9 @@ namespace PixiEditor.Models.Tools.Tools
         /// <param name="thickness">Thickness of cap.</param>
         private IEnumerable<Coordinates> GetRoundCap(Coordinates position, int thickness)
         {
-            CircleTool circle = new CircleTool();
-            Coordinates[] rectangleCords = CoordinatesCalculator.RectangleToCoordinates(
+            IEnumerable<Coordinates> rectangleCords = CoordinatesCalculator.RectangleToCoordinates(
                 CoordinatesCalculator.CalculateThicknessCenter(position, thickness));
-            return circle.CreateEllipse(rectangleCords[0], rectangleCords[^1], 1, true);
+            return circleTool.CreateEllipse(rectangleCords.First(), rectangleCords.Last(), 1, true);
         }
 
         private IEnumerable<Coordinates> BresenhamLine(int x1, int y1, int x2, int y2)

+ 1 - 1
PixiEditor/PixiEditor.csproj

@@ -2,7 +2,7 @@
 
   <PropertyGroup>
     <OutputType>WinExe</OutputType>
-    <TargetFramework>netcoreapp3.1</TargetFramework>
+    <TargetFramework>net5.0-windows</TargetFramework>
     <UseWPF>true</UseWPF>
     <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
     <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>