Skip to content

Commit aba6e19

Browse files
rubanbsboris
and
boris
authored
Added asynchronous version of the Hash method (#122)
Co-authored-by: boris <[email protected]>
1 parent 3a6c3a3 commit aba6e19

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

src/ImageHash/ImageHashExtensions.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ namespace CoenM.ImageHash
22
{
33
using System;
44
using System.IO;
5+
using System.Threading;
6+
using System.Threading.Tasks;
57
using SixLabors.ImageSharp;
68
using SixLabors.ImageSharp.PixelFormats;
79

@@ -31,5 +33,28 @@ public static ulong Hash(this IImageHash hashImplementation, Stream stream)
3133
using var image = Image.Load<Rgba32>(stream);
3234
return hashImplementation.Hash(image);
3335
}
36+
37+
/// <summary>Asynchronously calculate the hash of the image (stream) using the hashImplementation.</summary>
38+
/// <param name="hashImplementation">HashImplementation to calculate the hash.</param>
39+
/// <param name="stream">Stream should 'contain' raw image data.</param>
40+
/// <param name="cancellationToken">Cancellation token.</param>
41+
/// <returns>hash value.</returns>
42+
/// <exception cref="ArgumentNullException">Thrown when <paramref name="hashImplementation"/> or <paramref name="stream"/> is <c>null</c>.</exception>
43+
/// <exception cref="SixLabors.ImageSharp.UnknownImageFormatException">Thrown when stream content cannot be loaded as an image.</exception>
44+
public static async Task<ulong> HashAsync(this IImageHash hashImplementation, Stream stream, CancellationToken cancellationToken)
45+
{
46+
if (hashImplementation == null)
47+
{
48+
throw new ArgumentNullException(nameof(hashImplementation));
49+
}
50+
51+
if (stream == null)
52+
{
53+
throw new ArgumentNullException(nameof(stream));
54+
}
55+
56+
using var image = await Image.LoadAsync<Rgba32>(stream, cancellationToken);
57+
return hashImplementation.Hash(image);
58+
}
3459
}
3560
}

0 commit comments

Comments
 (0)