Skip to content

Commit 7a5974f

Browse files
committed
Made the ErrorMetric mandatory and immutable in the CompareSettings.
1 parent 99e10c4 commit 7a5974f

File tree

7 files changed

+22
-16
lines changed

7 files changed

+22
-16
lines changed

src/Magick.NET.Core/Factories/ISettingsFactory.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ public interface ISettingsFactory<TQuantumType>
1515
/// <summary>
1616
/// Initializes a new instance that implements <see cref="ICompareSettings{TQuantumType}"/>.
1717
/// </summary>
18+
/// <param name="metric">The error metric to use.</param>
1819
/// <returns>A new <see cref="ICompareSettings{TQuantumType}"/> instance.</returns>
19-
ICompareSettings<TQuantumType> CreateCompareSettings();
20+
ICompareSettings<TQuantumType> CreateCompareSettings(ErrorMetric metric);
2021

2122
/// <summary>
2223
/// Initializes a new instance that implements <see cref="IComplexSettings"/>.

src/Magick.NET.Core/Settings/ICompareSettings{TQuantumType}.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ public interface ICompareSettings<TQuantumType>
1313
where TQuantumType : struct, IConvertible
1414
{
1515
/// <summary>
16-
/// Gets or sets the error metric to use.
16+
/// Gets the error metric to use.
1717
/// </summary>
18-
ErrorMetric Metric { get; set; }
18+
ErrorMetric Metric { get; }
1919

2020
/// <summary>
2121
/// Gets or sets the color that emphasize pixel differences.

src/Magick.NET/Factories/SettingsFactory.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@ public sealed class SettingsFactory : ISettingsFactory<QuantumType>
2121
/// <summary>
2222
/// Initializes a new instance that implements <see cref="ICompareSettings{TQuantumType}"/>.
2323
/// </summary>
24+
/// <param name="metric">The error metric to use.</param>
2425
/// <returns>A new <see cref="ICompareSettings{TQuantumType}"/> instance.</returns>
25-
public ICompareSettings<QuantumType> CreateCompareSettings()
26-
=> new CompareSettings();
26+
public ICompareSettings<QuantumType> CreateCompareSettings(ErrorMetric metric)
27+
=> new CompareSettings(metric);
2728

2829
/// <summary>
2930
/// Initializes a new instance that implements <see cref="IComplexSettings"/>.

src/Magick.NET/MagickImage.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1746,7 +1746,7 @@ public IMagickImage<QuantumType> Compare(IMagickImage image, ErrorMetric metric,
17461746
/// <returns>The image that contains the difference.</returns>
17471747
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
17481748
public IMagickImage<QuantumType> Compare(IMagickImage image, ErrorMetric metric, Channels channels, out double distortion)
1749-
=> Compare(image, new CompareSettings { Metric = metric }, channels, out distortion);
1749+
=> Compare(image, new CompareSettings(metric), channels, out distortion);
17501750

17511751
/// <summary>
17521752
/// Returns the distortion based on the specified metric.

src/Magick.NET/Settings/CompareSettings.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,16 @@ namespace ImageMagick;
1919
public sealed class CompareSettings : ICompareSettings<QuantumType>
2020
{
2121
/// <summary>
22-
/// Gets or sets the error metric to use.
22+
/// Initializes a new instance of the <see cref="CompareSettings"/> class.
2323
/// </summary>
24-
public ErrorMetric Metric { get; set; }
24+
/// <param name="metric">The error metric to use.</param>
25+
public CompareSettings(ErrorMetric metric)
26+
=> Metric = metric;
27+
28+
/// <summary>
29+
/// Gets the error metric to use.
30+
/// </summary>
31+
public ErrorMetric Metric { get; }
2532

2633
/// <summary>
2734
/// Gets or sets the color that emphasize pixel differences.

tests/Magick.NET.Tests/Factories/SettingsFactoryTests/TheCreateCompareSettingsMethod.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@ public void ShouldCreateInstance()
1616
{
1717
var factory = new SettingsFactory();
1818

19-
var settings = factory.CreateCompareSettings();
19+
var settings = factory.CreateCompareSettings(ErrorMetric.NormalizedCrossCorrelation);
2020

2121
Assert.NotNull(settings);
2222
Assert.IsType<CompareSettings>(settings);
23+
Assert.Equal(ErrorMetric.NormalizedCrossCorrelation, settings.Metric);
2324
}
2425
}
2526
}

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public void ShouldThrowAnExceptionWhenImageIsNullAndSettingsAreNotNull()
4444
{
4545
using var image = new MagickImage();
4646

47-
Assert.Throws<ArgumentNullException>("image", () => image.Compare(null, new CompareSettings(), out var distortion));
47+
Assert.Throws<ArgumentNullException>("image", () => image.Compare(null, new CompareSettings(ErrorMetric.PeakSignalToNoiseRatio), out var distortion));
4848
}
4949

5050
[Fact]
@@ -71,10 +71,7 @@ public void ShouldReturnEmptyErrorInfoWhenTheImagesAreEqual()
7171
[Fact]
7272
public void ShouldReturnZeroWhenTheImagesAreEqual()
7373
{
74-
var settings = new CompareSettings
75-
{
76-
Metric = ErrorMetric.RootMeanSquared,
77-
};
74+
var settings = new CompareSettings(ErrorMetric.RootMeanSquared);
7875

7976
using var image = new MagickImage(Files.Builtin.Logo);
8077
using var other = new MagickImage(Files.Builtin.Logo);
@@ -114,9 +111,8 @@ public void ShouldReturnErrorInfoWhenTheImagesAreNotEqual()
114111
[Fact]
115112
public void ShouldReturnNonZeroValueWhenTheImagesAreNotEqual()
116113
{
117-
var settings = new CompareSettings
114+
var settings = new CompareSettings(ErrorMetric.RootMeanSquared)
118115
{
119-
Metric = ErrorMetric.RootMeanSquared,
120116
HighlightColor = MagickColors.Yellow,
121117
LowlightColor = MagickColors.Red,
122118
MasklightColor = MagickColors.Magenta,

0 commit comments

Comments
 (0)