summaryrefslogtreecommitdiff
path: root/src/addons/GifMaker/GifDecoder.gd
blob: 08ccaa91c6df913e2f899fdf4e5351faa24c484a (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
42
43
44
extends Node
class_name GifDecoder

func decode_file(path):
	var file = File.new()
	file.open(path, File.READ)
	var bytes = file.get_buffer(file.get_len())
	file.close()

	return decode_data(bytes)

func decode_block(block: PoolByteArray, start, end):
	var string = block.subarray(start, end).get_string_from_utf8()
	if string.begins_with('GIF_MAKER::'):
		return str2var(string.substr('GIF_MAKER::'.length(), string.length()))
	return string

func decode_data(bytes: PoolByteArray):
	var index = 0
	var start = -1
	var end = -1
	var data = []

	for byte in bytes:
		if start != -1 \
				and byte == 0x21:
			end = index - 1

			data.append(decode_block(bytes, start, end))
			start = -1
			end = -1

		if byte == 0x21 \
				and index < bytes.size() \
				and bytes[index + 1] == 0xFE:
			start = index + 2

		index += 1

	if end == -1 and start != -1:
		end = bytes.size() - 1
		data.append(decode_block(bytes, start, end))

	return data