Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add guards for MagickImage.MeanShift #1612

Merged
merged 1 commit into from
Apr 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading