Skip to content

perf: use index access to Dictionary #1621

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Magick.NET.Core/Statistics/IMoments.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ public interface IMoments
/// Gets the moments for the all the channels.
/// </summary>
/// <returns>The moments for the all the channels.</returns>
IChannelMoments Composite();
IChannelMoments? Composite();

/// <summary>
/// Gets the moments for the specified channel.
/// </summary>
/// <param name="channel">The channel to get the moments for.</param>
/// <returns>The moments for the specified channel.</returns>
IChannelMoments GetChannel(PixelChannel channel);
IChannelMoments? GetChannel(PixelChannel channel);
}
2 changes: 1 addition & 1 deletion src/Magick.NET.Core/Statistics/IPerceptualHash.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public interface IPerceptualHash
/// </summary>
/// <param name="channel">The channel to get the has for.</param>
/// <returns>The perceptual hash for the specified channel.</returns>
IChannelPerceptualHash GetChannel(PixelChannel channel);
IChannelPerceptualHash? GetChannel(PixelChannel channel);

/// <summary>
/// Returns the sum squared difference between this hash and the other hash.
Expand Down
4 changes: 2 additions & 2 deletions src/Magick.NET/Statistics/Moments.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ internal Moments(MagickImage image, IntPtr list)
/// </summary>
/// <returns>The moments for the all the channels.</returns>
public IChannelMoments Composite()
=> GetChannel(PixelChannel.Composite);
=> _channels[PixelChannel.Composite];

/// <summary>
/// Gets the moments for the specified channel.
/// </summary>
/// <param name="channel">The channel to get the moments for.</param>
/// <returns>The moments for the specified channel.</returns>
public IChannelMoments GetChannel(PixelChannel channel)
public IChannelMoments? GetChannel(PixelChannel channel)
{
_channels.TryGetValue(channel, out var moments);
return moments;
Expand Down
17 changes: 13 additions & 4 deletions src/Magick.NET/Statistics/PerceptualHash.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ internal bool Isvalid
/// </summary>
/// <param name="channel">The channel to get the has for.</param>
/// <returns>The perceptual hash for the specified channel.</returns>
public IChannelPerceptualHash GetChannel(PixelChannel channel)
public IChannelPerceptualHash? GetChannel(PixelChannel channel)
{
_channels.TryGetValue(channel, out var perceptualHash);
return perceptualHash;
Expand All @@ -78,10 +78,19 @@ public double SumSquaredDistance(IPerceptualHash other)
{
Throw.IfNull(nameof(other), other);

var red = other.GetChannel(PixelChannel.Red);
var green = other.GetChannel(PixelChannel.Green);
var blue = other.GetChannel(PixelChannel.Blue);

if (red is null || green is null || blue is null)
{
throw new NotSupportedException("other IPerceptualHash must have Red, Green and Blue channel");
}

return
_channels[PixelChannel.Red].SumSquaredDistance(other.GetChannel(PixelChannel.Red)) +
_channels[PixelChannel.Green].SumSquaredDistance(other.GetChannel(PixelChannel.Green)) +
_channels[PixelChannel.Blue].SumSquaredDistance(other.GetChannel(PixelChannel.Blue));
_channels[PixelChannel.Red].SumSquaredDistance(red) +
_channels[PixelChannel.Green].SumSquaredDistance(green) +
_channels[PixelChannel.Blue].SumSquaredDistance(blue);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright Dirk Lemstra https://github.com/dlemstra/Magick.NET.
// Licensed under the Apache License, Version 2.0.

using ImageMagick;
using Xunit;

namespace Magick.NET.Tests;

public partial class MomentsTests
{
public class TheGetChannelMethod
{
[Fact]
public void ShouldReturnNullWhenChannelDoesNotExist()
{
using var image = new MagickImage(Files.ImageMagickJPG);
var moments = image.Moments();

Assert.NotNull(moments.GetChannel(PixelChannel.Red));
Assert.NotNull(moments.GetChannel(PixelChannel.Green));
Assert.NotNull(moments.GetChannel(PixelChannel.Blue));

Assert.Null(moments.GetChannel(PixelChannel.Black));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright Dirk Lemstra https://github.com/dlemstra/Magick.NET.
// Licensed under the Apache License, Version 2.0.

using ImageMagick;
using Xunit;

namespace Magick.NET.Tests;

public partial class PerceptualHashTests
{
public class TheGetChannelMethod
{
[Fact]
public void ShouldReturnNullWhenChannelDoesNotExist()
{
var hash = new PerceptualHash("81b4488652898d48a7a9622346206e620f8a646682939835e986ec98c78f887ae8c67f81b1e884c58a0d18af2d622718fd35623ffdeac9a78cbaedaa81d888434e824c683ad781c37895978c8688c426628ed61b216279b81b48887318a1628af43622a2619d162372");

Assert.NotNull(hash.GetChannel(PixelChannel.Red));
Assert.NotNull(hash.GetChannel(PixelChannel.Green));
Assert.NotNull(hash.GetChannel(PixelChannel.Blue));

Assert.Null(hash.GetChannel(PixelChannel.Black));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,38 @@
// 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 PerceptualHashTests
{
public class TestPerceptualHash : IPerceptualHash
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will move this to a separate file after merging this PR. Not yet sure where I want to put this. And maybe we could use NSubstitute instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm used to anonymous object that can implements an Interface in PHP or Kotlin 😜

I don't know NSubstitute (this one?), but I could use it instead of this Test class.

{
public IChannelPerceptualHash GetChannel(PixelChannel channel)
=> null;

public double SumSquaredDistance(IPerceptualHash other)
{
throw new System.NotImplementedException();
}
}

public class TheSumSquaredDistanceMethod
{
[Fact]
public void ShouldThrowNotSupportedExceptionIfCustomImplementationDoesNotHaveExpectedChannels()
{
using var image = new MagickImage(Files.ImageMagickJPG);
var phash = image.PerceptualHash();
Assert.NotNull(phash);

var exception = Assert.Throws<NotSupportedException>(() => phash.SumSquaredDistance(new TestPerceptualHash()));
Assert.Equal("other IPerceptualHash must have Red, Green and Blue channel", exception.Message);
}

[Fact]
public void ShouldReturnTheDifference()
{
Expand Down
Loading