"""Export a hand-coded 16×16 pixel axe as a transparent RGBA PNG."""
from pathlib import Path
import struct
import zlib

PALETTE = {
    '.': (0, 0, 0, 0),
    '#': (35, 42, 49, 255),   # outline
    's': (104, 130, 146, 255), # steel shadow
    'm': (166, 192, 203, 255), # steel
    'h': (225, 241, 242, 255), # blade highlight
    'w': (145, 84, 43, 255),   # handle
    'g': (206, 144, 73, 255),  # handle highlight
}
PIXELS = [
    '................',
    '..........###...',
    '.........#mmh#..',
    '........#smmhh#.',
    '.......##ssmhh#.',
    '.......#w#smhh#.',
    '......#gw##smh#.',
    '.....#gw#..###..',
    '....#gw#........',
    '...#gw#.........',
    '..#gw#..........',
    '.#gw#...........',
    '.#w#............',
    '..#.............',
    '................',
    '................',
]
assert len(PIXELS) == 16 and all(len(row) == 16 for row in PIXELS)

def chunk(kind, data):
    return (struct.pack('>I', len(data)) + kind + data
            + struct.pack('>I', zlib.crc32(kind + data) & 0xffffffff))

def export(path, scale=1):
    rows = []
    for row in PIXELS:
        rgba = b''.join(bytes(PALETTE[pixel]) * scale for pixel in row)
        rows.extend([b'\x00' + rgba] * scale)
    size = 16 * scale
    png = b'\x89PNG\r\n\x1a\n'
    png += chunk(b'IHDR', struct.pack('>IIBBBBB', size, size, 8, 6, 0, 0, 0))
    png += chunk(b'IDAT', zlib.compress(b''.join(rows)))
    png += chunk(b'IEND', b'')
    path.write_bytes(png)

if __name__ == '__main__':
    directory = Path(__file__).resolve().parent
    export(directory / 'axe_16x16.png')
    export(directory / 'axe_preview.png', scale=16)
