CyberCity/tools/topng.py
MiguelCarino 56c1b629fc A fourth world lit from inside the street, and a canal to cross on the way
Edo, on key 4: a castle town in the rain, the first world lit from within
rather than from a long way off. Paper lanterns under the eaves and oil lamps
behind the shopfronts invert the lighting model — bright at the bottom of a
wall, black at the top — and under an overcast deck the picture is carried by
lines (ridge tile, drip edge, mid-rail, gutter, the batter of a castle wall)
rather than by tone.

The canal is a painted plane, not carved geometry: a negative height renders
byte-identically to zero, so the water sits on the y=0 floor pass with a 0.45 m
stone coping beside it — an occlusion budget rather than a wall, since a real
1.2 m embankment would hide the water and the far bank behind its own kerb.
The reflect pass gives back a quarter of the floor rows where a puddle gives
back three cells.

Same engine, same seed: #42 is the same corner of the same street lattice in
all four.

New: src/surf_japan.js and jp_{town,castle,banner,flora}.js; a canal-flicker
harness in tools/. Readme carries the Edo brightness ladder and the two new
plates.
2026-09-02 01:30:18 -06:00

62 lines
2.7 KiB
Python

#!/usr/bin/env python3
"""Render a headless CyberCity frame to a PNG, with the same bloom the canvas applies."""
import sys, json
from PIL import Image, ImageDraw, ImageFont, ImageFilter
CW, CH = 9, 16
# Liberation Mono Bold at 15 px in a 9x16 cell is the reference print, and it is what
# render_canvas.js's atlas bakes and what surf_west.js's INK ramp was measured against — so it is
# tried FIRST and every fallback below is a degradation that changes the ink census. The list
# exists because this file is the standing verification procedure ("every change gets looked at
# this way before it ships") and it was unrunnable on macOS, where that path does not exist: a
# gate nobody can run is not a gate. Order is exact match, then the same metrics under another
# name, then anything monospace, then PIL's bitmap default.
_FONT_PATHS = [
"/usr/share/fonts/liberation-mono-fonts/LiberationMono-Bold.ttf",
"/usr/share/fonts/truetype/liberation/LiberationMono-Bold.ttf",
"/Library/Fonts/LiberationMono-Bold.ttf",
"/System/Library/Fonts/SFNSMono.ttf",
"/System/Library/Fonts/Menlo.ttc",
"/System/Library/Fonts/Supplemental/Courier New Bold.ttf",
]
FONT = None
for _p in _FONT_PATHS:
try:
FONT = ImageFont.truetype(_p, 15)
break
except OSError:
continue
if FONT is None:
sys.stderr.write("topng: no mono TTF found, falling back to the bitmap default; "
"glyph ink will not match the print\n")
FONT = ImageFont.load_default()
def main(src, dst):
lines = open(src).read().split('\n')
assert lines[0].startswith('CCFRAME'), 'not a CCFRAME dump'
cols, rows = map(int, lines[1].split())
pal = [tuple(map(int, c.split(','))) for c in lines[2].split(' ', 1)[1].split()]
glyphs = json.loads(lines[3].split(' ', 1)[1])
im = Image.new('RGB', (cols * CW, rows * CH), (2, 3, 6))
d = ImageDraw.Draw(im)
lit = 0
for y in range(rows):
parts = lines[4 + y].split(' ')
for x in range(cols):
fld = parts[x].split(',') # CCFRAME 2 appends a kind byte; ignore it here
ch, ci, lu = int(fld[0]), int(fld[1]), int(fld[2])
if ch == 0 or lu == 0: continue
r, g, b = pal[ci % len(pal)]
k = lu / 255.0
d.text((x * CW, y * CH), glyphs[ch], font=FONT,
fill=(int(r * k), int(g * k), int(b * k)))
lit += 1
g1 = im.filter(ImageFilter.GaussianBlur(1.5))
g2 = im.filter(ImageFilter.GaussianBlur(5.0))
glow = Image.blend(g1, g2, 0.5)
im = Image.blend(im, Image.eval(glow, lambda v: min(255, int(v * 2.0))), 0.5)
im.save(dst)
print(f"{dst} {cols}x{rows} cells {lit/(cols*rows)*100:.1f}% lit")
if __name__ == '__main__':
main(sys.argv[1], sys.argv[2])