MAOS.git
Publicgit clone https://git.maproj.org/MAOS.gitMAOS.git / bootanimation / generate.py
generate.py276 lines · 9.4 KiBRaw
| 1 | #!/usr/bin/env python3 |
| 2 | """Generate the MAOS boot animation from the mouse glyph. |
| 3 | |
| 4 | Renders a draw-on wipe of the MAOS mouse silhouette (white on black), |
| 5 | holds, then loops a gentle fade while boot continues. Output is a |
| 6 | bootanimation.zip in this directory, ready for |
| 7 | PRODUCT_COPY_FILES into system/media/bootanimation.zip. |
| 8 | |
| 9 | The SVG path is parsed + flattened with only the standard library + Pillow |
| 10 | (no rsvg/inkscape needed), so the file is reproducible anywhere. |
| 11 | |
| 12 | Usage: ./generate.py (writes bootanimation.zip next to this script) |
| 13 | """ |
| 14 | |
| 15 | import math |
| 16 | import os |
| 17 | import zipfile |
| 18 | |
| 19 | from PIL import Image, ImageDraw |
| 20 | |
| 21 | # The MAOS mouse glyph (same path as the maproj.org tab icon / homepage card). |
| 22 | PATH_D = ( |
| 23 | "M12,22q-2.9,0-4.95-2.05T5,15V9Q5,6.1 7.05,4.05T12,2t4.95,2.05T19,9v6" |
| 24 | "q0,2.9-2.05,4.95T12,22ZM13,9h4q0-1.8-1.14-3.18T13,4.1V9ZM7,9h4V4.1Q9.28," |
| 25 | "4.45 8.14,5.82T7,9Zm5,11q2.08,0 3.54-1.46T17,15V11H7v4q0,2.07 1.46,3.54" |
| 26 | "T12,20Zm0-9Zm1-2ZM11,9Zm1,2Z" |
| 27 | ) |
| 28 | |
| 29 | SIZE = 1080 # square canvas; bootanimation centers/scales on device |
| 30 | FPS = 30 |
| 31 | DRAW_FRAMES = 24 # wipe-in duration |
| 32 | HOLD_FRAMES = 12 # full glyph hold before the loop section |
| 33 | LOOP_FRAMES = 30 # gentle fade loop while boot continues |
| 34 | GLYPH_SCALE = 560 # glyph pixel size on the canvas |
| 35 | |
| 36 | |
| 37 | def tokenize(d): |
| 38 | out, num, i = [], "", 0 |
| 39 | while i < len(d): |
| 40 | c = d[i] |
| 41 | if c in "MmLlHhVvQqTtZz": |
| 42 | if num: |
| 43 | out.append(float(num)) |
| 44 | num = "" |
| 45 | out.append(c) |
| 46 | elif c in "0123456789.+-": |
| 47 | # a sign starts a new number (but not a decimal point or exponent) |
| 48 | if c in "+-" and num and num[-1] not in "eE": |
| 49 | out.append(float(num)) |
| 50 | num = c |
| 51 | else: |
| 52 | num += c |
| 53 | # commas / whitespace just separate |
| 54 | elif num: |
| 55 | out.append(float(num)) |
| 56 | num = "" |
| 57 | i += 1 |
| 58 | if num: |
| 59 | out.append(float(num)) |
| 60 | return out |
| 61 | |
| 62 | |
| 63 | def flatten_quadratic(p0, p1, p2, steps=24): |
| 64 | pts = [] |
| 65 | for i in range(steps + 1): |
| 66 | t = i / steps |
| 67 | mt = 1 - t |
| 68 | pts.append(( |
| 69 | mt * mt * p0[0] + 2 * mt * t * p1[0] + t * t * p2[0], |
| 70 | mt * mt * p0[1] + 2 * mt * t * p1[1] + t * t * p2[1], |
| 71 | )) |
| 72 | return pts |
| 73 | |
| 74 | |
| 75 | def parse_path(d): |
| 76 | """Parse the SVG path into a list of subpaths (each a point list).""" |
| 77 | toks = tokenize(d) |
| 78 | subpaths, cur, pos, start = [], [], (0.0, 0.0), (0.0, 0.0) |
| 79 | last_q = None # control point reflected by smooth T/t |
| 80 | i = 0 |
| 81 | |
| 82 | def args(n): |
| 83 | nonlocal i |
| 84 | vals = toks[i:i + n] |
| 85 | i += n |
| 86 | return vals |
| 87 | |
| 88 | while i < len(toks): |
| 89 | t = toks[i] |
| 90 | if isinstance(t, str): |
| 91 | cmd = t |
| 92 | i += 1 |
| 93 | else: |
| 94 | cmd = last_cmd # implicit repetition of previous command |
| 95 | last_cmd = cmd |
| 96 | rel = cmd.islower() |
| 97 | c = cmd.upper() |
| 98 | |
| 99 | if c == "M": |
| 100 | x, y = args(2) |
| 101 | if rel: |
| 102 | x, y = pos[0] + x, pos[1] + y |
| 103 | if cur: |
| 104 | subpaths.append(cur) |
| 105 | cur, pos, start = [], (x, y), (x, y) |
| 106 | last_q = None |
| 107 | # subsequent implicit pairs are linetos |
| 108 | while i < len(toks) and not isinstance(toks[i], str): |
| 109 | x, y = args(2) |
| 110 | if rel: |
| 111 | x, y = pos[0] + x, pos[1] + y |
| 112 | cur.append((x, y)) |
| 113 | pos = (x, y) |
| 114 | elif c == "Z": |
| 115 | if cur: |
| 116 | cur.append(start) |
| 117 | subpaths.append(cur) |
| 118 | cur = [] |
| 119 | pos = start |
| 120 | last_q = None |
| 121 | elif c in "Q": |
| 122 | while i < len(toks) and not isinstance(toks[i], str): |
| 123 | x1, y1, x, y = args(4) |
| 124 | if rel: |
| 125 | x1, y1 = pos[0] + x1, pos[1] + y1 |
| 126 | x, y = pos[0] + x, pos[1] + y |
| 127 | cur += flatten_quadratic(pos, (x1, y1), (x, y))[1:] |
| 128 | last_q = (x1, y1) |
| 129 | pos = (x, y) |
| 130 | elif c in "T": |
| 131 | while i < len(toks) and not isinstance(toks[i], str): |
| 132 | x, y = args(2) |
| 133 | if rel: |
| 134 | x, y = pos[0] + x, pos[1] + y |
| 135 | # reflection of previous control point (or current point) |
| 136 | if last_q is None: |
| 137 | x1, y1 = pos |
| 138 | else: |
| 139 | x1, y1 = 2 * pos[0] - last_q[0], 2 * pos[1] - last_q[1] |
| 140 | cur += flatten_quadratic(pos, (x1, y1), (x, y))[1:] |
| 141 | last_q = (x1, y1) |
| 142 | pos = (x, y) |
| 143 | elif c == "H": |
| 144 | while i < len(toks) and not isinstance(toks[i], str): |
| 145 | (x,) = args(1) |
| 146 | if rel: |
| 147 | x = pos[0] + x |
| 148 | cur.append((x, pos[1])) |
| 149 | pos = (x, pos[1]) |
| 150 | last_q = None |
| 151 | elif c == "V": |
| 152 | while i < len(toks) and not isinstance(toks[i], str): |
| 153 | (y,) = args(1) |
| 154 | if rel: |
| 155 | y = pos[1] + y |
| 156 | cur.append((pos[0], y)) |
| 157 | pos = (pos[0], y) |
| 158 | last_q = None |
| 159 | else: |
| 160 | raise ValueError(f"unsupported path command: {cmd}") |
| 161 | if cur: |
| 162 | subpaths.append(cur) |
| 163 | return subpaths |
| 164 | |
| 165 | |
| 166 | def render_glyph(size): |
| 167 | """Rasterize the glyph white-on-transparent at the given pixel size. |
| 168 | |
| 169 | Uses the nonzero winding rule across ALL subpaths as one path (matching |
| 170 | SVG's default fill-rule): inner detail subpaths wound opposite to the |
| 171 | outline carve the button/panel voids. PIL fills each polygon |
| 172 | independently, so the winding is resolved here with a scanline fill. |
| 173 | """ |
| 174 | subs = parse_path(PATH_D) |
| 175 | s = size / 24.0 |
| 176 | scaled = [[(x * s, y * s) for x, y in sub] for sub in subs] |
| 177 | # edge list with direction sign for the winding count |
| 178 | edges = [] |
| 179 | for sub in scaled: |
| 180 | for i in range(len(sub)): |
| 181 | x0, y0 = sub[i] |
| 182 | x1, y1 = sub[(i + 1) % len(sub)] |
| 183 | if y0 != y1: |
| 184 | edges.append((x0, y0, x1, y1)) |
| 185 | img = Image.new("L", (size, size), 0) |
| 186 | px = img.load() |
| 187 | for y in range(size): |
| 188 | yc = y + 0.5 |
| 189 | crossings = [] |
| 190 | for x0, y0, x1, y1 in edges: |
| 191 | if (y0 <= yc < y1) or (y1 <= yc < y0): |
| 192 | x = x0 + (yc - y0) * (x1 - x0) / (y1 - y0) |
| 193 | crossings.append((x, 1 if y1 > y0 else -1)) |
| 194 | crossings.sort() |
| 195 | winding, prev = 0, 0.0 |
| 196 | for x, d in crossings: |
| 197 | if winding != 0 and x > prev: |
| 198 | for xx in range(max(int(prev), 0), min(int(x) + 1, size)): |
| 199 | px[xx, y] = 255 |
| 200 | winding += d |
| 201 | prev = x |
| 202 | return img |
| 203 | |
| 204 | |
| 205 | def frame(reveal, alpha): |
| 206 | """One SIZE×SIZE frame: black bg, glyph revealed top-to-`reveal`, at `alpha`.""" |
| 207 | canvas = Image.new("RGB", (SIZE, SIZE), (0, 0, 0)) |
| 208 | glyph = render_glyph(GLYPH_SCALE) |
| 209 | ox, oy = (SIZE - GLYPH_SCALE) // 2, (SIZE - GLYPH_SCALE) // 2 |
| 210 | if reveal < 1.0: |
| 211 | # wipe: keep only rows above the reveal line (with a soft edge) |
| 212 | cutoff = int(GLYPH_SCALE * reveal) |
| 213 | mask = Image.new("L", (GLYPH_SCALE, GLYPH_SCALE), 0) |
| 214 | md = ImageDraw.Draw(mask) |
| 215 | md.rectangle([0, 0, GLYPH_SCALE, max(cutoff - 8, 0)], fill=255) |
| 216 | if 0 < cutoff < GLYPH_SCALE: |
| 217 | for r in range(8): |
| 218 | md.line([0, cutoff - 8 + r, GLYPH_SCALE, cutoff - 8 + r], |
| 219 | fill=int(255 * (1 - r / 8))) |
| 220 | cut = Image.new("L", (GLYPH_SCALE, GLYPH_SCALE), 0) |
| 221 | cut.paste(mask) |
| 222 | empty = Image.new("L", (GLYPH_SCALE, GLYPH_SCALE), 0) |
| 223 | glyph = Image.composite(glyph, empty, cut) |
| 224 | if alpha < 1.0: |
| 225 | glyph = glyph.point(lambda v: int(v * alpha)) |
| 226 | white = Image.new("RGB", (GLYPH_SCALE, GLYPH_SCALE), (255, 255, 255)) |
| 227 | canvas.paste(white, (ox, oy), glyph) |
| 228 | return canvas |
| 229 | |
| 230 | |
| 231 | def main(): |
| 232 | here = os.path.dirname(os.path.abspath(__file__)) |
| 233 | work = os.path.join(here, "work") |
| 234 | for part in ("part0", "part1"): |
| 235 | os.makedirs(os.path.join(work, part), exist_ok=True) |
| 236 | |
| 237 | # part0: draw-on wipe, then hold — plays once. |
| 238 | frames = [] |
| 239 | for f in range(DRAW_FRAMES): |
| 240 | t = (f + 1) / DRAW_FRAMES |
| 241 | ease = 1 - (1 - t) ** 2 # ease-out: fast start, soft landing |
| 242 | frames.append(("part0", frame(ease, 1.0))) |
| 243 | for _ in range(HOLD_FRAMES): |
| 244 | frames.append(("part0", frame(1.0, 1.0))) |
| 245 | # part1: gentle fade loop while boot continues — loops forever. |
| 246 | for f in range(LOOP_FRAMES): |
| 247 | a = 0.75 + 0.25 * math.cos(2 * math.pi * f / LOOP_FRAMES) |
| 248 | frames.append(("part1", frame(1.0, a))) |
| 249 | |
| 250 | counters = {"part0": 0, "part1": 0} |
| 251 | for part, img in frames: |
| 252 | counters[part] += 1 |
| 253 | img.save(os.path.join(work, part, f"{counters[part]:05d}.png")) |
| 254 | |
| 255 | desc = ( |
| 256 | f"{SIZE} {SIZE} {FPS}\n" |
| 257 | f"p 1 0 part0\n" |
| 258 | f"p 0 0 part1\n" |
| 259 | ) |
| 260 | with open(os.path.join(work, "desc.txt"), "w") as f: |
| 261 | f.write(desc) |
| 262 | |
| 263 | # bootanimation.zip must be STORED (uncompressed) — the loader mmaps it. |
| 264 | out = os.path.join(here, "bootanimation.zip") |
| 265 | with zipfile.ZipFile(out, "w", zipfile.ZIP_STORED) as z: |
| 266 | z.write(os.path.join(work, "desc.txt"), "desc.txt") |
| 267 | for part in ("part0", "part1"): |
| 268 | d = os.path.join(work, part) |
| 269 | for name in sorted(os.listdir(d)): |
| 270 | z.write(os.path.join(d, name), f"{part}/{name}") |
| 271 | total = DRAW_FRAMES + HOLD_FRAMES + LOOP_FRAMES |
| 272 | print(f"wrote {out}: {total} frames, {os.path.getsize(out)} bytes") |
| 273 | |
| 274 | |
| 275 | if __name__ == "__main__": |
| 276 | main() |