Browse Source

Clean up scanline decoding code.

af/merge-core
James Jackson-South 7 years ago
parent
commit
e89c1ef3d4
  1. 38
      src/ImageSharp/Formats/Png/PngDecoderCore.cs
  2. 40
      src/ImageSharp/Formats/Png/Zlib/ZlibInflateStream.cs

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

@ -5,6 +5,7 @@ using System;
using System.Buffers.Binary;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
@ -175,18 +176,7 @@ namespace SixLabors.ImageSharp.Formats.Png
this.InitializeImage(metadata, out image);
}
var deframeStream = new ZlibInflateStream(this.currentStream, this.ReadNextDataChunk);
try
{
deframeStream.AllocateNewBytes(chunk.Length, true);
this.ReadScanlines(deframeStream.CompressedStream, image.Frames.RootFrame, pngMetadata);
}
finally
{
// If an invalid Zlib stream is discovered the decoder will throw an exception
// due to the critical nature of the data chunk.
deframeStream.Dispose();
}
this.ReadScanlines(chunk, image.Frames.RootFrame, pngMetadata);
break;
case PngChunkType.Palette:
@ -472,19 +462,25 @@ namespace SixLabors.ImageSharp.Formats.Png
/// Reads the scanlines within the image.
/// </summary>
/// <typeparam name="TPixel">The pixel format.</typeparam>
/// <param name="dataStream">The <see cref="MemoryStream"/> containing data.</param>
/// <param name="chunk">The png chunk containing the compressed scanline data.</param>
/// <param name="image"> The pixel data.</param>
/// <param name="pngMetadata">The png metadata</param>
private void ReadScanlines<TPixel>(Stream dataStream, ImageFrame<TPixel> image, PngMetadata pngMetadata)
private void ReadScanlines<TPixel>(PngChunk chunk, ImageFrame<TPixel> image, PngMetadata pngMetadata)
where TPixel : struct, IPixel<TPixel>
{
if (this.header.InterlaceMethod == PngInterlaceMode.Adam7)
{
this.DecodeInterlacedPixelData(dataStream, image, pngMetadata);
}
else
using (var deframeStream = new ZlibInflateStream(this.currentStream, this.ReadNextDataChunk))
{
this.DecodePixelData(dataStream, image, pngMetadata);
deframeStream.AllocateNewBytes(chunk.Length, true);
DeflateStream dataStream = deframeStream.CompressedStream;
if (this.header.InterlaceMethod == PngInterlaceMode.Adam7)
{
this.DecodeInterlacedPixelData(dataStream, image, pngMetadata);
}
else
{
this.DecodePixelData(dataStream, image, pngMetadata);
}
}
}
@ -1021,7 +1017,7 @@ namespace SixLabors.ImageSharp.Formats.Png
private bool TryUncompressTextData(ReadOnlySpan<byte> compressedData, Encoding encoding, out string value)
{
using (var memoryStream = new MemoryStream(compressedData.ToArray()))
using (var inflateStream = new ZlibInflateStream(memoryStream, () => 0))
using (var inflateStream = new ZlibInflateStream(memoryStream))
{
if (!inflateStream.AllocateNewBytes(compressedData.Length, false))
{

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

@ -20,14 +20,14 @@ namespace SixLabors.ImageSharp.Formats.Png.Zlib
private static readonly byte[] ChecksumBuffer = new byte[4];
/// <summary>
/// The inner raw memory stream
/// A default delegate to get more data from the inner stream.
/// </summary>
private readonly Stream innerStream;
private static readonly Func<int> GetDataNoOp = () => 0;
/// <summary>
/// The compressed stream sitting over the top of the deframer
/// The inner raw memory stream
/// </summary>
private DeflateStream compressedStream;
private readonly Stream innerStream;
/// <summary>
/// A value indicating whether this instance of the given entity has been disposed.
@ -55,8 +55,17 @@ namespace SixLabors.ImageSharp.Formats.Png.Zlib
/// <summary>
/// Initializes a new instance of the <see cref="ZlibInflateStream"/> class.
/// </summary>
/// <param name="innerStream">The inner raw stream</param>
/// <param name="getData">A delegate to get more data from the inner stream</param>
/// <param name="innerStream">The inner raw stream.</param>
public ZlibInflateStream(Stream innerStream)
: this(innerStream, GetDataNoOp)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ZlibInflateStream"/> class.
/// </summary>
/// <param name="innerStream">The inner raw stream.</param>
/// <param name="getData">A delegate to get more data from the inner stream.</param>
public ZlibInflateStream(Stream innerStream, Func<int> getData)
{
this.innerStream = innerStream;
@ -76,12 +85,12 @@ namespace SixLabors.ImageSharp.Formats.Png.Zlib
public override long Length => throw new NotSupportedException();
/// <inheritdoc/>
public override long Position { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
public override long Position { get => throw new NotSupportedException(); set => throw new NotSupportedException(); }
/// <summary>
/// Gets the compressed stream over the deframed inner stream
/// </summary>
public DeflateStream CompressedStream => this.compressedStream;
public DeflateStream CompressedStream { get; private set; }
/// <summary>
/// Adds new bytes from a frame found in the original stream
@ -92,7 +101,7 @@ namespace SixLabors.ImageSharp.Formats.Png.Zlib
public bool AllocateNewBytes(int bytes, bool isCriticalChunk)
{
this.currentDataRemaining = bytes;
if (this.compressedStream is null)
if (this.CompressedStream is null)
{
return this.InitializeInflateStream(isCriticalChunk);
}
@ -101,10 +110,7 @@ namespace SixLabors.ImageSharp.Formats.Png.Zlib
}
/// <inheritdoc/>
public override void Flush()
{
throw new NotSupportedException();
}
public override void Flush() => throw new NotSupportedException();
/// <inheritdoc/>
public override int ReadByte()
@ -186,10 +192,10 @@ namespace SixLabors.ImageSharp.Formats.Png.Zlib
if (disposing)
{
// dispose managed resources
if (this.compressedStream != null)
if (this.CompressedStream != null)
{
this.compressedStream.Dispose();
this.compressedStream = null;
this.CompressedStream.Dispose();
this.CompressedStream = null;
}
}
@ -264,7 +270,7 @@ namespace SixLabors.ImageSharp.Formats.Png.Zlib
}
// Initialize the deflate Stream.
this.compressedStream = new DeflateStream(this, CompressionMode.Decompress, true);
this.CompressedStream = new DeflateStream(this, CompressionMode.Decompress, true);
return true;
}

Loading…
Cancel
Save