Free games for graphing calculators: NumWorks, TI-Nspire CX, Casio fx-CG50 and soon TI-84 Plus CE. Balatro, Celeste, Hollow Knight, Minecraft and more.
C calculatorcalculator-gamescasiochesscrossy-roaddrive-madfx-cg50gamesgeometry-dashgraphing-calculatorndlessnumworksnumworks-appsnumworks-calculatornumworks-gamenumworks-gamestetristi-84-plus-ceti-nspire
#!/usr/bin/env python3
"""Packs a game's screenshots for the launcher's carousel.
Each screenshot is scaled to 192x144 (60 %), reduced to at most 48 colours (by default)
and compressed with raw DEFLATE; the launcher inflates it when the card is on
screen. On the calculator everything goes in the game's block of flash, so
uninstalling the game frees its screenshots too.
Usage: shots.py --id numdash --index 1 out.c shot1.png shot2.png ...
"""
import argparse
import os
import shutil
import subprocess
import tempfile
import zlib
from PIL import Image
W, H = 192, 144
def rgb565(r, g, b):
return (min(31, (r + 4) >> 3) << 11) | (min(63, (g + 2) >> 2) << 5) | min(31, (b + 4) >> 3)
def deflate(data):
try:
import zopfli.zlib
return zopfli.zlib.compress(bytes(data), numiterations=60)[2:-4]
except ImportError:
c = zlib.compressobj(9, zlib.DEFLATED, -15, 9)
return c.compress(bytes(data)) + c.flush()
def quantize(im, colors):
"""pngquant keeps small palettes faithful; PIL's median cut is the fallback."""
if shutil.which("pngquant"):
with tempfile.TemporaryDirectory() as d:
src, dst = os.path.join(d, "in.png"), os.path.join(d, "out.png")
im.save(src)
subprocess.run(["pngquant", "--nofs", "--speed", "1", "--force", "--output", dst, str(colors), src],
check=True)
q = Image.open(dst)
q.load()
if q.mode == "P":
return q
return im.quantize(colors=colors, method=Image.Quantize.MEDIANCUT, dither=Image.Dither.NONE)
def pack(path, colors):
im = Image.open(path).convert("RGB")
if im.size != (W, H):
im = im.resize((W, H), Image.LANCZOS)
q = quantize(im, colors)
idx = q.tobytes()
used = max(idx) + 1
pal = q.getpalette()[:3 * used]
pal565 = [rgb565(*pal[i:i + 3]) for i in range(0, len(pal), 3)]
return pal565, deflate(idx)
def c_array(ctype, name, values, per_line, attr):
out = [f"static const {ctype} {name}[{len(values)}] {attr} = {{"]
for i in range(0, len(values), per_line):
out.append(" " + ",".join(str(v) for v in values[i:i + per_line]) + ",")
out.append("};")
return out
def main():
ap = argparse.ArgumentParser()
ap.add_argument("--id", required=True)
ap.add_argument("--index", type=int, required=True)
ap.add_argument("--colors", type=int, default=48)
ap.add_argument("out")
ap.add_argument("shots", nargs="+")
a = ap.parse_args()
section = f".rodata.np.{a.index}.{a.id}.shots"
lines = [f"/* Generated by tools/shots.py for {a.id}. */", '#include "np.h"', "#ifdef __ELF__",
f'#define S __attribute__((section("{section}"), aligned(4)))', "#else", "#define S", "#endif", ""]
total = 0
entries = []
for k, path in enumerate(a.shots):
pal, z = pack(path, a.colors)
total += len(z) + 2 * len(pal) + 16
lines += c_array("color_t", f"pal{k}", pal, 16, "S")
lines += c_array("uint8_t", f"z{k}", list(z), 32, "S")
entries.append(f" {{{W}, {H}, {len(pal)}, {len(z)}, pal{k}, z{k}}},")
lines.append(f"const np_shot_t np_{a.id}_shots[{len(entries)}] S = {{")
lines += entries
lines.append("};")
open(a.out, "w").write("\n".join(lines) + "\n")
print(f"shots {a.id}: {len(a.shots)} screenshots, {total} bytes")
if __name__ == "__main__":
main()