Browse Source

Fixed recently openend document preview

flabbet 2 years ago
parent
commit
c64693ec03

+ 12 - 1
src/ChunkyImageLib/Surface.cs

@@ -37,6 +37,17 @@ public class Surface : IDisposable
     {
         DrawingSurface.Canvas.DrawSurface(original.DrawingSurface, 0, 0);
     }
+    
+    public static Surface Combine(int width, int height, List<(Image img, VecI offset)> images)
+    {
+        Surface surface = new Surface(new VecI(width, height));
+        foreach (var (img, offset) in images)
+        {
+            surface.DrawingSurface.Canvas.DrawImage(img, offset.X, offset.Y);
+        }
+
+        return surface;
+    }
 
     public static Surface Load(string path)
     {
@@ -137,7 +148,7 @@ public class Surface : IDisposable
         Unsafe.InitBlockUnaligned((byte*)buffer, 0, (uint)byteC);
         return buffer;
     }
-
+    
     public void Dispose()
     {
         if (disposed)

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

@@ -9,6 +9,7 @@ namespace PixiEditor.DrawingApi.Core.Bridge.Operations
         public Image Snapshot(DrawingSurface drawingSurface);
         public void DisposeImage(Image image);
         public Image FromEncodedData(string path);
+        public Image FromEncodedData(byte[] dataBytes);
         public void GetColorShifts(ref int platformColorAlphaShift, ref int platformColorRedShift, ref int platformColorGreenShift, ref int platformColorBlueShift);
         public ImgData Encode(Image image);
         public int GetWidth(IntPtr objectPointer);

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

@@ -30,6 +30,11 @@ namespace PixiEditor.DrawingApi.Core.Surface.ImageData
         {
             return DrawingBackendApi.Current.ImageImplementation.FromEncodedData(path);
         }
+        
+        public static Image FromEncodedData(byte[] dataBytes)
+        {
+            return DrawingBackendApi.Current.ImageImplementation.FromEncodedData(dataBytes);
+        }
 
         public ImgData Encode()
         {

+ 2 - 3
src/PixiEditor.DrawingApi.Skia/Implementations/SkiaCanvasImplementation.cs

@@ -18,16 +18,15 @@ namespace PixiEditor.DrawingApi.Skia.Implementations
         private readonly SkObjectImplementation<SKBitmap> _bitmapImpl;
         private readonly SkObjectImplementation<SKPath> _pathImpl;
 
-        public SkiaCanvasImplementation(SkObjectImplementation<SKPaint> paintImpl, SkObjectImplementation<SKSurface> surfaceImpl, SkObjectImplementation<SKImage> imageImpl, SkObjectImplementation<SKBitmap> bitmapImpl, SkObjectImplementation<SKPath> pathImpl)
+        public SkiaCanvasImplementation(SkObjectImplementation<SKPaint> paintImpl, SkObjectImplementation<SKImage> imageImpl, SkObjectImplementation<SKBitmap> bitmapImpl, SkObjectImplementation<SKPath> pathImpl)
         {
             _paintImpl = paintImpl;
-            _surfaceImpl = surfaceImpl;
             _imageImpl = imageImpl;
             _bitmapImpl = bitmapImpl;
             _pathImpl = pathImpl;
         }
         
-        public void SetSurfaceImpl(SkiaSurfaceImplementation surfaceImpl)
+        public void SetSurfaceImplementation(SkiaSurfaceImplementation surfaceImpl)
         {
             _surfaceImpl = surfaceImpl;
         }

+ 19 - 1
src/PixiEditor.DrawingApi.Skia/Implementations/SkiaImageImplementation.cs

@@ -10,15 +10,33 @@ namespace PixiEditor.DrawingApi.Skia.Implementations
     public class SkiaImageImplementation : SkObjectImplementation<SKImage>, IImageImplementation
     {
         private readonly SkObjectImplementation<SKData> _imgImplementation;
+        private SkObjectImplementation<SKSurface> _surfaceImplementation;
         
         public SkiaImageImplementation(SkObjectImplementation<SKData> imgDataImplementation)
         {
             _imgImplementation = imgDataImplementation;
         }
         
+        public void SetSurfaceImplementation(SkObjectImplementation<SKSurface> surfaceImplementation)
+        {
+            _surfaceImplementation = surfaceImplementation;
+        }
+        
         public Image Snapshot(DrawingSurface drawingSurface)
         {
-            throw new NotImplementedException();
+            var surface = _surfaceImplementation[drawingSurface.ObjectPointer];
+            SKImage snapshot = surface.Snapshot();
+            
+            ManagedInstances[snapshot.Handle] = snapshot;
+            return new Image(snapshot.Handle);
+        }
+        
+        public Image FromEncodedData(byte[] dataBytes)
+        {
+            SKImage img = SKImage.FromEncodedData(dataBytes);
+            ManagedInstances[img.Handle] = img;
+            
+            return new Image(img.Handle);
         }
 
         public void DisposeImage(Image image)

+ 3 - 2
src/PixiEditor.DrawingApi.Skia/SkiaDrawingBackend.cs

@@ -46,11 +46,12 @@ namespace PixiEditor.DrawingApi.Skia
             SkiaBitmapImplementation bitmapImpl = new SkiaBitmapImplementation();
             BitmapImplementation = bitmapImpl;
             
-            SkiaCanvasImplementation canvasImpl = new SkiaCanvasImplementation(paintImpl, null, imgImpl, bitmapImpl, pathImpl);
+            SkiaCanvasImplementation canvasImpl = new SkiaCanvasImplementation(paintImpl, imgImpl, bitmapImpl, pathImpl);
             
             var surfaceImpl = new SkiaSurfaceImplementation(pixmapImpl, canvasImpl, paintImpl);
 
-            canvasImpl.SetSurfaceImpl(surfaceImpl);
+            canvasImpl.SetSurfaceImplementation(surfaceImpl);
+            imgImpl.SetSurfaceImplementation(surfaceImpl);
 
             CanvasImplementation = canvasImpl;
 

+ 6 - 0
src/PixiEditor/Helpers/Extensions/ParserHelpers.cs

@@ -1,4 +1,5 @@
 using PixiEditor.DrawingApi.Core.ColorsImpl;
+using PixiEditor.DrawingApi.Core.Surface.ImageData;
 using PixiEditor.Parser;
 using PixiEditor.Parser.Collections;
 using PixiEditor.ViewModels.SubViewModels.Document;
@@ -7,6 +8,11 @@ namespace PixiEditor.Helpers.Extensions;
 
 internal static class ParserHelpers
 {
+    public static Image ToImage(this SerializableLayer serializableLayer)
+    {
+        return Image.FromEncodedData(serializableLayer.PngBytes);
+    }
+    
     public static DocumentViewModel ToDocument(this SerializableDocument serializableDocument)
     {
         List<SerializableLayer> builtLayers = new List<SerializableLayer>();

+ 5 - 5
src/PixiEditor/Models/DataHolders/RecentlyOpenedDocument.cs

@@ -3,6 +3,7 @@ using System.IO;
 using System.Windows.Media.Imaging;
 using ChunkyImageLib;
 using ChunkyImageLib.DataHolders;
+using PixiEditor.DrawingApi.Core.Numerics;
 using PixiEditor.Helpers;
 using PixiEditor.Models.IO;
 using PixiEditor.Parser;
@@ -71,7 +72,7 @@ internal class RecentlyOpenedDocument : NotifyableObject
     {
         if (FileExtension == ".pixi")
         {
-            /*
+            
             SerializableDocument serializableDocument;
 
             try
@@ -87,12 +88,11 @@ internal class RecentlyOpenedDocument : NotifyableObject
             using Surface surface = Surface.Combine(serializableDocument.Width, serializableDocument.Height,
                 serializableDocument.Layers
                     .Where(x => x.Opacity > 0.8)
-                    .Select(x => (x.ToSKImage(), new VecI(x.OffsetX, x.OffsetY))));
+                    .Select(x => (x.ToImage(), new VecI(x.OffsetX, x.OffsetY))).ToList());
 
-            return DownscaleToMaxSize(surface.ToWriteableBitmap());*/
-            return null;
+            return DownscaleToMaxSize(surface.ToWriteableBitmap());
         }
-        else if (SupportedFilesHelper.IsExtensionSupported(FileExtension))
+        if (SupportedFilesHelper.IsExtensionSupported(FileExtension))
         {
             WriteableBitmap bitmap = null;