Browse Source

I can smell the merge conflicts

Equbuxu 4 years ago
parent
commit
83f9a10144

+ 10 - 10
PixiEditor/Models/Colors/ExColor.cs

@@ -1,5 +1,5 @@
-using System;
-using System.Windows.Media;
+using SkiaSharp;
+using System;
 
 namespace PixiEditor.Models.Colors
 {
@@ -14,19 +14,19 @@ namespace PixiEditor.Models.Colors
         ///     Negative values produce darker colors.
         /// </param>
         /// <returns>
-        ///     Corrected <see cref="Color" /> structure.
+        ///     Corrected <see cref="SKColor" /> structure.
         /// </returns>
-        public static Color ChangeColorBrightness(Color color, float correctionFactor)
+        public static SKColor ChangeColorBrightness(SKColor color, float correctionFactor)
         {
-            Tuple<int, float, float> hsl = RgbToHsl(color.R, color.G, color.B);
+            Tuple<int, float, float> hsl = RgbToHsl(color.Red, color.Green, color.Blue);
             int h = hsl.Item1;
             float s = hsl.Item2;
             float l = hsl.Item3;
 
             l = Math.Clamp(l + correctionFactor, 0, 100);
-            Color rgb = HslToRgb(h, s, l);
+            SKColor rgb = HslToRgb(h, s, l);
 
-            return Color.FromArgb(color.A, rgb.R, rgb.G, rgb.B);
+            return new SKColor(rgb.Red, rgb.Green, rgb.Blue, color.Alpha);
         }
 
         /// <summary>
@@ -94,7 +94,7 @@ namespace PixiEditor.Models.Colors
         ///     Converts HSL color format to RGB.
         /// </summary>
         /// <returns>RGB Color.</returns>
-        public static Color HslToRgb(int h, float s, float l)
+        public static SKColor HslToRgb(int h, float s, float l)
         {
             s /= 100;
             l /= 100;
@@ -119,7 +119,7 @@ namespace PixiEditor.Models.Colors
                 b = (byte)(255 * HueToRgb(v1, v2, hue - (1.0f / 3)));
             }
 
-            return Color.FromRgb(r, g, b);
+            return new SKColor(r, g, b);
         }
 
         private static float HueToRgb(float v1, float v2, float hue)
@@ -152,4 +152,4 @@ namespace PixiEditor.Models.Colors
             return v1;
         }
     }
-}
+}

+ 3 - 3
PixiEditor/Models/Controllers/BitmapManager.cs

@@ -6,6 +6,7 @@ using PixiEditor.Models.Position;
 using PixiEditor.Models.Tools;
 using PixiEditor.Models.Tools.Tools;
 using PixiEditor.Models.Tools.ToolSettings.Settings;
+using SkiaSharp;
 using System;
 using System.Collections.Generic;
 using System.Collections.ObjectModel;
@@ -13,7 +14,6 @@ using System.Diagnostics;
 using System.Linq;
 using System.Windows;
 using System.Windows.Input;
-using System.Windows.Media;
 
 namespace PixiEditor.Models.Controllers
 {
@@ -57,7 +57,7 @@ namespace PixiEditor.Models.Controllers
 
         public Layer ActiveLayer => ActiveDocument.ActiveLayer;
 
-        public Color PrimaryColor { get; set; }
+        public SKColor PrimaryColor { get; set; }
 
         public int ToolSize
         {
@@ -231,7 +231,7 @@ namespace PixiEditor.Models.Controllers
             {
                 ActiveDocument.GeneratePreviewLayer();
                 ActiveDocument.PreviewLayer.SetPixels(
-                    BitmapPixelChanges.FromSingleColoredArray(highlightArea, Color.FromArgb(77, 0, 0, 0)));
+                    BitmapPixelChanges.FromSingleColoredArray(highlightArea, new SKColor(0, 0, 0, 77)));
             }
         }
 

+ 6 - 6
PixiEditor/Models/Controllers/BitmapOperationsUtility.cs

@@ -6,11 +6,11 @@ using PixiEditor.Models.Position;
 using PixiEditor.Models.Tools;
 using PixiEditor.Models.Tools.ToolSettings.Settings;
 using PixiEditor.Models.Undo;
+using SkiaSharp;
 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Windows.Input;
-using System.Windows.Media;
 
 namespace PixiEditor.Models.Controllers
 {
@@ -40,8 +40,8 @@ namespace PixiEditor.Models.Controllers
                 return;
             }
 
-            BitmapPixelChanges changes = BitmapPixelChanges.FromSingleColoredArray(pixels, Color.FromArgb(0, 0, 0, 0));
-            Dictionary<Guid, Color[]> oldValues = BitmapUtils.GetPixelsForSelection(layers, pixels);
+            BitmapPixelChanges changes = BitmapPixelChanges.FromSingleColoredArray(pixels, SKColors.Empty);
+            Dictionary<Guid, SKColor[]> oldValues = BitmapUtils.GetPixelsForSelection(layers, pixels);
             LayerChange[] old = new LayerChange[layers.Length];
             LayerChange[] newChange = new LayerChange[layers.Length];
             for (int i = 0; i < layers.Length; i++)
@@ -115,7 +115,7 @@ namespace PixiEditor.Models.Controllers
             previewLayerChanges = null;
         }
 
