-
Notifications
You must be signed in to change notification settings - Fork 4
/
make_images.py
executable file
·285 lines (231 loc) · 7.53 KB
/
make_images.py
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#!/usr/bin/env python3
"""Turns a description .yaml into an image outfile file (c source)
Encoding is for potential efficiencies on a Sharp Memory Display which
has pixels laid out like this
01234567|01234567|...
01234567|01234567|...
To make RLE more effective, we use vertical column stripes 8 pixels wide.
It looks like this
0|3|6
1|4|7
2|5|8
The example above would represent a 24x3 image.
If an imagewidth is not a multiple of 8 (say it's 9), then a
full extra byte is used to hold the extra. So a 9-pixel-wide image
would be represented with 2 horizontal bytes, enough for 16 pixels.
This may seem wasteful, but the RLE can be a big help. For example
the 9 pixel wide image can only be either 0x00 or 0x80 in the second byte
which leads to favorible compression odds.
"""
from typing import Any, Dict, IO, List, Tuple
from dataclasses import dataclass
import os
import pathlib
import sys
import yaml
from PIL import Image
from lib import rle, codegen
class Error(Exception):
pass
class UnknownKeyError(Error):
pass
class ConfigSection:
"""Used to document and present keys"""
def __init__(self, section_data: Dict[str, Any]) -> None:
self.config_keys = (
('path', 'Path to the image'),
('name', 'The #define name. Default to a names that is based on the file name.'),
('invert', 'If true, then white pixels become the 1\'s in the image'),
('tile_x', 'If > 0, break the image into tiles.'),
('tile_y', 'If > 0, break the image into tiles.'),
)
known_keys = set(c[0] for c in self.config_keys)
unknown_keys = set(section_data).difference(known_keys)
if unknown_keys:
raise UnknownKeyError(
'Unknown keys in config: %s. Valid keys include: %s' % (
sorted(unknown_keys), sorted(known_keys)))
self.data = section_data
def get(self, key: str, default: Any = None) -> Any:
if default is None:
return self.data[key]
return self.data.get(key, default)
@dataclass
class ImageProps:
name: str
invert: bool
image: Image.Image
rle: List[int]
def add_image_as_tiles(
image_list: List[ImageProps],
defines: Dict[str, Any],
img: Image.Image,
name: str,
invert: bool,
tile_x: int,
tile_y: int) -> None:
if (tile_x == 0) or (tile_x > img.width):
tile_x = img.width
if (tile_y == 0) or (tile_y > img.height):
tile_y = img.height
# need to round up the values
columns = int((img.width + tile_x - 1) / tile_x)
rows = int((img.height + tile_y - 1) / tile_y)
defines[f'{name}_COLUMNS'] = columns
defines[f'{name}_ROWS'] = rows
for row in range(0, rows):
for column in range(0, columns):
left = column * tile_x
right = min(left + tile_x, img.width)
top = row * tile_y
bottom = min(top + tile_y, img.height)
image_list.append(ImageProps(
name=f'{name}_{column}_{row}',
invert=invert,
image=img.crop([left, top, right, bottom]),
rle=[]
))
def process_section(
image_list: List[ImageProps],
defines: Dict[str, Any],
section: ConfigSection) -> None:
name = section.get('Name', '')
path = section.get('path')
if not name:
name = os.path.splitext(os.path.split(path)[1])[0].upper()
name = name + "_IMG"
first_char = name[0]
if first_char < 'A' or first_char > 'Z':
name = 'I' + name
with Image.open(path) as img:
tile_x = section.get('tile_x', 0)
tile_y = section.get('tile_y', 0)
if not tile_x and not tile_y:
image_list.append(ImageProps(
name=name,
invert=section.get('invert', False),
image=img.copy(),
rle=[]
))
else:
add_image_as_tiles(
image_list,
defines,
img,
name,
section.get('invert', False),
tile_x,
tile_y)
def generate_offsets(fout: IO, image_list: List[ImageProps]) -> None:
fout.write(' // Image Offsets\n')
# 2 bytes width, 2 bytes height, 4 bytes offset
offset = len(image_list) * 8
index = 0;
for i in image_list:
line = ''.join((
' ',
'%s, ' % u16_to_hexstr(i.image.width),
'%s, ' % u16_to_hexstr(i.image.height),
'%s, ' % u32_to_hexstr(offset),
f'// {index}: {i.name} w={i.image.width}, h={i.image.height}, off={offset}\n'
))
fout.write(line)
offset += len(i.rle)
index += 1
def u16_to_hexstr(val: int) -> str:
return "0x%02X, 0x%02X" % (
val & 0xFF,
(val >> 8) & 0xFF,
)
def u32_to_hexstr(val: int) -> str:
return "0x%02X, 0x%02X, 0x%02X, 0x%02X" % (
val & 0xFF,
(val >> 8) & 0xFF,
(val >> 16) & 0xFF,
(val >> 24) & 0xFF
)
def scale_image_if_needed(
img: Image.Image, max_image_comment_size: int) -> Image.Image:
if (img.width <= max_image_comment_size and
img.height <= max_image_comment_size):
return img
if img.width >= img.height:
# make the width the larger dimension
new_width = 80
new_height = img.height * 80 // img.width
else:
new_width = img.width * 80 // img.height
new_height = 80
if new_width < 1:
new_width = 1
if new_height < 1:
new_height = 1
return img.resize((new_width, new_height))
def generate_image_comment(
index: int,
i: ImageProps,
max_image_comment_size: int,
fout: IO
) -> None:
"""Generates a comment block for an image."""
fout.write(f' // Image {index}: {i.name} w={i.image.width} h={i.image.height}\n')
img = scale_image_if_needed(i.image, max_image_comment_size)
for y in range(img.height):
fout.write(' // ')
for x in range(img.width):
fout.write('#' if bool(img.getpixel((x,y)) == i.invert) else '-')
fout.write('\n')
def dump_sources(
path: str,
image_list: List[ImageProps],
defines: Dict[str, Any],
max_image_comment_size: int) -> None:
out_path = pathlib.Path(path).with_suffix('.c')
var_name = out_path.with_suffix('').name.replace('.', '_').replace('-', '_')
define_list = []
if defines:
for k, v in sorted(defines.items()):
define_list.append((k, v))
define_list.extend((i.name, idx) for idx, i in enumerate(image_list))
codegen.dump_c_header(path, var_name, define_list)
with out_path.open('w', encoding='utf8') as fout:
fout.write('\n'.join((
'// Generated image data for %s' % var_name,
'',
'#include <inttypes.h>',
'#include <pico/platform.h>',
'',
'// Note: all values are little endian per r2040 spec',
'',
'const uint8_t %s[] __in_flash() = {' % var_name,
' 0x53, 0x48, 0x49, 0x31, // id: SHI1',
' %s, // image count (%d)' % (u32_to_hexstr(len(image_list)), len(image_list)),
''
)))
for i in image_list:
i.rle = rle.create_rle_data(i.image.height, i.image, i.invert)
generate_offsets(fout, image_list)
fout.write(' // Image data\n')
index = 0
for i in image_list:
generate_image_comment(index, i, max_image_comment_size, fout)
codegen.generate_character_data(i.rle, fout)
fout.write('};\n')
print('Wrote %s' % out_path)
def main():
"""Entry point."""
if len(sys.argv) != 2:
sys.exit('Usage: make_images.py <config.yaml>')
path = sys.argv[1]
with open(path, encoding='utf8') as f:
cfg = yaml.safe_load(f)
max_image_comment_size = cfg.get('max_image_comment_size', 80)
# creates a list of (name, image) tuples
config_sections = (ConfigSection(section) for section in cfg['images'])
image_list = []
defines = {}
for section in config_sections:
process_section(image_list, defines, section)
dump_sources(path, image_list, defines, max_image_comment_size)
if __name__ == '__main__':
main()