// Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. using System; using SixLabors.ImageSharp.Advanced; using SixLabors.ImageSharp.Drawing.Brushes.Processors; using SixLabors.ImageSharp.Drawing.Processors; using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.PixelFormats; using SixLabors.Primitives; namespace SixLabors.ImageSharp.Drawing.Brushes { /// /// Provides an implementation of an image brush for painting images within areas. /// /// The pixel format. public class ImageBrush : IBrush where TPixel : struct, IPixel { /// /// The image to paint. /// private readonly ImageFrame image; /// /// Initializes a new instance of the class. /// /// The image. public ImageBrush(ImageFrame image) { this.image = image; } /// public BrushApplicator CreateApplicator(ImageFrame source, RectangleF region, GraphicsOptions options) { return new ImageBrushApplicator(source, this.image, region, options); } /// /// The image brush applicator. /// private class ImageBrushApplicator : BrushApplicator { /// /// 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() { this.source.Dispose(); } /// internal override void Apply(Span scanline, int x, int y) { // Create a span for colors using (var amountBuffer = new Buffer(scanline.Length)) using (var overlay = new Buffer(scanline.Length)) { 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++) { amountBuffer[i] = scanline[i] * this.Options.BlendPercentage; int sourceX = (i + offsetX) % this.xLength; TPixel pixel = sourceRow[sourceX]; overlay[i] = pixel; } Span destinationRow = this.Target.GetPixelRowSpan(y).Slice(x, scanline.Length); this.Blender.Blend(destinationRow, destinationRow, overlay, amountBuffer); } } } } }