-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbitmap_generator.py
More file actions
176 lines (142 loc) · 5.74 KB
/
bitmap_generator.py
File metadata and controls
176 lines (142 loc) · 5.74 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
Import("projenv")
# extra post action for converting face images from png to source code
try:
from PIL import Image
except ImportError:
projenv.Execute(
projenv.VerboseAction(
'$PYTHONEXE -m pip install "pillow==12.0.0" ',
"Installing build Python dependencies",
)
)
from PIL import Image
import os
import re
import json
from datetime import datetime
from math import floor
class Bitmap:
def __init__(self, name, data, size):
self.name = name
self.data = data
self.size = size
def matrix_to_point(bit, row):
offset = floor(row / 8) * 8
return ((7 - bit + offset), (row - offset))
def process_image(image):
width, height = image.size
ret = []
for row in range(0, width):
result = 0
for bit in range(7, -1, -1):
point = matrix_to_point(bit, row)
data = image.getpixel(point)
result = (result << 1) | (data & 1)
ret.append(result)
return ret
def str_to_identifier(string):
return "".join(
re.findall(
"[_a-zA-Z][_a-zA-Z0-9]*",
string
)
)
bitmap_file_types = [".png", ".bmp"]
bitmaps_dir = "src/Assets/Bitmaps"
source_filename = "Standard"
bitmaps_header_location = os.path.join(bitmaps_dir, f"{source_filename}.h")
bitmaps_source_location = os.path.join(bitmaps_dir, f"{source_filename}.cpp")
# iterate over bitmap asset directories, note down unique folders as namespaces, note down latest modify time for later comparison
namespaces = []
last_mtime = 0
for root, dirs, files in os.walk(bitmaps_dir):
namespaces += dirs
for file in files:
if os.path.splitext(file)[1] not in bitmap_file_types:
continue
path = os.path.join(root, file)
mtime = os.path.getmtime(path)
if mtime > last_mtime:
last_mtime = mtime
if not (os.path.exists(bitmaps_source_location) and os.path.exists(bitmaps_header_location) and os.path.getmtime(bitmaps_source_location) > last_mtime and os.path.getmtime(bitmaps_header_location) > last_mtime):
print("Bitmap assets have been updated, regenerating code for bitmaps...")
print(f"Found {len(namespaces)} namespaces: {', '.join(namespaces)}")
result = {n : [] for n in namespaces}
metadata = {}
# main code generator loop
for root, dirs, files in os.walk(bitmaps_dir):
namespace = os.path.basename(root)
for file in files:
file_extension = os.path.splitext(file)[1]
if file == "metadata.json":
# load metadata
with open(os.path.join(root, file)) as metadata_file:
metadata[namespace] = json.load(metadata_file)
continue
if os.path.splitext(file)[1] not in bitmap_file_types:
continue
path = os.path.join(root, file)
image_name = str_to_identifier("".join(file.split(".")[:-1]))
im = Image.open(path)
im = im.convert("1", dither=Image.Dither.NONE)
converted = process_image(im)
print(f"{file} {im.size} -> Bitmaps::{namespace}::{image_name}")
result[namespace].append(
Bitmap (
name = image_name,
data = ", ".join(map(lambda x: format(x, "#04x"), converted)),
size = im.size[0]
)
)
timestamp = datetime.now()
# sort namespaces and bitmaps alphabetically
for namespace in result:
result[namespace].sort(key = lambda x: x.name)
result = dict(sorted(result.items()))
# parse metadata
print("Parsing metadata...")
for namespace in metadata.keys():
metadata[namespace]["randomizerExceptions"] = list(map(lambda filename: str_to_identifier("".join(filename.split(".")[:-1])), metadata[namespace]["randomizerExceptions"]))
print("Generating bitmap code...")
# generate and save header file
out = f"""#pragma once
// This header file is automatically generated during the build process!
// To modify, edit the original png files and build again.
#include <vector>
#include "Arduino.h"
#include "Typedefs.h"
namespace Bitmaps {{
extern const char* BITMAP_GENERATION_TIMESTAMP;\n
"""
for namespace in result:
out += f" namespace {namespace} {{\n extern Bitmap\n"
out += ",\n".join(f" {bitmap.name}[{bitmap.size}]" for bitmap in result[namespace])
out += ";\n }\n\n"
out += " extern std::vector<Bitmap*>\n"
out += ",\n".join(f" {namespace}Bitmaps" for namespace in result)
out += ";\n"
out += "}"
with open(bitmaps_header_location, "w") as output_file:
output_file.write(out)
# generate and save cpp file
out = f"""#include \"{source_filename}.h\"
// This source file is automatically generated during the build process!
// To modify, edit the original png files and build again.
namespace Bitmaps {{
const char* BITMAP_GENERATION_TIMESTAMP = \"{timestamp}\";\n
"""
for namespace in result:
out += f" namespace {namespace} {{\n"
for bitmap in result[namespace]:
out += f" Bitmap {bitmap.name}[{bitmap.size}] {{ {bitmap.data} }};\n"
out += " }\n\n"
for namespace in result:
out += f" std::vector<Bitmap*> {namespace}Bitmaps {{\n"
out += ",\n".join(f" {namespace}::{bitmap.name}" for bitmap in filter(lambda bitmap: bitmap.name not in metadata[namespace]["randomizerExceptions"], result[namespace]))
out += "\n };\n\n"
out += "}"
with open(bitmaps_source_location, "w") as output_file:
output_file.write(out)
print("Finished bitmap code generation.")
else:
print("No changes in bitmaps, skipping bitmap code generation.")