Forráskód Böngészése

Get Layer Image works

flabbet 2 éve
szülő
commit
55d5cca78b

+ 12 - 3
src/ChunkyImageLib/ChunkyImageEx.cs

@@ -7,6 +7,15 @@ using PixiEditor.DrawingApi.Core.Surface.PaintImpl;
 namespace ChunkyImageLib;
 public static class IReadOnlyChunkyImageEx
 {
+    /// <summary>
+    ///     Draws the image onto the passed surface.
+    /// </summary>
+    /// <param name="image">Image to draw.</param>
+    /// <param name="fullResRegion">A region inside an chunky image.</param>
+    /// <param name="resolution">Chunk resolution.</param>
+    /// <param name="surface">Surface to draw onto.</param>
+    /// <param name="pos">Starting position on the surface.</param>
+    /// <param name="paint">Paint that is used to draw.</param>
     public static void DrawMostUpToDateRegionOn
         (this IReadOnlyChunkyImage image, RectI fullResRegion, ChunkResolution resolution, DrawingSurface surface, VecI pos, Paint? paint = null)
     {
@@ -14,13 +23,13 @@ public static class IReadOnlyChunkyImageEx
         surface.Canvas.ClipRect(RectD.Create(pos, fullResRegion.Size));
 
         VecI chunkTopLeft = OperationHelper.GetChunkPos(fullResRegion.TopLeft, ChunkyImage.FullChunkSize);
-        VecI chunkBotRigth = OperationHelper.GetChunkPos(fullResRegion.BottomRight, ChunkyImage.FullChunkSize);
+        VecI chunkBotRight = OperationHelper.GetChunkPos(fullResRegion.BottomRight, ChunkyImage.FullChunkSize);
         VecI offsetFullRes = (chunkTopLeft * ChunkyImage.FullChunkSize) - fullResRegion.Pos;
         VecI offsetTargetRes = (VecI)(offsetFullRes * resolution.Multiplier());
 
-        for (int j = chunkTopLeft.Y; j <= chunkBotRigth.Y; j++)
+        for (int j = chunkTopLeft.Y; j <= chunkBotRight.Y; j++)
         {
-            for (int i = chunkTopLeft.X; i <= chunkBotRigth.X; i++)
+            for (int i = chunkTopLeft.X; i <= chunkBotRight.X; i++)
             {
                 var chunkPos = new VecI(i, j);
                 image.DrawMostUpToDateChunkOn(chunkPos, resolution, surface, offsetTargetRes + (chunkPos - chunkTopLeft) * resolution.PixelSize() + pos, paint);

+ 14 - 9
src/PixiEditor/Helpers/Extensions/ParserHelpers.cs

@@ -1,7 +1,10 @@
 using ChunkyImageLib;
 using ChunkyImageLib.DataHolders;
 using ChunkyImageLib.Operations;
+using PixiEditor.ChangeableDocument.Changeables.Interfaces;
+using PixiEditor.ChangeableDocument.Rendering;
 using PixiEditor.DrawingApi.Core.ColorsImpl;
+using PixiEditor.DrawingApi.Core.Numerics;
 using PixiEditor.DrawingApi.Core.Surface.ImageData;
 using PixiEditor.Parser;
 using PixiEditor.Parser.Collections;
@@ -160,16 +163,16 @@ internal static class ParserHelpers
         }
     }
     
-    public static SerializableDocument ToSerializable(this DocumentViewModel document)
+    public static SerializableDocument ToSerializable(this DocumentViewModel documentViewModel)
     {
-        return new SerializableDocument(document.Width, document.Height,
-                ToSerializableGroups(document.StructureRoot, document),
-                ToSerializableLayers(document.StructureRoot))
-            .AddSwatches(document.Swatches)
-            .AddPalette(document.Palette);
+        return new SerializableDocument(documentViewModel.Width, documentViewModel.Height,
+                ToSerializableGroups(documentViewModel.StructureRoot, documentViewModel),
+                ToSerializableLayers(documentViewModel.StructureRoot, documentViewModel))
+            .AddSwatches(documentViewModel.Swatches)
+            .AddPalette(documentViewModel.Palette);
     }
 
-    private static List<SerializableLayer> ToSerializableLayers(FolderViewModel documentStructureRoot)
+    private static List<SerializableLayer> ToSerializableLayers(FolderViewModel documentStructureRoot, DocumentViewModel document)
     {
         List<SerializableLayer> layers = new List<SerializableLayer>();
         
@@ -177,15 +180,17 @@ internal static class ParserHelpers
         {
             if (member is LayerViewModel layer)
             {
-                layers.Add(layer.ToSerializable());
+                layers.Add(layer.ToSerializable(document));
             }
         });
         
         return layers;
     }
 
-    private static SerializableLayer ToSerializable(this LayerViewModel layer)
+    private static SerializableLayer ToSerializable(this LayerViewModel layer, DocumentViewModel document)
     {
+        var result = document.GetLayerImage(layer.GuidValue);
+
         return new SerializableLayer();
     }
 

+ 35 - 0
src/PixiEditor/ViewModels/SubViewModels/Document/DocumentViewModel.cs

@@ -180,6 +180,41 @@ internal class DocumentViewModel : NotifyableObject
         PreviewBitmap = new WriteableBitmap(previewSize.X, previewSize.Y, 96, 96, PixelFormats.Pbgra32, null);
         PreviewSurface = DrawingSurface.Create(new ImageInfo(previewSize.X, previewSize.Y, ColorType.Bgra8888), PreviewBitmap.BackBuffer, PreviewBitmap.BackBufferStride);
     }
+    
+    /// <summary>
+    ///     Creates a surface for layer image.
+    /// </summary>
+    /// <param name="layerGuid">Guid of the layer inside structure.</param>
+    /// <returns>Surface if the layer has some drawn pixels, null if the image is empty.</returns>
+    /// <exception cref="ArgumentException">Exception when guid is not found inside structure or if it's not a layer</exception>
+    /// <remarks>So yeah, welcome folks to the multithreaded world, where possibilities are endless! (and chances of objects getting
+    /// edited, in between of processing you want to make exist). You might encounter ObjectDisposedException and other mighty creatures here if
+    /// you are lucky enough. Have fun!</remarks>
+    public Surface? GetLayerImage(Guid layerGuid)
+    {
+        IReadOnlyDocument document = Internals.Tracker.Document;
+        var layer = (IReadOnlyLayer?)document.FindMember(layerGuid);
+
+        if (layer is null)
+            throw new ArgumentException(@"The given guid does not belong to a layer.", nameof(layerGuid));
+
+
+        RectI? tightBounds = layer.LayerImage.FindLatestBounds();
+
+        if (tightBounds is null)
+            return null;
+
+        tightBounds = tightBounds.Value.Intersect(RectI.Create(0, 0, document.Size.X, document.Size.Y));
+
+        Surface surface = new Surface(tightBounds.Value.Size);
+
+        layer.LayerImage.DrawMostUpToDateRegionOn(
+            tightBounds.Value,
+            ChunkResolution.Full,
+            surface.DrawingSurface, VecI.Zero);
+
+        return surface;
+    }
 
     public static DocumentViewModel Build(Action<DocumentViewModelBuilder> builder)
     {