seq / rate steps, jiffy delays, CUR hotspots, INFO title; AF_ICON clear is UNSUPPORTED. Tested against Pillow-written ICO / CUR frames assembled into ANI files.
67 lines
3.4 KiB
Python
67 lines
3.4 KiB
Python
# ANI fixtures (tools/fixtures.py): animated cursors assembled here around ICO / CUR frames written by Pillow (32 x 32
|
|
# RGBA; the CUR ones patched from Pillow's ICO with hotspots): with seq + rate chunks, with neither, with INFO text, and
|
|
# one with AF_ICON clear (UNSUPPORTED). References: Pillow's decode of every frame, the step order and delays here.
|
|
# tests/gen/ani.luau { cases = { { name, data, w, h, steps = { { frame, delay } }, frames = { rgba }, hotspot?,
|
|
# title?, unsupported? } } }
|
|
import importlib.util, os, struct
|
|
|
|
def _load(n):
|
|
spec = importlib.util.spec_from_file_location(n, os.path.join(os.path.dirname(os.path.abspath(__file__)), n + ".py"))
|
|
m = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(m)
|
|
return m
|
|
|
|
R, T = _load("_raster_ref"), _load("_tex_reference")
|
|
|
|
|
|
def chunk(cid, data):
|
|
return cid + struct.pack("<I", len(data)) + data + (b"\0" if len(data) % 2 else b"")
|
|
|
|
|
|
def riff_list(kind, items):
|
|
return chunk(b"LIST", kind + b"".join(chunk(c, d) for c, d in items))
|
|
|
|
|
|
def ani(icons, steps=None, rates=None, disp=10, flags=1, title=None):
|
|
n = len(icons)
|
|
ns = len(steps) if steps else n
|
|
body = b"ACON" + chunk(b"anih", struct.pack("<9I", 36, n, ns, 32, 32, 32, 1, disp, flags | (2 if steps else 0)))
|
|
if title:
|
|
body += riff_list(b"INFO", [(b"INAM", title.encode() + b"\0")])
|
|
if rates:
|
|
body += chunk(b"rate", struct.pack("<%dI" % len(rates), *rates))
|
|
if steps:
|
|
body += chunk(b"seq ", struct.pack("<%dI" % len(steps), *steps))
|
|
body += riff_list(b"fram", [(b"icon", d) for d in icons])
|
|
return b"RIFF" + struct.pack("<I", len(body)) + body
|
|
|
|
|
|
def generate(ctx):
|
|
w = R.Work(ctx)
|
|
try:
|
|
for i in range(4):
|
|
px = bytes(v for p in range(32 * 32) for v in (((p * (i + 3)) % 256, 40 * i, (p // 32) * 8, 255 if (p + i) % 5 else 0)))
|
|
w.png("f%d.png" % i, 32, 32, px)
|
|
ctx.python(["pillow"], "from PIL import Image\nimport os\nos.chdir(%r)\nfor i in range(4):\n Image.open('f%%d.png' %% i).save('f%%d.ico' %% i, sizes=[(32, 32)])\n" % w.dir)
|
|
icos = [w.read("f%d.ico" % i) for i in range(4)]
|
|
# CUR: type 2, the planes / bit count fields of the entry become the hotspot
|
|
curs = []
|
|
for i, d in enumerate(icos):
|
|
b = bytearray(d)
|
|
b[2:4] = struct.pack("<H", 2)
|
|
b[6 + 4:6 + 8] = struct.pack("<HH", 3 + i, 7)
|
|
curs.append(bytes(b))
|
|
refs = [T.rgba_from(m, raw) for (m, ww, hh, raw) in T.pillow(ctx, icos)]
|
|
cases = []
|
|
steps, rates = [0, 1, 2, 1, 3], [6, 12, 6, 30, 3]
|
|
cases.append({"name": "seq + rate", "data": ani(icos, steps, rates, title="spinner"), "w": 32, "h": 32,
|
|
"steps": [{"frame": s + 1, "delay": r / 60} for s, r in zip(steps, rates)], "frames": refs, "title": "spinner"})
|
|
cases.append({"name": "plain", "data": ani(icos[:3], disp=4), "w": 32, "h": 32,
|
|
"steps": [{"frame": i + 1, "delay": 4 / 60} for i in range(3)], "frames": refs[:3]})
|
|
cases.append({"name": "cursor frames", "data": ani(curs[:2], disp=8), "w": 32, "h": 32,
|
|
"steps": [{"frame": i + 1, "delay": 8 / 60} for i in range(2)], "frames": refs[:2], "hotspot": [3, 7]})
|
|
cases.append({"name": "raw bitmaps", "data": ani(icos[:1], flags=0), "unsupported": True})
|
|
ctx.emit("ani", {"cases": cases})
|
|
finally:
|
|
w.close()
|