Browse Source

Use memory stream for uncompressed data instead of a list

pull/2110/head
Brian Popow 4 years ago
parent
commit
a0e38c87b0
  1. 16
      src/ImageSharp/Formats/Png/PngDecoderCore.cs

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

@ -1223,26 +1223,26 @@ namespace SixLabors.ImageSharp.Formats.Png
fixed (byte* compressedDataBase = compressedData) fixed (byte* compressedDataBase = compressedData)
{ {
using (IMemoryOwner<byte> destBuffer = this.memoryAllocator.Allocate<byte>(this.Configuration.StreamProcessingBufferSize)) using (IMemoryOwner<byte> destBuffer = this.memoryAllocator.Allocate<byte>(this.Configuration.StreamProcessingBufferSize))
using (var memoryStream = new UnmanagedMemoryStream(compressedDataBase, compressedData.Length)) using (var memoryStreamOutput = new MemoryStream(compressedData.Length))
using (var bufferedStream = new BufferedReadStream(this.Configuration, memoryStream)) using (var memoryStreamInput = new UnmanagedMemoryStream(compressedDataBase, compressedData.Length))
using (var bufferedStream = new BufferedReadStream(this.Configuration, memoryStreamInput))
using (var inflateStream = new ZlibInflateStream(bufferedStream)) using (var inflateStream = new ZlibInflateStream(bufferedStream))
{ {
Span<byte> dest = destBuffer.GetSpan(); Span<byte> destUncompressedData = destBuffer.GetSpan();
if (!inflateStream.AllocateNewBytes(compressedData.Length, false)) if (!inflateStream.AllocateNewBytes(compressedData.Length, false))
{ {
uncompressedBytesArray = Array.Empty<byte>(); uncompressedBytesArray = Array.Empty<byte>();
return false; return false;
} }
var uncompressedBytes = new List<byte>(compressedData.Length); int bytesRead = inflateStream.CompressedStream.Read(destUncompressedData, 0, destUncompressedData.Length);
int bytesRead = inflateStream.CompressedStream.Read(dest, 0, dest.Length);
while (bytesRead != 0) while (bytesRead != 0)
{ {
uncompressedBytes.AddRange(dest.Slice(0, bytesRead).ToArray()); memoryStreamOutput.Write(destUncompressedData.Slice(0, bytesRead));
bytesRead = inflateStream.CompressedStream.Read(dest, 0, dest.Length); bytesRead = inflateStream.CompressedStream.Read(destUncompressedData, 0, destUncompressedData.Length);
} }
uncompressedBytesArray = uncompressedBytes.ToArray(); uncompressedBytesArray = memoryStreamOutput.ToArray();
return true; return true;
} }
} }

Loading…
Cancel
Save