summaryrefslogtreecommitdiff
path: root/src/addons/GifMaker/godot-gdgifexporter/gdgifexporter/gif-lzw/lsbbitpacker.gd
blob: 3141d091409f7ebb3be4c7ba327e26e38db9e293 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
extends Reference


class LSBLZWBitPacker:
	var bit_index: int = 0
	var stream: int = 0

	var chunks: PoolByteArray = PoolByteArray([])

	func put_byte():
		chunks.append(stream & 0xff)
		bit_index -= 8
		stream >>= 8

	func write_bits(value: int, bits_count: int) -> void:
		value &= (1 << bits_count) - 1
		value <<= bit_index
		stream |= value
		bit_index += bits_count
		while bit_index >= 8:
			self.put_byte()

	func pack() -> PoolByteArray:
		if bit_index != 0:
			self.put_byte()
		return chunks

	func reset() -> void:
		bit_index = 0
		stream = 0
		chunks = PoolByteArray([])