Skip to content

Commit 99e10c4

Browse files
committed
Refactored the compare method of MagickImage that required the diff image as an argument and changed that to a return value.
1 parent bed0aa6 commit 99e10c4

File tree

4 files changed

+53
-83
lines changed

4 files changed

+53
-83
lines changed

src/Magick.NET.Core/IMagickImage.cs

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -837,27 +837,6 @@ public partial interface IMagickImage : IDisposable
837837
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
838838
double Compare(IMagickImage image, ErrorMetric metric, Channels channels);
839839

840-
/// <summary>
841-
/// Returns the distortion based on the specified metric.
842-
/// </summary>
843-
/// <param name="image">The other image to compare with this image.</param>
844-
/// <param name="metric">The metric to use.</param>
845-
/// <param name="difference">The image that will contain the difference.</param>
846-
/// <returns>The distortion based on the specified metric.</returns>
847-
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
848-
double Compare(IMagickImage image, ErrorMetric metric, IMagickImage difference);
849-
850-
/// <summary>
851-
/// Returns the distortion based on the specified metric.
852-
/// </summary>
853-
/// <param name="image">The other image to compare with this image.</param>
854-
/// <param name="metric">The metric to use.</param>
855-
/// <param name="difference">The image that will contain the difference.</param>
856-
/// <param name="channels">The channel(s) to compare.</param>
857-
/// <returns>The distortion based on the specified metric.</returns>
858-
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
859-
double Compare(IMagickImage image, ErrorMetric metric, IMagickImage difference, Channels channels);
860-
861840
/// <summary>
862841
/// Compose an image onto another at specified offset using the 'In' operator.
863842
/// </summary>

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

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,26 +102,47 @@ public partial interface IMagickImage<TQuantumType> : IMagickImage, IComparable<
102102
/// <param name="stopColor">The stop color of the color range.</param>
103103
void ColorThreshold(IMagickColor<TQuantumType> startColor, IMagickColor<TQuantumType> stopColor);
104104

105+
/// <summary>
106+
/// Returns the distortion based on the specified metric.
107+
/// </summary>
108+
/// <param name="image">The other image to compare with this image.</param>
109+
/// <param name="metric">The metric to use.</param>
110+
/// <param name="distortion">The distortion based on the specified metric.</param>
111+
/// <returns>The image that contains the difference.</returns>
112+
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
113+
IMagickImage<TQuantumType> Compare(IMagickImage image, ErrorMetric metric, out double distortion);
114+
115+
/// <summary>
116+
/// Returns the distortion based on the specified metric.
117+
/// </summary>
118+
/// <param name="image">The other image to compare with this image.</param>
119+
/// <param name="metric">The metric to use.</param>
120+
/// <param name="channels">The channel(s) to compare.</param>
121+
/// <param name="distortion">The distortion based on the specified metric.</param>
122+
/// <returns>The image that contains the difference.</returns>
123+
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
124+
IMagickImage<TQuantumType> Compare(IMagickImage image, ErrorMetric metric, Channels channels, out double distortion);
125+
105126
/// <summary>
106127
/// Returns the distortion based on the specified metric.
107128
/// </summary>
108129
/// <param name="image">The other image to compare with this image.</param>
109130
/// <param name="settings">The settings to use.</param>
110-
/// <param name="difference">The image that will contain the difference.</param>
111-
/// <returns>The distortion based on the specified metric.</returns>
131+
/// <param name="distortion">The distortion based on the specified metric.</param>
132+
/// <returns>The image that contains the difference.</returns>
112133
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
113-
double Compare(IMagickImage image, ICompareSettings<TQuantumType> settings, IMagickImage difference);
134+
IMagickImage<TQuantumType> Compare(IMagickImage image, ICompareSettings<TQuantumType> settings, out double distortion);
114135

115136
/// <summary>
116137
/// Returns the distortion based on the specified metric.
117138
/// </summary>
118139
/// <param name="image">The other image to compare with this image.</param>
119140
/// <param name="settings">The settings to use.</param>
120-
/// <param name="difference">The image that will contain the difference.</param>
121141
/// <param name="channels">The channel(s) to compare.</param>
122-
/// <returns>The distortion based on the specified metric.</returns>
142+
/// <param name="distortion">The distortion based on the specified metric.</param>
143+
/// <returns>The image that contains the difference.</returns>
123144
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
124-
double Compare(IMagickImage image, ICompareSettings<TQuantumType> settings, IMagickImage difference, Channels channels);
145+
IMagickImage<TQuantumType> Compare(IMagickImage image, ICompareSettings<TQuantumType> settings, Channels channels, out double distortion);
125146

