Skip to content

Commit d43ec49

Browse files
committed
Use memory allocator for destination buffer for the uncomressed bytes
1 parent 2dd3598 commit d43ec49

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

src/ImageSharp/Formats/Png/PngDecoderCore.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1222,24 +1222,24 @@ private unsafe bool TryUncompressZlibData(ReadOnlySpan<byte> compressedData, out
12221222
{
12231223
fixed (byte* compressedDataBase = compressedData)
12241224
{
1225+
using (IMemoryOwner<byte> destBuffer = this.memoryAllocator.Allocate<byte>(this.Configuration.StreamProcessingBufferSize))
12251226
using (var memoryStream = new UnmanagedMemoryStream(compressedDataBase, compressedData.Length))
12261227
using (var bufferedStream = new BufferedReadStream(this.Configuration, memoryStream))
12271228
using (var inflateStream = new ZlibInflateStream(bufferedStream))
12281229
{
1230+
Span<byte> dest = destBuffer.GetSpan();
12291231
if (!inflateStream.AllocateNewBytes(compressedData.Length, false))
12301232
{
12311233
uncompressedBytesArray = Array.Empty<byte>();
12321234
return false;
12331235
}
12341236

12351237
var uncompressedBytes = new List<byte>(compressedData.Length);
1236-
1237-
// Note: this uses a buffer which is only 4 bytes long to read the stream, maybe allocating a larger buffer makes sense here.
1238-
int bytesRead = inflateStream.CompressedStream.Read(this.buffer, 0, this.buffer.Length);
1238+
int bytesRead = inflateStream.CompressedStream.Read(dest, 0, dest.Length);
12391239
while (bytesRead != 0)
12401240
{
1241-
uncompressedBytes.AddRange(this.buffer.AsSpan(0, bytesRead).ToArray());
1242-
bytesRead = inflateStream.CompressedStream.Read(this.buffer, 0, this.buffer.Length);
1241+
uncompressedBytes.AddRange(dest.Slice(0, bytesRead).ToArray());
1242+
bytesRead = inflateStream.CompressedStream.Read(dest, 0, dest.Length);
12431243
}
12441244

12451245
uncompressedBytesArray = uncompressedBytes.ToArray();

0 commit comments

Comments
 (0)