From 02c6dec4e12593648386910c04599efaaea14611 Mon Sep 17 00:00:00 2001 From: Florian Levis Date: Fri, 9 Feb 2024 16:14:52 +0100 Subject: [PATCH 1/4] fix: add guards for MagickImage.BilateralBlur --- src/Magick.NET/MagickImage.cs | 16 ++++++---- .../TheBilateralBlurMethod.cs | 29 +++++++++++++++++++ 2 files changed, 40 insertions(+), 5 deletions(-) diff --git a/src/Magick.NET/MagickImage.cs b/src/Magick.NET/MagickImage.cs index 8d00101a86..7b94970400 100644 --- a/src/Magick.NET/MagickImage.cs +++ b/src/Magick.NET/MagickImage.cs @@ -1312,10 +1312,12 @@ public void AutoThreshold(AutoThresholdMethod method) /// /// Applies a non-linear, edge-preserving, and noise-reducing smoothing filter. /// - /// The width of the neighborhood in pixels. - /// The height of the neighborhood in pixels. + /// The width of the neighborhood in pixels (> 0). + /// The height of the neighborhood in pixels (> 0). 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); } @@ -1323,12 +1325,16 @@ public void BilateralBlur(int width, int height) /// /// Applies a non-linear, edge-preserving, and noise-reducing smoothing filter. /// - /// The width of the neighborhood in pixels. - /// The height of the neighborhood in pixels. + /// The width of the neighborhood in pixels (> 0). + /// The height of the neighborhood in pixels (> 0). /// The sigma in the intensity space. /// The sigma in the coordinate space. 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); + } /// /// Forces all pixels below the threshold into black while leaving all pixels at or above diff --git a/tests/Magick.NET.Tests/MagickImageTests/TheBilateralBlurMethod.cs b/tests/Magick.NET.Tests/MagickImageTests/TheBilateralBlurMethod.cs index b1a257c258..2ed3b1fd29 100644 --- a/tests/Magick.NET.Tests/MagickImageTests/TheBilateralBlurMethod.cs +++ b/tests/Magick.NET.Tests/MagickImageTests/TheBilateralBlurMethod.cs @@ -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; @@ -10,6 +11,34 @@ public partial class MagickImageTests { public class TheBilateralBlurMethod { + [Fact] + public void ShouldThrowExceptionWhenWidthIsLessThanOne() + { + using var image = new MagickImage(Files.NoisePNG); + Assert.Throws("width", () => image.BilateralBlur(1, 2)); + } + + [Fact] + public void ShouldThrowExceptionWhenWidthIsLessThanOneWithLowAndHigh() + { + using var image = new MagickImage(Files.NoisePNG); + Assert.Throws("width", () => image.BilateralBlur(1, 2, 0.1, 0.1)); + } + + [Fact] + public void ShouldThrowExceptionWhenHeightIsLessThanOne() + { + using var image = new MagickImage(Files.NoisePNG); + Assert.Throws("height", () => image.BilateralBlur(2, 1)); + } + + [Fact] + public void ShouldThrowExceptionWhenHeightIsLessThanOneWithLowAndHigh() + { + using var image = new MagickImage(Files.NoisePNG); + Assert.Throws("height", () => image.BilateralBlur(2, 1, 0.1, 0.1)); + } + [Fact] public void ShouldApplyTheFilter() { From 015ae29b7a56cd9358f8713dc0afc194143e4d6f Mon Sep 17 00:00:00 2001 From: Florian Levis Date: Fri, 9 Feb 2024 16:44:06 +0100 Subject: [PATCH 2/4] doc: add missing exception tag --- src/Magick.NET/MagickImage.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Magick.NET/MagickImage.cs b/src/Magick.NET/MagickImage.cs index 7b94970400..f218309023 100644 --- a/src/Magick.NET/MagickImage.cs +++ b/src/Magick.NET/MagickImage.cs @@ -1314,6 +1314,7 @@ public void AutoThreshold(AutoThresholdMethod method) /// /// The width of the neighborhood in pixels (> 0). /// The height of the neighborhood in pixels (> 0). + /// Thrown when an error is raised by ImageMagick. public void BilateralBlur(int width, int height) { Throw.IfFalse(nameof(width), width > 1, "The width must be > 1"); @@ -1329,6 +1330,7 @@ public void BilateralBlur(int width, int height) /// The height of the neighborhood in pixels (> 0). /// The sigma in the intensity space. /// The sigma in the coordinate space. + /// Thrown when an error is raised by ImageMagick. public void BilateralBlur(int width, int height, double intensitySigma, double spatialSigma) { Throw.IfFalse(nameof(width), width > 1, "The width must be > 1"); From 53bfe085c0ffd4e8f63bd85862360c9e96fda125 Mon Sep 17 00:00:00 2001 From: Florian Levis Date: Fri, 9 Feb 2024 19:17:57 +0100 Subject: [PATCH 3/4] cr: use IfNegative test, add missing doc tag --- src/Magick.NET.Core/IMagickImage.cs | 4 +++- src/Magick.NET/MagickImage.cs | 16 ++++++++-------- .../MagickImageTests/TheBilateralBlurMethod.cs | 16 ++++++++-------- 3 files changed, 19 insertions(+), 17 deletions(-) diff --git a/src/Magick.NET.Core/IMagickImage.cs b/src/Magick.NET.Core/IMagickImage.cs index 1038219362..a09be9362b 100644 --- a/src/Magick.NET.Core/IMagickImage.cs +++ b/src/Magick.NET.Core/IMagickImage.cs @@ -559,7 +559,8 @@ Interlace Interlace /// Applies a non-linear, edge-preserving, and noise-reducing smoothing filter. /// /// The width of the neighborhood in pixels. - /// The height of the neighborhood in pixels.\ + /// The height of the neighborhood in pixels. + /// Thrown when an error is raised by ImageMagick. void BilateralBlur(int width, int height); /// @@ -569,6 +570,7 @@ Interlace Interlace /// The height of the neighborhood in pixels. /// The sigma in the intensity space. /// The sigma in the coordinate space. + /// Thrown when an error is raised by ImageMagick. void BilateralBlur(int width, int height, double intensitySigma, double spatialSigma); /// diff --git a/src/Magick.NET/MagickImage.cs b/src/Magick.NET/MagickImage.cs index f218309023..6dddd47aea 100644 --- a/src/Magick.NET/MagickImage.cs +++ b/src/Magick.NET/MagickImage.cs @@ -1312,13 +1312,13 @@ public void AutoThreshold(AutoThresholdMethod method) /// /// Applies a non-linear, edge-preserving, and noise-reducing smoothing filter. /// - /// The width of the neighborhood in pixels (> 0). - /// The height of the neighborhood in pixels (> 0). + /// The width of the neighborhood in pixels. + /// The height of the neighborhood in pixels. /// Thrown when an error is raised by ImageMagick. 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"); + Throw.IfNegative(nameof(width), width); + Throw.IfNegative(nameof(height), height); var intensitySigma = Math.Sqrt((width * width) + (height * height)); BilateralBlur(width, height, intensitySigma, intensitySigma * 0.25); } @@ -1326,15 +1326,15 @@ public void BilateralBlur(int width, int height) /// /// Applies a non-linear, edge-preserving, and noise-reducing smoothing filter. /// - /// The width of the neighborhood in pixels (> 0). - /// The height of the neighborhood in pixels (> 0). + /// The width of the neighborhood in pixels. + /// The height of the neighborhood in pixels. /// The sigma in the intensity space. /// The sigma in the coordinate space. /// Thrown when an error is raised by ImageMagick. public void BilateralBlur(int width, int height, double intensitySigma, double spatialSigma) { - Throw.IfFalse(nameof(width), width > 1, "The width must be > 1"); - Throw.IfFalse(nameof(height), height > 1, "The height must be > 1"); + Throw.IfNegative(nameof(width), width); + Throw.IfNegative(nameof(height), height); _nativeInstance.BilateralBlur(width, height, intensitySigma, spatialSigma); } diff --git a/tests/Magick.NET.Tests/MagickImageTests/TheBilateralBlurMethod.cs b/tests/Magick.NET.Tests/MagickImageTests/TheBilateralBlurMethod.cs index 2ed3b1fd29..15d278687f 100644 --- a/tests/Magick.NET.Tests/MagickImageTests/TheBilateralBlurMethod.cs +++ b/tests/Magick.NET.Tests/MagickImageTests/TheBilateralBlurMethod.cs @@ -12,31 +12,31 @@ public partial class MagickImageTests public class TheBilateralBlurMethod { [Fact] - public void ShouldThrowExceptionWhenWidthIsLessThanOne() + public void ShouldThrowExceptionWhenWidthIsNegative() { using var image = new MagickImage(Files.NoisePNG); - Assert.Throws("width", () => image.BilateralBlur(1, 2)); + Assert.Throws("width", () => image.BilateralBlur(-1, 2)); } [Fact] - public void ShouldThrowExceptionWhenWidthIsLessThanOneWithLowAndHigh() + public void ShouldThrowExceptionWhenWidthIsNegativeThanOneWithLowAndHigh() { using var image = new MagickImage(Files.NoisePNG); - Assert.Throws("width", () => image.BilateralBlur(1, 2, 0.1, 0.1)); + Assert.Throws("width", () => image.BilateralBlur(-1, 2, 0.1, 0.1)); } [Fact] - public void ShouldThrowExceptionWhenHeightIsLessThanOne() + public void ShouldThrowExceptionWhenHeightIsNegative() { using var image = new MagickImage(Files.NoisePNG); - Assert.Throws("height", () => image.BilateralBlur(2, 1)); + Assert.Throws("height", () => image.BilateralBlur(2, -1)); } [Fact] - public void ShouldThrowExceptionWhenHeightIsLessThanOneWithLowAndHigh() + public void ShouldThrowExceptionWhenHeightIsNegativeWithLowAndHigh() { using var image = new MagickImage(Files.NoisePNG); - Assert.Throws("height", () => image.BilateralBlur(2, 1, 0.1, 0.1)); + Assert.Throws("height", () => image.BilateralBlur(2, -1, 0.1, 0.1)); } [Fact] From 2ead0fd58aedcd2a3be6c81e3ab325e97b0be7c3 Mon Sep 17 00:00:00 2001 From: Florian Levis Date: Fri, 9 Feb 2024 19:31:22 +0100 Subject: [PATCH 4/4] cs: new line after bloc of args check --- src/Magick.NET/MagickImage.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Magick.NET/MagickImage.cs b/src/Magick.NET/MagickImage.cs index 6dddd47aea..7d81463bdb 100644 --- a/src/Magick.NET/MagickImage.cs +++ b/src/Magick.NET/MagickImage.cs @@ -1319,6 +1319,7 @@ public void BilateralBlur(int width, int height) { Throw.IfNegative(nameof(width), width); Throw.IfNegative(nameof(height), height); + var intensitySigma = Math.Sqrt((width * width) + (height * height)); BilateralBlur(width, height, intensitySigma, intensitySigma * 0.25); } @@ -1335,6 +1336,7 @@ public void BilateralBlur(int width, int height, double intensitySigma, double s { Throw.IfNegative(nameof(width), width); Throw.IfNegative(nameof(height), height); + _nativeInstance.BilateralBlur(width, height, intensitySigma, spatialSigma); }