aboutsummaryrefslogtreecommitdiff
path: root/scripts/parse_sprite_png.py
blob: 730b487a41d7c47fb4f3763cc5926f53098b1412 (plain)
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
from PIL import Image
import numpy as np
import sys

sprite_8x16 = "--8x16" in sys.argv

sprite_8x8 = "--8x8" in sys.argv

sprite_1bpp = "--1bpp" in sys.argv

file = Image.open(sys.argv[1]).convert("RGB")

px_array = np.asarray(file)

def getpx(sprite_nb, x, y):
    double_sprite_nb = int(sprite_nb / 4)
    sprite_double_line = int(double_sprite_nb / (file.width / 16))
    sprite_double_column = int(double_sprite_nb % (file.width / 16))
    sprite_tile_y = 1 if sprite_nb & 0b10 else 0
    sprite_tile_x = 1 if sprite_nb & 0b01 else 0

    if sprite_8x16 or file.width < 16:
        sprite_tile_x ^= sprite_tile_y
        sprite_tile_y ^= sprite_tile_x
        sprite_tile_x ^= sprite_tile_y

    sprite_line = sprite_double_line * 2 + sprite_tile_y
    sprite_column = sprite_double_column * 2 + sprite_tile_x

    if sprite_8x8:
        sprite_line = int(sprite_nb / (file.width / 8))
        sprite_column = int(sprite_nb % (file.width / 8))

    if file.width < 16:
        sprite_line = sprite_column * 2 + sprite_line
        sprite_column = 0

    return [int(x) for x in px_array[int(sprite_line * 8 + y)][int(sprite_column * 8 + x)]]


if file.width % 8 != 0 or file.height % 8 != 0:
    raise ValueError("Width and height must be multiples of 8px")

sprite_nb = int((file.width / 8) * (file.height / 8))

for nb in range(0, sprite_nb):
    result1 = [0,0,0,0,0,0,0,0]
    result2 = [0,0,0,0,0,0,0,0]
    for y in range(0, 8):
        for x in range(0, 8):
            px = getpx(nb, x, y)
            dw = px[0] - 0xe0 + px[1] - 0xf8 + px[2] - 0xd0
            dlg = px[0] - 0x88 + px[1] - 0xc0 + px[2] - 0x70
            ddg = px[0] - 0x34 + px[1] - 0x68 + px[2] - 0x56
            db = px[0] - 0x08 + px[1] - 0x18 + px[2] - 0x20
    
            if min(abs(db), abs(ddg)) < min(abs(dlg), abs(dw)):
                result2[y] |= 1 << (7-x)
            if min(abs(db), abs(dlg)) < min(abs(ddg), abs(dw)):
                result1[y] |= 1 << (7-x)
            
            if abs(db) < min(abs(dw), abs(dlg), abs(ddg)):
                print("#", end = '')
            elif abs(ddg) < min(abs(dw), abs(dlg), abs(db)):
                print(";", end = '')
            elif abs(dlg) < min(abs(dw), abs(ddg), abs(db)):
                print(".", end = '')
            else:
                print(" ", end = '')
        print("\n", end = '')
    print("\n")
    for i in range(0, 8):
        if sprite_1bpp:
            if i == 0:
                print(".DB $%02x" % (result1[i]), end='')
            else:
                print(", $%02x" % (result1[i]), end='')
        else:
            if i == 0:
                print(".DB $%02x, $%02x" % (result1[i], result2[i]), end='')
            else:
                print(", $%02x, $%02x" % (result1[i], result2[i]), end='')
    print("\n")