Repository navigation
Expand file tree
/
Copy pathplanar.py
More file actions
60 lines (51 loc) · 2.11 KB
/
Copy pathplanar.py
File metadata and controls
60 lines (51 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
"""Render Amiga bitplane data at a given geometry, either layout.
Nothing on this disc carries a palette next to its pixels, so the renderer
defaults to a grey ramp: the question these images have to answer first is
*what shape is it*, and a ramp answers that as well as the real colours do.
planar.py FILE OUT.png W H PLANES [sep|inter] [offset] [pad]
`sep` -- all of plane 0, then all of plane 1 (this disc's `.img` layout)
`inter` -- the eight plane-rows of row 0, then those of row 1
`pad` -- extra bytes between one plane and the next (0 on this disc)
"""
import sys
from PIL import Image
def unpack(d, w, h, planes, layout='sep', off=0, pad=0):
bpr = (w + 15) // 16 * 2 # Amiga rows are word-aligned
px = bytearray(w * h)
for p in range(planes):
for y in range(h):
if layout == 'sep':
row = off + p * (bpr * h + pad) + y * bpr
else:
row = off + y * bpr * planes + p * bpr
if row + bpr > len(d):
return px
o = y * w
for xb in range(bpr):
b = d[row + xb]
if not b:
continue
for k in range(8):
if b & (0x80 >> k):
x = xb * 8 + k
if x < w:
px[o + x] |= 1 << p
return px
def save(px, w, h, out, planes):
im = Image.frombytes('P', (w, h), bytes(px))
n = 1 << planes
flat = []
for i in range(256):
v = min(255, i * 255 // max(1, n - 1))
flat += [v, v, v]
im.putpalette(flat)
im.convert('RGB').save(out)
if __name__ == '__main__':
src, out = sys.argv[1], sys.argv[2]
w, h, planes = int(sys.argv[3]), int(sys.argv[4]), int(sys.argv[5])
layout = sys.argv[6] if len(sys.argv) > 6 else 'sep'
off = int(sys.argv[7], 0) if len(sys.argv) > 7 else 0
pad = int(sys.argv[8], 0) if len(sys.argv) > 8 else 0
d = open(src, 'rb').read()
save(unpack(d, w, h, planes, layout, off, pad), w, h, out, planes)
print('%s %dx%d %d planes %s @0x%X' % (out, w, h, planes, layout, off))