|
| 1 | +// Copyright (c) Six Labors. |
| 2 | +// Licensed under the Six Labors Split License. |
| 3 | + |
| 4 | +using SixLabors.ImageSharp.Processing.Processors.Convolution; |
| 5 | + |
| 6 | +namespace SixLabors.ImageSharp.Processing.Extensions.Convolution; |
| 7 | + |
| 8 | +/// <summary> |
| 9 | +/// Defines general convolution extensions to apply on an <see cref="Image"/> |
| 10 | +/// using Mutate/Clone. |
| 11 | +/// </summary> |
| 12 | +public static class ConvolutionExtensions |
| 13 | +{ |
| 14 | + /// <summary> |
| 15 | + /// Applies a convolution filter to the image. |
| 16 | + /// </summary> |
| 17 | + /// <param name="source">The current image processing context.</param> |
| 18 | + /// <param name="kernelXY">The convolution kernel to apply.</param> |
| 19 | + /// <returns>The <see cref="IImageProcessingContext"/>.</returns> |
| 20 | + public static IImageProcessingContext Convolve(this IImageProcessingContext source, DenseMatrix<float> kernelXY) |
| 21 | + => Convolve(source, kernelXY, false); |
| 22 | + |
| 23 | + /// <summary> |
| 24 | + /// Applies a convolution filter to the image. |
| 25 | + /// </summary> |
| 26 | + /// <param name="source">The current image processing context.</param> |
| 27 | + /// <param name="kernelXY">The convolution kernel to apply.</param> |
| 28 | + /// <param name="preserveAlpha">Whether the convolution filter is applied to alpha as well as the color channels.</param> |
| 29 | + /// <returns>The <see cref="IImageProcessingContext"/>.</returns> |
| 30 | + public static IImageProcessingContext Convolve(this IImageProcessingContext source, DenseMatrix<float> kernelXY, bool preserveAlpha) |
| 31 | + => Convolve(source, kernelXY, preserveAlpha, BorderWrappingMode.Repeat, BorderWrappingMode.Repeat); |
| 32 | + |
| 33 | + /// <summary> |
| 34 | + /// Applies a convolution filter to the image. |
| 35 | + /// </summary> |
| 36 | + /// <param name="source">The current image processing context.</param> |
| 37 | + /// <param name="kernelXY">The convolution kernel to apply.</param> |
| 38 | + /// <param name="preserveAlpha">Whether the convolution filter is applied to alpha as well as the color channels.</param> |
| 39 | + /// <param name="borderWrapModeX">The <see cref="BorderWrappingMode"/> to use when mapping the pixels outside of the border, in X direction.</param> |
| 40 | + /// <param name="borderWrapModeY">The <see cref="BorderWrappingMode"/> to use when mapping the pixels outside of the border, in Y direction.</param> |
| 41 | + /// <returns>The <see cref="IImageProcessingContext"/>.</returns> |
| 42 | + public static IImageProcessingContext Convolve( |
| 43 | + this IImageProcessingContext source, |
| 44 | + DenseMatrix<float> kernelXY, |
| 45 | + bool preserveAlpha, |
| 46 | + BorderWrappingMode borderWrapModeX, |
| 47 | + BorderWrappingMode borderWrapModeY) |
| 48 | + => source.ApplyProcessor(new ConvolutionProcessor(kernelXY, preserveAlpha, borderWrapModeX, borderWrapModeY)); |
| 49 | + |
| 50 | + /// <summary> |
| 51 | + /// Applies a convolution filter to the image. |
| 52 | + /// </summary> |
| 53 | + /// <param name="source">The current image processing context.</param> |
| 54 | + /// <param name="rectangle">The rectangle structure that specifies the portion of the image object to alter.</param> |
| 55 | + /// <param name="kernelXY">The convolution kernel to apply.</param> |
| 56 | + /// <returns>The <see cref="IImageProcessingContext"/>.</returns> |
| 57 | + public static IImageProcessingContext Convolve(this IImageProcessingContext source, Rectangle rectangle, DenseMatrix<float> kernelXY) |
| 58 | + => Convolve(source, rectangle, kernelXY, false); |
| 59 | + |
| 60 | + /// <summary> |
| 61 | + /// Applies a convolution filter to the image. |
| 62 | + /// </summary> |
| 63 | + /// <param name="source">The current image processing context.</param> |
| 64 | + /// <param name="rectangle">The rectangle structure that specifies the portion of the image object to alter.</param> |
| 65 | + /// <param name="kernelXY">The convolution kernel to apply.</param> |
| 66 | + /// <param name="preserveAlpha">Whether the convolution filter is applied to alpha as well as the color channels.</param> |
| 67 | + /// <returns>The <see cref="IImageProcessingContext"/>.</returns> |
| 68 | + public static IImageProcessingContext Convolve(this IImageProcessingContext source, Rectangle rectangle, DenseMatrix<float> kernelXY, bool preserveAlpha) |
| 69 | + => Convolve(source, rectangle, kernelXY, preserveAlpha, BorderWrappingMode.Repeat, BorderWrappingMode.Repeat); |
| 70 | + |
| 71 | + /// <summary> |
| 72 | + /// Applies a convolution filter to the image. |
| 73 | + /// </summary> |
| 74 | + /// <param name="source">The current image processing context.</param> |
| 75 | + /// <param name="rectangle">The rectangle structure that specifies the portion of the image object to alter.</param> |
| 76 | + /// <param name="kernelXY">The convolution kernel to apply.</param> |
| 77 | + /// <param name="preserveAlpha">Whether the convolution filter is applied to alpha as well as the color channels.</param> |
| 78 | + /// <param name="borderWrapModeX">The <see cref="BorderWrappingMode"/> to use when mapping the pixels outside of the border, in X direction.</param> |
| 79 | + /// <param name="borderWrapModeY">The <see cref="BorderWrappingMode"/> to use when mapping the pixels outside of the border, in Y direction.</param> |
| 80 | + /// <returns>The <see cref="IImageProcessingContext"/>.</returns> |
| 81 | + public static IImageProcessingContext Convolve( |
| 82 | + this IImageProcessingContext source, |
| 83 | + Rectangle rectangle, |
| 84 | + DenseMatrix<float> kernelXY, |
| 85 | + bool preserveAlpha, |
| 86 | + BorderWrappingMode borderWrapModeX, |
| 87 | + BorderWrappingMode borderWrapModeY) |
| 88 | + => source.ApplyProcessor(new ConvolutionProcessor(kernelXY, preserveAlpha, borderWrapModeX, borderWrapModeY), rectangle); |
| 89 | +} |
0 commit comments