126147
/// <summary>
127148
/// Determines the connected-components of the image.

src/Magick.NET/MagickImage.cs

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1729,64 +1729,57 @@ public double Compare(IMagickImage image, ErrorMetric metric, Channels channels)
17291729
/// Returns the distortion based on the specified metric.
17301730
/// </summary>
17311731
/// <param name="image">The other image to compare with this image.</param>
1732-
/// <param name="settings">The settings to use.</param>
1733-
/// <param name="difference">The image that will contain the difference.</param>
1734-
/// <returns>The distortion based on the specified metric.</returns>
1732+
/// <param name="metric">The metric to use.</param>
1733+
/// <param name="distortion">The distortion based on the specified metric.</param>
1734+
/// <returns>The image that contains the difference.</returns>
17351735
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
1736-
public double Compare(IMagickImage image, ICompareSettings<QuantumType> settings, IMagickImage difference)
1737-
=> Compare(image, settings, difference, ImageMagick.Channels.Undefined);
1736+
public IMagickImage<QuantumType> Compare(IMagickImage image, ErrorMetric metric, out double distortion)
1737+
=> Compare(image, metric, ImageMagick.Channels.Undefined, out distortion);
17381738

17391739
/// <summary>
17401740
/// Returns the distortion based on the specified metric.
17411741
/// </summary>
17421742
/// <param name="image">The other image to compare with this image.</param>
17431743
/// <param name="metric">The metric to use.</param>
1744-
/// <param name="difference">The image that will contain the difference.</param>
1745-
/// <returns>The distortion based on the specified metric.</returns>
1744+
/// <param name="channels">The channel(s) to compare.</param>
1745+
/// <param name="distortion">The distortion based on the specified metric.</param>
1746+
/// <returns>The image that contains the difference.</returns>
17461747
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
1747-
public double Compare(IMagickImage image, ErrorMetric metric, IMagickImage difference)
1748-
=> Compare(image, metric, difference, ImageMagick.Channels.Undefined);
1748+
public IMagickImage<QuantumType> Compare(IMagickImage image, ErrorMetric metric, Channels channels, out double distortion)
1749+
=> Compare(image, new CompareSettings { Metric = metric }, channels, out distortion);
17491750

17501751
/// <summary>
17511752
/// Returns the distortion based on the specified metric.
17521753
/// </summary>
17531754
/// <param name="image">The other image to compare with this image.</param>
1754-
/// <param name="metric">The metric to use.</param>
1755-
/// <param name="difference">The image that will contain the difference.</param>
1756-
/// <param name="channels">The channel(s) to compare.</param>
1757-
/// <returns>The distortion based on the specified metric.</returns>
1755+
/// <param name="settings">The settings to use.</param>
1756+
/// <param name="distortion">The distortion based on the specified metric.</param>
1757+
/// <returns>The image that contains the difference.</returns>
17581758
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
1759-
public double Compare(IMagickImage image, ErrorMetric metric, IMagickImage difference, Channels channels)
1760-
=> Compare(image, new CompareSettings { Metric = metric }, difference, channels);
1759+
public IMagickImage<QuantumType> Compare(IMagickImage image, ICompareSettings<QuantumType> settings, out double distortion)
1760+
=> Compare(image, settings, ImageMagick.Channels.Undefined, out distortion);
17611761

17621762
/// <summary>
17631763
/// Returns the distortion based on the specified metric.
17641764
/// </summary>
17651765
/// <param name="image">The other image to compare with this image.</param>
17661766
/// <param name="settings">The settings to use.</param>
1767-
/// <param name="difference">The image that will contain the difference.</param>
17681767
/// <param name="channels">The channel(s) to compare.</param>
1769-
/// <returns>The distortion based on the specified metric.</returns>
1768+
/// <param name="distortion">The distortion based on the specified metric.</param>
1769+
/// <returns>The image that contains the difference.</returns>
17701770
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
1771-
public double Compare(IMagickImage image, ICompareSettings<QuantumType> settings, IMagickImage difference, Channels channels)
1771+
public IMagickImage<QuantumType> Compare(IMagickImage image, ICompareSettings<QuantumType> settings, Channels channels, out double distortion)
17721772
{
17731773
Throw.IfNull(nameof(image), image);
17741774
Throw.IfNull(nameof(settings), settings);
1775-
Throw.IfNull(nameof(difference), difference);
1776-
1777-
if (difference is not MagickImage differenceImage)
1778-
throw new NotSupportedException();
17791775

17801776
using var temporaryDefines = new TemporaryDefines(this);
17811777
temporaryDefines.SetArtifact("compare:highlight-color", settings.HighlightColor);
17821778
temporaryDefines.SetArtifact("compare:lowlight-color", settings.LowlightColor);
17831779
temporaryDefines.SetArtifact("compare:masklight-color", settings.MasklightColor);
17841780

1785-
var result = _nativeInstance.Compare(image, settings.Metric, channels, out var distortion);
1786-
if (result != IntPtr.Zero)
1787-
differenceImage._nativeInstance.Instance = result;
1788-
1789-
return distortion;
1781+
var result = _nativeInstance.Compare(image, settings.Metric, channels, out distortion);
1782+
return Create(result, _settings);
17901783
}
17911784

