2020-08-18 22:01:00 +00:00
|
|
|
extends Reference
|
2020-08-07 05:13:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
class LSB_LZWBitPacker:
|
|
|
|
var bit_index: int = 0
|
2020-08-08 21:16:03 +00:00
|
|
|
var stream: int = 0
|
2020-08-07 05:13:04 +00:00
|
|
|
|
|
|
|
var chunks: PoolByteArray = PoolByteArray([])
|
|
|
|
|
|
|
|
func put_byte():
|
2020-08-08 21:16:03 +00:00
|
|
|
chunks.append(stream & 0xff)
|
|
|
|
bit_index -= 8
|
|
|
|
stream >>= 8
|
2020-08-07 05:13:04 +00:00
|
|
|
|
|
|
|
func write_bits(value: int, bits_count: int) -> void:
|
2020-08-08 21:16:03 +00:00
|
|
|
value &= (1 << bits_count) - 1
|
|
|
|
value <<= bit_index
|
|
|
|
stream |= value
|
|
|
|
bit_index += bits_count
|
|
|
|
while bit_index >= 8:
|
|
|
|
self.put_byte()
|
2020-08-07 05:13:04 +00:00
|
|
|
|
|
|
|
func pack() -> PoolByteArray:
|
|
|
|
if bit_index != 0:
|
|
|
|
self.put_byte()
|
|
|
|
return chunks
|
|
|
|
|
|
|
|
func reset() -> void:
|
|
|
|
bit_index = 0
|
2020-08-08 21:16:03 +00:00
|
|
|
stream = 0
|
2020-08-07 05:13:04 +00:00
|
|
|
chunks = PoolByteArray([])
|