-        private void UseTool(List<Coordinates> mouseMoveCords, BitmapOperationTool tool, Color color)
+        private void UseTool(List<Coordinates> mouseMoveCords, BitmapOperationTool tool, SKColor color)
         {
             if (sizeSetting == null)
             {
@@ -238,7 +238,7 @@ namespace PixiEditor.Models.Controllers
 
         private BitmapPixelChanges GetOldPixelsValues(Coordinates[] coordinates)
         {
-            Dictionary<Coordinates, Color> values = new Dictionary<Coordinates, Color>();
+            Dictionary<Coordinates, SKColor> values = new Dictionary<Coordinates, SKColor>();
             //using (Manager.ActiveLayer.LayerBitmap.GetBitmapContext(ReadWriteMode.ReadOnly))
             {
                 Coordinates[] relativeCoords = Manager.ActiveLayer.ConvertToRelativeCoordinates(coordinates);
@@ -247,7 +247,7 @@ namespace PixiEditor.Models.Controllers
                     var cl = Manager.ActiveLayer.GetPixel(relativeCoords[i].X, relativeCoords[i].Y);
                     values.Add(
                         coordinates[i],
-                        Color.FromArgb(cl.Alpha, cl.Red, cl.Green, cl.Blue));
+                        cl);
                 }
             }
 

+ 10 - 8
PixiEditor/Models/Controllers/ClipboardController.cs

@@ -1,4 +1,5 @@
-using PixiEditor.Models.ImageManipulation;
+using PixiEditor.Models.DataHolders;
+using PixiEditor.Models.ImageManipulation;
 using PixiEditor.Models.Layers;
 using PixiEditor.Models.Position;
 using PixiEditor.Models.Undo;
@@ -19,7 +20,8 @@ namespace PixiEditor.Models.Controllers
         public static void CopyToClipboard(Layer[] layers, Coordinates[] selection, int originalImageWidth, int originalImageHeight)
         {
             Clipboard.Clear();
-            WriteableBitmap combinedBitmaps = BitmapUtils.CombineLayers(originalImageWidth, originalImageHeight, layers);
+            using var tempSurface = BitmapUtils.CombineLayers(originalImageWidth, originalImageHeight, layers);
+            WriteableBitmap combinedBitmaps = tempSurface.ToWriteableBitmap();
             using (MemoryStream pngStream = new MemoryStream())
             {
                 DataObject data = new DataObject();
@@ -41,7 +43,7 @@ namespace PixiEditor.Models.Controllers
         /// </summary>
         public static void PasteFromClipboard()
         {
-            WriteableBitmap image = GetImageFromClipboard();
+            Surface image = GetImageFromClipboard();
             if (image != null)
             {
                 AddImageToLayers(image);
@@ -56,7 +58,7 @@ namespace PixiEditor.Models.Controllers
         ///     Gets image from clipboard, supported PNG, Dib and Bitmap.
         /// </summary>
         /// <returns>WriteableBitmap.</returns>
-        public static WriteableBitmap GetImageFromClipboard()
+        public static Surface GetImageFromClipboard()
         {
             DataObject dao = (DataObject)Clipboard.GetDataObject();
             WriteableBitmap finalImage = null;
@@ -80,7 +82,7 @@ namespace PixiEditor.Models.Controllers
                 finalImage = new WriteableBitmap((dao.GetData(DataFormats.Bitmap) as BitmapSource)!);
             }
 
-            return finalImage;
+            return new Surface(finalImage);
         }
 
         public static bool IsImageInClipboard()
@@ -116,15 +118,15 @@ namespace PixiEditor.Models.Controllers
 
         private static void AddLayerProcess(object[] parameters)
         {
-            if (parameters.Length == 0 || !(parameters[0] is WriteableBitmap))
+            if (parameters.Length == 0 || !(parameters[0] is Surface))
             {
                 return;
             }
 
-            AddImageToLayers((WriteableBitmap)parameters[0]);
+            AddImageToLayers((Surface)parameters[0]);
         }
 
-        private static void AddImageToLayers(WriteableBitmap image)
+        private static void AddImageToLayers(Surface image)
         {
             ViewModelMain.Current.BitmapManager.ActiveDocument.AddNewLayer("Image", image);
         }

+ 15 - 19
PixiEditor/Models/DataHolders/BitmapPixelChanges.cs

@@ -1,38 +1,34 @@
-using System;
-using System.Collections.Generic;
-using System.Collections.ObjectModel;
-using System.Diagnostics;
-using System.Linq;
-using System.Windows.Media;
-using System.Windows.Media.Imaging;
-using PixiEditor.Exceptions;
+using PixiEditor.Exceptions;
 using PixiEditor.Helpers.Extensions;
-using PixiEditor.Models.Layers;
 using PixiEditor.Models.Position;
+using SkiaSharp;
+using System;
+using System.Collections.Generic;
+using System.Linq;
 
 namespace PixiEditor.Models.DataHolders
 {
     public struct BitmapPixelChanges
     {
-        public BitmapPixelChanges(Dictionary<Coordinates, Color> changedPixels)
+        public BitmapPixelChanges(Dictionary<Coordinates, SKColor> changedPixels)
         {
             ChangedPixels = changedPixels;
             WasBuiltAsSingleColored = false;
         }
 
-        public static BitmapPixelChanges Empty => new BitmapPixelChanges(new Dictionary<Coordinates, Color>());
+        public static BitmapPixelChanges Empty => new BitmapPixelChanges(new Dictionary<Coordinates, SKColor>());
 
         public bool WasBuiltAsSingleColored { get; private set; }
 
-        public Dictionary<Coordinates, Color> ChangedPixels { get; set; }
+        public Dictionary<Coordinates, SKColor> ChangedPixels { get; set; }
 
         /// <summary>
         ///     Builds BitmapPixelChanges with only one color for specified coordinates.
         /// </summary>
         /// <returns>Single-colored BitmapPixelChanges.</returns>
-        public static BitmapPixelChanges FromSingleColoredArray(IEnumerable<Coordinates> coordinates, Color color)
+        public static BitmapPixelChanges FromSingleColoredArray(IEnumerable<Coordinates> coordinates, SKColor color)
         {
-            Dictionary<Coordinates, Color> dict = new Dictionary<Coordinates, Color>();
+            Dictionary<Coordinates, SKColor> dict = new Dictionary<Coordinates, SKColor>();
             foreach (Coordinates coordinate in coordinates)
             {
                 if (dict.ContainsKey(coordinate))
@@ -76,16 +72,16 @@ namespace PixiEditor.Models.DataHolders
         /// <summary>
         ///     Builds BitmapPixelChanges using 2 same-length enumerables of coordinates and colors.
         /// </summary>
-        public static BitmapPixelChanges FromArrays(IEnumerable<Coordinates> coordinates, IEnumerable<Color> color)
+        public static BitmapPixelChanges FromArrays(IEnumerable<Coordinates> coordinates, IEnumerable<SKColor> color)
         {
             Coordinates[] coordinateArray = coordinates.ToArray();
-            Color[] colorArray = color.ToArray();
+            SKColor[] colorArray = color.ToArray();
             if (coordinateArray.Length != colorArray.Length)
             {
                 throw new ArrayLengthMismatchException();
             }
 
-            Dictionary<Coordinates, Color> dict = new Dictionary<Coordinates, Color>();
+            Dictionary<Coordinates, SKColor> dict = new Dictionary<Coordinates, SKColor>();
             for (int i = 0; i < coordinateArray.Length; i++)
             {
                 dict.Add(coordinateArray[i], colorArray[i]);
@@ -96,7 +92,7 @@ namespace PixiEditor.Models.DataHolders
 
         public BitmapPixelChanges WithoutTransparentPixels()
         {
-            return new BitmapPixelChanges(ChangedPixels.Where(x => x.Value.A > 0).ToDictionary(y => y.Key, y => y.Value));
+            return new BitmapPixelChanges(ChangedPixels.Where(x => x.Value.Alpha > 0).ToDictionary(y => y.Key, y => y.Value));
         }
     }
-}
+}

+ 6 - 8
PixiEditor/Models/DataHolders/RecentlyOpenedDocument.cs

@@ -1,12 +1,10 @@
-using System.Diagnostics;
-using System.IO;
-using System.Linq;
-using System.Windows.Media.Imaging;
-using PixiEditor.Helpers;
+using PixiEditor.Helpers;
 using PixiEditor.Models.ImageManipulation;
 using PixiEditor.Models.IO;
-using PixiEditor.Models.Layers;
 using PixiEditor.Parser;
+using System.Diagnostics;
+using System.IO;
+using System.Windows.Media.Imaging;
 
 namespace PixiEditor.Models.DataHolders
 {
@@ -95,7 +93,7 @@ namespace PixiEditor.Models.DataHolders
 
                 try
                 {
-                    bitmap = Importer.ImportImage(FilePath);
+                    bitmap = Importer.ImportWriteableBitmap(FilePath);
                 }
                 catch
                 {
@@ -108,4 +106,4 @@ namespace PixiEditor.Models.DataHolders
             return null;
         }
     }
-}
+}

+ 34 - 17
PixiEditor/Models/DataHolders/Surface.cs

@@ -37,23 +37,21 @@ namespace PixiEditor.Models.DataHolders
 
         public Surface(int w, int h, byte[] pbgra32Bytes)
         {
-            SKImageInfo info = new SKImageInfo(w, h, SKColorType.Bgra8888, SKAlphaType.Premul);
-            var ptr = Marshal.AllocHGlobal(pbgra32Bytes.Length);
-            try
-            {
-                Marshal.Copy(pbgra32Bytes, 0, ptr, pbgra32Bytes.Length);
-                SKPixmap map = new(info, ptr);
-                SKSurface surface = SKSurface.Create(map);
-                var newSurface = CreateSurface(w, h);
-                surface.Draw(newSurface.Canvas, 0, 0, ReplacingPaint);
-                SkiaSurface = newSurface;
-                Width = w;
-                Height = h;
-            }
-            finally
-            {
-                Marshal.FreeHGlobal(ptr);
-            }
+            Width = w;
+            Height = h;
+            SkiaSurface = Pbgra32BytesToSkSurface(w, h, pbgra32Bytes);
+        }
+
+        public Surface(WriteableBitmap original)
+        {
+            if (original.Format != PixelFormats.Pbgra32)
+                throw new ArgumentException("This method only supports Pbgra32 bitmaps");
+            byte[] pixels = new byte[original.PixelWidth * original.PixelHeight * 4];
+            original.CopyPixels(pixels, original.PixelWidth * 4, 0);
+
+            Width = original.PixelWidth;
+            Height = original.PixelHeight;
+            SkiaSurface = Pbgra32BytesToSkSurface(Width, Height, pixels);
         }
 
         public Surface ResizeNearestNeighbor(int newW, int newH)
@@ -138,6 +136,25 @@ namespace PixiEditor.Models.DataHolders
             SkiaSurface.Dispose();
         }
 
+        private static SKSurface Pbgra32BytesToSkSurface(int w, int h, byte[] pbgra32Bytes)
+        {
+            SKImageInfo info = new SKImageInfo(w, h, SKColorType.Bgra8888, SKAlphaType.Premul);
+            var ptr = Marshal.AllocHGlobal(pbgra32Bytes.Length);
+            try
+            {
+                Marshal.Copy(pbgra32Bytes, 0, ptr, pbgra32Bytes.Length);
+                using SKPixmap map = new(info, ptr);
+                using SKSurface surface = SKSurface.Create(map);
+                var newSurface = CreateSurface(w, h);
+                surface.Draw(newSurface.Canvas, 0, 0, ReplacingPaint);
+                return newSurface;
+            }
+            finally
+            {
+                Marshal.FreeHGlobal(ptr);
+            }
+        }
+
         private static SKSurface CreateSurface(int w, int h)
         {
             return SKSurface.Create(new SKImageInfo(w, h, SKColorType.RgbaF16, SKAlphaType.Premul, SKColorSpace.CreateSrgb()));

+ 22 - 22
PixiEditor/Models/IO/Exporter.cs

@@ -71,6 +71,28 @@ namespace PixiEditor.Models.IO
             }
         }
 
+        public static void SaveAsGZippedBytes(string path, Surface surface)
+        {
+            var imageInfo = new SKImageInfo(surface.Width, surface.Height, SKColorType.RgbaF16);
+            var unmanagedBuffer = Marshal.AllocHGlobal(surface.Width * surface.Height * 8);
+            //+8 bytes for width and height
+            var bytes = new byte[surface.Width * surface.Height * 8 + 8];
+            try
+            {
+                surface.SkiaSurface.ReadPixels(imageInfo, unmanagedBuffer, surface.Width * 8, 0, 0);
+                Marshal.Copy(unmanagedBuffer, bytes, 8, surface.Width * surface.Height * 8);
+            }
+            finally
+            {
+                Marshal.FreeHGlobal(unmanagedBuffer);
+            }
+            BitConverter.GetBytes((int)surface.Width).CopyTo(bytes, 0);
+            BitConverter.GetBytes((int)surface.Height).CopyTo(bytes, 4);
+            using FileStream outputStream = new(path, FileMode.Create);
+            using GZipStream compressedStream = new GZipStream(outputStream, CompressionLevel.Fastest);
+            compressedStream.Write(bytes);
+        }
+
         /// <summary>
         ///     Saves image to PNG file.
         /// </summary>
@@ -95,27 +117,5 @@ namespace PixiEditor.Models.IO
                 MessageBox.Show(err.ToString(), "Error", MessageBoxButton.OK, MessageBoxImage.Error);
             }
         }
-
-        public static void SaveAsGZippedBytes(string path, Surface surface)
-        {
-            var imageInfo = new SKImageInfo(surface.Width, surface.Height, SKColorType.RgbaF16);
-            var unmanagedBuffer = Marshal.AllocHGlobal(surface.Width * surface.Height * 8);
-            //+8 bytes for width and height
-            var bytes = new byte[surface.Width * surface.Height * 8 + 8];
-            try
-            {
-                surface.SkiaSurface.ReadPixels(imageInfo, unmanagedBuffer, surface.Width * 8, 0, 0);
-                Marshal.Copy(unmanagedBuffer, bytes, 8, surface.Width * surface.Height * 8);
-            }
-            finally
-            {
-                Marshal.FreeHGlobal(unmanagedBuffer);
-            }
-            BitConverter.GetBytes((int)surface.Width).CopyTo(bytes, 0);
-            BitConverter.GetBytes((int)surface.Height).CopyTo(bytes, 4);
-            using FileStream outputStream = new(path, FileMode.Create);
-            using GZipStream compressedStream = new GZipStream(outputStream, CompressionLevel.Fastest);
-            compressedStream.Write(bytes);
-        }
     }
 }

