Browse Source

adapt #1286

pull/1574/head
Anton Firszov 6 years ago
parent
commit
803ebaa8ad
  1. 8
      src/ImageSharp/Formats/Bmp/BmpDecoder.cs
  2. 10
      src/ImageSharp/Formats/Gif/GifDecoder.cs
  3. 29
      src/ImageSharp/Formats/ImageDecoderUtilities.cs
  4. 8
      src/ImageSharp/Formats/Jpeg/JpegDecoder.cs
  5. 8
      src/ImageSharp/Formats/Png/PngDecoder.cs
  6. 2
      src/ImageSharp/Formats/Png/PngDecoderCore.cs
  7. 8
      src/ImageSharp/Formats/Tga/TgaDecoder.cs
  8. 2
      src/ImageSharp/Image.FromStream.cs

8
src/ImageSharp/Formats/Bmp/BmpDecoder.cs

@ -37,7 +37,7 @@ namespace SixLabors.ImageSharp.Formats.Bmp
Guard.NotNull(stream, nameof(stream)); Guard.NotNull(stream, nameof(stream));
var decoder = new BmpDecoderCore(configuration, this); var decoder = new BmpDecoderCore(configuration, this);
return decoder.Decode<TPixel>(stream, CreateLargeImageException); return decoder.Decode<TPixel>(configuration, stream, CreateLargeImageException);
} }
/// <inheritdoc /> /// <inheritdoc />
@ -51,7 +51,7 @@ namespace SixLabors.ImageSharp.Formats.Bmp
Guard.NotNull(stream, nameof(stream)); Guard.NotNull(stream, nameof(stream));
var decoder = new BmpDecoderCore(configuration, this); var decoder = new BmpDecoderCore(configuration, this);
return decoder.DecodeAsync<TPixel>(stream, CreateLargeImageException, cancellationToken); return decoder.DecodeAsync<TPixel>(configuration, stream, CreateLargeImageException, cancellationToken);
} }
/// <inheritdoc /> /// <inheritdoc />
@ -64,7 +64,7 @@ namespace SixLabors.ImageSharp.Formats.Bmp
{ {
Guard.NotNull(stream, nameof(stream)); Guard.NotNull(stream, nameof(stream));
return new BmpDecoderCore(configuration, this).Identify(stream, CreateLargeImageException); return new BmpDecoderCore(configuration, this).Identify(configuration, stream, CreateLargeImageException);
} }
/// <inheritdoc/> /// <inheritdoc/>
@ -72,7 +72,7 @@ namespace SixLabors.ImageSharp.Formats.Bmp
{ {
Guard.NotNull(stream, nameof(stream)); 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) private static InvalidImageContentException CreateLargeImageException(InvalidMemoryOperationException ex, Size dims)

10
src/ImageSharp/Formats/Gif/GifDecoder.cs

@ -31,7 +31,7 @@ namespace SixLabors.ImageSharp.Formats.Gif
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
{ {
var decoder = new GifDecoderCore(configuration, this); var decoder = new GifDecoderCore(configuration, this);
return decoder.Decode<TPixel>(stream); return decoder.Decode<TPixel>(configuration, stream);
} }
/// <inheritdoc /> /// <inheritdoc />
@ -43,7 +43,7 @@ namespace SixLabors.ImageSharp.Formats.Gif
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
{ {
var decoder = new GifDecoderCore(configuration, this); var decoder = new GifDecoderCore(configuration, this);
return decoder.DecodeAsync<TPixel>(stream, cancellationToken); return decoder.DecodeAsync<TPixel>(configuration, stream, cancellationToken);
} }
/// <inheritdoc /> /// <inheritdoc />
@ -58,7 +58,7 @@ namespace SixLabors.ImageSharp.Formats.Gif
var decoder = new GifDecoderCore(configuration, this); var decoder = new GifDecoderCore(configuration, this);
using var bufferedStream = new BufferedReadStream(stream); using var bufferedStream = new BufferedReadStream(configuration, stream);
return decoder.Identify(bufferedStream, default); return decoder.Identify(bufferedStream, default);
} }
@ -69,8 +69,8 @@ namespace SixLabors.ImageSharp.Formats.Gif
var decoder = new GifDecoderCore(configuration, this); var decoder = new GifDecoderCore(configuration, this);
using var bufferedStream = new BufferedReadStream(stream); using var bufferedStream = new BufferedReadStream(configuration, stream);
return await decoder.IdentifyAsync(bufferedStream, cancellationToken); return await decoder.IdentifyAsync(configuration, bufferedStream, cancellationToken);
} }
} }
} }

29
src/ImageSharp/Formats/ImageDecoderUtilities.cs

