// 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 { public interface IImageProcessor { IImageProcessor CreatePixelSpecificProcessor() where TPixel : struct, IPixel; } /// /// Encapsulates methods to alter the pixels of an image. /// /// The pixel format. public interface IImageProcessor where TPixel : struct, IPixel { /// /// Applies the process to the specified portion of the specified . /// /// The source image. Cannot be null. /// /// The structure that specifies the portion of the image object to draw. /// /// /// is null. /// /// /// doesn't fit the dimension of the image. /// void Apply(Image source, Rectangle sourceRectangle); } internal static class ImageProcessorExtensions { public static void Apply(this IImageProcessor processor, Image source, Rectangle sourceRectangle) { var visitor = new ApplyVisitor(processor, sourceRectangle); source.AcceptVisitor(visitor); } private class ApplyVisitor : IImageVisitor { private readonly IImageProcessor processor; private readonly Rectangle sourceRectangle; public ApplyVisitor(IImageProcessor processor, Rectangle sourceRectangle) { this.processor = processor; this.sourceRectangle = sourceRectangle; } public void Visit(Image image) where TPixel : struct, IPixel { var processorImpl = processor.CreatePixelSpecificProcessor(); processorImpl.Apply(image, this.sourceRectangle); } } } }