Skip to content

Commit

Permalink
Made the ErrorMetric mandatory and immutable in the CompareSettings.
Browse files Browse the repository at this point in the history
  • Loading branch information
dlemstra committed Aug 2, 2024
1 parent 99e10c4 commit 7a5974f
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 16 deletions.
3 changes: 2 additions & 1 deletion src/Magick.NET.Core/Factories/ISettingsFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ public interface ISettingsFactory<TQuantumType>
/// <summary>
/// Initializes a new instance that implements <see cref="ICompareSettings{TQuantumType}"/>.
/// </summary>
/// <param name="metric">The error metric to use.</param>
/// <returns>A new <see cref="ICompareSettings{TQuantumType}"/> instance.</returns>
ICompareSettings<TQuantumType> CreateCompareSettings();
ICompareSettings<TQuantumType> CreateCompareSettings(ErrorMetric metric);

/// <summary>
/// Initializes a new instance that implements <see cref="IComplexSettings"/>.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ public interface ICompareSettings<TQuantumType>
where TQuantumType : struct, IConvertible
{
/// <summary>
/// Gets or sets the error metric to use.
/// Gets the error metric to use.
/// </summary>
ErrorMetric Metric { get; set; }
ErrorMetric Metric { get; }

/// <summary>
/// Gets or sets the color that emphasize pixel differences.
Expand Down
5 changes: 3 additions & 2 deletions src/Magick.NET/Factories/SettingsFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ public sealed class SettingsFactory : ISettingsFactory<QuantumType>
/// <summary>
/// Initializes a new instance that implements <see cref="ICompareSettings{TQuantumType}"/>.
/// </summary>
/// <param name="metric">The error metric to use.</param>
/// <returns>A new <see cref="ICompareSettings{TQuantumType}"/> instance.</returns>
public ICompareSettings<QuantumType> CreateCompareSettings()
=> new CompareSettings();
public ICompareSettings<QuantumType> CreateCompareSettings(ErrorMetric metric)
=> new CompareSettings(metric);

/// <summary>
/// Initializes a new instance that implements <see cref="IComplexSettings"/>.
Expand Down
2 changes: 1 addition & 1 deletion src/Magick.NET/MagickImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1746,7 +1746,7 @@ public IMagickImage<QuantumType> Compare(IMagickImage image, ErrorMetric metric,
/// <returns>The image that contains the difference.</returns>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
public IMagickImage<QuantumType> Compare(IMagickImage image, ErrorMetric metric, Channels channels, out double distortion)
=> Compare(image, new CompareSettings { Metric = metric }, channels, out distortion);
=> Compare(image, new CompareSettings(metric), channels, out distortion);

/// <summary>
/// Returns the distortion based on the specified metric.
Expand Down
11 changes: 9 additions & 2 deletions src/Magick.NET/Settings/CompareSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,16 @@ namespace ImageMagick;
public sealed class CompareSettings : ICompareSettings<QuantumType>
{
/// <summary>
/// Gets or sets the error metric to use.
/// Initializes a new instance of the <see cref="CompareSettings"/> class.
/// </summary>
public ErrorMetric Metric { get; set; }
/// <param name="metric">The error metric to use.</param>
public CompareSettings(ErrorMetric metric)
=> Metric = metric;

/// <summary>
/// Gets the error metric to use.
/// </summary>
public ErrorMetric Metric { get; }

/// <summary>
/// Gets or sets the color that emphasize pixel differences.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ public void ShouldCreateInstance()
{
var factory = new SettingsFactory();

var settings = factory.CreateCompareSettings();
var settings = factory.CreateCompareSettings(ErrorMetric.NormalizedCrossCorrelation);

Assert.NotNull(settings);
Assert.IsType<CompareSettings>(settings);
Assert.Equal(ErrorMetric.NormalizedCrossCorrelation, settings.Metric);
}
}
}
10 changes: 3 additions & 7 deletions tests/Magick.NET.Tests/MagickImageTests/TheCompareMethod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void ShouldThrowAnExceptionWhenImageIsNullAndSettingsAreNotNull()
{
using var image = new MagickImage();

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

[Fact]
Expand All @@ -71,10 +71,7 @@ public void ShouldReturnEmptyErrorInfoWhenTheImagesAreEqual()
[Fact]
public void ShouldReturnZeroWhenTheImagesAreEqual()
{
var settings = new CompareSettings
{
Metric = ErrorMetric.RootMeanSquared,
};
var settings = new CompareSettings(ErrorMetric.RootMeanSquared);

using var image = new MagickImage(Files.Builtin.Logo);
using var other = new MagickImage(Files.Builtin.Logo);
Expand Down Expand Up @@ -114,9 +111,8 @@ public void ShouldReturnErrorInfoWhenTheImagesAreNotEqual()
[Fact]
public void ShouldReturnNonZeroValueWhenTheImagesAreNotEqual()
{
var settings = new CompareSettings
var settings = new CompareSettings(ErrorMetric.RootMeanSquared)
{
Metric = ErrorMetric.RootMeanSquared,
HighlightColor = MagickColors.Yellow,
LowlightColor = MagickColors.Red,
MasklightColor = MagickColors.Magenta,
Expand Down

0 comments on commit 7a5974f

Please sign in to comment.