mirror of https://github.com/SixLabors/ImageSharp
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
117 lines
4.0 KiB
117 lines
4.0 KiB
// Copyright (c) Six Labors and contributors.
|
|
// Licensed under the Apache License, Version 2.0.
|
|
|
|
using System;
|
|
using System.Buffers;
|
|
|
|
using SixLabors.ImageSharp.Advanced;
|
|
using SixLabors.ImageSharp.Memory;
|
|
using SixLabors.ImageSharp.PixelFormats;
|
|
using SixLabors.Memory;
|
|
using SixLabors.Primitives;
|
|
|
|
namespace SixLabors.ImageSharp.Processing
|
|
{
|
|
/// <summary>
|
|
/// Provides an implementation of a solid brush for painting solid color areas.
|
|
/// </summary>
|
|
/// <typeparam name="TPixel">The pixel format.</typeparam>
|
|
public class SolidBrush<TPixel> : IBrush<TPixel>
|
|
where TPixel : struct, IPixel<TPixel>
|
|
{
|
|
/// <summary>
|
|
/// The color to paint.
|
|
/// </summary>
|
|
private readonly TPixel color;
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="SolidBrush{TPixel}"/> class.
|
|
/// </summary>
|
|
/// <param name="color">The color.</param>
|
|
public SolidBrush(TPixel color)
|
|
{
|
|
this.color = color;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets the color.
|
|
/// </summary>
|
|
/// <value>
|
|
/// The color.
|
|
/// </value>
|
|
public TPixel Color => this.color;
|
|
|
|
/// <inheritdoc />
|
|
public BrushApplicator<TPixel> CreateApplicator(ImageFrame<TPixel> source, RectangleF region, GraphicsOptions options)
|
|
{
|
|
return new SolidBrushApplicator(source, this.color, options);
|
|
}
|
|
|
|
/// <summary>
|
|
/// The solid brush applicator.
|
|
/// </summary>
|
|
private class SolidBrushApplicator : BrushApplicator<TPixel>
|
|
{
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="SolidBrushApplicator"/> class.
|
|
/// </summary>
|
|
/// <param name="source">The source image.</param>
|
|
/// <param name="color">The color.</param>
|
|
/// <param name="options">The options</param>
|
|
public SolidBrushApplicator(ImageFrame<TPixel> source, TPixel color, GraphicsOptions options)
|
|
: base(source, options)
|
|
{
|
|
this.Colors = source.MemoryAllocator.Allocate<TPixel>(source.Width);
|
|
this.Colors.GetSpan().Fill(color);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets the colors.
|
|
/// </summary>
|
|
protected IMemoryOwner<TPixel> Colors { get; }
|
|
|
|
/// <summary>
|
|
/// Gets the color for a single pixel.
|
|
/// </summary>
|
|
/// <param name="x">The x.</param>
|
|
/// <param name="y">The y.</param>
|
|
/// <returns>
|
|
/// The color
|
|
/// </returns>
|
|
internal override TPixel this[int x, int y] => this.Colors.GetSpan()[x];
|
|
|
|
/// <inheritdoc />
|
|
public override void Dispose()
|
|
{
|
|
this.Colors.Dispose();
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
internal override void Apply(Span<float> scanline, int x, int y)
|
|
{
|
|
Span<TPixel> destinationRow = this.Target.GetPixelRowSpan(y).Slice(x, scanline.Length);
|
|
|
|
MemoryAllocator memoryAllocator = this.Target.MemoryAllocator;
|
|
|
|
if (this.Options.BlendPercentage == 1f)
|
|
{
|
|
this.Blender.Blend(memoryAllocator, destinationRow, destinationRow, this.Colors.GetSpan(), scanline);
|
|
}
|
|
else
|
|
{
|
|
using (IMemoryOwner<float> amountBuffer = memoryAllocator.Allocate<float>(scanline.Length))
|
|
{
|
|
Span<float> amountSpan = amountBuffer.GetSpan();
|
|
|
|
for (int i = 0; i < scanline.Length; i++)
|
|
{
|
|
amountSpan[i] = scanline[i] * this.Options.BlendPercentage;
|
|
}
|
|
|
|
this.Blender.Blend(memoryAllocator, destinationRow, destinationRow, this.Colors.GetSpan(), amountSpan);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|