Browse Source

Update GlowProcessor{TPixel}.cs

pull/1108/head
James Jackson-South 6 years ago
parent
commit
c01889f11f
  1. 128
      src/ImageSharp/Processing/Processors/Overlays/GlowProcessor{TPixel}.cs

128
src/ImageSharp/Processing/Processors/Overlays/GlowProcessor{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;
@ -37,78 +38,83 @@ namespace SixLabors.ImageSharp.Processing.Processors.Overlays
/// <inheritdoc/> /// <inheritdoc/>
protected override void OnFrameApply(ImageFrame<TPixel> source) protected override void OnFrameApply(ImageFrame<TPixel> source)
{ {
// TODO: can we simplify the rectangle calculation?
int startY = this.SourceRectangle.Y;
int endY = this.SourceRectangle.Bottom;
int startX = this.SourceRectangle.X;
int endX = this.SourceRectangle.Right;
TPixel glowColor = this.definition.GlowColor.ToPixel<TPixel>(); TPixel glowColor = this.definition.GlowColor.ToPixel<TPixel>();
Vector2 center = Rectangle.Center(this.SourceRectangle); float blendPercent = this.definition.GraphicsOptions.BlendPercentage;
float finalRadius = this.definition.Radius.Calculate(source.Size()); var interest = Rectangle.Intersect(this.SourceRectangle, source.Bounds());
Vector2 center = Rectangle.Center(interest);
float finalRadius = this.definition.Radius.Calculate(interest.Size);
float maxDistance = finalRadius > 0 float maxDistance = finalRadius > 0
? MathF.Min(finalRadius, this.SourceRectangle.Width * .5F) ? MathF.Min(finalRadius, interest.Width * .5F)
: this.SourceRectangle.Width * .5F; : interest.Width * .5F;
// 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) rowColors.GetSpan().Fill(glowColor);
{
startX = 0; 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(glowColor); Span<float> amountsSpan = memory.Span;
ParallelRowIterator.IterateRows<float>( for (int y = rows.Min; y < rows.Max; y++)
workingRect, {
configuration, for (int i = 0; i < this.bounds.Width; i++)
(rows, amounts) => {
{ float distance = Vector2.Distance(this.center, new Vector2(i + this.bounds.X, y));
Span<float> amountsSpan = amounts.Span; amountsSpan[i] = (this.blendPercent * (1 - (.95F * (distance / this.maxDistance)))).Clamp(0, 1);
}
for (int y = rows.Min; y < rows.Max; y++)
{ Span<TPixel> destination = this.source.GetPixelRowSpan(y).Slice(this.bounds.X, this.bounds.Width);
int offsetY = y - startY;
this.blender.Blend(
for (int i = 0; i < width; i++) this.configuration,
{ destination,
float distance = Vector2.Distance(center, new Vector2(i + offsetX, offsetY)); destination,
amountsSpan[i] = this.colors.GetSpan(),
(blendPercentage * (1 - (.95F * (distance / maxDistance)))).Clamp(0, 1); amountsSpan);
} }
Span<TPixel> destination = source.GetPixelRowSpan(offsetY).Slice(offsetX, width);
this.blender.Blend(
configuration,
destination,
destination,
rowColors.GetSpan(),
amountsSpan);
}
});
} }
} }
} }

Loading…
Cancel
Save