Skip to content

Commit

Permalink
fix: add guards for MagickImage.BilateralBlur
Browse files Browse the repository at this point in the history
  • Loading branch information
Gounlaf committed Feb 9, 2024
1 parent 258d230 commit 6035129
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/Magick.NET/MagickImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1316,6 +1316,8 @@ public void AutoThreshold(AutoThresholdMethod method)
/// <param name="height">The height of the neighborhood in pixels.</param>
public void BilateralBlur(int width, int height)
{
Throw.IfFalse(nameof(width), width > 1, "The width must be > 1");
Throw.IfFalse(nameof(height), height > 1, "The height must be > 1");
var intensitySigma = Math.Sqrt((width * width) + (height * height));
BilateralBlur(width, height, intensitySigma, intensitySigma * 0.25);
}
Expand All @@ -1328,7 +1330,11 @@ public void BilateralBlur(int width, int height)
/// <param name="intensitySigma">The sigma in the intensity space.</param>
/// <param name="spatialSigma">The sigma in the coordinate space.</param>
public void BilateralBlur(int width, int height, double intensitySigma, double spatialSigma)
=> _nativeInstance.BilateralBlur(width, height, intensitySigma, spatialSigma);
{
Throw.IfFalse(nameof(width), width > 1, "The width must be > 1");
Throw.IfFalse(nameof(height), height > 1, "The height must be > 1");
_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 ShouldThrowExceptionWhenWidthIsLessThanOne()
{
using var image = new MagickImage(Files.NoisePNG);
Assert.Throws<ArgumentException>("width", () => image.BilateralBlur(1, 2));
}

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

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

[Fact]
public void ShouldThrowExceptionWhenHeightIsLessThanOneWithLowAndHigh()
{
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

0 comments on commit 6035129

Please sign in to comment.