Skip to content

Commit 1ec479e

Browse files
Merge pull request #2797 from SixLabors/js/convolution-api
Expose Convolution Api
2 parents 2d7cf48 + 3bd1efb commit 1ec479e

30 files changed

+446
-197
lines changed

src/ImageSharp/Processing/Extensions/Convolution/BokehBlurExtensions.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,13 @@ public static IImageProcessingContext BokehBlur(this IImageProcessingContext sou
4444
/// Applies a bokeh blur to the image.
4545
/// </summary>
4646
/// <param name="source">The current image processing context.</param>
47-
/// <param name="radius">The 'radius' value representing the size of the area to sample.</param>
48-
/// <param name="components">The 'components' value representing the number of kernels to use to approximate the bokeh effect.</param>
49-
/// <param name="gamma">The gamma highlight factor to use to emphasize bright spots in the source image</param>
5047
/// <param name="rectangle">
5148
/// The <see cref="Rectangle"/> structure that specifies the portion of the image object to alter.
5249
/// </param>
50+
/// <param name="radius">The 'radius' value representing the size of the area to sample.</param>
51+
/// <param name="components">The 'components' value representing the number of kernels to use to approximate the bokeh effect.</param>
52+
/// <param name="gamma">The gamma highlight factor to use to emphasize bright spots in the source image</param>
5353
/// <returns>The <see cref="IImageProcessingContext"/>.</returns>
54-
public static IImageProcessingContext BokehBlur(this IImageProcessingContext source, int radius, int components, float gamma, Rectangle rectangle)
54+
public static IImageProcessingContext BokehBlur(this IImageProcessingContext source, Rectangle rectangle, int radius, int components, float gamma)
5555
=> source.ApplyProcessor(new BokehBlurProcessor(radius, components, gamma), rectangle);
5656
}

src/ImageSharp/Processing/Extensions/Convolution/BoxBlurExtensions.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,20 +44,22 @@ public static IImageProcessingContext BoxBlur(this IImageProcessingContext sourc
4444
/// Applies a box blur to the image.
4545
/// </summary>
4646
/// <param name="source">The current image processing context.</param>
47-
/// <param name="radius">The 'radius' value representing the size of the area to sample.</param>
4847
/// <param name="rectangle">
4948
/// The <see cref="Rectangle"/> structure that specifies the portion of the image object to alter.
5049
/// </param>
50+
/// <param name="radius">The 'radius' value representing the size of the area to sample.</param>
5151
/// <param name="borderWrapModeX">
5252
/// The <see cref="BorderWrappingMode"/> to use when mapping the pixels outside of the border, in X direction.
5353
/// </param>
5454
/// <param name="borderWrapModeY">
5555
/// The <see cref="BorderWrappingMode"/> to use when mapping the pixels outside of the border, in Y direction.
5656
/// </param>
5757
/// <returns>The <see cref="IImageProcessingContext"/>.</returns>
58-
public static IImageProcessingContext BoxBlur(this IImageProcessingContext source, int radius, Rectangle rectangle, BorderWrappingMode borderWrapModeX, BorderWrappingMode borderWrapModeY)
59-
{
60-
var processor = new BoxBlurProcessor(radius, borderWrapModeX, borderWrapModeY);
61-
return source.ApplyProcessor(processor, rectangle);
62-
}
58+
public static IImageProcessingContext BoxBlur(
59+
this IImageProcessingContext source,
60+
Rectangle rectangle,
61+
int radius,
62+
BorderWrappingMode borderWrapModeX,
63+
BorderWrappingMode borderWrapModeY)
64+
=> source.ApplyProcessor(new BoxBlurProcessor(radius, borderWrapModeX, borderWrapModeY), rectangle);
6365
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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

Comments
 (0)