Browse Source

Update GlowProcessor{TPixel}.cs

af/octree-no-pixelmap
James Jackson-South 7 years ago
parent
commit
2fce671c2d
  1. 102
      src/ImageSharp/Processing/Processors/Overlays/GlowProcessor{TPixel}.cs

102
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;
}
if (minY > 0) ParallelRowIterator.IterateRows<RowIntervalAction, float>(
{ interest,
startY = 0; configuration,
new RowIntervalAction(configuration, interest, rowColors, this.blender, center, maxDistance, blendPercent, source));
} }
int width = maxX - minX; private readonly struct RowIntervalAction : IRowIntervalAction<float>
int offsetX = minX - startX;
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); 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)
{
this.configuration = configuration;
this.bounds = bounds;
this.colors = colors;
this.blender = blender;
this.center = center;
this.maxDistance = maxDistance;
this.blendPercent = blendPercent;
this.source = source;
}
ParallelRowIterator.IterateRows<float>( [MethodImpl(InliningOptions.ShortMethod)]
workingRect, public void Invoke(in RowInterval rows, Memory<float> memory)
configuration,
(rows, amounts) =>
{ {
Span<float> amountsSpan = amounts.Span; Span<float> amountsSpan = memory.Span;
for (int y = rows.Min; y < rows.Max; y++) for (int y = rows.Min; y < rows.Max; y++)
{ {
int offsetY = y - startY; for (int i = 0; i < this.bounds.Width; i++)
for (int i = 0; i < width; i++)
{ {
float distance = Vector2.Distance(center, new Vector2(i + offsetX, offsetY)); float distance = Vector2.Distance(this.center, new Vector2(i + this.bounds.X, y));
amountsSpan[i] = amountsSpan[i] = (this.blendPercent * (1 - (.95F * (distance / this.maxDistance)))).Clamp(0, 1);
(blendPercentage * (1 - (.95F * (distance / maxDistance)))).Clamp(0, 1);
} }
Span<TPixel> destination = source.GetPixelRowSpan(offsetY).Slice(offsetX, width); Span<TPixel> destination = this.source.GetPixelRowSpan(y).Slice(this.bounds.X, this.bounds.Width);
this.blender.Blend( this.blender.Blend(
configuration, this.configuration,
destination, destination,
destination, destination,
rowColors.GetSpan(), this.colors.GetSpan(),
amountsSpan); amountsSpan);
} }
});
} }
} }
} }

Loading…
Cancel
Save