-
-
Notifications
You must be signed in to change notification settings - Fork 851
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Expose Convolution Api #2797
Merged
Merged
Expose Convolution Api #2797
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b69405f
Add convolution API
JimBobSquarePants 065c1f9
Add test output
JimBobSquarePants 014ac52
Merge branch 'main' into js/convolution-api
JimBobSquarePants 430ceee
Merge branch 'main' into js/convolution-api
JimBobSquarePants 3bd1efb
Normalize parameter order.
JimBobSquarePants File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
src/ImageSharp/Processing/Extensions/Convolution/ConvolutionExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
// Copyright (c) Six Labors. | ||
// Licensed under the Six Labors Split License. | ||
|
||
using SixLabors.ImageSharp.Processing.Processors.Convolution; | ||
|
||
namespace SixLabors.ImageSharp.Processing.Extensions.Convolution; | ||
|
||
/// <summary> | ||
/// Defines general convolution extensions to apply on an <see cref="Image"/> | ||
/// using Mutate/Clone. | ||
/// </summary> | ||
public static class ConvolutionExtensions | ||
{ | ||
/// <summary> | ||
/// Applies a convolution filter to the image. | ||
/// </summary> | ||
/// <param name="source">The current image processing context.</param> | ||
/// <param name="kernelXY">The convolution kernel to apply.</param> | ||
/// <returns>The <see cref="IImageProcessingContext"/>.</returns> | ||
public static IImageProcessingContext Convolve(this IImageProcessingContext source, DenseMatrix<float> kernelXY) | ||
=> Convolve(source, kernelXY, false); | ||
|
||
/// <summary> | ||
/// Applies a convolution filter to the image. | ||
/// </summary> | ||
/// <param name="source">The current image processing context.</param> | ||
/// <param name="kernelXY">The convolution kernel to apply.</param> | ||
/// <param name="preserveAlpha">Whether the convolution filter is applied to alpha as well as the color channels.</param> | ||
/// <returns>The <see cref="IImageProcessingContext"/>.</returns> | ||
public static IImageProcessingContext Convolve(this IImageProcessingContext source, DenseMatrix<float> kernelXY, bool preserveAlpha) | ||
=> Convolve(source, kernelXY, preserveAlpha, BorderWrappingMode.Repeat, BorderWrappingMode.Repeat); | ||
|
||
/// <summary> | ||
/// Applies a convolution filter to the image. | ||
/// </summary> | ||
/// <param name="source">The current image processing context.</param> | ||
/// <param name="kernelXY">The convolution kernel to apply.</param> | ||
/// <param name="preserveAlpha">Whether the convolution filter is applied to alpha as well as the color channels.</param> | ||
/// <param name="borderWrapModeX">The <see cref="BorderWrappingMode"/> to use when mapping the pixels outside of the border, in X direction.</param> | ||
/// <param name="borderWrapModeY">The <see cref="BorderWrappingMode"/> to use when mapping the pixels outside of the border, in Y direction.</param> | ||
/// <returns>The <see cref="IImageProcessingContext"/>.</returns> | ||
public static IImageProcessingContext Convolve( | ||
this IImageProcessingContext source, | ||
DenseMatrix<float> kernelXY, | ||
bool preserveAlpha, | ||
BorderWrappingMode borderWrapModeX, | ||
BorderWrappingMode borderWrapModeY) | ||
=> source.ApplyProcessor(new ConvolutionProcessor(kernelXY, preserveAlpha, borderWrapModeX, borderWrapModeY)); | ||
|
||
/// <summary> | ||
/// Applies a convolution filter to the image. | ||
/// </summary> | ||
/// <param name="source">The current image processing context.</param> | ||
/// <param name="rectangle">The rectangle structure that specifies the portion of the image object to alter.</param> | ||
/// <param name="kernelXY">The convolution kernel to apply.</param> | ||
/// <returns>The <see cref="IImageProcessingContext"/>.</returns> | ||
public static IImageProcessingContext Convolve(this IImageProcessingContext source, Rectangle rectangle, DenseMatrix<float> kernelXY) | ||
=> Convolve(source, rectangle, kernelXY, false); | ||
|
||
/// <summary> | ||
/// Applies a convolution filter to the image. | ||
/// </summary> | ||
/// <param name="source">The current image processing context.</param> | ||
/// <param name="rectangle">The rectangle structure that specifies the portion of the image object to alter.</param> | ||
/// <param name="kernelXY">The convolution kernel to apply.</param> | ||
/// <param name="preserveAlpha">Whether the convolution filter is applied to alpha as well as the color channels.</param> | ||
/// <returns>The <see cref="IImageProcessingContext"/>.</returns> | ||
public static IImageProcessingContext Convolve(this IImageProcessingContext source, Rectangle rectangle, DenseMatrix<float> kernelXY, bool preserveAlpha) | ||
=> Convolve(source, rectangle, kernelXY, preserveAlpha, BorderWrappingMode.Repeat, BorderWrappingMode.Repeat); | ||
|
||
/// <summary> | ||
/// Applies a convolution filter to the image. | ||
/// </summary> | ||
/// <param name="source">The current image processing context.</param> | ||
/// <param name="rectangle">The rectangle structure that specifies the portion of the image object to alter.</param> | ||
/// <param name="kernelXY">The convolution kernel to apply.</param> | ||
/// <param name="preserveAlpha">Whether the convolution filter is applied to alpha as well as the color channels.</param> | ||
/// <param name="borderWrapModeX">The <see cref="BorderWrappingMode"/> to use when mapping the pixels outside of the border, in X direction.</param> | ||
/// <param name="borderWrapModeY">The <see cref="BorderWrappingMode"/> to use when mapping the pixels outside of the border, in Y direction.</param> | ||
/// <returns>The <see cref="IImageProcessingContext"/>.</returns> | ||
public static IImageProcessingContext Convolve( | ||
this IImageProcessingContext source, | ||
Rectangle rectangle, | ||
DenseMatrix<float> kernelXY, | ||
bool preserveAlpha, | ||
BorderWrappingMode borderWrapModeX, | ||
BorderWrappingMode borderWrapModeY) | ||
=> source.ApplyProcessor(new ConvolutionProcessor(kernelXY, preserveAlpha, borderWrapModeX, borderWrapModeY), rectangle); | ||
} | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
src/ImageSharp/Processing/Processors/Convolution/ConvolutionProcessor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// Copyright (c) Six Labors. | ||
// Licensed under the Six Labors Split License. | ||
|
||
using SixLabors.ImageSharp.PixelFormats; | ||
|
||
namespace SixLabors.ImageSharp.Processing.Processors.Convolution; | ||
|
||
/// <summary> | ||
/// Defines a processor that uses a 2 dimensional matrix to perform convolution against an image. | ||
/// </summary> | ||
public class ConvolutionProcessor : IImageProcessor | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="ConvolutionProcessor"/> class. | ||
/// </summary> | ||
/// <param name="kernelXY">The 2d gradient operator.</param> | ||
/// <param name="preserveAlpha">Whether the convolution filter is applied to alpha as well as the color channels.</param> | ||
/// <param name="borderWrapModeX">The <see cref="BorderWrappingMode"/> to use when mapping the pixels outside of the border, in X direction.</param> | ||
/// <param name="borderWrapModeY">The <see cref="BorderWrappingMode"/> to use when mapping the pixels outside of the border, in Y direction.</param> | ||
public ConvolutionProcessor( | ||
in DenseMatrix<float> kernelXY, | ||
bool preserveAlpha, | ||
BorderWrappingMode borderWrapModeX, | ||
BorderWrappingMode borderWrapModeY) | ||
{ | ||
this.KernelXY = kernelXY; | ||
this.PreserveAlpha = preserveAlpha; | ||
this.BorderWrapModeX = borderWrapModeX; | ||
this.BorderWrapModeY = borderWrapModeY; | ||
} | ||
|
||
/// <summary> | ||
/// Gets the 2d convolution kernel. | ||
/// </summary> | ||
public DenseMatrix<float> KernelXY { get; } | ||
|
||
/// <summary> | ||
/// Gets a value indicating whether the convolution filter is applied to alpha as well as the color channels. | ||
/// </summary> | ||
public bool PreserveAlpha { get; } | ||
|
||
/// <summary> | ||
/// Gets the <see cref="BorderWrappingMode"/> to use when mapping the pixels outside of the border, in X direction. | ||
/// </summary> | ||
public BorderWrappingMode BorderWrapModeX { get; } | ||
|
||
/// <summary> | ||
/// Gets the <see cref="BorderWrappingMode"/> to use when mapping the pixels outside of the border, in Y direction. | ||
/// </summary> | ||
public BorderWrappingMode BorderWrapModeY { get; } | ||
|
||
/// <inheritdoc/> | ||
public IImageProcessor<TPixel> CreatePixelSpecificProcessor<TPixel>(Configuration configuration, Image<TPixel> source, Rectangle sourceRectangle) | ||
where TPixel : unmanaged, | ||
IPixel<TPixel> | ||
{ | ||
if (this.KernelXY.TryGetLinearlySeparableComponents(out float[]? kernelX, out float[]? kernelY)) | ||
{ | ||
return new Convolution2PassProcessor<TPixel>( | ||
configuration, | ||
kernelX, | ||
kernelY, | ||
this.PreserveAlpha, | ||
source, | ||
sourceRectangle, | ||
this.BorderWrapModeX, | ||
this.BorderWrapModeY); | ||
} | ||
|
||
return new ConvolutionProcessor<TPixel>( | ||
configuration, | ||
this.KernelXY, | ||
this.PreserveAlpha, | ||
source, | ||
sourceRectangle, | ||
this.BorderWrapModeX, | ||
this.BorderWrapModeY); | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would add integration tests that excercise at least 2 different values for all parameters to validate that everything is plumbed through end-to-end, otherwise LGTM.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll have alook. Thanks!