-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathiffshow.py
More file actions
executable file
·265 lines (229 loc) · 7.11 KB
/
iffshow.py
File metadata and controls
executable file
·265 lines (229 loc) · 7.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#!/usr/bin/env python
# Show an IFF-ILBM file in a PyGame window
import pygame
import sys, os, time
from math import ceil
# window resolution
res = 1280, 960
fn = sys.argv[1]
f = open(fn, "rb")
a = bytearray(f.read())
def limit(x):
"Exclude forbidden characters in chunk names; print a question mark instead"
if x < 0x20 or x > 0x7e:
x = 0x3f
return x
def s(n):
"Get chunk name"
return "".join([chr(limit(a[q])) for q in range(n, n+4)])
def get_size(n):
"Get chunk size"
return 256**3 * a[n] + 256**2 * a[n+1] + 256 * a[n+2] + a[n+3]
xdim, ydim, planes, comp, HAM, EHB = 0, 0, 1, False, False, False
colormap, colormap_pure = [], []
def bmhd(n):
"BMHD chunk handler"
global xdim, ydim, planes, comp
xdim = 256*a[n+8] + a[n+9]
ydim = 256*a[n+10] + a[n+11]
planes = a[n+16]
if a[n+18]:
nc = ""
comp = True
else:
nc = "not "
print(f"{xdim}x{ydim}, {planes} planes, {nc}compressed")
if planes == 24 or planes == 32:
print("deep image")
def cmap(n):
"CMAP chunk handler"
global colormap, colormap_pure
s = get_size(n+4)
n += 8
cm1, cm2 = [], []
for i in range(s // 3):
cm1.append( (a[n+3*i], a[n+3*i+1], a[n+3*i+2]) )
# add EHB colors:
cm2.append( (a[n+3*i]//2, a[n+3*i+1]//2, a[n+3*i+2]//2) )
#print(colormap)
colormap = cm1 + cm2
colormap_pure = cm1
print(s // 3, "colors loaded")
if s(0) != "FORM":
print("not a FORM")
sys.exit(1)
PBM = False
if s(8) == "PBM ":
print("PBM image")
PBM = True
elif s(8) != "ILBM":
print("not an ILBM")
sys.exit(1)
file_size = get_size(4) + 8
pos = 12
body_start = 0
while True:
ch = s(pos)
siz = get_size(pos+4)
print("chunk:", ch, "; size:", siz)
if ch == "BMHD":
bmhd(pos)
if ch == "CMAP":
cmap(pos)
if ch == "CAMG":
if a[pos + 10] & 8:
print("HAM mode")
HAM = True
if a[pos + 11] & 0x80:
print("EHB mode")
EHB = True
if ch == "BODY":
body_start = pos + 8
if siz % 2:
siz += 1
pos += 8 + siz
if pos >= len(a) or pos >= file_size:
break
# set EHB colors:
if EHB:
cm1, cm2 = [], []
for i in range(32):
r, g, b = colormap[i]
cm1.append((r, g, b))
# add EHB colors:
cm2.append((r//2, g//2, b//2))
colormap = cm1 + cm2
colormap_pure = cm1
img = pygame.Surface((xdim, ydim))
pos = body_start
bits_per_line = 16 * int(ceil(xdim / 16))
bytes_per_line = int(bits_per_line * planes / 8)
# Draw regular IFF file
if not PBM: # read ILBM image
for y in range(ydim):
# handle run-length image compression
if comp == 1:
tmp = []
while True:
con = a[pos]
# http://fileformats.archiveteam.org/wiki/PackBits
if 0 <= con <= 127:
for k in range(con+1):
tmp.append(a[pos+1+k])
pos += con + 2
elif 129 <= con <= 255:
for k in range(257 - con):
tmp.append(a[pos+1])
pos += 2
else:
pos += 1
if len(tmp) > bytes_per_line:
print(y, "error")
1/0
if len(tmp) == bytes_per_line:
break
bb = bytearray(tmp)
else:
bb = a[pos:pos+bytes_per_line]
pos += bytes_per_line
bits = ""
for n in range(bytes_per_line):
bits += format(bb[n], '08b')
# set up HAM variables
if HAM:
rr, gg, bb = 0, 0, 0
hi, lo = 2**(planes-1), 2**(planes-2)
if planes == 6:
fact = 16
mask = 15
if planes == 8:
fact = 4
mask = 63
for x in range(xdim):
col = 0
for p in range(planes):
if bits[bits_per_line * p + x] == "1":
col |= 1 << p
if planes == 24 or planes == 32:
bb = (col >> 16) & 255
gg = (col >> 8) & 255
rr = col & 255
img.set_at((x, y), (rr, gg, bb))
elif HAM: # https://en.wikipedia.org/wiki/ILBM
if col & hi == 0 and col & lo == 0:
index = col & mask
img.set_at((x, y), colormap[index])
rr, gg, bb = colormap[index][0], colormap[index][1], colormap[index][2]
if col & hi == 0 and col & lo == lo:
bb = fact * (col & mask)
img.set_at((x, y), (rr, gg, bb))
if col & hi == hi and col & lo == lo:
gg = fact * (col & mask)
img.set_at((x, y), (rr, gg, bb))
if col & hi == hi and col & lo == 0:
rr = fact * (col & mask)
img.set_at((x, y), (rr, gg, bb))
else:
img.set_at((x, y), colormap[col])
else: # read PBM image
if comp == 1 and planes > 0:
tmp = []
pos = body_start
while True:
con = a[pos]
# http://fileformats.archiveteam.org/wiki/PackBits
if 0 <= con <= 127:
for k in range(con+1):
tmp.append(a[pos+1+k])
pos += con + 2
elif 129 <= con <= 255:
for k in range(257 - con):
tmp.append(a[pos+1])
pos += 2
else:
pos += 1
if len(tmp) == xdim * ydim:
break
bb = bytearray(tmp)
else:
bb = a[body_start:]
for y in range(ydim):
for x in range(xdim):
col = bb[xdim * y + x]
img.set_at((x, y), colormap[col])
# Draw IFF palette file
pal = pygame.Surface((16, 16))
for y in range(16):
for x in range(16):
i = 16 * x + y
if i < len(colormap_pure):
pal.set_at((x, y), colormap_pure[i])
# Open a PyGame window
pygame.init()
screen = pygame.display.set_mode(res)
if xdim < 320 and ydim < 200: # not a full-screen image?
img2 = pygame.Surface((320, 256)) # then show image on a 320x256 PAL screen
img2.blit(img, (0, 0))
img2 = pygame.transform.scale(img2, res)
else:
img2 = pygame.transform.scale(img, res)
pal2 = pygame.transform.scale(pal, res)
run = True
pygame.display.set_caption(f"IFFshow: {os.path.basename(fn)} ({xdim}x{ydim})")
showpal = False
while run:
for event in pygame.event.get():
if (event.type == pygame.QUIT or
(event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE)):
run = False
if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
showpal = not showpal
if event.type == pygame.KEYDOWN and event.key == pygame.K_s:
pygame.image.save(img2, fn + ".png")
if planes == 0 or showpal:
screen.blit(pal2, (0, 0))
else:
screen.blit(img2, (0, 0))
pygame.display.flip()
time.sleep(0.1)
pygame.quit()