diff --git a/src/ImageSharp/Image.Decode.cs b/src/ImageSharp/Image.Decode.cs index 75dbd6c5c8..9bb680c82a 100644 --- a/src/ImageSharp/Image.Decode.cs +++ b/src/ImageSharp/Image.Decode.cs @@ -2,11 +2,8 @@ // Licensed under the Apache License, Version 2.0. using System; -using System.Buffers; using System.IO; -using System.Linq; using System.Threading; -using System.Threading.Tasks; using SixLabors.ImageSharp.Formats; using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.Metadata; @@ -96,20 +93,6 @@ namespace SixLabors.ImageSharp return format; } - /// - /// By reading the header on the provided stream this calculates the images format. - /// - /// The image stream to read the header from. - /// The configuration. - /// The mime type or null if none found. - private static Task InternalDetectFormatAsync(Stream stream, Configuration config) - { - // We are going to cheat here because we know that by this point we have been wrapped in a - // seekable stream then we are free to use sync APIs this is potentially brittle and may - // need a better fix in the future. - return Task.FromResult(InternalDetectFormat(stream, config)); - } - /// /// By reading the header on the provided stream this calculates the images format. /// @@ -126,23 +109,6 @@ namespace SixLabors.ImageSharp : null; } - /// - /// By reading the header on the provided stream this calculates the images format. - /// - /// The image stream to read the header from. - /// The configuration. - /// The decoder and the image format or null if none found. - 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); - } - /// /// Decodes the image stream to the current image. /// @@ -166,32 +132,6 @@ namespace SixLabors.ImageSharp return (img, format); } - /// - /// Decodes the image stream to the current image. - /// - /// The stream. - /// the configuration. - /// The token to monitor for cancellation requests. - /// The pixel format. - /// A representing the asynchronous operation. - private static async Task<(Image Image, IImageFormat Format)> DecodeAsync( - Stream stream, - Configuration config, - CancellationToken cancellationToken) - where TPixel : unmanaged, IPixel - { - (IImageDecoder decoder, IImageFormat format) = await DiscoverDecoderAsync(stream, config) - .ConfigureAwait(false); - if (decoder is null) - { - return (null, null); - } - - Image img = await decoder.DecodeAsync(config, stream, cancellationToken) - .ConfigureAwait(false); - return (img, format); - } - private static (Image Image, IImageFormat Format) Decode(Stream stream, Configuration config, CancellationToken cancellationToken = default) { IImageDecoder decoder = DiscoverDecoder(stream, config, out IImageFormat format); @@ -204,18 +144,6 @@ namespace SixLabors.ImageSharp return (img, format); } - private static async Task<(Image Image, IImageFormat Format)> DecodeAsync(Stream stream, Configuration config, CancellationToken cancellationToken) - { - (IImageDecoder decoder, IImageFormat format) = await DiscoverDecoderAsync(stream, config).ConfigureAwait(false); - if (decoder is null) - { - return (null, null); - } - - Image img = await decoder.DecodeAsync(config, stream, cancellationToken).ConfigureAwait(false); - return (img, format); - } - /// /// Reads the raw image information from the specified stream. /// @@ -237,33 +165,5 @@ namespace SixLabors.ImageSharp IImageInfo info = detector?.Identify(config, stream, cancellationToken); return (info, format); } - - /// - /// Reads the raw image information from the specified stream. - /// - /// The stream. - /// the configuration. - /// The token to monitor for cancellation requests. - /// - /// A representing the asynchronous operation with the - /// property of the returned type set to null if a suitable detector - /// is not found. - private static async Task<(IImageInfo ImageInfo, IImageFormat Format)> InternalIdentityAsync(Stream stream, Configuration config, CancellationToken cancellationToken) - { - (IImageDecoder decoder, IImageFormat format) = await DiscoverDecoderAsync(stream, config).ConfigureAwait(false); - - if (!(decoder is IImageInfoDetector detector)) - { - return (null, null); - } - - if (detector is null) - { - return (null, format); - } - - IImageInfo info = await detector.IdentifyAsync(config, stream, cancellationToken).ConfigureAwait(false); - return (info, format); - } } } diff --git a/src/ImageSharp/Image.FromStream.cs b/src/ImageSharp/Image.FromStream.cs index 43f4449c53..8260caf56f 100644 --- a/src/ImageSharp/Image.FromStream.cs +++ b/src/ImageSharp/Image.FromStream.cs @@ -65,7 +65,7 @@ namespace SixLabors.ImageSharp => WithSeekableStreamAsync( configuration, stream, - (s, _) => InternalDetectFormatAsync(s, configuration), + (s, _) => InternalDetectFormat(s, configuration), cancellationToken); /// @@ -208,7 +208,7 @@ namespace SixLabors.ImageSharp => WithSeekableStreamAsync( configuration, stream, - (s, ct) => InternalIdentityAsync(s, configuration ?? Configuration.Default, ct), + (s, ct) => InternalIdentity(s, configuration ?? Configuration.Default, ct), cancellationToken); /// @@ -341,7 +341,7 @@ namespace SixLabors.ImageSharp return WithSeekableStreamAsync( configuration, stream, - (s, ct) => decoder.DecodeAsync(configuration, s, ct), + (s, ct) => decoder.Decode(configuration, s, ct), cancellationToken); } @@ -468,7 +468,7 @@ namespace SixLabors.ImageSharp => WithSeekableStreamAsync( Configuration.Default, stream, - (s, ct) => decoder.DecodeAsync(Configuration.Default, s, ct), + (s, ct) => decoder.Decode(Configuration.Default, s, ct), cancellationToken); /// @@ -511,7 +511,7 @@ namespace SixLabors.ImageSharp => WithSeekableStreamAsync( configuration, stream, - (s, ct) => decoder.DecodeAsync(configuration, s, ct), + (s, ct) => decoder.Decode(configuration, s, ct), cancellationToken); /// @@ -586,7 +586,7 @@ namespace SixLabors.ImageSharp (Image Image, IImageFormat Format) data = await WithSeekableStreamAsync( configuration, stream, - async (s, ct) => await DecodeAsync(s, configuration, ct).ConfigureAwait(false), + (s, ct) => Decode(s, configuration, ct), cancellationToken) .ConfigureAwait(false); @@ -629,7 +629,7 @@ namespace SixLabors.ImageSharp await WithSeekableStreamAsync( configuration, stream, - (s, ct) => DecodeAsync(s, configuration, ct), + (s, ct) => Decode(s, configuration, ct), cancellationToken) .ConfigureAwait(false); @@ -759,7 +759,7 @@ namespace SixLabors.ImageSharp private static async Task WithSeekableStreamAsync( Configuration configuration, Stream stream, - Func> action, + Func action, CancellationToken cancellationToken) { Guard.NotNull(configuration, nameof(configuration)); @@ -774,7 +774,7 @@ namespace SixLabors.ImageSharp await stream.CopyToAsync(memoryStream, configuration.StreamProcessingBufferSize, cancellationToken).ConfigureAwait(false); memoryStream.Position = 0; - return await action(memoryStream, cancellationToken).ConfigureAwait(false); + return action(memoryStream, cancellationToken); } } }