Skip to content
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

fix: add guards for MagickImage.BilateralBlur #1545

Merged
merged 4 commits into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/Magick.NET.Core/IMagickImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,8 @@ Interlace Interlace
/// Applies a non-linear, edge-preserving, and noise-reducing smoothing filter.
/// </summary>
/// <param name="width">The width of the neighborhood in pixels.</param>
/// <param name="height">The height of the neighborhood in pixels.</param>\
/// <param name="height">The height of the neighborhood in pixels.</param>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
void BilateralBlur(int width, int height);

/// <summary>
Expand All @@ -569,6 +570,7 @@ Interlace Interlace
/// <param name="height">The height of the neighborhood in pixels.</param>
/// <param name="intensitySigma">The sigma in the intensity space.</param>
/// <param name="spatialSigma">The sigma in the coordinate space.</param>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
void BilateralBlur(int width, int height, double intensitySigma, double spatialSigma);

/// <summary>
Expand Down
10 changes: 9 additions & 1 deletion src/Magick.NET/MagickImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1314,8 +1314,11 @@ public void AutoThreshold(AutoThresholdMethod method)
/// </summary>
/// <param name="width">The width of the neighborhood in pixels.</param>
/// <param name="height">The height of the neighborhood in pixels.</param>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
public void BilateralBlur(int width, int height)
{
Throw.IfNegative(nameof(width), width);
Throw.IfNegative(nameof(height), height);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a newline after the Thow's? I do that in the rest of the library.

p.s. I missed that in your other pr...

Copy link
Sponsor Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✔️

var intensitySigma = Math.Sqrt((width * width) + (height * height));
BilateralBlur(width, height, intensitySigma, intensitySigma * 0.25);
}
Expand All @@ -1327,8 +1330,13 @@ public void BilateralBlur(int width, int height)
/// <param name="height">The height of the neighborhood in pixels.</param>
/// <param name="intensitySigma">The sigma in the intensity space.</param>
/// <param name="spatialSigma">The sigma in the coordinate space.</param>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
public void BilateralBlur(int width, int height, double intensitySigma, double spatialSigma)
=> _nativeInstance.BilateralBlur(width, height, intensitySigma, spatialSigma);
{
Throw.IfNegative(nameof(width), width);
Throw.IfNegative(nameof(height), height);
_nativeInstance.BilateralBlur(width, height, intensitySigma, spatialSigma);
}

/// <summary>
/// Forces all pixels below the threshold into black while leaving all pixels at or above
Expand Down
29 changes: 29 additions & 0 deletions tests/Magick.NET.Tests/MagickImageTests/TheBilateralBlurMethod.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright Dirk Lemstra https://github.com/dlemstra/Magick.NET.
// Licensed under the Apache License, Version 2.0.

using System;
using ImageMagick;
using Xunit;

Expand All @@ -10,6 +11,34 @@ public partial class MagickImageTests
{
public class TheBilateralBlurMethod
{
[Fact]
public void ShouldThrowExceptionWhenWidthIsNegative()
{
using var image = new MagickImage(Files.NoisePNG);
Assert.Throws<ArgumentException>("width", () => image.BilateralBlur(-1, 2));
}

[Fact]
public void ShouldThrowExceptionWhenWidthIsNegativeThanOneWithLowAndHigh()
{
using var image = new MagickImage(Files.NoisePNG);
Assert.Throws<ArgumentException>("width", () => image.BilateralBlur(-1, 2, 0.1, 0.1));
}

[Fact]
public void ShouldThrowExceptionWhenHeightIsNegative()
{
using var image = new MagickImage(Files.NoisePNG);
Assert.Throws<ArgumentException>("height", () => image.BilateralBlur(2, -1));
}

[Fact]
public void ShouldThrowExceptionWhenHeightIsNegativeWithLowAndHigh()
{
using var image = new MagickImage(Files.NoisePNG);
Assert.Throws<ArgumentException>("height", () => image.BilateralBlur(2, -1, 0.1, 0.1));
}

[Fact]
public void ShouldApplyTheFilter()
{
Expand Down
Loading