@ -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 ;
}
/// <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 mime type or null if none found.</returns>
private static Task < IImageFormat > 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 ) ) ;
}
/// <summary>
/// By reading the header on the provided stream this calculates the images format.
/// </summary>
@ -126,23 +109,6 @@ namespace SixLabors.ImageSharp
: 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>
@ -166,32 +132,6 @@ namespace SixLabors.ImageSharp
return ( img , format ) ;
}
/// <summary>
/// Decodes the image stream to the current image.
/// </summary>
/// <param name="stream">The stream.</param>
/// <param name="config">the configuration.</param>
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
/// <typeparam name="TPixel">The pixel format.</typeparam>
/// <returns>A <see cref="Task{ValueTuple}"/> representing the asynchronous operation.</returns>
private static async Task < ( Image < TPixel > Image , IImageFormat Format ) > DecodeAsync < TPixel > (
Stream stream ,
Configuration config ,
CancellationToken cancellationToken )
where TPixel : unmanaged , IPixel < TPixel >
{
( IImageDecoder decoder , IImageFormat format ) = await DiscoverDecoderAsync ( stream , config )
. ConfigureAwait ( false ) ;
if ( decoder is null )
{
return ( null , null ) ;
}
Image < TPixel > img = await decoder . DecodeAsync < TPixel > ( 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 ) ;
}
/// <summary>
/// Reads the raw image information from the specified stream.
/// </summary>
@ -237,33 +165,5 @@ namespace SixLabors.ImageSharp
IImageInfo info = detector ? . Identify ( config , stream , cancellationToken ) ;
return ( info , format ) ;
}
/// <summary>
/// Reads the raw image information from the specified stream.
/// </summary>
/// <param name="stream">The stream.</param>
/// <param name="config">the configuration.</param>
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
/// <returns>
/// A <see cref="Task{ValueTuple}"/> representing the asynchronous operation with the
/// <see cref="IImageInfo"/> property of the returned type set to null if a suitable detector
/// is not found.</returns>
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 ) ;
}
}
}