Skip to content

Instantly share code, notes, and snippets.

@cleure
Created June 24, 2017 17:01
Show Gist options
  • Save cleure/d0965d5a6664019f11f0d426a4d76c09 to your computer and use it in GitHub Desktop.
Save cleure/d0965d5a6664019f11f0d426a4d76c09 to your computer and use it in GitHub Desktop.
A tool to graph the graphics roms from Taito SJ Arcade Hardware to an Image.
import os, sys, math
from PIL import Image
def chunks(l, n):
"""Yield successive n-sized chunks from l."""
for i in range(0, len(l), n):
yield l[i:i + n]
def split_images(data):
# The Taito SJ hardware appears to break characters/tiles into 8x8 blocks. These blocks
# are encoded as 8-byte chunks, with each byte mapping to a row, and each bit of a byte
# mapping to a column. For instance, the tile representing the number 1 may be stored as:
#
# Legend:
# . = 0
# X = 1
#
# Bits 8 7 6 5 4 3 2 1
# __________________
# Bytes 1 | . . . . . . . .
# 2 | . . X X X X . .
# 3 | . . X X X X . .
# 4 | . . . X X X . .
# 5 | . . . X X X . .
# 6 | . . . X X X . .
# 7 | . . X X X X X .
# 8 | . . X X X X X .
#
bit_map = [
0b00000001,
0b00000010,
0b00000100,
0b00001000,
0b00010000,
0b00100000,
0b01000000,
0b10000000
]
images = []
for chunk in chunks(data, 8):
im = []
for _byte in chunk:
byte = ord(_byte)
line = []
for c in bit_map:
line.append(1 if byte & c else 0)
im.append(line)
images.append(im)
return images
def to_image(images):
# @TODO: Split this out into a layout engine + rasterizer
n_images = len(images)
per_row = 8
char_size = 8
width = per_row * char_size
height = (n_images / per_row) * char_size
image = Image.new('L', (width, height))
pixels = image.load()
for i in range(len(images)):
y = (i / per_row) * char_size
for line in images[i]:
x = (i % per_row) * char_size
for j in range(len(line)):
pixels[x, y] = 255 if line[j] else 0
x += 1
y += 1
return image
def main():
if len(sys.argv) < 2:
print('Usage: %s <input>' % (sys.argv[0]))
sys.exit(1)
fp = open(sys.argv[1], 'rb')
data = fp.read()
fp.close()
images = split_images(data)
output = to_image(images)
output.save('output.png')
if __name__ == '__main__':
main()
@cleure
Copy link
Author

cleure commented Jun 24, 2017

Dump of graphics roms from Jungle Hunt: http://imgur.com/a/vg7SM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment