Browse Source

use ParallelHelper in BackgroundColorProcessor

af/merge-core
Anton Firszov 8 years ago
parent
commit
63db600074
  1. 38
      src/ImageSharp/Processing/Processors/Overlays/BackgroundColorProcessor.cs

38
src/ImageSharp/Processing/Processors/Overlays/BackgroundColorProcessor.cs

@ -6,6 +6,7 @@ using System.Buffers;
using System.Threading.Tasks; using System.Threading.Tasks;
using SixLabors.ImageSharp.Advanced; using SixLabors.ImageSharp.Advanced;
using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.ParallelUtils;
using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.PixelFormats;
using SixLabors.Memory; using SixLabors.Memory;
using SixLabors.Primitives; using SixLabors.Primitives;
@ -67,6 +68,8 @@ namespace SixLabors.ImageSharp.Processing.Processors.Overlays
int width = maxX - minX; int width = maxX - minX;
var workingRect = Rectangle.FromLTRB(minX, minY, maxX, maxY);
using (IMemoryOwner<TPixel> colors = source.MemoryAllocator.Allocate<TPixel>(width)) using (IMemoryOwner<TPixel> colors = source.MemoryAllocator.Allocate<TPixel>(width))
using (IMemoryOwner<float> amount = source.MemoryAllocator.Allocate<float>(width)) using (IMemoryOwner<float> amount = source.MemoryAllocator.Allocate<float>(width))
{ {
@ -74,25 +77,30 @@ namespace SixLabors.ImageSharp.Processing.Processors.Overlays
Span<TPixel> colorSpan = colors.GetSpan(); Span<TPixel> colorSpan = colors.GetSpan();
Span<float> amountSpan = amount.GetSpan(); Span<float> amountSpan = amount.GetSpan();
// TODO: Use Span.Fill? colorSpan.Fill(this.Color);
for (int i = 0; i < width; i++) amountSpan.Fill(this.GraphicsOptions.BlendPercentage);
{
colorSpan[i] = this.Color;
amountSpan[i] = this.GraphicsOptions.BlendPercentage;
}
PixelBlender<TPixel> blender = PixelOperations<TPixel>.Instance.GetPixelBlender(this.GraphicsOptions); PixelBlender<TPixel> blender = PixelOperations<TPixel>.Instance.GetPixelBlender(this.GraphicsOptions);
ParallelFor.WithConfiguration(
minY, ParallelHelper.IterateRows(
maxY, workingRect,
configuration, configuration,
y => rows =>
{ {
Span<TPixel> destination = source.GetPixelRowSpan(y - startY).Slice(minX - startX, width); for (int y = rows.Min; y < rows.Max; y++)
{
Span<TPixel> destination =
source.GetPixelRowSpan(y - startY).Slice(minX - startX, width);
// This switched color & destination in the 2nd and 3rd places because we are applying the target color under the current one // This switched color & destination in the 2nd and 3rd places because we are applying the target color under the current one
blender.Blend(source.MemoryAllocator, destination, colors.GetSpan(), destination, amount.GetSpan()); blender.Blend(
}); source.MemoryAllocator,
destination,
colors.GetSpan(),
destination,
amount.GetSpan());
}
});
} }
} }
} }

Loading…
Cancel
Save