-
Notifications
You must be signed in to change notification settings - Fork 8
/
show-tiles.py
61 lines (45 loc) · 1.37 KB
/
show-tiles.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
import sys
import Image
import string
import ega_palette
def put_tile(image, tiledata, x, y):
bitmap = [chr(0)] * 256
for ty in range(16):
for tx in range(16):
# get the four bits for this pixel
c0 = ord(tiledata[(tx >> 3) + (ty << 1) + 0])
c1 = ord(tiledata[(tx >> 3) + (ty << 1) + 32])
c2 = ord(tiledata[(tx >> 3) + (ty << 1) + 64])
c3 = ord(tiledata[(tx >> 3) + (ty << 1) + 96])
# build up a nibble
bit = 7 - (tx & 7)
color = ((c3 >> bit) & 1) << 3
color = color | (((c2 >> bit) & 1) << 2)
color = color | (((c1 >> bit) & 1) << 1)
color = color | (((c0 >> bit) & 1) << 0)
#image.putpixel((x+tx, y+ty), color)
bitmap[ty*16 + tx] = chr(color)
s = string.joinfields(bitmap, '')
smallimage = Image.fromstring("P", (16, 16), s)
image.paste(smallimage, (x, y))
# egatiles stores tiles used in all levels
f = open("egatiles.dd2", "rb")
tiles = f.read()
f.close()
total_tiles = len(tiles) / 128
width = 13
height = total_tiles / width
if total_tiles % width > 0:
height = height + 1
im = Image.new("P", (width*16, height*16))
im.im.putpalette("RGB", ega_palette.palette)
for y in range(height):
print "y %d" % y
for x in range(width):
tile_index = (y*width + x)
tile_offset = tile_index << 7
tiledata = tiles[tile_offset : tile_offset+128]
if len(tiledata) < 128:
break
put_tile(im, tiledata, x * 16, y * 16)
im.save("egatiles.png")