Skip to content

Commit

Permalink
adapt #1286
Browse files Browse the repository at this point in the history
  • Loading branch information
antonfirsov committed Jul 25, 2020
1 parent 42d440e commit f31c8c1
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 32 deletions.
8 changes: 4 additions & 4 deletions src/ImageSharp/Formats/Bmp/BmpDecoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public Image<TPixel> Decode<TPixel>(Configuration configuration, Stream stream)
Guard.NotNull(stream, nameof(stream));

var decoder = new BmpDecoderCore(configuration, this);
return decoder.Decode<TPixel>(stream, CreateLargeImageException);
return decoder.Decode<TPixel>(configuration, stream, CreateLargeImageException);
}

/// <inheritdoc />
Expand All @@ -51,7 +51,7 @@ public Task<Image<TPixel>> DecodeAsync<TPixel>(Configuration configuration, Stre
Guard.NotNull(stream, nameof(stream));

var decoder = new BmpDecoderCore(configuration, this);
return decoder.DecodeAsync<TPixel>(stream, CreateLargeImageException, cancellationToken);
return decoder.DecodeAsync<TPixel>(configuration, stream, CreateLargeImageException, cancellationToken);
}

/// <inheritdoc />
Expand All @@ -64,15 +64,15 @@ public IImageInfo Identify(Configuration configuration, Stream stream)
{
Guard.NotNull(stream, nameof(stream));

return new BmpDecoderCore(configuration, this).Identify(stream, CreateLargeImageException);
return new BmpDecoderCore(configuration, this).Identify(configuration, stream, CreateLargeImageException);
}

/// <inheritdoc/>
public Task<IImageInfo> IdentifyAsync(Configuration configuration, Stream stream, CancellationToken cancellationToken)
{
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)
Expand Down
10 changes: 5 additions & 5 deletions src/ImageSharp/Formats/Gif/GifDecoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public Image<TPixel> Decode<TPixel>(Configuration configuration, Stream stream)
where TPixel : unmanaged, IPixel<TPixel>
{
var decoder = new GifDecoderCore(configuration, this);
return decoder.Decode<TPixel>(stream);
return decoder.Decode<TPixel>(configuration, stream);
}

/// <inheritdoc />
Expand All @@ -43,7 +43,7 @@ public Task<Image<TPixel>> DecodeAsync<TPixel>(Configuration configuration, Stre
where TPixel : unmanaged, IPixel<TPixel>
{
var decoder = new GifDecoderCore(configuration, this);
return decoder.DecodeAsync<TPixel>(stream, cancellationToken);
return decoder.DecodeAsync<TPixel>(configuration, stream, cancellationToken);
}

/// <inheritdoc />
Expand All @@ -58,7 +58,7 @@ public IImageInfo Identify(Configuration configuration, Stream stream)

var decoder = new GifDecoderCore(configuration, this);

using var bufferedStream = new BufferedReadStream(stream);
using var bufferedStream = new BufferedReadStream(configuration, stream);
return decoder.Identify(bufferedStream, default);
}

Expand All @@ -69,8 +69,8 @@ public async Task<IImageInfo> IdentifyAsync(Configuration configuration, Stream

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);
}
}
}
29 changes: 20 additions & 9 deletions src/ImageSharp/Formats/ImageDecoderUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,38 @@ internal static class ImageDecoderUtilities
/// 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(stream, DefaultLargeImageExceptionFactory, cancellationToken);
=> decoder.IdentifyAsync(configuration, stream, DefaultLargeImageExceptionFactory, cancellationToken);

