all: fox.png.c fox.png.h

.ONESHELL:

%.png.h: %.png.c
	true

%.png.c: SHELL=/usr/bin/python3
%.png.c: %.png
	#!/usr/bin/env python3
	import os, re, sys, json
	from PIL import Image
	name = re.sub('[^0-9a-zA-Z]+', '_', "$<")
	with Image.open("$<") as img:
	  with open("$<.c", "w") as c:
	    with open("$<.h", "w") as h:
	      array = img.convert('RGB').load()
	      h.write(f'#ifndef {name}_H\n#define {name}_H\n\nuint8_t {name}[{img.height}][{img.width}][3];\n\n#endif\n')
	      c.write(f'uint8_t {name}[{img.height}][{img.width}][3] = {{\n')
	      for y in range(img.height):
	        c.write('  { ')
	        for x in range(img.width):
	          p = array[x,y]
	          c.write(f'{{{p[0]}, {p[1]}, {p[2]}}}, ')
	        c.write('},\n')
	      c.write('};\n')
