Skip to content

Commit

Permalink
fix: add guard for MagickImage.Morphology (#1618)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gounlaf authored Apr 30, 2024
1 parent 4abaf2c commit d58472f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 13 deletions.
12 changes: 6 additions & 6 deletions src/Magick.NET.Core/IMagickImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2163,7 +2163,7 @@ Interlace Interlace
/// <param name="method">The morphology method.</param>
/// <param name="kernel">Built-in kernel.</param>
/// <param name="channels">The channels to apply the kernel to.</param>
/// <param name="iterations">The number of iterations.</param>
/// <param name="iterations">The number of iterations. A value of -1 means loop until no change found.</param>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
void Morphology(MorphologyMethod method, Kernel kernel, Channels channels, int iterations);

Expand All @@ -2172,7 +2172,7 @@ Interlace Interlace
/// </summary>
/// <param name="method">The morphology method.</param>
/// <param name="kernel">Built-in kernel.</param>
/// <param name="iterations">The number of iterations.</param>
/// <param name="iterations">The number of iterations. A value of -1 means loop until no change found.</param>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
void Morphology(MorphologyMethod method, Kernel kernel, int iterations);

Expand Down Expand Up @@ -2202,7 +2202,7 @@ Interlace Interlace
/// <param name="kernel">Built-in kernel.</param>
/// <param name="arguments">Kernel arguments.</param>
/// <param name="channels">The channels to apply the kernel to.</param>
/// <param name="iterations">The number of iterations.</param>
/// <param name="iterations">The number of iterations. A value of -1 means loop until no change found.</param>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
void Morphology(MorphologyMethod method, Kernel kernel, string? arguments, Channels channels, int iterations);

Expand All @@ -2212,7 +2212,7 @@ Interlace Interlace
/// <param name="method">The morphology method.</param>
/// <param name="kernel">Built-in kernel.</param>
/// <param name="arguments">Kernel arguments.</param>
/// <param name="iterations">The number of iterations.</param>
/// <param name="iterations">The number of iterations. A value of -1 means loop until no change found.</param>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
void Morphology(MorphologyMethod method, Kernel kernel, string? arguments, int iterations);

Expand All @@ -2239,7 +2239,7 @@ Interlace Interlace
/// <param name="method">The morphology method.</param>
/// <param name="userKernel">User suplied kernel.</param>
/// <param name="channels">The channels to apply the kernel to.</param>
/// <param name="iterations">The number of iterations.</param>
/// <param name="iterations">The number of iterations. A value of -1 means loop until no change found.</param>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
void Morphology(MorphologyMethod method, string userKernel, Channels channels, int iterations);

Expand All @@ -2248,7 +2248,7 @@ Interlace Interlace
/// </summary>
/// <param name="method">The morphology method.</param>
/// <param name="userKernel">User suplied kernel.</param>
/// <param name="iterations">The number of iterations.</param>
/// <param name="iterations">The number of iterations. A value of -1 means loop until no change found.</param>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
void Morphology(MorphologyMethod method, string userKernel, int iterations);

Expand Down
18 changes: 11 additions & 7 deletions src/Magick.NET/MagickImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4140,7 +4140,7 @@ public void Morphology(MorphologyMethod method, Kernel kernel, Channels channels
/// <param name="method">The morphology method.</param>
/// <param name="kernel">Built-in kernel.</param>
/// <param name="channels">The channels to apply the kernel to.</param>
/// <param name="iterations">The number of iterations.</param>
/// <param name="iterations">The number of iterations. A value of -1 means loop until no change found.</param>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
public void Morphology(MorphologyMethod method, Kernel kernel, Channels channels, int iterations)
=> Morphology(method, kernel, string.Empty, channels, iterations);
Expand All @@ -4150,7 +4150,7 @@ public void Morphology(MorphologyMethod method, Kernel kernel, Channels channels
/// </summary>
/// <param name="method">The morphology method.</param>
/// <param name="kernel">Built-in kernel.</param>
/// <param name="iterations">The number of iterations.</param>
/// <param name="iterations">The number of iterations. A value of -1 means loop until no change found.</param>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
public void Morphology(MorphologyMethod method, Kernel kernel, int iterations)
=> Morphology(method, kernel, string.Empty, ImageMagick.Channels.Undefined, iterations);
Expand Down Expand Up @@ -4183,7 +4183,7 @@ public void Morphology(MorphologyMethod method, Kernel kernel, string? arguments
/// <param name="kernel">Built-in kernel.</param>
/// <param name="arguments">Kernel arguments.</param>
/// <param name="channels">The channels to apply the kernel to.</param>
/// <param name="iterations">The number of iterations.</param>
/// <param name="iterations">The number of iterations. A value of -1 means loop until no change found.</param>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
public void Morphology(MorphologyMethod method, Kernel kernel, string? arguments, Channels channels, int iterations)
{
Expand All @@ -4198,7 +4198,7 @@ public void Morphology(MorphologyMethod method, Kernel kernel, string? arguments
/// <param name="method">The morphology method.</param>
/// <param name="kernel">Built-in kernel.</param>
/// <param name="arguments">Kernel arguments.</param>
/// <param name="iterations">The number of iterations.</param>
/// <param name="iterations">The number of iterations. A value of -1 means loop until no change found.</param>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
public void Morphology(MorphologyMethod method, Kernel kernel, string? arguments, int iterations)
=> Morphology(method, kernel, arguments, ImageMagick.Channels.Undefined, iterations);
Expand Down Expand Up @@ -4228,17 +4228,21 @@ public void Morphology(MorphologyMethod method, string userKernel, Channels chan
/// <param name="method">The morphology method.</param>
/// <param name="userKernel">User suplied kernel.</param>
/// <param name="channels">The channels to apply the kernel to.</param>
/// <param name="iterations">The number of iterations.</param>
/// <param name="iterations">The number of iterations. A value of -1 means loop until no change found.</param>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
public void Morphology(MorphologyMethod method, string userKernel, Channels channels, int iterations)
=> _nativeInstance.Morphology(method, userKernel, channels, iterations);
{
Throw.IfTrue(nameof(iterations), iterations < -1, "The number of iterations must be unlimited (-1) or positive");

_nativeInstance.Morphology(method, userKernel, channels, iterations);
}

/// <summary>
/// Applies a kernel to the image according to the given mophology method.
/// </summary>
/// <param name="method">The morphology method.</param>
/// <param name="userKernel">User suplied kernel.</param>
/// <param name="iterations">The number of iterations.</param>
/// <param name="iterations">The number of iterations. A value of -1 means loop until no change found.</param>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
public void Morphology(MorphologyMethod method, string userKernel, int iterations)
=> Morphology(method, userKernel, ImageMagick.Channels.Undefined, iterations);
Expand Down
10 changes: 10 additions & 0 deletions tests/Magick.NET.Tests/MagickImageTests/TheMorphologyMethod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ public void ShouldThrowExceptionWhenSettingsIsNull()
Assert.Throws<ArgumentNullException>("settings", () => image.Morphology(null));
}

[Fact]
public void ShouldThrowExceptionWhenIterationsIsLowerThanMinusOne()
{
using var image = new MagickImage();
var settings = new MorphologySettings();
settings.Iterations = -2;

Assert.Throws<ArgumentException>("iterations", () => image.Morphology(settings));
}

[Fact]
public void ShouldUseTheSpecifiedSettings()
{
Expand Down

0 comments on commit d58472f

Please sign in to comment.