Browse Source

Use Point for offset

af/merge-core
James Jackson-South 6 years ago
parent
commit
9b1db0c81f
  1. 0
      src/ImageSharp/Processing/Extensions/Effects/PixelShaderExtensions.cs
  2. 6
      src/ImageSharp/Processing/PositionAwarePixelShader.cs
  3. 7
      src/ImageSharp/Processing/Processors/Effects/PixelShaderProcessorBase.cs
  4. 4
      src/ImageSharp/Processing/Processors/Effects/PixelShaderProcessor{TPixel}.cs
  5. 4
      src/ImageSharp/Processing/Processors/Effects/PositionAwarePixelShaderProcessor{TPixel}.cs
  6. 14
      tests/ImageSharp.Tests/Processing/Processors/Effects/PixelShaderTest.cs

0
src/ImageSharp/Processing/Extensions/PixelShaderExtensions.cs → src/ImageSharp/Processing/Extensions/Effects/PixelShaderExtensions.cs

6
src/ImageSharp/Processing/PositionAwarePixelShader.cs

@ -3,6 +3,7 @@
using System;
using System.Numerics;
using SixLabors.Primitives;
namespace SixLabors.ImageSharp.Processing
{
@ -10,8 +11,7 @@ namespace SixLabors.ImageSharp.Processing
/// 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>
/// <param name="offset">The initial horizontal and vertical 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);
public delegate void PositionAwarePixelShader(Span<Vector4> span, Point offset);
}

7
src/ImageSharp/Processing/Processors/Effects/PixelShaderProcessorBase.cs

@ -56,7 +56,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Effects
PixelOperations<TPixel>.Instance.ToVector4(configuration, rowSpan, vectorSpan, modifiers);
// Run the user defined pixel shader on the current row of pixels
this.ApplyPixelShader(vectorSpan, y, startX);
this.ApplyPixelShader(vectorSpan, new Point(startX, y));
PixelOperations<TPixel>.Instance.FromVector4Destructive(configuration, vectorSpan, rowSpan, modifiers);
}
@ -67,8 +67,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Effects
/// Applies the current pixel shader effect on a target row of preprocessed pixels.
/// </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>
protected abstract void ApplyPixelShader(Span<Vector4> span, int offsetY, int offsetX);
/// <param name="offset">The initial horizontal and vertical offset for the input pixels to process.</param>
protected abstract void ApplyPixelShader(Span<Vector4> span, Point offset);
}
}

4
src/ImageSharp/Processing/Processors/Effects/PixelShaderProcessor{TPixel}.cs

@ -13,7 +13,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Effects
/// Applies a user defined pixel shader effect through a given delegate.
/// </summary>
/// <typeparam name="TPixel">The pixel format.</typeparam>
internal class PixelShaderProcessor<TPixel> : PixelShaderProcessorBase<TPixel>
internal sealed class PixelShaderProcessor<TPixel> : PixelShaderProcessorBase<TPixel>
where TPixel : struct, IPixel<TPixel>
{
/// <summary>
@ -34,6 +34,6 @@ namespace SixLabors.ImageSharp.Processing.Processors.Effects
}
/// <inheritdoc/>
protected override void ApplyPixelShader(Span<Vector4> span, int offsetY, int offsetX) => this.pixelShader(span);
protected override void ApplyPixelShader(Span<Vector4> span, Point offset) => this.pixelShader(span);
}
}

4
src/ImageSharp/Processing/Processors/Effects/PositionAwarePixelShaderProcessor{TPixel}.cs

@ -13,7 +13,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Effects
/// Applies a user defined pixel shader effect through a given delegate.
/// </summary>
/// <typeparam name="TPixel">The pixel format.</typeparam>
internal class PositionAwarePixelShaderProcessor<TPixel> : PixelShaderProcessorBase<TPixel>
internal sealed class PositionAwarePixelShaderProcessor<TPixel> : PixelShaderProcessorBase<TPixel>
where TPixel : struct, IPixel<TPixel>
{
/// <summary>
@ -34,6 +34,6 @@ namespace SixLabors.ImageSharp.Processing.Processors.Effects
}
/// <inheritdoc/>
protected override void ApplyPixelShader(Span<Vector4> span, int offsetY, int offsetX) => this.pixelShader(span, offsetY, offsetX);
protected override void ApplyPixelShader(Span<Vector4> span, Point offset) => this.pixelShader(span, offset);
}
}

14
tests/ImageSharp.Tests/Processing/Processors/Effects/PixelShaderTest.cs

@ -58,8 +58,10 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Effects
{
provider.RunValidatingProcessorTest(
c => c.ApplyPixelShaderProcessor(
(span, y, x) =>
(span, offset) =>
{
int y = offset.Y;
int x = offset.X;
for (int i = 0; i < span.Length; i++)
{
float
@ -67,8 +69,8 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Effects
cosine = MathF.Cos(x + i),
sum = sine + cosine,
abs = MathF.Abs(sum),
a = 0.5f + abs / 2; // Max value for sin(y) + cos(x) is 2
a = 0.5f + (abs / 2); // Max value for sin(y) + cos(x) is 2
Vector4 v4 = span[i];
float avg = (v4.X + v4.Y + v4.Z) / 3f;
var gray = new Vector4(avg, avg, avg, a);
@ -86,8 +88,10 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Effects
{
provider.RunRectangleConstrainedValidatingProcessorTest(
(c, rect) => c.ApplyPixelShaderProcessor(
(span, y, x) =>
(span, offset) =>
{
int y = offset.Y;
int x = offset.X;
for (int i = 0; i < span.Length; i++)
{
float
@ -95,7 +99,7 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Effects
cosine = MathF.Cos(x + i),
sum = sine + cosine,
abs = MathF.Abs(sum),
a = 0.5f + abs / 2;
a = 0.5f + (abs / 2);
Vector4 v4 = span[i];
float avg = (v4.X + v4.Y + v4.Z) / 3f;

Loading…
Cancel
Save