summaryrefslogtreecommitdiff
path: root/extras/gfx.py
diff options
context:
space:
mode:
authorBryan Bishop <kanzure@gmail.com>2013-06-25 23:48:44 -0500
committerBryan Bishop <kanzure@gmail.com>2013-06-25 23:48:44 -0500
commitbd6a98531753805fda06f1261a99ed36ff20adbc (patch)
tree7c2efa2ab89eea8db200db1c80eb70db0e5bf45d /extras/gfx.py
parent7da503ac3385335bb9fa57f1fbdaaec34b9ef1f5 (diff)
parent78ee534e25fc7ba78469e8d8243afeb3bd14a1a3 (diff)
Merge branch 'gfx-fixes-again' into master.
Various whitespace and formatting changes.
Diffstat (limited to 'extras/gfx.py')
-rw-r--r--extras/gfx.py79
1 files changed, 39 insertions, 40 deletions
diff --git a/extras/gfx.py b/extras/gfx.py
index 22ecc7cc6..1504933a4 100644
--- a/extras/gfx.py
+++ b/extras/gfx.py
@@ -27,23 +27,23 @@ def mkdir_p(path):
else: raise
-def hex_dump(input, debug = True):
+def hex_dump(input, debug=True):
"""
Display hex dump in rows of 16 bytes.
"""
-
+
dump = ''
output = ''
stream = ''
address = 0x00
margin = 2 + len(hex(len(input))[2:])
-
+
# dump
for byte in input:
cool = hex(byte)[2:].zfill(2)
dump += cool + ' '
if debug: stream += cool
-
+
# convenient for testing quick edits in bgb
if debug: output += stream + '\n'
@@ -52,17 +52,16 @@ def hex_dump(input, debug = True):
chars_per_byte = 3 # '__ '
chars_per_line = bytes_per_line * chars_per_byte
num_lines = int(ceil(float(len(dump)) / float(chars_per_line)))
-
+
# top
# margin
for char in range(margin):
output += ' '
- #
for byte in range(bytes_per_line):
output += hex(byte)[2:].zfill(2) + ' '
output = output[:-1] # last space
-
+
# print hex
for line in range(num_lines):
# address
@@ -72,7 +71,7 @@ def hex_dump(input, debug = True):
end = chars_per_line + start - 1 # ignore last space
output += dump[start:end]
address += 0x10
-
+
return output
@@ -83,7 +82,7 @@ def get_tiles(image):
tiles = []
tile = []
bytes_per_tile = 16
-
+
cur_byte = 0
for byte in image:
# build tile
@@ -113,7 +112,7 @@ def transpose(tiles):
"""
Transpose a tile arrangement along line y=x.
"""
-
+
# horizontal <-> vertical
# 00 01 02 03 04 05 00 06 0c 12 18 1e
# 06 07 08 09 0a 0b 01 07 0d 13 19 1f
@@ -122,7 +121,7 @@ def transpose(tiles):
# 18 19 1a 1b 1c 1d 04 0a 10 16 1c 22
# 1e 1f 20 21 22 23 05 0b 11 17 1d 23
# etc
-
+
flipped = []
t = 0 # which tile we're on
w = int(sqrt(len(tiles))) # assume square image
@@ -196,18 +195,18 @@ lowmax = 1 << 5 # standard 5-bit param
class Compressed:
-
+
"""
Compress 2bpp data.
"""
-
- def __init__(self, image = None, mode = 'horiz', size = None):
+
+ def __init__(self, image=None, mode='horiz', size=None):
assert image, 'need something to compress!'
image = list(image)
self.image = image
self.pic = []
self.animtiles = []
-
+
# only transpose pic (animtiles were never transposed in decompression)
if size != None:
for byte in range((size*size)*16):
@@ -221,7 +220,7 @@ class Compressed:
self.tiles = get_tiles(self.pic)
self.tiles = transpose(self.tiles)
self.pic = connect(self.tiles)
-
+
self.image = self.pic + self.animtiles
self.end = len(self.image)
@@ -326,15 +325,15 @@ class Compressed:
def scanRepeats(self):
"""
Works, but doesn't do flipped/reversed streams yet.
-
+
This takes up most of the compress time and only saves a few bytes
it might be more feasible to exclude it entirely.
"""
-
+
self.repeats = []
self.flips = []
self.reverses = []
-
+
# make a 5-letter word list of the sequence
letters = 5 # how many bytes it costs to use a repeat over a literal
# any shorter and it's not worth the trouble
@@ -345,7 +344,7 @@ class Compressed:
for j in range(letters):
word.append( ord(self.image[i+j]) )
words.append((word, i))
-
+
zeros = []
for zero in range(letters):
zeros.append( 0 )
@@ -405,13 +404,13 @@ class Compressed:
else: # no more overlaps
buffer.append(match)
else: # last match, so there's nothing to check
- buffer.append(match)
+ buffer.append(match)
matches = buffer
# remove alternating sequences
buffer = []
for match in matches:
- for i in range(6 if letters > 6 else letters):
+ for i in range(6 if letters > 6 else letters):
if match[0][i] != match[0][i&1]:
buffer.append(match)
break
@@ -422,7 +421,7 @@ class Compressed:
def doRepeats(self):
"""doesn't output the right values yet"""
-
+
unusedrepeats = []
for repeat in self.repeats:
if self.address >= repeat[2]:
@@ -598,7 +597,7 @@ class Decompressed:
data can be fed in from rom if [start] is specified
"""
- def __init__(self, lz = None, mode = None, size = None, start = 0):
+ def __init__(self, lz=None, mode=None, size=None, start=0):
# todo: play nice with Compressed
assert lz, 'need something to compress!'
@@ -734,7 +733,7 @@ class Decompressed:
def doFlip(self):
"""
Repeat flipped bytes from 2bpp output.
-
+
eg 11100100 -> 00100111
quat 3 2 1 0 -> 0 2 1 3
"""
@@ -831,7 +830,7 @@ unowns = 0x124000
num_unowns = 26
unown_dex = 201
-def decompress_monster_by_id(id = 0, type = front):
+def decompress_monster_by_id(id=0, type=front):
# no unowns here
if id + 1 == unown_dex: return None
# get size
@@ -847,7 +846,7 @@ def decompress_monster_by_id(id = 0, type = front):
monster = Decompressed(rom, 'vert', size, address)
return monster
-def decompress_monsters(type = front):
+def decompress_monsters(type=front):
for id in range(num_monsters):
# decompress
monster = decompress_monster_by_id(id, type)
@@ -865,7 +864,7 @@ def decompress_monsters(type = front):
to_file(folder+filename, monster.pic)
-def decompress_unown_by_id(letter, type = front):
+def decompress_unown_by_id(letter, type=front):
# get size
if type == front:
size = sizes[unown_dex-1]
@@ -879,7 +878,7 @@ def decompress_unown_by_id(letter, type = front):
unown = Decompressed(rom, 'vert', size, address)
return unown
-def decompress_unowns(type = front):
+def decompress_unowns(type=front):
for letter in range(num_unowns):
# decompress
unown = decompress_unown_by_id(letter, type)
@@ -993,7 +992,7 @@ def decompress_misc():
gfx = Decompressed( rom, mode, None, address )
to_file(filename, gfx.output)
-def decompress_all(debug = False):
+def decompress_all(debug=False):
"""
Decompress all known compressed data in baserom.
"""
@@ -1028,7 +1027,7 @@ def decompress_all(debug = False):
return
-def decompress_from_address(address, mode='horiz', filename = 'de.2bpp', size = None):
+def decompress_from_address(address, mode='horiz', filename='de.2bpp', size=None):
"""
Write decompressed data from an address to a 2bpp file.
"""
@@ -1036,7 +1035,7 @@ def decompress_from_address(address, mode='horiz', filename = 'de.2bpp', size =
to_file(filename, image.pic)
-def decompress_file(filein, fileout, mode = 'horiz', size = None):
+def decompress_file(filein, fileout, mode='horiz', size=None):
f = open(filein, 'rb')
image = f.read()
f.close()
@@ -1046,7 +1045,7 @@ def decompress_file(filein, fileout, mode = 'horiz', size = None):
to_file(fileout, de.pic)
-def compress_file(filein, fileout, mode = 'horiz'):
+def compress_file(filein, fileout, mode='horiz'):
f = open(filein, 'rb')
image = f.read()
f.close()
@@ -1099,7 +1098,7 @@ def hex_to_rgb(word):
blue = word & 0b11111
return (red, green, blue)
-def grab_palettes(address, length = 0x80):
+def grab_palettes(address, length=0x80):
output = ''
for word in range(length/2):
color = ord(rom[address+1])*0x100 + ord(rom[address])
@@ -1228,7 +1227,7 @@ def dmg2rgb(word):
blue = word & 0b11111
alpha = 255
return ((red<<3)+0b100, (green<<3)+0b100, (blue<<3)+0b100, alpha)
-
+
def rgb_to_dmg(color):
word = (color['r'] / 8)
word += (color['g'] / 8) << 5
@@ -1556,8 +1555,8 @@ def lz_to_png_by_file(filename):
def dump_tileset_pngs():
"""
- Convert .lz format tilesets into .png format tilesets.
-
+ Convert .lz format tilesets into .png format tilesets.
+
Also, leaves a bunch of wonderful .2bpp files everywhere for your amusement.
"""
for tileset_id in range(37):
@@ -1581,7 +1580,7 @@ def decompress_frontpic_anim(lz_file):
def expand_pic_palettes():
"""
Add white and black to palette files with fewer than 4 colors.
-
+
Pokemon Crystal only defines two colors for a pic palette to
save space, filling in black/white at runtime.
Instead of managing palette files of varying length, black
@@ -1601,7 +1600,7 @@ def expand_pic_palettes():
if __name__ == "__main__":
debug = False
-
+
argv = [None] * 5
for i, arg in enumerate(sys.argv):
argv[i] = arg
@@ -1667,7 +1666,7 @@ if __name__ == "__main__":
filein = argv[2]
fileout = argv[3]
compress_file(filein, fileout)
-
+
elif argv[1] == '2bpp-to-png':
to_png(argv[2])