Jelajahi Sumber

fixed color space image saving/loading

flabbet 8 bulan lalu
induk
melakukan
5ccbe60877

+ 2 - 2
src/PixiEditor/Helpers/SurfaceHelpers.cs

@@ -36,11 +36,11 @@ public static class SurfaceHelpers
         return result;
     }
 
-    public static unsafe byte[] ToByteArray(this Surface surface, ColorType colorType = ColorType.Bgra8888, AlphaType alphaType = AlphaType.Premul)
+    public static unsafe byte[] ToByteArray(this Surface surface, ColorType colorType = ColorType.Bgra8888, AlphaType alphaType = AlphaType.Premul, ColorSpace colorSpace = null)
     {
         int width = surface.Size.X;
         int height = surface.Size.Y;
-        var imageInfo = new ImageInfo(width, height, colorType, alphaType, ColorSpace.CreateSrgb());
+        var imageInfo = new ImageInfo(width, height, colorType, alphaType, colorSpace == null ? surface.ImageInfo.ColorSpace : colorSpace);
 
         byte[] buffer = new byte[width * height * imageInfo.BytesPerPixel];
         fixed (void* pointer = buffer)

+ 1 - 1
src/PixiEditor/Models/Serialization/Factories/ChunkyImageSerializationFactory.cs

@@ -35,7 +35,7 @@ public class ChunkyImageSerializationFactory : SerializationFactory<byte[], Chun
                 return false;
             }
 
-            original = new ChunkyImage(surface.Size, Config.ProcessingProcessingColorSpace);
+            original = new ChunkyImage(surface.Size, Config.ProcessingColorSpace);
             original.EnqueueDrawImage(VecI.Zero, surface);
             original.CommitChanges();
             return true;

+ 11 - 6
src/PixiEditor/Models/Serialization/Factories/SurfaceSerializationFactory.cs

@@ -11,7 +11,8 @@ public class SurfaceSerializationFactory : SerializationFactory<byte[], Surface>
     public override byte[] Serialize(Surface original)
     {
         var encoder = Config.Encoder;
-        byte[] result = encoder.Encode(original.ToByteArray(), original.Size.X, original.Size.Y);
+        byte[] result = encoder.Encode(original.ToByteArray(), original.Size.X, original.Size.Y,
+            original.ImageInfo.ColorSpace?.IsSrgb ?? true);
 
         return result;
     }
@@ -21,21 +22,25 @@ public class SurfaceSerializationFactory : SerializationFactory<byte[], Surface>
     {
         if (serialized is byte[] imgBytes)
         {
-            original = DecodeSurface(imgBytes, Config.Encoder);
+            original = DecodeSurface(imgBytes, Config.Encoder, Config.ProcessingColorSpace);
             return true;
         }
-        
+
         original = null;
         return false;
     }
 
 
-    public static Surface DecodeSurface(byte[] imgBytes, ImageEncoder encoder)
+    public static Surface DecodeSurface(byte[] imgBytes, ImageEncoder encoder, ColorSpace processingColorSpace)
     {
         byte[] decoded =
             encoder.Decode(imgBytes, out SKImageInfo info);
-        using Image img = Image.FromPixels(info.ToImageInfo(), decoded);
-        Surface surface = new Surface(img.Size);
+        ImageInfo finalInfo = info.ToImageInfo();
+
+        using Image img = Image.FromPixels(finalInfo, decoded);
+        Surface surface = Surface.ForDisplay(finalInfo.Size);
+
+
         surface.DrawingSurface.Canvas.DrawImage(img, 0, 0);
 
         return surface;

+ 2 - 2
src/PixiEditor/Models/Serialization/SerializationConfig.cs

@@ -6,11 +6,11 @@ namespace PixiEditor.Models.Serialization;
 public class SerializationConfig
 {
     public ImageEncoder Encoder { get; set; }
-    public ColorSpace ProcessingProcessingColorSpace { get; set; }
+    public ColorSpace ProcessingColorSpace { get; set; }
     
     public SerializationConfig(ImageEncoder encoder, ColorSpace processingColorSpace)
     {
         Encoder = encoder;
-        ProcessingProcessingColorSpace = processingColorSpace;
+        ProcessingColorSpace = processingColorSpace;
     }
 }

+ 1 - 1
src/PixiEditor/ViewModels/Document/DocumentViewModel.Serialization.cs

@@ -431,7 +431,7 @@ internal partial class DocumentViewModel
         var shape = layer.Shape;
         var imageSize = layer.ImageSize;
 
-        var imageBytes = config.Encoder.Encode(layer.ImageBgra8888Bytes.ToArray(), imageSize.X, imageSize.Y);
+        var imageBytes = config.Encoder.Encode(layer.ImageBgra8888Bytes.ToArray(), imageSize.X, imageSize.Y, true);
 
         return new ReferenceLayer
         {

+ 1 - 1
src/PixiParser

@@ -1 +1 @@
-Subproject commit 4176e1eb211e5e6f8932671e82b2813c737a1a56
+Subproject commit 7f066e0c25dfdf064ec3ea965b90c4b125321dd9