Implement .pic to .2bpp decompression (#470)

This commit is contained in:
Sylvie 2024-09-26 00:45:11 -04:00 committed by GitHub
parent af36156a5b
commit 9a2f1b2170
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 234 additions and 5033 deletions

View file

@ -1,267 +0,0 @@
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""Supplementary scripts for graphics conversion."""
import os
import argparse
from pokemontools import gfx, lz
# Graphics with inverted tilemaps that aren't covered by filepath_rules.
pics = [
'gfx/shrink1',
'gfx/shrink2',
]
def recursive_read(filename):
def recurse(filename_):
lines = []
for line in open(filename_):
if 'include "' in line.lower():
lines += recurse(line.split('"')[1])
else:
lines += [line]
return lines
lines = recurse(filename)
return ''.join(lines)
base_stats = None
def get_base_stats():
global base_stats
if not base_stats:
base_stats = recursive_read('data/base_stats.asm')
return base_stats
def get_pokemon_dimensions(path):
try:
byte = bytearray(open(path, 'rb').read())[0]
width = byte & 0xf
height = (byte >> 8) & 0xf
return width, height
except:
return None
def get_animation_frames(path=None, w=7, h=7, bitmask_path=None, frame_path=None):
"""Retrieve animation frame tilemaps from generated frame/bitmask data."""
if not path:
path = bitmask_path
if not path:
path = frame_path
if not path:
raise Exception("need at least one of path, bitmask_path or frame_path")
if not bitmask_path:
bitmask_path = os.path.join(os.path.split(path)[0], 'bitmask.asm')
if not frame_path:
frame_path = os.path.join(os.path.split(path)[0], 'frames.asm')
bitmask_lines = open(bitmask_path).readlines()
frame_lines = open(frame_path).readlines()
bitmask_length = w * h
bitmasks = []
bitmask = []
for line in bitmask_lines:
if '\tdb ' in line:
value = line.split('\tdb ')[1].strip().replace('%', '0b')
value = int(value, 0)
#print line.strip(), value, len(bitmasks), len(bitmask)
for bit in xrange(8):
bitmask += [(value >> bit) & 1]
if len(bitmask) >= bitmask_length:
bitmasks += [bitmask]
bitmask = []
break
if bitmask:
bitmasks += [bitmask]
frames = []
frame_labels = []
i = 0
for line in frame_lines:
if '\tdw ' in line:
frame_labels += [line.split('\tdw ')[1].strip()]
else:
for part in line.split():
part = part.strip()
if part in frame_labels:
frames += [(part, i)]
i += 1
results = []
for label, i in frames:
result = []
# get the bitmask and tile ids for each frame
# don't care if we read past bounds, so just read the rest of the file
values = []
for line in frame_lines[i:]:
if '\tdb ' in line:
values += line.split('\tdb ')[1].split(';')[0].split(',')
#print bitmasks
#print values[0]
#print int(values[0].replace('$', '0x'), 0)
bitmask = bitmasks[int(values[0].replace('$', '0x'), 0)]
tiles = values[1:]
k = 0
j = 0
for bit in bitmask:
if bit:
result += [int(tiles[k].replace('$', '0x'), 0)]
k += 1
else:
result += [j]
j += 1
results += [result]
return results
def get_animated_graphics(path, w=7, h=7, bitmask_path=None, frame_path=None):
frames = get_animation_frames(path, w, h, bitmask_path, frame_path)
new_path = path.replace('.animated.2bpp', '.2bpp')
tiles = gfx.get_tiles(bytearray(open(path, 'rb').read()))
new_tiles = tiles[:w * h]
for frame in frames:
for tile in frame:
new_tiles += [tiles[tile]]
new_graphic = gfx.connect(new_tiles)
print new_path, list(new_graphic)
open(new_path, 'wb').write(bytearray(new_graphic))
return new_path
def filepath_rules(filepath):
"""Infer attributes of certain graphics by their location in the filesystem."""
args = {}
filedir, filename = os.path.split(filepath)
if filedir.startswith('./'):
filedir = filedir[2:]
name, ext = os.path.splitext(filename)
if ext == '.lz':
name, ext = os.path.splitext(name)
pokemon_name = ''
if 'gfx/pokemon/' in filedir:
pokemon_name = filedir.split('/')[-1]
if pokemon_name.startswith('unown_'):
index = filedir.find(pokemon_name)
if index != -1:
filedir = filedir[:index + len('unown')] + filedir[index + len('unown_a'):]
if name == 'front' or name == 'front.animated':
args['pal_file'] = os.path.join(filedir, 'normal.pal')
args['pic'] = True
args['animate'] = True
elif name == 'back':
args['pal_file'] = os.path.join(filedir, 'normal.pal')
args['pic'] = True
elif 'gfx/trainers' in filedir:
args['pic'] = True
elif os.path.join(filedir, name) in pics:
args['pic'] = True
elif filedir == 'gfx/tilesets':
args['tileset'] = True
if args.get('pal_file'):
if os.path.exists(args['pal_file']):
args['palout'] = args['pal_file']
else:
del args['pal_file']
if args.get('pic'):
if ext == '.png':
w, h = gfx.png.Reader(filepath).asRGBA8()[:2]
w = min(w/8, h/8)
args['pic_dimensions'] = w, w
elif ext == '.2bpp':
if pokemon_name and name == 'front' or name == 'front.animated':
w, h = get_pokemon_dimensions(filepath.replace(ext, '.dimensions')) or (7, 7)
args['pic_dimensions'] = w, w
elif pokemon_name and name == 'back':
args['pic_dimensions'] = 6, 6
else:
args['pic_dimensions'] = 7, 7
if args.get('tileset'):
args['width'] = 128
return args
def to_1bpp(filename, **kwargs):
name, ext = os.path.splitext(filename)
if ext == '.1bpp': pass
elif ext == '.2bpp': gfx.export_2bpp_to_1bpp(filename, **kwargs)
elif ext == '.png': gfx.export_png_to_1bpp(filename, **kwargs)
elif ext == '.lz':
decompress(filename, **kwargs)
to_1bpp(name, **kwargs)
def to_2bpp(filename, **kwargs):
name, ext = os.path.splitext(filename)
if ext == '.1bpp': gfx.export_1bpp_to_2bpp(filename, **kwargs)
elif ext == '.2bpp': pass
elif ext == '.png': gfx.export_png_to_2bpp(filename, **kwargs)
elif ext == '.lz':
decompress(filename, **kwargs)
to_2bpp(name, **kwargs)
def to_png(filename, **kwargs):
name, ext = os.path.splitext(filename)
if ext == '.1bpp': gfx.export_1bpp_to_png(filename, **kwargs)
elif ext == '.2bpp' and name.endswith('.animated'):
w, h = kwargs.get('pic_dimensions') or (7, 7)
new_path = get_animated_graphics(filename, w=w, h=h)
return to_png(new_path, **kwargs)
elif ext == '.2bpp': gfx.export_2bpp_to_png(filename, **kwargs)
elif ext == '.png': pass
elif ext == '.lz':
decompress(filename, **kwargs)
to_png(name, **kwargs)
def compress(filename, **kwargs):
data = open(filename, 'rb').read()
lz_data = lz.Compressed(data).output
open(filename + '.lz', 'wb').write(bytearray(lz_data))
def decompress(filename, **kwargs):
lz_data = open(filename, 'rb').read()
data = lz.Decompressed(lz_data).output
name, ext = os.path.splitext(filename)
open(name, 'wb').write(bytearray(data))
methods = {
'2bpp': to_2bpp,
'1bpp': to_1bpp,
'png': to_png,
'lz': compress,
'unlz': decompress,
}
def main(method_name, filenames=None):
if filenames is None: filenames = []
for filename in filenames:
args = filepath_rules(filename)
method = methods.get(method_name)
if method:
method(filename, **args)
def get_args():
ap = argparse.ArgumentParser()
ap.add_argument('method_name')
ap.add_argument('filenames', nargs='*')
args = ap.parse_args()
return args
if __name__ == '__main__':
main(**get_args().__dict__)

View file

@ -1,491 +0,0 @@
#!/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()

View file

@ -1,25 +1,29 @@
/*
* Copyright © 2013 stag019 <stag019@gmail.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#define PROGRAM_NAME "pkmncompress" #define PROGRAM_NAME "pkmncompress"
#define USAGE_OPTS "infile.2bpp outfile.pic" #define USAGE_OPTS "[-h|--help] [-u|--uncompress] infile.2bpp outfile.pic"
#include "common.h" #include "common.h"
uint8_t compressed[15 * 15 * 0x10]; void parse_args(int argc, char *argv[], bool *uncomp) {
struct option long_options[] = {
{"uncompress", no_argument, 0, 'u'},
{"help", no_argument, 0, 'h'},
{0}
};
for (int opt; (opt = getopt_long(argc, argv, "uh", long_options)) != -1;) {
switch (opt) {
case 'u':
*uncomp = true;
break;
case 'h':
usage_exit(0);
break;
default:
usage_exit(1);
}
}
}
uint8_t output[15 * 15 * 0x10];
int cur_bit; int cur_bit;
int cur_byte; int cur_byte;
@ -28,7 +32,27 @@ void write_bit(int bit) {
cur_byte++; cur_byte++;
cur_bit = 0; cur_bit = 0;
} }
compressed[cur_byte] |= bit << (7 - cur_bit); output[cur_byte] |= bit << (7 - cur_bit);
}
int read_bit(uint8_t *data) {
if (cur_bit == -1) {
cur_byte++;
cur_bit = 7;
}
return (data[cur_byte] >> cur_bit--) & 1;
}
void transpose_tiles(uint8_t *data, int width) {
int size = width * width * 0x10;
uint8_t *transposed = xmalloc(size);
for (int i = 0; i < size; i++) {
int j = (i / 0x10) * width * 0x10;
j = (j % size) + 0x10 * (j / size) + (i % 0x10);
transposed[j] = data[i];
}
memcpy(data, transposed, size);
free(transposed);
} }
void compress_plane(uint8_t *plane, int width) { void compress_plane(uint8_t *plane, int width) {
@ -83,32 +107,26 @@ void write_data_packet(uint8_t *bit_groups, int n) {
} }
} }
int interpret_compress(uint8_t *plane1, uint8_t *plane2, int mode, int order, int width) { int interpret_compress(uint8_t *planes[2], int mode, int order, int width) {
int ram_size = width * width * 8; int ram_size = width * width * 8;
uint8_t *_plane1 = xmalloc(ram_size); uint8_t *rams[2] = {xmalloc(ram_size), xmalloc(ram_size)};
uint8_t *_plane2 = xmalloc(ram_size); memcpy(rams[0], planes[order], ram_size);
if (order) { memcpy(rams[1], planes[order ^ 1], ram_size);
memcpy(_plane1, plane2, ram_size); if (mode != 0) {
memcpy(_plane2, plane1, ram_size);
} else {
memcpy(_plane1, plane1, ram_size);
memcpy(_plane2, plane2, ram_size);
}
if (mode != 1) {
for (int i = 0; i < ram_size; i++) { for (int i = 0; i < ram_size; i++) {
_plane2[i] ^= _plane1[i]; rams[1][i] ^= rams[0][i];
} }
} }
compress_plane(_plane1, width); compress_plane(rams[0], width);
if (mode != 2) { if (mode != 1) {
compress_plane(_plane2, width); compress_plane(rams[1], width);
} }
cur_bit = 7; cur_bit = 7;
cur_byte = 0; cur_byte = 0;
memset(compressed, 0, COUNTOF(compressed)); memset(output, 0, COUNTOF(output));
compressed[0] = (width << 4) | width; output[0] = (width << 4) | width;
write_bit(order); write_bit(order);
uint8_t bit_groups[0x1000] = {0}; uint8_t bit_groups[15 * 4 * 15 * 8] = {0};
int index = 0; int index = 0;
for (int plane = 0; plane < 2; plane++) { for (int plane = 0; plane < 2; plane++) {
int type = 0; int type = 0;
@ -117,9 +135,18 @@ int interpret_compress(uint8_t *plane1, uint8_t *plane2, int mode, int order, in
for (int x = 0; x < width; x++) { for (int x = 0; x < width; x++) {
for (int bit = 0; bit < 8; bit += 2) { for (int bit = 0; bit < 8; bit += 2) {
for (int y = 0, byte = x * width * 8; y < width * 8; y++, byte++) { for (int y = 0, byte = x * width * 8; y < width * 8; y++, byte++) {
int bit_group = ((plane ? _plane2 : _plane1)[byte] >> (6 - bit)) & 3; int bit_group = (rams[plane][byte] >> (6 - bit)) & 3;
if (!bit_group) { if (bit_group) {
if (!type) { if (type == 0) {
write_bit(1);
} else if (type == 1) {
rle_encode_number(nums);
}
type = 2;
bit_groups[index++] = bit_group;
nums = 0;
} else {
if (type == 0) {
write_bit(0); write_bit(0);
} else if (type == 1) { } else if (type == 1) {
nums++; nums++;
@ -131,15 +158,6 @@ int interpret_compress(uint8_t *plane1, uint8_t *plane2, int mode, int order, in
type = 1; type = 1;
memset(bit_groups, 0, COUNTOF(bit_groups)); memset(bit_groups, 0, COUNTOF(bit_groups));
index = 0; index = 0;
} else {
if (!type) {
write_bit(1);
} else if (type == 1) {
rle_encode_number(nums);
}
type = 2;
bit_groups[index++] = bit_group;
nums = 0;
} }
} }
} }
@ -150,69 +168,20 @@ int interpret_compress(uint8_t *plane1, uint8_t *plane2, int mode, int order, in
write_data_packet(bit_groups, index); write_data_packet(bit_groups, index);
} }
if (!plane) { if (!plane) {
if (mode < 2) { if (mode == 0) {
write_bit(0); write_bit(0);
} else { } else {
write_bit(1); write_bit(1);
write_bit(mode - 2); write_bit(mode - 1);
} }
} }
} }
free(_plane1); free(rams[0]);
free(_plane2); free(rams[1]);
return (cur_byte + 1) * 8 + cur_bit; return (cur_byte + 1) * 8 + cur_bit;
} }
int compress(uint8_t *data, int width) { int get_width(long filesize) {
int ram_size = width * width * 8;
uint8_t *plane1 = xmalloc(ram_size);
uint8_t *plane2 = xmalloc(ram_size);
for (int i = 0; i < ram_size; i++) {
plane1[i] = data[i * 2];
plane2[i] = data[i * 2 + 1];
}
uint8_t current[COUNTOF(compressed)] = {0};
int compressed_size = -1;
for (int mode = 1; mode < 4; mode++) {
for (int order = 0; order < 2; order++) {
if (mode == 1 && order == 0) {
continue;
}
int new_size = interpret_compress(plane1, plane2, mode, order, width);
if (compressed_size == -1 || new_size < compressed_size) {
compressed_size = new_size;
memset(current, 0, COUNTOF(current));
memcpy(current, compressed, compressed_size / 8);
}
}
}
memset(compressed, 0, COUNTOF(compressed));
memcpy(compressed, current, compressed_size / 8);
free(plane1);
free(plane2);
return compressed_size / 8;
}
uint8_t *transpose_tiles(uint8_t *data, int width) {
int size = width * width * 0x10;
uint8_t *transposed = xmalloc(size);
for (int i = 0; i < size; i++) {
int j = (i / 0x10) * width * 0x10;
j = (j % size) + 0x10 * (j / size) + (i % 0x10);
transposed[j] = data[i];
}
free(data);
return transposed;
}
int main(int argc, char *argv[]) {
if (argc != 3) {
usage_exit(1);
}
long filesize;
uint8_t *data = read_u8(argv[1], &filesize);
int width = 0; int width = 0;
for (int w = 1; w < 16; w++) { for (int w = 1; w < 16; w++) {
if (filesize == w * w * 0x10) { if (filesize == w * w * 0x10) {
@ -223,10 +192,169 @@ int main(int argc, char *argv[]) {
if (!width) { if (!width) {
error_exit("Image is not a square, or is larger than 15x15 tiles"); error_exit("Image is not a square, or is larger than 15x15 tiles");
} }
return width;
}
data = transpose_tiles(data, width); int compress(uint8_t *data, long filesize) {
int compressed_size = compress(data, width); int width = get_width(filesize);
write_u8(argv[2], compressed, compressed_size); int ram_size = width * width * 8;
uint8_t *planes[2] = {xmalloc(ram_size), xmalloc(ram_size)};
transpose_tiles(data, width);
for (int i = 0; i < ram_size; i++) {
planes[0][i] = data[i * 2];
planes[1][i] = data[i * 2 + 1];
}
uint8_t current[COUNTOF(output)] = {0};
int compressed_size = -1;
for (int mode = 0; mode < 3; mode++) {
for (int order = 0; order < 2; order++) {
if (mode == 0 && order == 0) {
continue;
}
int new_size = interpret_compress(planes, mode, order, width);
if (compressed_size == -1 || new_size < compressed_size) {
compressed_size = new_size;
memset(current, 0, COUNTOF(current));
memcpy(current, output, compressed_size / 8);
}
}
}
memset(output, 0, COUNTOF(output));
memcpy(output, current, compressed_size / 8);
free(planes[0]);
free(planes[1]);
return compressed_size / 8;
}
int read_int(uint8_t *data, int count) {
int n = 0;
while (count--) {
n = (n << 1) | read_bit(data);
}
return n;
}
uint8_t *fill_plane(uint8_t *data, int width) {
static int table[0x10] = {
0x0001, 0x0003, 0x0007, 0x000F, 0x001F, 0x003F, 0x007F, 0x00FF,
0x01FF, 0x03FF, 0x07FF, 0x0FFF, 0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF,
};
int mode = read_bit(data);
int size = width * width * 0x20;
uint8_t *plane = xmalloc(size);
int len = 0;
while (len < size) {
if (mode) {
while (len < size) {
int bit_group = read_int(data, 2);
if (!bit_group) {
break;
}
plane[len++] = bit_group;
}
} else {
size_t w = 0;
while (read_bit(data)) {
w++;
}
if (w >= COUNTOF(table)) {
error_exit("Invalid compressed data");
}
int n = table[w] + read_int(data, w + 1);
while (len < size && n--) {
plane[len++] = 0;
}
}
mode ^= 1;
}
if (len > size) {
error_exit("Invalid compressed data");
}
uint8_t *ram = xmalloc(size);
len = 0;
for (int y = 0; y < width; y++) {
for (int x = 0; x < width * 8; x++) {
for (int i = 0; i < 4; i++) {
ram[len++] = plane[(y * 4 + i) * width * 8 + x];
}
}
}
for (int i = 0; i < size - 3; i += 4) {
ram[i / 4] = (ram[i] << 6) | (ram[i + 1] << 4) | (ram[i + 2] << 2) | ram[i + 3];
}
free(plane);
return ram;
}
void uncompress_plane(uint8_t *plane, int width) {
static int codes[2][0x10] = {
{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},
};
for (int x = 0; x < width * 8; x++) {
int bit = 0;
for (int y = 0; y < width; y++) {
int i = y * width * 8 + x;
int nybble_hi = (plane[i] >> 4) & 0xF;
int code_hi = codes[bit][nybble_hi];
bit = code_hi & 1;
int nybble_lo = plane[i] & 0xF;
int code_lo = codes[bit][nybble_lo];
bit = code_lo & 1;
plane[i] = (code_hi << 4) | code_lo;
}
}
}
int uncompress(uint8_t *data) {
cur_bit = 7;
int width = read_int(data, 4);
if (read_int(data, 4) != width) {
error_exit("Image is not a square");
}
int size = width * width * 8;
uint8_t *rams[2];
int order = read_bit(data);
rams[order] = fill_plane(data, width);
int mode = read_bit(data);
if (mode) {
mode += read_bit(data);
}
rams[order ^ 1] = fill_plane(data, width);
uncompress_plane(rams[order], width);
if (mode != 1) {
uncompress_plane(rams[order ^ 1], width);
}
if (mode != 0) {
for (int i = 0; i < size; i++) {
rams[order ^ 1][i] ^= rams[order][i];
}
}
for (int i = 0; i < size; i++) {
output[i * 2] = rams[0][i];
output[i * 2 + 1] = rams[1][i];
}
transpose_tiles(output, width);
free(rams[0]);
free(rams[1]);
return size * 2;
}
int main(int argc, char *argv[]) {
bool uncomp = false;
parse_args(argc, argv, &uncomp);
argc -= optind;
argv += optind;
if (argc < 1) {
usage_exit(1);
}
long filesize;
uint8_t *data = read_u8(argv[0], &filesize);
int output_size = uncomp ? uncompress(data) : compress(data, filesize);
write_u8(argv[1], output, output_size);
free(data); free(data);
return 0; return 0;

View file

@ -1 +0,0 @@
# A subset of https://github.com/pret/pokemon-reverse-engineering-tools

View file

@ -1,938 +0,0 @@
# -*- coding: utf-8 -*-
import os
import sys
import png
from math import sqrt, floor, ceil
import argparse
import operator
from lz import Compressed, Decompressed
def split(list_, interval):
"""
Split a list by length.
"""
for i in xrange(0, len(list_), interval):
j = min(i + interval, len(list_))
yield list_[i:j]
def hex_dump(data, length=0x10):
"""
just use hexdump -C
"""
margin = len('%x' % len(data))
output = []
address = 0
for line in split(data, length):
output += [
hex(address)[2:].zfill(margin) +
' | ' +
' '.join('%.2x' % byte for byte in line)
]
address += length
return '\n'.join(output)
def get_tiles(image):
"""
Split a 2bpp image into 8x8 tiles.
"""
return list(split(image, 0x10))
def connect(tiles):
"""
Combine 8x8 tiles into a 2bpp image.
"""
return [byte for tile in tiles for byte in tile]
def transpose(tiles, width=None):
"""
Transpose a tile arrangement along line y=-x.
00 01 02 03 04 05 00 06 0c 12 18 1e
06 07 08 09 0a 0b 01 07 0d 13 19 1f
0c 0d 0e 0f 10 11 <-> 02 08 0e 14 1a 20
12 13 14 15 16 17 03 09 0f 15 1b 21
18 19 1a 1b 1c 1d 04 0a 10 16 1c 22
1e 1f 20 21 22 23 05 0b 11 17 1d 23
00 01 02 03 00 04 08
04 05 06 07 <-> 01 05 09
08 09 0a 0b 02 06 0a
03 07 0b
"""
if width == None:
width = int(sqrt(len(tiles))) # assume square image
tiles = sorted(enumerate(tiles), key= lambda (i, tile): i % width)
return [tile for i, tile in tiles]
def transpose_tiles(image, width=None):
return connect(transpose(get_tiles(image), width))
def interleave(tiles, width):
"""
00 01 02 03 04 05 00 02 04 06 08 0a
06 07 08 09 0a 0b 01 03 05 07 09 0b
0c 0d 0e 0f 10 11 --> 0c 0e 10 12 14 16
12 13 14 15 16 17 0d 0f 11 13 15 17
18 19 1a 1b 1c 1d 18 1a 1c 1e 20 22
1e 1f 20 21 22 23 19 1b 1d 1f 21 23
"""
interleaved = []
left, right = split(tiles[::2], width), split(tiles[1::2], width)
for l, r in zip(left, right):
interleaved += l + r
return interleaved
def deinterleave(tiles, width):
"""
00 02 04 06 08 0a 00 01 02 03 04 05
01 03 05 07 09 0b 06 07 08 09 0a 0b
0c 0e 10 12 14 16 --> 0c 0d 0e 0f 10 11
0d 0f 11 13 15 17 12 13 14 15 16 17
18 1a 1c 1e 20 22 18 19 1a 1b 1c 1d
19 1b 1d 1f 21 23 1e 1f 20 21 22 23
"""
deinterleaved = []
rows = list(split(tiles, width))
for left, right in zip(rows[::2], rows[1::2]):
for l, r in zip(left, right):
deinterleaved += [l, r]
return deinterleaved
def interleave_tiles(image, width):
return connect(interleave(get_tiles(image), width))
def deinterleave_tiles(image, width):
return connect(deinterleave(get_tiles(image), width))
def condense_image_to_map(image, pic=0):
"""
Reduce an image of adjacent frames to an image containing a base frame and any unrepeated tiles.
Returns the new image and the corresponding tilemap used to reconstruct the input image.
If <pic> is 0, ignore the concept of frames. This behavior might be better off as another function.
"""
tiles = get_tiles(image)
new_tiles, tilemap = condense_tiles_to_map(tiles, pic)
new_image = connect(new_tiles)
return new_image, tilemap
def condense_tiles_to_map(tiles, pic=0):
"""
Reduce a sequence of tiles representing adjacent frames to a base frame and any unrepeated tiles.
Returns the new tiles and the corresponding tilemap used to reconstruct the input tile sequence.
If <pic> is 0, ignore the concept of frames. This behavior might be better off as another function.
"""
# Leave the first frame intact for pics.
new_tiles = tiles[:pic]
tilemap = range(pic)
for i, tile in enumerate(tiles[pic:]):
if tile not in new_tiles:
new_tiles.append(tile)
if pic:
# Match the first frame exactly where possible.
# This reduces the space needed to replace tiles in pic animations.
# For example, if a tile is repeated twice in the first frame,
# but at the same relative index as the second tile, use the second index.
# When creating a bitmask later, the second index would not require a replacement, but the first index would have.
pic_i = i % pic
if tile == new_tiles[pic_i]:
tilemap.append(pic_i)
else:
tilemap.append(new_tiles.index(tile))
else:
tilemap.append(new_tiles.index(tile))
return new_tiles, tilemap
def test_condense_tiles_to_map():
test = condense_tiles_to_map(list('abcadbae'))
if test != (list('abcde'), [0, 1, 2, 0, 3, 1, 0, 4]):
raise Exception(test)
test = condense_tiles_to_map(list('abcadbae'), 2)
if test != (list('abcde'), [0, 1, 2, 0, 3, 1, 0, 4]):
raise Exception(test)
test = condense_tiles_to_map(list('abcadbae'), 4)
if test != (list('abcade'), [0, 1, 2, 3, 4, 1, 0, 5]):
raise Exception(test)
test = condense_tiles_to_map(list('abcadbea'), 4)
if test != (list('abcade'), [0, 1, 2, 3, 4, 1, 5, 3]):
raise Exception(test)
def to_file(filename, data):
"""
Apparently open(filename, 'wb').write(bytearray(data)) won't work.
"""
file = open(filename, 'wb')
for byte in data:
file.write('%c' % byte)
file.close()
def decompress_file(filein, fileout=None):
image = bytearray(open(filein).read())
de = Decompressed(image)
if fileout == None:
fileout = os.path.splitext(filein)[0]
to_file(fileout, de.output)
def compress_file(filein, fileout=None):
image = bytearray(open(filein).read())
lz = Compressed(image)
if fileout == None:
fileout = filein + '.lz'
to_file(fileout, lz.output)
def bin_to_rgb(word):
red = word & 0b11111
word >>= 5
green = word & 0b11111
word >>= 5
blue = word & 0b11111
return (red, green, blue)
def convert_binary_pal_to_text_by_filename(filename):
pal = bytearray(open(filename).read())
return convert_binary_pal_to_text(pal)
def convert_binary_pal_to_text(pal):
output = ''
words = [hi * 0x100 + lo for lo, hi in zip(pal[::2], pal[1::2])]
for word in words:
red, green, blue = ['%.2d' % c for c in bin_to_rgb(word)]
output += '\tRGB ' + ', '.join((red, green, blue))
output += '\n'
return output
def read_rgb_macros(lines):
colors = []
for line in lines:
macro = line.split(" ")[0].strip()
if macro == 'RGB':
params = ' '.join(line.split(" ")[1:]).split(',')
red, green, blue = [int(v) for v in params]
colors += [[red, green, blue]]
return colors
def rewrite_binary_pals_to_text(filenames):
for filename in filenames:
pal_text = convert_binary_pal_to_text_by_filename(filename)
with open(filename, 'w') as out:
out.write(pal_text)
def flatten(planar):
"""
Flatten planar 2bpp image data into a quaternary pixel map.
"""
strips = []
for bottom, top in split(planar, 2):
bottom = bottom
top = top
strip = []
for i in xrange(7,-1,-1):
color = (
(bottom >> i & 1) +
(top *2 >> i & 2)
)
strip += [color]
strips += strip
return strips
def to_lines(image, width):
"""
Convert a tiled quaternary pixel map to lines of quaternary pixels.
"""
tile_width = 8
tile_height = 8
num_columns = width / tile_width
height = len(image) / width
lines = []
for cur_line in xrange(height):
tile_row = cur_line / tile_height
line = []
for column in xrange(num_columns):
anchor = (
num_columns * tile_row * tile_width * tile_height +
column * tile_width * tile_height +
cur_line % tile_height * tile_width
)
line += image[anchor : anchor + tile_width]
lines += [line]
return lines
def dmg2rgb(word):
"""
For PNGs.
"""
def shift(value):
while True:
yield value & (2**5 - 1)
value >>= 5
word = shift(word)
# distribution is less even w/ << 3
red, green, blue = [int(color * 8.25) for color in [word.next() for _ in xrange(3)]]
alpha = 255
return (red, green, blue, alpha)
def rgb_to_dmg(color):
"""
For PNGs.
"""
word = (color['r'] / 8)
word += (color['g'] / 8) << 5
word += (color['b'] / 8) << 10
return word
def pal_to_png(filename):
"""
Interpret a .pal file as a png palette.
"""
with open(filename) as rgbs:
colors = read_rgb_macros(rgbs.readlines())
a = 255
palette = []
for color in colors:
# even distribution over 000-255
r, g, b = [int(hue * 8.25) for hue in color]
palette += [(r, g, b, a)]
white = (255,255,255,255)
black = (000,000,000,255)
if white not in palette and len(palette) < 4:
palette = [white] + palette
if black not in palette and len(palette) < 4:
palette = palette + [black]
return palette
def png_to_rgb(palette):
"""
Convert a png palette to rgb macros.
"""
output = ''
for color in palette:
r, g, b = [color[c] / 8 for c in 'rgb']
output += '\tRGB ' + ', '.join(['%.2d' % hue for hue in (r, g, b)])
output += '\n'
return output
def read_filename_arguments(filename):
"""
Infer graphics conversion arguments given a filename.
Arguments are separated with '.'.
"""
parsed_arguments = {}
int_arguments = {
'w': 'width',
'h': 'height',
't': 'tile_padding',
}
arguments = os.path.splitext(filename)[0].lstrip('.').split('.')[1:]
for argument in arguments:
# Check for integer arguments first (i.e. "w128").
arg = argument[0]
param = argument[1:]
if param.isdigit():
arg = int_arguments.get(arg, False)
if arg:
parsed_arguments[arg] = int(param)
elif argument == 'arrange':
parsed_arguments['norepeat'] = True
parsed_arguments['tilemap'] = True
# Pic dimensions (i.e. "6x6").
elif 'x' in argument and any(map(str.isdigit, argument)):
w, h = argument.split('x')
if w.isdigit() and h.isdigit():
parsed_arguments['pic_dimensions'] = (int(w), int(h))
else:
parsed_arguments[argument] = True
return parsed_arguments
def export_2bpp_to_png(filein, fileout=None, pal_file=None, height=0, width=0, tile_padding=0, pic_dimensions=None, **kwargs):
if fileout == None:
fileout = os.path.splitext(filein)[0] + '.png'
image = open(filein, 'rb').read()
arguments = {
'width': width,
'height': height,
'pal_file': pal_file,
'tile_padding': tile_padding,
'pic_dimensions': pic_dimensions,
}
arguments.update(read_filename_arguments(filein))
if pal_file == None:
if os.path.exists(os.path.splitext(fileout)[0]+'.pal'):
arguments['pal_file'] = os.path.splitext(fileout)[0]+'.pal'
result = convert_2bpp_to_png(image, **arguments)
width, height, palette, greyscale, bitdepth, px_map = result
w = png.Writer(
width,
height,
palette=palette,
compression=9,
greyscale=greyscale,
bitdepth=bitdepth
)
with open(fileout, 'wb') as f:
w.write(f, px_map)
def convert_2bpp_to_png(image, **kwargs):
"""
Convert a planar 2bpp graphic to png.
"""
image = bytearray(image)
pad_color = bytearray([0])
width = kwargs.get('width', 0)
height = kwargs.get('height', 0)
tile_padding = kwargs.get('tile_padding', 0)
pic_dimensions = kwargs.get('pic_dimensions', None)
pal_file = kwargs.get('pal_file', None)
interleave = kwargs.get('interleave', False)
# Width must be specified to interleave.
if interleave and width:
image = interleave_tiles(image, width / 8)
# Pad the image by a given number of tiles if asked.
image += pad_color * 0x10 * tile_padding
# Some images are transposed in blocks.
if pic_dimensions:
w, h = pic_dimensions
if not width: width = w * 8
pic_length = w * h * 0x10
trailing = len(image) % pic_length
pic = []
for i in xrange(0, len(image) - trailing, pic_length):
pic += transpose_tiles(image[i:i+pic_length], h)
image = bytearray(pic) + image[len(image) - trailing:]
# Pad out trailing lines.
image += pad_color * 0x10 * ((w - (len(image) / 0x10) % h) % w)
def px_length(img):
return len(img) * 4
def tile_length(img):
return len(img) * 4 / (8*8)
if width and height:
tile_width = width / 8
more_tile_padding = (tile_width - (tile_length(image) % tile_width or tile_width))
image += pad_color * 0x10 * more_tile_padding
elif width and not height:
tile_width = width / 8
more_tile_padding = (tile_width - (tile_length(image) % tile_width or tile_width))
image += pad_color * 0x10 * more_tile_padding
height = px_length(image) / width
elif height and not width:
tile_height = height / 8
more_tile_padding = (tile_height - (tile_length(image) % tile_height or tile_height))
image += pad_color * 0x10 * more_tile_padding
width = px_length(image) / height
# at least one dimension should be given
if width * height != px_length(image):
# look for possible combos of width/height that would form a rectangle
matches = []
# Height need not be divisible by 8, but width must.
# See pokered gfx/minimize_pic.1bpp.
for w in range(8, px_length(image) / 2 + 1, 8):
h = px_length(image) / w
if w * h == px_length(image):
matches += [(w, h)]
# go for the most square image
if len(matches):
width, height = sorted(matches, key= lambda (w, h): (h % 8 != 0, w + h))[0] # favor height
else:
raise Exception, 'Image can\'t be divided into tiles (%d px)!' % (px_length(image))
# convert tiles to lines
lines = to_lines(flatten(image), width)
if pal_file == None:
palette = None
greyscale = True
bitdepth = 2
px_map = [[3 - pixel for pixel in line] for line in lines]
else: # gbc color
palette = pal_to_png(pal_file)
greyscale = False
bitdepth = 8
px_map = [[pixel for pixel in line] for line in lines]
return width, height, palette, greyscale, bitdepth, px_map
def get_pic_animation(tmap, w, h):
"""
Generate pic animation data from a combined tilemap of each frame.
"""
frame_text = ''
bitmask_text = ''
frames = list(split(tmap, w * h))
base = frames.pop(0)
bitmasks = []
for i in xrange(len(frames)):
frame_text += '\tdw .frame{}\n'.format(i + 1)
for i, frame in enumerate(frames):
bitmask = map(operator.ne, frame, base)
if bitmask not in bitmasks:
bitmasks.append(bitmask)
which_bitmask = bitmasks.index(bitmask)
mask = iter(bitmask)
masked_frame = filter(lambda _: mask.next(), frame)
frame_text += '.frame{}\n'.format(i + 1)
frame_text += '\tdb ${:02x} ; bitmask\n'.format(which_bitmask)
if masked_frame:
frame_text += '\tdb {}\n'.format(', '.join(
map('${:02x}'.format, masked_frame)
))
for i, bitmask in enumerate(bitmasks):
bitmask_text += '; {}\n'.format(i)
for byte in split(bitmask, 8):
byte = int(''.join(map(int.__repr__, reversed(byte))), 2)
bitmask_text += '\tdb %{:08b}\n'.format(byte)
return frame_text, bitmask_text
def export_png_to_2bpp(filein, fileout=None, palout=None, **kwargs):
arguments = {
'tile_padding': 0,
'pic_dimensions': None,
'animate': False,
'stupid_bitmask_hack': [],
}
arguments.update(kwargs)
arguments.update(read_filename_arguments(filein))
image, arguments = png_to_2bpp(filein, **arguments)
if fileout == None:
fileout = os.path.splitext(filein)[0] + '.2bpp'
to_file(fileout, image)
tmap = arguments.get('tmap')
if tmap != None and arguments['animate'] and arguments['pic_dimensions']:
# Generate pic animation data.
frame_text, bitmask_text = get_pic_animation(tmap, *arguments['pic_dimensions'])
frames_path = os.path.join(os.path.split(fileout)[0], 'frames.asm')
with open(frames_path, 'w') as out:
out.write(frame_text)
bitmask_path = os.path.join(os.path.split(fileout)[0], 'bitmask.asm')
# The following Pokemon have a bitmask dummied out.
for exception in arguments['stupid_bitmask_hack']:
if exception in bitmask_path:
bitmasks = bitmask_text.split(';')
bitmasks[-1] = bitmasks[-1].replace('1', '0')
bitmask_text = ';'.join(bitmasks)
with open(bitmask_path, 'w') as out:
out.write(bitmask_text)
elif tmap != None and arguments.get('tilemap', False):
tilemap_path = os.path.splitext(fileout)[0] + '.tilemap'
to_file(tilemap_path, tmap)
palette = arguments.get('palette')
if palout == None:
palout = os.path.splitext(fileout)[0] + '.pal'
export_palette(palette, palout)
def get_image_padding(width, height, wstep=8, hstep=8):
padding = {
'left': 0,
'right': 0,
'top': 0,
'bottom': 0,
}
if width % wstep and width >= wstep:
pad = float(width % wstep) / 2
padding['left'] = int(ceil(pad))
padding['right'] = int(floor(pad))
if height % hstep and height >= hstep:
pad = float(height % hstep) / 2
padding['top'] = int(ceil(pad))
padding['bottom'] = int(floor(pad))
return padding
def png_to_2bpp(filein, **kwargs):
"""
Convert a png image to planar 2bpp.
"""
arguments = {
'tile_padding': 0,
'pic_dimensions': False,
'interleave': False,
'norepeat': False,
'tilemap': False,
}
arguments.update(kwargs)
if type(filein) is str:
filein = open(filein)
assert type(filein) is file
width, height, rgba, info = png.Reader(filein).asRGBA8()
# png.Reader returns flat pixel data. Nested is easier to work with
len_px = len('rgba')
image = []
palette = []
for line in rgba:
newline = []
for px in xrange(0, len(line), len_px):
color = dict(zip('rgba', line[px:px+len_px]))
if color not in palette:
if len(palette) < 4:
palette += [color]
else:
# TODO Find the nearest match
print 'WARNING: %s: Color %s truncated to' % (filein, color),
color = sorted(palette, key=lambda x: sum(x.values()))[0]
print color
newline += [color]
image += [newline]
assert len(palette) <= 4, '%s: palette should be 4 colors, is really %d (%s)' % (filein, len(palette), palette)
# Pad out smaller palettes with greyscale colors
greyscale = {
'black': { 'r': 0x00, 'g': 0x00, 'b': 0x00, 'a': 0xff },
'grey': { 'r': 0x55, 'g': 0x55, 'b': 0x55, 'a': 0xff },
'gray': { 'r': 0xaa, 'g': 0xaa, 'b': 0xaa, 'a': 0xff },
'white': { 'r': 0xff, 'g': 0xff, 'b': 0xff, 'a': 0xff },
}
preference = 'white', 'black', 'grey', 'gray'
for hue in map(greyscale.get, preference):
if len(palette) >= 4:
break
if hue not in palette:
palette += [hue]
palette.sort(key=lambda x: sum(x.values()))
# Game Boy palette order
palette.reverse()
# Map pixels to quaternary color ids
padding = get_image_padding(width, height)
width += padding['left'] + padding['right']
height += padding['top'] + padding['bottom']
pad = bytearray([0])
qmap = []
qmap += pad * width * padding['top']
for line in image:
qmap += pad * padding['left']
for color in line:
qmap += [palette.index(color)]
qmap += pad * padding['right']
qmap += pad * width * padding['bottom']
# Graphics are stored in tiles instead of lines
tile_width = 8
tile_height = 8
num_columns = max(width, tile_width) / tile_width
num_rows = max(height, tile_height) / tile_height
image = []
for row in xrange(num_rows):
for column in xrange(num_columns):
# Split it up into strips to convert to planar data
for strip in xrange(min(tile_height, height)):
anchor = (
row * num_columns * tile_width * tile_height +
column * tile_width +
strip * width
)
line = qmap[anchor : anchor + tile_width]
bottom, top = 0, 0
for bit, quad in enumerate(line):
bottom += (quad & 1) << (7 - bit)
top += (quad /2 & 1) << (7 - bit)
image += [bottom, top]
dim = arguments['pic_dimensions']
if dim:
if type(dim) in (tuple, list):
w, h = dim
else:
# infer dimensions based on width.
w = width / tile_width
h = height / tile_height
if h % w == 0:
h = w
tiles = get_tiles(image)
pic_length = w * h
tile_width = width / 8
trailing = len(tiles) % pic_length
new_image = []
for block in xrange(len(tiles) / pic_length):
offset = (h * tile_width) * ((block * w) / tile_width) + ((block * w) % tile_width)
pic = []
for row in xrange(h):
index = offset + (row * tile_width)
pic += tiles[index:index + w]
new_image += transpose(pic, w)
new_image += tiles[len(tiles) - trailing:]
image = connect(new_image)
# Remove any tile padding used to make the png rectangular.
image = image[:len(image) - arguments['tile_padding'] * 0x10]
tmap = None
if arguments['interleave']:
image = deinterleave_tiles(image, num_columns)
if arguments['pic_dimensions']:
image, tmap = condense_image_to_map(image, w * h)
elif arguments['norepeat']:
image, tmap = condense_image_to_map(image)
if not arguments['tilemap']:
tmap = None
arguments.update({ 'palette': palette, 'tmap': tmap, })
return image, arguments
def export_palette(palette, filename):
"""
Export a palette from png to rgb macros in a .pal file.
"""
if os.path.exists(filename):
# Pic palettes are 2 colors (black/white are added later).
with open(filename) as rgbs:
colors = read_rgb_macros(rgbs.readlines())
if len(colors) == 2:
palette = palette[1:3]
text = png_to_rgb(palette)
with open(filename, 'w') as out:
out.write(text)
def png_to_lz(filein):
name = os.path.splitext(filein)[0]
export_png_to_2bpp(filein)
image = open(name+'.2bpp', 'rb').read()
to_file(name+'.2bpp'+'.lz', Compressed(image).output)
def convert_2bpp_to_1bpp(data):
"""
Convert planar 2bpp image data to 1bpp. Assume images are two colors.
"""
return data[::2]
def convert_1bpp_to_2bpp(data):
"""
Convert 1bpp image data to planar 2bpp (black/white).
"""
output = []
for i in data:
output += [i, i]
return output
def export_2bpp_to_1bpp(filename):
name, extension = os.path.splitext(filename)
image = open(filename, 'rb').read()
image = convert_2bpp_to_1bpp(image)
to_file(name + '.1bpp', image)
def export_1bpp_to_2bpp(filename):
name, extension = os.path.splitext(filename)
image = open(filename, 'rb').read()
image = convert_1bpp_to_2bpp(image)
to_file(name + '.2bpp', image)
def export_1bpp_to_png(filename, fileout=None):
if fileout == None:
fileout = os.path.splitext(filename)[0] + '.png'
arguments = read_filename_arguments(filename)
image = open(filename, 'rb').read()
image = convert_1bpp_to_2bpp(image)
result = convert_2bpp_to_png(image, **arguments)
width, height, palette, greyscale, bitdepth, px_map = result
w = png.Writer(width, height, palette=palette, compression=9, greyscale=greyscale, bitdepth=bitdepth)
with open(fileout, 'wb') as f:
w.write(f, px_map)
def export_png_to_1bpp(filename, fileout=None):
if fileout == None:
fileout = os.path.splitext(filename)[0] + '.1bpp'
arguments = read_filename_arguments(filename)
image = png_to_1bpp(filename, **arguments)
to_file(fileout, image)
def png_to_1bpp(filename, **kwargs):
image, kwargs = png_to_2bpp(filename, **kwargs)
return convert_2bpp_to_1bpp(image)
def convert_to_2bpp(filenames=[]):
for filename in filenames:
filename, name, extension = try_decompress(filename)
if extension == '.1bpp':
export_1bpp_to_2bpp(filename)
elif extension == '.2bpp':
pass
elif extension == '.png':
export_png_to_2bpp(filename)
else:
raise Exception, "Don't know how to convert {} to 2bpp!".format(filename)
def convert_to_1bpp(filenames=[]):
for filename in filenames:
filename, name, extension = try_decompress(filename)
if extension == '.1bpp':
pass
elif extension == '.2bpp':
export_2bpp_to_1bpp(filename)
elif extension == '.png':
export_png_to_1bpp(filename)
else:
raise Exception, "Don't know how to convert {} to 1bpp!".format(filename)
def convert_to_png(filenames=[]):
for filename in filenames:
filename, name, extension = try_decompress(filename)
if extension == '.1bpp':
export_1bpp_to_png(filename)
elif extension == '.2bpp':
export_2bpp_to_png(filename)
elif extension == '.png':
pass
else:
raise Exception, "Don't know how to convert {} to png!".format(filename)
def compress(filenames=[]):
for filename in filenames:
data = open(filename, 'rb').read()
lz_data = Compressed(data).output
to_file(filename + '.lz', lz_data)
def decompress(filenames=[]):
for filename in filenames:
name, extension = os.path.splitext(filename)
lz_data = open(filename, 'rb').read()
data = Decompressed(lz_data).output
to_file(name, data)
def try_decompress(filename):
"""
Try to decompress a graphic when determining the filetype.
This skips the manual unlz step when attempting
to convert lz-compressed graphics to png.
"""
name, extension = os.path.splitext(filename)
if extension == '.lz':
decompress([filename])
filename = name
name, extension = os.path.splitext(filename)
return filename, name, extension
def main():
ap = argparse.ArgumentParser()
ap.add_argument('mode')
ap.add_argument('filenames', nargs='*')
args = ap.parse_args()
method = {
'2bpp': convert_to_2bpp,
'1bpp': convert_to_1bpp,
'png': convert_to_png,
'lz': compress,
'unlz': decompress,
}.get(args.mode, None)
if method == None:
raise Exception, "Unknown conversion method!"
method(args.filenames)
if __name__ == "__main__":
main()

View file

@ -1,580 +0,0 @@
# -*- coding: utf-8 -*-
"""
Pokemon Crystal data de/compression.
"""
"""
A rundown of Pokemon Crystal's compression scheme:
Control commands occupy bits 5-7.
Bits 0-4 serve as the first parameter <n> for each command.
"""
lz_commands = {
'literal': 0, # n values for n bytes
'iterate': 1, # one value for n bytes
'alternate': 2, # alternate two values for n bytes
'blank': 3, # zero for n bytes
}
"""
Repeater commands repeat any data that was just decompressed.
They take an additional signed parameter <s> to mark a relative starting point.
These wrap around (positive from the start, negative from the current position).
"""
lz_commands.update({
'repeat': 4, # n bytes starting from s
'flip': 5, # n bytes in reverse bit order starting from s
'reverse': 6, # n bytes backwards starting from s
})
"""
The long command is used when 5 bits aren't enough. Bits 2-4 contain a new control code.
Bits 0-1 are appended to a new byte as 8-9, allowing a 10-bit parameter.
"""
lz_commands.update({
'long': 7, # n is now 10 bits for a new control code
})
max_length = 1 << 10 # can't go higher than 10 bits
lowmax = 1 << 5 # standard 5-bit param
"""
If 0xff is encountered instead of a command, decompression ends.
"""
lz_end = 0xff
bit_flipped = [
sum(((byte >> i) & 1) << (7 - i) for i in xrange(8))
for byte in xrange(0x100)
]
class Compressed:
"""
Usage:
lz = Compressed(data).output
or
lz = Compressed().compress(data)
or
c = Compressed()
c.data = data
lz = c.compress()
There are some issues with reproducing the target compressor.
Some notes are listed here:
- the criteria for detecting a lookback is inconsistent
- sometimes lookbacks that are mostly 0s are pruned, sometimes not
- target appears to skip ahead if it can use a lookback soon, stopping the current command short or in some cases truncating it with literals.
- this has been implemented, but the specifics are unknown
- self.min_scores: It's unknown if blank's minimum score should be 1 or 2. Most likely it's 1, with some other hack to account for edge cases.
- may be related to the above
- target does not appear to compress backwards
"""
def __init__(self, *args, **kwargs):
self.min_scores = {
'blank': 1,
'iterate': 2,
'alternate': 3,
'repeat': 3,
'reverse': 3,
'flip': 3,
}
self.preference = [
'repeat',
'blank',
'flip',
'reverse',
'iterate',
'alternate',
#'literal',
]
self.lookback_methods = 'repeat', 'reverse', 'flip'
self.__dict__.update({
'data': None,
'commands': lz_commands,
'debug': False,
'literal_only': False,
})
self.arg_names = 'data', 'commands', 'debug', 'literal_only'
self.__dict__.update(kwargs)
self.__dict__.update(dict(zip(self.arg_names, args)))
if self.data is not None:
self.compress()
def compress(self, data=None):
if data is not None:
self.data = data
self.data = list(bytearray(self.data))
self.indexes = {}
self.lookbacks = {}
for method in self.lookback_methods:
self.lookbacks[method] = {}
self.address = 0
self.end = len(self.data)
self.output = []
self.literal = None
while self.address < self.end:
if self.score():
self.do_literal()
self.do_winner()
else:
if self.literal == None:
self.literal = self.address
self.address += 1
self.do_literal()
self.output += [lz_end]
return self.output
def reset_scores(self):
self.scores = {}
self.offsets = {}
self.helpers = {}
for method in self.min_scores.iterkeys():
self.scores[method] = 0
def bit_flip(self, byte):
return bit_flipped[byte]
def do_literal(self):
if self.literal != None:
length = abs(self.address - self.literal)
start = min(self.literal, self.address + 1)
self.helpers['literal'] = self.data[start:start+length]
self.do_cmd('literal', length)
self.literal = None
def score(self):
self.reset_scores()
map(self.score_literal, ['iterate', 'alternate', 'blank'])
for method in self.lookback_methods:
self.scores[method], self.offsets[method] = self.find_lookback(method, self.address)
self.stop_short()
return any(
score
> self.min_scores[method] + int(score > lowmax)
for method, score in self.scores.iteritems()
)
def stop_short(self):
"""
If a lookback is close, reduce the scores of other commands.
"""
best_method, best_score = max(
self.scores.items(),
key = lambda x: (
x[1],
-self.preference.index(x[0])
)
)
for method in self.lookback_methods:
min_score = self.min_scores[method]
for address in xrange(self.address+1, self.address+best_score):
length, index = self.find_lookback(method, address)
if length > max(min_score, best_score):
# BUG: lookbacks can reduce themselves. This appears to be a bug in the target also.
for m, score in self.scores.items():
self.scores[m] = min(score, address - self.address)
def read(self, address=None):
if address is None:
address = self.address
if 0 <= address < len(self.data):
return self.data[address]
return None
def find_all_lookbacks(self):
for method in self.lookback_methods:
for address, byte in enumerate(self.data):
self.find_lookback(method, address)
def find_lookback(self, method, address=None):
"""Temporarily stubbed, because the real function doesn't run in polynomial time."""
return 0, None
def broken_find_lookback(self, method, address=None):
if address is None:
address = self.address
existing = self.lookbacks.get(method, {}).get(address)
if existing != None:
return existing
lookback = 0, None
# Better to not carelessly optimize at the moment.
"""
if address < 2:
return lookback
"""
byte = self.read(address)
if byte is None:
return lookback
direction, mutate = {
'repeat': ( 1, int),
'reverse': (-1, int),
'flip': ( 1, self.bit_flip),
}[method]
# Doesn't seem to help
"""
if mutate == self.bit_flip:
if byte == 0:
self.lookbacks[method][address] = lookback
return lookback
"""
data_len = len(self.data)
is_two_byte_index = lambda index: int(index < address - 0x7f)
for index in self.get_indexes(mutate(byte)):
if index >= address:
break
old_length, old_index = lookback
if direction == 1:
if old_length > data_len - index: break
else:
if old_length > index: continue
if self.read(index) in [None]: continue
length = 1 # we know there's at least one match, or we wouldn't be checking this index
while 1:
this_byte = self.read(address + length)
that_byte = self.read(index + length * direction)
if that_byte == None or this_byte != mutate(that_byte):
break
length += 1
score = length - is_two_byte_index(index)
old_score = old_length - is_two_byte_index(old_index)
if score >= old_score or (score == old_score and length > old_length):
# XXX maybe avoid two-byte indexes when possible
if score >= lookback[0] - is_two_byte_index(lookback[1]):
lookback = length, index
self.lookbacks[method][address] = lookback
return lookback
def get_indexes(self, byte):
if not self.indexes.has_key(byte):
self.indexes[byte] = []
index = -1
while 1:
try:
index = self.data.index(byte, index + 1)
except ValueError:
break
self.indexes[byte].append(index)
return self.indexes[byte]
def score_literal(self, method):
address = self.address
compare = {
'blank': [0],
'iterate': [self.read(address)],
'alternate': [self.read(address), self.read(address + 1)],
}[method]
# XXX may or may not be correct
if method == 'alternate' and compare[0] == 0:
return
length = 0
while self.read(address + length) == compare[length % len(compare)]:
length += 1
self.scores[method] = length
self.helpers[method] = compare
def do_winner(self):
winners = filter(
lambda (method, score):
score
> self.min_scores[method] + int(score > lowmax),
self.scores.iteritems()
)
winners.sort(
key = lambda (method, score): (
-(score - self.min_scores[method] - int(score > lowmax)),
self.preference.index(method)
)
)
winner, score = winners[0]
length = min(score, max_length)
self.do_cmd(winner, length)
self.address += length
def do_cmd(self, cmd, length):
start_address = self.address
cmd_length = length - 1
output = []
if length > lowmax:
output.append(
(self.commands['long'] << 5)
+ (self.commands[cmd] << 2)
+ (cmd_length >> 8)
)
output.append(
cmd_length & 0xff
)
else:
output.append(
(self.commands[cmd] << 5)
+ cmd_length
)
self.helpers['blank'] = [] # quick hack
output += self.helpers.get(cmd, [])
if cmd in self.lookback_methods:
offset = self.offsets[cmd]
# Negative offsets are one byte.
# Positive offsets are two.
if 0 < start_address - offset - 1 <= 0x7f:
offset = (start_address - offset - 1) | 0x80
output += [offset]
else:
output += [offset / 0x100, offset % 0x100] # big endian
if self.debug:
print ' '.join(map(str, [
cmd, length, '\t',
' '.join(map('{:02x}'.format, output)),
self.data[start_address:start_address+length] if cmd in self.lookback_methods else '',
]))
self.output += output
class Decompressed:
"""
Interpret and decompress lz-compressed data, usually 2bpp.
"""
"""
Usage:
data = Decompressed(lz).output
or
data = Decompressed().decompress(lz)
or
d = Decompressed()
d.lz = lz
data = d.decompress()
To decompress from offset 0x80000 in a rom:
data = Decompressed(rom, start=0x80000).output
"""
lz = None
start = 0
commands = lz_commands
debug = False
arg_names = 'lz', 'start', 'commands', 'debug'
def __init__(self, *args, **kwargs):
self.__dict__.update(dict(zip(self.arg_names, args)))
self.__dict__.update(kwargs)
self.command_names = dict(map(reversed, self.commands.items()))
self.address = self.start
if self.lz is not None:
self.decompress()
if self.debug: print self.command_list()
def command_list(self):
"""
Print a list of commands that were used. Useful for debugging.
"""
text = ''
output_address = 0
for name, attrs in self.used_commands:
length = attrs['length']
address = attrs['address']
offset = attrs['offset']
direction = attrs['direction']
text += '{2:03x} {0}: {1}'.format(name, length, output_address)
text += '\t' + ' '.join(
'{:02x}'.format(int(byte))
for byte in self.lz[ address : address + attrs['cmd_length'] ]
)
if offset is not None:
repeated_data = self.output[ offset : offset + length * direction : direction ]
if name == 'flip':
repeated_data = map(bit_flipped.__getitem__, repeated_data)
text += ' [' + ' '.join(map('{:02x}'.format, repeated_data)) + ']'
text += '\n'
output_address += length
return text
def decompress(self, lz=None):
if lz is not None:
self.lz = lz
self.lz = bytearray(self.lz)
self.used_commands = []
self.output = []
while 1:
cmd_address = self.address
self.offset = None
self.direction = None
if (self.byte == lz_end):
self.next()
break
self.cmd = (self.byte & 0b11100000) >> 5
if self.cmd_name == 'long':
# 10-bit length
self.cmd = (self.byte & 0b00011100) >> 2
self.length = (self.next() & 0b00000011) * 0x100
self.length += self.next() + 1
else:
# 5-bit length
self.length = (self.next() & 0b00011111) + 1
self.__class__.__dict__[self.cmd_name](self)
self.used_commands += [(
self.cmd_name,
{
'length': self.length,
'address': cmd_address,
'offset': self.offset,
'cmd_length': self.address - cmd_address,
'direction': self.direction,
}
)]
# Keep track of the data we just decompressed.
self.compressed_data = self.lz[self.start : self.address]
@property
def byte(self):
return self.lz[ self.address ]
def next(self):
byte = self.byte
self.address += 1
return byte
@property
def cmd_name(self):
return self.command_names.get(self.cmd)
def get_offset(self):
if self.byte >= 0x80: # negative
# negative
offset = self.next() & 0x7f
offset = len(self.output) - offset - 1
else:
# positive
offset = self.next() * 0x100
offset += self.next()
self.offset = offset
def literal(self):
"""
Copy data directly.
"""
self.output += self.lz[ self.address : self.address + self.length ]
self.address += self.length
def iterate(self):
"""
Write one byte repeatedly.
"""
self.output += [self.next()] * self.length
def alternate(self):
"""
Write alternating bytes.
"""
alts = [self.next(), self.next()]
self.output += [ alts[x & 1] for x in xrange(self.length) ]
def blank(self):
"""
Write zeros.
"""
self.output += [0] * self.length
def flip(self):
"""
Repeat flipped bytes from output.
Example: 11100100 -> 00100111
"""
self._repeat(table=bit_flipped)
def reverse(self):
"""
Repeat reversed bytes from output.
"""
self._repeat(direction=-1)
def repeat(self):
"""
Repeat bytes from output.
"""
self._repeat()
def _repeat(self, direction=1, table=None):
self.get_offset()
self.direction = direction
# Note: appends must be one at a time (this way, repeats can draw from themselves if required)
for i in xrange(self.length):
byte = self.output[ self.offset + i * direction ]
self.output.append( table[byte] if table else byte )

File diff suppressed because it is too large Load diff