-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHuffmanTable.py
More file actions
48 lines (44 loc) · 1.47 KB
/
HuffmanTable.py
File metadata and controls
48 lines (44 loc) · 1.47 KB
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
45
46
47
48
class HuffmanTable:
def __init__(self, stream):
self.tables, self.code_lengths = decode_huffman_table(stream)
def get_table(self):
return self.tables
def get_code_lengths(self):
return self.code_lengths
def decode_huffman_table(stream):
tables = {}
all_code_lengths = {}
# remove ht len
#length = stream.pop(0) | stream.pop(0)
curr_table = 0
curr_code_len = 0
while stream:
#stream.pop(0) #remove table type
length = stream.pop(0) | stream.pop(0)
stream.pop(0) #remove table type
# save first 16 code lengths, look into uint 16
code_lengths = []
code_length_count = 0
for i in range(16): # max of 16 code lengths
value = stream.pop(0)
code_lengths.append(value)
code_length_count += value
all_code_lengths[curr_code_len] = code_lengths
curr_code_len += 1
# save the code Values
values = [stream.pop(0) for i in range(code_length_count)]
# map bits to values
mappings = {}
mapping_value = 0
itr = 0
for value in code_lengths:
for j in range(value):
mappings[mapping_value] = values[itr]
itr += 1
mapping_value += 1
mapping_value <<= 1
tables[curr_table] = mappings
curr_table += 1
#if stream:
#stream.pop(0) # remove ht type
return tables, all_code_lengths