|
|
@ -58,16 +58,8 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder |
|
|
Unsafe.CopyBlockUnaligned(ref this.Values[0], ref MemoryMarshal.GetReference(values), (uint)values.Length); |
|
|
Unsafe.CopyBlockUnaligned(ref this.Values[0], ref MemoryMarshal.GetReference(values), (uint)values.Length); |
|
|
|
|
|
|
|
|
// TODO: testing code
|
|
|
// TODO: testing code
|
|
|
Span<char> huffSize = new char[257]; |
|
|
Span<char> huffSize = stackalloc char[257]; |
|
|
Span<uint> huffCode = new uint[257]; |
|
|
Span<uint> huffCode = stackalloc uint[257]; |
|
|
|
|
|
|
|
|
fixed (void* maxCodePtr = this.MaxCode, valOffsetPtr = this.ValOffset, lookaheadSizePtr = this.LookaheadSize, lookaheadValuePtr = this.LookaheadValue) |
|
|
|
|
|
{ |
|
|
|
|
|
// TODO: testing spans
|
|
|
|
|
|
var maxCodeSpan = new Span<ulong>(maxCodePtr, 18); |
|
|
|
|
|
var valOffsetSpan = new Span<int>(valOffsetPtr, 19); |
|
|
|
|
|
var lookaheadSizeSpan = new Span<byte>(lookaheadSizePtr, JpegConstants.Huffman.LookupSize); |
|
|
|
|
|
var lookaheadValueSpan = new Span<byte>(lookaheadValuePtr, JpegConstants.Huffman.LookupSize); |
|
|
|
|
|
|
|
|
|
|
|
// Figure C.1: make table of Huffman code length for each symbol
|
|
|
// Figure C.1: make table of Huffman code length for each symbol
|
|
|
int p = 0; |
|
|
int p = 0; |
|
|
@ -115,27 +107,27 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder |
|
|
{ |
|
|
{ |
|
|
if (codeLengths[j] != 0) |
|
|
if (codeLengths[j] != 0) |
|
|
{ |
|
|
{ |
|
|
valOffsetSpan[j] = p - (int)huffCode[p]; |
|
|
this.ValOffset[j] = p - (int)huffCode[p]; |
|
|
p += codeLengths[j]; |
|
|
p += codeLengths[j]; |
|
|
maxCodeSpan[j] = huffCode[p - 1]; // Maximum code of length l
|
|
|
this.MaxCode[j] = huffCode[p - 1]; // Maximum code of length l
|
|
|
maxCodeSpan[j] <<= JpegConstants.Huffman.RegisterSize - j; // Left justify
|
|
|
this.MaxCode[j] <<= JpegConstants.Huffman.RegisterSize - j; // Left justify
|
|
|
maxCodeSpan[j] |= (1ul << (JpegConstants.Huffman.RegisterSize - j)) - 1; |
|
|
this.MaxCode[j] |= (1ul << (JpegConstants.Huffman.RegisterSize - j)) - 1; |
|
|
} |
|
|
} |
|
|
else |
|
|
else |
|
|
{ |
|
|
{ |
|
|
maxCodeSpan[j] = 0; |
|
|
this.MaxCode[j] = 0; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
valOffsetSpan[18] = 0; |
|
|
this.ValOffset[18] = 0; |
|
|
maxCodeSpan[17] = ulong.MaxValue; // Ensures huff decode terminates
|
|
|
this.MaxCode[17] = ulong.MaxValue; // Ensures huff decode terminates
|
|
|
|
|
|
|
|
|
// Compute lookahead tables to speed up decoding.
|
|
|
// Compute lookahead tables to speed up decoding.
|
|
|
// First we set all the table entries to JpegConstants.Huffman.SlowBits, indicating "too long";
|
|
|
// First we set all the table entries to JpegConstants.Huffman.SlowBits, indicating "too long";
|
|
|
// then we iterate through the Huffman codes that are short enough and
|
|
|
// then we iterate through the Huffman codes that are short enough and
|
|
|
// fill in all the entries that correspond to bit sequences starting
|
|
|
// fill in all the entries that correspond to bit sequences starting
|
|
|
// with that code.
|
|
|
// with that code.
|
|
|
ref byte lookupSizeRef = ref lookaheadSizeSpan[0]; |
|
|
ref byte lookupSizeRef = ref this.LookaheadSize[0]; |
|
|
Unsafe.InitBlockUnaligned(ref lookupSizeRef, JpegConstants.Huffman.SlowBits, JpegConstants.Huffman.LookupSize); |
|
|
Unsafe.InitBlockUnaligned(ref lookupSizeRef, JpegConstants.Huffman.SlowBits, JpegConstants.Huffman.LookupSize); |
|
|
|
|
|
|
|
|
p = 0; |
|
|
p = 0; |
|
|
@ -149,8 +141,8 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder |
|
|
int lookBits = (int)(huffCode[p] << jShift); |
|
|
int lookBits = (int)(huffCode[p] << jShift); |
|
|
for (int ctr = 1 << (JpegConstants.Huffman.LookupBits - length); ctr > 0; ctr--) |
|
|
for (int ctr = 1 << (JpegConstants.Huffman.LookupBits - length); ctr > 0; ctr--) |
|
|
{ |
|
|
{ |
|
|
lookaheadSizeSpan[lookBits] = (byte)length; |
|
|
this.LookaheadSize[lookBits] = (byte)length; |
|
|
lookaheadValueSpan[lookBits] = this.Values[p]; |
|
|
this.LookaheadValue[lookBits] = this.Values[p]; |
|
|
lookBits++; |
|
|
lookBits++; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
@ -158,4 +150,3 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|