Browse Source

Added initial version of the pixel shader delegate and processors

af/merge-core
Sergio Pedri 6 years ago
parent
commit
1bd8a734ea
  1. 12
      src/ImageSharp/Processing/Delegates/PixelShader.cs
  2. 38
      src/ImageSharp/Processing/Processors/DelegatePixelShaderProcessor.cs
  3. 66
      src/ImageSharp/Processing/Processors/DelegatePixelShaderProcessor{TPixel}.cs

12
src/ImageSharp/Processing/Delegates/PixelShader.cs

@ -0,0 +1,12 @@
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>
/// <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 PixelShader(Span<Vector4> span);
}

38
src/ImageSharp/Processing/Processors/DelegatePixelShaderProcessor.cs

@ -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
{
/// <summary>
/// Applies a user defined pixel shader effect through a given delegate.
/// </summary>
public sealed class DelegatePixelShaderProcessor : IImageProcessor
{
/// <summary>
/// Initializes a new instance of the <see cref="DelegatePixelShaderProcessor"/> class.
/// </summary>
/// <param name="pixelShader">
/// The user defined pixel shader to use to modify images.
/// </param>
public DelegatePixelShaderProcessor(PixelShader pixelShader)
{
this.PixelShader = pixelShader;
}
/// <summary>
/// Gets the user defined pixel shader.
/// </summary>
public PixelShader PixelShader { get; }
/// <inheritdoc />
public IImageProcessor<TPixel> CreatePixelSpecificProcessor<TPixel>(Image<TPixel> source, Rectangle sourceRectangle)
where TPixel : struct, IPixel<TPixel>
{
return new DelegatePixelShaderProcessor<TPixel>(this, source, sourceRectangle);
}
}
}

66
src/ImageSharp/Processing/Processors/DelegatePixelShaderProcessor{TPixel}.cs

@ -0,0 +1,66 @@
// 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
{
/// <summary>
/// Performs simple binary threshold filtering against an image.
/// </summary>
/// <typeparam name="TPixel">The pixel format.</typeparam>
internal class DelegatePixelShaderProcessor<TPixel> : ImageProcessor<TPixel>
where TPixel : struct, IPixel<TPixel>
{
/// <summary>
/// The user defined pixel shader.
/// </summary>
private readonly PixelShader pixelShader;
/// <summary>
/// Initializes a new instance of the <see cref="DelegatePixelShaderProcessor{TPixel}"/> class.
/// </summary>
/// <param name="definition">The <see cref="DelegatePixelShaderProcessor"/> 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 DelegatePixelShaderProcessor(DelegatePixelShaderProcessor 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;
PixelShader 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);
// Run the user defined pixel shader on the current row of pixels
pixelShader(vectorSpan);
PixelOperations<TPixel>.Instance.FromVector4Destructive(this.Configuration, vectorSpan, rowSpan);
}
});
}
}
}
Loading…
Cancel
Save