@ -17,20 +17,23 @@ namespace SixLabors.ImageSharp.Formats
/// Reads the raw image information from the specified stream. /// Reads the raw image information from the specified stream.
/// </summary> /// </summary>
/// <param name="decoder">The decoder.</param> /// <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="stream">The <see cref="Stream"/> containing image data.</param>
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param> /// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
/// <exception cref="ArgumentNullException"><paramref name="stream"/> is null.</exception> /// <exception cref="ArgumentNullException"><paramref name="stream"/> is null.</exception>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns> /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
public static Task<IImageInfo> IdentifyAsync( public static Task<IImageInfo> IdentifyAsync(
this IImageDecoderInternals decoder, this IImageDecoderInternals decoder,
Configuration configuration,
Stream stream, Stream stream,
CancellationToken cancellationToken) CancellationToken cancellationToken)
=> decoder.IdentifyAsync(stream, DefaultLargeImageExceptionFactory, cancellationToken); => decoder.IdentifyAsync(configuration, stream, DefaultLargeImageExceptionFactory, cancellationToken);
/// <summary> /// <summary>
/// Reads the raw image information from the specified stream. /// Reads the raw image information from the specified stream.
/// </summary> /// </summary>
/// <param name="decoder">The decoder.</param> /// <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="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="tooLargeImageExceptionFactory">Factory method to handle <see cref="InvalidMemoryOperationException"/> as <see cref="InvalidImageContentException"/>.</param>
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param> /// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
@ -38,13 +41,14 @@ namespace SixLabors.ImageSharp.Formats
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns> /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
public static Task<IImageInfo> IdentifyAsync( public static Task<IImageInfo> IdentifyAsync(
this IImageDecoderInternals decoder, this IImageDecoderInternals decoder,
Configuration configuration,
Stream stream, Stream stream,
Func<InvalidMemoryOperationException, Size, InvalidImageContentException> tooLargeImageExceptionFactory, Func<InvalidMemoryOperationException, Size, InvalidImageContentException> tooLargeImageExceptionFactory,
CancellationToken cancellationToken) CancellationToken cancellationToken)
{ {
try try
{ {
using BufferedReadStream bufferedReadStream = new BufferedReadStream(stream); using BufferedReadStream bufferedReadStream = new BufferedReadStream(configuration, stream);
IImageInfo imageInfo = decoder.Identify(bufferedReadStream, cancellationToken); IImageInfo imageInfo = decoder.Identify(bufferedReadStream, cancellationToken);
return Task.FromResult(imageInfo); return Task.FromResult(imageInfo);
} }
@ -68,15 +72,18 @@ namespace SixLabors.ImageSharp.Formats
/// </summary> /// </summary>
/// <typeparam name="TPixel">The pixel format.</typeparam> /// <typeparam name="TPixel">The pixel format.</typeparam>
/// <param name="decoder">The decoder.</param> /// <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="stream">The <see cref="Stream"/> containing image data.</param>
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param> /// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns> /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
public static Task<Image<TPixel>> DecodeAsync<TPixel>( public static Task<Image<TPixel>> DecodeAsync<TPixel>(
this IImageDecoderInternals decoder, this IImageDecoderInternals decoder,
Configuration configuration,
Stream stream, Stream stream,
CancellationToken cancellationToken) CancellationToken cancellationToken)
where TPixel : unmanaged, IPixel<TPixel> => where TPixel : unmanaged, IPixel<TPixel> =>
decoder.DecodeAsync<TPixel>( decoder.DecodeAsync<TPixel>(
configuration,
stream, stream,
DefaultLargeImageExceptionFactory, DefaultLargeImageExceptionFactory,
cancellationToken); cancellationToken);
@ -86,12 +93,14 @@ namespace SixLabors.ImageSharp.Formats
/// </summary> /// </summary>
/// <typeparam name="TPixel">The pixel format.</typeparam> /// <typeparam name="TPixel">The pixel format.</typeparam>
/// <param name="decoder">The decoder.</param> /// <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="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="largeImageExceptionFactory">Factory method to handle <see cref="InvalidMemoryOperationException"/> as <see cref="InvalidImageContentException"/>.</param>
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param> /// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns> /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
public static Task<Image<TPixel>> DecodeAsync<TPixel>( public static Task<Image<TPixel>> DecodeAsync<TPixel>(
this IImageDecoderInternals decoder, this IImageDecoderInternals decoder,
Configuration configuration,
Stream stream, Stream stream,
Func<InvalidMemoryOperationException, Size, InvalidImageContentException> largeImageExceptionFactory, Func<InvalidMemoryOperationException, Size, InvalidImageContentException> largeImageExceptionFactory,
CancellationToken cancellationToken) CancellationToken cancellationToken)
@ -99,7 +108,7 @@ namespace SixLabors.ImageSharp.Formats
{ {
try try
{ {
using BufferedReadStream bufferedReadStream = new BufferedReadStream(stream); using BufferedReadStream bufferedReadStream = new BufferedReadStream(configuration, stream);
Image<TPixel> image = decoder.Decode<TPixel>(bufferedReadStream, cancellationToken); Image<TPixel> image = decoder.Decode<TPixel>(bufferedReadStream, cancellationToken);
return Task.FromResult(image); return Task.FromResult(image);
} }
@ -118,15 +127,16 @@ namespace SixLabors.ImageSharp.Formats
} }
} }
public static IImageInfo Identify(this IImageDecoderInternals decoder, Stream stream) public static IImageInfo Identify(this IImageDecoderInternals decoder, Configuration configuration, Stream stream)
=> decoder.Identify(stream, DefaultLargeImageExceptionFactory); => decoder.Identify(configuration, stream, DefaultLargeImageExceptionFactory);
public static IImageInfo Identify( public static IImageInfo Identify(
this IImageDecoderInternals decoder, this IImageDecoderInternals decoder,
Configuration configuration,
Stream stream, Stream stream,
Func<InvalidMemoryOperationException, Size, InvalidImageContentException> largeImageExceptionFactory) Func<InvalidMemoryOperationException, Size, InvalidImageContentException> largeImageExceptionFactory)
{ {
using BufferedReadStream bufferedReadStream = new BufferedReadStream(stream); using BufferedReadStream bufferedReadStream = new BufferedReadStream(configuration, stream);
try try
{ {
@ -138,17 +148,18 @@ namespace SixLabors.ImageSharp.Formats
} }
} }
public static Image<TPixel> Decode<TPixel>(this IImageDecoderInternals decoder, Stream stream) public static Image<TPixel> Decode<TPixel>(this IImageDecoderInternals decoder, Configuration configuration, Stream stream)
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
=> decoder.Decode<TPixel>(stream, DefaultLargeImageExceptionFactory); => decoder.Decode<TPixel>(configuration, stream, DefaultLargeImageExceptionFactory);
public static Image<TPixel> Decode<TPixel>( public static Image<TPixel> Decode<TPixel>(
this IImageDecoderInternals decoder, this IImageDecoderInternals decoder,
Configuration configuration,
Stream stream, Stream stream,
Func<InvalidMemoryOperationException, Size, InvalidImageContentException> largeImageExceptionFactory) Func<InvalidMemoryOperationException, Size, InvalidImageContentException> largeImageExceptionFactory)
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
{ {
using BufferedReadStream bufferedReadStream = new BufferedReadStream(stream); using BufferedReadStream bufferedReadStream = new BufferedReadStream(configuration, stream);
try try
{ {

8
src/ImageSharp/Formats/Jpeg/JpegDecoder.cs

@ -25,7 +25,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg
Guard.NotNull(stream, nameof(stream)); Guard.NotNull(stream, nameof(stream));
using var decoder = new JpegDecoderCore(configuration, this); using var decoder = new JpegDecoderCore(configuration, this);
return decoder.Decode<TPixel>(stream); return decoder.Decode<TPixel>(configuration, stream);
} }
/// <inheritdoc /> /// <inheritdoc />
@ -39,7 +39,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg
Guard.NotNull(stream, nameof(stream)); Guard.NotNull(stream, nameof(stream));
using var decoder = new JpegDecoderCore(configuration, this); using var decoder = new JpegDecoderCore(configuration, this);
return decoder.DecodeAsync<TPixel>(stream, cancellationToken); return decoder.DecodeAsync<TPixel>(configuration, stream, cancellationToken);
} }
/// <inheritdoc /> /// <inheritdoc />
@ -52,7 +52,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg
Guard.NotNull(stream, nameof(stream)); Guard.NotNull(stream, nameof(stream));
using var decoder = new JpegDecoderCore(configuration, this); using var decoder = new JpegDecoderCore(configuration, this);
return decoder.Identify(stream); return decoder.Identify(configuration, stream);
} }
/// <inheritdoc/> /// <inheritdoc/>
@ -61,7 +61,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg
Guard.NotNull(stream, nameof(stream)); Guard.NotNull(stream, nameof(stream));
using var decoder = new JpegDecoderCore(configuration, this); using var decoder = new JpegDecoderCore(configuration, this);
return decoder.IdentifyAsync(stream, cancellationToken); return decoder.IdentifyAsync(configuration, stream, cancellationToken);
} }
} }
} }