+ 21 - 2
PixiEditor/Models/IO/Importer.cs

@@ -23,7 +23,7 @@ namespace PixiEditor.Models.IO
         /// <returns>WriteableBitmap of imported image.</returns>
         public static Surface ImportImage(string path, int width, int height)
         {
-            Surface wbmp = ImportImage(path);
+            Surface wbmp = ImportSurface(path);
             if (wbmp.Width != width || wbmp.Height != height)
             {
                 var resized = wbmp.ResizeNearestNeighbor(width, height);
@@ -38,7 +38,26 @@ namespace PixiEditor.Models.IO
         ///     Imports image from path and resizes it to given dimensions.
         /// </summary>
         /// <param name="path">Path of image.</param>
-        public static Surface ImportImage(string path)
+        public static Surface ImportSurface(string path)
+        {
+            try
+            {
+                using var image = SKImage.FromEncodedData(path);
+                Surface surface = new Surface(image.Width, image.Height);
+                surface.SkiaSurface.Canvas.DrawImage(image, new SKPoint(0, 0));
+                return surface;
+            }
+            catch (NotSupportedException)
+            {
+                throw new CorruptedFileException();
+            }
+            catch (FileFormatException)
+            {
+                throw new CorruptedFileException();
+            }
+        }
+
+        public static WriteableBitmap ImportWriteableBitmap(string path)
         {
             try
             {

+ 8 - 10
PixiEditor/Models/Layers/Layer.cs

@@ -8,7 +8,6 @@ using System.Collections.Generic;
 using System.Diagnostics;
 using System.Linq;
 using System.Windows;
-using System.Windows.Media;
 
 namespace PixiEditor.Models.Layers
 {
@@ -16,7 +15,6 @@ namespace PixiEditor.Models.Layers
     public class Layer : BasicLayer
     {
         private const int SizeOfArgb = 4;
-        private readonly Color transparent = Color.FromArgb(0, 0, 0, 0);
         private bool clipRequested;
 
         private bool isActive;
@@ -60,7 +58,7 @@ namespace PixiEditor.Models.Layers
             LayerGuid = Guid.NewGuid();
         }
 
-        public Dictionary<Coordinates, Color> LastRelativeCoordinates { get; set; }
+        public Dictionary<Coordinates, SKColor> LastRelativeCoordinates { get; set; }
 
         public string LayerHighlightColor
         {
@@ -303,7 +301,7 @@ namespace PixiEditor.Models.Layers
         /// <param name="color">Color of pixel.</param>
         /// <param name="dynamicResize">Resizes bitmap to fit content.</param>
         /// <param name="applyOffset">Converts pixels coordinates to relative to bitmap.</param>
-        public void SetPixel(Coordinates coordinates, Color color, bool dynamicResize = true, bool applyOffset = true)
+        public void SetPixel(Coordinates coordinates, SKColor color, bool dynamicResize = true, bool applyOffset = true)
         {
             SetPixels(BitmapPixelChanges.FromSingleColoredArray(new[] { coordinates }, color), dynamicResize, applyOffset);
         }
@@ -333,14 +331,14 @@ namespace PixiEditor.Models.Layers
 
             LastRelativeCoordinates = pixels.ChangedPixels;
 
-            foreach (KeyValuePair<Coordinates, Color> coords in pixels.ChangedPixels)
+            foreach (KeyValuePair<Coordinates, SKColor> coords in pixels.ChangedPixels)
             {
                 if (OutOfBounds(coords.Key))
                 {
                     continue;
                 }
 
-                LayerBitmap.SetSRGBPixel(coords.Key.X, coords.Key.Y, new SKColor(coords.Value.R, coords.Value.G, coords.Value.B, coords.Value.A));
+                LayerBitmap.SetSRGBPixel(coords.Key.X, coords.Key.Y, coords.Value);
             }
 
             ClipIfNecessary();
@@ -379,7 +377,7 @@ namespace PixiEditor.Models.Layers
             int newMinX = minMaxCords.Coords1.X - OffsetX;
             int newMinY = minMaxCords.Coords1.Y - OffsetY;
 
-            if (!(pixels.WasBuiltAsSingleColored && pixels.ChangedPixels.First().Value.A == 0))
+            if (!(pixels.WasBuiltAsSingleColored && pixels.ChangedPixels.First().Value.Alpha == 0))
             {
                 if ((newMaxX + 1 > Width && Width < MaxWidth) || (newMaxY + 1 > Height && Height < MaxHeight))
                 {
@@ -438,7 +436,7 @@ namespace PixiEditor.Models.Layers
             return LayerBitmap.ToPbgra32ByteArray();
         }
 
-        private Dictionary<Coordinates, Color> GetRelativePosition(Dictionary<Coordinates, Color> changedPixels)
+        private Dictionary<Coordinates, SKColor> GetRelativePosition(Dictionary<Coordinates, SKColor> changedPixels)
         {
             return changedPixels.ToDictionary(
                 d => new Coordinates(d.Key.X - OffsetX, d.Key.Y - OffsetY),
@@ -454,7 +452,7 @@ namespace PixiEditor.Models.Layers
             int maxY = minY;
             bool clipRequested = false;
 
-            foreach (KeyValuePair<Coordinates, Color> pixel in pixels.ChangedPixels)
+            foreach (KeyValuePair<Coordinates, SKColor> pixel in pixels.ChangedPixels)
             {
                 if (pixel.Key.X < minX)
                 {
@@ -474,7 +472,7 @@ namespace PixiEditor.Models.Layers
                     maxY = pixel.Key.Y;
                 }
 
-                if (clipRequested == false && IsBorderPixel(pixel.Key) && pixel.Value.A == 0)
+                if (clipRequested == false && IsBorderPixel(pixel.Key) && pixel.Value.Alpha == 0)
                 {
                     clipRequested = true;
                 }

+ 7 - 8
PixiEditor/Models/Tools/BitmapOperationTool.cs

@@ -1,11 +1,10 @@
-using System;
-using System.Collections.Generic;
-using System.Windows.Documents;
-using System.Windows.Media;
-using PixiEditor.Models.DataHolders;
+using PixiEditor.Models.DataHolders;
 using PixiEditor.Models.Layers;
 using PixiEditor.Models.Position;
-
+using SkiaSharp;
+using System;
+using System.Collections.Generic;
+
 namespace PixiEditor.Models.Tools
 {
     public abstract class BitmapOperationTool : Tool
@@ -18,7 +17,7 @@ namespace PixiEditor.Models.Tools
 
         private readonly LayerChange[] onlyLayerArr = new LayerChange[] { new LayerChange(BitmapPixelChanges.Empty, Guid.Empty) };
 
-        public abstract LayerChange[] Use(Layer layer, List<Coordinates> mouseMove, Color color);
+        public abstract LayerChange[] Use(Layer layer, List<Coordinates> mouseMove, SKColor color);
 
         protected LayerChange[] Only(BitmapPixelChanges changes, Layer layer)
         {
@@ -32,4 +31,4 @@ namespace PixiEditor.Models.Tools
             return onlyLayerArr;
         }
     }
-}
+}

+ 2 - 2
PixiEditor/Models/Tools/ShapeTool.cs

@@ -2,10 +2,10 @@ using PixiEditor.Models.DataHolders;
 using PixiEditor.Models.Layers;
 using PixiEditor.Models.Position;
 using PixiEditor.Models.Tools.ToolSettings.Toolbars;
+using SkiaSharp;
 using System.Collections.Generic;
 using System.Linq;
 using System.Windows.Input;
-using System.Windows.Media;
 
 namespace PixiEditor.Models.Tools
 {
@@ -56,7 +56,7 @@ namespace PixiEditor.Models.Tools
         }
 
         // TODO: Add cache for lines 31, 32 (hopefully it would speed up calculation)
-        public abstract override LayerChange[] Use(Layer layer, List<Coordinates> coordinates, Color color);
+        public abstract override LayerChange[] Use(Layer layer, List<Coordinates> coordinates, SKColor color);
 
         protected static IEnumerable<Coordinates> GetThickShape(IEnumerable<Coordinates> shape, int thickness)
         {

+ 10 - 12
PixiEditor/Models/Tools/Tools/BrightnessTool.cs

@@ -1,9 +1,4 @@
-using System;
-using System.Collections.Generic;
-using System.Windows.Controls;
-using System.Windows.Input;
-using System.Windows.Media;
-using PixiEditor.Helpers.Extensions;
+using PixiEditor.Helpers.Extensions;
 using PixiEditor.Models.Colors;
 using PixiEditor.Models.DataHolders;
 using PixiEditor.Models.Enums;
@@ -11,6 +6,9 @@ using PixiEditor.Models.Layers;
 using PixiEditor.Models.Position;
 using PixiEditor.Models.Tools.ToolSettings.Settings;
 using PixiEditor.Models.Tools.ToolSettings.Toolbars;
+using SkiaSharp;
+using System.Collections.Generic;
+using System.Windows.Input;
 
 namespace PixiEditor.Models.Tools.Tools
 {
@@ -51,7 +49,7 @@ namespace PixiEditor.Models.Tools.Tools
             }
         }
 
-        public override LayerChange[] Use(Layer layer, List<Coordinates> coordinates, Color color)
+        public override LayerChange[] Use(Layer layer, List<Coordinates> coordinates, SKColor color)
         {
             int toolSize = Toolbar.GetSetting<SizeSetting>("ToolSize").Value;
             float correctionFactor = Toolbar.GetSetting<FloatSetting>("CorrectionFactor").Value;
@@ -78,7 +76,7 @@ namespace PixiEditor.Models.Tools.Tools
                 centeredCoords.Coords1.Y,
                 centeredCoords.Coords2.X,
                 centeredCoords.Coords2.Y);
-            BitmapPixelChanges changes = new BitmapPixelChanges(new Dictionary<Coordinates, Color>());
+            BitmapPixelChanges changes = new BitmapPixelChanges(new Dictionary<Coordinates, SKColor>());
 
             foreach (Coordinates coordinate in rectangleCoordinates)
             {
@@ -92,9 +90,9 @@ namespace PixiEditor.Models.Tools.Tools
                     pixelsVisited.Add(coordinate);
                 }
 
-                Color pixel = layer.GetPixelWithOffset(coordinate.X, coordinate.Y);
-                Color newColor = ExColor.ChangeColorBrightness(
-                    Color.FromArgb(pixel.A, pixel.R, pixel.G, pixel.B),
+                SKColor pixel = layer.GetPixelWithOffset(coordinate.X, coordinate.Y);
+                SKColor newColor = ExColor.ChangeColorBrightness(
+                    pixel,
                     correctionFactor);
                 changes.ChangedPixels.Add(
                     new Coordinates(coordinate.X, coordinate.Y),
@@ -104,4 +102,4 @@ namespace PixiEditor.Models.Tools.Tools
             return changes;
         }
     }
-}
+}

+ 11 - 9
PixiEditor/Models/Tools/Tools/CircleTool.cs

@@ -1,13 +1,14 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Windows.Input;
-using System.Windows.Media;
-using PixiEditor.Helpers.Extensions;
+using PixiEditor.Helpers.Extensions;
 using PixiEditor.Models.DataHolders;
 using PixiEditor.Models.Layers;
 using PixiEditor.Models.Position;
 using PixiEditor.Models.Tools.ToolSettings.Settings;
+using SkiaSharp;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Windows.Input;
+using System.Windows.Media;
 
 namespace PixiEditor.Models.Tools.Tools
 {
@@ -36,7 +37,7 @@ namespace PixiEditor.Models.Tools.Tools
             }
         }
 
-        public override LayerChange[] Use(Layer layer, List<Coordinates> coordinates, Color color)
+        public override LayerChange[] Use(Layer layer, List<Coordinates> coordinates, SKColor color)
         {
             int thickness = Toolbar.GetSetting<SizeSetting>("ToolSize").Value;
             DoubleCords fixedCoordinates = CalculateCoordinatesForShapeRotation(coordinates[^1], coordinates[0]);
@@ -44,7 +45,8 @@ namespace PixiEditor.Models.Tools.Tools
             BitmapPixelChanges pixels = BitmapPixelChanges.FromSingleColoredArray(outline, color);
             if (Toolbar.GetSetting<BoolSetting>("Fill").Value)
             {
-                Color fillColor = Toolbar.GetSetting<ColorSetting>("FillColor").Value;
+                Color temp = Toolbar.GetSetting<ColorSetting>("FillColor").Value;
+                SKColor fillColor = new SKColor(temp.R, temp.G, temp.B, temp.A);
                 pixels.ChangedPixels.AddRangeNewOnly(
                     BitmapPixelChanges.FromSingleColoredArray(CalculateFillForEllipse(outline), fillColor)
                         .ChangedPixels);
@@ -212,4 +214,4 @@ namespace PixiEditor.Models.Tools.Tools
             return outputCoordinates;
         }
     }
-}
+}

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

@@ -1,11 +1,11 @@
-using System.Collections.Generic;
-using System.Windows.Media;
-using PixiEditor.Models.Controllers;
+using PixiEditor.Models.Controllers;
 using PixiEditor.Models.DataHolders;
 using PixiEditor.Models.Layers;
 using PixiEditor.Models.Position;
 using PixiEditor.Models.Tools.ToolSettings.Settings;
 using PixiEditor.Models.Tools.ToolSettings.Toolbars;
+using SkiaSharp;
+using System.Collections.Generic;
 
 namespace PixiEditor.Models.Tools.Tools
 {
@@ -22,7 +22,7 @@ namespace PixiEditor.Models.Tools.Tools
 
         public override string Tooltip => "Erasers color from pixel. (E)";
 
-        public override LayerChange[] Use(Layer layer, List<Coordinates> coordinates, Color color)
+        public override LayerChange[] Use(Layer layer, List<Coordinates> coordinates, SKColor color)
         {
             return Erase(layer, coordinates, Toolbar.GetSetting<SizeSetting>("ToolSize").Value);
         }
@@ -30,8 +30,8 @@ namespace PixiEditor.Models.Tools.Tools
         public LayerChange[] Erase(Layer layer, List<Coordinates> coordinates, int toolSize)
         {
             Coordinates startingCords = coordinates.Count > 1 ? coordinates[1] : coordinates[0];
-            BitmapPixelChanges pixels = pen.Draw(startingCords, coordinates[0], System.Windows.Media.Colors.Transparent, toolSize);
+            BitmapPixelChanges pixels = pen.Draw(startingCords, coordinates[0], SKColors.Transparent, toolSize);
             return Only(pixels, layer);
         }
     }
-}
+}

+ 7 - 7
PixiEditor/Models/Tools/Tools/LineTool.cs

@@ -1,13 +1,13 @@
-using System.Collections.Generic;
-using System.Linq;
-using System.Windows.Input;
-using System.Windows.Media;
-using PixiEditor.Models.DataHolders;
+using PixiEditor.Models.DataHolders;
 using PixiEditor.Models.Enums;
 using PixiEditor.Models.Layers;
 using PixiEditor.Models.Position;
 using PixiEditor.Models.Tools.ToolSettings.Settings;
 using PixiEditor.Models.Tools.ToolSettings.Toolbars;
+using SkiaSharp;
+using System.Collections.Generic;
+using System.Linq;
+using System.Windows.Input;
 
 namespace PixiEditor.Models.Tools.Tools
 {
@@ -40,7 +40,7 @@ namespace PixiEditor.Models.Tools.Tools
             }
         }
 
