code wiki / _hdl_build / nx_grade.nx

nx_grade.nx source

↩ module page · 97 lines · 4949 B

1// nx_grade.nx -- run the ecosystem's photoreal grader (ns_assess) on ANY jpeg, print LEVEL + 5 axes. 2// The RED-TEAM harness: feed it real photos, clay renders, and adversarial inputs (noise/flat/checker) to 3// test whether the grader is HONEST or GAMEABLE. Same decode path the corpus grader uses. license_tier: ORIGINAL 4// nx_grade <image.jpg> <label> 5import "nx_syscalls.nx" 6import "nx_itoa_lib.nx" // shared MSB-first emitter (zero-alloc) 7import "nx_jpeg_ascii.nx" 8import "nx_natstat.nx" 9import "nx_png_decoder.nx" 10const K_65536: i64 = 65536 11 12func g_hw(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n+1 } sys_write(1, s, n); return 0 } 13// MIGRATED to the shared emitter (debt 1785563586). The old body mmapped a scratch buffer 14// per call and never freed it. At PAGE granularity that is 4096B leaked PER CALL -- the 15// defect that took 28.5GB of a 36GB host in nx_ts_lumadiff (2MB input, ~3.66M calls). 16// nxi_* is MSB-first, allocates NOTHING, and emits identical bytes including the sign. 17func g_pn(v: i64) -> i64 { nxi_out(v); return 0 } 18 19// shared assess+print tail (JPEG and PNG branches both land here) 20func g_run(fb: *i64, iw: i64, ih: i64, label: *u8) -> i64 { 21 let sc: i64 = iw*ih*8 + 64 22 let buf1: *i64 = sys_mmap(sc) as *i64 23 let buf2: *i64 = sys_mmap(sc) as *i64 24 let out: *i64 = sys_mmap(64) as *i64 25 let lvl: i64 = ns_assess(fb, iw, ih, buf1, buf2, out) 26 let hard: i64 = ns_assess2(fb, iw, ih, buf1, buf2, out) 27 let coh: i64 = out[5] 28 g_hw("{\x22label\x22:\x22" as *u8); g_hw(label) 29 g_hw("\x22,\x22w\x22:" as *u8); g_pn(iw); g_hw(",\x22h\x22:" as *u8); g_pn(ih) 30 g_hw(",\x22LEVEL_old\x22:" as *u8); g_pn(lvl) 31 g_hw(",\x22LEVEL\x22:" as *u8); g_pn(hard) 32 g_hw(",\x22verdict\x22:\x22" as *u8); g_hw(ns_verdict(hard)) 33 g_hw("\x22,\x22axes\x22:{\x22scale\x22:" as *u8); g_pn(out[0]) 34 g_hw(",\x22mscn\x22:" as *u8); g_pn(out[1]) 35 g_hw(",\x22kurt\x22:" as *u8); g_pn(out[2]) 36 g_hw(",\x22colour\x22:" as *u8); g_pn(out[3]) 37 g_hw(",\x22ai_fp\x22:" as *u8); g_pn(out[4]) 38 g_hw(",\x22coherence\x22:" as *u8); g_pn(coh) 39 g_hw("}}\n" as *u8) 40 return 0 41} 42 43func main(argc: i64, argv: *i64) -> i64 { 44 let path: *u8 = argv[1] as *u8 45 let label: *u8 = argv[2] as *u8 46 let lenp: *i64 = sys_mmap(16) as *i64 47 let raw: *u8 = sys_read_file(path, lenp) 48 if (raw as i64) == 0 { g_hw("{\x22error\x22:\x22read\x22}\n" as *u8); return 2 } 49 let n: i64 = lenp[0] 50 // ---- PNG branch (2026-08-04): same grader through the sovereign nx_png_decode path -- the 51 // compare lane grades PNGs; "ANY jpeg" was the-only-decoder-wired, not a design choice ---- 52 if n > 8 { if raw[0] == (137 as u8) { if raw[1] == (80 as u8) { 53 let res: *NxPngResult = nx_png_decode(raw, n) 54 if (res as i64) == 0 { g_hw("{\x22error\x22:\x22png-decode-null\x22}\n" as *u8); return 4 } 55 if res.error_code != 0 { g_hw("{\x22error\x22:\x22png-decode\x22,\x22rc\x22:" as *u8); g_pn(res.error_code); g_hw("}\n" as *u8); return 4 } 56 let phdr: *NxPngHeader = res.header 57 let pw: i64 = phdr.width 58 let ph: i64 = phdr.height 59 let ch: i64 = res.n_channels 60 let px: *u8 = res.pixels 61 if pw <= 0 { g_hw("{\x22error\x22:\x22png-dims\x22}\n" as *u8); return 4 } 62 let pfb: *i64 = sys_mmap(pw*ph*8) as *i64 63 var q: i64 = 0 64 while q < pw*ph { 65 var pr: i64 = 0 66 var pg: i64 = 0 67 var pb: i64 = 0 68 if ch >= 3 { pr = px[q*ch] as i64; pg = px[q*ch+1] as i64; pb = px[q*ch+2] as i64 } 69 if ch <= 2 { pr = px[q*ch] as i64; pg = pr; pb = pr } 70 pfb[q] = pr + pg*256 + pb*K_65536 71 q = q + 1 72 } 73 g_run(pfb, pw, ph, label) 74 return 0 75 } } } 76 // find SOI (0xFF 0xD8) 77 var soi: i64 = 0; var f: i64 = 0 78 while soi < n - 1 { if raw[soi] == (255 as u8) { if raw[soi+1] == (216 as u8) { f = 1; soi = n } } if f == 0 { soi = soi + 1 } } 79 if f == 0 { g_hw("{\x22error\x22:\x22no-soi\x22}\n" as *u8); return 3 } 80 var start: i64 = soi; if soi >= n - 1 { start = soi - (n - 1) } // undo the =n sentinel 81 // recompute start cleanly 82 var s2: i64 = 0 83 while s2 < n - 1 { if raw[s2] == (255 as u8) { if raw[s2+1] == (216 as u8) { start = s2; s2 = n } } s2 = s2 + 1 } 84 85 let rgbp: *i64 = sys_mmap(8) as *i64 86 let wp: *i64 = sys_mmap(8) as *i64 87 let hp: *i64 = sys_mmap(8) as *i64 88 let drc: i64 = nx_jpeg_decode_rgb((raw as i64 + start) as *u8, n - start, rgbp, wp, hp) 89 if drc != NX_JPEG_ASCII_OK { g_hw("{\x22error\x22:\x22decode\x22,\x22rc\x22:" as *u8); g_pn(drc); g_hw("}\n" as *u8); return 4 } 90 let rgb: *u8 = rgbp[0] as *u8 91 let iw: i64 = wp[0]; let ih: i64 = hp[0] 92 let fb: *i64 = sys_mmap(iw*ih*8) as *i64 93 var p: i64 = 0 94 while p < iw*ih { fb[p] = (rgb[p*3] & 255) + (rgb[p*3+1] & 255)*256 + (rgb[p*3+2] & 255)*K_65536; p = p + 1 } 95 g_run(fb, iw, ih, label) 96 return 0 97}