17921785
/// <summary>

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

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -43,35 +43,16 @@ public void ShouldThrowAnExceptionWhenImageIsNullAndErrorMetricIsSpecified()
4343
public void ShouldThrowAnExceptionWhenImageIsNullAndSettingsAreNotNull()
4444
{
4545
using var image = new MagickImage();
46-
using var diff = new MagickImage();
4746

48-
Assert.Throws<ArgumentNullException>("image", () => image.Compare(null, new CompareSettings(), diff));
47+
Assert.Throws<ArgumentNullException>("image", () => image.Compare(null, new CompareSettings(), out var distortion));
4948
}
5049

5150
[Fact]
5251
public void ShouldThrowAnExceptionWhenSettingsIsNull()
5352
{
5453
using var image = new MagickImage();
55-
using var diff = new MagickImage();
56-
57-
Assert.Throws<ArgumentNullException>("settings", () => image.Compare(image, null, diff));
58-
}
59-
60-
[Fact]
61-
public void ShouldThrowAnExceptionWhenDifferenceIsNull()
62-
{
63-
using var image = new MagickImage();
64-
65-
Assert.Throws<ArgumentNullException>("difference", () => image.Compare(image, new CompareSettings(), null));
66-
}
67-
68-
[Fact]
69-
public void ShouldThrowAnExceptionWhenDifferenceIsNotMagickImage()
70-
{
71-
using var image = new MagickImage();
72-
var diff = Substitute.For<IMagickImage<QuantumType>>();
7354

74-
Assert.Throws<NotSupportedException>(() => image.Compare(image, new CompareSettings(), diff));
55+
Assert.Throws<ArgumentNullException>("settings", () => image.Compare(image, null, out var distortion));
7556
}
7657

7758
[Fact]
@@ -97,8 +78,7 @@ public void ShouldReturnZeroWhenTheImagesAreEqual()
9778

9879
using var image = new MagickImage(Files.Builtin.Logo);
9980
using var other = new MagickImage(Files.Builtin.Logo);
100-
using var diff = new MagickImage();
101-
var result = image.Compare(other, settings, diff);
81+
using var diff = image.Compare(other, settings, out var result);
10282

10383
Assert.Equal(0, result);
10484
}
@@ -108,8 +88,7 @@ public void ShouldReturnZeroWhenTheImagesAreEqualAndErrorMetricIsRootMeanSquared
10888
{
10989
using var image = new MagickImage(Files.Builtin.Logo);
11090
using var other = new MagickImage(Files.Builtin.Logo);
111-
using var diff = new MagickImage();
112-
var result = image.Compare(other, ErrorMetric.RootMeanSquared, diff);
91+
using var diff = image.Compare(other, ErrorMetric.RootMeanSquared, out var result);
11392

11493
Assert.Equal(0, result);
11594
}
@@ -150,8 +129,7 @@ public void ShouldReturnNonZeroValueWhenTheImagesAreNotEqual()
150129
using var other = new MagickImage(Files.Builtin.Logo);
151130
other.Rotate(180);
152131

153-
using var diff = new MagickImage();
154-
var result = image.Compare(other, settings, diff);
132+
using var diff = image.Compare(other, settings, out var result);
155133

156134
Assert.InRange(result, 0.36, 0.37);
157135
ColorAssert.Equal(MagickColors.Yellow, diff, 150, 50);
@@ -164,9 +142,8 @@ public void ShouldUseTheColorFuzz()
164142
{
165143
using var image = new MagickImage(new MagickColor("#f1d3bc"), 1, 1);
166144
using var other = new MagickImage(new MagickColor("#24292e"), 1, 1);
167-
using var diff = new MagickImage();
168145
image.ColorFuzz = new Percentage(81);
169-
var result = image.Compare(other, ErrorMetric.Absolute, diff);
146+
using var diff = image.Compare(other, ErrorMetric.Absolute, out var result);
170147

171148
Assert.Equal(0, result);
172149
ColorAssert.Equal(new MagickColor("#fd2ff729f28b"), diff, 0, 0);

0 commit comments

Comments
 (0)