Skip to content

Commit c591004

Browse files
committed
Added CloneArea to MagickImage that will replace the Clone overload with a MagickGeometry.
1 parent 32996b4 commit c591004

File tree

4 files changed

+155
-20
lines changed

4 files changed

+155
-20
lines changed

src/Magick.NET.Core/IMagickImage{TQuantumType}.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public partial interface IMagickImage<TQuantumType> : IMagickImage, IComparable<
4949
/// <param name="geometry">The area to clone.</param>
5050
/// <returns>A clone of the current image.</returns>
5151
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
52+
[Obsolete($"This property will be removed in the next major release, use {nameof(CloneArea)} instead.")]
5253
IMagickImage<TQuantumType> Clone(IMagickGeometry geometry);
5354

5455
/// <summary>
@@ -57,6 +58,7 @@ public partial interface IMagickImage<TQuantumType> : IMagickImage, IComparable<
5758
/// <param name="width">The width of the area to clone.</param>
5859
/// <param name="height">The height of the area to clone.</param>
5960
/// <returns>A clone of the current image.</returns>
61+
[Obsolete($"This property will be removed in the next major release, use {nameof(CloneArea)} instead.")]
6062
IMagickImage<TQuantumType> Clone(uint width, uint height);
6163

6264
/// <summary>
@@ -67,8 +69,35 @@ public partial interface IMagickImage<TQuantumType> : IMagickImage, IComparable<
6769
/// <param name="width">The width of the area to clone.</param>
6870
/// <param name="height">The height of the area to clone.</param>
6971
/// <returns>A clone of the current image.</returns>
72+
[Obsolete($"This property will be removed in the next major release, use {nameof(CloneArea)} instead.")]
7073
IMagickImage<TQuantumType> Clone(int x, int y, uint width, uint height);
7174

75+
/// <summary>
76+
/// Creates a clone of the current image with the specified geometry.
77+
/// </summary>
78+
/// <param name="geometry">The area to clone.</param>
79+
/// <returns>A clone of the current image.</returns>
80+
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
81+
IMagickImage<TQuantumType> CloneArea(IMagickGeometry geometry);
82+
83+
/// <summary>
84+
/// Creates a clone of the current image.
85+
/// </summary>
86+
/// <param name="width">The width of the area to clone.</param>
87+
/// <param name="height">The height of the area to clone.</param>
88+
/// <returns>A clone of the current image.</returns>
89+
IMagickImage<TQuantumType> CloneArea(uint width, uint height);
90+
91+
/// <summary>
92+
/// Creates a clone of the current image.
93+
/// </summary>
94+
/// <param name="x">The X offset from origin.</param>
95+
/// <param name="y">The Y offset from origin.</param>
96+
/// <param name="width">The width of the area to clone.</param>
97+
/// <param name="height">The height of the area to clone.</param>
98+
/// <returns>A clone of the current image.</returns>
99+
IMagickImage<TQuantumType> CloneArea(int x, int y, uint width, uint height);
100+
72101
/// <summary>
73102
/// Sets the alpha channel to the specified color.
74103
/// </summary>

