|
| 1 | +// Copyright (c) Six Labors. |
| 2 | +// Licensed under the Apache License, Version 2.0. |
| 3 | + |
| 4 | +using System; |
| 5 | +using SixLabors.ImageSharp.PixelFormats; |
| 6 | +using SixLabors.ImageSharp.Processing; |
| 7 | +using SixLabors.ImageSharp.Processing.Processors; |
| 8 | + |
| 9 | +namespace SixLabors.ImageSharp.Drawing.Processing.Processors.Drawing |
| 10 | +{ |
| 11 | + /// <summary> |
| 12 | + /// Allows the recursive application of processing operations against and image. |
| 13 | + /// </summary> |
| 14 | + public class RecursiveImageProcessor : IImageProcessor |
| 15 | + { |
| 16 | + /// <summary> |
| 17 | + /// Initializes a new instance of the <see cref="RecursiveImageProcessor"/> class. |
| 18 | + /// </summary> |
| 19 | + /// <param name="options">The drawing options.</param> |
| 20 | + /// <param name="path">The logic path.</param> |
| 21 | + /// <param name="operation">The operation to perform on the source.</param> |
| 22 | + public RecursiveImageProcessor(DrawingOptions options, IPath path, Action<IImageProcessingContext> operation) |
| 23 | + { |
| 24 | + this.Options = options; |
| 25 | + this.Path = path; |
| 26 | + this.Operation = operation; |
| 27 | + } |
| 28 | + |
| 29 | + /// <summary> |
| 30 | + /// Gets the drawing options. |
| 31 | + /// </summary> |
| 32 | + public DrawingOptions Options { get; } |
| 33 | + |
| 34 | + /// <summary> |
| 35 | + /// Gets the logic path. |
| 36 | + /// </summary> |
| 37 | + public IPath Path { get; } |
| 38 | + |
| 39 | + /// <summary> |
| 40 | + /// Gets the operation to perform on the source. |
| 41 | + /// </summary> |
| 42 | + public Action<IImageProcessingContext> Operation { get; } |
| 43 | + |
| 44 | + /// <inheritdoc/> |
| 45 | + public IImageProcessor<TPixel> CreatePixelSpecificProcessor<TPixel>( |
| 46 | + Configuration configuration, |
| 47 | + Image<TPixel> source, |
| 48 | + Rectangle sourceRectangle) |
| 49 | + where TPixel : unmanaged, IPixel<TPixel> |
| 50 | + => new RecursiveImageProcessorInner<TPixel>(this, source, configuration, sourceRectangle); |
| 51 | + |
| 52 | + /// <summary> |
| 53 | + /// The main workhorse class. This has access to the pixel buffer but |
| 54 | + /// in an abstract/generic way. |
| 55 | + /// </summary> |
| 56 | + /// <typeparam name="TPixel">The type of pixel.</typeparam> |
| 57 | + private class RecursiveImageProcessorInner<TPixel> : IImageProcessor<TPixel> |
| 58 | + where TPixel : unmanaged, IPixel<TPixel> |
| 59 | + { |
| 60 | + private readonly RecursiveImageProcessor recursiveImageProcessor; |
| 61 | + private readonly Image<TPixel> source; |
| 62 | + private readonly Configuration configuration; |
| 63 | + private readonly Rectangle sourceRectangle; |
| 64 | + |
| 65 | + public RecursiveImageProcessorInner(RecursiveImageProcessor recursiveImageProcessor, Image<TPixel> source, Configuration configuration, Rectangle sourceRectangle) |
| 66 | + { |
| 67 | + this.recursiveImageProcessor = recursiveImageProcessor; |
| 68 | + this.source = source; |
| 69 | + this.configuration = configuration; |
| 70 | + this.sourceRectangle = sourceRectangle; |
| 71 | + } |
| 72 | + |
| 73 | + public void Dispose() |
| 74 | + { |
| 75 | + } |
| 76 | + |
| 77 | + public void Execute() |
| 78 | + { |
| 79 | + // Perform any processing logic you want here, in this case we are going to |
| 80 | + // clone the source image then apply the changes requested before cropping |
| 81 | + // down and using an image brush to draw that portion the the original image |
| 82 | + // so clone out our source image so we can apply |
| 83 | + // various effects to it without mutating the origional yet. |
| 84 | + using Image<TPixel> clone = this.source.Clone(this.recursiveImageProcessor.Operation); |
| 85 | + |
| 86 | + // Crop it down to just the size of the shape |
| 87 | + clone.Mutate(x => x.Crop((Rectangle)this.recursiveImageProcessor.Path.Bounds)); |
| 88 | + |
| 89 | + // Use an image brush to apply cloned image as the source for filling the shape |
| 90 | + var brush = new ImageBrush(clone); |
| 91 | + |
| 92 | + // Grab hold of an image processor that can fill paths with a brush to allow it to do the hard pixel pushing for us |
| 93 | + var processor = new FillPathProcessor(this.recursiveImageProcessor.Options, brush, this.recursiveImageProcessor.Path); |
| 94 | + using IImageProcessor<TPixel> p = processor.CreatePixelSpecificProcessor(this.configuration, this.source, this.sourceRectangle); |
| 95 | + |
| 96 | + // Fill the shape using the image brush |
| 97 | + p.Execute(); |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | +} |
0 commit comments