Browse Source

Fixed reference layer

Krzysztof Krysiński 1 year ago
parent
commit
62114d822a

+ 12 - 0
src/ChunkyImageLib/Surface.cs

@@ -75,6 +75,18 @@ public class Surface : IDisposable
         return surface;
     }
 
+    public static Surface? Load(byte[] encoded, ColorType colorType, VecI imageSize)
+    {
+        using var image = Image.FromPixels(new ImageInfo(imageSize.X, imageSize.Y, colorType), encoded);
+        if (image is null)
+            return null;
+
+        var surface = new Surface(new VecI(image.Width, image.Height));
+        surface.DrawingSurface.Canvas.DrawImage(image, 0, 0);
+
+        return surface;
+    }
+
     public unsafe void DrawBytes(VecI size, byte[] bytes, ColorType colorType, AlphaType alphaType)
     {
         ImageInfo info = new ImageInfo(size.X, size.Y, colorType, alphaType);

+ 2 - 1
src/PixiEditor.AvaloniaUI/ViewModels/Document/ReferenceLayerViewModel.cs

@@ -13,6 +13,7 @@ using PixiEditor.AvaloniaUI.ViewModels.Tools.Tools;
 using PixiEditor.ChangeableDocument.Actions.Generated;
 using PixiEditor.DrawingApi.Core.Numerics;
 using PixiEditor.DrawingApi.Core.Surface;
+using PixiEditor.DrawingApi.Core.Surface.ImageData;
 
 namespace PixiEditor.AvaloniaUI.ViewModels.Document;
 
@@ -112,7 +113,7 @@ internal class ReferenceLayerViewModel : ObservableObject, IReferenceLayerHandle
     
     public void SetReferenceLayer(ImmutableArray<byte> imageBgra8888Bytes, VecI imageSize, ShapeCorners shape)
     {
-        ReferenceBitmap = Surface.Load(imageBgra8888Bytes.ToArray()); //TODO: Was WriteableBitmapUtility.FromBgra8888Array(imageBgra8888Bytes.ToArray(), imageSize);
+        ReferenceBitmap = Surface.Load(imageBgra8888Bytes.ToArray(), ColorType.Bgra8888, imageSize); //TODO: Was WriteableBitmapUtility.FromBgra8888Array(imageBgra8888Bytes.ToArray(), imageSize);
         referenceShape = shape;
         isVisible = true;
         isTransforming = false;

+ 1 - 0
src/PixiEditor.DrawingApi.Core/Bridge/Operations/IImageImplementation.cs

@@ -12,6 +12,7 @@ namespace PixiEditor.DrawingApi.Core.Bridge.Operations
         public void DisposeImage(Image image);
         public Image? FromEncodedData(string path);
         public Image? FromEncodedData(byte[] dataBytes);
+        public Image? FromPixelCopy(ImageInfo info, byte[] pixels);
         public void GetColorShifts(ref int platformColorAlphaShift, ref int platformColorRedShift, ref int platformColorGreenShift, ref int platformColorBlueShift);
         public ImgData Encode(Image image);
         public ImgData Encode(Image image, EncodedImageFormat format, int quality);

+ 5 - 0
src/PixiEditor.DrawingApi.Core/Surface/ImageData/Image.cs

@@ -38,6 +38,11 @@ namespace PixiEditor.DrawingApi.Core.Surface.ImageData
             return DrawingBackendApi.Current.ImageImplementation.FromEncodedData(dataBytes);
         }
 
+        public static Image? FromPixels(ImageInfo info, byte[] pixels)
+        {
+            return DrawingBackendApi.Current.ImageImplementation.FromPixelCopy(info, pixels);
+        }
+
         public ImgData Encode()
         {
             return DrawingBackendApi.Current.ImageImplementation.Encode(this);

+ 9 - 0
src/PixiEditor.DrawingApi.Skia/Implementations/SkiaImageImplementation.cs

@@ -66,6 +66,15 @@ namespace PixiEditor.DrawingApi.Skia.Implementations
             return new Image(nativeImg.Handle);
         }
 
+        public Image? FromPixelCopy(ImageInfo info, byte[] pixels)
+        {
+            var nativeImg = SKImage.FromPixelCopy(info.ToSkImageInfo(), pixels);
+            if (nativeImg is null)
+                return null;
+            ManagedInstances[nativeImg.Handle] = nativeImg;
+            return new Image(nativeImg.Handle);
+        }
+
         public void GetColorShifts(ref int platformColorAlphaShift, ref int platformColorRedShift, ref int platformColorGreenShift,
             ref int platformColorBlueShift)
         {