From 6639709e213ea2c5394841cf483703c10da3a4f4 Mon Sep 17 00:00:00 2001 From: flabbet Date: Thu, 16 Dec 2021 17:46:09 +0100 Subject: [PATCH] Some progress has been made --- PixiEditor/Models/Layers/Layer.cs | 2 +- PixiEditor/Models/Undo/StorageBasedChange.cs | 47 +++++++++++++++++--- 2 files changed, 41 insertions(+), 8 deletions(-) diff --git a/PixiEditor/Models/Layers/Layer.cs b/PixiEditor/Models/Layers/Layer.cs index ce39b4083..2ff66ba80 100644 --- a/PixiEditor/Models/Layers/Layer.cs +++ b/PixiEditor/Models/Layers/Layer.cs @@ -140,7 +140,7 @@ public bool IsRenaming public Surface LayerBitmap { get => layerBitmap; - private set + set { Int32Rect prevRect = new Int32Rect(OffsetX, OffsetY, Width, Height); layerBitmap = value; diff --git a/PixiEditor/Models/Undo/StorageBasedChange.cs b/PixiEditor/Models/Undo/StorageBasedChange.cs index 14b7c780d..d7af54fed 100644 --- a/PixiEditor/Models/Undo/StorageBasedChange.cs +++ b/PixiEditor/Models/Undo/StorageBasedChange.cs @@ -3,6 +3,7 @@ using PixiEditor.Models.Layers; using System; using System.Collections.Generic; +using System.Diagnostics; using System.IO; using System.Linq; using System.Text; @@ -94,12 +95,13 @@ public void SaveLayersOnDevice() storedLayer.SerializedRect.Width, storedLayer.SerializedRect.Height); - Random rand = new Random(); + using var image = layer.LayerBitmap.SkiaSurface.Snapshot(); + Surface targetSizeSurface = new Surface(finalRect.Width, finalRect.Height); - layer.LayerBitmap.SkiaSurface.Canvas.DrawPoint(storedLayer.SerializedRect.Left - layer.OffsetX, storedLayer.SerializedRect.Top - layer.OffsetY, - new SKColor((byte)rand.Next(0, 255), (byte)rand.Next(0, 255), (byte)rand.Next(0, 255))); + targetSizeSurface.SkiaSurface.Canvas.DrawImage(image, finalRect, SKRect.Create(0, 0, finalRect.Width, finalRect.Height), Surface.ReplacingPaint); + DebugSavePng(targetSizeSurface, storedLayer); - Exporter.SaveAsGZippedBytes(storedLayer.StoredPngLayerName, layer.LayerBitmap, finalRect); + Exporter.SaveAsGZippedBytes(storedLayer.StoredPngLayerName, targetSizeSurface); } i++; @@ -108,6 +110,19 @@ public void SaveLayersOnDevice() layersToStore = new List(); } + [Conditional("DEBUG")] + private static void DebugSavePng(Surface surface, UndoLayer storedLayer) + { + //Debug png visualization + using var targetSizeImage = surface.SkiaSurface.Snapshot(); + using (var data = targetSizeImage.Encode(SKEncodedImageFormat.Png, 80)) + using (var stream = File.OpenWrite(storedLayer.StoredPngLayerName + ".png")) + { + // save the data to a stream + data.SaveTo(stream); + } + } + /// /// Loads saved layers from disk. /// @@ -122,7 +137,15 @@ public Layer[] LoadLayersFromDevice() var foundLayer = Document.Layers.FirstOrDefault(x => x.LayerGuid == storedLayer.LayerGuid); if (foundLayer != null) { - using var snapshot = bitmap.SkiaSurface.Snapshot(); + Surface targetSizeSurface = new Surface(storedLayer.Width, storedLayer.Height); + using var foundLayerSnapshot = foundLayer.LayerBitmap.SkiaSurface.Snapshot(); + targetSizeSurface.SkiaSurface.Canvas.DrawImage( + foundLayerSnapshot, + SKRect.Create(storedLayer.OffsetX - foundLayer.OffsetX, storedLayer.OffsetY - foundLayer.OffsetY, storedLayer.Width, storedLayer.Height), + SKRect.Create(0, 0, storedLayer.Width, storedLayer.Height), + Surface.ReplacingPaint); + + foundLayer.Offset = new Thickness(storedLayer.OffsetX, storedLayer.OffsetY, 0, 0); SKRect finalRect = SKRect.Create( storedLayer.SerializedRect.Left - foundLayer.OffsetX, @@ -130,8 +153,14 @@ public Layer[] LoadLayersFromDevice() storedLayer.SerializedRect.Width, storedLayer.SerializedRect.Height); - foundLayer.LayerBitmap.SkiaSurface.Canvas.DrawImage(snapshot, finalRect, - new SKPaint { BlendMode = SKBlendMode.Src }); + using var snapshot = bitmap.SkiaSurface.Snapshot(); + + targetSizeSurface.SkiaSurface.Canvas.DrawImage( + snapshot, + finalRect, + Surface.ReplacingPaint); + + foundLayer.LayerBitmap = targetSizeSurface; layers[i] = foundLayer; } @@ -153,6 +182,10 @@ public Layer[] LoadLayersFromDevice() layers[i].ChangeGuid(storedLayer.LayerGuid); } +#if DEBUG + File.Delete(StoredLayers[i].StoredPngLayerName + ".png"); +#endif + File.Delete(StoredLayers[i].StoredPngLayerName); }