2019-03-09 17:24:49 +00:00
|
|
|
#!/usr/bin/env python3
|
2014-04-29 12:26:51 +00:00
|
|
|
import glob
|
|
|
|
import os
|
2019-04-18 14:27:27 +00:00
|
|
|
|
2014-04-29 12:26:51 +00:00
|
|
|
from PIL import Image
|
|
|
|
|
|
|
|
hdrs = []
|
|
|
|
data = []
|
|
|
|
imgs = []
|
|
|
|
|
2019-04-18 14:27:27 +00:00
|
|
|
|
2014-04-29 12:26:51 +00:00
|
|
|
def encode_pixels(img):
|
2019-04-18 14:27:27 +00:00
|
|
|
r = ""
|
2021-02-21 22:22:17 +00:00
|
|
|
img = ["1" if x >= 128 else "0" for x in img]
|
2019-04-18 14:27:27 +00:00
|
|
|
for i in range(len(img) // 8):
|
|
|
|
c = "".join(img[i * 8 : i * 8 + 8])
|
|
|
|
r += "0x%02x, " % int(c, 2)
|
|
|
|
return r
|
|
|
|
|
2014-04-29 12:26:51 +00:00
|
|
|
|
|
|
|
cnt = 0
|
2019-04-18 14:27:27 +00:00
|
|
|
for fn in sorted(glob.glob("*.png")):
|
|
|
|
print("Processing:", fn)
|
|
|
|
im = Image.open(fn)
|
|
|
|
name = os.path.splitext(fn)[0]
|
|
|
|
w, h = im.size
|
|
|
|
if w % 8 != 0:
|
2021-09-27 10:13:51 +00:00
|
|
|
raise Exception(f"Width must be divisible by 8! ({fn} is {w}x{h})")
|
2019-04-18 14:27:27 +00:00
|
|
|
img = list(im.getdata())
|
2021-09-27 10:13:51 +00:00
|
|
|
hdrs.append(f"extern const BITMAP bmp_{name};\n")
|
|
|
|
imgs.append(f"const BITMAP bmp_{name} = {{{w}, {h}, bmp_{name}_data}};\n")
|
|
|
|
data.append(f"const uint8_t bmp_{name}_data[] = {{ {encode_pixels(img)}}};\n")
|
2019-04-18 14:27:27 +00:00
|
|
|
cnt += 1
|
|
|
|
|
|
|
|
with open("../bitmaps.c", "wt") as f:
|
|
|
|
f.write("// clang-format off\n")
|
|
|
|
f.write('#include "bitmaps.h"\n\n')
|
|
|
|
for i in range(cnt):
|
|
|
|
f.write(data[i])
|
|
|
|
f.write("\n")
|
|
|
|
for i in range(cnt):
|
|
|
|
f.write(imgs[i])
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
with open("../bitmaps.h", "wt") as f:
|
|
|
|
f.write(
|
|
|
|
"""#ifndef __BITMAPS_H__
|
2014-04-29 12:26:51 +00:00
|
|
|
#define __BITMAPS_H__
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
typedef struct {
|
2019-04-18 14:27:27 +00:00
|
|
|
uint8_t width, height;
|
|
|
|
const uint8_t *data;
|
2014-04-29 12:26:51 +00:00
|
|
|
} BITMAP;
|
|
|
|
|
2019-04-18 14:27:27 +00:00
|
|
|
"""
|
|
|
|
)
|
2014-04-29 12:26:51 +00:00
|
|
|
|
2019-04-18 14:27:27 +00:00
|
|
|
for i in range(cnt):
|
|
|
|
f.write(hdrs[i])
|
2014-04-29 12:26:51 +00:00
|
|
|
|
2019-04-18 14:27:27 +00:00
|
|
|
f.write("\n#endif\n")
|
|
|
|
f.close()
|