Browse Source

Update VignetteProcessor{TPixel}.cs

af/octree-no-pixelmap
James Jackson-South 6 years ago
parent
commit
5f64e371d5
  1. 136
      src/ImageSharp/Processing/Processors/Overlays/VignetteProcessor{TPixel}.cs

136
src/ImageSharp/Processing/Processors/Overlays/VignetteProcessor{TPixel}.cs

@ -4,6 +4,7 @@
using System; using System;
using System.Buffers; using System.Buffers;
using System.Numerics; using System.Numerics;
using System.Runtime.CompilerServices;
using SixLabors.ImageSharp.Advanced; using SixLabors.ImageSharp.Advanced;
using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.PixelFormats;
@ -18,7 +19,6 @@ namespace SixLabors.ImageSharp.Processing.Processors.Overlays
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
private readonly PixelBlender<TPixel> blender; private readonly PixelBlender<TPixel> blender;
private readonly VignetteProcessor definition; private readonly VignetteProcessor definition;
/// <summary> /// <summary>
@ -38,80 +38,94 @@ namespace SixLabors.ImageSharp.Processing.Processors.Overlays
/// <inheritdoc/> /// <inheritdoc/>
protected override void OnFrameApply(ImageFrame<TPixel> source) protected override void OnFrameApply(ImageFrame<TPixel> source)
{ {
int startY = this.SourceRectangle.Y;
int endY = this.SourceRectangle.Bottom;
int startX = this.SourceRectangle.X;
int endX = this.SourceRectangle.Right;
TPixel vignetteColor = this.definition.VignetteColor.ToPixel<TPixel>(); TPixel vignetteColor = this.definition.VignetteColor.ToPixel<TPixel>();
Vector2 centre = Rectangle.Center(this.SourceRectangle); float blendPercent = this.definition.GraphicsOptions.BlendPercentage;
var interest = Rectangle.Intersect(this.SourceRectangle, source.Bounds());
Vector2 center = Rectangle.Center(interest);
float finalRadiusX = this.definition.RadiusX.Calculate(interest.Size);
float finalRadiusY = this.definition.RadiusY.Calculate(interest.Size);
Size sourceSize = source.Size();
float finalRadiusX = this.definition.RadiusX.Calculate(sourceSize);
float finalRadiusY = this.definition.RadiusY.Calculate(sourceSize);
float rX = finalRadiusX > 0 float rX = finalRadiusX > 0
? MathF.Min(finalRadiusX, this.SourceRectangle.Width * .5F) ? MathF.Min(finalRadiusX, interest.Width * .5F)
: this.SourceRectangle.Width * .5F; : interest.Width * .5F;
float rY = finalRadiusY > 0 float rY = finalRadiusY > 0
? MathF.Min(finalRadiusY, this.SourceRectangle.Height * .5F) ? MathF.Min(finalRadiusY, interest.Height * .5F)
: this.SourceRectangle.Height * .5F; : interest.Height * .5F;
float maxDistance = MathF.Sqrt((rX * rX) + (rY * rY)); float maxDistance = MathF.Sqrt((rX * rX) + (rY * rY));
// Align start/end positions. Configuration configuration = this.Configuration;
int minX = Math.Max(0, startX); MemoryAllocator allocator = configuration.MemoryAllocator;
int maxX = Math.Min(source.Width, endX);
int minY = Math.Max(0, startY);
int maxY = Math.Min(source.Height, endY);
// Reset offset if necessary. using (IMemoryOwner<TPixel> rowColors = allocator.Allocate<TPixel>(interest.Width))
if (minX > 0)
{ {
startX = 0; rowColors.GetSpan().Fill(vignetteColor);
ParallelRowIterator.IterateRows<RowIntervalAction, float>(
interest,
configuration,
new RowIntervalAction(configuration, interest, rowColors, this.blender, center, maxDistance, blendPercent, source));
} }
}
if (minY > 0) private readonly struct RowIntervalAction : IRowIntervalAction<float>
{
private readonly Configuration configuration;
private readonly Rectangle bounds;
private readonly PixelBlender<TPixel> blender;
private readonly Vector2 center;
private readonly float maxDistance;
private readonly float blendPercent;
private readonly IMemoryOwner<TPixel> colors;
private readonly ImageFrame<TPixel> source;
[MethodImpl(InliningOptions.ShortMethod)]
public RowIntervalAction(
Configuration configuration,
Rectangle bounds,
IMemoryOwner<TPixel> colors,
PixelBlender<TPixel> blender,
Vector2 center,
float maxDistance,
float blendPercent,
ImageFrame<TPixel> source)
{ {
startY = 0; this.configuration = configuration;
this.bounds = bounds;
this.colors = colors;
this.blender = blender;
this.center = center;
this.maxDistance = maxDistance;
this.blendPercent = blendPercent;
this.source = source;
} }
int width = maxX - minX; [MethodImpl(InliningOptions.ShortMethod)]
int offsetX = minX - startX; public void Invoke(in RowInterval rows, Memory<float> memory)
var workingRect = Rectangle.FromLTRB(minX, minY, maxX, maxY);
float blendPercentage = this.definition.GraphicsOptions.BlendPercentage;
Configuration configuration = this.Configuration;
MemoryAllocator memoryAllocator = configuration.MemoryAllocator;
using (IMemoryOwner<TPixel> rowColors = memoryAllocator.Allocate<TPixel>(width))
{ {
rowColors.GetSpan().Fill(vignetteColor); Span<float> amountsSpan = memory.Span;
Span<TPixel> colorSpan = this.colors.GetSpan();
ParallelRowIterator.IterateRows<float>(
workingRect, for (int y = rows.Min; y < rows.Max; y++)
configuration, {
(rows, amounts) => for (int i = 0; i < this.bounds.Width; i++)
{ {
Span<float> amountsSpan = amounts.Span; float distance = Vector2.Distance(this.center, new Vector2(i + this.bounds.X, y));
amountsSpan[i] = (this.blendPercent * (.9F * (distance / this.maxDistance))).Clamp(0, 1);
for (int y = rows.Min; y < rows.Max; y++) }
{
int offsetY = y - startY; Span<TPixel> destination = this.source.GetPixelRowSpan(y).Slice(this.bounds.X, this.bounds.Width);
for (int i = 0; i < width; i++) this.blender.Blend(
{ this.configuration,
float distance = Vector2.Distance(centre, new Vector2(i + offsetX, offsetY)); destination,
amountsSpan[i] = (blendPercentage * (.9F * (distance / maxDistance))).Clamp(0, 1); destination,
} colorSpan,
amountsSpan);
Span<TPixel> destination = source.GetPixelRowSpan(offsetY).Slice(offsetX, width); }
this.blender.Blend(
configuration,
destination,
destination,
rowColors.GetSpan(),
amountsSpan);
}
});
} }
} }
} }

Loading…
Cancel
Save