Browse Source

Added fast path for SolidBrush in FillProcessor.

af/merge-core
woutware 8 years ago
parent
commit
3feccfa6ff
  1. 21
      src/ImageSharp.Drawing/Processing/Drawing/Brushes/SolidBrush{TPixel}.cs
  2. 66
      src/ImageSharp.Drawing/Processing/Drawing/Processors/FillProcessor.cs

21
src/ImageSharp.Drawing/Processing/Drawing/Brushes/SolidBrush{TPixel}.cs

@ -90,16 +90,23 @@ namespace SixLabors.ImageSharp.Processing.Drawing.Brushes
MemoryManager memoryManager = this.Target.MemoryManager; MemoryManager memoryManager = this.Target.MemoryManager;
using (IBuffer<float> amountBuffer = memoryManager.Allocate<float>(scanline.Length)) if (this.Options.BlendPercentage == 1f)
{ {
Span<float> amountSpan = amountBuffer.Span; this.Blender.Blend(memoryManager, destinationRow, destinationRow, this.Colors.Span, scanline);
}
for (int i = 0; i < scanline.Length; i++) else
{
using (IBuffer<float> amountBuffer = memoryManager.Allocate<float>(scanline.Length))
{ {
amountSpan[i] = scanline[i] * this.Options.BlendPercentage; Span<float> amountSpan = amountBuffer.Span;
}
for (int i = 0; i < scanline.Length; i++)
{
amountSpan[i] = scanline[i] * this.Options.BlendPercentage;
}
this.Blender.Blend(memoryManager, destinationRow, destinationRow, this.Colors.Span, amountSpan); this.Blender.Blend(memoryManager, destinationRow, destinationRow, this.Colors.Span, amountSpan);
}
} }
} }
} }

66
src/ImageSharp.Drawing/Processing/Drawing/Processors/FillProcessor.cs

@ -3,6 +3,7 @@
using System; using System;
using System.Threading.Tasks; using System.Threading.Tasks;
using SixLabors.ImageSharp.Advanced;
using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing.Drawing.Brushes; using SixLabors.ImageSharp.Processing.Drawing.Brushes;
@ -49,38 +50,57 @@ namespace SixLabors.ImageSharp.Processing.Drawing.Processors
int minY = Math.Max(0, startY); int minY = Math.Max(0, startY);
int maxY = Math.Min(source.Height, endY); int maxY = Math.Min(source.Height, endY);
// Reset offset if necessary.
if (minX > 0)
{
startX = 0;
}
if (minY > 0)
{
startY = 0;
}
int width = maxX - minX; int width = maxX - minX;
using (IBuffer<float> amount = source.MemoryManager.Allocate<float>(width)) var solidBrush = this.brush as SolidBrush<TPixel>;
using (BrushApplicator<TPixel> applicator = this.brush.CreateApplicator(
source,
sourceRectangle,
this.options))
{
amount.Span.Fill(this.options.BlendPercentage);
// If there's no reason for blending, then avoid it.
if (solidBrush != null && this.options.BlendPercentage == 1f && solidBrush.Color.ToVector4().Z == 1f)
{
Parallel.For( Parallel.For(
minY, minY,
maxY, maxY,
configuration.ParallelOptions, configuration.ParallelOptions,
y => y =>
{ {
int offsetY = y - startY; int offsetY = y - startY;
int offsetX = minX - startX; int offsetX = minX - startX;
source.GetPixelRowSpan(y).Slice(minX, width).Fill(solidBrush.Color);
});
}
else
{
// Reset offset if necessary.
if (minX > 0)
{
startX = 0;
}
if (minY > 0)
{
startY = 0;
}
using (IBuffer<float> amount = source.MemoryManager.Allocate<float>(width))
using (BrushApplicator<TPixel> applicator = this.brush.CreateApplicator(
source,
sourceRectangle,
this.options))
{
amount.Span.Fill(this.options.BlendPercentage);
Parallel.For(
minY,
maxY,
configuration.ParallelOptions,
y =>
{
int offsetY = y - startY;
int offsetX = minX - startX;
applicator.Apply(amount.Span, offsetX, offsetY); applicator.Apply(amount.Span, offsetX, offsetY);
}); });
}
} }
} }
} }

Loading…
Cancel
Save