From 3eb09054a4155b6c402f5152728d8a33baa16cde Mon Sep 17 00:00:00 2001 From: Florian Levis Date: Sun, 11 Feb 2024 15:38:17 +0100 Subject: [PATCH 1/3] fix: add guards for MagickImage.Clahe --- src/Magick.NET/MagickImage.cs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Magick.NET/MagickImage.cs b/src/Magick.NET/MagickImage.cs index 12dd928319..1dcab0ebb6 100644 --- a/src/Magick.NET/MagickImage.cs +++ b/src/Magick.NET/MagickImage.cs @@ -1552,7 +1552,13 @@ public void Clahe(Percentage xTiles, Percentage yTiles, int numberBins, double c /// The contrast limit for localised changes in contrast. A limit less than 1 /// results in standard non-contrast limited AHE. public void Clahe(int xTiles, int yTiles, int numberBins, double clipLimit) - => _nativeInstance.Clahe(xTiles, yTiles, numberBins, clipLimit); + { + Throw.IfNegative(nameof(xTiles), xTiles); + Throw.IfNegative(nameof(yTiles), yTiles); + Throw.IfNegative(nameof(numberBins), numberBins); + + _nativeInstance.Clahe(xTiles, yTiles, numberBins, clipLimit); + } /// /// Set each pixel whose value is below zero to zero and any the pixel whose value is above From 6cbed952bd2ff79b1e9ab24dadddcbe1db7493d4 Mon Sep 17 00:00:00 2001 From: Florian Levis Date: Sun, 11 Feb 2024 15:38:45 +0100 Subject: [PATCH 2/3] doc: add missing exception tags (thrown by nativeInstance) --- src/Magick.NET.Core/IMagickImage.cs | 2 ++ src/Magick.NET/MagickImage.cs | 2 ++ 2 files changed, 4 insertions(+) diff --git a/src/Magick.NET.Core/IMagickImage.cs b/src/Magick.NET.Core/IMagickImage.cs index a09be9362b..0fb2c7c024 100644 --- a/src/Magick.NET.Core/IMagickImage.cs +++ b/src/Magick.NET.Core/IMagickImage.cs @@ -738,6 +738,7 @@ Interlace Interlace /// The number of bins for histogram ("dynamic range"). /// The contrast limit for localised changes in contrast. A limit less than 1 /// results in standard non-contrast limited AHE. + /// Thrown when an error is raised by ImageMagick. void Clahe(Percentage xTiles, Percentage yTiles, int numberBins, double clipLimit); /// @@ -749,6 +750,7 @@ Interlace Interlace /// The number of bins for histogram ("dynamic range"). /// The contrast limit for localised changes in contrast. A limit less than 1 /// results in standard non-contrast limited AHE. + /// Thrown when an error is raised by ImageMagick. void Clahe(int xTiles, int yTiles, int numberBins, double clipLimit); /// diff --git a/src/Magick.NET/MagickImage.cs b/src/Magick.NET/MagickImage.cs index 1dcab0ebb6..55376f3cd6 100644 --- a/src/Magick.NET/MagickImage.cs +++ b/src/Magick.NET/MagickImage.cs @@ -1539,6 +1539,7 @@ public void ChopVertical(int offset, int height) /// The number of bins for histogram ("dynamic range"). /// The contrast limit for localised changes in contrast. A limit less than 1 /// results in standard non-contrast limited AHE. + /// Thrown when an error is raised by ImageMagick. public void Clahe(Percentage xTiles, Percentage yTiles, int numberBins, double clipLimit) => Clahe(Width * xTiles, Height * yTiles, numberBins, clipLimit); @@ -1551,6 +1552,7 @@ public void Clahe(Percentage xTiles, Percentage yTiles, int numberBins, double c /// The number of bins for histogram ("dynamic range"). /// The contrast limit for localised changes in contrast. A limit less than 1 /// results in standard non-contrast limited AHE. + /// Thrown when an error is raised by ImageMagick. public void Clahe(int xTiles, int yTiles, int numberBins, double clipLimit) { Throw.IfNegative(nameof(xTiles), xTiles); From 6e5f7017859ff0c0fb07b14b1bc7b5af49e8cbcb Mon Sep 17 00:00:00 2001 From: Florian Levis Date: Sun, 11 Feb 2024 15:43:09 +0100 Subject: [PATCH 3/3] tests: add corresponding tests --- .../MagickImageTests/TheClaheMethod.cs | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/tests/Magick.NET.Tests/MagickImageTests/TheClaheMethod.cs b/tests/Magick.NET.Tests/MagickImageTests/TheClaheMethod.cs index 1c6bb00120..442151c2d1 100644 --- a/tests/Magick.NET.Tests/MagickImageTests/TheClaheMethod.cs +++ b/tests/Magick.NET.Tests/MagickImageTests/TheClaheMethod.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,27 @@ public partial class MagickImageTests { public class TheClaheMethod { + [Fact] + public void ShouldThrowExceptionWhenXTilesIsNegative() + { + using var image = new MagickImage(Files.FujiFilmFinePixS1ProPNG); + Assert.Throws("xTiles", () => image.Clahe(-10, 20, 30, 1.5)); + } + + [Fact] + public void ShouldThrowExceptionWhenYTilesIsNegative() + { + using var image = new MagickImage(Files.FujiFilmFinePixS1ProPNG); + Assert.Throws("yTiles", () => image.Clahe(10, -20, 30, 1.5)); + } + + [Fact] + public void ShouldThrowExceptionWhenNumberBinsIsNegative() + { + using var image = new MagickImage(Files.FujiFilmFinePixS1ProPNG); + Assert.Throws("numberBins", () => image.Clahe(10, 20, -30, 1.5)); + } + [Fact] public void ShouldChangeTheImage() {