8
src/ImageSharp/Formats/Png/PngDecoder.cs

@ -23,7 +23,7 @@ namespace SixLabors.ImageSharp.Formats.Png
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
{ {
var decoder = new PngDecoderCore(configuration, this); var decoder = new PngDecoderCore(configuration, this);
return decoder.Decode<TPixel>(stream); return decoder.Decode<TPixel>(configuration, stream);
} }
/// <inheritdoc /> /// <inheritdoc />
@ -34,7 +34,7 @@ namespace SixLabors.ImageSharp.Formats.Png
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
{ {
var decoder = new PngDecoderCore(configuration, this); var decoder = new PngDecoderCore(configuration, this);
return decoder.DecodeAsync<TPixel>(stream, cancellationToken); return decoder.DecodeAsync<TPixel>(configuration, stream, cancellationToken);
} }
/// <inheritdoc /> /// <inheritdoc />
@ -45,14 +45,14 @@ namespace SixLabors.ImageSharp.Formats.Png
public IImageInfo Identify(Configuration configuration, Stream stream) public IImageInfo Identify(Configuration configuration, Stream stream)
{ {
var decoder = new PngDecoderCore(configuration, this); var decoder = new PngDecoderCore(configuration, this);
return decoder.Identify(stream); return decoder.Identify(configuration, stream);
} }
/// <inheritdoc/> /// <inheritdoc/>
public Task<IImageInfo> IdentifyAsync(Configuration configuration, Stream stream, CancellationToken cancellationToken) public Task<IImageInfo> IdentifyAsync(Configuration configuration, Stream stream, CancellationToken cancellationToken)
{ {
var decoder = new PngDecoderCore(configuration, this); var decoder = new PngDecoderCore(configuration, this);
return decoder.IdentifyAsync(stream, cancellationToken); return decoder.IdentifyAsync(configuration, stream, cancellationToken);
} }
} }
} }