src/Magick.NET/MagickImage.cs

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1519,7 +1519,39 @@ public IMagickImage<QuantumType> Clone()
15191519
/// <param name="geometry">The area to clone.</param>
15201520
/// <returns>A clone of the current image.</returns>
15211521
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
1522+
[Obsolete($"This property will be removed in the next major release, use {nameof(CloneArea)} instead.")]
15221523
public IMagickImage<QuantumType> Clone(IMagickGeometry geometry)
1524+
=> CloneArea(geometry);
1525+
1526+
/// <summary>
1527+
/// Creates a clone of the current image.
1528+
/// </summary>
1529+
/// <param name="width">The width of the area to clone.</param>
1530+
/// <param name="height">The height of the area to clone.</param>
1531+
/// <returns>A clone of the current image.</returns>
1532+
[Obsolete($"This property will be removed in the next major release, use {nameof(CloneArea)} instead.")]
1533+
public IMagickImage<QuantumType> Clone(uint width, uint height)
1534+
=> CloneArea(new MagickGeometry(width, height));
1535+
1536+
/// <summary>
1537+
/// Creates a clone of the current image.
1538+
/// </summary>
1539+
/// <param name="x">The X offset from origin.</param>
1540+
/// <param name="y">The Y offset from origin.</param>
1541+
/// <param name="width">The width of the area to clone.</param>
1542+
/// <param name="height">The height of the area to clone.</param>
1543+
/// <returns>A clone of the current image.</returns>
1544+
[Obsolete($"This property will be removed in the next major release, use {nameof(CloneArea)} instead.")]
1545+
public IMagickImage<QuantumType> Clone(int x, int y, uint width, uint height)
1546+
=> CloneArea(new MagickGeometry(x, y, width, height));
1547+
1548+
/// <summary>
1549+
/// Creates a clone of the current image with the specified geometry.
1550+
/// </summary>
1551+
/// <param name="geometry">The area to clone.</param>
1552+
/// <returns>A clone of the current image.</returns>
1553+
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
1554+
public IMagickImage<QuantumType> CloneArea(IMagickGeometry geometry)
15231555
{
15241556
Throw.IfNull(nameof(geometry), geometry);
15251557

@@ -1537,8 +1569,8 @@ public IMagickImage<QuantumType> Clone(IMagickGeometry geometry)
15371569
/// <param name="width">The width of the area to clone.</param>
15381570
/// <param name="height">The height of the area to clone.</param>
15391571
/// <returns>A clone of the current image.</returns>
1540-
public IMagickImage<QuantumType> Clone(uint width, uint height)
1541-
=> Clone(new MagickGeometry(width, height));
1572+
public IMagickImage<QuantumType> CloneArea(uint width, uint height)
1573+
=> CloneArea(new MagickGeometry(width, height));
15421574

15431575
/// <summary>
15441576
/// Creates a clone of the current image.
@@ -1548,8 +1580,8 @@ public IMagickImage<QuantumType> Clone(uint width, uint height)
15481580
/// <param name="width">The width of the area to clone.</param>
15491581
/// <param name="height">The height of the area to clone.</param>
15501582
/// <returns>A clone of the current image.</returns>
1551-
public IMagickImage<QuantumType> Clone(int x, int y, uint width, uint height)
1552-
=> Clone(new MagickGeometry(x, y, width, height));
1583+
public IMagickImage<QuantumType> CloneArea(int x, int y, uint width, uint height)
1584+
=> CloneArea(new MagickGeometry(x, y, width, height));
15531585

15541586
/// <summary>
15551587
/// Apply a color lookup table (CLUT) to the image.
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// Copyright Dirk Lemstra https://github.com/dlemstra/Magick.NET.
2+
// Licensed under the Apache License, Version 2.0.
3+
4+
using ImageMagick;
5+
using Xunit;
6+
7+
namespace Magick.NET.Tests;
8+
9+
public partial class MagickImageTests
10+
{
11+
public class TheCloneAreaMethod
12+
{
13+
public class WithGeometry
14+
{
15+
[Fact]
16+
public void ShouldCloneTheSpecifiedGeometry()
17+
{
18+
using var icon = new MagickImage(Files.MagickNETIconPNG);
19+
using var area = icon.Clone();
20+
area.Crop(64, 64, Gravity.Southeast);
21+
area.ResetPage();
22+
area.Crop(64, 32, Gravity.North);
23+
24+
using var part = icon.CloneArea(new MagickGeometry(64, 64, 64, 32));
25+
26+
Assert.Equal(area.Width, part.Width);
27+
Assert.Equal(area.Height, part.Height);
28+
29+
Assert.Equal(0.0, area.Compare(part, ErrorMetric.RootMeanSquared));
30+
}
31+
}
32+
33+
public class WithWidthAndHeight
34+
{
35+
[Fact]
36+
public void ShouldClonePartOfTheImageWhenWidthAndHeightAreSpecified()
37+
{
38+
using var icon = new MagickImage(Files.MagickNETIconPNG);
39+
using var area = icon.Clone();
40+
area.Crop(32, 64, Gravity.Northwest);
41+
42+
Assert.Equal(32U, area.Width);
43+
Assert.Equal(64U, area.Height);
44+
45+
using var part = icon.CloneArea(32, 64);
46+
47+
Assert.Equal(area.Width, part.Width);
48+
Assert.Equal(area.Height, part.Height);
49+
Assert.Equal(0.0, area.Compare(part, ErrorMetric.RootMeanSquared));
50+
Assert.Equal(8192, part.ToByteArray(MagickFormat.Rgba).Length);
51+
}
52+
}
53+
54+
public class WithWidthAndHeightAndOffset
55+
{
56+
[Fact]
57+
public void ShouldCloneUsingTheSpecifiedOffset()
58+
{
59+
using var icon = new MagickImage(Files.MagickNETIconPNG);
60+
using var area = icon.Clone();
61+
area.Crop(64, 64, Gravity.Southeast);
62+
area.ResetPage();
63+
area.Crop(64, 32, Gravity.North);
64+
65+
using var part = icon.CloneArea(64, 64, 64, 32);
66+
67+
Assert.Equal(area.Width, part.Width);
68+
Assert.Equal(area.Height, part.Height);
69+
70+
Assert.Equal(0.0, area.Compare(part, ErrorMetric.RootMeanSquared));
71+
}
72+
}
73+
}
74+
}

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

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright Dirk Lemstra https://github.com/dlemstra/Magick.NET.
22
// Licensed under the Apache License, Version 2.0.
33

4+
using System;
45
using ImageMagick;
56
using Xunit;
67

@@ -10,31 +11,28 @@ public partial class MagickImageTests
1011
{
1112
public class TheCloneMethod
1213
{
13-
public class WithoutArguments
14+
[Fact]
15+
public void ShouldThrowExceptionWhenNoImageIsRead()
1416
{
15-
[Fact]
16-
public void ShouldThrowExceptionWhenNoImageIsRead()
17-
{
18-
using var image = new MagickImage();
17+
using var image = new MagickImage();
1918

20-
Assert.Throws<MagickCorruptImageErrorException>(() => image.Clone());
21-
}
19+
Assert.Throws<MagickCorruptImageErrorException>(() => image.Clone());
20+
}
2221

23-
[Fact]
24-
public void ShouldCloneTheImage()
25-
{
26-
using var image = new MagickImage(Files.Builtin.Logo);
22+
[Fact]
23+
public void ShouldCloneTheImage()
24+
{
25+
using var image = new MagickImage(Files.Builtin.Logo);
2726

28-
var clone = image.Clone();
27+
var clone = image.Clone();
2928

30-
Assert.Equal(image, clone);
31-
Assert.False(ReferenceEquals(image, clone));
32-
}
29+
Assert.Equal(image, clone);
30+
Assert.False(ReferenceEquals(image, clone));
3331
}
3432

33+
[Obsolete]
3534
public class WithWidthAndHeight
3635
{
37-
[Fact]
3836
public void ShouldClonePartOfTheImageWhenWidthAndHeightAreSpecified()
3937
{
4038
using var icon = new MagickImage(Files.MagickNETIconPNG);
@@ -53,6 +51,7 @@ public void ShouldClonePartOfTheImageWhenWidthAndHeightAreSpecified()
5351
}
5452
}
5553

54+
[Obsolete]
5655
public class WithWidthAndHeightAndOffset
5756
{
5857
[Fact]
@@ -73,6 +72,7 @@ public void ShouldCloneUsingTheSpecifiedOffset()
7372
}
7473
}
7574

75+
[Obsolete]
7676
public class WithGeometry
7777
{
7878
[Fact]

0 commit comments

Comments
 (0)