Exemplo n.º 1
0
 /// <summary>
 /// Reads the raw image information from the specified stream.
 /// </summary>
 /// <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="tooLargeImageExceptionFactory">Factory method to handle <see cref="InvalidMemoryOperationException"/> as <see cref="InvalidImageContentException"/>.</param>
 /// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
 /// <exception cref="ArgumentNullException"><paramref name="stream"/> is null.</exception>
 /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
 public static Task <IImageInfo> IdentifyAsync(
     this IImageDecoderInternals decoder,
     Configuration configuration,
     Stream stream,
     Func <InvalidMemoryOperationException, Size, InvalidImageContentException> tooLargeImageExceptionFactory,
     CancellationToken cancellationToken)
 {
     try
     {
         using var bufferedReadStream = new BufferedReadStream(configuration, stream);
         IImageInfo imageInfo = decoder.Identify(bufferedReadStream, cancellationToken);
         return(Task.FromResult(imageInfo));
     }
     catch (InvalidMemoryOperationException ex)
     {
         InvalidImageContentException invalidImageContentException = tooLargeImageExceptionFactory(ex, decoder.Dimensions);
         return(Task.FromException <IImageInfo>(invalidImageContentException));
     }
     catch (OperationCanceledException)
     {
         return(Task.FromCanceled <IImageInfo>(cancellationToken));
     }
     catch (Exception ex)
     {
         return(Task.FromException <IImageInfo>(ex));
     }
 }
Exemplo n.º 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));
     }
 }
Exemplo n.º 3
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="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,
     CancellationToken cancellationToken)
     where TPixel : unmanaged, IPixel <TPixel> =>
 decoder.DecodeAsync <TPixel>(
     configuration,
     stream,
     DefaultLargeImageExceptionFactory,
     cancellationToken);
Exemplo n.º 4
0
        /// <summary>
        /// Reads the raw image information from the specified stream.
        /// </summary>
        /// <param name="decoder">The decoder.</param>
        /// <param name="stream">The <see cref="Stream"/> containing image data.</param>
        public static async Task <IImageInfo> IdentifyAsync(this IImageDecoderInternals decoder, Stream stream)
        {
            if (stream.CanSeek)
            {
                return(decoder.Identify(stream));
            }

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

            ms.Position = 0;
            return(decoder.Identify(ms));
        }
Exemplo n.º 5
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="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));
        }
Exemplo n.º 6
0
        public static IImageInfo Identify(
            this IImageDecoderInternals decoder,
            Configuration configuration,
            Stream stream)
        {
            using var bufferedReadStream = new BufferedReadStream(configuration, stream);

            try
            {
                return(decoder.Identify(bufferedReadStream, default));
            }
            catch (InvalidMemoryOperationException ex)
            {
                throw new InvalidImageContentException(decoder.Dimensions, ex);
            }
        }
Exemplo n.º 7
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);
            }
        }
Exemplo n.º 8
0
 /// <summary>
 /// Reads the raw image information from the specified stream.
 /// </summary>
 /// <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="cancellationToken">The token to monitor for cancellation requests.</param>
 /// <exception cref="ArgumentNullException"><paramref name="stream"/> is null.</exception>
 /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
 public static Task <IImageInfo> IdentifyAsync(
     this IImageDecoderInternals decoder,
     Configuration configuration,
     Stream stream,
     CancellationToken cancellationToken)
 => decoder.IdentifyAsync(configuration, stream, DefaultLargeImageExceptionFactory, cancellationToken);
Exemplo n.º 9
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);
Exemplo n.º 10
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="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));
Exemplo n.º 11
0
 /// <summary>
 /// Reads the raw image information from the specified stream.
 /// </summary>
 /// <param name="decoder">The decoder.</param>
 /// <param name="stream">The <see cref="BufferedReadStream"/> containing image data.</param>
 /// <exception cref="ArgumentNullException"><paramref name="stream"/> is null.</exception>
 /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
 public static Task <IImageInfo> IdentifyAsync(this IImageDecoderInternals decoder, BufferedReadStream stream)
 => Task.FromResult(decoder.Identify(stream));