From 258d2304773fab778aada86ca6eda04e23f0e8e2 Mon Sep 17 00:00:00 2001 From: Florian Levis Date: Thu, 8 Feb 2024 23:27:36 +0100 Subject: [PATCH] fix: add guards for MagickImage.AdaptiveThreshold (#1544) --- src/Magick.NET/MagickImage.cs | 10 +++++++--- .../TheAdaptiveThresholdMethod.cs | 15 +++++++++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/Magick.NET/MagickImage.cs b/src/Magick.NET/MagickImage.cs index a5c2ed051f..8d00101a86 100644 --- a/src/Magick.NET/MagickImage.cs +++ b/src/Magick.NET/MagickImage.cs @@ -1094,7 +1094,7 @@ public void AdaptiveSharpen(double radius, double sigma, Channels channels) /// The height of the pixel neighborhood. /// Thrown when an error is raised by ImageMagick. public void AdaptiveThreshold(int width, int height) - => AdaptiveThreshold(width, height, 0, ImageMagick.Channels.Undefined); + => AdaptiveThreshold(width, height, 0.0, ImageMagick.Channels.Undefined); /// /// Local adaptive threshold image. @@ -1105,7 +1105,7 @@ public void AdaptiveThreshold(int width, int height) /// The channel(s) that should be thresholded. /// Thrown when an error is raised by ImageMagick. public void AdaptiveThreshold(int width, int height, Channels channels) - => AdaptiveThreshold(width, height, 0, channels); + => AdaptiveThreshold(width, height, 0.0, channels); /// /// Local adaptive threshold image. @@ -1128,7 +1128,11 @@ public void AdaptiveThreshold(int width, int height, double bias) /// The channel(s) that should be thresholded. /// Thrown when an error is raised by ImageMagick. public void AdaptiveThreshold(int width, int height, double bias, Channels channels) - => _nativeInstance.AdaptiveThreshold(width, height, bias, channels); + { + Throw.IfNegative(nameof(width), width); + Throw.IfNegative(nameof(height), height); + _nativeInstance.AdaptiveThreshold(width, height, bias, channels); + } /// /// Local adaptive threshold image. diff --git a/tests/Magick.NET.Tests/MagickImageTests/TheAdaptiveThresholdMethod.cs b/tests/Magick.NET.Tests/MagickImageTests/TheAdaptiveThresholdMethod.cs index 0192a565ce..1154b10eff 100644 --- a/tests/Magick.NET.Tests/MagickImageTests/TheAdaptiveThresholdMethod.cs +++ b/tests/Magick.NET.Tests/MagickImageTests/TheAdaptiveThresholdMethod.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,20 @@ public partial class MagickImageTests { public class TheAdaptiveThresholdMethod { + [Fact] + public void ShouldThrowExceptionWhenWidthIsNegative() + { + using var image = new MagickImage(Files.MagickNETIconPNG); + Assert.Throws("width", () => image.AdaptiveThreshold(-1, 10, 0.0, Channels.Red)); + } + + [Fact] + public void ShouldThrowExceptionWhenHeightIsNegative() + { + using var image = new MagickImage(Files.MagickNETIconPNG); + Assert.Throws("height", () => image.AdaptiveThreshold(10, -1, 0.0, Channels.Red)); + } + [Fact] public void ShouldThresholdTheImage() {