-        public override LayerChange[] Use(Layer layer, List<Coordinates> coordinates, Color color)
+        public override LayerChange[] Use(Layer layer, List<Coordinates> coordinates, SKColor color)
         {
             BitmapPixelChanges pixels =
                 BitmapPixelChanges.FromSingleColoredArray(
@@ -217,4 +217,4 @@ namespace PixiEditor.Models.Tools.Tools
             return coordinates;
         }
     }
-}
+}

+ 15 - 15
PixiEditor/Models/Tools/Tools/MoveTool.cs

@@ -1,10 +1,4 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Windows;
-using System.Windows.Input;
-using System.Windows.Media;
-using PixiEditor.Helpers.Extensions;
+using PixiEditor.Helpers.Extensions;
 using PixiEditor.Models.Controllers;
 using PixiEditor.Models.DataHolders;
 using PixiEditor.Models.Enums;
@@ -13,6 +7,12 @@ using PixiEditor.Models.Layers;
 using PixiEditor.Models.Position;
 using PixiEditor.Models.Undo;
 using PixiEditor.ViewModels;
+using SkiaSharp;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Windows;
+using System.Windows.Input;
 using Transform = PixiEditor.Models.ImageManipulation.Transform;
 
 namespace PixiEditor.Models.Tools.Tools
