-
Notifications
You must be signed in to change notification settings - Fork 4
/
TIM.py
308 lines (283 loc) · 11.7 KB
/
TIM.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
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
bl_info = {
"name": "Vagrant Story file formats Add-on",
"description": "Import-Export Vagrant Story file formats (WEP, SHP, SEQ, ZUD, MPD, ZND, P, FBT, FBC).",
"author": "Sigfrid Korobetski (LunaticChimera)",
"version": (2, 12),
"blender": (3, 2, 0),
"location": "File > Import-Export",
"category": "Import-Export",
}
import struct
import bpy
from . import color
class WEPTIM:
def __init__(self):
self.texMapSize = 0
self.unk = 0
self.halfW = 0
self.halfH = 0
self.textureWidth = 0
self.textureHeigth = 0
self.numColor = 0
self.palletColors = []
self.handleColors = [] # common colors between pallets, 1/3 of num colors
self.textures = []
self.numPallets = 7
self.cluts = []
def __repr__(self):
return "(TIM : "+ " texMapSize = "+ repr(self.texMapSize)+ " unk = "+ repr(self.unk)+ " halfW = "+ repr(self.halfW)+ " halfH = "+ repr(self.halfH)+ " numColor = "+ repr(self.numColor)+ ")"
def feed(self, file):
self.texMapSize,self.unk,self.halfW,self.halfH,self.numColor = struct.unpack("I 4B", file.read(8))
self.textureWidth = self.halfW * 2
self.textureHeigth = self.halfH * 2
self.textures = []
if self.numColor > 0:
self.handleColors = []
for j in range(0, int(self.numColor / 3)):
colorData = struct.unpack("H", file.read(2))[0]
self.handleColors.append(color.from16bits(colorData))
for i in range(0, self.numPallets):
colors = []
colors += self.handleColors
for j in range(0, int(self.numColor / 3 * 2)):
colorData = struct.unpack("H", file.read(2))[0]
colors.append(color.from16bits(colorData))
self.palletColors.append(colors)
# pallet colors indexes
cluts = []
for x in range(0, self.textureWidth):
for y in range(0, self.textureHeigth):
clut = struct.unpack("B", file.read(1))[0] # CLUT colour reference
cluts.append(clut)
for i in range(0, self.numPallets):
pixmap = []
for j in range(0, len(cluts)):
if int(cluts[j]) < self.numColor:
pixmap.extend(self.palletColors[i][int(cluts[j])].toFloat())
else:
pixmap.extend(self.palletColors[i][0].toFloat())
self.textures.append(pixmap)
# we add pallets colors in the first raw (never used in UVs)
# by doing this we make sure all colors are used and ordered
i = 0
for x in range(0, 7):
for y in range(0, 48):
self.textures[x][i] = self.palletColors[x][y].R / 255
self.textures[x][i + 1] = self.palletColors[x][y].G / 255
self.textures[x][i + 2] = self.palletColors[x][y].B / 255
self.textures[x][i + 3] = self.palletColors[x][y].A / 255
i += 4
i = 0
def tobin(self):
bin = bytes()
bin += struct.pack("I 4B", self.texMapSize, self.unk, self.halfW, self.halfH, self.numColor)
for i in range(0, 16): # 16 = 48/3
if i < len(self.handleColors):
bin += struct.pack("H", int(self.handleColors[i].to16bits(), 16))
else:
bin += struct.pack("H", 65535)
for i in range(0, 7):
for j in range(16, 48):
if i < len(self.palletColors):
if j < len(self.palletColors[i]):
bin += struct.pack("H", int(self.palletColors[i][j].to16bits(), 16))
else:
bin += struct.pack("H", 65535)
else:
bin += struct.pack("H", 65535)
i = 0
for x in range(0, self.textureWidth):
for y in range(0, self.textureHeigth):
if i < len(self.cluts):
bin += struct.pack("B", self.cluts[i])
else:
bin += b"\x00"
i += 1
return bin
def binsize(self):
size = 8 # tim header
# we considere 48 colors per palette
size += 16 * 2 # handle colors
size += 32 * 2 * 7 # palettes colors
size += self.textureWidth * self.textureHeigth # indexes one byte per pixel
return size
class SHPTIM:
def __init__(self):
self.texMapSize = 0
self.unk = 0
self.halfW = 0
self.halfH = 0
self.textureWidth = 0
self.textureHeigth = 0
self.numColor = 0
self.palletColors = []
self.textures = []
self.numPallets = 2
self.cluts = []
self.doubleClut = False
def __repr__(self):
return "(TIM : "+ " texMapSize = "+ repr(self.texMapSize)+ " unk = "+ repr(self.unk)+ " halfW = "+ repr(self.halfW)+ " halfH = "+ repr(self.halfH)+ " numColor = "+ repr(self.numColor)+ ")"
def feed(self, file):
self.texMapSize,self.unk,self.halfW,self.halfH,self.numColor = struct.unpack("I 4B", file.read(8))
self.textureWidth = self.halfW * 2
self.textureHeigth = self.halfH * 2
self.textures = []
if self.numColor > 0:
for i in range(0, self.numPallets):
colors = []
for j in range(0, int(self.numColor)):
colorData = struct.unpack("H", file.read(2))[0]
colors.append(color.from16bits(colorData))
self.palletColors.append(colors)
# pallet colors indexes
cluts = []
for x in range(0, self.textureWidth):
for y in range(0, self.textureHeigth):
if self.doubleClut == False:
clut = struct.unpack("B", file.read(1))[0] # CLUT colour reference
cluts.append(clut)
else:
# when colored faces a single byte is two pixels
id = struct.unpack("B", file.read(1))[0]
cluts.append(id % 16)
cluts.append(id // 16)
for i in range(0, self.numPallets):
pixmap = []
for j in range(0, len(cluts)):
if int(cluts[j]) < self.numColor:
pixmap.extend(
self.palletColors[i][int(cluts[j])].toFloat())
else:
pixmap.extend(self.palletColors[i][0].toFloat())
self.textures.append(pixmap)
if self.doubleClut == True: # when colored faces we must multiply by 4
self.textureWidth = self.halfW * 4
# TODO : inverse textures and UVs
def tobin(self):
bin = bytes()
bin += struct.pack("I 4B", self.texMapSize, self.unk, self.halfW, self.halfH, self.numColor)
for i in range(0, 2):
for j in range(0, self.numColor):
if i < len(self.palletColors):
if j < len(self.palletColors[i]):
bin += struct.pack("H", int(self.palletColors[i][j].to16bits(), 16))
else:
bin += struct.pack("H", 65535)
else:
bin += struct.pack("H", 65535)
i = 0
for x in range(0, self.textureWidth):
for y in range(0, self.textureHeigth):
if i < len(self.cluts):
bin += struct.pack("B", self.cluts[i])
else:
bin += b"\x00"
i += 1
return bin
class TIM16BPP:
# 16BPP TIM Header
# [1-4] - 10 00 00 00: ID Tag for TIM
# [5-8] - 02 00 00 00: ID Tag for 16BPP
# [9-12] - Size of image data + 12 (accounting for 12 bytes before image data starts)
# [13-14] - Image Org X
# [15-16] - Image Org Y
# [17-18] - Image Width (Stored as actual width)
# [19-20] - Image Height
def __init__(self):
self.h = 0
self.bpp = 0
self.imgLen = 0
self.fx = 0
self.fy = 0
self.width = 0
self.height = 0
self.dataLen = 0
self.dataPtr = 0
self.idx = ""
self.texture = None
self.offset = 0
self.isCLUT = False
self.bytes = bytearray()
self.colors = []
def __repr__(self):
return (
"(--TIM16BPP-- | "+ " offset : "+repr(self.offset)+ ", idx : "+repr(self.idx)+ ", h : "+repr(self.h)+ ", bpp : "+repr(self.bpp)+ ", imgLen : "
+repr(self.imgLen)+ ", fx : "+repr(self.fx)+ ", fy : "+repr(self.fy)+ ", width : "+repr(self.width)+ ", height : "+repr(self.height)
)
def parse(self, idx, file, offset, len):
self.offset = offset
self.idx = idx
self.h, self.bpp, self.imgLen, self.fx, self.fy, self.width, self.height = struct.unpack("3I4H", file.read(20))
self.dataLen = self.imgLen - 12
self.dataPtr = file.tell()
# we fill a bytearray because we don't know yet if the TIM is an index or a CLUT
self.bytes = bytearray()
self.bytes = file.read(self.dataLen)
file.seek(self.dataPtr)
# if fy != 0 it seems to be a CLUT
# so we store colors
if self.fy != 0:
self.isCLUT = True
self.colors = []
for x in range(0, self.width):
for y in range(0, self.height):
colorData = struct.unpack("H", file.read(2))[0]
col = color.from16bits(colorData)
self.colors.append(col)
# fb.setPixel(self.fx + x, self.fy + y, col)
#else:
# size = self.width * self.height * 2
# pixmap = []
# for i in range(0, size):
# c = self.bytes[i]
# l = ( ( c & 0xF0 ) >> 4 )
# r = ( c & 0x0F )
# pixmap.extend(color.GreyCLUT[r].toFloat())
# pixmap.extend(color.GreyCLUT[l].toFloat())
# texImage = bpy.data.textures.new("TIM16BPP-"+repr(self.idx), 'IMAGE')
# texImage.image = bpy.data.images.new("TIM16BPP-"+repr(self.idx), self.width*4, self.height)
# texImage.image.pixels = pixmap
def buildCLUT(self, x, y, alpha = False):
ox = x - self.fx
oy = y - self.fy
dec = oy * self.width + ox
bufferArray = []
for i in range(dec, dec+16):
col = self.colors[i]
if alpha == True:
col.alphaFromGrey()
bufferArray.append(col)
return bufferArray
def build(self, clut):
size = self.width * self.height * 2
pixmap = []
for i in range(0, size):
c = self.bytes[i]
l = ( ( c & 0xF0 ) >> 4 )
r = ( c & 0x0F )
pixmap.extend(clut[r].toFloat())
pixmap.extend(clut[l].toFloat())
return pixmap
# we don't need this anymore
class FrameBuffer:
def __init__(self):
self.width = 1024
self.height = 512
self.buffer = bytearray()
# clean empty frame
self.r = self.width*self.height*4
for i in range(0, self.r):
self.buffer += b"\x00"
def setPixel(self, x, y, col):
i = (y * self.width + x) * 4
self.buffer[i + 0] = col.R
self.buffer[i + 1] = col.G
self.buffer[i + 2] = col.B
self.buffer[i + 3] = col.A
def buildTexture(self):
pixmap = []
for i in range(0, self.r):
pixmap.append(self.buffer[i]/255)
texImage = bpy.data.textures.new("FrameBuffer", 'IMAGE')
texImage.image = bpy.data.images.new("FrameBuffer_Tex", self.width, self.height)
texImage.image.pixels = pixmap