Skip to content

Commit c37b0bd

Browse files
committed
Added gravity overload to the Splice method (#1881).
1 parent 55a1155 commit c37b0bd

File tree

5 files changed

+47
-2
lines changed

5 files changed

+47
-2
lines changed

src/Magick.NET.Core/IMagickImageCreateOperations.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1154,6 +1154,14 @@ public interface IMagickImageCreateOperations
11541154
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
11551155
void Splice(IMagickGeometry geometry);
11561156

1157+
/// <summary>
1158+
/// Splice the background color into the image.
1159+
/// </summary>
1160+
/// <param name="geometry">The geometry to use.</param>
1161+
/// <param name="gravity">The gravity to use.</param>
1162+
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
1163+
void Splice(IMagickGeometry geometry, Gravity gravity);
1164+
11571165
/// <summary>
11581166
/// Spread pixels randomly within image.
11591167
/// </summary>

src/Magick.NET/MagickImage.CloneMutator.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -653,7 +653,10 @@ public void SparseColor(Channels channels, SparseColorMethod method, params ISpa
653653
=> SparseColor(channels, method, (IEnumerable<ISparseColorArg<QuantumType>>)args);
654654

655655
public void Splice(IMagickGeometry geometry)
656-
=> SetResult(NativeMagickImage.Splice(MagickRectangle.FromGeometry(geometry, (uint)NativeMagickImage.Width_Get(), (uint)NativeMagickImage.Height_Get())));
656+
=> Splice(geometry, Gravity.Undefined);
657+
658+
public void Splice(IMagickGeometry geometry, Gravity gravity)
659+
=> SetResult(NativeMagickImage.Splice(MagickRectangle.FromGeometry(geometry, (uint)NativeMagickImage.Width_Get(), (uint)NativeMagickImage.Height_Get()), gravity));
657660

658661
public void Spread()
659662
=> Spread(NativeMagickImage.Interpolate_Get(), 3);

src/Magick.NET/MagickImage.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6358,6 +6358,18 @@ public void Splice(IMagickGeometry geometry)
63586358
mutator.Splice(geometry);
63596359
}
63606360

6361+
/// <summary>
6362+
/// Splice the background color into the image.
6363+
/// </summary>
6364+
/// <param name="geometry">The geometry to use.</param>
6365+
/// <param name="gravity">The gravity to use.</param>
6366+
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
6367+
public void Splice(IMagickGeometry geometry, Gravity gravity)
6368+
{
6369+
using var mutator = new Mutator(_nativeInstance);
6370+
mutator.Splice(geometry, gravity);
6371+
}
6372+
63616373
/// <summary>
63626374
/// Spread pixels randomly within image.
63636375
/// </summary>

src/Magick.NET/Native/MagickImage.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -718,7 +718,7 @@ private unsafe sealed partial class NativeMagickImage : NativeInstance, INativeM
718718
public partial IntPtr SparseColor(Channels channel, SparseColorMethod method, double[] values, nuint length);
719719

720720
[Throws]
721-
public partial IntPtr Splice(MagickRectangle geometry);
721+
public partial IntPtr Splice(MagickRectangle geometry, Gravity gravity);
722722

723723
[Throws]
724724
public partial IntPtr Spread(PixelInterpolateMethod method, double radius);

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@ public void ShouldThrowExceptionWhenGeometryIsNull()
1919
Assert.Throws<ArgumentNullException>("geometry", () => image.Splice(null!));
2020
}
2121

22+
[Fact]
23+
public void ShouldThrowExceptionWhenGeometryIsNullAndGravityIsSpecified()
24+
{
25+
using var image = new MagickImage();
26+
27+
Assert.Throws<ArgumentNullException>("geometry", () => image.Splice(null!, Gravity.Center));
28+
}
29+
2230
[Fact]
2331
public void ShouldSpliceTheBackgroundColorIntoTheImage()
2432
{
@@ -31,5 +39,19 @@ public void ShouldSpliceTheBackgroundColorIntoTheImage()
3139
ColorAssert.Equal(MagickColors.Fuchsia, image, 105, 50);
3240
ColorAssert.Equal(new MagickColor("#0000"), image, 115, 70);
3341
}
42+
43+
[Fact]
44+
public void ShouldUseTheGravityWhenSplicingTheBackgroundColorIntoTheImage()
45+
{
46+
using var image = new MagickImage(Files.SnakewarePNG);
47+
image.BackgroundColor = MagickColors.Fuchsia;
48+
image.Splice(new MagickGeometry(105, 50, 10, 20), Gravity.Center);
49+
50+
Assert.Equal(296U, image.Width);
51+
Assert.Equal(87U, image.Height);
52+
image.Write("i:/test.png");
53+
ColorAssert.Equal(MagickColors.Fuchsia, image, 110, 60);
54+
ColorAssert.Equal(new MagickColor("#0000"), image, 109, 59);
55+
}
3456
}
3557
}

0 commit comments

Comments
 (0)