@@ -23,8 +23,8 @@ namespace PixiEditor.Models.Tools.Tools
         private Dictionary<Guid, bool> clearedPixels = new Dictionary<Guid, bool>();
         private Coordinates[] currentSelection;
         private Coordinates lastMouseMove;
-        private Dictionary<Guid, Color[]> startPixelColors;
-        private Dictionary<Guid, Color[]> endPixelColors;
+        private Dictionary<Guid, SKColor[]> startPixelColors;
+        private Dictionary<Guid, SKColor[]> endPixelColors;
         private Dictionary<Guid, Thickness> startingOffsets;
         private Coordinates[] startSelection;
         private bool updateViewModelSelection = true;
@@ -89,7 +89,7 @@ namespace PixiEditor.Models.Tools.Tools
 
                     ((LayerChange[])changes.NewValue).First(x => x.LayerGuid == layerGuid).PixelChanges.ChangedPixels
                         .AddRangeNewOnly(BitmapPixelChanges
-                            .FromSingleColoredArray(startSelection, System.Windows.Media.Colors.Transparent)
+                            .FromSingleColoredArray(startSelection, SKColors.Transparent)
                             .ChangedPixels);
                 }
             }
@@ -141,7 +141,7 @@ namespace PixiEditor.Models.Tools.Tools
             startingOffsets = GetOffsets(affectedLayers);
         }
 
