# The dithers games use now against the best space-time lattices, by simulation (practical.py), through
# Starlock's pass (paint on) and with paint off (as is, and through a slight eye blur): the error of one
# frame, what each eye model keeps, the shimmer below 20 Hz at 60 fps, and how visible the grain and the
# shimmer are (vis.py's generous model, at 60 and 30 fps). Prints out/compare.txt. About 15 minutes.
import sys
import numpy as np
import filters as F, st, vis, practical as P

EYES = [st.box(2), st.box(4), st.decay(40), st.decay(80)]
BANDS = [st.shimmer_band(20)]
CHECKS = [('bright', 60), ('bright', 30)]
STBN = np.load('stbn64x32.npy').astype(float) / 65536 + .5 / 65536
BLUE = STBN[:1]  # (one slice of the spatiotemporal tile is itself a blue-noise tile)
LEVELS = int(sys.argv[1]) if len(sys.argv) > 1 else 32

def table(name, taps, deg, lattices):
    print(f'== {name}')
    print(f'  {"dither":32s} {"1 frame":>8s} ' + ' '.join(f'{G.name:>11s}' for G in EYES) + f' {"<20Hz":>7s}   grain/shimmer L* 60 fps   shimmer 30 fps')
    T = 64 + 48; n = 96 + 2 * 12
    cands = [('ign still', None), ('ign + 5.588238 t', None), ('ign + golden t', None), ('ign + half every other frame', None),
             ('white', None), ('blue still', BLUE), ('blue + golden t', BLUE), ('stbn', STBN)]
    cands += [(f'lattice {a},{b},{c}/{N}', (a, b, c, N)) for a, b, c, N in lattices]
    for nm, tile in cands:
        fr = P.make(nm, n, T, tile=tile)
        one, kept, sh, js = P.score(fr, taps, EYES, BANDS, levels=LEVELS, deg=deg, checks=CHECKS)
        print(f'  {nm:32s} {one:8.4f} ' + ' '.join(f'{k:11.4f}' for k in kept) + f' {sh[0]:7.4f}   {js[0][0]:6.1f} {js[0][1]:6.1f}            {js[1][1]:6.1f}')

table('Starlock full pass (taps a CSS pixel apart; the dither per tap)', F.kuwahara(5), vis.CSS_DEG,
      [(54, 11, 0, 56), (54, 11, 28, 56), (19, 4, 20, 40), (15, 7, 10, 20)])
table('paint off, per device pixel, as is', [(0, 0, 1.0)], vis.DEV_DEG, [(5, 2, 0, 11), (1, 1, 1, 2), (3, 2, 4, 8), (11, 6, 3, 24)])
table('paint off, through a slight eye blur', F.gaussian(1.0), vis.DEV_DEG, [(5, 2, 0, 11), (1, 1, 1, 2), (3, 2, 4, 8), (11, 6, 3, 24)])
