-
Notifications
You must be signed in to change notification settings - Fork 1
/
chr.py
61 lines (53 loc) · 2 KB
/
chr.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
from PIL import Image
class Chr:
INDEXED = 0
WITH_ALPHA = 1
def __init__(self, images, chr_type):
self.images = images
self.type = chr_type
def draw(self, canvas, x, y, palette):
if self.type == Chr.INDEXED:
self.images[0].putpalette(palette)
canvas.paste(self.images[0], (x, y))
elif self.type == Chr.WITH_ALPHA:
canvas.paste(self.images[palette], (x, y), mask=self.images[palette])
@staticmethod
def make_indexed_chr(chr_data):
pixels = Chr.pattern_map(chr_data)
img = Image.new('P', (8, 8))
for x in range(8):
for y in range(8):
img.putpixel((x, y), pixels[x + y * 8])
img = img.resize((16, 16))
return Chr([img], Chr.INDEXED)
@staticmethod
def old_make_chr_with_alpha(chr_data, palettes):
pixels = Chr.pattern_map(chr_data)
images = []
for p in palettes:
img = Image.new('RGBA', (8, 8))
for x in range(8):
for y in range(8):
img.putpixel((x, y), p[pixels[x + y * 8]])
img = img.resize((16, 16), resample=Image.NEAREST)
images.append(img)
return Chr(images, Chr.WITH_ALPHA)
@staticmethod
def make_chr_with_alpha(chr_data, palette):
pixels = Chr.pattern_map(chr_data)
images = []
img = Image.new('RGBA', (8, 8))
for x in range(8):
for y in range(8):
img.putpixel((x, y), palette[pixels[x + y * 8]])
img = img.resize((16, 16), resample=Image.NEAREST)
images.append(img)
return Chr(images, Chr.WITH_ALPHA)
# this converts the 16 byte pattern table data to an array of color indexes 0-3 for the 8x8 tile
@staticmethod
def pattern_map(pattern):
pixels = []
for i in range(0, 64):
pixels.append(
(((pattern[(i >> 3) + 8] >> (7 - (i & 7))) & 1) << 1) + (pattern[i >> 3] >> (7 - (i & 7)) & 1))
return pixels