-        public override LayerChange[] Use(Layer layer, List<Coordinates> mouseMove, Color color)
+        public override LayerChange[] Use(Layer layer, List<Coordinates> mouseMove, SKColor color)
         {
             LayerChange[] result = new LayerChange[affectedLayers.Length];
             var end = mouseMove[0];
@@ -209,7 +209,7 @@ namespace PixiEditor.Models.Tools.Tools
 
         private BitmapPixelChanges RemoveTransparentPixels(BitmapPixelChanges pixels)
         {
-            foreach (var item in pixels.ChangedPixels.Where(x => x.Value.A == 0).ToList())
+            foreach (var item in pixels.ChangedPixels.Where(x => x.Value.Alpha == 0).ToList())
             {
                 pixels.ChangedPixels.Remove(item.Key);
             }
@@ -221,7 +221,7 @@ namespace PixiEditor.Models.Tools.Tools
         {
             lastMouseMove = start;
             clearedPixels = new Dictionary<Guid, bool>();
-            endPixelColors = new Dictionary<Guid, Color[]>();
+            endPixelColors = new Dictionary<Guid, SKColor[]>();
             currentSelection = null;
             affectedLayers = null;
             updateViewModelSelection = true;
@@ -241,10 +241,10 @@ namespace PixiEditor.Models.Tools.Tools
             if (!clearedPixels.ContainsKey(layerGuid) || clearedPixels[layerGuid] == false)
             {
                 BitmapManager.ActiveDocument.Layers.First(x => x == layer)
-                    .SetPixels(BitmapPixelChanges.FromSingleColoredArray(selection, System.Windows.Media.Colors.Transparent));
+                    .SetPixels(BitmapPixelChanges.FromSingleColoredArray(selection, SKColors.Transparent));
 
                 clearedPixels[layerGuid] = true;
             }
         }
     }
-}
+}