/// <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 BufferedReadStream bufferedReadStream = new BufferedReadStream(stream);
using BufferedReadStream bufferedReadStream = new BufferedReadStream(configuration, stream);
IImageInfo imageInfo = decoder.Identify(bufferedReadStream, cancellationToken);
return Task.FromResult(imageInfo);
}
Expand All @@ -68,15 +72,18 @@ public static Task<IImageInfo> IdentifyAsync(
/// </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);
Expand All @@ -86,20 +93,22 @@ public static Task<Image<TPixel>> DecodeAsync<TPixel>(
/// </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 BufferedReadStream bufferedReadStream = new BufferedReadStream(stream);
using BufferedReadStream bufferedReadStream = new BufferedReadStream(configuration, stream);
Image<TPixel> image = decoder.Decode<TPixel>(bufferedReadStream, cancellationToken);
return Task.FromResult(image);
}
Expand All @@ -118,15 +127,16 @@ public static Task<Image<TPixel>> DecodeAsync<TPixel>(
}
}

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<InvalidMemoryOperationException, Size, InvalidImageContentException> largeImageExceptionFactory)
{
using BufferedReadStream bufferedReadStream = new BufferedReadStream(stream);
using BufferedReadStream bufferedReadStream = new BufferedReadStream(configuration, stream);

try
{
Expand All @@ -138,17 +148,18 @@ public static IImageInfo Identify(
}
}

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>
=> decoder.Decode<TPixel>(stream, DefaultLargeImageExceptionFactory);
=> decoder.Decode<TPixel>(configuration, stream, DefaultLargeImageExceptionFactory);

public static Image<TPixel> Decode<TPixel>(
this IImageDecoderInternals decoder,
Configuration configuration,
Stream stream,
Func<InvalidMemoryOperationException, Size, InvalidImageContentException> largeImageExceptionFactory)
where TPixel : unmanaged, IPixel<TPixel>
{
using BufferedReadStream bufferedReadStream = new BufferedReadStream(stream);
using BufferedReadStream bufferedReadStream = new BufferedReadStream(configuration, stream);

try
{
Expand Down
8 changes: 4 additions & 4 deletions src/ImageSharp/Formats/Jpeg/JpegDecoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public Image<TPixel> Decode<TPixel>(Configuration configuration, Stream stream)
Guard.NotNull(stream, nameof(stream));

using var decoder = new JpegDecoderCore(configuration, this);
return decoder.Decode<TPixel>(stream);
return decoder.Decode<TPixel>(configuration, stream);
}

/// <inheritdoc />
Expand All @@ -39,7 +39,7 @@ public Task<Image<TPixel>> DecodeAsync<TPixel>(Configuration configuration, Stre
Guard.NotNull(stream, nameof(stream));

using var decoder = new JpegDecoderCore(configuration, this);
return decoder.DecodeAsync<TPixel>(stream, cancellationToken);
return decoder.DecodeAsync<TPixel>(configuration, stream, cancellationToken);
}

/// <inheritdoc />
Expand All @@ -52,7 +52,7 @@ public IImageInfo Identify(Configuration configuration, Stream stream)
Guard.NotNull(stream, nameof(stream));

using var decoder = new JpegDecoderCore(configuration, this);
return decoder.Identify(stream);
return decoder.Identify(configuration, stream);
}

/// <inheritdoc/>
Expand All @@ -61,7 +61,7 @@ public Task<IImageInfo> IdentifyAsync(Configuration configuration, Stream stream
Guard.NotNull(stream, nameof(stream));

using var decoder = new JpegDecoderCore(configuration, this);
return decoder.IdentifyAsync(stream, cancellationToken);
return decoder.IdentifyAsync(configuration, stream, cancellationToken);
}
}
}
8 changes: 4 additions & 4 deletions src/ImageSharp/Formats/Png/PngDecoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public Image<TPixel> Decode<TPixel>(Configuration configuration, Stream stream)
where TPixel : unmanaged, IPixel<TPixel>
{
var decoder = new PngDecoderCore(configuration, this);
return decoder.Decode<TPixel>(stream);
return decoder.Decode<TPixel>(configuration, stream);
}

/// <inheritdoc />
Expand All @@ -34,7 +34,7 @@ public Task<Image<TPixel>> DecodeAsync<TPixel>(Configuration configuration, Stre
where TPixel : unmanaged, IPixel<TPixel>
{
var decoder = new PngDecoderCore(configuration, this);
return decoder.DecodeAsync<TPixel>(stream, cancellationToken);
return decoder.DecodeAsync<TPixel>(configuration, stream, cancellationToken);
}

/// <inheritdoc />
Expand All @@ -45,14 +45,14 @@ public async Task<Image> DecodeAsync(Configuration configuration, Stream stream,
public IImageInfo Identify(Configuration configuration, Stream stream)
{
var decoder = new PngDecoderCore(configuration, this);
return decoder.Identify(stream);
return decoder.Identify(configuration, stream);
}

/// <inheritdoc/>
public Task<IImageInfo> IdentifyAsync(Configuration configuration, Stream stream, CancellationToken cancellationToken)
{
var decoder = new PngDecoderCore(configuration, this);
return decoder.IdentifyAsync(stream, cancellationToken);
return decoder.IdentifyAsync(configuration, stream, cancellationToken);
}
}
}
2 changes: 1 addition & 1 deletion src/ImageSharp/Formats/Png/PngDecoderCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1028,7 +1028,7 @@ private void ReadInternationalTextChunk(PngMetadata metadata, ReadOnlySpan<byte>
private bool TryUncompressTextData(ReadOnlySpan<byte> 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))
Expand Down
8 changes: 4 additions & 4 deletions src/ImageSharp/Formats/Tga/TgaDecoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public Image<TPixel> Decode<TPixel>(Configuration configuration, Stream stream)
Guard.NotNull(stream, nameof(stream));

var decoder = new TgaDecoderCore(configuration, this);
return decoder.Decode<TPixel>(stream);
return decoder.Decode<TPixel>(configuration, stream);
}

/// <inheritdoc />
Expand All @@ -36,7 +36,7 @@ public Task<Image<TPixel>> DecodeAsync<TPixel>(Configuration configuration, Stre
Guard.NotNull(stream, nameof(stream));

var decoder = new TgaDecoderCore(configuration, this);
return decoder.DecodeAsync<TPixel>(stream, cancellationToken);
return decoder.DecodeAsync<TPixel>(configuration, stream, cancellationToken);
}

/// <inheritdoc />
Expand All @@ -48,15 +48,15 @@ public IImageInfo Identify(Configuration configuration, Stream stream)
{
Guard.NotNull(stream, nameof(stream));

return new TgaDecoderCore(configuration, this).Identify(stream);
return new TgaDecoderCore(configuration, this).Identify(configuration, stream);
}

/// <inheritdoc/>
public Task<IImageInfo> IdentifyAsync(Configuration configuration, Stream stream, CancellationToken cancellationToken)
{
Guard.NotNull(stream, nameof(stream));

return new TgaDecoderCore(configuration, this).IdentifyAsync(stream, cancellationToken);
return new TgaDecoderCore(configuration, this).IdentifyAsync(configuration, stream, cancellationToken);
}
}
}
2 changes: 1 addition & 1 deletion src/ImageSharp/Image.FromStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -891,7 +891,7 @@ private static async Task<T> WithSeekableStreamAsync<T>(
}

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);
Expand Down

0 comments on commit f31c8c1

Please sign in to comment.