/// <summary>
        /// Decodes the image from the specified stream.
        /// </summary>
        /// <typeparam name="TPixel">The pixel format.</typeparam>
        /// <param name="decoder">The decoder.</param>
        /// <param name="stream">The stream, where the image should be decoded from. Cannot be null.</param>
        /// <exception cref="System.ArgumentNullException">
        ///    <para><paramref name="stream"/> is null.</para>
        /// </exception>
        /// <returns>The decoded image.</returns>
        public static async Task <Image <TPixel> > DecodeAsync <TPixel>(this IImageDecoderInternals decoder, Stream stream)
            where TPixel : unmanaged, IPixel <TPixel>
        {
            if (stream.CanSeek)
            {
                return(decoder.Decode <TPixel>(stream));
            }

            using MemoryStream ms = decoder.Configuration.MemoryAllocator.AllocateFixedCapacityMemoryStream(stream.Length);
            await stream.CopyToAsync(ms).ConfigureAwait(false);

            ms.Position = 0;
            return(decoder.Decode <TPixel>(ms));
        }
Example #2
0
 /// <summary>
 /// Decodes the image from the specified stream.
 /// </summary>
 /// <typeparam name="TPixel">The pixel format.</typeparam>
 /// <param name="decoder">The decoder.</param>
 /// <param name="configuration">The configuration for the image.</param>
 /// <param name="stream">The <see cref="Stream"/> containing image data.</param>
 /// <param name="largeImageExceptionFactory">Factory method to handle <see cref="InvalidMemoryOperationException"/> as <see cref="InvalidImageContentException"/>.</param>
 /// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
 /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
 public static Task <Image <TPixel> > DecodeAsync <TPixel>(
     this IImageDecoderInternals decoder,
     Configuration configuration,
     Stream stream,
     Func <InvalidMemoryOperationException, Size, InvalidImageContentException> largeImageExceptionFactory,
     CancellationToken cancellationToken)
     where TPixel : unmanaged, IPixel <TPixel>
 {
     try
     {
         using var bufferedReadStream = new BufferedReadStream(configuration, stream);
         Image <TPixel> image = decoder.Decode <TPixel>(bufferedReadStream, cancellationToken);
         return(Task.FromResult(image));
     }
     catch (InvalidMemoryOperationException ex)
     {
         InvalidImageContentException invalidImageContentException = largeImageExceptionFactory(ex, decoder.Dimensions);
         return(Task.FromException <Image <TPixel> >(invalidImageContentException));
     }
     catch (OperationCanceledException)
     {
         return(Task.FromCanceled <Image <TPixel> >(cancellationToken));
     }
     catch (Exception ex)
     {
         return(Task.FromException <Image <TPixel> >(ex));
     }
 }
Example #3
0
        public static Image <TPixel> Decode <TPixel>(
            this IImageDecoderInternals decoder,
            Configuration configuration,
            Stream stream,
            Func <InvalidMemoryOperationException, Size, InvalidImageContentException> largeImageExceptionFactory)
            where TPixel : unmanaged, IPixel <TPixel>
        {
            using var bufferedReadStream = new BufferedReadStream(configuration, stream);

            try
            {
                return(decoder.Decode <TPixel>(bufferedReadStream, default));
            }
            catch (InvalidMemoryOperationException ex)
            {
                throw largeImageExceptionFactory(ex, decoder.Dimensions);
            }
        }
Example #4
0
 public static Image <TPixel> Decode <TPixel>(this IImageDecoderInternals decoder, Configuration configuration, Stream stream)
     where TPixel : unmanaged, IPixel <TPixel>
 => decoder.Decode <TPixel>(configuration, stream, DefaultLargeImageExceptionFactory);
 /// <summary>
 /// Decodes the image from the specified stream.
 /// </summary>
 /// <typeparam name="TPixel">The pixel format.</typeparam>
 /// <param name="decoder">The decoder.</param>
 /// <param name="stream">The <see cref="BufferedReadStream"/> containing image data.</param>
 /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
 public static Task <Image <TPixel> > DecodeAsync <TPixel>(this IImageDecoderInternals decoder, BufferedReadStream stream)
     where TPixel : unmanaged, IPixel <TPixel>
 => Task.FromResult(decoder.Decode <TPixel>(stream));