Skip to content

Commit 16b62fe

Browse files
committed
Use a constant for the maximum number of perceptual hashes.
1 parent 5473186 commit 16b62fe

File tree

1 file changed

+7
-8
lines changed

1 file changed

+7
-8
lines changed

src/Magick.NET/Statistics/ChannelPerceptualHash.cs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ namespace ImageMagick;
1212
/// </summary>
1313
public partial class ChannelPerceptualHash : IChannelPerceptualHash
1414
{
15+
private const int _maximumNumberOfPerceptualHashes = 7;
1516
private readonly Dictionary<ColorSpace, HuPhashList> _huPhashes = new();
1617
private string _hash = string.Empty;
1718

@@ -44,7 +45,7 @@ internal ChannelPerceptualHash(PixelChannel channel, ColorSpace[] colorSpaces, I
4445
/// <returns>The hu perceptual hash for the specified colorspace.</returns>
4546
public double HuPhash(ColorSpace colorSpace, int index)
4647
{
47-
Throw.IfOutOfRange(index, 7);
48+
Throw.IfOutOfRange(index, _maximumNumberOfPerceptualHashes);
4849

4950
if (!_huPhashes.TryGetValue(colorSpace, out var huPhashList))
5051
{
@@ -67,7 +68,7 @@ public double SumSquaredDistance(IChannelPerceptualHash other)
6768

6869
foreach (var huPhashList in _huPhashes)
6970
{
70-
for (var i = 0; i < 7; i++)
71+
for (var i = 0; i < _maximumNumberOfPerceptualHashes; i++)
7172
{
7273
var a = huPhashList.Value[i];
7374
var b = other.HuPhash(huPhashList.Key, i);
@@ -110,7 +111,7 @@ private void ParseHash(ColorSpace[] colorSpaces, string hash)
110111
foreach (var colorSpace in colorSpaces)
111112
{
112113
var huPhashList = new HuPhashList();
113-
for (var i = 0; i < 7; i++, offset += 5)
114+
for (var i = 0; i < _maximumNumberOfPerceptualHashes; i++, offset += 5)
114115
{
115116
if (!int.TryParse(hash.Substring(offset, 5), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out var hex))
116117
throw new ArgumentException("Invalid hash specified", nameof(hash));
@@ -132,7 +133,7 @@ private void SetHash()
132133

133134
foreach (var huPhashList in _huPhashes.Values)
134135
{
135-
for (var i = 0; i < 7; i++)
136+
for (var i = 0; i < _maximumNumberOfPerceptualHashes; i++)
136137
{
137138
var value = huPhashList[i];
138139

@@ -156,7 +157,7 @@ private void AddHuPhash(NativeChannelPerceptualHash instance, ColorSpace colorSp
156157
{
157158
var huPhashList = new HuPhashList();
158159

159-
for (var i = 0; i < 7; i++)
160+
for (var i = 0; i < _maximumNumberOfPerceptualHashes; i++)
160161
huPhashList[i] = instance.GetHuPhash(colorSpaceIndex, (uint)i);
161162

162163
_huPhashes[colorSpace] = huPhashList;
@@ -172,9 +173,7 @@ public HuPhashList()
172173
}
173174

174175
public HuPhashList(double[] values)
175-
{
176-
_values = values;
177-
}
176+
=> _values = values;
178177

179178
public double this[int index]
180179
{

0 commit comments

Comments
 (0)