From 803ebaa8ad8893fb0a89daaa81f7b37bacb8c08a Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Sat, 25 Jul 2020 19:13:37 +0200 Subject: [PATCH] adapt #1286 --- src/ImageSharp/Formats/Bmp/BmpDecoder.cs | 8 ++--- src/ImageSharp/Formats/Gif/GifDecoder.cs | 10 +++---- .../Formats/ImageDecoderUtilities.cs | 29 +++++++++++++------ src/ImageSharp/Formats/Jpeg/JpegDecoder.cs | 8 ++--- src/ImageSharp/Formats/Png/PngDecoder.cs | 8 ++--- src/ImageSharp/Formats/Png/PngDecoderCore.cs | 2 +- src/ImageSharp/Formats/Tga/TgaDecoder.cs | 8 ++--- src/ImageSharp/Image.FromStream.cs | 2 +- 8 files changed, 43 insertions(+), 32 deletions(-) diff --git a/src/ImageSharp/Formats/Bmp/BmpDecoder.cs b/src/ImageSharp/Formats/Bmp/BmpDecoder.cs index e057db1508..fe831caf83 100644 --- a/src/ImageSharp/Formats/Bmp/BmpDecoder.cs +++ b/src/ImageSharp/Formats/Bmp/BmpDecoder.cs @@ -37,7 +37,7 @@ namespace SixLabors.ImageSharp.Formats.Bmp Guard.NotNull(stream, nameof(stream)); var decoder = new BmpDecoderCore(configuration, this); - return decoder.Decode(stream, CreateLargeImageException); + return decoder.Decode(configuration, stream, CreateLargeImageException); } /// @@ -51,7 +51,7 @@ namespace SixLabors.ImageSharp.Formats.Bmp Guard.NotNull(stream, nameof(stream)); var decoder = new BmpDecoderCore(configuration, this); - return decoder.DecodeAsync(stream, CreateLargeImageException, cancellationToken); + return decoder.DecodeAsync(configuration, stream, CreateLargeImageException, cancellationToken); } /// @@ -64,7 +64,7 @@ namespace SixLabors.ImageSharp.Formats.Bmp { Guard.NotNull(stream, nameof(stream)); - return new BmpDecoderCore(configuration, this).Identify(stream, CreateLargeImageException); + return new BmpDecoderCore(configuration, this).Identify(configuration, stream, CreateLargeImageException); } /// @@ -72,7 +72,7 @@ namespace SixLabors.ImageSharp.Formats.Bmp { Guard.NotNull(stream, nameof(stream)); - return new BmpDecoderCore(configuration, this).IdentifyAsync(stream, CreateLargeImageException, cancellationToken); + return new BmpDecoderCore(configuration, this).IdentifyAsync(configuration, stream, CreateLargeImageException, cancellationToken); } private static InvalidImageContentException CreateLargeImageException(InvalidMemoryOperationException ex, Size dims) diff --git a/src/ImageSharp/Formats/Gif/GifDecoder.cs b/src/ImageSharp/Formats/Gif/GifDecoder.cs index 206dd6f2f5..e42f9c9f25 100644 --- a/src/ImageSharp/Formats/Gif/GifDecoder.cs +++ b/src/ImageSharp/Formats/Gif/GifDecoder.cs @@ -31,7 +31,7 @@ namespace SixLabors.ImageSharp.Formats.Gif where TPixel : unmanaged, IPixel { var decoder = new GifDecoderCore(configuration, this); - return decoder.Decode(stream); + return decoder.Decode(configuration, stream); } /// @@ -43,7 +43,7 @@ namespace SixLabors.ImageSharp.Formats.Gif where TPixel : unmanaged, IPixel { var decoder = new GifDecoderCore(configuration, this); - return decoder.DecodeAsync(stream, cancellationToken); + return decoder.DecodeAsync(configuration, stream, cancellationToken); } /// @@ -58,7 +58,7 @@ namespace SixLabors.ImageSharp.Formats.Gif var decoder = new GifDecoderCore(configuration, this); - using var bufferedStream = new BufferedReadStream(stream); + using var bufferedStream = new BufferedReadStream(configuration, stream); return decoder.Identify(bufferedStream, default); } @@ -69,8 +69,8 @@ namespace SixLabors.ImageSharp.Formats.Gif var decoder = new GifDecoderCore(configuration, this); - using var bufferedStream = new BufferedReadStream(stream); - return await decoder.IdentifyAsync(bufferedStream, cancellationToken); + using var bufferedStream = new BufferedReadStream(configuration, stream); + return await decoder.IdentifyAsync(configuration, bufferedStream, cancellationToken); } } } diff --git a/src/ImageSharp/Formats/ImageDecoderUtilities.cs b/src/ImageSharp/Formats/ImageDecoderUtilities.cs index a7ade5e24a..5e4aeb9eb6 100644 --- a/src/ImageSharp/Formats/ImageDecoderUtilities.cs +++ b/src/ImageSharp/Formats/ImageDecoderUtilities.cs @@ -17,20 +17,23 @@ namespace SixLabors.ImageSharp.Formats /// Reads the raw image information from the specified stream. /// /// The decoder. + /// /// The configuration for the image. /// The containing image data. /// The token to monitor for cancellation requests. /// is null. /// A representing the asynchronous operation. public static Task IdentifyAsync( this IImageDecoderInternals decoder, + Configuration configuration, Stream stream, CancellationToken cancellationToken) - => decoder.IdentifyAsync(stream, DefaultLargeImageExceptionFactory, cancellationToken); + => decoder.IdentifyAsync(configuration, stream, DefaultLargeImageExceptionFactory, cancellationToken); /// /// Reads the raw image information from the specified stream. /// /// The decoder. + /// The configuration for the image. /// The containing image data. /// Factory method to handle as . /// The token to monitor for cancellation requests. @@ -38,13 +41,14 @@ namespace SixLabors.ImageSharp.Formats /// A representing the asynchronous operation. public static Task IdentifyAsync( this IImageDecoderInternals decoder, + Configuration configuration, Stream stream, Func tooLargeImageExceptionFactory, CancellationToken cancellationToken) { try { - using BufferedReadStream bufferedReadStream = new BufferedReadStream(stream); + using BufferedReadStream bufferedReadStream = new BufferedReadStream(configuration, stream); IImageInfo imageInfo = decoder.Identify(bufferedReadStream, cancellationToken); return Task.FromResult(imageInfo); } @@ -68,15 +72,18 @@ namespace SixLabors.ImageSharp.Formats /// /// The pixel format. /// The decoder. + /// The configuration for the image. /// The containing image data. /// The token to monitor for cancellation requests. /// A representing the asynchronous operation. public static Task> DecodeAsync( this IImageDecoderInternals decoder, + Configuration configuration, Stream stream, CancellationToken cancellationToken) where TPixel : unmanaged, IPixel => decoder.DecodeAsync( + configuration, stream, DefaultLargeImageExceptionFactory, cancellationToken); @@ -86,12 +93,14 @@ namespace SixLabors.ImageSharp.Formats /// /// The pixel format. /// The decoder. + /// The configuration for the image. /// The containing image data. /// Factory method to handle as . /// The token to monitor for cancellation requests. /// A representing the asynchronous operation. public static Task> DecodeAsync( this IImageDecoderInternals decoder, + Configuration configuration, Stream stream, Func largeImageExceptionFactory, CancellationToken cancellationToken) @@ -99,7 +108,7 @@ namespace SixLabors.ImageSharp.Formats { try { - using BufferedReadStream bufferedReadStream = new BufferedReadStream(stream); + using BufferedReadStream bufferedReadStream = new BufferedReadStream(configuration, stream); Image image = decoder.Decode(bufferedReadStream, cancellationToken); return Task.FromResult(image); } @@ -118,15 +127,16 @@ namespace SixLabors.ImageSharp.Formats } } - public static IImageInfo Identify(this IImageDecoderInternals decoder, Stream stream) - => decoder.Identify(stream, DefaultLargeImageExceptionFactory); + public static IImageInfo Identify(this IImageDecoderInternals decoder, Configuration configuration, Stream stream) + => decoder.Identify(configuration, stream, DefaultLargeImageExceptionFactory); public static IImageInfo Identify( this IImageDecoderInternals decoder, + Configuration configuration, Stream stream, Func largeImageExceptionFactory) { - using BufferedReadStream bufferedReadStream = new BufferedReadStream(stream); + using BufferedReadStream bufferedReadStream = new BufferedReadStream(configuration, stream); try { @@ -138,17 +148,18 @@ namespace SixLabors.ImageSharp.Formats } } - public static Image Decode(this IImageDecoderInternals decoder, Stream stream) + public static Image Decode(this IImageDecoderInternals decoder, Configuration configuration, Stream stream) where TPixel : unmanaged, IPixel - => decoder.Decode(stream, DefaultLargeImageExceptionFactory); + => decoder.Decode(configuration, stream, DefaultLargeImageExceptionFactory); public static Image Decode( this IImageDecoderInternals decoder, + Configuration configuration, Stream stream, Func largeImageExceptionFactory) where TPixel : unmanaged, IPixel { - using BufferedReadStream bufferedReadStream = new BufferedReadStream(stream); + using BufferedReadStream bufferedReadStream = new BufferedReadStream(configuration, stream); try { diff --git a/src/ImageSharp/Formats/Jpeg/JpegDecoder.cs b/src/ImageSharp/Formats/Jpeg/JpegDecoder.cs index 106c1f597d..93697db95f 100644 --- a/src/ImageSharp/Formats/Jpeg/JpegDecoder.cs +++ b/src/ImageSharp/Formats/Jpeg/JpegDecoder.cs @@ -25,7 +25,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg Guard.NotNull(stream, nameof(stream)); using var decoder = new JpegDecoderCore(configuration, this); - return decoder.Decode(stream); + return decoder.Decode(configuration, stream); } /// @@ -39,7 +39,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg Guard.NotNull(stream, nameof(stream)); using var decoder = new JpegDecoderCore(configuration, this); - return decoder.DecodeAsync(stream, cancellationToken); + return decoder.DecodeAsync(configuration, stream, cancellationToken); } /// @@ -52,7 +52,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg Guard.NotNull(stream, nameof(stream)); using var decoder = new JpegDecoderCore(configuration, this); - return decoder.Identify(stream); + return decoder.Identify(configuration, stream); } /// @@ -61,7 +61,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg Guard.NotNull(stream, nameof(stream)); using var decoder = new JpegDecoderCore(configuration, this); - return decoder.IdentifyAsync(stream, cancellationToken); + return decoder.IdentifyAsync(configuration, stream, cancellationToken); } } } diff --git a/src/ImageSharp/Formats/Png/PngDecoder.cs b/src/ImageSharp/Formats/Png/PngDecoder.cs index 677a2003e9..1ee47a0170 100644 --- a/src/ImageSharp/Formats/Png/PngDecoder.cs +++ b/src/ImageSharp/Formats/Png/PngDecoder.cs @@ -23,7 +23,7 @@ namespace SixLabors.ImageSharp.Formats.Png where TPixel : unmanaged, IPixel { var decoder = new PngDecoderCore(configuration, this); - return decoder.Decode(stream); + return decoder.Decode(configuration, stream); } /// @@ -34,7 +34,7 @@ namespace SixLabors.ImageSharp.Formats.Png where TPixel : unmanaged, IPixel { var decoder = new PngDecoderCore(configuration, this); - return decoder.DecodeAsync(stream, cancellationToken); + return decoder.DecodeAsync(configuration, stream, cancellationToken); } /// @@ -45,14 +45,14 @@ namespace SixLabors.ImageSharp.Formats.Png public IImageInfo Identify(Configuration configuration, Stream stream) { var decoder = new PngDecoderCore(configuration, this); - return decoder.Identify(stream); + return decoder.Identify(configuration, stream); } /// public Task IdentifyAsync(Configuration configuration, Stream stream, CancellationToken cancellationToken) { var decoder = new PngDecoderCore(configuration, this); - return decoder.IdentifyAsync(stream, cancellationToken); + return decoder.IdentifyAsync(configuration, stream, cancellationToken); } } } diff --git a/src/ImageSharp/Formats/Png/PngDecoderCore.cs b/src/ImageSharp/Formats/Png/PngDecoderCore.cs index b751a704af..3fa0e3f586 100644 --- a/src/ImageSharp/Formats/Png/PngDecoderCore.cs +++ b/src/ImageSharp/Formats/Png/PngDecoderCore.cs @@ -1028,7 +1028,7 @@ namespace SixLabors.ImageSharp.Formats.Png private bool TryUncompressTextData(ReadOnlySpan compressedData, Encoding encoding, out string value) { using (var memoryStream = new MemoryStream(compressedData.ToArray())) - using (var bufferedStream = new BufferedReadStream(memoryStream)) + using (var bufferedStream = new BufferedReadStream(this.Configuration, memoryStream)) using (var inflateStream = new ZlibInflateStream(bufferedStream)) { if (!inflateStream.AllocateNewBytes(compressedData.Length, false)) diff --git a/src/ImageSharp/Formats/Tga/TgaDecoder.cs b/src/ImageSharp/Formats/Tga/TgaDecoder.cs index bcf52a0119..bad769fa91 100644 --- a/src/ImageSharp/Formats/Tga/TgaDecoder.cs +++ b/src/ImageSharp/Formats/Tga/TgaDecoder.cs @@ -22,7 +22,7 @@ namespace SixLabors.ImageSharp.Formats.Tga Guard.NotNull(stream, nameof(stream)); var decoder = new TgaDecoderCore(configuration, this); - return decoder.Decode(stream); + return decoder.Decode(configuration, stream); } /// @@ -36,7 +36,7 @@ namespace SixLabors.ImageSharp.Formats.Tga Guard.NotNull(stream, nameof(stream)); var decoder = new TgaDecoderCore(configuration, this); - return decoder.DecodeAsync(stream, cancellationToken); + return decoder.DecodeAsync(configuration, stream, cancellationToken); } /// @@ -48,7 +48,7 @@ namespace SixLabors.ImageSharp.Formats.Tga { Guard.NotNull(stream, nameof(stream)); - return new TgaDecoderCore(configuration, this).Identify(stream); + return new TgaDecoderCore(configuration, this).Identify(configuration, stream); } /// @@ -56,7 +56,7 @@ namespace SixLabors.ImageSharp.Formats.Tga { Guard.NotNull(stream, nameof(stream)); - return new TgaDecoderCore(configuration, this).IdentifyAsync(stream, cancellationToken); + return new TgaDecoderCore(configuration, this).IdentifyAsync(configuration, stream, cancellationToken); } } } diff --git a/src/ImageSharp/Image.FromStream.cs b/src/ImageSharp/Image.FromStream.cs index 182f0ac822..58d6640a06 100644 --- a/src/ImageSharp/Image.FromStream.cs +++ b/src/ImageSharp/Image.FromStream.cs @@ -891,7 +891,7 @@ namespace SixLabors.ImageSharp } using MemoryStream memoryStream = configuration.MemoryAllocator.AllocateFixedCapacityMemoryStream(stream.Length); - TODO! await stream.CopyToAsync(memoryStream, cancellationToken).ConfigureAwait(false); + await stream.CopyToAsync(memoryStream, configuration.StreamProcessingBufferSize, cancellationToken).ConfigureAwait(false); memoryStream.Position = 0; return await action(memoryStream, cancellationToken).ConfigureAwait(false);