Skip to content

Commit 78b7465

Browse files
authored
fix: add guards for MagickImage.MeanShift (#1612)
1 parent 32edbce commit 78b7465

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/Magick.NET/MagickImage.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4035,7 +4035,12 @@ public void MeanShift(int width, int height)
40354035
/// <param name="height">The height of the pixels neighborhood.</param>
40364036
/// <param name="colorDistance">The color distance.</param>
40374037
public void MeanShift(int width, int height, Percentage colorDistance)
4038-
=> _nativeInstance.MeanShift(width, height, PercentageHelper.ToQuantum(colorDistance));
4038+
{
4039+
Throw.IfNegative(nameof(width), width);
4040+
Throw.IfNegative(nameof(height), height);
4041+
4042+
_nativeInstance.MeanShift(width, height, PercentageHelper.ToQuantum(colorDistance));
4043+
}
40394044

40404045
/// <summary>
40414046
/// Filter image by replacing each pixel component with the median color in a circular neighborhood.

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

Lines changed: 17 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,22 @@ public partial class MagickImageTests
1011
{
1112
public class TheMeanShiftMethod
1213
{
14+
[Fact]
15+
public void ShouldThrowExceptionWhenWidthIsNegative()
16+
{
17+
using var image = new MagickImage(Files.FujiFilmFinePixS1ProPNG);
18+
19+
Assert.Throws<ArgumentException>("width", () => image.MeanShift(-1, 1));
20+
}
21+
22+
[Fact]
23+
public void ShouldThrowExceptionWhenHeightIsNegative()
24+
{
25+
using var image = new MagickImage(Files.FujiFilmFinePixS1ProPNG);
26+
27+
Assert.Throws<ArgumentException>("height", () => image.MeanShift(1, -1));
28+
}
29+
1330
[Fact]
1431
public void ShouldNotChangeImageWhenSizeIsOne()
1532
{

0 commit comments

Comments
 (0)