code wiki / _hdl_build / nx_ssim_gate.nx
nx_ssim_gate.nx source
↩ module page · 56 lines · 2978 B
1// nx_ssim_gate.nx -- proves the sovereign SSIM metric (nx_quality_metric qm_ssim_milli), incl. the key property
2// that distinguishes it from PSNR: structure-sensitivity. Native, fast.
3import "nx_syscalls.nx"
4import "nx_quality_metric.nx"
5import "nx_gate_verdict.nx"
6
7func g_puts(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
8func g_pn(v: i64) -> i64 {
9 let b: *u8 = sys_mmap(28); var x: i64 = v
10 if x < 0 { b[0]=45; sys_write(1,b,1); x = 0 - x }
11 if x == 0 { b[0]=48; sys_write(1,b,1); return 0 }
12 var d: i64=0; var y: i64=x
13 while y>0 { d=d+1; y=y/10 }
14 var i: i64=d-1; y=x
15 while i>=0 { b[i]=(48+(y%10)) as u8; y=y/10; i=i-1 }
16 sys_write(1,b,d); return 0
17}
18func g_check(name: *u8, cond: i64) -> i64 {
19 if cond==1 { g_puts(" PASS " as *u8) } else { g_puts(" FAIL " as *u8) }
20 g_puts(name); g_puts("\n" as *u8); return cond
21}
22
23func main() -> i64 {
24 g_puts("nx_ssim gate (structural similarity, MEASURED)\n" as *u8)
25 var pass: i64 = 0; var total: i64 = 0
26 let W: i64 = 16; let H: i64 = 16; let n: i64 = W*H
27 let a: *u8 = sys_mmap(n); let off: *u8 = sys_mmap(n); let dmg: *u8 = sys_mmap(n)
28 var i: i64 = 0
29 while i < n {
30 a[i] = (40 + (i % 60)) as u8
31 off[i] = (a[i] + 10) as u8 // uniform brightness shift (structure intact)
32 if (i & 1) == 0 { dmg[i] = a[i] } else { dmg[i] = (220 - (a[i] as i64)) as u8 } // alternating inversion (structure destroyed)
33 i = i + 1
34 }
35
36 let s_id: i64 = qm_ssim_milli(a, a, W, H)
37 let s_off: i64 = qm_ssim_milli(a, off, W, H)
38 let s_dmg: i64 = qm_ssim_milli(a, dmg, W, H)
39 g_puts(" [measure] SSIMx1000: identical=" as *u8); g_pn(s_id); g_puts(" +10 offset=" as *u8); g_pn(s_off); g_puts(" structural-damage=" as *u8); g_pn(s_dmg); g_puts("\n" as *u8)
40
41 pass = pass + g_check("identical -> SSIM 1000" as *u8, s_id == 1000); total=total+1
42 pass = pass + g_check("uniform +10 offset -> SSIM still high (structure intact; PSNR would say only 28dB)" as *u8, s_off > 900); total=total+1
43 pass = pass + g_check("structural damage -> SSIM low" as *u8, s_dmg < 600); total=total+1
44 pass = pass + g_check("structure-sensitive: offset SSIM >> damage SSIM" as *u8, s_off > s_dmg + 300); total=total+1
45
46 g_puts("---- ssim gate: passed " as *u8); g_pn(pass); g_puts(" / " as *u8); g_pn(total); g_puts(" ----\n" as *u8)
47 // MIGRATED onto nx_gate_verdict by nx_gate_dry_apply (D001, minimal form): every check
48 // row above is untouched, so the PASS/FAIL vector cannot change; only the hand-rolled
49 // verdict emission is replaced by the ONE shared base class. Proven by nx_gate_migrate verify.
50 let ctr__dry: *i64 = gv_ctr()
51 ctr__dry[0] = pass
52 ctr__dry[1] = total
53 let rc__dry: i64 = gv_verdict("SSIM-GATE" as *u8, ctr__dry, "teeth unchanged; verdict emission migrated onto the shared base class" as *u8)
54 sys_exit(rc__dry)
55 return rc__dry
56}