Browse Source

Fixed MatrixTransform and SeparateChannels nodes

CPKreuz 1 year ago
parent
commit
bd2a7a21df

+ 8 - 10
src/PixiEditor.ChangeableDocument/Changeables/Graph/Nodes/MatrixTransformNode.cs

@@ -1,5 +1,4 @@
 using PixiEditor.ChangeableDocument.Rendering;
-using PixiEditor.DrawingApi.Core.Surface.ImageData;
 using PixiEditor.DrawingApi.Core.Surface.PaintImpl;
 using PixiEditor.Numerics;
 
@@ -7,7 +6,6 @@ namespace PixiEditor.ChangeableDocument.Changeables.Graph.Nodes;
 
 public class MatrixTransformNode : Node
 {
-    
     private Matrix4x5F previousMatrix = new(
         (1, 0, 0, 0, 0),
         (0, 1, 0, 0, 0),
@@ -16,22 +14,22 @@ public class MatrixTransformNode : Node
     
     private Paint paint;
     
-    public OutputProperty<Image> Transformed { get; }
+    public OutputProperty<Surface> Transformed { get; }
     
-    public InputProperty<Image?> Input { get; }
+    public InputProperty<Surface?> Input { get; }
     
     public InputProperty<Matrix4x5F> Matrix { get; }
 
     public MatrixTransformNode()
     {
-        Transformed = CreateOutput<Image>(nameof(Transformed), "TRANSFORMED", null);
-        Input = CreateInput<Image>(nameof(Input), "INPUT", null);
+        Transformed = CreateOutput<Surface>(nameof(Transformed), "TRANSFORMED", null);
+        Input = CreateInput<Surface>(nameof(Input), "INPUT", null);
         Matrix = CreateInput(nameof(Matrix), "MATRIX", previousMatrix);
 
         paint = new Paint { ColorFilter = ColorFilter.CreateColorMatrix(previousMatrix) };
     }
     
-    protected override Image? OnExecute(RenderingContext context)
+    protected override Surface? OnExecute(RenderingContext context)
     {
         var currentMatrix = Matrix.Value;
         if (previousMatrix != currentMatrix)
@@ -40,11 +38,11 @@ public class MatrixTransformNode : Node
             previousMatrix = currentMatrix;
         }
 
-        using var workingSurface = new Surface(Input.Value.Size);
+        var workingSurface = new Surface(Input.Value.Size);
         
-        workingSurface.DrawingSurface.Canvas.DrawImage(Input.Value, 0, 0, paint);
+        workingSurface.DrawingSurface.Canvas.DrawSurface(Input.Value.DrawingSurface, 0, 0, paint);
 
-        Transformed.Value = workingSurface.DrawingSurface.Snapshot();
+        Transformed.Value = workingSurface;
         
         return Transformed.Value;
     }

+ 26 - 28
src/PixiEditor.ChangeableDocument/Changeables/Graph/Nodes/SeparateChannelsNode.cs

@@ -21,30 +21,30 @@ public class SeparateChannelsNode : Node
     private readonly ColorFilter _blueGrayscaleFilter = ColorFilter.CreateColorMatrix(ColorMatrix.UseBlue + ColorMatrix.MapBlueToRedGreen + ColorMatrix.OpaqueAlphaOffset);
     private readonly ColorFilter _alphaGrayscaleFilter = ColorFilter.CreateColorMatrix(ColorMatrix.MapAlphaToRedGreenBlue + ColorMatrix.OpaqueAlphaOffset);
 
-    public OutputProperty<Image?> Red { get; }
+    public OutputProperty<Surface?> Red { get; }
     
-    public OutputProperty<Image?> Green { get; }
+    public OutputProperty<Surface?> Green { get; }
     
-    public OutputProperty<Image?> Blue { get; }
+    public OutputProperty<Surface?> Blue { get; }
 
-    public OutputProperty<Image?> Alpha { get; }
+    public OutputProperty<Surface?> Alpha { get; }
     
-    public InputProperty<Image?> Image { get; }
+    public InputProperty<Surface?> Image { get; }
     
     public InputProperty<bool> Grayscale { get; }
 
     public SeparateChannelsNode()
     {
-        Red = CreateOutput<Image>(nameof(Red), "RED", null);
-        Green = CreateOutput<Image>(nameof(Green), "GREEN", null);
-        Blue = CreateOutput<Image>(nameof(Blue), "BLUE", null);
-        Alpha = CreateOutput<Image>(nameof(Alpha), "ALPHA", null);
+        Red = CreateOutput<Surface>(nameof(Red), "RED", null);
+        Green = CreateOutput<Surface>(nameof(Green), "GREEN", null);
+        Blue = CreateOutput<Surface>(nameof(Blue), "BLUE", null);
+        Alpha = CreateOutput<Surface>(nameof(Alpha), "ALPHA", null);
         
-        Image = CreateInput<Image>(nameof(Image), "IMAGE", null);
+        Image = CreateInput<Surface>(nameof(Image), "IMAGE", null);
         Grayscale = CreateInput(nameof(Grayscale), "GRAYSCALE", false);
     }
     
-    protected override Image OnExecute(RenderingContext context)
+    protected override Surface OnExecute(RenderingContext context)
     {
         var image = Image.Value;
         var grayscale = Grayscale.Value;
@@ -59,33 +59,31 @@ public class SeparateChannelsNode : Node
         Blue.Value = GetImage(image, blue);
         Alpha.Value = GetImage(image, alpha);
 
-        using var previewSurface = new Surface(image.Size);
+        var previewSurface = new Surface(image.Size * 2);
 
-        var half = image.Size / 2;
-        var halfX = half.X;
-        var halfY = half.Y;
+        var size = image.Size;
         
-        var redRect = new RectD(new VecD(), half);
-        var greenRect = new RectD(new VecD(halfX, 0), half);
-        var blueRect = new RectD(new VecD(0, halfY), half);
-        var alphaRect = new RectD(new VecD(halfX, halfY), half);
+        var redPos = new VecI();
+        var greenPos = new VecI(size.X, 0);
+        var bluePos = new VecI(0, size.Y);
+        var alphaPos = new VecI(size.X, size.Y);
         
-        previewSurface.DrawingSurface.Canvas.DrawImage(Red.Value, redRect, context.ReplacingPaintWithOpacity);
-        previewSurface.DrawingSurface.Canvas.DrawImage(Green.Value, greenRect, context.ReplacingPaintWithOpacity);
-        previewSurface.DrawingSurface.Canvas.DrawImage(Blue.Value, blueRect, context.ReplacingPaintWithOpacity);
-        previewSurface.DrawingSurface.Canvas.DrawImage(Alpha.Value, alphaRect, context.ReplacingPaintWithOpacity);
+        previewSurface.DrawingSurface.Canvas.DrawSurface(Red.Value.DrawingSurface, redPos, context.ReplacingPaintWithOpacity);
+        previewSurface.DrawingSurface.Canvas.DrawSurface(Green.Value.DrawingSurface, greenPos, context.ReplacingPaintWithOpacity);
+        previewSurface.DrawingSurface.Canvas.DrawSurface(Blue.Value.DrawingSurface, bluePos, context.ReplacingPaintWithOpacity);
+        previewSurface.DrawingSurface.Canvas.DrawSurface(Alpha.Value.DrawingSurface, alphaPos, context.ReplacingPaintWithOpacity);
         
-        return previewSurface.DrawingSurface.Snapshot();
+        return previewSurface;
     }
 
-    private Image GetImage(Image image, ColorFilter filter)
+    private Surface GetImage(Surface image, ColorFilter filter)
     {
-        using var imageSurface = new Surface(image.Size);
+        var imageSurface = new Surface(image.Size);
 
         _paint.ColorFilter = filter;
-        imageSurface.DrawingSurface.Canvas.DrawImage(image, 0, 0, _paint);
+        imageSurface.DrawingSurface.Canvas.DrawSurface(image.DrawingSurface, 0, 0, _paint);
 
-        return imageSurface.DrawingSurface.Snapshot();
+        return imageSurface;
     }
 
     public override bool Validate() => Image.Value != null;