mirror of https://github.com/SixLabors/ImageSharp
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
105 lines
4.6 KiB
105 lines
4.6 KiB
// Copyright (c) Six Labors and contributors.
|
|
// Licensed under the Apache License, Version 2.0.
|
|
|
|
using System;
|
|
using System.Numerics;
|
|
using System.Runtime.CompilerServices;
|
|
using SixLabors.ImageSharp.Advanced;
|
|
using SixLabors.ImageSharp.Memory;
|
|
using SixLabors.ImageSharp.PixelFormats;
|
|
|
|
namespace SixLabors.ImageSharp.Processing.Processors.Effects
|
|
{
|
|
/// <summary>
|
|
/// The base class for all processors that accept a user defined row processing delegate.
|
|
/// </summary>
|
|
/// <typeparam name="TPixel">The pixel format.</typeparam>
|
|
/// <typeparam name="TDelegate">The row processor type.</typeparam>
|
|
internal sealed class PixelRowDelegateProcessor<TPixel, TDelegate> : ImageProcessor<TPixel>
|
|
where TPixel : struct, IPixel<TPixel>
|
|
where TDelegate : struct, IPixelRowDelegate
|
|
{
|
|
private readonly TDelegate rowDelegate;
|
|
|
|
/// <summary>
|
|
/// The <see cref="PixelConversionModifiers"/> to apply during the pixel conversions.
|
|
/// </summary>
|
|
private readonly PixelConversionModifiers modifiers;
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="PixelRowDelegateProcessor{TPixel,TDelegate}"/> class.
|
|
/// </summary>
|
|
/// <param name="rowDelegate">The row processor to use to process each pixel row</param>
|
|
/// <param name="configuration">The configuration which allows altering default behaviour or extending the library.</param>
|
|
/// <param name="modifiers">The <see cref="PixelConversionModifiers"/> to apply during the pixel conversions.</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 PixelRowDelegateProcessor(
|
|
in TDelegate rowDelegate,
|
|
Configuration configuration,
|
|
PixelConversionModifiers modifiers,
|
|
Image<TPixel> source,
|
|
Rectangle sourceRectangle)
|
|
: base(configuration, source, sourceRectangle)
|
|
{
|
|
this.rowDelegate = rowDelegate;
|
|
this.modifiers = modifiers;
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
protected override void OnFrameApply(ImageFrame<TPixel> source)
|
|
{
|
|
var interest = Rectangle.Intersect(this.SourceRectangle, source.Bounds());
|
|
|
|
ParallelRowIterator.IterateRows<RowIntervalOperation, Vector4>(
|
|
interest,
|
|
this.Configuration,
|
|
new RowIntervalOperation(interest.X, source, this.Configuration, this.modifiers, this.rowDelegate));
|
|
}
|
|
|
|
/// <summary>
|
|
/// A <see langword="struct"/> implementing the convolution logic for <see cref="PixelRowDelegateProcessor{TPixel,TDelegate}"/>.
|
|
/// </summary>
|
|
private readonly struct RowIntervalOperation : IRowIntervalOperation<Vector4>
|
|
{
|
|
private readonly int startX;
|
|
private readonly ImageFrame<TPixel> source;
|
|
private readonly Configuration configuration;
|
|
private readonly PixelConversionModifiers modifiers;
|
|
private readonly TDelegate rowProcessor;
|
|
|
|
[MethodImpl(InliningOptions.ShortMethod)]
|
|
public RowIntervalOperation(
|
|
int startX,
|
|
ImageFrame<TPixel> source,
|
|
Configuration configuration,
|
|
PixelConversionModifiers modifiers,
|
|
in TDelegate rowProcessor)
|
|
{
|
|
this.startX = startX;
|
|
this.source = source;
|
|
this.configuration = configuration;
|
|
this.modifiers = modifiers;
|
|
this.rowProcessor = rowProcessor;
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
[MethodImpl(InliningOptions.ShortMethod)]
|
|
public void Invoke(in RowInterval rows, Memory<Vector4> memory)
|
|
{
|
|
for (int y = rows.Min; y < rows.Max; y++)
|
|
{
|
|
Span<Vector4> vectorSpan = memory.Span;
|
|
int length = vectorSpan.Length;
|
|
Span<TPixel> rowSpan = this.source.GetPixelRowSpan(y).Slice(this.startX, length);
|
|
PixelOperations<TPixel>.Instance.ToVector4(this.configuration, rowSpan, vectorSpan, this.modifiers);
|
|
|
|
// Run the user defined pixel shader to the current row of pixels
|
|
Unsafe.AsRef(this.rowProcessor).Invoke(vectorSpan, new Point(this.startX, y));
|
|
|
|
PixelOperations<TPixel>.Instance.FromVector4Destructive(this.configuration, vectorSpan, rowSpan, this.modifiers);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|