[3662b5]: / infinity / formats / tis.py  Maximize  Restore  History

Download this file

320 lines (250 with data), 9.7 kB

  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
# -*-python-*-
# ie_shell.py - Simple shell for Infinity Engine-based game files
# Copyright (C) 2004 by Jaroslav Benkovsky, <edheldil@users.sf.net>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
import struct
import sys
import PIL
from infinity import core
from infinity.format import Format, register_format
from infinity.imagesequence import ImageSequence
class TIS_Format (Format, ImageSequence):
header_desc = (
{ 'key': 'signature',
'type': 'STR4',
'off': 0x0000,
'label': 'Signature' },
{ 'key': 'version',
'type': 'STR4',
'off':0x0004,
'label': 'Version'},
{ 'key': 'tile_cnt',
'type': 'DWORD',
'off': 0x0008,
'label': 'Count of tiles' },
{ 'key': 'length',
'type': 'DWORD',
'off': 0x000C,
'label': 'Tile size (bytes)' },
{ 'key': 'tile_off',
'type': 'DWORD',
'off': 0x0010,
'label': 'Tiles offset'},
{ 'key': 'size',
'type': 'DWORD',
'off': 0x0014,
'label': 'Tile width (or height)'},
)
palette_entry_desc = (
{ 'key': 'b',
'type': 'BYTE',
'off': 0x0000,
'label': 'B'},
{ 'key': 'g',
'type': 'BYTE',
'off': 0x0001,
'label': 'G'},
{ 'key': 'r',
'type': 'BYTE',
'off': 0x0002,
'label': 'R'},
{ 'key': 'a',
'type': 'BYTE',
'off': 0x0003,
'label': 'A'},
)
def __init__ (self):
Format.__init__ (self)
ImageSequence.__init__ (self)
self.expect_signature = 'TIS'
self.tile_list = []
def read (self, stream):
self.read_header (stream)
palette_size = 4 * 256
tile_size = self.header['size'] ** 2
off = self.header['tile_off']
for i in range (self.header['tile_cnt']):
pal = []
self.read_palette (stream, off, pal)
off += palette_size
bin_data = stream.read_blob (off, tile_size)
#tile_data = struct.unpack ('%dB' %tile_size, bin_data)
off += tile_size
obj = {'palette': pal, 'tile_data': bin_data}
self.tile_list.append (obj)
return self
def write (self, stream):
self.header['tile_cnt'] = len (self.tile_list)
self.header['tile_off'] = self.get_struc_size (self.header_desc)
self.write_header (stream)
tile_size = self.header['size'] ** 2
palette_size = 4 * 256
off = self.header['tile_off']
for tile in self.tile_list:
self.write_palette (stream, off, tile['palette'])
off += palette_size
#bin_data = struct.pack ('%dB' %tile_size, *tile['tile_data'])
stream.write_blob (tile['tile_data'], off)
off += tile_size
def printme (self):
self.print_header ()
if self.get_option ('format.tis.print_palettes'):
i = 0
for obj in self.tile_list:
print('Palette #%d' %i)
self.print_palette (obj['palette'])
i = i + 1
if self.get_option ('format.tis.print_tiles'):
i = 0
for obj in self.tile_list:
print('Tile #%d' %i)
#print obj['offset']
self.print_tile (obj)
i = i + 1
def read_palette (self, stream, offset, obj):
for i in range (256):
obj2 = {}
self.read_struc (stream, offset, self.palette_entry_desc, obj2)
obj.append (obj2)
offset = offset + 4
return obj
def write_palette (self, stream, offset, obj):
for i in range (256):
self.write_struc (stream, offset, self.palette_entry_desc, obj[i])
offset = offset + 4
def print_palette (self, palette):
i = 0
for obj in palette:
print("%3d: %3d %3d %3d %3d (#%02x%02x%02x%02x)" %(i, obj['r'], obj['g'], obj['b'], obj['a'], obj['r'], obj['g'], obj['b'], obj['a']))
i = i + 1
# def read_tile (self, stream, obj):
# size = obj['width'] * obj['height']
# bin_data = stream.read_blob (obj['offset'], size)
# obj['tile_data'] = struct.unpack ('%dB' %size, bin_data)
#
#
def frame_to_image (self, obj):
pal = obj['palette']
data = [ '%c%c%c\xff' %(pal[ord(p)]['r'], pal[ord(p)]['g'], pal[ord(p)]['b']) for p in obj['tile_data']]
pixels = ''.join(data)
# FIXME: not needed
obj['x'] = 0
obj['y'] = 0
sz = self.header['size']
obj['width'] = sz
obj['height'] = sz
img = PIL.Image.fromstring ('RGBA', (sz, sz), pixels, "raw", 'RGBA', 0, 1)
img.x = 0
img.y = 0
obj['image'] = img
return img
# def print_tile (self, obj):
# gray = ' #*+:.'
# grsz = len (gray) - 1
# ndx = 0
#
# for i in range (self.header['size']):
# for j in range (self.header['size']):
# pix = obj['tile_data'][ndx]
#
# p = obj['palette'][pix]
# gr = 1 + (p['r'] + p['g'] + p['b']) / (3 * (255 / grsz))
# if gr >= grsz:
# gr = grsz - 1
# sys.stdout.write (gray[gr])
# #sys.stdout.write (gray[gr])
# #print gray[gr],
# ndx = ndx + 1
# print
# print
# # FIXME: use stream instead of fh?
# def write_ppm (self, fh):
# fh.write ("P6\n")
# fh.write ("# ie_shell\n");
# fh.write ("%d %d\n" %(self.header['width'], self.header['height']));
# fh.write ("255\n");
#
# for line in range (self.header['height']):
# row = line / self.header['block_size']
# scanline = line % self.header['block_size']
#
# for i in range (self.header['columns']):
# tile = self.tile_list[(row * self.header['columns']) + i]
# pal = tile['palette']
#
# o = scanline * tile['width']
# for x in range (tile['width']):
# pix = tile['tile_data'][o + x]
# col = pal[pix]
# fh.write ('%c%c%c' %(col['r'], col['g'], col['b']))
def get_frame_lol (self):
return [ self.tile_list ]
def from_mos (self, mos):
self.tile_list = []
size = self.header['size'] = mos.header['block_size']
for obj in mos.tile_list:
# FIXME: this destroys original tiles in the MOS_Format object
width = obj['width']
height = obj['height']
if width < size:
pad = [ obj['tile_data'][size - 1] ] * (size - width)
else:
pad = []
#print 'Pad:', pad
start = 0
data = []
for i in range (obj['height']):
row = list (obj['tile_data'][start:start+width]) + pad
data.extend (row)
start += width
if height < size:
pad = data[-size:] * (size - height)
else:
pad = []
#print 'Pad:', pad
data.extend (pad)
data = struct.pack ("%dB" %(size*size), *data)
self.tile_list.append ({ 'palette': obj['palette'], 'tile_data': data })
self.header['tile_cnt'] = len (self.tile_list)
self.header['tile_off'] = self.get_struc_size (self.header_desc)
self.header['length'] = 4 * 256 + self.header['size'] ** 2
class UTIS_Format (TIS_Format):
def __init__ (self):
TIS_Format.__init__ (self)
self.header = {}
self.reset_struc (self.header_desc, self.header)
self.header['size'] = 64
def read (self, stream):
#self.read_header (stream)
palette_size = 4 * 256
tile_size = self.header['size'] ** 2
off = 0
while True:
pal = []
try:
self.read_palette (stream, off, pal)
except: # FIXME: ugly hack
break
off += palette_size
bin_data = stream.read_blob (off, tile_size)
if len (bin_data) != tile_size:
break
off += tile_size
obj = {'palette': pal, 'tile_data': bin_data}
self.tile_list.append (obj)
self.header['tile_cnt'] = len (self.tile_list)
return self
register_format ('TIS', 'V1', TIS_Format)