-
Notifications
You must be signed in to change notification settings - Fork 7
/
TPLcy.pyx
348 lines (269 loc) · 12.7 KB
/
TPLcy.pyx
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
# TPL
# (Cython Version)
#
# Decodes TPL textures in various formats.
class Decoder():
"""Object that decodes a texture"""
def __init__(self, tex, w, h, updater=None, updateInterval=0.1):
"""Initializes the decoder"""
self.tex = tex
self.size = [w, h]
self.updater = updater
self.updateInterval = updateInterval
self.progress = 0
self.result = None
def run(self):
"""Runs the algorithm"""
pass
class I4Decoder(Decoder):
"""Decodes an I4 texture"""
# Format:
# IIII
def __init__(self, tex, w, h, updater=None, updateInterval=0.1):
"""Initializes the decoder"""
super().__init__(tex, w, h, updater, updateInterval)
def run(self):
"""Runs the algorithm"""
tex, w, h = self.tex, self.size[0], self.size[1]
argbBuf = bytearray(w * h * 4)
i = 0
for ytile in range(0, h, 8):
for xtile in range(0, w, 8):
for ypixel in range(ytile, ytile + 8):
for xpixel in range(xtile, xtile + 8, 2):
if(xpixel >= w or ypixel >= h):
continue
newpixel = (tex[i] >> 4) * 255 / 15 # upper nybble
newpixel = int(newpixel)
argbBuf[(((ypixel * w) + xpixel) * 4) + 0] = newpixel
argbBuf[(((ypixel * w) + xpixel) * 4) + 1] = newpixel
argbBuf[(((ypixel * w) + xpixel) * 4) + 2] = newpixel
argbBuf[(((ypixel * w) + xpixel) * 4) + 3] = 0xFF
newpixel = (tex[i] & 0x0F) * 255 / 15 # lower nybble
newpixel = int(newpixel)
argbBuf[(((ypixel * w) + xpixel) * 4) + 4] = newpixel
argbBuf[(((ypixel * w) + xpixel) * 4) + 5] = newpixel
argbBuf[(((ypixel * w) + xpixel) * 4) + 6] = newpixel
argbBuf[(((ypixel * w) + xpixel) * 4) + 7] = 0xFF
i += 1
newProgress = (ytile / h) - self.progress
if newProgress > self.updateInterval and self.updater:
self.progress += self.updateInterval
self.updater()
self.result = bytes(argbBuf)
return self.result
class I8Decoder(Decoder):
"""Decodes an I8 texture"""
# Format:
# IIIIIIII
def __init__(self, tex, w, h, updater=None, updateInterval=0.1):
"""Initializes the decoder"""
super().__init__(tex, w, h, updater, updateInterval)
def run(self):
"""Runs the algorithm"""
tex, w, h = self.tex, self.size[0], self.size[1]
argbBuf = bytearray(w * h * 4)
i = 0
for ytile in range(0, h, 4):
for xtile in range(0, w, 8):
for ypixel in range(ytile, ytile + 4):
for xpixel in range(xtile, xtile + 8):
if(xpixel >= w or ypixel >= h):
continue
newpixel = tex[i]
argbBuf[((ypixel * w) + xpixel) * 4] = newpixel
argbBuf[(((ypixel * w) + xpixel) * 4) + 1] = newpixel
argbBuf[(((ypixel * w) + xpixel) * 4) + 2] = newpixel
argbBuf[(((ypixel * w) + xpixel) * 4) + 3] = 0xFF
i += 1
newProgress = (ytile / h) - self.progress
if newProgress > self.updateInterval and self.updater:
self.progress += self.updateInterval
self.updater()
self.result = bytes(argbBuf)
return self.result
class IA4Decoder(Decoder):
"""Decodes an IA4 texture"""
# Format:
# IIIIAAAA
def __init__(self, tex, w, h, updater=None, updateInterval=0.1):
"""Initializes the decoder"""
super().__init__(tex, w, h, updater, updateInterval)
def run(self):
"""Runs the algorithm"""
tex, w, h = self.tex, self.size[0], self.size[1]
argbBuf = bytearray(w * h * 4)
i = 0
for ytile in range(0, h, 4):
for xtile in range(0, w, 8):
for ypixel in range(ytile, ytile + 4):
for xpixel in range(xtile, xtile + 8):
if(xpixel >= w or ypixel >= h):
continue
alpha = (tex[i] >> 4) * 255 / 15
newpixel = (tex[i] & 0x0F) * 255 / 15
alpha, newpixel = int(alpha), int(newpixel)
argbBuf[((ypixel * w) + xpixel) * 4] = newpixel
argbBuf[(((ypixel * w) + xpixel) * 4) + 1] = newpixel
argbBuf[(((ypixel * w) + xpixel) * 4) + 2] = newpixel
argbBuf[(((ypixel * w) + xpixel) * 4) + 3] = alpha
i += 1
newProgress = (ytile / h) - self.progress
if newProgress > self.updateInterval and self.updater:
self.progress += self.updateInterval
self.updater()
self.result = bytes(argbBuf)
return self.result
class IA8Decoder(Decoder):
"""Decodes an IA8 texture"""
# Format:
# IIIIIIII AAAAAAAA
def __init__(self, tex, w, h, updater=None, updateInterval=0.1):
"""Initializes the decoder"""
super().__init__(tex, w, h, updater, updateInterval)
def run(self):
"""Runs the algorithm"""
tex, w, h = self.tex, self.size[0], self.size[1]
argbBuf = bytearray(w * h * 4)
i = 0
for ytile in range(0, h, 4):
for xtile in range(0, w, 4):
for ypixel in range(ytile, ytile + 4):
for xpixel in range(xtile, xtile + 4):
if(xpixel >= w or ypixel >= h):
continue
newpixel = tex[i]
i += 1
alpha = tex[i]
i += 1
argbBuf[((ypixel * w) + xpixel) * 4] = newpixel
argbBuf[(((ypixel * w) + xpixel) * 4) + 1] = newpixel
argbBuf[(((ypixel * w) + xpixel) * 4) + 2] = newpixel
argbBuf[(((ypixel * w) + xpixel) * 4) + 3] = alpha
newProgress = (ytile / h) - self.progress
if newProgress > self.updateInterval and self.updater:
self.progress += self.updateInterval
self.updater()
self.result = bytes(argbBuf)
return self.result
class RGB565Decoder(Decoder):
"""Decodes an RGB565 texture"""
# Format:
# RRRRRGGG GGGBBBBB
def __init__(self, tex, w, h, updater=None, updateInterval=0.1):
"""Initializes the decoder"""
super().__init__(tex, w, h, updater, updateInterval)
def run(self):
"""Runs the algorithm"""
tex, w, h = self.tex, self.size[0], self.size[1]
argbBuf = bytearray(w * h * 4)
i = 0
for ytile in range(0, h, 4):
for xtile in range(0, w, 4):
for ypixel in range(ytile, ytile + 4):
for xpixel in range(xtile, xtile + 4):
if(xpixel >= w or ypixel >= h):
continue
blue = (tex[i] & 0x1F) * 255 / 0x1F
green1 = (tex[i] >> 5)
green2 = (tex[i+1] & 0x7)
green = (green1 << 3) | (green2)
red = (tex[i+1] >> 3) * 255 / 0x1F
red, green, blue, alpha = int(red), int(green), int(blue), 0xFF
argbBuf[((ypixel * w) + xpixel) * 4] = blue
argbBuf[(((ypixel * w) + xpixel) * 4) + 1] = green
argbBuf[(((ypixel * w) + xpixel) * 4) + 2] = red
argbBuf[(((ypixel * w) + xpixel) * 4) + 3] = alpha
i += 2
newProgress = (ytile / h) - self.progress
if newProgress > self.updateInterval and self.updater:
self.progress += self.updateInterval
self.updater()
self.result = bytes(argbBuf)
return self.result
class RGB4A3Decoder(Decoder):
"""Decodes an RGB4A3 texture"""
# Formats:
# 1RRRRRGG GGGBBBBB
# 0RRRRGGG GBBBBAAA
def __init__(self, tex, w, h, updater=None, updateInterval=0.1):
"""Initializes the decoder"""
super().__init__(tex, w, h, updater, updateInterval)
def run(self):
"""Runs the algorithm"""
tex, w, h = self.tex, self.size[0], self.size[1]
argbBuf = bytearray(w * h * 4)
i = 0
for ytile in range(0, h, 4):
for xtile in range(0, w, 4):
for ypixel in range(ytile, ytile + 4):
for xpixel in range(xtile, xtile + 4):
if(xpixel >= w or ypixel >= h):
continue
newpixel = (tex[i] << 8) | tex[i+1]
newpixel = int(newpixel)
if newpixel & 0x8000: # RGB555
red = ((newpixel >> 10) & 0x1F) * 255 / 0x1F
green = ((newpixel >> 5) & 0x1F) * 255 / 0x1F
blue = (newpixel & 0x1F) * 255 / 0x1F
alpha = 0xFF
else: # RGB4A3
alpha = ((newpixel & 0x7000) >> 12) * 255 / 0x7
blue = ((newpixel & 0xF00) >> 8) * 255 / 0xF
green = ((newpixel & 0xF0) >> 4) * 255 / 0xF
red = (newpixel & 0xF) * 255 / 0xF
red, green, blue, alpha = int(red), int(green), int(blue), int(alpha)
argbBuf[((ypixel * w) + xpixel) * 4] = blue
argbBuf[(((ypixel * w) + xpixel) * 4) + 1] = green
argbBuf[(((ypixel * w) + xpixel) * 4) + 2] = red
argbBuf[(((ypixel * w) + xpixel) * 4) + 3] = alpha
i += 2
newProgress = (ytile / h) - self.progress
if newProgress > self.updateInterval and self.updater:
self.progress += self.updateInterval
self.updater()
self.result = bytes(argbBuf)
return self.result
class RGBA8Decoder(Decoder):
"""Decodes an RGBA8 texture"""
# Format:
# RRRRRRRR GGGGGGGG BBBBBBBB AAAAAAAA
def __init__(self, tex, w, h, updater=None, updateInterval=0.1):
"""Initializes the decoder"""
super().__init__(tex, w, h, updater, updateInterval)
def run(self):
"""Runs the algorithm"""
tex, w, h = self.tex, self.size[0], self.size[1]
argbBuf = bytearray(w * h * 4)
i = 0
for ytile in range(0, h, 4):
for xtile in range(0, w, 4):
A = []
R = []
G = []
B = []
try:
for AR in range(16):
A.append(tex[i])
R.append(tex[i+1])
i += 2
for GB in range(16):
G.append(tex[i])
B.append(tex[i+1])
i += 2
except IndexError: continue
j = 0
for ypixel in range(ytile, ytile+4):
for xpixel in range(xtile, xtile+4):
red, green, blue, alpha = R[j], G[j], B[j], A[j]
argbBuf[((ypixel * w) + xpixel) * 4] = blue
argbBuf[(((ypixel * w) + xpixel) * 4) + 1] = green
argbBuf[(((ypixel * w) + xpixel) * 4) + 2] = red
argbBuf[(((ypixel * w) + xpixel) * 4) + 3] = alpha
j += 1
newProgress = (ytile / h) - self.progress
if newProgress > self.updateInterval and self.updater:
self.progress += self.updateInterval
self.updater()
self.result = bytes(argbBuf)
return self.result