// Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. using SixLabors.ImageSharp.PixelFormats; using SixLabors.Primitives; namespace SixLabors.ImageSharp.Processing.Processors.Drawing { /// /// Combines two images together by blending the pixels. /// public class DrawImageProcessor : IImageProcessor { /// /// Initializes a new instance of the class. /// /// The image to blend. /// The location to draw the blended image. /// The blending mode to use when drawing the image. /// The Alpha blending mode to use when drawing the image. /// The opacity of the image to blend. public DrawImageProcessor( Image image, Point location, PixelColorBlendingMode colorBlendingMode, PixelAlphaCompositionMode alphaCompositionMode, float opacity) { this.Image = image; this.Location = location; this.ColorBlendingMode = colorBlendingMode; this.AlphaCompositionMode = alphaCompositionMode; this.Opacity = opacity; } /// /// Gets the image to blend. /// public Image Image { get; } /// /// Gets the location to draw the blended image. /// public Point Location { get; } /// /// Gets the blending mode to use when drawing the image. /// public PixelColorBlendingMode ColorBlendingMode { get; } /// /// Gets the Alpha blending mode to use when drawing the image. /// public PixelAlphaCompositionMode AlphaCompositionMode { get; } /// /// Gets the opacity of the image to blend. /// public float Opacity { get; } /// public IImageProcessor CreatePixelSpecificProcessor(Image source, Rectangle sourceRectangle) where TPixelBg : struct, IPixel { var visitor = new ProcessorFactoryVisitor(this, source, sourceRectangle); this.Image.AcceptVisitor(visitor); return visitor.Result; } private class ProcessorFactoryVisitor : IImageVisitor where TPixelBg : struct, IPixel { private readonly DrawImageProcessor definition; private readonly Image source; private readonly Rectangle sourceRectangle; public ProcessorFactoryVisitor(DrawImageProcessor definition, Image source, Rectangle sourceRectangle) { this.definition = definition; this.source = source; this.sourceRectangle = sourceRectangle; } public IImageProcessor Result { get; private set; } public void Visit(Image image) where TPixelFg : struct, IPixel { this.Result = new DrawImageProcessor( image, this.source, this.sourceRectangle, this.definition.Location, this.definition.ColorBlendingMode, this.definition.AlphaCompositionMode, this.definition.Opacity); } } } }