Răsfoiți Sursa

Fix old files not being parsable

CPKreuz 2 ani în urmă
părinte
comite
1d40e424f7

+ 44 - 1
src/PixiEditor/Helpers/Extensions/PixiParserDocumentEx.cs

@@ -1,8 +1,8 @@
 using ChunkyImageLib;
 using PixiEditor.DrawingApi.Core.ColorsImpl;
 using PixiEditor.DrawingApi.Core.Numerics;
-using PixiEditor.Models.IO;
 using PixiEditor.Parser;
+using PixiEditor.Parser.Deprecated;
 using PixiEditor.ViewModels.SubViewModels.Document;
 
 namespace PixiEditor.Helpers.Extensions;
@@ -77,4 +77,47 @@ internal static class PixiParserDocumentEx
             }
         }
     }
+    
+    public static SKBitmap RenderOldDocument(this SerializableDocument document)
+    {
+        SKImageInfo info = new(document.Width, document.Height, SKColorType.RgbaF32, SKAlphaType.Unpremul, SKColorSpace.CreateSrgb());
+        using SKSurface surface = SKSurface.Create(info);
+        SKCanvas canvas = surface.Canvas;
+        using SKPaint paint = new();
+
+        foreach (var layer in document)
+        {
+            if (layer.PngBytes == null || layer.PngBytes.Length == 0)
+            {
+                continue;
+            }
+
+            bool visible = document.Layers.GetFinalLayerVisibilty(layer);
+
+            if (!visible)
+            {
+                continue;
+            }
+
+            double opacity = document.Layers.GetFinalLayerOpacity(layer);
+
+            if (opacity == 0)
+            {
+                continue;
+            }
+
+            using SKColorFilter filter = SKColorFilter.CreateBlendMode(SKColors.White.WithAlpha((byte)(opacity * 255)), SKBlendMode.DstIn);
+            paint.ColorFilter = filter;
+
+            using var image = SKImage.FromEncodedData(layer.PngBytes);
+            
+            canvas.DrawImage(image, layer.OffsetX, layer.OffsetY, paint);
+        }
+
+        SKBitmap bitmap = new(info);
+
+        surface.ReadPixels(info, bitmap.GetPixels(), info.RowBytes, 0, 0);
+
+        return bitmap;
+    }
 }

+ 22 - 2
src/PixiEditor/Models/Controllers/ClipboardController.cs

@@ -6,7 +6,6 @@ using System.Windows;
 using System.Windows.Media;
 using System.Windows.Media.Imaging;
 using ChunkyImageLib;
-using ChunkyImageLib.DataHolders;
 using PixiEditor.ChangeableDocument.Enums;
 using PixiEditor.DrawingApi.Core.Numerics;
 using PixiEditor.DrawingApi.Core.Surface.ImageData;
@@ -14,6 +13,7 @@ using PixiEditor.Helpers;
 using PixiEditor.Models.Dialogs;
 using PixiEditor.Models.IO;
 using PixiEditor.Parser;
+using PixiEditor.Parser.Deprecated;
 using PixiEditor.ViewModels.SubViewModels.Document;
 
 namespace PixiEditor.Models.Controllers;
@@ -138,7 +138,27 @@ internal static class ClipboardController
                     if (Path.GetExtension(path) == ".pixi")
                     {
                         using var stream = new FileStream(path, FileMode.Open, FileAccess.Read);
-                        imported = Surface.Load(PixiParser.ReadPreview(stream));
+                        
+                        try
+                        {
+                            imported = Surface.Load(PixiParser.Deserialize(path).PreviewImage);
+                        }
+                        catch (InvalidFileException e)
+                        {
+                            // Check if it could be a old file
+                            if (!e.Message.StartsWith("Header"))
+                            {
+                                throw;
+                            }
+                            
+                            stream.Position = 0;
+                            using var bitmap = DepractedPixiParser.Deserialize(stream).RenderOldDocument();
+                            var size = new VecI(bitmap.Width, bitmap.Height);
+                            imported = new Surface(size);
+                            imported.DrawBytes(size, bitmap.Bytes, ColorType.RgbaF32, AlphaType.Premul);
+                            
+                            System.Diagnostics.Debug.Write(imported.ToString());
+                        }
                     }
                     else
                     {