+ 7 - 7
PixiEditor/Models/Tools/Tools/PenTool.cs

@@ -6,11 +6,11 @@ using PixiEditor.Models.Position;
 using PixiEditor.Models.Tools.ToolSettings;
 using PixiEditor.Models.Tools.ToolSettings.Settings;
 using PixiEditor.Models.Tools.ToolSettings.Toolbars;
+using SkiaSharp;
 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Windows.Input;
-using System.Windows.Media;
 
 namespace PixiEditor.Models.Tools.Tools
 {
@@ -48,7 +48,7 @@ namespace PixiEditor.Models.Tools.Tools
             confirmedPixels.Clear();
         }
 
-        public override LayerChange[] Use(Layer layer, List<Coordinates> coordinates, Color color)
+        public override LayerChange[] Use(Layer layer, List<Coordinates> coordinates, SKColor color)
         {
             Coordinates startingCords = coordinates.Count > 1 ? coordinates[1] : coordinates[0];
             BitmapPixelChanges pixels = Draw(
@@ -61,7 +61,7 @@ namespace PixiEditor.Models.Tools.Tools
             return Only(pixels, layer);
         }
 
-        public BitmapPixelChanges Draw(Coordinates startingCoords, Coordinates latestCords, Color color, int toolSize, bool pixelPerfect = false, Layer previewLayer = null)
+        public BitmapPixelChanges Draw(Coordinates startingCoords, Coordinates latestCords, SKColor color, int toolSize, bool pixelPerfect = false, Layer previewLayer = null)
         {
             if (!pixelPerfect)
             {
@@ -69,7 +69,7 @@ namespace PixiEditor.Models.Tools.Tools
                     lineTool.CreateLine(startingCoords, latestCords, toolSize), color);
             }
 
-            if (previewLayer != null && previewLayer.GetPixelWithOffset(latestCords.X, latestCords.Y).A > 0)
+            if (previewLayer != null && previewLayer.GetPixelWithOffset(latestCords.X, latestCords.Y).Alpha > 0)
             {
                 confirmedPixels.Add(latestCords);
             }
@@ -103,7 +103,7 @@ namespace PixiEditor.Models.Tools.Tools
 
         private void MovePixelsToCheck(BitmapPixelChanges changes)
         {
-            if (changes.ChangedPixels[lastChangedPixels[1]].A != 0)
+            if (changes.ChangedPixels[lastChangedPixels[1]].Alpha != 0)
             {
                 lastChangedPixels[0] = lastChangedPixels[1];
                 lastChangedPixels[1] = lastChangedPixels[2];
@@ -128,7 +128,7 @@ namespace PixiEditor.Models.Tools.Tools
             }
         }
 
-        private BitmapPixelChanges ApplyPixelPerfectToPixels(Coordinates p1, Coordinates p2, Coordinates p3, Color color, int toolSize)
+        private BitmapPixelChanges ApplyPixelPerfectToPixels(Coordinates p1, Coordinates p2, Coordinates p3, SKColor color, int toolSize)
         {
             if (Math.Abs(p3.X - p1.X) == 1 && Math.Abs(p3.Y - p1.Y) == 1 && !confirmedPixels.Contains(p2))
             {
@@ -136,7 +136,7 @@ namespace PixiEditor.Models.Tools.Tools
                 changes.ChangedPixels.AddRangeNewOnly(
                     BitmapPixelChanges.FromSingleColoredArray(
                         GetThickShape(new[] { p2 }, toolSize),
-                        System.Windows.Media.Colors.Transparent).ChangedPixels);
+                        SKColors.Transparent).ChangedPixels);
                 return changes;
             }
 

