mirror of https://github.com/SixLabors/ImageSharp
4 changed files with 118 additions and 1 deletions
@ -0,0 +1,14 @@ |
|||
using System; |
|||
using System.Numerics; |
|||
|
|||
namespace SixLabors.ImageSharp.Processing.Delegates |
|||
{ |
|||
/// <summary>
|
|||
/// A <see langword="delegate"/> representing a user defined pixel shader.
|
|||
/// </summary>
|
|||
/// <param name="span">The target row of <see cref="Vector4"/> pixels to process.</param>
|
|||
/// <param name="offsetY">The initial vertical offset for the input pixels to process.</param>
|
|||
/// <param name="offsetX">The initial horizontal offset for the input pixels to process.</param>
|
|||
/// <remarks>The <see cref="Vector4.X"/>, <see cref="Vector4.Y"/>, <see cref="Vector4.Z"/>, and <see cref="Vector4.W"/> fields map the RGBA channels respectively.</remarks>
|
|||
public delegate void PositionAwarePixelShader(Span<Vector4> span, int offsetY, int offsetX); |
|||
} |
|||
@ -0,0 +1,38 @@ |
|||
// Copyright (c) Six Labors and contributors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
|
|||
using SixLabors.ImageSharp.PixelFormats; |
|||
using SixLabors.ImageSharp.Processing.Delegates; |
|||
using SixLabors.Primitives; |
|||
|
|||
namespace SixLabors.ImageSharp.Processing.Processors.PixelShading |
|||
{ |
|||
/// <summary>
|
|||
/// Applies a user defined pixel shader effect through a given delegate.
|
|||
/// </summary>
|
|||
public sealed class PositionAwarePixelShaderProcessor : IImageProcessor |
|||
{ |
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="PositionAwarePixelShaderProcessor"/> class.
|
|||
/// </summary>
|
|||
/// <param name="pixelShader">
|
|||
/// The user defined pixel shader to use to modify images.
|
|||
/// </param>
|
|||
public PositionAwarePixelShaderProcessor(PositionAwarePixelShader pixelShader) |
|||
{ |
|||
this.PixelShader = pixelShader; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Gets the user defined pixel shader.
|
|||
/// </summary>
|
|||
public PositionAwarePixelShader PixelShader { get; } |
|||
|
|||
/// <inheritdoc />
|
|||
public IImageProcessor<TPixel> CreatePixelSpecificProcessor<TPixel>(Image<TPixel> source, Rectangle sourceRectangle) |
|||
where TPixel : struct, IPixel<TPixel> |
|||
{ |
|||
return new PositionAwarePixelShader<TPixel>(this, source, sourceRectangle); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,65 @@ |
|||
// Copyright (c) Six Labors and contributors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
|
|||
using System; |
|||
using System.Numerics; |
|||
|
|||
using SixLabors.ImageSharp.Advanced; |
|||
using SixLabors.ImageSharp.Advanced.ParallelUtils; |
|||
using SixLabors.ImageSharp.PixelFormats; |
|||
using SixLabors.ImageSharp.Processing.Delegates; |
|||
using SixLabors.Primitives; |
|||
|
|||
namespace SixLabors.ImageSharp.Processing.Processors.PixelShading |
|||
{ |
|||
/// <summary>
|
|||
/// Applies a user defined pixel shader effect through a given delegate.
|
|||
/// </summary>
|
|||
/// <typeparam name="TPixel">The pixel format.</typeparam>
|
|||
internal class PositionAwarePixelShaderProcessor<TPixel> : ImageProcessor<TPixel> |
|||
where TPixel : struct, IPixel<TPixel> |
|||
{ |
|||
/// <summary>
|
|||
/// The user defined pixel shader.
|
|||
/// </summary>
|
|||
private readonly PositionAwarePixelShader pixelShader; |
|||
|
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="PositionAwarePixelShaderProcessor{TPixel}"/> class.
|
|||
/// </summary>
|
|||
/// <param name="definition">The <see cref="PositionAwarePixelShaderProcessor"/> defining the processor parameters.</param>
|
|||
/// <param name="source">The source <see cref="Image{TPixel}"/> for the current processor instance.</param>
|
|||
/// <param name="sourceRectangle">The source area to process for the current processor instance.</param>
|
|||
public PositionAwarePixelShaderProcessor(PositionAwarePixelShaderProcessor definition, Image<TPixel> source, Rectangle sourceRectangle) |
|||
: base(source, sourceRectangle) |
|||
{ |
|||
this.pixelShader = definition.PixelShader; |
|||
} |
|||
|
|||
/// <inheritdoc/>
|
|||
protected override void OnFrameApply(ImageFrame<TPixel> source) |
|||
{ |
|||
var interest = Rectangle.Intersect(this.SourceRectangle, source.Bounds()); |
|||
int startX = interest.X; |
|||
PositionAwarePixelShader pixelShader = this.pixelShader; |
|||
|
|||
ParallelHelper.IterateRowsWithTempBuffer<Vector4>( |
|||
interest, |
|||
this.Configuration, |
|||
(rows, vectorBuffer) => |
|||
{ |
|||
for (int y = rows.Min; y < rows.Max; y++) |
|||
{ |
|||
Span<Vector4> vectorSpan = vectorBuffer.Span; |
|||
int length = vectorSpan.Length; |
|||
Span<TPixel> rowSpan = source.GetPixelRowSpan(y).Slice(startX, length); |
|||
PixelOperations<TPixel>.Instance.ToVector4(this.Configuration, rowSpan, vectorSpan); |
|||
|
|||
pixelShader(vectorSpan, y, startX); |
|||
|
|||
PixelOperations<TPixel>.Instance.FromVector4Destructive(this.Configuration, vectorSpan, rowSpan); |
|||
} |
|||
}); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue