Browse Source

Minor code refactoring

af/merge-core
Sergio Pedri 6 years ago
parent
commit
e081fd08dc
  1. 64
      src/ImageSharp/Processing/Processors/PixelShading/Abstract/PixelShaderProcessorBase.cs
  2. 31
      src/ImageSharp/Processing/Processors/PixelShading/PixelShaderProcessor{TPixel}.cs
  3. 30
      src/ImageSharp/Processing/Processors/PixelShading/PositionAwarePixelShaderProcessor{TPixel}.cs

64
src/ImageSharp/Processing/Processors/PixelShading/Abstract/PixelShaderProcessorBase.cs

@ -0,0 +1,64 @@
// 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.Primitives;
namespace SixLabors.ImageSharp.Processing.Processors.PixelShading.Abstract
{
/// <summary>
/// Applies a user defined pixel shader effect through a given delegate.
/// </summary>
/// <typeparam name="TPixel">The pixel format.</typeparam>
internal abstract class PixelShaderProcessorBase<TPixel> : ImageProcessor<TPixel>
where TPixel : struct, IPixel<TPixel>
{
/// <summary>
/// Initializes a new instance of the <see cref="PixelShaderProcessorBase{TPixel}"/> class.
/// </summary>
/// <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>
protected PixelShaderProcessorBase(Image<TPixel> source, Rectangle sourceRectangle)
: base(source, sourceRectangle)
{ }
/// <inheritdoc/>
protected override void OnFrameApply(ImageFrame<TPixel> source)
{
var interest = Rectangle.Intersect(this.SourceRectangle, source.Bounds());
int startX = interest.X;
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
this.ApplyPixelShader(vectorSpan, y, startX);
PixelOperations<TPixel>.Instance.FromVector4Destructive(this.Configuration, vectorSpan, rowSpan);
}
});
}
/// <summary>
/// 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);
}
}

31
src/ImageSharp/Processing/Processors/PixelShading/PixelShaderProcessor{TPixel}.cs

@ -4,10 +4,9 @@
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.ImageSharp.Processing.Processors.PixelShading.Abstract;
using SixLabors.Primitives;
namespace SixLabors.ImageSharp.Processing.Processors.PixelShading
@ -16,7 +15,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.PixelShading
/// Applies a user defined pixel shader effect through a given delegate.
/// </summary>
/// <typeparam name="TPixel">The pixel format.</typeparam>
internal class PixelShaderProcessor<TPixel> : ImageProcessor<TPixel>
internal class PixelShaderProcessor<TPixel> : PixelShaderProcessorBase<TPixel>
where TPixel : struct, IPixel<TPixel>
{
/// <summary>
@ -37,30 +36,6 @@ namespace SixLabors.ImageSharp.Processing.Processors.PixelShading
}
/// <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);
}
});
}
protected override void ApplyPixelShader(Span<Vector4> span, int offsetY, int offsetX) => this.pixelShader(span);
}
}

30
src/ImageSharp/Processing/Processors/PixelShading/PositionAwarePixelShaderProcessor{TPixel}.cs

@ -4,10 +4,9 @@
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.ImageSharp.Processing.Processors.PixelShading.Abstract;
using SixLabors.Primitives;
namespace SixLabors.ImageSharp.Processing.Processors.PixelShading
@ -16,7 +15,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.PixelShading
/// 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>
internal class PositionAwarePixelShaderProcessor<TPixel> : PixelShaderProcessorBase<TPixel>
where TPixel : struct, IPixel<TPixel>
{
/// <summary>
@ -37,29 +36,6 @@ namespace SixLabors.ImageSharp.Processing.Processors.PixelShading
}
/// <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);
}
});
}
protected override void ApplyPixelShader(Span<Vector4> span, int offsetY, int offsetX) => this.pixelShader(span, offsetY, offsetX);
}
}

Loading…
Cancel
Save