Created
September 6, 2019 06:04
-
-
Save bbbradsmith/2cf144059a164fd68205cd8c7c30c321 to your computer and use it in GitHub Desktop.
Read & Rhyme (Atari ST) python image file dumper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# | |
# Python script for dumping images from Read & Rhyme (Atari ST) | |
# Brad Smith, 2019 | |
# http://rainwarrior.ca | |
# | |
# Format is very simple: | |
# 1 x WORD header? always 0. | |
# 16 x WORD palette | |
# 320 x 200 image packed in 4-plane WORDs (standard Atari ST image) | |
# | |
import os | |
import struct | |
import PIL.Image | |
def find_files(ext): | |
ext = ext.lower() | |
fo = [] | |
for dirpath, dirnames, filenames in os.walk("."): | |
for fn in filenames: | |
if fn.lower().endswith(ext): | |
fo.append(fn) | |
return fo | |
def dump_pal(filename,verbose=False): | |
d = open(filename,"rb").read() | |
head = struct.unpack(">H",d[0:2])[0] # always zero? | |
if verbose: | |
print("Head: %04X" % head) | |
pal = [] | |
for i in range(0,16): | |
p = struct.unpack(">H",d[2+(i*2):4+(i*2)])[0] | |
r = ((p >> 8) & 7) << 5 | |
g = ((p >> 4) & 7) << 5 | |
b = ((p >> 0) & 7) << 5 | |
pal.append(r) | |
pal.append(g) | |
pal.append(b) | |
if verbose: | |
print("Palette %2d: %04X (%3d,%3d,%3d)" % (i,p,r,g,b)) | |
return pal | |
def dump_rhy(filename,overrides={}): | |
print("Dumping: " + filename) | |
d = open(filename,"rb").read() | |
pal = dump_pal(filename,True) | |
if filename in overrides: | |
override = overrides[filename] | |
print("Palette override: " + override) | |
pal = dump_pal(override,False) | |
img = PIL.Image.new("P",(320,200)) | |
img.putpalette(pal) | |
for y in range(0,200): | |
for x in range(0,320,16): | |
pos = 34 + (y * 8 * 320 // 16) + (x * 8 // 16) | |
p = struct.unpack(">HHHH",d[pos:pos+8]) | |
for s in range(0,16): | |
p0 = (p[0] >> (15 - s)) & 1 | |
p1 = (p[1] >> (15 - s)) & 1 | |
p2 = (p[2] >> (15 - s)) & 1 | |
p3 = (p[3] >> (15 - s)) & 1 | |
img.putpixel((x+s,y),p0|(p1<<1)|(p2<<2)|(p3<<3)) | |
fileout = filename+".PNG" | |
print("Saving: " + fileout) | |
img.save(fileout) | |
print() | |
# these two don't have any palette data | |
overrides = { | |
"RHYMEOVL.RHY":"RHYMEPIC.RHY", | |
"ROCKETS.RHY":"ROCKPIC.RHY", | |
} | |
for f in find_files(".RHY"): | |
dump_rhy(f,overrides) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment