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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
|
# Contributed by
# Dany Zatuchna (danzat at gmail)
""" Implementation of the following grammar for the GIF89a file format
<GIF Data Stream> ::= Header <Logical Screen> <Data>* Trailer
<Logical Screen> ::= Logical Screen Descriptor [Global Color Table]
<Data> ::= <Graphic Block> |
<Special-Purpose Block>
<Graphic Block> ::= [Graphic Control Extension] <Graphic-Rendering Block>
<Graphic-Rendering Block> ::= <Table-Based Image> |
Plain Text Extension
<Table-Based Image> ::= Image Descriptor [Local Color Table] Image Data
<Special-Purpose Block> ::= Application Extension |
Comment Extension
"""
from construct import *
import six
data_sub_block = Struct("data_sub_block",
ULInt8("size"),
String("data", lambda ctx: ctx["size"])
)
gif_logical_screen = Struct("logical_screen",
ULInt16("width"),
ULInt16("height"),
BitStruct("flags",
Bit("global_color_table"),
BitField("color_resolution", 3),
Bit("sort_flag"),
BitField("global_color_table_bpp", 3)
),
ULInt8("bgcolor_index"),
ULInt8("pixel_aspect_ratio"),
If(lambda ctx: ctx["flags"]["global_color_table"],
Array(lambda ctx: 2**(ctx["flags"]["global_color_table_bpp"] + 1),
Struct("palette",
ULInt8("R"),
ULInt8("G"),
ULInt8("B")
)
)
)
)
gif_header = Struct("gif_header",
Const(String("signature", 3), six.b("GIF")),
Const(String("version", 3), six.b("89a")),
)
application_extension = Struct("application_extension",
Const(ULInt8("block_size"), 11),
String("application_identifier", 8),
String("application_auth_code", 3),
data_sub_block,
ULInt8("block_terminator")
)
comment_extension = Struct("comment_extension",
data_sub_block,
ULInt8("block_terminator")
)
graphic_control_extension = Struct("graphic_control_extension",
Const(ULInt8("block_size"), 4),
BitStruct("flags",
BitField("reserved", 3),
BitField("disposal_method", 3),
Bit("user_input_flag"),
Bit("transparent_color_flag"),
),
ULInt16("delay"),
ULInt8("transparent_color_index"),
ULInt8("block_terminator")
)
plain_text_extension = Struct("plain_text_extension",
Const(ULInt8("block_size"), 12),
ULInt16("text_left"),
ULInt16("text_top"),
ULInt16("text_width"),
ULInt16("text_height"),
ULInt8("cell_width"),
ULInt8("cell_height"),
ULInt8("foreground_index"),
ULInt8("background_index"),
data_sub_block,
ULInt8("block_terminator")
)
extension = Struct("extension",
ULInt8("label"),
Switch("ext", lambda ctx: ctx["label"], {
0xFF: application_extension,
0xFE: comment_extension,
0xF9: graphic_control_extension,
0x01: plain_text_extension
})
)
image_descriptor = Struct("image_descriptor",
ULInt16("left"),
ULInt16("top"),
ULInt16("width"),
ULInt16("height"),
BitStruct("flags",
Bit("local_color_table"),
Bit("interlace"),
Bit("sort"),
BitField("reserved", 2),
BitField("local_color_table_bpp", 3)
),
If(lambda ctx: ctx["flags"]["local_color_table"],
Array(lambda ctx: 2**(ctx["flags"]["local_color_table_bpp"] + 1),
Struct("palette",
ULInt8("R"),
ULInt8("G"),
ULInt8("B")
)
)
),
ULInt8("LZW_minimum_code_size"),
RepeatUntil(lambda obj, ctx: obj.size == 0, data_sub_block)
)
gif_data = Struct("gif_data",
ULInt8("introducer"),
Switch("dat", lambda ctx: ctx["introducer"], {
0x21: extension,
0x2C: image_descriptor
})
)
gif_file = Struct("gif_file",
gif_header,
gif_logical_screen,
OptionalGreedyRange(gif_data),
#Const(ULInt8("trailer"), 0x3B)
)
if __name__ == "__main__":
f = open("../../../tests/sample.gif", "rb")
s = f.read()
f.close()
print(gif_file.parse(s))
|