-
Notifications
You must be signed in to change notification settings - Fork 1
/
gifrawlib.py
218 lines (164 loc) · 5.06 KB
/
gifrawlib.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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# -*- coding: utf-8 -*-
# 低レベルGIFライブラリ
import struct
# ファイルヘッダ
class FileHeader:
def __init__(self):
self.signature = "GIF87a"
self.width = 0 # pixel
self.height = 0 # pixel
self.color_resolution = 8 # bits
self.is_sorted_color_table = False
self.color_table_size = 0 # bits
self.background_color_index = 0
self.pixel_aspect_ratio = 0
def has_color_table_flag(self):
return 0 if self.color_table_size == 0 else 1
def color_resolution_flag(self):
return self.color_resolution - 1
def is_sorted_color_table_flag(self):
return 1 if self.is_sorted_color_table else 0
def color_table_size_flag(self):
if self.color_table_size == 0:
return 0
else:
return self.color_table_size - 1
def flag(self):
flag = 0
flag |= (self.has_color_table_flag() << 7)
flag |= (self.color_resolution_flag() << 4)
flag |= (self.is_sorted_color_table_flag() << 3)
flag |= (self.color_table_size_flag() << 0)
return flag
def write(self, io):
io.write(self.signature)
io.write(struct.pack("H", self.width))
io.write(struct.pack("H", self.height))
io.write(struct.pack("B", self.flag()))
io.write(struct.pack("B", self.background_color_index))
io.write(struct.pack("B", self.pixel_aspect_ratio))
return self
# イメージブロックヘッダ
class ImageBlockHeader:
def __init__(self):
self.left = 0 # pixel
self.top = 0 # pixel
self.width = 0 # pixel
self.height = 0 # pixel
self.is_interlaced = False
self.is_sorted_color_table = False
self.color_table_size = 0 # bits
def has_color_table_flag(self):
return 0 if self.color_table_size == 0 else 1
def is_interlaced_flag(self):
return 1 if self.is_interlaced else 0
def is_sorted_color_table_flag(self):
return 1 if self.is_sorted_color_table else 0
def color_table_size_flag(self):
if self.color_table_size == 0:
return 0
else:
return self.color_table_size - 1
def flag(self):
flag = 0
flag |= (self.has_color_table_flag() << 7)
flag |= (self.is_interlaced_flag() << 6)
flag |= (self.is_sorted_color_table_flag() << 5)
flag |= (self.color_table_size_flag() << 0)
return flag
def write(self, io):
io.write(struct.pack("B", 0x2c))
io.write(struct.pack("H", self.left))
io.write(struct.pack("H", self.top))
io.write(struct.pack("H", self.width))
io.write(struct.pack("H", self.height))
io.write(struct.pack("B", self.flag()))
return self
# トレーラー
class Trailer:
def __init__(self):
pass
def write(self, io):
io.write(struct.pack("B", 0x3b))
return self
# カラーテーブル
# MEMO: ひとまず8bitカラー専用とする
class ColorTable:
def __init__(self):
self.table = []
def size(self):
return len(self.table)
def bit_size(self):
return 8
def append(self, color):
self.table.append(color)
return self
def get_colors(self):
return self.table[:]
def write(self, io):
count = 0
max = 2 ** self.bit_size()
for (r, g, b) in self.table:
io.write(struct.pack("BBB", r, g, b))
count += 1
for i in range(max - count):
io.write(struct.pack("BBB", 0, 0, 0))
return self
# 無圧縮イメージブロックデータ
# MEMO: ひとまず8bitカラー専用とする
class UncompressedImageBlockData:
def __init__(self):
self.pixels = []
def size(self):
return len(self.pixels)
def append(self, pixel):
self.pixels.append(pixel)
return self
def bitsets(self):
bitsets = []
count = 0
for pixel in self.pixels:
if count % 254 == 0:
bitsets.append("100000000") # clear code
bin = ""
for i in range(8):
bin = str(pixel & 1) + bin
pixel >>= 1
bitsets.append("0" + bin)
count += 1
if len(self.pixels) == 0:
bitsets.append("100000000") # clear code
bitsets.append("100000001") # end code
return bitsets
def bits_to_bytes(self, bits):
bytes = []
while len(bits) > 0:
octet = bits[0:8]
bits = bits[8:]
bytes.append(int(octet, 2))
return bytes
def bytes(self):
bitsets = self.bitsets()
bitsets.reverse()
bits = "".join(bitsets)
padding = "".join(["0" for i in range(8 - (len(bits) % 8))]) if len(bits) % 8 > 0 else ""
bytes = self.bits_to_bytes(padding + bits)
bytes.reverse()
return bytes
# イメージブロック
class ImageBlock:
def __init__(self):
self.minimum_code = 8 # bit
self.data = []
def write(self, io):
io.write(struct.pack("B", self.minimum_code))
bytes = self.data[:]
while len(bytes) > 0:
block = bytes[0:255]
bytes = bytes[255:]
io.write(struct.pack("B", len(block)))
for byte in block:
io.write(struct.pack("B", byte))
# Block Terminator
io.write(struct.pack("B", 0))
return self