// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.
using System;
using System.Numerics;
using System.Threading.Tasks;
using SixLabors.ImageSharp.Advanced;
using SixLabors.ImageSharp.Drawing;
using SixLabors.ImageSharp.Drawing.Brushes;
using SixLabors.ImageSharp.Drawing.Brushes.Processors;
using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
using SixLabors.Primitives;
namespace SixLabors.ImageSharp.Drawing.Processors
{
///
/// Using the bursh as a source of pixels colors blends the brush color with source.
///
/// The pixel format.
internal class FillProcessor : ImageProcessor
where TPixel : struct, IPixel
{
///
/// The brush.
///
private readonly IBrush brush;
private readonly GraphicsOptions options;
///
/// Initializes a new instance of the class.
///
/// The brush to source pixel colors from.
/// The options
public FillProcessor(IBrush brush, GraphicsOptions options)
{
this.brush = brush;
this.options = options;
}
///
protected override void OnApply(ImageFrame source, Rectangle sourceRectangle)
{
int startX = sourceRectangle.X;
int endX = sourceRectangle.Right;
int startY = sourceRectangle.Y;
int endY = sourceRectangle.Bottom;
// Align start/end positions.
int minX = Math.Max(0, startX);
int maxX = Math.Min(source.Width, endX);
int minY = Math.Max(0, startY);
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;
using (var amount = new Buffer(width))
using (BrushApplicator applicator = this.brush.CreateApplicator(source, sourceRectangle, this.options))
{
for (int i = 0; i < width; i++)
{
amount[i] = this.options.BlendPercentage;
}
Parallel.For(
minY,
maxY,
source.Configuration().ParallelOptions,
y =>
{
int offsetY = y - startY;
int offsetX = minX - startX;
applicator.Apply(amount, offsetX, offsetY);
});
}
}
}
}