Skip to content

Commit a3d25eb

Browse files
authored
fix: add guards for MagickImage.Clahe (#1551)
1 parent c801291 commit a3d25eb

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

src/Magick.NET.Core/IMagickImage.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,7 @@ Interlace Interlace
738738
/// <param name="numberBins">The number of bins for histogram ("dynamic range").</param>
739739
/// <param name="clipLimit">The contrast limit for localised changes in contrast. A limit less than 1
740740
/// results in standard non-contrast limited AHE.</param>
741+
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
741742
void Clahe(Percentage xTiles, Percentage yTiles, int numberBins, double clipLimit);
742743

743744
/// <summary>
@@ -749,6 +750,7 @@ Interlace Interlace
749750
/// <param name="numberBins">The number of bins for histogram ("dynamic range").</param>
750751
/// <param name="clipLimit">The contrast limit for localised changes in contrast. A limit less than 1
751752
/// results in standard non-contrast limited AHE.</param>
753+
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
752754
void Clahe(int xTiles, int yTiles, int numberBins, double clipLimit);
753755

754756
/// <summary>

src/Magick.NET/MagickImage.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1539,6 +1539,7 @@ public void ChopVertical(int offset, int height)
15391539
/// <param name="numberBins">The number of bins for histogram ("dynamic range").</param>
15401540
/// <param name="clipLimit">The contrast limit for localised changes in contrast. A limit less than 1
15411541
/// results in standard non-contrast limited AHE.</param>
1542+
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
15421543
public void Clahe(Percentage xTiles, Percentage yTiles, int numberBins, double clipLimit)
15431544
=> Clahe(Width * xTiles, Height * yTiles, numberBins, clipLimit);
15441545

@@ -1551,8 +1552,15 @@ public void Clahe(Percentage xTiles, Percentage yTiles, int numberBins, double c
15511552
/// <param name="numberBins">The number of bins for histogram ("dynamic range").</param>
15521553
/// <param name="clipLimit">The contrast limit for localised changes in contrast. A limit less than 1
15531554
/// results in standard non-contrast limited AHE.</param>
1555+
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
15541556
public void Clahe(int xTiles, int yTiles, int numberBins, double clipLimit)
1555-
=> _nativeInstance.Clahe(xTiles, yTiles, numberBins, clipLimit);
1557+
{
1558+
Throw.IfNegative(nameof(xTiles), xTiles);
1559+
Throw.IfNegative(nameof(yTiles), yTiles);
1560+
Throw.IfNegative(nameof(numberBins), numberBins);
1561+
1562+
_nativeInstance.Clahe(xTiles, yTiles, numberBins, clipLimit);
1563+
}
15561564

15571565
/// <summary>
15581566
/// Set each pixel whose value is below zero to zero and any the pixel whose value is above

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

Lines changed: 22 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,27 @@ public partial class MagickImageTests
1011
{
1112
public class TheClaheMethod
1213
{
14+
[Fact]
15+
public void ShouldThrowExceptionWhenXTilesIsNegative()
16+
{
17+
using var image = new MagickImage(Files.FujiFilmFinePixS1ProPNG);
18+
Assert.Throws<ArgumentException>("xTiles", () => image.Clahe(-10, 20, 30, 1.5));
19+
}
20+
21+
[Fact]
22+
public void ShouldThrowExceptionWhenYTilesIsNegative()
23+
{
24+
using var image = new MagickImage(Files.FujiFilmFinePixS1ProPNG);
25+
Assert.Throws<ArgumentException>("yTiles", () => image.Clahe(10, -20, 30, 1.5));
26+
}
27+
28+
[Fact]
29+
public void ShouldThrowExceptionWhenNumberBinsIsNegative()
30+
{
31+
using var image = new MagickImage(Files.FujiFilmFinePixS1ProPNG);
32+
Assert.Throws<ArgumentException>("numberBins", () => image.Clahe(10, 20, -30, 1.5));
33+
}
34+
1335
[Fact]
1436
public void ShouldChangeTheImage()
1537
{

0 commit comments

Comments
 (0)