Skip to content

Commit 830f7d8

Browse files
authored
fix: add guards for MagickImage.BilateralBlur (#1545)
1 parent 258d230 commit 830f7d8

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed

src/Magick.NET.Core/IMagickImage.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,8 @@ Interlace Interlace
559559
/// Applies a non-linear, edge-preserving, and noise-reducing smoothing filter.
560560
/// </summary>
561561
/// <param name="width">The width of the neighborhood in pixels.</param>
562-
/// <param name="height">The height of the neighborhood in pixels.</param>\
562+
/// <param name="height">The height of the neighborhood in pixels.</param>
563+
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
563564
void BilateralBlur(int width, int height);
564565

565566
/// <summary>
@@ -569,6 +570,7 @@ Interlace Interlace
569570
/// <param name="height">The height of the neighborhood in pixels.</param>
570571
/// <param name="intensitySigma">The sigma in the intensity space.</param>
571572
/// <param name="spatialSigma">The sigma in the coordinate space.</param>
573+
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
572574
void BilateralBlur(int width, int height, double intensitySigma, double spatialSigma);
573575

574576
/// <summary>

src/Magick.NET/MagickImage.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1314,8 +1314,12 @@ public void AutoThreshold(AutoThresholdMethod method)
13141314
/// </summary>
13151315
/// <param name="width">The width of the neighborhood in pixels.</param>
13161316
/// <param name="height">The height of the neighborhood in pixels.</param>
1317+
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
13171318
public void BilateralBlur(int width, int height)
13181319
{
1320+
Throw.IfNegative(nameof(width), width);
1321+
Throw.IfNegative(nameof(height), height);
1322+
13191323
var intensitySigma = Math.Sqrt((width * width) + (height * height));
13201324
BilateralBlur(width, height, intensitySigma, intensitySigma * 0.25);
13211325
}
@@ -1327,8 +1331,14 @@ public void BilateralBlur(int width, int height)
13271331
/// <param name="height">The height of the neighborhood in pixels.</param>
13281332
/// <param name="intensitySigma">The sigma in the intensity space.</param>
13291333
/// <param name="spatialSigma">The sigma in the coordinate space.</param>
1334+
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
13301335
public void BilateralBlur(int width, int height, double intensitySigma, double spatialSigma)
1331-
=> _nativeInstance.BilateralBlur(width, height, intensitySigma, spatialSigma);
1336+
{
1337+
Throw.IfNegative(nameof(width), width);
1338+
Throw.IfNegative(nameof(height), height);
1339+
1340+
_nativeInstance.BilateralBlur(width, height, intensitySigma, spatialSigma);
1341+
}
13321342

13331343
/// <summary>
13341344
/// Forces all pixels below the threshold into black while leaving all pixels at or above

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

Lines changed: 29 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,34 @@ public partial class MagickImageTests
1011
{
1112
public class TheBilateralBlurMethod
1213
{
14+
[Fact]
15+
public void ShouldThrowExceptionWhenWidthIsNegative()
16+
{
17+
using var image = new MagickImage(Files.NoisePNG);
18+
Assert.Throws<ArgumentException>("width", () => image.BilateralBlur(-1, 2));
19+
}
20+
21+
[Fact]
22+
public void ShouldThrowExceptionWhenWidthIsNegativeThanOneWithLowAndHigh()
23+
{
24+
using var image = new MagickImage(Files.NoisePNG);
25+
Assert.Throws<ArgumentException>("width", () => image.BilateralBlur(-1, 2, 0.1, 0.1));
26+
}
27+
28+
[Fact]
29+
public void ShouldThrowExceptionWhenHeightIsNegative()
30+
{
31+
using var image = new MagickImage(Files.NoisePNG);
32+
Assert.Throws<ArgumentException>("height", () => image.BilateralBlur(2, -1));
33+
}
34+
35+
[Fact]
36+
public void ShouldThrowExceptionWhenHeightIsNegativeWithLowAndHigh()
37+
{
38+
using var image = new MagickImage(Files.NoisePNG);
39+
Assert.Throws<ArgumentException>("height", () => image.BilateralBlur(2, -1, 0.1, 0.1));
40+
}
41+
1342
[Fact]
1443
public void ShouldApplyTheFilter()
1544
{

0 commit comments

Comments
 (0)