From a8fdb13f2a1e1f0ab370aed70c088a71826ab0d7 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 26 Dec 2019 11:27:21 +0100 Subject: [PATCH] Added support for custom pixel modifiers --- .../Effects/PixelShaderProcessor.cs | 24 +++++++++++++++++++ .../Effects/PixelShaderProcessorBase.cs | 13 +++++++--- .../Effects/PixelShaderProcessor{TPixel}.cs | 2 +- .../PositionAwarePixelShaderProcessor.cs | 24 +++++++++++++++++++ ...sitionAwarePixelShaderProcessor{TPixel}.cs | 2 +- 5 files changed, 60 insertions(+), 5 deletions(-) diff --git a/src/ImageSharp/Processing/Processors/Effects/PixelShaderProcessor.cs b/src/ImageSharp/Processing/Processors/Effects/PixelShaderProcessor.cs index 6c44199da..2a271bef3 100644 --- a/src/ImageSharp/Processing/Processors/Effects/PixelShaderProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Effects/PixelShaderProcessor.cs @@ -11,6 +11,11 @@ namespace SixLabors.ImageSharp.Processing.Processors.Effects /// public sealed class PixelShaderProcessor : IImageProcessor { + /// + /// The default to apply during the pixel conversions. + /// + public const PixelConversionModifiers DefaultModifiers = PixelConversionModifiers.None; + /// /// Initializes a new instance of the class. /// @@ -20,6 +25,20 @@ namespace SixLabors.ImageSharp.Processing.Processors.Effects public PixelShaderProcessor(PixelShader pixelShader) { this.PixelShader = pixelShader; + this.Modifiers = DefaultModifiers; + } + + /// + /// Initializes a new instance of the class. + /// + /// + /// The user defined pixel shader to use to modify images. + /// + /// The to apply during the pixel conversions. + public PixelShaderProcessor(PixelShader pixelShader, PixelConversionModifiers modifiers) + { + this.PixelShader = pixelShader; + this.Modifiers = modifiers; } /// @@ -27,6 +46,11 @@ namespace SixLabors.ImageSharp.Processing.Processors.Effects /// public PixelShader PixelShader { get; } + /// + /// Gets the to apply during the pixel conversions. + /// + public PixelConversionModifiers Modifiers { get; } + /// public IImageProcessor CreatePixelSpecificProcessor(Image source, Rectangle sourceRectangle) where TPixel : struct, IPixel diff --git a/src/ImageSharp/Processing/Processors/Effects/PixelShaderProcessorBase.cs b/src/ImageSharp/Processing/Processors/Effects/PixelShaderProcessorBase.cs index a8e032ee1..f1245ca51 100644 --- a/src/ImageSharp/Processing/Processors/Effects/PixelShaderProcessorBase.cs +++ b/src/ImageSharp/Processing/Processors/Effects/PixelShaderProcessorBase.cs @@ -18,14 +18,21 @@ namespace SixLabors.ImageSharp.Processing.Processors.Effects internal abstract class PixelShaderProcessorBase : ImageProcessor where TPixel : struct, IPixel { + /// + /// The to apply during the pixel conversions. + /// + private readonly PixelConversionModifiers modifiers; + /// /// Initializes a new instance of the class. /// + /// The to apply during the pixel conversions. /// The source for the current processor instance. /// The source area to process for the current processor instance. - protected PixelShaderProcessorBase(Image source, Rectangle sourceRectangle) + protected PixelShaderProcessorBase(PixelConversionModifiers modifiers, Image source, Rectangle sourceRectangle) : base(source, sourceRectangle) { + this.modifiers = modifiers; } /// @@ -44,12 +51,12 @@ namespace SixLabors.ImageSharp.Processing.Processors.Effects Span vectorSpan = vectorBuffer.Span; int length = vectorSpan.Length; Span rowSpan = source.GetPixelRowSpan(y).Slice(startX, length); - PixelOperations.Instance.ToVector4(this.Configuration, rowSpan, vectorSpan); + PixelOperations.Instance.ToVector4(this.Configuration, rowSpan, vectorSpan, this.modifiers); // Run the user defined pixel shader on the current row of pixels this.ApplyPixelShader(vectorSpan, y, startX); - PixelOperations.Instance.FromVector4Destructive(this.Configuration, vectorSpan, rowSpan); + PixelOperations.Instance.FromVector4Destructive(this.Configuration, vectorSpan, rowSpan, this.modifiers); } }); } diff --git a/src/ImageSharp/Processing/Processors/Effects/PixelShaderProcessor{TPixel}.cs b/src/ImageSharp/Processing/Processors/Effects/PixelShaderProcessor{TPixel}.cs index 29e3ab5a9..4abd69838 100644 --- a/src/ImageSharp/Processing/Processors/Effects/PixelShaderProcessor{TPixel}.cs +++ b/src/ImageSharp/Processing/Processors/Effects/PixelShaderProcessor{TPixel}.cs @@ -28,7 +28,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Effects /// The source for the current processor instance. /// The source area to process for the current processor instance. public PixelShaderProcessor(PixelShaderProcessor definition, Image source, Rectangle sourceRectangle) - : base(source, sourceRectangle) + : base(definition.Modifiers, source, sourceRectangle) { this.pixelShader = definition.PixelShader; } diff --git a/src/ImageSharp/Processing/Processors/Effects/PositionAwarePixelShaderProcessor.cs b/src/ImageSharp/Processing/Processors/Effects/PositionAwarePixelShaderProcessor.cs index a83e8b5a4..908f7472b 100644 --- a/src/ImageSharp/Processing/Processors/Effects/PositionAwarePixelShaderProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Effects/PositionAwarePixelShaderProcessor.cs @@ -11,6 +11,11 @@ namespace SixLabors.ImageSharp.Processing.Processors.Effects /// public sealed class PositionAwarePixelShaderProcessor : IImageProcessor { + /// + /// The default to apply during the pixel conversions. + /// + public const PixelConversionModifiers DefaultModifiers = PixelConversionModifiers.None; + /// /// Initializes a new instance of the class. /// @@ -20,6 +25,20 @@ namespace SixLabors.ImageSharp.Processing.Processors.Effects public PositionAwarePixelShaderProcessor(PositionAwarePixelShader pixelShader) { this.PixelShader = pixelShader; + this.Modifiers = DefaultModifiers; + } + + /// + /// Initializes a new instance of the class. + /// + /// + /// The user defined pixel shader to use to modify images. + /// + /// The to apply during the pixel conversions. + public PositionAwarePixelShaderProcessor(PositionAwarePixelShader pixelShader, PixelConversionModifiers modifiers) + { + this.PixelShader = pixelShader; + this.Modifiers = modifiers; } /// @@ -27,6 +46,11 @@ namespace SixLabors.ImageSharp.Processing.Processors.Effects /// public PositionAwarePixelShader PixelShader { get; } + /// + /// Gets the to apply during the pixel conversions. + /// + public PixelConversionModifiers Modifiers { get; } + /// public IImageProcessor CreatePixelSpecificProcessor(Image source, Rectangle sourceRectangle) where TPixel : struct, IPixel diff --git a/src/ImageSharp/Processing/Processors/Effects/PositionAwarePixelShaderProcessor{TPixel}.cs b/src/ImageSharp/Processing/Processors/Effects/PositionAwarePixelShaderProcessor{TPixel}.cs index 6c4eee35f..3b9f37b48 100644 --- a/src/ImageSharp/Processing/Processors/Effects/PositionAwarePixelShaderProcessor{TPixel}.cs +++ b/src/ImageSharp/Processing/Processors/Effects/PositionAwarePixelShaderProcessor{TPixel}.cs @@ -28,7 +28,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Effects /// The source for the current processor instance. /// The source area to process for the current processor instance. public PositionAwarePixelShaderProcessor(PositionAwarePixelShaderProcessor definition, Image source, Rectangle sourceRectangle) - : base(source, sourceRectangle) + : base(definition.Modifiers, source, sourceRectangle) { this.pixelShader = definition.PixelShader; }