Skip to content

Commit d58472f

Browse files
authored
fix: add guard for MagickImage.Morphology (#1618)
1 parent 4abaf2c commit d58472f

File tree

3 files changed

+27
-13
lines changed

3 files changed

+27
-13
lines changed

src/Magick.NET.Core/IMagickImage.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2163,7 +2163,7 @@ Interlace Interlace
21632163
/// <param name="method">The morphology method.</param>
21642164
/// <param name="kernel">Built-in kernel.</param>
21652165
/// <param name="channels">The channels to apply the kernel to.</param>
2166-
/// <param name="iterations">The number of iterations.</param>
2166+
/// <param name="iterations">The number of iterations. A value of -1 means loop until no change found.</param>
21672167
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
21682168
void Morphology(MorphologyMethod method, Kernel kernel, Channels channels, int iterations);
21692169

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

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

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

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

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

src/Magick.NET/MagickImage.cs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4140,7 +4140,7 @@ public void Morphology(MorphologyMethod method, Kernel kernel, Channels channels
41404140
/// <param name="method">The morphology method.</param>
41414141
/// <param name="kernel">Built-in kernel.</param>
41424142
/// <param name="channels">The channels to apply the kernel to.</param>
4143-
/// <param name="iterations">The number of iterations.</param>
4143+
/// <param name="iterations">The number of iterations. A value of -1 means loop until no change found.</param>
41444144
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
41454145
public void Morphology(MorphologyMethod method, Kernel kernel, Channels channels, int iterations)
41464146
=> Morphology(method, kernel, string.Empty, channels, iterations);
@@ -4150,7 +4150,7 @@ public void Morphology(MorphologyMethod method, Kernel kernel, Channels channels
41504150
/// </summary>
41514151
/// <param name="method">The morphology method.</param>
41524152
/// <param name="kernel">Built-in kernel.</param>
4153-
/// <param name="iterations">The number of iterations.</param>
4153+
/// <param name="iterations">The number of iterations. A value of -1 means loop until no change found.</param>
41544154
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
41554155
public void Morphology(MorphologyMethod method, Kernel kernel, int iterations)
41564156
=> Morphology(method, kernel, string.Empty, ImageMagick.Channels.Undefined, iterations);
@@ -4183,7 +4183,7 @@ public void Morphology(MorphologyMethod method, Kernel kernel, string? arguments
41834183
/// <param name="kernel">Built-in kernel.</param>
41844184
/// <param name="arguments">Kernel arguments.</param>
41854185
/// <param name="channels">The channels to apply the kernel to.</param>
4186-
/// <param name="iterations">The number of iterations.</param>
4186+
/// <param name="iterations">The number of iterations. A value of -1 means loop until no change found.</param>
41874187
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
41884188
public void Morphology(MorphologyMethod method, Kernel kernel, string? arguments, Channels channels, int iterations)
41894189
{
@@ -4198,7 +4198,7 @@ public void Morphology(MorphologyMethod method, Kernel kernel, string? arguments
41984198
/// <param name="method">The morphology method.</param>
41994199
/// <param name="kernel">Built-in kernel.</param>
42004200
/// <param name="arguments">Kernel arguments.</param>
4201-
/// <param name="iterations">The number of iterations.</param>
4201+
/// <param name="iterations">The number of iterations. A value of -1 means loop until no change found.</param>
42024202
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
42034203
public void Morphology(MorphologyMethod method, Kernel kernel, string? arguments, int iterations)
42044204
=> Morphology(method, kernel, arguments, ImageMagick.Channels.Undefined, iterations);
@@ -4228,17 +4228,21 @@ public void Morphology(MorphologyMethod method, string userKernel, Channels chan
42284228
/// <param name="method">The morphology method.</param>
42294229
/// <param name="userKernel">User suplied kernel.</param>
42304230
/// <param name="channels">The channels to apply the kernel to.</param>
4231-
/// <param name="iterations">The number of iterations.</param>
4231+
/// <param name="iterations">The number of iterations. A value of -1 means loop until no change found.</param>
42324232
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
42334233
public void Morphology(MorphologyMethod method, string userKernel, Channels channels, int iterations)
4234-
=> _nativeInstance.Morphology(method, userKernel, channels, iterations);
4234+
{
4235+
Throw.IfTrue(nameof(iterations), iterations < -1, "The number of iterations must be unlimited (-1) or positive");
4236+
4237+
_nativeInstance.Morphology(method, userKernel, channels, iterations);
4238+
}
42354239

42364240
/// <summary>
42374241
/// Applies a kernel to the image according to the given mophology method.
42384242
/// </summary>
42394243
/// <param name="method">The morphology method.</param>
42404244
/// <param name="userKernel">User suplied kernel.</param>
4241-
/// <param name="iterations">The number of iterations.</param>
4245+
/// <param name="iterations">The number of iterations. A value of -1 means loop until no change found.</param>
42424246
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
42434247
public void Morphology(MorphologyMethod method, string userKernel, int iterations)
42444248
=> Morphology(method, userKernel, ImageMagick.Channels.Undefined, iterations);

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,16 @@ public void ShouldThrowExceptionWhenSettingsIsNull()
3838
Assert.Throws<ArgumentNullException>("settings", () => image.Morphology(null));
3939
}
4040

41+
[Fact]
42+
public void ShouldThrowExceptionWhenIterationsIsLowerThanMinusOne()
43+
{
44+
using var image = new MagickImage();
45+
var settings = new MorphologySettings();
46+
settings.Iterations = -2;
47+
48+
Assert.Throws<ArgumentException>("iterations", () => image.Morphology(settings));
49+
}
50+
4151
[Fact]
4252
public void ShouldUseTheSpecifiedSettings()
4353
{

0 commit comments

Comments
 (0)