Selaa lähdekoodia

Replace color wip

Krzysztof Krysiński 3 vuotta sitten
vanhempi
commit
c1321def35

+ 9 - 0
PixiEditor/Models/DataHolders/Document/Document.Operations.cs

@@ -7,6 +7,7 @@ using SkiaSharp;
 using System;
 using System.Linq;
 using System.Windows;
+using System.Windows.Media;
 
 namespace PixiEditor.Models.DataHolders
 {
@@ -14,6 +15,14 @@ namespace PixiEditor.Models.DataHolders
     {
         public event EventHandler<DocumentSizeChangedEventArgs> DocumentSizeChanged;
 
+        public void ReplaceColor(SKColor oldColor, SKColor newColor)
+        {
+            foreach (var layer in Layers)
+            {
+                layer.ReplaceColor(oldColor, newColor);
+            }
+        }
+
         /// <summary>
         ///     Resizes canvas to specified width and height to selected anchor.
         /// </summary>

+ 12 - 0
PixiEditor/Models/DataHolders/Surface.cs

@@ -129,6 +129,18 @@ namespace PixiEditor.Models.DataHolders
             SkiaSurface.Canvas.DrawPoint(x, y, drawingPaint);
         }
 
+        public unsafe void SetSRGBPixelUnmanaged(int x, int y, SKColor color)
+        {
+            Half* ptr = (Half*)(surfaceBuffer + (x + y * Width) * 8);
+
+            float normalizedAlpha = color.Alpha / 255.0f;
+
+            ptr[0] = (Half)(color.Red * normalizedAlpha);
+            ptr[1] = (Half)(color.Green * normalizedAlpha);
+            ptr[2] = (Half)(color.Blue * normalizedAlpha);
+            ptr[3] = (Half)(normalizedAlpha);
+        }
+
         public unsafe byte[] ToByteArray(SKColorType colorType = SKColorType.Bgra8888, SKAlphaType alphaType = SKAlphaType.Premul)
         {
             var imageInfo = new SKImageInfo(Width, Height, colorType, alphaType, SKColorSpace.CreateSrgb());

+ 59 - 0
PixiEditor/Models/Layers/Layer.cs

@@ -8,7 +8,12 @@ using System;
 using System.Collections.Generic;
 using System.Diagnostics;
 using System.Linq;
+using System.Runtime.CompilerServices;
+using System.Runtime.InteropServices;
+using System.Threading;
+using System.Threading.Tasks;
 using System.Windows;
+using System.Windows.Media;
 
 namespace PixiEditor.Models.Layers
 {
@@ -31,6 +36,7 @@ namespace PixiEditor.Models.Layers
 
         private string layerHighlightColor = "#666666";
 
+
         public Layer(string name, int maxWidth, int maxHeight)
         {
             Name = name;
@@ -693,5 +699,58 @@ namespace PixiEditor.Models.Layers
             Width = newWidth;
             Height = newHeight;
         }
+
+
+        public void ReplaceColor(SKColor oldColor, SKColor newColor)
+        {
+            if (LayerBitmap == null)
+            {
+                return;
+            }
+
+            Stopwatch sw = new Stopwatch();
+
+            sw.Start();
+
+            int maxThreads = Environment.ProcessorCount;
+            int rowsPerThread = Height / maxThreads;
+
+            Thread[] threads = new Thread[maxThreads];
+
+            for (int i = 0; i < maxThreads; i++)
+            {
+                var i1 = i;
+                threads[i] = new Thread(() =>
+                {
+                    int startRow = i1 * rowsPerThread;
+                    int endRow = (i1 + 1) * rowsPerThread;
+                    if (i1 == maxThreads - 1)
+                    {
+                        endRow = Height;
+                    }
+
+                    for (int y = startRow; y < endRow; y++)
+                    {
+                        for (int x = 0; x < Width; x++)
+                        {
+                            if (LayerBitmap.GetSRGBPixel(x, y) == oldColor)
+                            {
+                                LayerBitmap.SetSRGBPixelUnmanaged(x, y, newColor);
+                            }
+                        }
+                    }
+                });
+
+                threads[i].Start();
+            }
+
+            threads.ToList().ForEach(x => x.Join());
+
+            sw.Stop();
+
+            layerBitmap.SkiaSurface.Canvas.DrawSurface(layerBitmap.SkiaSurface, 0, 0, new SKPaint { BlendMode = SKBlendMode.Dst });
+
+            InvokeLayerBitmapChange();
+        }
     }
 }

+ 1 - 3
PixiEditor/ViewModels/SubViewModels/Main/ColorsViewModel.cs

@@ -148,9 +148,7 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
 
         public void SwapColors(object parameter)
         {
-            var tmp = PrimaryColor;
-            PrimaryColor = SecondaryColor;
-            SecondaryColor = tmp;
+            (PrimaryColor, SecondaryColor) = (SecondaryColor, PrimaryColor);
         }
 
         public void AddSwatch(SKColor color)

+ 4 - 1
PixiEditor/ViewModels/SubViewModels/Main/DebugViewModel.cs

@@ -2,6 +2,8 @@
 using System;
 using System.IO;
 using System.Reflection;
+using System.Windows.Media;
+using SkiaSharp;
 
 namespace PixiEditor.ViewModels.SubViewModels.Main
 {
@@ -21,9 +23,10 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
             CrashCommand = new RelayCommand(_ => throw new InvalidOperationException("Debug Crash"));
         }
 
-        public static void OpenFolder(object parameter)
+        public void OpenFolder(object parameter)
         {
             ProcessHelpers.ShellExecuteEV(parameter as string);
+            Owner.BitmapManager.ActiveDocument.ReplaceColor(SKColors.White, SKColors.Blue);
         }
 
         public static void OpenInstallLocation(object parameter)