// 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
{
///
/// Provides an implementation of an image brush for painting images within areas.
///
/// The pixel format.
public class ImageBrush : IBrush
{
///
/// The image to paint.
///
private readonly Image image;
///
/// Initializes a new instance of the class.
///
/// The image.
public ImageBrush(Image image)
{
this.image = image;
}
///
public BrushApplicator CreateApplicator(
ImageFrame source,
RectangleF region,
GraphicsOptions options)
where TPixel : struct, IPixel
{
Image specificImage = (Image)this.image;
return new ImageBrushApplicator(source, specificImage.Frames.RootFrame, region, options);
}
///
/// The image brush applicator.
///
private class ImageBrushApplicator : BrushApplicator
where TPixel : struct, IPixel
{
///
/// The source image.
///
private readonly ImageFrame source;
///
/// The y-length.
///
private readonly int yLength;
///
/// The x-length.
///
private readonly int xLength;
///
/// The Y offset.
///
private readonly int offsetY;
///
/// The X offset.
///
private readonly int offsetX;
///
/// Initializes a new instance of the class.
///
/// The target image.
/// The image.
/// The region.
/// The options
public ImageBrushApplicator(ImageFrame target, ImageFrame image, RectangleF region, GraphicsOptions options)
: base(target, options)
{
this.source = image;
this.xLength = image.Width;
this.yLength = image.Height;
this.offsetY = (int)MathF.Max(MathF.Floor(region.Top), 0);
this.offsetX = (int)MathF.Max(MathF.Floor(region.Left), 0);
}
///
/// Gets the color for a single pixel.
///
/// The x.
/// The y.
///
/// The color
///
internal override TPixel this[int x, int y]
{
get
{
int srcX = (x - this.offsetX) % this.xLength;
int srcY = (y - this.offsetY) % this.yLength;
return this.source[srcX, srcY];
}
}
///
public override void Dispose()
{
}
///
internal override void Apply(Span scanline, int x, int y)
{
// Create a span for colors
using (IMemoryOwner amountBuffer = this.Target.MemoryAllocator.Allocate(scanline.Length))
using (IMemoryOwner overlay = this.Target.MemoryAllocator.Allocate(scanline.Length))
{
Span amountSpan = amountBuffer.GetSpan();
Span overlaySpan = overlay.GetSpan();
int sourceY = (y - this.offsetY) % this.yLength;
int offsetX = x - this.offsetX;
Span sourceRow = this.source.GetPixelRowSpan(sourceY);
for (int i = 0; i < scanline.Length; i++)
{
amountSpan[i] = scanline[i] * this.Options.BlendPercentage;
int sourceX = (i + offsetX) % this.xLength;
TPixel pixel = sourceRow[sourceX];
overlaySpan[i] = pixel;
}
Span destinationRow = this.Target.GetPixelRowSpan(y).Slice(x, scanline.Length);
this.Blender.Blend(
this.source.Configuration,
destinationRow,
destinationRow,
overlaySpan,
amountSpan);
}
}
}
}
}