// // Copyright (c) James Jackson-South and contributors. // Licensed under the Apache License, Version 2.0. // namespace ImageSharp.Drawing.Brushes { using System; using ImageSharp.Memory; using ImageSharp.PixelFormats; using Processors; using SixLabors.Primitives; /// /// 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 ImageBase image; /// /// Initializes a new instance of the class. /// /// The image. public ImageBrush(ImageBase image) { this.image = image; } /// public BrushApplicator CreateApplicator(ImageBase 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 ImageBase 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(ImageBase target, ImageBase 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.GetRowSpan(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.GetRowSpan(x, y).Slice(0, scanline.Length); this.Blender.Blend(destinationRow, destinationRow, overlay, amountBuffer); } } } } }