Skip to content

Commit 417f8b4

Browse files
authored
Correct return type of channel methods in Moments and PerceptualHash (#1621)
1 parent 6d8d6f5 commit 417f8b4

7 files changed

Lines changed: 92 additions & 9 deletions

File tree

src/Magick.NET.Core/Statistics/IMoments.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ public interface IMoments
1212
/// Gets the moments for the all the channels.
1313
/// </summary>
1414
/// <returns>The moments for the all the channels.</returns>
15-
IChannelMoments Composite();
15+
IChannelMoments? Composite();
1616

1717
/// <summary>
1818
/// Gets the moments for the specified channel.
1919
/// </summary>
2020
/// <param name="channel">The channel to get the moments for.</param>
2121
/// <returns>The moments for the specified channel.</returns>
22-
IChannelMoments GetChannel(PixelChannel channel);
22+
IChannelMoments? GetChannel(PixelChannel channel);
2323
}

src/Magick.NET.Core/Statistics/IPerceptualHash.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public interface IPerceptualHash
1313
/// </summary>
1414
/// <param name="channel">The channel to get the has for.</param>
1515
/// <returns>The perceptual hash for the specified channel.</returns>
16-
IChannelPerceptualHash GetChannel(PixelChannel channel);
16+
IChannelPerceptualHash? GetChannel(PixelChannel channel);
1717

1818
/// <summary>
1919
/// Returns the sum squared difference between this hash and the other hash.

src/Magick.NET/Statistics/Moments.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@ internal Moments(MagickImage image, IntPtr list)
2929
/// </summary>
3030
/// <returns>The moments for the all the channels.</returns>
3131
public IChannelMoments Composite()
32-
=> GetChannel(PixelChannel.Composite);
32+
=> _channels[PixelChannel.Composite];
3333

3434
/// <summary>
3535
/// Gets the moments for the specified channel.
3636
/// </summary>
3737
/// <param name="channel">The channel to get the moments for.</param>
3838
/// <returns>The moments for the specified channel.</returns>
39-
public IChannelMoments GetChannel(PixelChannel channel)
39+
public IChannelMoments? GetChannel(PixelChannel channel)
4040
{
4141
_channels.TryGetValue(channel, out var moments);
4242
return moments;

src/Magick.NET/Statistics/PerceptualHash.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ internal bool Isvalid
6363
/// </summary>
6464
/// <param name="channel">The channel to get the has for.</param>
6565
/// <returns>The perceptual hash for the specified channel.</returns>
66-
public IChannelPerceptualHash GetChannel(PixelChannel channel)
66+
public IChannelPerceptualHash? GetChannel(PixelChannel channel)
6767
{
6868
_channels.TryGetValue(channel, out var perceptualHash);
6969
return perceptualHash;
@@ -78,10 +78,19 @@ public double SumSquaredDistance(IPerceptualHash other)
7878
{
7979
Throw.IfNull(nameof(other), other);
8080

81+
var red = other.GetChannel(PixelChannel.Red);
82+
var green = other.GetChannel(PixelChannel.Green);
83+
var blue = other.GetChannel(PixelChannel.Blue);
84+
85+
if (red is null || green is null || blue is null)
86+
{
87+
throw new NotSupportedException("other IPerceptualHash must have Red, Green and Blue channel");
88+
}
89+
8190
return
82-
_channels[PixelChannel.Red].SumSquaredDistance(other.GetChannel(PixelChannel.Red)) +
83-
_channels[PixelChannel.Green].SumSquaredDistance(other.GetChannel(PixelChannel.Green)) +
84-
_channels[PixelChannel.Blue].SumSquaredDistance(other.GetChannel(PixelChannel.Blue));
91+
_channels[PixelChannel.Red].SumSquaredDistance(red) +
92+
_channels[PixelChannel.Green].SumSquaredDistance(green) +
93+
_channels[PixelChannel.Blue].SumSquaredDistance(blue);
8594
}
8695

8796
/// <summary>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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 MomentsTests
10+
{
11+
public class TheGetChannelMethod
12+
{
13+
[Fact]
14+
public void ShouldReturnNullWhenChannelDoesNotExist()
15+
{
16+
using var image = new MagickImage(Files.ImageMagickJPG);
17+
var moments = image.Moments();
18+
19+
Assert.NotNull(moments.GetChannel(PixelChannel.Red));
20+
Assert.NotNull(moments.GetChannel(PixelChannel.Green));
21+
Assert.NotNull(moments.GetChannel(PixelChannel.Blue));
22+
23+
Assert.Null(moments.GetChannel(PixelChannel.Black));
24+
}
25+
}
26+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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 PerceptualHashTests
10+
{
11+
public class TheGetChannelMethod
12+
{
13+
[Fact]
14+
public void ShouldReturnNullWhenChannelDoesNotExist()
15+
{
16+
var hash = new PerceptualHash("81b4488652898d48a7a9622346206e620f8a646682939835e986ec98c78f887ae8c67f81b1e884c58a0d18af2d622718fd35623ffdeac9a78cbaedaa81d888434e824c683ad781c37895978c8688c426628ed61b216279b81b48887318a1628af43622a2619d162372");
17+
18+
Assert.NotNull(hash.GetChannel(PixelChannel.Red));
19+
Assert.NotNull(hash.GetChannel(PixelChannel.Green));
20+
Assert.NotNull(hash.GetChannel(PixelChannel.Blue));
21+
22+
Assert.Null(hash.GetChannel(PixelChannel.Black));
23+
}
24+
}
25+
}

tests/Magick.NET.Tests/Statistics/PerceptualHashTests/TheSumSquaredDistanceMethod.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,38 @@
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

78
namespace Magick.NET.Tests;
89

910
public partial class PerceptualHashTests
1011
{
12+
public class TestPerceptualHash : IPerceptualHash
13+
{
14+
public IChannelPerceptualHash GetChannel(PixelChannel channel)
15+
=> null;
16+
17+
public double SumSquaredDistance(IPerceptualHash other)
18+
{
19+
throw new System.NotImplementedException();
20+
}
21+
}
22+
1123
public class TheSumSquaredDistanceMethod
1224
{
25+
[Fact]
26+
public void ShouldThrowNotSupportedExceptionIfCustomImplementationDoesNotHaveExpectedChannels()
27+
{
28+
using var image = new MagickImage(Files.ImageMagickJPG);
29+
var phash = image.PerceptualHash();
30+
Assert.NotNull(phash);
31+
32+
var exception = Assert.Throws<NotSupportedException>(() => phash.SumSquaredDistance(new TestPerceptualHash()));
33+
Assert.Equal("other IPerceptualHash must have Red, Green and Blue channel", exception.Message);
34+
}
35+
1336
[Fact]
1437
public void ShouldReturnTheDifference()
1538
{

0 commit comments

Comments
 (0)