Skip to content

Commit c2be9c1

Browse files
elpekeninArdakilic
authored andcommitted
[Feature] Some metadata on QGF/QFF files (qmk#20101)
1 parent 97f089b commit c2be9c1

File tree

4 files changed

+146
-52
lines changed

4 files changed

+146
-52
lines changed

lib/python/qmk/cli/painter/convert_graphics.py

+10-24
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
"""This script tests QGF functionality.
22
"""
3-
import re
4-
import datetime
53
from io import BytesIO
64
from qmk.path import normpath
7-
from qmk.painter import render_header, render_source, render_license, render_bytes, valid_formats
5+
from qmk.painter import generate_subs, render_header, render_source, valid_formats
86
from milc import cli
97
from PIL import Image
108

119

1210
@cli.argument('-v', '--verbose', arg_only=True, action='store_true', help='Turns on verbose output.')
1311
@cli.argument('-i', '--input', required=True, help='Specify input graphic file.')
1412
@cli.argument('-o', '--output', default='', help='Specify output directory. Defaults to same directory as input.')
15-
@cli.argument('-f', '--format', required=True, help='Output format, valid types: %s' % (', '.join(valid_formats.keys())))
13+
@cli.argument('-f', '--format', required=True, help=f'Output format, valid types: {", ".join(valid_formats.keys())}')
1614
@cli.argument('-r', '--no-rle', arg_only=True, action='store_true', help='Disables the use of RLE when encoding images.')
1715
@cli.argument('-d', '--no-deltas', arg_only=True, action='store_true', help='Disables the use of delta frames when encoding animations.')
1816
@cli.argument('-w', '--raw', arg_only=True, action='store_true', help='Writes out the QGF file as raw data instead of c/h combo.')
@@ -51,43 +49,31 @@ def painter_convert_graphics(cli):
5149

5250
# Convert the image to QGF using PIL
5351
out_data = BytesIO()
54-
input_img.save(out_data, "QGF", use_deltas=(not cli.args.no_deltas), use_rle=(not cli.args.no_rle), qmk_format=format, verbose=cli.args.verbose)
52+
metadata = []
53+
input_img.save(out_data, "QGF", use_deltas=(not cli.args.no_deltas), use_rle=(not cli.args.no_rle), qmk_format=format, verbose=cli.args.verbose, metadata=metadata)
5554
out_bytes = out_data.getvalue()
5655

5756
if cli.args.raw:
58-
raw_file = cli.args.output / (cli.args.input.stem + ".qgf")
57+
raw_file = cli.args.output / f"{cli.args.input.stem}.qgf"
5958
with open(raw_file, 'wb') as raw:
6059
raw.write(out_bytes)
6160
return
6261

6362
# Work out the text substitutions for rendering the output data
64-
subs = {
65-
'generated_type': 'image',
66-
'var_prefix': 'gfx',
67-
'generator_command': f'qmk painter-convert-graphics -i {cli.args.input.name} -f {cli.args.format}',
68-
'year': datetime.date.today().strftime("%Y"),
69-
'input_file': cli.args.input.name,
70-
'sane_name': re.sub(r"[^a-zA-Z0-9]", "_", cli.args.input.stem),
71-
'byte_count': len(out_bytes),
72-
'bytes_lines': render_bytes(out_bytes),
73-
'format': cli.args.format,
74-
}
75-
76-
# Render the license
77-
subs.update({'license': render_license(subs)})
63+
args_str = " ".join((f"--{arg} {getattr(cli.args, arg.replace('-', '_'))}" for arg in ["input", "output", "format", "no-rle", "no-deltas"]))
64+
command = f"qmk painter-convert-graphics {args_str}"
65+
subs = generate_subs(cli, out_bytes, image_metadata=metadata, command=command)
7866

7967
# Render and write the header file
8068
header_text = render_header(subs)
81-
header_file = cli.args.output / (cli.args.input.stem + ".qgf.h")
69+
header_file = cli.args.output / f"{cli.args.input.stem}.qgf.h"
8270
with open(header_file, 'w') as header:
8371
print(f"Writing {header_file}...")
8472
header.write(header_text)
85-
header.close()
8673

8774
# Render and write the source file
8875
source_text = render_source(subs)
89-
source_file = cli.args.output / (cli.args.input.stem + ".qgf.c")
76+
source_file = cli.args.output / f"{cli.args.input.stem}.qgf.c"
9077
with open(source_file, 'w') as source:
9178
print(f"Writing {source_file}...")
9279
source.write(source_text)
93-
source.close()

lib/python/qmk/cli/painter/make_font.py

+11-25
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
"""This script automates the conversion of font files into a format QMK firmware understands.
22
"""
33

4-
import re
5-
import datetime
64
from io import BytesIO
75
from qmk.path import normpath
8-
from qmk.painter_qff import QFFFont
9-
from qmk.painter import render_header, render_source, render_license, render_bytes, valid_formats
6+
from qmk.painter_qff import _generate_font_glyphs_list, QFFFont
7+
from qmk.painter import generate_subs, render_header, render_source, valid_formats
108
from milc import cli
119

1210

@@ -31,7 +29,7 @@ def painter_make_font_image(cli):
3129
@cli.argument('-o', '--output', default='', help='Specify output directory. Defaults to same directory as input.')
3230
@cli.argument('-n', '--no-ascii', arg_only=True, action='store_true', help='Disables output of the full ASCII character set (0x20..0x7E), exporting only the glyphs specified.')
3331
@cli.argument('-u', '--unicode-glyphs', default='', help='Also generate the specified unicode glyphs.')
34-
@cli.argument('-f', '--format', required=True, help='Output format, valid types: %s' % (', '.join(valid_formats.keys())))
32+
@cli.argument('-f', '--format', required=True, help=f'Output format, valid types: {", ".join(valid_formats.keys())}')
3533
@cli.argument('-r', '--no-rle', arg_only=True, action='store_true', help='Disable the use of RLE to minimise converted image size.')
3634
@cli.argument('-w', '--raw', arg_only=True, action='store_true', help='Writes out the QFF file as raw data instead of c/h combo.')
3735
@cli.subcommand('Converts an input font image to something QMK firmware understands')
@@ -53,43 +51,31 @@ def painter_convert_font_image(cli):
5351

5452
# Render out the data
5553
out_data = BytesIO()
56-
font.save_to_qff(format, (False if cli.args.no_rle else True), out_data)
54+
font.save_to_qff(format, not cli.args.no_rle, out_data)
5755
out_bytes = out_data.getvalue()
5856

5957
if cli.args.raw:
60-
raw_file = cli.args.output / (cli.args.input.stem + ".qff")
58+
raw_file = cli.args.output / f"{cli.args.input.stem}.qff"
6159
with open(raw_file, 'wb') as raw:
6260
raw.write(out_bytes)
6361
return
6462

6563
# Work out the text substitutions for rendering the output data
66-
subs = {
67-
'generated_type': 'font',
68-
'var_prefix': 'font',
69-
'generator_command': f'qmk painter-convert-font-image -i {cli.args.input.name} -f {cli.args.format}',
70-
'year': datetime.date.today().strftime("%Y"),
71-
'input_file': cli.args.input.name,
72-
'sane_name': re.sub(r"[^a-zA-Z0-9]", "_", cli.args.input.stem),
73-
'byte_count': len(out_bytes),
74-
'bytes_lines': render_bytes(out_bytes),
75-
'format': cli.args.format,
76-
}
77-
78-
# Render the license
79-
subs.update({'license': render_license(subs)})
64+
args_str = " ".join((f"--{arg} {getattr(cli.args, arg.replace('-', '_'))}" for arg in ["input", "output", "no-ascii", "unicode-glyphs", "format", "no-rle"]))
65+
command = f"qmk painter-convert-font-image {args_str}"
66+
metadata = {"glyphs": _generate_font_glyphs_list(not cli.args.no_ascii, cli.args.unicode_glyphs)}
67+
subs = generate_subs(cli, out_bytes, font_metadata=metadata, command=command)
8068

8169
# Render and write the header file
8270
header_text = render_header(subs)
83-
header_file = cli.args.output / (cli.args.input.stem + ".qff.h")
71+
header_file = cli.args.output / f"{cli.args.input.stem}.qff.h"
8472
with open(header_file, 'w') as header:
8573
print(f"Writing {header_file}...")
8674
header.write(header_text)
87-
header.close()
8875

8976
# Render and write the source file
9077
source_text = render_source(subs)
91-
source_file = cli.args.output / (cli.args.input.stem + ".qff.c")
78+
source_file = cli.args.output / f"{cli.args.input.stem}.qff.c"
9279
with open(source_file, 'w') as source:
9380
print(f"Writing {source_file}...")
9481
source.write(source_text)
95-
source.close()

lib/python/qmk/painter.py

+102
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Functions that help us work with Quantum Painter's file formats.
22
"""
3+
import datetime
34
import math
45
import re
56
from string import Template
@@ -79,6 +80,105 @@
7980
}
8081
}
8182

83+
84+
def _render_text(values):
85+
# FIXME: May need more chars with GIFs containing lots of frames (or longer durations)
86+
return "|".join([f"{i:4d}" for i in values])
87+
88+
89+
def _render_numeration(metadata):
90+
return _render_text(range(len(metadata)))
91+
92+
93+
def _render_values(metadata, key):
94+
return _render_text([i[key] for i in metadata])
95+
96+
97+
def _render_image_metadata(metadata):
98+
size = metadata.pop(0)
99+
100+
lines = [
101+
"// Image's metadata",
102+
"// ----------------",
103+
f"// Width: {size['width']}",
104+
f"// Height: {size['height']}",
105+
]
106+
107+
if len(metadata) == 1:
108+
lines.append("// Single frame")
109+
110+
else:
111+
lines.extend([
112+
f"// Frame: {_render_numeration(metadata)}",
113+
f"// Duration(ms): {_render_values(metadata, 'delay')}",
114+
f"// Compression: {_render_values(metadata, 'compression')} >> See qp.h, painter_compression_t",
115+
f"// Delta: {_render_values(metadata, 'delta')}",
116+
])
117+
118+
deltas = []
119+
for i, v in enumerate(metadata):
120+
# Not a delta frame, go to next one
121+
if not v["delta"]:
122+
continue
123+
124+
# Unpack rect's coords
125+
l, t, r, b = v["delta_rect"]
126+
127+
delta_px = (r - l) * (b - t)
128+
px = size["width"] * size["height"]
129+
130+
# FIXME: May need need more chars here too
131+
deltas.append(f"// Frame {i:3d}: ({l:3d}, {t:3d}) - ({r:3d}, {b:3d}) >> {delta_px:4d}/{px:4d} pixels ({100*delta_px/px:.2f}%)")
132+
133+
if deltas:
134+
lines.append("// Areas on delta frames")
135+
lines.extend(deltas)
136+
137+
return "\n".join(lines)
138+
139+
140+
def generate_subs(cli, out_bytes, *, font_metadata=None, image_metadata=None, command):
141+
if font_metadata is not None and image_metadata is not None:
142+
raise ValueError("Cant generate subs for font and image at the same time")
143+
144+
subs = {
145+
"year": datetime.date.today().strftime("%Y"),
146+
"input_file": cli.args.input.name,
147+
"sane_name": re.sub(r"[^a-zA-Z0-9]", "_", cli.args.input.stem),
148+
"byte_count": len(out_bytes),
149+
"bytes_lines": render_bytes(out_bytes),
150+
"format": cli.args.format,
151+
"generator_command": command,
152+
}
153+
154+
if font_metadata is not None:
155+
subs.update({
156+
"generated_type": "font",
157+
"var_prefix": "font",
158+
# not using triple quotes to avoid extra indentation/weird formatted code
159+
"metadata": "\n".join([
160+
"// Font's metadata",
161+
"// ---------------",
162+
f"// Glyphs: {', '.join([i for i in font_metadata['glyphs']])}",
163+
]),
164+
})
165+
166+
elif image_metadata is not None:
167+
subs.update({
168+
"generated_type": "image",
169+
"var_prefix": "gfx",
170+
"generator_command": command,
171+
"metadata": _render_image_metadata(image_metadata),
172+
})
173+
174+
else:
175+
raise ValueError("Pass metadata for either an image or a font")
176+
177+
subs.update({"license": render_license(subs)})
178+
179+
return subs
180+
181+
82182
license_template = """\
83183
// Copyright ${year} QMK -- generated source code only, ${generated_type} retains original copyright
84184
// SPDX-License-Identifier: GPL-2.0-or-later
@@ -110,6 +210,8 @@ def render_header(subs):
110210

111211
source_file_template = """\
112212
${license}
213+
${metadata}
214+
113215
#include <qp.h>
114216
115217
const uint32_t ${var_prefix}_${sane_name}_length = ${byte_count};

lib/python/qmk/painter_qgf.py

+23-3
Original file line numberDiff line numberDiff line change
@@ -327,8 +327,9 @@ def _compress_image(frame, last_frame, *, use_rle, use_deltas, format_, **_kwarg
327327

328328

329329
# Helper function to save each frame to the output file
330-
def _write_frame(idx, frame, last_frame, *, fp, frame_offsets, **kwargs):
331-
# Not an argument of the function as it would consume from **kwargs
330+
def _write_frame(idx, frame, last_frame, *, fp, frame_offsets, metadata, **kwargs):
331+
# Not an argument of the function as it would then not be part of kwargs
332+
# This would cause an issue with `_compress_image(**kwargs)` missing an argument
332333
format_ = kwargs["format_"]
333334

334335
# (potentially) Apply RLE and/or delta, and work out output image's information
@@ -370,6 +371,21 @@ def _write_frame(idx, frame, last_frame, *, fp, frame_offsets, **kwargs):
370371
vprint(f'{f"Frame {idx:3d} delta":26s} {fp.tell():5d}d / {fp.tell():04X}h')
371372
delta_descriptor.write(fp)
372373

374+
# Store metadata, showed later in a comment in the generated file
375+
frame_metadata = {
376+
"compression": frame_descriptor.compression,
377+
"delta": frame_descriptor.is_delta,
378+
"delay": frame_descriptor.delay,
379+
}
380+
if frame_metadata["delta"]:
381+
frame_metadata.update({"delta_rect": [
382+
delta_descriptor.left,
383+
delta_descriptor.top,
384+
delta_descriptor.right,
385+
delta_descriptor.bottom,
386+
]})
387+
metadata.append(frame_metadata)
388+
373389
# Write out the data for this frame to the output
374390
data_descriptor = QGFFrameDataDescriptorV1()
375391
data_descriptor.data = image_data
@@ -383,6 +399,10 @@ def _save(im, fp, _filename):
383399
# Work out from the parameters if we need to do anything special
384400
encoderinfo = im.encoderinfo.copy()
385401

402+
# Store image file in metadata structure
403+
metadata = encoderinfo.get("metadata", [])
404+
metadata.append({"width": im.width, "height": im.height})
405+
386406
# Helper for prints, noop taking any args if not verbose
387407
global vprint
388408
verbose = encoderinfo.get("verbose", False)
@@ -417,7 +437,7 @@ def _save(im, fp, _filename):
417437
frame_offsets.write(fp)
418438

419439
# Iterate over each if the input frames, writing it to the output in the process
420-
write_frame = functools.partial(_write_frame, format_=encoderinfo["qmk_format"], fp=fp, use_deltas=encoderinfo.get("use_deltas", True), use_rle=encoderinfo.get("use_rle", True), frame_offsets=frame_offsets)
440+
write_frame = functools.partial(_write_frame, format_=encoderinfo["qmk_format"], fp=fp, use_deltas=encoderinfo.get("use_deltas", True), use_rle=encoderinfo.get("use_rle", True), frame_offsets=frame_offsets, metadata=metadata)
421441
for_all_frames(write_frame)
422442

423443
# Go back and update the graphics descriptor now that we can determine the final file size

0 commit comments

Comments
 (0)