Browse Source

Fix issue on mono <= 5.14 when reading multiple png data chunks.

Fixes #598
af/merge-core
Curtis Wensley 8 years ago
parent
commit
061436f765
  1. 139
      src/ImageSharp/Formats/Png/PngDecoderCore.cs
  2. 16
      src/ImageSharp/Formats/Png/Zlib/ZlibInflateStream.cs

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

@ -187,6 +187,11 @@ namespace SixLabors.ImageSharp.Formats.Png
/// </summary> /// </summary>
private bool hasTrans; private bool hasTrans;
/// <summary>
/// The next chunk of data to return
/// </summary>
private PngChunk? nextChunk;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="PngDecoderCore"/> class. /// Initializes a new instance of the <see cref="PngDecoderCore"/> class.
/// </summary> /// </summary>
@ -223,67 +228,66 @@ namespace SixLabors.ImageSharp.Formats.Png
Image<TPixel> image = null; Image<TPixel> image = null;
try try
{ {
using (var deframeStream = new ZlibInflateStream(this.currentStream)) while (!this.isEndChunkReached && this.TryReadChunk(out PngChunk chunk))
{ {
while (!this.isEndChunkReached && this.TryReadChunk(out PngChunk chunk)) try
{ {
try switch (chunk.Type)
{ {
switch (chunk.Type) case PngChunkType.Header:
{ this.ReadHeaderChunk(pngMetaData, chunk.Data.Array);
case PngChunkType.Header: this.ValidateHeader();
this.ReadHeaderChunk(pngMetaData, chunk.Data.Array); break;
this.ValidateHeader(); case PngChunkType.Physical:
break; this.ReadPhysicalChunk(metaData, chunk.Data.GetSpan());
case PngChunkType.Physical: break;
this.ReadPhysicalChunk(metaData, chunk.Data.GetSpan()); case PngChunkType.Gamma:
break; this.ReadGammaChunk(pngMetaData, chunk.Data.GetSpan());
case PngChunkType.Gamma: break;
this.ReadGammaChunk(pngMetaData, chunk.Data.GetSpan()); case PngChunkType.Data:
break; if (image is null)
case PngChunkType.Data: {
if (image is null) this.InitializeImage(metaData, out image);
{ }
this.InitializeImage(metaData, out image);
} using (var deframeStream = new ZlibInflateStream(this.currentStream, this.ReadNextDataChunk))
{
deframeStream.AllocateNewBytes(chunk.Length); deframeStream.AllocateNewBytes(chunk.Length);
this.ReadScanlines(deframeStream.CompressedStream, image.Frames.RootFrame); this.ReadScanlines(deframeStream.CompressedStream, image.Frames.RootFrame);
this.currentStream.Read(this.crcBuffer, 0, 4); }
break; break;
case PngChunkType.Palette: case PngChunkType.Palette:
byte[] pal = new byte[chunk.Length]; byte[] pal = new byte[chunk.Length];
Buffer.BlockCopy(chunk.Data.Array, 0, pal, 0, chunk.Length); Buffer.BlockCopy(chunk.Data.Array, 0, pal, 0, chunk.Length);
this.palette = pal; this.palette = pal;
break; break;
case PngChunkType.PaletteAlpha: case PngChunkType.PaletteAlpha:
byte[] alpha = new byte[chunk.Length]; byte[] alpha = new byte[chunk.Length];
Buffer.BlockCopy(chunk.Data.Array, 0, alpha, 0, chunk.Length); Buffer.BlockCopy(chunk.Data.Array, 0, alpha, 0, chunk.Length);
this.paletteAlpha = alpha; this.paletteAlpha = alpha;
this.AssignTransparentMarkers(alpha); this.AssignTransparentMarkers(alpha);
break; break;
case PngChunkType.Text: case PngChunkType.Text:
this.ReadTextChunk(metaData, chunk.Data.Array, chunk.Length); this.ReadTextChunk(metaData, chunk.Data.Array, chunk.Length);
break; break;
case PngChunkType.Exif: case PngChunkType.Exif:
if (!this.ignoreMetadata) if (!this.ignoreMetadata)
{ {
byte[] exifData = new byte[chunk.Length]; byte[] exifData = new byte[chunk.Length];
Buffer.BlockCopy(chunk.Data.Array, 0, exifData, 0, chunk.Length); Buffer.BlockCopy(chunk.Data.Array, 0, exifData, 0, chunk.Length);
metaData.ExifProfile = new ExifProfile(exifData); metaData.ExifProfile = new ExifProfile(exifData);
} }
break; break;
case PngChunkType.End: case PngChunkType.End:
this.isEndChunkReached = true; this.isEndChunkReached = true;
break; break;
}
}
finally
{
chunk.Data?.Dispose(); // Data is rented in ReadChunkData()
} }
} }
finally
{
chunk.Data?.Dispose(); // Data is rented in ReadChunkData()
}
} }
if (image is null) if (image is null)
@ -1366,6 +1370,25 @@ namespace SixLabors.ImageSharp.Formats.Png
metadata.Properties.Add(new ImageProperty(name, value)); metadata.Properties.Add(new ImageProperty(name, value));
} }
/// <summary>
/// Reads the next data chunk.
/// </summary>
/// <returns>Count of bytes in the next data chunk, or 0 if there are no more data chunks left.</returns>
private int ReadNextDataChunk()
{
this.currentStream.Read(this.crcBuffer, 0, 4);
this.TryReadChunk(out PngChunk chunk);
if (chunk.Type == PngChunkType.Data)
{
return chunk.Length;
}
this.nextChunk = chunk;
return 0;
}
/// <summary> /// <summary>
/// Reads a chunk from the stream. /// Reads a chunk from the stream.
/// </summary> /// </summary>
@ -1375,6 +1398,12 @@ namespace SixLabors.ImageSharp.Formats.Png
/// </returns> /// </returns>
private bool TryReadChunk(out PngChunk chunk) private bool TryReadChunk(out PngChunk chunk)
{ {
if (this.nextChunk != null)
{
chunk = this.nextChunk.Value;
return true;
}
int length = this.ReadChunkLength(); int length = this.ReadChunkLength();
if (length == -1) if (length == -1)

16
src/ImageSharp/Formats/Png/Zlib/ZlibInflateStream.cs

@ -52,13 +52,20 @@ namespace SixLabors.ImageSharp.Formats.Png.Zlib
/// </summary> /// </summary>
private int currentDataRemaining; private int currentDataRemaining;
/// <summary>
/// Delegate to get more data once we've exhausted the current data remaining
/// </summary>
private Func<int> getData;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="ZlibInflateStream"/> class. /// Initializes a new instance of the <see cref="ZlibInflateStream"/> class.
/// </summary> /// </summary>
/// <param name="innerStream">The inner raw stream</param> /// <param name="innerStream">The inner raw stream</param>
public ZlibInflateStream(Stream innerStream) /// <param name="getData">A delegate to get more data from the inner stream</param>
public ZlibInflateStream(Stream innerStream, Func<int> getData)
{ {
this.innerStream = innerStream; this.innerStream = innerStream;
this.getData = getData;
} }
/// <inheritdoc/> /// <inheritdoc/>
@ -112,7 +119,12 @@ namespace SixLabors.ImageSharp.Formats.Png.Zlib
{ {
if (this.currentDataRemaining == 0) if (this.currentDataRemaining == 0)
{ {
return 0; this.currentDataRemaining = this.getData();
if (this.currentDataRemaining == 0)
{
return 0;
}
} }
int bytesToRead = Math.Min(count, this.currentDataRemaining); int bytesToRead = Math.Min(count, this.currentDataRemaining);

Loading…
Cancel
Save