Browse Source

Fixes to make all unit tests pass

- Also fix some formatting warnings
af/merge-core
Curtis Wensley 7 years ago
parent
commit
4979e1971f
  1. 23
      src/ImageSharp/Formats/Png/PngDecoderCore.cs
  2. 44
      src/ImageSharp/Formats/Png/Zlib/ZlibInflateStream.cs

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

@ -252,9 +252,10 @@ namespace SixLabors.ImageSharp.Formats.Png
using (var deframeStream = new ZlibInflateStream(this.currentStream, this.ReadNextDataChunk)) 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);
} }
break; break;
case PngChunkType.Palette: case PngChunkType.Palette:
byte[] pal = new byte[chunk.Length]; byte[] pal = new byte[chunk.Length];
@ -1376,16 +1377,23 @@ namespace SixLabors.ImageSharp.Formats.Png
/// <returns>Count of bytes in the next data chunk, or 0 if there are no more data chunks left.</returns> /// <returns>Count of bytes in the next data chunk, or 0 if there are no more data chunks left.</returns>
private int ReadNextDataChunk() private int ReadNextDataChunk()
{ {
this.currentStream.Read(this.crcBuffer, 0, 4); if (this.nextChunk != null)
{
return 0;
}
this.TryReadChunk(out PngChunk chunk); this.currentStream.Read(this.crcBuffer, 0, 4);
if (chunk.Type == PngChunkType.Data) if (this.TryReadChunk(out PngChunk chunk))
{ {
return chunk.Length; if (chunk.Type == PngChunkType.Data)
{
return chunk.Length;
}
this.nextChunk = chunk;
} }
this.nextChunk = chunk;
return 0; return 0;
} }
@ -1401,6 +1409,9 @@ namespace SixLabors.ImageSharp.Formats.Png
if (this.nextChunk != null) if (this.nextChunk != null)
{ {
chunk = this.nextChunk.Value; chunk = this.nextChunk.Value;
this.nextChunk = null;
return true; return true;
} }

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

@ -42,11 +42,6 @@ namespace SixLabors.ImageSharp.Formats.Png.Zlib
/// </remarks> /// </remarks>
private bool isDisposed; private bool isDisposed;
/// <summary>
/// Whether the crc value has been read.
/// </summary>
private bool crcRead;
/// <summary> /// <summary>
/// The current data remaining to be read /// The current data remaining to be read
/// </summary> /// </summary>
@ -119,17 +114,36 @@ namespace SixLabors.ImageSharp.Formats.Png.Zlib
{ {
if (this.currentDataRemaining == 0) if (this.currentDataRemaining == 0)
{ {
this.currentDataRemaining = this.getData(); // last buffer was read in its entirety, let's make sure we don't actually have more
this.currentDataRemaining = this.getData();
if (this.currentDataRemaining == 0) if (this.currentDataRemaining == 0)
{ {
return 0; return 0;
} }
} }
int bytesToRead = Math.Min(count, this.currentDataRemaining); int bytesToRead = Math.Min(count, this.currentDataRemaining);
this.currentDataRemaining -= bytesToRead; this.currentDataRemaining -= bytesToRead;
return this.innerStream.Read(buffer, offset, bytesToRead); int bytesRead = this.innerStream.Read(buffer, offset, bytesToRead);
// keep reading data until we've reached the end of the stream or filled the buffer
while (this.currentDataRemaining == 0 && bytesRead < count)
{
this.currentDataRemaining = this.getData();
if (this.currentDataRemaining == 0)
{
return bytesRead;
}
offset += bytesRead;
bytesToRead = Math.Min(count - bytesRead, this.currentDataRemaining);
this.currentDataRemaining -= bytesToRead;
bytesRead += this.innerStream.Read(buffer, offset, bytesToRead);
}
return bytesRead;
} }
/// <inheritdoc/> /// <inheritdoc/>
@ -165,14 +179,6 @@ namespace SixLabors.ImageSharp.Formats.Png.Zlib
{ {
this.compressedStream.Dispose(); this.compressedStream.Dispose();
this.compressedStream = null; this.compressedStream = null;
if (!this.crcRead)
{
// Consume the trailing 4 bytes
this.innerStream.Read(ChecksumBuffer, 0, 4);
this.currentDataRemaining -= 4;
this.crcRead = true;
}
} }
} }

Loading…
Cancel
Save