Skip to content

Commit

Permalink
Moved Crop to IMagickImageCreateOperations.
Browse files Browse the repository at this point in the history
  • Loading branch information
dlemstra committed Nov 13, 2024
1 parent 1ac655b commit 28c91e4
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 44 deletions.
36 changes: 0 additions & 36 deletions src/Magick.NET.Core/IMagickImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -853,42 +853,6 @@ public partial interface IMagickImage : IMagickImageCreateOperations, IDisposabl
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
void CopyPixels(IMagickImage source, IMagickGeometry geometry, int x, int y, Channels channels);

/// <summary>
/// Crop image (subregion of original image). <see cref="ResetPage"/> should be called unless
/// the <see cref="Page"/> information is needed.
/// </summary>
/// <param name="width">The width of the subregion to crop.</param>
/// <param name="height">The height of the subregion to crop.</param>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
void Crop(uint width, uint height);

/// <summary>
/// Crop image (subregion of original image). <see cref="ResetPage"/> should be called unless
/// the <see cref="Page"/> information is needed.
/// </summary>
/// <param name="width">The width of the subregion to crop.</param>
/// <param name="height">The height of the subregion to crop.</param>
/// <param name="gravity">The position where the cropping should start from.</param>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
void Crop(uint width, uint height, Gravity gravity);

/// <summary>
/// Crop image (subregion of original image). <see cref="ResetPage"/> should be called unless
/// the <see cref="Page"/> information is needed.
/// </summary>
/// <param name="geometry">The subregion to crop.</param>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
void Crop(IMagickGeometry geometry);

/// <summary>
/// Crop image (subregion of original image). <see cref="ResetPage"/> should be called unless
/// the <see cref="Page"/> information is needed.
/// </summary>
/// <param name="geometry">The subregion to crop.</param>
/// <param name="gravity">The position where the cropping should start from.</param>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
void Crop(IMagickGeometry geometry, Gravity gravity);

/// <summary>
/// Displaces an image's colormap by a given number of positions.
/// </summary>
Expand Down
36 changes: 36 additions & 0 deletions src/Magick.NET.Core/IMagickImageCreateOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,42 @@ public interface IMagickImageCreateOperations
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
void Convolve(IConvolveMatrix matrix);

/// <summary>
/// Crop image (subregion of original image). <see cref="IMagickImage.ResetPage"/> should be called unless
/// the <see cref="IMagickImage.Page"/> information is needed.
/// </summary>
/// <param name="width">The width of the subregion to crop.</param>
/// <param name="height">The height of the subregion to crop.</param>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
void Crop(uint width, uint height);

/// <summary>
/// Crop image (subregion of original image). <see cref="IMagickImage.ResetPage"/> should be called unless
/// the <see cref="Page"/> information is needed.
/// </summary>
/// <param name="width">The width of the subregion to crop.</param>
/// <param name="height">The height of the subregion to crop.</param>
/// <param name="gravity">The position where the cropping should start from.</param>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
void Crop(uint width, uint height, Gravity gravity);

/// <summary>
/// Crop image (subregion of original image). <see cref="IMagickImage.ResetPage"/> should be called unless
/// the <see cref="IMagickImage.Page"/> information is needed.
/// </summary>
/// <param name="geometry">The subregion to crop.</param>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
void Crop(IMagickGeometry geometry);

/// <summary>
/// Crop image (subregion of original image). <see cref="IMagickImage.ResetPage"/> should be called unless
/// the <see cref="IMagickImage.Page"/> information is needed.
/// </summary>
/// <param name="geometry">The subregion to crop.</param>
/// <param name="gravity">The position where the cropping should start from.</param>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
void Crop(IMagickGeometry geometry, Gravity gravity);

/// <summary>
/// Resize image to specified size.
/// <para />
Expand Down
16 changes: 16 additions & 0 deletions src/Magick.NET/MagickImage.CloneMutator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,22 @@ public void Convolve(IConvolveMatrix matrix)
SetResult(NativeMagickImage.Convolve(matrix));
}

