Browse Source

Optimize Glow/Vignette

af/merge-core
James Jackson-South 9 years ago
parent
commit
5ad15fe31b
  1. 41
      src/ImageSharp/Processing/Processors/Overlays/GlowProcessor.cs
  2. 9
      src/ImageSharp/Processing/Processors/Overlays/VignetteProcessor.cs

41
src/ImageSharp/Processing/Processors/Overlays/GlowProcessor.cs

@ -52,7 +52,7 @@ namespace ImageSharp.Processing.Processors
int startX = sourceRectangle.X; int startX = sourceRectangle.X;
int endX = sourceRectangle.Right; int endX = sourceRectangle.Right;
TPixel glowColor = this.GlowColor; TPixel glowColor = this.GlowColor;
Vector2 centre = Rectangle.Center(sourceRectangle).ToVector2(); var centre = Rectangle.Center(sourceRectangle).ToVector2();
float maxDistance = this.Radius > 0 ? MathF.Min(this.Radius, sourceRectangle.Width * .5F) : sourceRectangle.Width * .5F; float maxDistance = this.Radius > 0 ? MathF.Min(this.Radius, sourceRectangle.Width * .5F) : sourceRectangle.Width * .5F;
// Align start/end positions. // Align start/end positions.
@ -73,8 +73,7 @@ namespace ImageSharp.Processing.Processors
} }
int width = maxX - minX; int width = maxX - minX;
using (Buffer<TPixel> rowColors = new Buffer<TPixel>(width)) using (var rowColors = new Buffer<TPixel>(width))
using (PixelAccessor<TPixel> sourcePixels = source.Lock())
{ {
for (int i = 0; i < width; i++) for (int i = 0; i < width; i++)
{ {
@ -82,26 +81,26 @@ namespace ImageSharp.Processing.Processors
} }
Parallel.For( Parallel.For(
minY, minY,
maxY, maxY,
this.ParallelOptions, this.ParallelOptions,
y => y =>
{ {
using (Buffer<float> amounts = new Buffer<float>(width)) using (var amounts = new Buffer<float>(width))
{ {
int offsetY = y - startY; int offsetY = y - startY;
int offsetX = minX - startX; int offsetX = minX - startX;
for (int i = 0; i < width; i++) for (int i = 0; i < width; i++)
{ {
float distance = Vector2.Distance(centre, new Vector2(i + offsetX, offsetY)); float distance = Vector2.Distance(centre, new Vector2(i + offsetX, offsetY));
amounts[i] = (this.options.BlendPercentage * (1 - (.95F * (distance / maxDistance)))).Clamp(0, 1); amounts[i] = (this.options.BlendPercentage * (1 - (.95F * (distance / maxDistance)))).Clamp(0, 1);
} }
Span<TPixel> destination = sourcePixels.GetRowSpan(offsetY).Slice(offsetX, width); Span<TPixel> destination = source.GetRowSpan(offsetY).Slice(offsetX, width);
this.blender.Blend(destination, destination, rowColors, amounts); this.blender.Blend(destination, destination, rowColors, amounts);
} }
}); });
} }
} }
} }

9
src/ImageSharp/Processing/Processors/Overlays/VignetteProcessor.cs

@ -58,7 +58,7 @@ namespace ImageSharp.Processing.Processors
int startX = sourceRectangle.X; int startX = sourceRectangle.X;
int endX = sourceRectangle.Right; int endX = sourceRectangle.Right;
TPixel vignetteColor = this.VignetteColor; TPixel vignetteColor = this.VignetteColor;
Vector2 centre = Rectangle.Center(sourceRectangle).ToVector2(); var centre = Rectangle.Center(sourceRectangle).ToVector2();
float rX = this.RadiusX > 0 ? MathF.Min(this.RadiusX, sourceRectangle.Width * .5F) : sourceRectangle.Width * .5F; float rX = this.RadiusX > 0 ? MathF.Min(this.RadiusX, sourceRectangle.Width * .5F) : sourceRectangle.Width * .5F;
float rY = this.RadiusY > 0 ? MathF.Min(this.RadiusY, sourceRectangle.Height * .5F) : sourceRectangle.Height * .5F; float rY = this.RadiusY > 0 ? MathF.Min(this.RadiusY, sourceRectangle.Height * .5F) : sourceRectangle.Height * .5F;
float maxDistance = MathF.Sqrt((rX * rX) + (rY * rY)); float maxDistance = MathF.Sqrt((rX * rX) + (rY * rY));
@ -81,8 +81,7 @@ namespace ImageSharp.Processing.Processors
} }
int width = maxX - minX; int width = maxX - minX;
using (Buffer<TPixel> rowColors = new Buffer<TPixel>(width)) using (var rowColors = new Buffer<TPixel>(width))
using (PixelAccessor<TPixel> sourcePixels = source.Lock())
{ {
for (int i = 0; i < width; i++) for (int i = 0; i < width; i++)
{ {
@ -95,7 +94,7 @@ namespace ImageSharp.Processing.Processors
this.ParallelOptions, this.ParallelOptions,
y => y =>
{ {
using (Buffer<float> amounts = new Buffer<float>(width)) using (var amounts = new Buffer<float>(width))
{ {
int offsetY = y - startY; int offsetY = y - startY;
int offsetX = minX - startX; int offsetX = minX - startX;
@ -105,7 +104,7 @@ namespace ImageSharp.Processing.Processors
amounts[i] = (this.options.BlendPercentage * (.9F * (distance / maxDistance))).Clamp(0, 1); amounts[i] = (this.options.BlendPercentage * (.9F * (distance / maxDistance))).Clamp(0, 1);
} }
Span<TPixel> destination = sourcePixels.GetRowSpan(offsetY).Slice(offsetX, width); Span<TPixel> destination = source.GetRowSpan(offsetY).Slice(offsetX, width);
this.blender.Blend(destination, destination, rowColors, amounts); this.blender.Blend(destination, destination, rowColors, amounts);
} }

Loading…
Cancel
Save