-
-
Notifications
You must be signed in to change notification settings - Fork 415
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added CloneAndMutate to IMagickImage that can be used to efficiently …
…clone and mutate an image (#1577).
- Loading branch information
Showing
9 changed files
with
226 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// Copyright Dirk Lemstra https://github.com/dlemstra/Magick.NET. | ||
// Licensed under the Apache License, Version 2.0. | ||
|
||
namespace ImageMagick; | ||
|
||
/// <summary> | ||
/// Interface that can be used to efficiently clone and mutate an image. | ||
/// </summary> | ||
public interface IMagickImageCloneMutator : IMagickImageCreateOperations | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Copyright Dirk Lemstra https://github.com/dlemstra/Magick.NET. | ||
// Licensed under the Apache License, Version 2.0. | ||
|
||
namespace ImageMagick; | ||
|
||
/// <summary> | ||
/// Interface that represents ImageMagick operations that create a new image. | ||
/// </summary> | ||
public interface IMagickImageCreateOperations | ||
{ | ||
/// <summary> | ||
/// Resize image to specified size. | ||
/// <para /> | ||
/// Resize will fit the image into the requested size. It does NOT fill, the requested box size. | ||
/// Use the <see cref="IMagickGeometry"/> overload for more control over the resulting size. | ||
/// </summary> | ||
/// <param name="width">The new width.</param> | ||
/// <param name="height">The new height.</param> | ||
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception> | ||
void Resize(uint width, uint height); | ||
|
||
/// <summary> | ||
/// Resize image to specified geometry. | ||
/// </summary> | ||
/// <param name="geometry">The geometry to use.</param> | ||
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception> | ||
void Resize(IMagickGeometry geometry); | ||
|
||
/// <summary> | ||
/// Resize image to specified percentage. | ||
/// </summary> | ||
/// <param name="percentage">The percentage.</param> | ||
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception> | ||
void Resize(Percentage percentage); | ||
|
||
/// <summary> | ||
/// Resize image to specified percentage. | ||
/// </summary> | ||
/// <param name="percentageWidth">The percentage of the width.</param> | ||
/// <param name="percentageHeight">The percentage of the height.</param> | ||
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception> | ||
void Resize(Percentage percentageWidth, Percentage percentageHeight); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Copyright Dirk Lemstra https://github.com/dlemstra/Magick.NET. | ||
// Licensed under the Apache License, Version 2.0. | ||
|
||
using System; | ||
|
||
namespace ImageMagick; | ||
|
||
/// <content /> | ||
public partial class MagickImage | ||
{ | ||
private class CloneMutator : IMagickImageCloneMutator, IDisposable | ||
{ | ||
private IntPtr _result = IntPtr.Zero; | ||
|
||
public CloneMutator(NativeMagickImage nativeMagickImage) | ||
=> NativeMagickImage = nativeMagickImage; | ||
|
||
protected NativeMagickImage NativeMagickImage { get; } | ||
|
||
public void Dispose() | ||
{ | ||
if (_result != IntPtr.Zero) | ||
NativeMagickImage.DisposeInstance(_result); | ||
} | ||
|
||
public IntPtr GetResult() | ||
{ | ||
var result = _result; | ||
_result = IntPtr.Zero; | ||
return result; | ||
} | ||
|
||
public void Resize(uint width, uint height) | ||
=> Resize(new MagickGeometry(width, height)); | ||
|
||
public void Resize(IMagickGeometry geometry) | ||
{ | ||
Throw.IfNull(nameof(geometry), geometry); | ||
|
||
SetResult(NativeMagickImage.Resize(geometry.ToString())); | ||
} | ||
|
||
public void Resize(Percentage percentage) | ||
=> Resize(new MagickGeometry(percentage, percentage)); | ||
|
||
public void Resize(Percentage percentageWidth, Percentage percentageHeight) | ||
=> Resize(new MagickGeometry(percentageWidth, percentageHeight)); | ||
|
||
protected virtual void SetResult(IntPtr result) | ||
{ | ||
if (_result != IntPtr.Zero) | ||
throw new InvalidOperationException("Only a single operation can be executed."); | ||
|
||
_result = result; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Copyright Dirk Lemstra https://github.com/dlemstra/Magick.NET. | ||
// Licensed under the Apache License, Version 2.0. | ||
|
||
using System; | ||
|
||
namespace ImageMagick; | ||
|
||
/// <content /> | ||
public partial class MagickImage | ||
{ | ||
private sealed class Mutater : CloneMutator | ||
{ | ||
public Mutater(NativeMagickImage nativeMagickImage) | ||
: base(nativeMagickImage) | ||
{ | ||
} | ||
|
||
protected override void SetResult(IntPtr result) | ||
=> NativeMagickImage.Instance = result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
tests/Magick.NET.Tests/MagickImageTests/TheCloneAndMutateMethod.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Copyright Dirk Lemstra https://github.com/dlemstra/Magick.NET. | ||
// Licensed under the Apache License, Version 2.0. | ||
|
||
using System; | ||
using ImageMagick; | ||
using Xunit; | ||
|
||
namespace Magick.NET.Tests; | ||
|
||
public partial class MagickImageTests | ||
{ | ||
public class TheCloneAndMutateMethod | ||
{ | ||
[Fact] | ||
public void ShouldThrowExceptionWhenNoImageIsRead() | ||
{ | ||
using var image = new MagickImage(); | ||
|
||
Assert.Throws<MagickCorruptImageErrorException>(() => image.CloneAndMutate(static mutator => mutator.Resize(50, 50))); | ||
} | ||
|
||
[Fact] | ||
public void ShouldThrowExceptionWhenNoActionIsExecuted() | ||
{ | ||
using var image = new MagickImage(); | ||
|
||
Assert.Throws<InvalidOperationException>(() => image.CloneAndMutate(_ => { })); | ||
} | ||
|
||
[Fact] | ||
public void ShouldThrowExceptionWhenMultipleActionsAreExecuted() | ||
{ | ||
using var image = new MagickImage(Files.Builtin.Logo); | ||
|
||
using var clone = image.CloneAndMutate(mutator => | ||
{ | ||
mutator.Resize(100, 100); | ||
Assert.Throws<InvalidOperationException>(() => mutator.Resize(50, 50)); | ||
}); | ||
} | ||
|
||
[Fact] | ||
public void ShouldCloneAndMutateTheImage() | ||
{ | ||
using var image = new MagickImage(Files.Builtin.Logo); | ||
using var clone = image.CloneAndMutate(static mutator => mutator.Resize(100, 100)); | ||
|
||
Assert.NotEqual(image, clone); | ||
Assert.False(ReferenceEquals(image, clone)); | ||
Assert.Equal(100U, clone.Width); | ||
Assert.Equal(75U, clone.Height); | ||
} | ||
} | ||
} |