Skip to content

Commit

Permalink
fix: add guards for MagickImage.MeanShift (#1612)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gounlaf authored Apr 27, 2024
1 parent 32edbce commit 78b7465
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/Magick.NET/MagickImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4035,7 +4035,12 @@ public void MeanShift(int width, int height)
/// <param name="height">The height of the pixels neighborhood.</param>
/// <param name="colorDistance">The color distance.</param>
public void MeanShift(int width, int height, Percentage colorDistance)
=> _nativeInstance.MeanShift(width, height, PercentageHelper.ToQuantum(colorDistance));
{
Throw.IfNegative(nameof(width), width);
Throw.IfNegative(nameof(height), height);

_nativeInstance.MeanShift(width, height, PercentageHelper.ToQuantum(colorDistance));
}

/// <summary>
/// Filter image by replacing each pixel component with the median color in a circular neighborhood.
Expand Down
17 changes: 17 additions & 0 deletions tests/Magick.NET.Tests/MagickImageTests/TheMeanShiftMethod.cs
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,22 @@ public partial class MagickImageTests
{
public class TheMeanShiftMethod
{
[Fact]
public void ShouldThrowExceptionWhenWidthIsNegative()
{
using var image = new MagickImage(Files.FujiFilmFinePixS1ProPNG);

Assert.Throws<ArgumentException>("width", () => image.MeanShift(-1, 1));
}

[Fact]
public void ShouldThrowExceptionWhenHeightIsNegative()
{
using var image = new MagickImage(Files.FujiFilmFinePixS1ProPNG);

Assert.Throws<ArgumentException>("height", () => image.MeanShift(1, -1));
}

[Fact]
public void ShouldNotChangeImageWhenSizeIsOne()
{
Expand Down

0 comments on commit 78b7465

Please sign in to comment.