remove extra whitespace in extras/gfx.py

This commit is contained in:
Bryan Bishop 2013-06-25 23:01:52 -05:00
parent cc1add094f
commit aade38c733
1 changed files with 27 additions and 28 deletions

View File

@ -31,19 +31,19 @@ def hex_dump(input, debug=True):
""" """
Display hex dump in rows of 16 bytes. Display hex dump in rows of 16 bytes.
""" """
dump = '' dump = ''
output = '' output = ''
stream = '' stream = ''
address = 0x00 address = 0x00
margin = 2 + len(hex(len(input))[2:]) margin = 2 + len(hex(len(input))[2:])
# dump # dump
for byte in input: for byte in input:
cool = hex(byte)[2:].zfill(2) cool = hex(byte)[2:].zfill(2)
dump += cool + ' ' dump += cool + ' '
if debug: stream += cool if debug: stream += cool
# convenient for testing quick edits in bgb # convenient for testing quick edits in bgb
if debug: output += stream + '\n' if debug: output += stream + '\n'
@ -52,17 +52,16 @@ def hex_dump(input, debug=True):
chars_per_byte = 3 # '__ ' chars_per_byte = 3 # '__ '
chars_per_line = bytes_per_line * chars_per_byte chars_per_line = bytes_per_line * chars_per_byte
num_lines = int(ceil(float(len(dump)) / float(chars_per_line))) num_lines = int(ceil(float(len(dump)) / float(chars_per_line)))
# top # top
# margin # margin
for char in range(margin): for char in range(margin):
output += ' ' output += ' '
#
for byte in range(bytes_per_line): for byte in range(bytes_per_line):
output += hex(byte)[2:].zfill(2) + ' ' output += hex(byte)[2:].zfill(2) + ' '
output = output[:-1] # last space output = output[:-1] # last space
# print hex # print hex
for line in range(num_lines): for line in range(num_lines):
# address # address
@ -72,7 +71,7 @@ def hex_dump(input, debug=True):
end = chars_per_line + start - 1 # ignore last space end = chars_per_line + start - 1 # ignore last space
output += dump[start:end] output += dump[start:end]
address += 0x10 address += 0x10
return output return output
@ -83,7 +82,7 @@ def get_tiles(image):
tiles = [] tiles = []
tile = [] tile = []
bytes_per_tile = 16 bytes_per_tile = 16
cur_byte = 0 cur_byte = 0
for byte in image: for byte in image:
# build tile # build tile
@ -113,7 +112,7 @@ def transpose(tiles):
""" """
Transpose a tile arrangement along line y=x. Transpose a tile arrangement along line y=x.
""" """
# horizontal <-> vertical # horizontal <-> vertical
# 00 01 02 03 04 05 00 06 0c 12 18 1e # 00 01 02 03 04 05 00 06 0c 12 18 1e
# 06 07 08 09 0a 0b 01 07 0d 13 19 1f # 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 # 18 19 1a 1b 1c 1d 04 0a 10 16 1c 22
# 1e 1f 20 21 22 23 05 0b 11 17 1d 23 # 1e 1f 20 21 22 23 05 0b 11 17 1d 23
# etc # etc
flipped = [] flipped = []
t = 0 # which tile we're on t = 0 # which tile we're on
w = int(sqrt(len(tiles))) # assume square image w = int(sqrt(len(tiles))) # assume square image
@ -196,18 +195,18 @@ lowmax = 1 << 5 # standard 5-bit param
class Compressed: class Compressed:
""" """
Compress 2bpp data. 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!' assert image, 'need something to compress!'
image = list(image) image = list(image)
self.image = image self.image = image
self.pic = [] self.pic = []
self.animtiles = [] self.animtiles = []
# only transpose pic (animtiles were never transposed in decompression) # only transpose pic (animtiles were never transposed in decompression)
if size != None: if size != None:
for byte in range((size*size)*16): for byte in range((size*size)*16):
@ -221,7 +220,7 @@ class Compressed:
self.tiles = get_tiles(self.pic) self.tiles = get_tiles(self.pic)
self.tiles = transpose(self.tiles) self.tiles = transpose(self.tiles)
self.pic = connect(self.tiles) self.pic = connect(self.tiles)
self.image = self.pic + self.animtiles self.image = self.pic + self.animtiles
self.end = len(self.image) self.end = len(self.image)
@ -326,15 +325,15 @@ class Compressed:
def scanRepeats(self): def scanRepeats(self):
""" """
Works, but doesn't do flipped/reversed streams yet. Works, but doesn't do flipped/reversed streams yet.
This takes up most of the compress time and only saves a few bytes This takes up most of the compress time and only saves a few bytes
it might be more feasible to exclude it entirely. it might be more feasible to exclude it entirely.
""" """
self.repeats = [] self.repeats = []
self.flips = [] self.flips = []
self.reverses = [] self.reverses = []
# make a 5-letter word list of the sequence # make a 5-letter word list of the sequence
letters = 5 # how many bytes it costs to use a repeat over a literal letters = 5 # how many bytes it costs to use a repeat over a literal
# any shorter and it's not worth the trouble # any shorter and it's not worth the trouble
@ -345,7 +344,7 @@ class Compressed:
for j in range(letters): for j in range(letters):
word.append( ord(self.image[i+j]) ) word.append( ord(self.image[i+j]) )
words.append((word, i)) words.append((word, i))
zeros = [] zeros = []
for zero in range(letters): for zero in range(letters):
zeros.append( 0 ) zeros.append( 0 )
@ -405,13 +404,13 @@ class Compressed:
else: # no more overlaps else: # no more overlaps
buffer.append(match) buffer.append(match)
else: # last match, so there's nothing to check else: # last match, so there's nothing to check
buffer.append(match) buffer.append(match)
matches = buffer matches = buffer
# remove alternating sequences # remove alternating sequences
buffer = [] buffer = []
for match in matches: 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]: if match[0][i] != match[0][i&1]:
buffer.append(match) buffer.append(match)
break break
@ -422,7 +421,7 @@ class Compressed:
def doRepeats(self): def doRepeats(self):
"""doesn't output the right values yet""" """doesn't output the right values yet"""
unusedrepeats = [] unusedrepeats = []
for repeat in self.repeats: for repeat in self.repeats:
if self.address >= repeat[2]: if self.address >= repeat[2]:
@ -734,7 +733,7 @@ class Decompressed:
def doFlip(self): def doFlip(self):
""" """
Repeat flipped bytes from 2bpp output. Repeat flipped bytes from 2bpp output.
eg 11100100 -> 00100111 eg 11100100 -> 00100111
quat 3 2 1 0 -> 0 2 1 3 quat 3 2 1 0 -> 0 2 1 3
""" """
@ -1228,7 +1227,7 @@ def dmg2rgb(word):
blue = word & 0b11111 blue = word & 0b11111
alpha = 255 alpha = 255
return ((red<<3)+0b100, (green<<3)+0b100, (blue<<3)+0b100, alpha) return ((red<<3)+0b100, (green<<3)+0b100, (blue<<3)+0b100, alpha)
def rgb_to_dmg(color): def rgb_to_dmg(color):
word = (color['r'] / 8) word = (color['r'] / 8)
word += (color['g'] / 8) << 5 word += (color['g'] / 8) << 5
@ -1556,8 +1555,8 @@ def lz_to_png_by_file(filename):
def dump_tileset_pngs(): 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. Also, leaves a bunch of wonderful .2bpp files everywhere for your amusement.
""" """
for tileset_id in range(37): for tileset_id in range(37):
@ -1581,7 +1580,7 @@ def decompress_frontpic_anim(lz_file):
def expand_pic_palettes(): def expand_pic_palettes():
""" """
Add white and black to palette files with fewer than 4 colors. Add white and black to palette files with fewer than 4 colors.
Pokemon Crystal only defines two colors for a pic palette to Pokemon Crystal only defines two colors for a pic palette to
save space, filling in black/white at runtime. save space, filling in black/white at runtime.
Instead of managing palette files of varying length, black Instead of managing palette files of varying length, black
@ -1601,7 +1600,7 @@ def expand_pic_palettes():
if __name__ == "__main__": if __name__ == "__main__":
debug = False debug = False
argv = [None] * 5 argv = [None] * 5
for i, arg in enumerate(sys.argv): for i, arg in enumerate(sys.argv):
argv[i] = arg argv[i] = arg
@ -1667,7 +1666,7 @@ if __name__ == "__main__":
filein = argv[2] filein = argv[2]
fileout = argv[3] fileout = argv[3]
compress_file(filein, fileout) compress_file(filein, fileout)
elif argv[1] == '2bpp-to-png': elif argv[1] == '2bpp-to-png':
to_png(argv[2]) to_png(argv[2])