+ 10 - 9
PixiEditor/Models/Tools/Tools/RectangleTool.cs

@@ -1,13 +1,13 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Windows.Input;
-using System.Windows.Media;
-using PixiEditor.Helpers.Extensions;
+using PixiEditor.Helpers.Extensions;
 using PixiEditor.Models.DataHolders;
 using PixiEditor.Models.Layers;
 using PixiEditor.Models.Position;
 using PixiEditor.Models.Tools.ToolSettings.Settings;
+using SkiaSharp;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Windows.Input;
 
 namespace PixiEditor.Models.Tools.Tools
 {
@@ -38,14 +38,15 @@ namespace PixiEditor.Models.Tools.Tools
             }
         }
 
-        public override LayerChange[] Use(Layer layer, List<Coordinates> coordinates, Color color)
+        public override LayerChange[] Use(Layer layer, List<Coordinates> coordinates, SKColor color)
         {
             int thickness = Toolbar.GetSetting<SizeSetting>("ToolSize").Value;
             BitmapPixelChanges pixels =
                 BitmapPixelChanges.FromSingleColoredArray(CreateRectangle(coordinates, thickness), color);
             if (Toolbar.GetSetting<BoolSetting>("Fill").Value)
             {
-                Color fillColor = Toolbar.GetSetting<ColorSetting>("FillColor").Value;
+                var tempColor = Toolbar.GetSetting<ColorSetting>("FillColor").Value;
+                SKColor fillColor = new SKColor(tempColor.R, tempColor.G, tempColor.B, tempColor.A);
                 pixels.ChangedPixels.AddRangeOverride(
                     BitmapPixelChanges.FromSingleColoredArray(
                             CalculateFillForRectangle(coordinates[^1], coordinates[0], thickness), fillColor)
@@ -136,4 +137,4 @@ namespace PixiEditor.Models.Tools.Tools
             return finalCoordinates;
         }
     }
-}
+}

+ 1 - 1
PixiEditor/Views/UserControls/AvalonDockWindows/ReferenceLayerWindow.xaml.cs

@@ -61,7 +61,7 @@ namespace PixiEditor.Views.UserControls.AvalonDockWindows
 
         private void UpdateLayer(object obj)
         {
-            Document.ReferenceLayer.LayerBitmap = Importer.ImportImage(FilePath);
+            Document.ReferenceLayer.LayerBitmap = Importer.ImportSurface(FilePath);
             Document.ReferenceLayer.Opacity = LayerOpacity;
         }
 

+ 1 - 1
PixiEditor/Views/UserControls/Layers/ReferenceLayer.xaml.cs

@@ -44,7 +44,7 @@ namespace PixiEditor.Views.UserControls.Layers
             string path = OpenFilePicker();
             if (path != null)
             {
-                var bitmap = Importer.ImportImage(path);
+                var bitmap = Importer.ImportSurface(path);
                 Layer = new Layer("_Reference Layer", bitmap);
             }
         }

+ 2 - 2
PixiEditorTests/ModelsTests/IO/ImporterTests.cs

@@ -37,7 +37,7 @@ namespace PixiEditorTests.ModelsTests.IO
         public void TestThatImportImageImportsImage()
         {
             Color color = Color.FromArgb(255, 255, 0, 0);
-            WriteableBitmap image = Importer.ImportImage(testImagePath);
+            WriteableBitmap image = Importer.ImportSurface(testImagePath);
 
             Assert.NotNull(image);
             Assert.Equal(5, image.PixelWidth);
@@ -62,7 +62,7 @@ namespace PixiEditorTests.ModelsTests.IO
         public void TestThatImporterThrowsCorruptedFileExceptionOnWrongImageFileWithSupportedExtension(string fileName)
         {
             string imagePath = $"{Environment.CurrentDirectory}\\..\\..\\..\\ModelsTests\\IO\\{fileName}";
-            Assert.Throws<CorruptedFileException>(() => { Importer.ImportImage(imagePath); });
+            Assert.Throws<CorruptedFileException>(() => { Importer.ImportSurface(imagePath); });
         }
 
         [Fact]