Skip to content

Commit

Permalink
Merge pull request #1260 from pekspro/DiscoverDecoderAsync
Browse files Browse the repository at this point in the history
Async version of DiscoverDecoder
  • Loading branch information
JimBobSquarePants authored Jul 7, 2020
2 parents 4a48355 + 343b609 commit c0594f7
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions src/ImageSharp/Image.Decode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,23 @@ private static IImageDecoder DiscoverDecoder(Stream stream, Configuration config
: null;
}

/// <summary>
/// By reading the header on the provided stream this calculates the images format.
/// </summary>
/// <param name="stream">The image stream to read the header from.</param>
/// <param name="config">The configuration.</param>
/// <returns>The decoder and the image format or null if none found.</returns>
private static async Task<(IImageDecoder decoder, IImageFormat format)> DiscoverDecoderAsync(Stream stream, Configuration config)
{
IImageFormat format = await InternalDetectFormatAsync(stream, config).ConfigureAwait(false);

IImageDecoder decoder = format != null
? config.ImageFormatsManager.FindDecoder(format)
: null;

return (decoder, format);
}

/// <summary>
/// Decodes the image stream to the current image.
/// </summary>
Expand Down Expand Up @@ -133,7 +150,7 @@ private static (Image<TPixel> Image, IImageFormat Format) Decode<TPixel>(Stream
private static async Task<(Image<TPixel> Image, IImageFormat Format)> DecodeAsync<TPixel>(Stream stream, Configuration config)
where TPixel : unmanaged, IPixel<TPixel>
{
IImageDecoder decoder = DiscoverDecoder(stream, config, out IImageFormat format);
(IImageDecoder decoder, IImageFormat format) = await DiscoverDecoderAsync(stream, config).ConfigureAwait(false);
if (decoder is null)
{
return (null, null);
Expand All @@ -157,7 +174,7 @@ private static (Image Image, IImageFormat Format) Decode(Stream stream, Configur

private static async Task<(Image Image, IImageFormat Format)> DecodeAsync(Stream stream, Configuration config)
{
IImageDecoder decoder = DiscoverDecoder(stream, config, out IImageFormat format);
(IImageDecoder decoder, IImageFormat format) = await DiscoverDecoderAsync(stream, config).ConfigureAwait(false);
if (decoder is null)
{
return (null, null);
Expand All @@ -177,7 +194,9 @@ private static (Image Image, IImageFormat Format) Decode(Stream stream, Configur
/// </returns>
private static (IImageInfo ImageInfo, IImageFormat Format) InternalIdentity(Stream stream, Configuration config)
{
if (!(DiscoverDecoder(stream, config, out IImageFormat format) is IImageInfoDetector detector))
IImageDecoder decoder = DiscoverDecoder(stream, config, out IImageFormat format);

if (!(decoder is IImageInfoDetector detector))
{
return (null, null);
}
Expand All @@ -197,7 +216,9 @@ private static (IImageInfo ImageInfo, IImageFormat Format) InternalIdentity(Stre
/// is not found.</returns>
private static async Task<(IImageInfo ImageInfo, IImageFormat Format)> InternalIdentityAsync(Stream stream, Configuration config)
{
if (!(DiscoverDecoder(stream, config, out IImageFormat format) is IImageInfoDetector detector))
(IImageDecoder decoder, IImageFormat format) = await DiscoverDecoderAsync(stream, config).ConfigureAwait(false);

if (!(decoder is IImageInfoDetector detector))
{
return (null, null);
}
Expand Down

0 comments on commit c0594f7

Please sign in to comment.