Files
JIANT/tools/gen_logo.py
T

99 lines
5.2 KiB
Python

#!/usr/bin/env python3
# SPDX-License-Identifier: GPL-3.0-only
"""V10.6l: the power-on logo, "jiant fm" (assets/ui-art/signature/jiant-fm-logo.jpg), as the owner writes it: his pen's
path read off his screen recording (below), smoothed (Catmull-Rom) and resampled every ~STEP px at
the splash's scale. Out: firmware/src/ui_signature.h, SIG_PTS x y bytes a point, a stroke starting where y has bit 7 set.
gen_logo.py OUT.h [--png PREVIEW.png]
"""
import sys, math
W, H, STEP = 220, 126, 3.0
STROKES = [ # the owner's own pen path, from his screen recording of writing it (every ink pixel timed by the frame it
# appeared in): in its pixels (936 x 940 canvas), in the order and direction he wrote them
# "jiant" in one line: the j up from its foot, over, down to its loop, up to the i's top, down, up to the a's top,
# the a's bowl, the n's two arches, up to the t's top, down its stem and out to the right
[(105, 580), (112, 515), (140, 410), (175, 340), (215, 280), (255, 240), (300, 210), (330, 201), (352, 215),
(362, 245), (360, 295), (350, 330), (325, 390), (290, 450), (260, 495), (250, 505), (257, 500), (280, 440),
(315, 380), (355, 320), (390, 285), (440, 235), (445, 229), (415, 305), (390, 370), (382, 388), (410, 345),
(450, 300), (490, 265), (537, 235), (525, 248), (500, 270), (483, 305), (478, 345), (485, 370), (505, 371),
(525, 355), (535, 325), (537, 290), (540, 270), (556, 320), (572, 367), (590, 310), (610, 262), (622, 310),
(635, 362), (655, 310), (677, 267), (690, 320), (702, 372), (722, 335), (740, 290), (765, 230), (785, 157),
(778, 200), (780, 290), (787, 365), (795, 370), (840, 340), (897, 296)],
[(686, 200), (780, 188), (877, 179)], # the t's bar
[(429, 133), (448, 153)], # the i's dot
[(267, 652), (290, 595), (315, 540), (350, 475), (380, 455), (425, 445), (486, 437)], # the F, up
[(320, 539), (370, 522), (417, 506)], # its bar
[(415, 649), (475, 570), (530, 515), (575, 477), (600, 465), (614, 470), (615, 490), (605, 535), (580, 595),
(550, 640), (530, 655), (570, 585), (600, 552), (630, 520), (660, 490), (680, 485), (686, 495), (683, 540),
(670, 595), (642, 632), (680, 580), (720, 540), (760, 510), (800, 487), (814, 495), (814, 530), (805, 590),
(790, 640), (772, 672)], # the m in one line
]
def catmull(p, n):
out = []
for i in range(len(p) - 1):
p0, p1, p2, p3 = p[max(i - 1, 0)], p[i], p[i + 1], p[min(i + 2, len(p) - 1)]
for k in range(n):
t = k / n
out.append(tuple(0.5 * (2 * p1[j] + (p2[j] - p0[j]) * t + (2 * p0[j] - 5 * p1[j] + 4 * p2[j] - p3[j]) * t * t +
(3 * p1[j] - p0[j] - 3 * p2[j] + p3[j]) * t ** 3) for j in (0, 1)))
out.append(p[-1])
return out
def main():
xs = [x for s in STROKES for x, _ in s]
ys = [y for s in STROKES for _, y in s]
pad = 8 # (the pen's half width, in the drawing's pixels, a margin)
x0, x1, y0, y1 = min(xs) - pad, max(xs) + pad, min(ys) - pad, max(ys) + pad
sc = min((W - 6) / (x1 - x0), (H - 6) / (y1 - y0))
ox, oy = (W - (x1 - x0) * sc) / 2, (H - (y1 - y0) * sc) / 2
data, n, prev = [], 0, []
for s in STROKES:
c = [((x - x0) * sc + ox, (y - y0) * sc + oy) for x, y in catmull(s, 12)]
pts, acc = [c[0]], 0.0
for a, b in zip(c, c[1:]):
acc += math.dist(a, b)
if acc >= STEP:
pts.append(b)
acc = 0.0
if pts[-1] != c[-1]:
pts.append(c[-1])
q = []
for x, y in pts:
p = (int(round(x)), int(round(y)))
if not q or p != q[-1]:
q.append(p)
for i, (x, y) in enumerate(q):
data += [x, y | (128 if i == 0 else 0)]
n += 1
prev.append(q)
lines = ["/* generated by tools/gen_logo.py from assets/ui-art/signature/jiant-fm-logo.jpg: the power-on logo */",
"#pragma once", f"#define SIG_W {W}", f"#define SIG_H {H}", f"#define SIG_N {n}",
"static const uint8_t SIG_PTS[SIG_N * 2] = {"]
for i in range(0, len(data), 24):
lines.append(" " + ", ".join(str(v) for v in data[i:i + 24]) + ",")
lines.append("};")
open(sys.argv[1], "w").write("\n".join(lines) + "\n")
print(f"{len(STROKES)} strokes, {n} points, {len(data)} bytes")
if "--png" in sys.argv:
from PIL import Image, ImageDraw
z = 4
pv = Image.new("RGB", (240 * z, (H + 12) * z))
d = ImageDraw.Draw(pv)
X0 = (240 - W) // 2
for q in prev:
for x, y in q:
f = x / W
st = [(255, 80, 90), (255, 0, 230), (140, 0, 240), (40, 0, 255)]
k = min(int(f * 3), 2); u = f * 3 - k
col = tuple(int(st[k][j] + (st[k + 1][j] - st[k][j]) * u) for j in range(3))
d.ellipse([((X0 + x - 3) * z, (y + 6 - 3) * z), ((X0 + x + 3) * z, (y + 6 + 3) * z)], fill=col)
pv.save(sys.argv[sys.argv.index("--png") + 1])
if __name__ == "__main__":
main()