2
src/ImageSharp/Formats/Png/PngDecoderCore.cs

@ -1028,7 +1028,7 @@ namespace SixLabors.ImageSharp.Formats.Png
private bool TryUncompressTextData(ReadOnlySpan<byte> compressedData, Encoding encoding, out string value) private bool TryUncompressTextData(ReadOnlySpan<byte> compressedData, Encoding encoding, out string value)
{ {
using (var memoryStream = new MemoryStream(compressedData.ToArray())) 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)) using (var inflateStream = new ZlibInflateStream(bufferedStream))
{ {
if (!inflateStream.AllocateNewBytes(compressedData.Length, false)) if (!inflateStream.AllocateNewBytes(compressedData.Length, false))

8
src/ImageSharp/Formats/Tga/TgaDecoder.cs

@ -22,7 +22,7 @@ namespace SixLabors.ImageSharp.Formats.Tga
Guard.NotNull(stream, nameof(stream)); Guard.NotNull(stream, nameof(stream));
var decoder = new TgaDecoderCore(configuration, this); var decoder = new TgaDecoderCore(configuration, this);
return decoder.Decode<TPixel>(stream); return decoder.Decode<TPixel>(configuration, stream);
} }
/// <inheritdoc /> /// <inheritdoc />
@ -36,7 +36,7 @@ namespace SixLabors.ImageSharp.Formats.Tga
Guard.NotNull(stream, nameof(stream)); Guard.NotNull(stream, nameof(stream));
var decoder = new TgaDecoderCore(configuration, this); var decoder = new TgaDecoderCore(configuration, this);
return decoder.DecodeAsync<TPixel>(stream, cancellationToken); return decoder.DecodeAsync<TPixel>(configuration, stream, cancellationToken);
} }
/// <inheritdoc /> /// <inheritdoc />
@ -48,7 +48,7 @@ namespace SixLabors.ImageSharp.Formats.Tga
{ {
Guard.NotNull(stream, nameof(stream)); Guard.NotNull(stream, nameof(stream));
return new TgaDecoderCore(configuration, this).Identify(stream); return new TgaDecoderCore(configuration, this).Identify(configuration, stream);
} }
/// <inheritdoc/> /// <inheritdoc/>
@ -56,7 +56,7 @@ namespace SixLabors.ImageSharp.Formats.Tga
{ {
Guard.NotNull(stream, nameof(stream)); Guard.NotNull(stream, nameof(stream));
return new TgaDecoderCore(configuration, this).IdentifyAsync(stream, cancellationToken); return new TgaDecoderCore(configuration, this).IdentifyAsync(configuration, stream, cancellationToken);
} }
} }
} }

2
src/ImageSharp/Image.FromStream.cs

@ -891,7 +891,7 @@ namespace SixLabors.ImageSharp
} }
using MemoryStream memoryStream = configuration.MemoryAllocator.AllocateFixedCapacityMemoryStream(stream.Length); 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; memoryStream.Position = 0;
return await action(memoryStream, cancellationToken).ConfigureAwait(false); return await action(memoryStream, cancellationToken).ConfigureAwait(false);

Loading…
Cancel
Save