Skip to content

Commit

Permalink
fix: add guards for MagickImage.AdaptiveThreshold (#1544)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gounlaf authored Feb 8, 2024
1 parent dd30d8b commit 258d230
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/Magick.NET/MagickImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1094,7 +1094,7 @@ public void AdaptiveSharpen(double radius, double sigma, Channels channels)
/// <param name="height">The height of the pixel neighborhood.</param>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
public void AdaptiveThreshold(int width, int height)
=> AdaptiveThreshold(width, height, 0, ImageMagick.Channels.Undefined);
=> AdaptiveThreshold(width, height, 0.0, ImageMagick.Channels.Undefined);

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

/// <summary>
/// Local adaptive threshold image.
Expand All @@ -1128,7 +1128,11 @@ public void AdaptiveThreshold(int width, int height, double bias)
/// <param name="channels">The channel(s) that should be thresholded.</param>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
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);
}

/// <summary>
/// Local adaptive threshold image.
Expand Down
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,20 @@ public partial class MagickImageTests
{
public class TheAdaptiveThresholdMethod
{
[Fact]
public void ShouldThrowExceptionWhenWidthIsNegative()
{
using var image = new MagickImage(Files.MagickNETIconPNG);
Assert.Throws<ArgumentException>("width", () => image.AdaptiveThreshold(-1, 10, 0.0, Channels.Red));
}

[Fact]
public void ShouldThrowExceptionWhenHeightIsNegative()
{
using var image = new MagickImage(Files.MagickNETIconPNG);
Assert.Throws<ArgumentException>("height", () => image.AdaptiveThreshold(10, -1, 0.0, Channels.Red));
}

[Fact]
public void ShouldThresholdTheImage()
{
Expand Down

0 comments on commit 258d230

Please sign in to comment.