This commit is contained in:
Ryan Oldenburg 2022-05-12 22:55:35 -05:00
parent 6cfb98553c
commit 2991c16c9d

View file

@ -383,25 +383,23 @@ proc decodeSOS(state: var DecoderState) =
proc fillBitBuffer(state: var DecoderState) = proc fillBitBuffer(state: var DecoderState) =
## When we are low on bits, we need to call this to populate some more. ## When we are low on bits, we need to call this to populate some more.
while true: while state.bitsBuffered < 24:
let b = if state.hitEnd: let b =
if state.hitEnd:
0.uint32 0.uint32
else: else:
state.readUint8().uint32 state.readUint8().uint32
if b == 0xFF: if b == 0xFF:
var c = state.readUint8() var c = state.readUint8()
while c == 0xFF: c = state.readUint8() while c == 0xFF:
c = state.readUint8()
if c != 0: if c != 0:
dec state.pos state.pos -= 2
dec state.pos
state.hitEnd = true state.hitEnd = true
return return
state.bitBuffer = state.bitBuffer or (b shl (24 - state.bitsBuffered)) state.bitBuffer = state.bitBuffer or (b shl (24 - state.bitsBuffered))
state.bitsBuffered += 8 state.bitsBuffered += 8
if not(state.bitsBuffered <= 24):
break
proc huffmanDecode(state: var DecoderState, tableCurrent, table: int): uint8 = proc huffmanDecode(state: var DecoderState, tableCurrent, table: int): uint8 =
## Decode a uint8 from the huffman table. ## Decode a uint8 from the huffman table.
if state.bitsBuffered < 16: if state.bitsBuffered < 16: