Skip to content

Commit 258d230

Browse files
authored
fix: add guards for MagickImage.AdaptiveThreshold (#1544)
1 parent dd30d8b commit 258d230

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

src/Magick.NET/MagickImage.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1094,7 +1094,7 @@ public void AdaptiveSharpen(double radius, double sigma, Channels channels)
10941094
/// <param name="height">The height of the pixel neighborhood.</param>
10951095
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
10961096
public void AdaptiveThreshold(int width, int height)
1097-
=> AdaptiveThreshold(width, height, 0, ImageMagick.Channels.Undefined);
1097+
=> AdaptiveThreshold(width, height, 0.0, ImageMagick.Channels.Undefined);
10981098

10991099
/// <summary>
11001100
/// Local adaptive threshold image.
@@ -1105,7 +1105,7 @@ public void AdaptiveThreshold(int width, int height)
11051105
/// <param name="channels">The channel(s) that should be thresholded.</param>
11061106
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
11071107
public void AdaptiveThreshold(int width, int height, Channels channels)
1108-
=> AdaptiveThreshold(width, height, 0, channels);
1108+
=> AdaptiveThreshold(width, height, 0.0, channels);
11091109

11101110
/// <summary>
11111111
/// Local adaptive threshold image.
@@ -1128,7 +1128,11 @@ public void AdaptiveThreshold(int width, int height, double bias)
11281128
/// <param name="channels">The channel(s) that should be thresholded.</param>
11291129
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
11301130
public void AdaptiveThreshold(int width, int height, double bias, Channels channels)
1131-
=> _nativeInstance.AdaptiveThreshold(width, height, bias, channels);
1131+
{
1132+
Throw.IfNegative(nameof(width), width);
1133+
Throw.IfNegative(nameof(height), height);
1134+
_nativeInstance.AdaptiveThreshold(width, height, bias, channels);
1135+
}
11321136

11331137
/// <summary>
11341138
/// Local adaptive threshold image.

tests/Magick.NET.Tests/MagickImageTests/TheAdaptiveThresholdMethod.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright Dirk Lemstra https://github.com/dlemstra/Magick.NET.
22
// Licensed under the Apache License, Version 2.0.
33

4+
using System;
45
using ImageMagick;
56
using Xunit;
67

@@ -10,6 +11,20 @@ public partial class MagickImageTests
1011
{
1112
public class TheAdaptiveThresholdMethod
1213
{
14+
[Fact]
15+
public void ShouldThrowExceptionWhenWidthIsNegative()
16+
{
17+
using var image = new MagickImage(Files.MagickNETIconPNG);
18+
Assert.Throws<ArgumentException>("width", () => image.AdaptiveThreshold(-1, 10, 0.0, Channels.Red));
19+
}
20+
21+
[Fact]
22+
public void ShouldThrowExceptionWhenHeightIsNegative()
23+
{
24+
using var image = new MagickImage(Files.MagickNETIconPNG);
25+
Assert.Throws<ArgumentException>("height", () => image.AdaptiveThreshold(10, -1, 0.0, Channels.Red));
26+
}
27+
1328
[Fact]
1429
public void ShouldThresholdTheImage()
1530
{

0 commit comments

Comments
 (0)