summaryrefslogtreecommitdiff
path: root/src/addons/GifMaker/godot-gdgifexporter/gdgifexporter/gif-lzw/lsbbitunpacker.gd
blob: 9f8507f2db8d8032349c407720ae2ea57c29b895 (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
32
33
34
35
36
37
38
39
40
41
extends Reference


class LSBLZWBitUnpacker:
	var chunk_stream: PoolByteArray
	var bit_index: int = 0
	var byte: int
	var byte_index: int = 0

	func _init(_chunk_stream: PoolByteArray):
		chunk_stream = _chunk_stream
		self.get_byte()

	func get_bit(value: int, index: int) -> int:
		return (value >> index) & 1

	func set_bit(value: int, index: int) -> int:
		return value | (1 << index)

	func get_byte():
		byte = chunk_stream[byte_index]
		byte_index += 1
		bit_index = 0

	func read_bits(bits_count: int) -> int:
		var result: int = 0
		var result_bit_index: int = 0

		for _i in range(bits_count):
			if self.get_bit(byte, bit_index) == 1:
				result = self.set_bit(result, result_bit_index)
			result_bit_index += 1
			bit_index += 1

			if bit_index == 8:
				self.get_byte()

		return result

	func remove_bits(bits_count: int) -> void:
		self.read_bits(bits_count)