-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdecode.py
executable file
·64 lines (55 loc) · 1.76 KB
/
decode.py
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/bin/env python3
import struct;
def create_blank_frame(col = 0):
return [[col] * 320] * 240
def write_frame(i, frame):
img = open("out/" + str(i) + ".ppm", "w")
img.write("P1\n320 240\n")
img.write("\n".join([" ".join(map(str, i)) for i in frame]))
img.write("\n")
img.close()
def rle_decode(f, startwith):
frame = []
i = 0
color = startwith == 1 # Volontary inverted due to the image format
line = []
j = 0
while i < 320*240:
num, = struct.unpack(">B", f.read(1))
i += num
j += num
if (j >= 320):
line.extend([0 if color else 1] * (num - (j - 320)))
frame.append(line)
line = []
if (j - 320 != 0):
line.extend([0 if color else 1] * (j - 320))
j = 0
else:
line.extend([0 if color else 1] * num)
color = not color
return frame
def main():
encoded_file = open("tmp", "rb")
last_frame = create_blank_frame(0)
i = 0
while True:
i += 1
frame_type, = struct.unpack(">B", encoded_file.read(1))
if frame_type == 3:
last_frame = create_blank_frame(1) # Volontary inverted due to the image format
write_frame(i, last_frame)
elif frame_type == 4:
last_frame = create_blank_frame(0) # Volontary inverted due to the image format
write_frame(i, last_frame)
elif frame_type == 5:
write_frame(i, last_frame)
elif frame_type == 1:
last_frame = rle_decode(encoded_file, 0)
write_frame(i, last_frame)
elif frame_type == 2:
last_frame = rle_decode(encoded_file, 1)
write_frame(i, last_frame)
else:
break;
main()