-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmacpaintLib.py
374 lines (307 loc) · 9.31 KB
/
macpaintLib.py
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
# -*- coding: utf-8 -*-
import sys
import os
import datetime
import struct
import zipfile
import gzip
import unicodedata
import PIL
import PIL.Image
import PIL.ImageDraw
import cStringIO
import pprint
pp = pprint.pprint
import pdb
kwdbg = 0
kwlog = 0
import time
#
# constants
#
# to be filled out
quickdraw1Colors = {}
pictPrefix = chr(0) * 512
#
# tools
#
def datestring(dt = None, dateonly=False, nospaces=False):
if not dt:
now = str(datetime.datetime.now())
else:
now = str(dt)
if not dateonly:
now = now[:19]
else:
now = now[:10]
if nospaces:
now = now.replace(" ", "_")
return now
def makeunicode( s, enc="utf-8", normalizer='NFC'):
try:
if type(s) != unicode:
s = unicode(s, enc)
except:
pass
s = unicodedata.normalize(normalizer, s)
return s
def iterateFolders( infolder, validExtensions=False ):
"""Iterator that walks a folder and returns all files."""
# for folder in dirs:
lastfolder = ""
for root, dirs, files in os.walk( infolder ):
root = makeunicode( root )
result = {}
pathlist = []
for thefile in files:
thefile = makeunicode( thefile )
basename, ext = os.path.splitext(thefile)
typ = ext.lower()
if thefile.startswith('.'):
continue
filepath = os.path.join( root, thefile )
dummy, folder = os.path.split( root )
if kwdbg or 1:
if root != lastfolder:
lastfolder = root
print
print "FOLDER:", repr( root )
filepath = makeunicode( filepath )
if 0:
if typ not in validExtensions:
# check for cvt file by scanning
f = open(filepath, 'rb')
data = f.read(4096)
f.close()
format = data[0x1e:0x3a]
formatOK = False
if format.startswith("PRG formatted GEOS file"):
formatOK = True
elif format.startswith("SEQ formatted GEOS file"):
broken = True
if not formatOK:
continue
typ = '.cvt'
if kwlog and 1:
print "FILE:", repr(filepath)
yield typ, filepath
def getCompressedFile( path, acceptedOnly=False ):
"""Open a gzip or zip compressed file. Return the GEOS and c64 files in
contained disk image(s)
"""
result = {}
# limit size of files to 10MB
# use a size limit?
if 0: #s.st_size > 10*2**20:
s = os.stat( path )
return result
folder, filename = os.path.split( path )
basename, ext = os.path.splitext( filename )
if ext.lower() == '.gz':
f = gzip.open(path, 'rb')
foldername = basename + '_gz'
result[foldername] = []
file_content = f.read()
f.close()
# only return those streams that have a chance of being an image
if len(file_content) in imagesizeToExt:
di = DiskImage( stream=file_content, tag=path )
if acceptedOnly:
for u in di.files:
if u.header.className in acceptedTypes:
result[foldername].append( u )
else:
result[foldername].extend(di.files)
return result
elif ext.lower() == '.zip':
foldername = basename + '_zip'
try:
handle = zipfile.ZipFile(path, 'r')
files = handle.infolist()
except Exception, err:
print "ZIP ERROR", err
return result
for zf in files:
print "ZIPFILE:", repr( zf.filename )
try:
h = handle.open(zf)
data = h.read()
except Exception, err:
continue
if len(data) in imagesizeToExt:
zfoldername = '/'.join( (foldername, zf.filename) )
result[zfoldername] = []
# pdb.set_trace()
di = DiskImage( stream=data, tag=path )
if acceptedOnly:
for u in di.files:
if u.header.className in acceptedTypes:
result[zfoldername].append( u )
else:
result[zfoldername].extend( di.files )
return result
return result
class ImageBuffer(list):
"""For debugging purposes mostly. Has a built in memory dump in
monitor format."""
def __init__(self):
super(ImageBuffer, self).__init__()
def dump(self):
hexdump( self )
def hexdump( s, col=32 ):
"""Using this for debugging was so memory lane..."""
cols = {
8: ( 7, 0xfffffff8),
16: (15, 0xfffffff0),
32: (31, 0xffffffe0),
64: (63, 0xffffffc0)}
if not col in cols:
col = 16
minorMask, majorMask = cols.get(col)
d = False
mask = col-1
if type(s) in( list, tuple): #ImageBuffer):
d = True
for i,c in enumerate(s):
if d:
t = hex(c)[2:]
else:
t = hex(ord(c))[2:]
t = t.rjust(2, '0')
# spit out address
if i % col == 0:
a = hex(i)[2:]
a = a.rjust(4,'0')
sys.stdout.write(a+': ')
sys.stdout.write(t+' ')
# spit out ascii line
if i & minorMask == minorMask:
offs = i & majorMask
for j in range(col):
c2 = s[offs+j]
if d:
d2 = chr(c2)
else:
d2 = c2
if 32 <= d2 < 127:
sys.stdout.write( c2 )
else:
sys.stdout.write( '.' )
sys.stdout.write('\n')
sys.stdout.write('\n')
sys.stdout.flush()
#
# file tools
#
#
# macpaint image conversion
#
def unpackBits( s ):
# 72 bytes times 720 lines
lbuf = []
f = cStringIO.StringIO( s )
p = 72 * 720 #len(lbuf)
q = 0
#t = chr(0) * 128
#if s.startswith( t ):
# s = s[128:]
done = False
while ( q < p and not done):
#pdb.set_trace()
c = f.read(1); q = f.tell()
if c == "":
done = True
break
co = ord(c)
# print hex(co)
if co < 128:
# print "POSITIVE OPCODE", hex(co)
l = [co]
for i in range( co+1 ):
c = f.read(1); q = f.tell()
if c == "":
done = True
break
lbuf.append( ord(c) )
l.append( ord(c) )
#hexdump( l )
else:
# print "NEGATIVE OPCODE", hex(co)
l = [co]
co -= 256
co = -co
# print hex(co)
co += 1
c = f.read(1); q = f.tell()
if c == "":
done = True
break
for i in range(co):
lbuf.append( ord( c ) )
l.append( ord(c) )
#hexdump( l )
return lbuf
def imageband2PNG( image, cardsw, h):
"""Convert a list of expanded image bytes into a PNG.
"""
cardsh = h >> 3
if h & 7 != 0:
cardsh += 1
w = cardsw * 8
noofcards = cardsw * cardsh
noofbytes = cardsw * h
# check sizes
n = len(image)
expectedSize = noofbytes
# repair section
if n < expectedSize:
# actual bits missing
# fill with 0
if kwlog or 1:
print "BITMAP BITS MISSING", expectedSize - n
# fill bitmap up
image.extend( [0] * (expectedSize - n) )
n = len(image)
elif n == noofbytes:
# everything's ok
if kwlog:
print "BITMAP OK"
elif n > noofbytes:
if kwlog or 1:
print "BITMAP TOO BIG", n
image = image[:noofbytes]
n = len(image)
# invert bw bitmap
# looks better most of the cases
bwbytes = [chr(i ^ 255) for i in image]
# for the bitmap image
bwbytes = ''.join( bwbytes )
bwimg = PIL.Image.frombytes('1', (w,h), bwbytes, decoder_name='raw')
return bwimg
if __name__ == '__main__':
for input in sys.argv[1:]:
path = os.path.abspath( os.path.expanduser( input ))
folder, filename = os.path.split( path )
basename, ext = os.path.splitext( filename )
if os.path.isdir( path ):
files = iterateFolders( path )
else:
files = ( (ext.lower(), path), )
for typ,path in files:
f = open(path, 'rb')
s = f.read()
f.close()
# pdb.set_trace()
folder, filename = os.path.split( path )
basename, ext = os.path.splitext( filename )
dest = "macpaintExports/"
if not os.path.exists( dest ):
os.makedirs( dest )
dest = os.path.join( dest, basename + ".png")
if s.startswith( (chr(0),chr(0),chr(0),chr(2)) ):
s = s[640:]
if typ in (".mac", ".mpnt", ".pnt", ".pntg", ".pic"):
print repr(path)
image = unpackBits( s )
img = imageband2PNG( image, 72, 720)
img.save( dest )