|
|
|
@ -2,6 +2,7 @@ |
|
|
|
// Licensed under the Apache License, Version 2.0.
|
|
|
|
|
|
|
|
using System; |
|
|
|
using System.Buffers; |
|
|
|
using System.Buffers.Binary; |
|
|
|
using System.Runtime.CompilerServices; |
|
|
|
using System.Runtime.InteropServices; |
|
|
|
@ -928,9 +929,10 @@ namespace SixLabors.ImageSharp.Formats.Jpeg |
|
|
|
{ |
|
|
|
int length = remaining; |
|
|
|
|
|
|
|
using (IManagedByteBuffer huffmanData = this.Configuration.MemoryAllocator.AllocateManagedByteBuffer(256, AllocationOptions.Clean)) |
|
|
|
using (IMemoryOwner<byte> huffmanData = this.Configuration.MemoryAllocator.Allocate<byte>(256, AllocationOptions.Clean)) |
|
|
|
{ |
|
|
|
ref byte huffmanDataRef = ref MemoryMarshal.GetReference(huffmanData.GetSpan()); |
|
|
|
Span<byte> huffmanDataSpan = huffmanData.GetSpan(); |
|
|
|
ref byte huffmanDataRef = ref MemoryMarshal.GetReference(huffmanDataSpan); |
|
|
|
for (int i = 2; i < remaining;) |
|
|
|
{ |
|
|
|
byte huffmanTableSpec = (byte)stream.ReadByte(); |
|
|
|
@ -949,11 +951,12 @@ namespace SixLabors.ImageSharp.Formats.Jpeg |
|
|
|
JpegThrowHelper.ThrowInvalidImageContentException("Bad Huffman Table index."); |
|
|
|
} |
|
|
|
|
|
|
|
stream.Read(huffmanData.Array, 0, 16); |
|
|
|
stream.Read(huffmanDataSpan, 0, 16); |
|
|
|
|
|
|
|
using (IManagedByteBuffer codeLengths = this.Configuration.MemoryAllocator.AllocateManagedByteBuffer(17, AllocationOptions.Clean)) |
|
|
|
using (IMemoryOwner<byte> codeLengths = this.Configuration.MemoryAllocator.Allocate<byte>(17, AllocationOptions.Clean)) |
|
|
|
{ |
|
|
|
ref byte codeLengthsRef = ref MemoryMarshal.GetReference(codeLengths.GetSpan()); |
|
|
|
Span<byte> codeLengthsSpan = codeLengths.GetSpan(); |
|
|
|
ref byte codeLengthsRef = ref MemoryMarshal.GetReference(codeLengthsSpan); |
|
|
|
int codeLengthSum = 0; |
|
|
|
|
|
|
|
for (int j = 1; j < 17; j++) |
|
|
|
@ -968,17 +971,18 @@ namespace SixLabors.ImageSharp.Formats.Jpeg |
|
|
|
JpegThrowHelper.ThrowInvalidImageContentException("Huffman table has excessive length."); |
|
|
|
} |
|
|
|
|
|
|
|
using (IManagedByteBuffer huffmanValues = this.Configuration.MemoryAllocator.AllocateManagedByteBuffer(256, AllocationOptions.Clean)) |
|
|
|
using (IMemoryOwner<byte> huffmanValues = this.Configuration.MemoryAllocator.Allocate<byte>(256, AllocationOptions.Clean)) |
|
|
|
{ |
|
|
|
stream.Read(huffmanValues.Array, 0, codeLengthSum); |
|
|
|
Span<byte> huffmanValuesSpan = huffmanValues.GetSpan(); |
|
|
|
stream.Read(huffmanValuesSpan, 0, codeLengthSum); |
|
|
|
|
|
|
|
i += 17 + codeLengthSum; |
|
|
|
|
|
|
|
this.BuildHuffmanTable( |
|
|
|
tableType == 0 ? this.dcHuffmanTables : this.acHuffmanTables, |
|
|
|
tableIndex, |
|
|
|
codeLengths.GetSpan(), |
|
|
|
huffmanValues.GetSpan()); |
|
|
|
codeLengthsSpan, |
|
|
|
huffmanValuesSpan); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|