public void Crop(uint width, uint height)
=> Crop(width, height, Gravity.Undefined);

public void Crop(uint width, uint height, Gravity gravity)
=> Crop(new MagickGeometry(0, 0, width, height), gravity);

public void Crop(IMagickGeometry geometry)
=> Crop(geometry, Gravity.Undefined);

public void Crop(IMagickGeometry geometry, Gravity gravity)
{
Throw.IfNull(nameof(geometry), geometry);

SetResult(NativeMagickImage.Crop(geometry.ToString(), gravity));
}

public void Resize(uint width, uint height)
=> Resize(new MagickGeometry(width, height));

Expand Down
20 changes: 14 additions & 6 deletions src/Magick.NET/MagickImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2424,7 +2424,10 @@ public void CopyPixels(IMagickImage source, IMagickGeometry geometry, int x, int
/// <param name="height">The height of the subregion to crop.</param>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
public void Crop(uint width, uint height)
=> Crop(width, height, Gravity.Undefined);
{
using var mutator = new Mutator(_nativeInstance);
mutator.Crop(width, height);
}

/// <summary>
/// Crop image (subregion of original image). <see cref="ResetPage"/> should be called unless
Expand All @@ -2435,7 +2438,10 @@ public void Crop(uint width, uint height)
/// <param name="gravity">The position where the cropping should start from.</param>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
public void Crop(uint width, uint height, Gravity gravity)
=> Crop(new MagickGeometry(0, 0, width, height), gravity);
{
using var mutator = new Mutator(_nativeInstance);
mutator.Crop(width, height, gravity);
}

/// <summary>
/// Crop image (subregion of original image). <see cref="ResetPage"/> should be called unless
Expand All @@ -2444,7 +2450,10 @@ public void Crop(uint width, uint height, Gravity gravity)
/// <param name="geometry">The subregion to crop.</param>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
public void Crop(IMagickGeometry geometry)
=> Crop(geometry, Gravity.Undefined);
{
using var mutator = new Mutator(_nativeInstance);
mutator.Crop(geometry);
}

/// <summary>
/// Crop image (subregion of original image). <see cref="ResetPage"/> should be called unless
Expand All @@ -2455,9 +2464,8 @@ public void Crop(IMagickGeometry geometry)
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
public void Crop(IMagickGeometry geometry, Gravity gravity)
{
Throw.IfNull(nameof(geometry), geometry);

_nativeInstance.Crop(geometry.ToString(), gravity);
using var mutator = new Mutator(_nativeInstance);
mutator.Crop(geometry, gravity);
}

/// <summary>
Expand Down
3 changes: 1 addition & 2 deletions src/Magick.NET/Native/MagickImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -353,8 +353,7 @@ private unsafe sealed partial class NativeMagickImage : NativeInstance, INativeM
public partial void CopyPixels(IMagickImage image, MagickRectangle geometry, OffsetInfo offset, Channels channels);

[Throws]
[SetInstance]
public partial void Crop(string geometry, Gravity gravity);
public partial IntPtr Crop(string geometry, Gravity gravity);

[Throws]
public partial IntPtr CropToTiles(string geometry);
Expand Down
17 changes: 17 additions & 0 deletions tests/Magick.NET.Tests/TestIssue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,22 @@ public class TestIssue
[Fact]
public void RunTest()
{
var drawables = new Drawables();

drawables.FontPointSize(40)
.Font(@"I:\issues\im7\1752\font1.ttf")
.FillColor(MagickColors.Black)
.TextAlignment(TextAlignment.Left)
.Text(100, 50, "This is such a nice font!");

drawables.FontPointSize(40)
.Font(@"I:\issues\im7\1752\font2.ttf")
.FillColor(MagickColors.Black)
.TextAlignment(TextAlignment.Left)
.Text(100, 100, "This is even better, or does it look the same?");

using var image = new MagickImage(MagickColors.White, 1000, 800);
drawables.Draw(image);
image.Write(@"I:\issues\im7\1752\z.png");
}
}

0 comments on commit 28c91e4

Please sign in to comment.