forked from pret/pokered
-
Notifications
You must be signed in to change notification settings - Fork 2
/
pic.py
executable file
·491 lines (394 loc) · 12.9 KB
/
pic.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
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
A library for use with compressed monster and trainer pics in pokered.
"""
from __future__ import absolute_import
from __future__ import division
import os
import sys
import argparse
from math import sqrt
from pokemontools import gfx
def bitflip(x, n):
r = 0
while n:
r = (r << 1) | (x & 1)
x >>= 1
n -= 1
return r
class Decompressor:
"""
pokered pic decompression.
Ported to python 2.7 from the python 3 code at https://github.com/magical/pokemon-sprites-rby.
"""
table1 = [(2 << i) - 1 for i in range(16)]
table2 = [
[0x0, 0x1, 0x3, 0x2, 0x7, 0x6, 0x4, 0x5, 0xf, 0xe, 0xc, 0xd, 0x8, 0x9, 0xb, 0xa],
[0xf, 0xe, 0xc, 0xd, 0x8, 0x9, 0xb, 0xa, 0x0, 0x1, 0x3, 0x2, 0x7, 0x6, 0x4, 0x5], # prev ^ 0xf
[0x0, 0x8, 0xc, 0x4, 0xe, 0x6, 0x2, 0xa, 0xf, 0x7, 0x3, 0xb, 0x1, 0x9, 0xd, 0x5],
[0xf, 0x7, 0x3, 0xb, 0x1, 0x9, 0xd, 0x5, 0x0, 0x8, 0xc, 0x4, 0xe, 0x6, 0x2, 0xa], # prev ^ 0xf
]
table3 = [bitflip(i, 4) for i in range(16)]
tilesize = 8
def __init__(self, f, mirror=False, planar=True):
self.bs = fbitstream(f)
self.mirror = mirror
self.planar = planar
self.data = None
def decompress(self):
rams = [[], []]
self.sizex = self._readint(4) * self.tilesize
self.sizey = self._readint(4)
self.size = self.sizex * self.sizey
self.ramorder = self._readbit()
r1 = self.ramorder
r2 = self.ramorder ^ 1
self._fillram(rams[r1])
mode = self._readbit()
if mode:
mode += self._readbit()
self._fillram(rams[r2])
rams[0] = bytearray(bitgroups_to_bytes(rams[0]))
rams[1] = bytearray(bitgroups_to_bytes(rams[1]))
if mode == 0:
self._decode(rams[0])
self._decode(rams[1])
elif mode == 1:
self._decode(rams[r1])
self._xor(rams[r1], rams[r2])
elif mode == 2:
self._decode(rams[r2], mirror=False)
self._decode(rams[r1])
self._xor(rams[r1], rams[r2])
else:
raise Exception("Invalid deinterlace mode!")
data = []
if self.planar:
for a, b in zip(rams[0], rams[1]):
data += [a, b]
self.data = bytearray(data)
else:
for a, b in zip(bitstream(rams[0]), bitstream(rams[1])):
data.append(a | (b << 1))
self.data = bitgroups_to_bytes(data)
def _fillram(self, ram):
mode = ['rle', 'data'][self._readbit()]
size = self.size * 4
while len(ram) < size:
if mode == 'rle':
self._read_rle_chunk(ram)
mode = 'data'
elif mode == 'data':
self._read_data_chunk(ram, size)
mode = 'rle'
if len(ram) > size:
#ram = ram[:size]
raise ValueError(size, len(ram))
ram[:] = self._deinterlace_bitgroups(ram)
def _read_rle_chunk(self, ram):
i = 0
while self._readbit():
i += 1
n = self.table1[i]
a = self._readint(i + 1)
n += a
for i in range(n):
ram.append(0)
def _read_data_chunk(self, ram, size):
while 1:
bitgroup = self._readint(2)
if bitgroup == 0:
break
ram.append(bitgroup)
if size <= len(ram):
break
def _decode(self, ram, mirror=None):
if mirror is None:
mirror = self.mirror
for x in range(self.sizex):
bit = 0
for y in range(self.sizey):
i = y * self.sizex + x
a = (ram[i] >> 4) & 0xf
b = ram[i] & 0xf
a = self.table2[bit][a]
bit = a & 1
if mirror:
a = self.table3[a]
b = self.table2[bit][b]
bit = b & 1
if mirror:
b = self.table3[b]
ram[i] = (a << 4) | b
def _xor(self, ram1, ram2, mirror=None):
if mirror is None:
mirror = self.mirror
for i in range(len(ram2)):
if mirror:
a = (ram2[i] >> 4) & 0xf
b = ram2[i] & 0xf
a = self.table3[a]
b = self.table3[b]
ram2[i] = (a << 4) | b
ram2[i] ^= ram1[i]
def _deinterlace_bitgroups(self, bits):
l = []
for y in range(self.sizey):
for x in range(self.sizex):
i = 4 * y * self.sizex + x
for j in range(4):
l.append(bits[i])
i += self.sizex
return l
def _readbit(self):
return next(self.bs)
def _readint(self, count):
return readint(self.bs, count)
def fbitstream(f):
while 1:
char = f.read(1)
if not char:
break
byte = ord(char)
for i in range(7, -1, -1):
yield (byte >> i) & 1
def bitstream(b):
for byte in b:
for i in range(7, -1, -1):
yield (byte >> i) & 1
def readint(bs, count):
n = 0
while count:
n <<= 1
n |= next(bs)
count -= 1
return n
def bitgroups_to_bytes(bits):
l = []
for i in range(0, len(bits) - 3, 4):
n = ((bits[i + 0] << 6)
| (bits[i + 1] << 4)
| (bits[i + 2] << 2)
| (bits[i + 3] << 0))
l.append(n)
return bytearray(l)
def bytes_to_bits(bytelist):
return list(bitstream(bytelist))
class Compressor:
"""
pokered pic compression.
Adapted from stag019's C compressor.
"""
table1 = [(2 << i) - 1 for i in range(16)]
table2 = [
[0x0, 0x1, 0x3, 0x2, 0x6, 0x7, 0x5, 0x4, 0xc, 0xd, 0xf, 0xe, 0xa, 0xb, 0x9, 0x8],
[0x8, 0x9, 0xb, 0xa, 0xe, 0xf, 0xd, 0xc, 0x4, 0x5, 0x7, 0x6, 0x2, 0x3, 0x1, 0x0], # reverse
]
table3 = [bitflip(i, 4) for i in range(16)]
def __init__(self, image, width=None, height=None):
self.image = bytearray(image)
self.size = len(self.image)
planar_tile = 8 * 8 // 4
tile_size = self.size // planar_tile
if height and not width: width = tile_size // height
elif width and not height: height = tile_size // width
elif not width and not height: width = height = int(sqrt(tile_size))
self.width, self.height = width, height
def compress(self):
"""
Compress the image five times (twice for each mode, except 0)
and use the smallest one (in bits).
"""
rams = [[],[]]
datas = []
for mode in range(3):
# Order is redundant for mode 0.
# While this seems like an optimization,
# it's actually required for 1:1 compression
# to the original compressed pics.
# This appears to be the algorithm
# that Game Freak's compressor used.
# Using order 0 instead of 1 breaks this feature.
for order in range(2):
if mode == 0 and order == 0:
continue
for i in range(2):
rams[i] = self.image[i::2]
self._interpret_compress(rams, mode, order)
datas += [(self.data[:], int(self.which_bit))]
# Pick the smallest pic, measured in bits.
datas = sorted(datas, key=lambda data_bit: (len(data_bit[0]), -data_bit[1]))
self.data, self.which_bit = datas[0]
def _interpret_compress(self, rams, mode, order):
self.data = []
self.which_bit = 0
r1 = order
r2 = order ^ 1
if mode == 0:
self._encode(rams[1])
self._encode(rams[0])
elif mode == 1:
self._xor(rams[r1], rams[r2])
self._encode(rams[r1])
elif mode == 2:
self._xor(rams[r1], rams[r2])
self._encode(rams[r1])
self._encode(rams[r2], mirror=False)
else:
raise Exception('invalid interlace mode!')
self._writeint(self.height, 4)
self._writeint(self.width, 4)
self._writebit(order)
self._fillram(rams[r1])
if mode == 0:
self._writebit(0)
else:
self._writebit(1)
self._writebit(mode - 1)
self._fillram(rams[r2])
def _fillram(self, ram):
rle = 0
nums = 0
bitgroups = []
for x in range(self.width):
for bit in range(0, 8, 2):
byte = x * self.height * 8
for y in range(self.height * 8):
bitgroup = (ram[byte] >> (6 - bit)) & 3
if bitgroup == 0:
if rle == 0:
self._writebit(0)
elif rle == 1:
nums += 1
else:
self._data_packet(bitgroups)
self._writebit(0)
self._writebit(0)
rle = 1
bitgroups = []
else:
if rle == 0:
self._writebit(1)
elif rle == 1:
self._rle(nums)
rle = -1
bitgroups += [bitgroup]
nums = 0
byte += 1
if rle == 1:
self._rle(nums)
else:
self._data_packet(bitgroups)
def _data_packet(self, bitgroups):
for bitgroup in bitgroups:
self._writebit((bitgroup >> 1) & 1)
self._writebit((bitgroup >> 0) & 1)
def _rle(self, nums):
nums += 1
# Get the previous power of 2.
# Deriving the bitcount from that seems to be
# faster on average than using the lookup table.
v = nums
v += 1
v |= v >> 1
v |= v >> 2
v |= v >> 4
v |= v >> 8
v |= v >> 16
v -= v >> 1
v -= 1
number = nums - v
bitcount = -1
while v:
v >>= 1
bitcount += 1
for j in range(bitcount):
self._writebit(1)
self._writebit(0)
for j in range(bitcount, -1, -1):
self._writebit((number >> j) & 1)
def _encode(self, ram, mirror=None):
a = b = 0
for i in range(len(ram)):
j = i // self.height
j += i % self.height * self.width * 8
if i % self.height == 0:
b = 0
a = (ram[j] >> 4) & 0xf
table = b & 1
code_1 = self.table2[table][a]
b = ram[j] & 0xf
table = a & 1
code_2 = self.table2[table][b]
ram[j] = (code_1 << 4) | code_2
def _xor(self, ram1, ram2):
for i in range(len(ram2)):
ram2[i] ^= ram1[i]
def _writebit(self, bit):
self.which_bit -= 1
if self.which_bit == -1:
self.which_bit = 7
self.data += [0]
if bit: self.data[-1] |= bit << self.which_bit
def _writeint(self, num, size=None):
bits = []
if size:
for i in range(size):
bits += [num & 1]
num >>= 1
else:
while num > 0:
bits += [num & 1]
num >>= 1
for bit in reversed(bits):
self._writebit(bit)
def decompress(f, offset=None, mirror=False):
"""
Decompress a pic given a file object. Return a planar 2bpp image.
Optional: offset (for roms).
"""
if offset is not None:
f.seek(offset)
dcmp = Decompressor(f, mirror=mirror)
dcmp.decompress()
return dcmp.data
def compress(f):
"""
Compress a planar 2bpp into a pic.
"""
comp = Compressor(f)
comp.compress()
return comp.data
def decompress_file(filename):
"""
Decompress a pic given a filename.
Export the resulting planar 2bpp image to
"""
pic = open(filename, 'rb')
image = decompress(pic)
image = gfx.transpose_tiles(image)
image = bytearray(image)
output_filename = os.path.splitext(filename)[0] + '.2bpp'
with open(output_filename, 'wb') as out:
out.write(image)
def compress_file(filename):
image = open(filename, 'rb').read()
image = gfx.transpose_tiles(image)
pic = compress(image)
pic = bytearray(pic)
output_filename = os.path.splitext(filename)[0] + '.pic'
with open(output_filename, 'wb') as out:
out.write(pic)
def main():
ap = argparse.ArgumentParser()
ap.add_argument('mode')
ap.add_argument('filenames', nargs='*')
args = ap.parse_args()
for filename in args.filenames:
if args.mode == 'decompress':
decompress_file(filename)
elif args.mode == 'compress':
compress_file(filename)
if __name__ == '__main__':
main()