Skip to content
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

Async version of DiscoverDecoder #1260

Merged
merged 3 commits into from
Jul 7, 2020
Merged
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
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