Browse Source

Add tests for discontiguous buffers for png

af/octree-no-pixelmap
Brian Popow 6 years ago
parent
commit
ba4ef9df80
  1. 15
      src/ImageSharp/Formats/Png/PngDecoder.cs
  2. 8
      src/ImageSharp/Formats/Png/PngDecoderCore.cs

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

@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0.
using System.IO;
using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
namespace SixLabors.ImageSharp.Formats.Png
@ -44,7 +45,19 @@ namespace SixLabors.ImageSharp.Formats.Png
where TPixel : struct, IPixel<TPixel>
{
var decoder = new PngDecoderCore(configuration, this);
return decoder.Decode<TPixel>(stream);
try
{
return decoder.Decode<TPixel>(stream);
}
catch (InvalidMemoryOperationException ex)
{
Size dims = decoder.Dimensions;
// TODO: use InvalidImageContentException here, if we decide to define it
// https://github.com/SixLabors/ImageSharp/issues/1110
throw new ImageFormatException($"Can not decode image. Failed to allocate buffers for possibly degenerate dimensions: {dims.Width}x{dims.Height}.", ex);
}
}
/// <inheritdoc/>

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

@ -106,7 +106,7 @@ namespace SixLabors.ImageSharp.Formats.Png
private int currentRow = Adam7.FirstRow[0];
/// <summary>
/// The current number of bytes read in the current scanline
/// The current number of bytes read in the current scanline.
/// </summary>
private int currentRowBytesRead;
@ -132,18 +132,20 @@ namespace SixLabors.ImageSharp.Formats.Png
this.ignoreMetadata = options.IgnoreMetadata;
}
public Size Dimensions => new Size(this.header.Width, this.header.Height);
/// <summary>
/// Decodes the stream to the image.
/// </summary>
/// <typeparam name="TPixel">The pixel format.</typeparam>
/// <param name="stream">The stream containing image data. </param>
/// <param name="stream">The stream containing image data.</param>
/// <exception cref="ImageFormatException">
/// Thrown if the stream does not contain and end chunk.
/// </exception>
/// <exception cref="ArgumentOutOfRangeException">
/// Thrown if the image is larger than the maximum allowable size.
/// </exception>
/// <returns>The decoded image</returns>
/// <returns>The decoded image.</returns>
public Image<TPixel> Decode<TPixel>(Stream stream)
where TPixel : struct, IPixel<TPixel>
{

Loading…
Cancel
Save