ModifyImageRightNode.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. using PixiEditor.ChangeableDocument.Changeables.Animations;
  2. using PixiEditor.ChangeableDocument.Changeables.Graph.Interfaces;
  3. using PixiEditor.ChangeableDocument.Rendering;
  4. using PixiEditor.DrawingApi.Core.ColorsImpl;
  5. using PixiEditor.DrawingApi.Core.Surface;
  6. using PixiEditor.DrawingApi.Core.Surface.ImageData;
  7. using PixiEditor.DrawingApi.Core.Surface.PaintImpl;
  8. using PixiEditor.Numerics;
  9. namespace PixiEditor.ChangeableDocument.Changeables.Graph.Nodes;
  10. public class ModifyImageRightNode : Node
  11. {
  12. private ModifyImageLeftNode startNode;
  13. private Paint drawingPaint = new Paint() { BlendMode = BlendMode.Src };
  14. public FieldInputProperty<Color> Color { get; }
  15. public OutputProperty<Image> Output { get; }
  16. public ModifyImageRightNode(ModifyImageLeftNode startNode)
  17. {
  18. this.startNode = startNode;
  19. Color = CreateFieldInput(nameof(Color), "COLOR", new Color());
  20. Output = CreateOutput<Image>(nameof(Output), "OUTPUT", null);
  21. }
  22. protected override Image? OnExecute(RenderingContext renderingContext)
  23. {
  24. if (startNode.Image.Value is not { Size: var size })
  25. {
  26. return null;
  27. }
  28. startNode.PreparePixmap();
  29. var width = size.X;
  30. var height = size.Y;
  31. using var surface = new Surface(size);
  32. for (int y = 0; y < height; y++)
  33. {
  34. for (int x = 0; x < width; x++)
  35. {
  36. var context = new FieldContext(new VecD((double)x / width, (double)y / height), new VecI(width, height));
  37. var color = Color.Value(context);
  38. drawingPaint.Color = color;
  39. surface.DrawingSurface.Canvas.DrawPixel(x, y, drawingPaint);
  40. }
  41. }
  42. Output.Value = surface.DrawingSurface.Snapshot();
  43. return Output.Value;
  44. }
  45. public override bool Validate() => true;
  46. public override Node CreateCopy() => throw new NotImplementedException();
  47. }