code wiki / _hdl_build / nx_polish_census.nx

nx_polish_census.nx source

↩ module page · 192 lines · 10995 B

1// nx_polish_census.nx -- MEASURE the visual POLISH gap vs Chrome/Edge as a NUMBER (operator: the evidence 2// "clearly shows our polish gap" -- so quantify it, no vibes). Legibility (OCR word-recall) says we're 3// readable (984); polish is a DIFFERENT axis: smooth anti-aliased edges vs blocky 1-bit ones. This census 4// decodes the SAME bench renders (ours / chrome / edge, via the sovereign PNG decoder) and computes an 5// ANTI-ALIASING QUALITY score: of the pixels ON A TEXT EDGE (a large luminance jump to a neighbor), what 6// fraction are INTERMEDIATE GRAY (30..225) = softened by AA, vs pure black/white = aliased. A 1-bit 7// bitmap render scores ~0; Chrome's rasterizer scores high. The GAP is the number to close. 8// permille AA = intermediate-gray-edge-px * 1000 / total-edge-px. NEG/liar-kill: a synthetic hard-edged 9// checkerboard MUST score ~0 (the metric can't be fooled into calling blocky "smooth"). 10// expect_exit: 0 license_tier: ORIGINAL 11import "nx_img_bytes_to_rgb.nx" 12import "nx_itoa_lib.nx" // shared MSB-first emitter (zero-alloc) 13const K_MAGIC_2126: i64 = 2126 14const K_MAGIC_7152: i64 = 7152 15const K_MAGIC_10000: i64 = 10000 16const K_MAGIC_16384: i64 = 16384 17 18func pc_puts(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 19// MIGRATED to the shared emitter (debt 1785563586). The old body mmapped a scratch buffer 20// per call and never freed it. At PAGE granularity that is 4096B leaked PER CALL -- the 21// defect that took 28.5GB of a 36GB host in nx_ts_lumadiff (2MB input, ~3.66M calls). 22// nxi_* is MSB-first, allocates NOTHING, and emits identical bytes including the sign. 23func pc_num(v: i64) -> i64 { nxi_out(v); return 0 } 24func pc_app(buf: *u8, pos: i64, s: *u8) -> i64 { var p: i64=pos; var i: i64=0; while s[i]!=(0 as u8) { buf[p]=s[i]; p=p+1; i=i+1 } return p } 25func pc_appd(buf: *u8, pos: i64, v: i64) -> i64 { 26 var p: i64=pos 27 var m: i64=v 28 if m<0 { buf[p]=45 as u8; p=p+1; m=0-m } 29 let t: *u8=sys_mmap(28) 30 var k: i64=0 31 if m==0 { t[0]=48 as u8; k=1 } 32 while m>0 { t[k]=(48+(m%10)) as u8; m=m/10; k=k+1 } 33 var i: i64=0 34 while i<k { buf[p]=t[k-1-i]; p=p+1; i=i+1 } 35 return p 36} 37func pc_write_file(path: *u8, data: *u8, n: i64) -> i64 { 38 let fd: i64 = sys_openat_wr(path, 0x1a4) 39 if fd<=0 { return 0 - 1 } 40 sys_write(fd, data, n) 41 sys_close(fd) 42 return n 43} 44func pc_lum(rgb: *u8, o: i64) -> i64 { return (K_MAGIC_2126*(rgb[o] as i64) + K_MAGIC_7152*(rgb[o+1] as i64) + 722*(rgb[o+2] as i64))/K_MAGIC_10000 } 45// parse the integer right after `key` in file `path` (e.g. "ours=984"); -1 if absent. 46func pc_parse_after(path: *u8, key: *u8) -> i64 { 47 let lp: *i64 = sys_mmap(16) as *i64 48 lp[0] = 0 - 1 49 let b: *u8 = sys_read_file(path, lp) 50 let n: i64 = lp[0] 51 if n <= 0 { return 0 - 1 } 52 var kl: i64 = 0 53 while key[kl]!=(0 as u8) { kl=kl+1 } 54 var i: i64 = 0 55 while i + kl < n { 56 var ok: i64 = 1 57 var j: i64 = 0 58 while j < kl { if b[i+j]!=key[j] { ok=0; j=kl } else { j=j+1 } } 59 if ok==1 { 60 var v: i64=0 61 var p: i64=i+kl 62 var any: i64=0 63 var go: i64=1 64 while go==1 { if p>=n { go=0 } else { let c: i64=b[p] as i64; if c>=48 { if c<=57 { v=v*10+(c-48); any=1; p=p+1 } else { go=0 } } else { go=0 } } } 65 if any==1 { return v } 66 return 0 - 1 67 } 68 i=i+1 69 } 70 return 0 - 1 71} 72 73// AA quality of an RGB image in permille: of the edge pixels (a horizontal luminance jump >100 to the 74// right neighbor), what fraction are intermediate-gray (30..225). out[0]=aa_permille out[1]=edge_px 75func pc_aa_score(rgb: *u8, w: i64, h: i64, out: *i64) -> i64 { 76 var edge: i64 = 0 77 var soft: i64 = 0 78 var y: i64 = 0 79 while y < h { 80 var x: i64 = 1 81 while x < w-1 { 82 let o: i64 = (y*w+x)*3 83 let l: i64 = pc_lum(rgb, o) 84 let lr: i64 = pc_lum(rgb, o+3) 85 var d: i64 = l - lr 86 if d < 0 { d = 0 - d } 87 if d > 100 { 88 edge = edge + 1 89 // this pixel OR its right neighbor is intermediate = a soft transition (AA), not a hard cliff 90 if l > 30 { if l < 225 { soft = soft + 1 } } 91 else { if lr > 30 { if lr < 225 { soft = soft + 1 } } } 92 } 93 x = x + 1 94 } 95 y = y + 1 96 } 97 out[1] = edge 98 if edge == 0 { out[0] = 0; return 0 } 99 out[0] = (soft * 1000) / edge 100 return 0 101} 102 103// load a PNG path -> RGB via the sovereign decoder. wh[0]=w wh[1]=h. returns rgb ptr or 0. 104func pc_load(path: *u8, wh: *i64) -> *u8 { 105 let lp: *i64 = sys_mmap(16) as *i64 106 lp[0] = 0 - 1 107 let raw: *u8 = sys_read_file(path, lp) 108 if lp[0] <= 0 { return 0 as *u8 } 109 return nx_img_bytes_to_rgb(raw, lp[0], wh) 110} 111 112func main() -> i64 { 113 pc_puts("POLISH census: the anti-aliasing gap vs Chrome/Edge, MEASURED in permille (readable != smooth)\n" as *u8) 114 let owh: *i64 = sys_mmap(16) as *i64 115 let cwh: *i64 = sys_mmap(16) as *i64 116 let ewh: *i64 = sys_mmap(16) as *i64 117 let orgb: *u8 = pc_load("knowledge/status/ocr_bench_ours.png\x00" as *u8, owh) 118 let crgb: *u8 = pc_load("knowledge/status/ocr_bench_chrome.png\x00" as *u8, cwh) 119 let ergb: *u8 = pc_load("knowledge/status/ocr_bench_edge.png\x00" as *u8, ewh) 120 121 let so: *i64 = sys_mmap(16) as *i64 122 let sc: *i64 = sys_mmap(16) as *i64 123 let se: *i64 = sys_mmap(16) as *i64 124 so[0]=0-1; sc[0]=0-1; se[0]=0-1 125 if orgb != (0 as *u8) { pc_aa_score(orgb, owh[0], owh[1], so) } 126 if crgb != (0 as *u8) { pc_aa_score(crgb, cwh[0], cwh[1], sc) } 127 if ergb != (0 as *u8) { pc_aa_score(ergb, ewh[0], ewh[1], se) } 128 129 pc_puts(" nishi AA=" as *u8); pc_num(so[0]); pc_puts(" permille (edge px " as *u8); pc_num(so[1]); pc_puts(", " as *u8); pc_num(owh[0]); pc_puts("x" as *u8); pc_num(owh[1]); pc_puts(")\n" as *u8) 130 pc_puts(" chrome AA=" as *u8); pc_num(sc[0]); pc_puts(" permille (edge px " as *u8); pc_num(sc[1]); pc_puts(")\n" as *u8) 131 pc_puts(" edge AA=" as *u8); pc_num(se[0]); pc_puts(" permille (edge px " as *u8); pc_num(se[1]); pc_puts(")\n" as *u8) 132 var gap: i64 = sc[0] - so[0] 133 pc_puts(" >>> POLISH GAP vs chrome = " as *u8); pc_num(gap); pc_puts(" permille (the number to close)\n" as *u8) 134 135 // NEG/liar-kill: a hard checkerboard scores ~0 AA (the metric can't call blocky smooth) 136 let CW: i64 = 64 137 let cb: *u8 = sys_mmap(CW*CW*3 + 16) 138 var yy: i64 = 0 139 while yy < CW { 140 var xx: i64 = 0 141 while xx < CW { 142 let o: i64 = (yy*CW+xx)*3 143 var v: i64 = 0 144 if ((xx/4 + yy/4) % 2) == 0 { v = 255 } 145 cb[o]=v as u8; cb[o+1]=v as u8; cb[o+2]=v as u8 146 xx = xx + 1 147 } 148 yy = yy + 1 149 } 150 let sn: *i64 = sys_mmap(16) as *i64 151 pc_aa_score(cb, CW, CW, sn) 152 153 // legibility parsed from the OCR judge -- the polish must NOT have collapsed readability 154 let ocr_ours: i64 = pc_parse_after("knowledge/status/ocr_legibility.log\x00" as *u8, "ours=\x00" as *u8) 155 156 var pass: i64=0 157 var ttl: i64=0 158 ttl=ttl+1; pc_puts(" C1 all three renders decoded + measured: " as *u8) 159 if so[0]>=0 { if sc[0]>=0 { if se[0]>=0 { pass=pass+1; pc_puts("PASS\n" as *u8) } else { pc_puts("FAIL\n" as *u8) } } else { pc_puts("FAIL\n" as *u8) } } else { pc_puts("FAIL\n" as *u8) } 160 ttl=ttl+1; pc_puts(" C2 chrome/edge actually anti-alias (AA>=300, proving the metric detects real AA): " as *u8) 161 if sc[0]>=300 { if se[0]>=300 { pass=pass+1; pc_puts("PASS\n" as *u8) } else { pc_puts("FAIL\n" as *u8) } } else { pc_puts("FAIL\n" as *u8) } 162 ttl=ttl+1; pc_puts(" C3 NEG: a hard checkerboard scores ~0 AA (metric can't be fooled, got " as *u8); pc_num(sn[0]); pc_puts("): " as *u8) 163 if sn[0] < 50 { pass=pass+1; pc_puts("PASS\n" as *u8) } else { pc_puts("FAIL\n" as *u8) } 164 ttl=ttl+1; pc_puts(" C5 POLISH GAP CLOSED: our AA within 100 of chrome (gap=" as *u8); pc_num(gap); pc_puts("): " as *u8) 165 if gap <= 100 { pass=pass+1; pc_puts("PASS\n" as *u8) } else { pc_puts("FAIL\n" as *u8) } 166 ttl=ttl+1; pc_puts(" C6 LEGIBILITY HELD: OCR word-recall still >=940 despite the AA (got " as *u8); pc_num(ocr_ours); pc_puts("): " as *u8) 167 if ocr_ours >= 940 { pass=pass+1; pc_puts("PASS\n" as *u8) } else { pc_puts("FAIL\n" as *u8) } 168 169 // artifacts 170 let hb: *u8 = sys_mmap(K_MAGIC_16384) 171 var hp: i64 = 0 172 hp = pc_app(hb, hp, "<!doctype html><html><head><title>Polish (anti-aliasing) census</title></head><body style=\x27background:#0b1020;color:#dbe4ff;font-family:monospace;padding:24px\x27><h2>The polish gap, measured -- anti-aliasing quality vs Chrome/Edge</h2><p style=\x27color:#8fa3cc\x27>Legibility (OCR) says we are readable; polish is a different axis. AA permille = fraction of text-edge pixels that are intermediate-gray (softened) vs a hard 1-bit cliff.</p><table style=\x27border-collapse:collapse\x27><tr><th style=\x27text-align:left;padding:4px 12px\x27>render</th><th style=\x27text-align:left;padding:4px 12px\x27>AA permille</th></tr>\x00" as *u8) 173 hp = pc_app(hb, hp, "<tr><td style=\x27padding:3px 12px\x27>nishi</td><td style=\x27padding:3px 12px;color:#f59e0b\x27>\x00" as *u8); hp = pc_appd(hb, hp, so[0]); hp = pc_app(hb, hp, "</td></tr>\x00" as *u8) 174 hp = pc_app(hb, hp, "<tr><td style=\x27padding:3px 12px\x27>chrome</td><td style=\x27padding:3px 12px;color:#22c55e\x27>\x00" as *u8); hp = pc_appd(hb, hp, sc[0]); hp = pc_app(hb, hp, "</td></tr>\x00" as *u8) 175 hp = pc_app(hb, hp, "<tr><td style=\x27padding:3px 12px\x27>edge</td><td style=\x27padding:3px 12px;color:#22c55e\x27>\x00" as *u8); hp = pc_appd(hb, hp, se[0]); hp = pc_app(hb, hp, "</td></tr>\x00" as *u8) 176 hp = pc_app(hb, hp, "</table><p>POLISH GAP vs chrome = <b style=\x27color:#f43f5e\x27>\x00" as *u8); hp = pc_appd(hb, hp, gap); hp = pc_app(hb, hp, "</b> permille</p></body></html>\x00" as *u8) 177 let hw: i64 = pc_write_file("knowledge/status/polish_census.html\x00" as *u8, hb, hp) 178 let lb: *u8 = sys_mmap(256) 179 var lo: i64 = 0 180 lo = pc_app(lb, lo, "POLISH nishi=\x00" as *u8); lo = pc_appd(lb, lo, so[0]) 181 lo = pc_app(lb, lo, " chrome=\x00" as *u8); lo = pc_appd(lb, lo, sc[0]) 182 lo = pc_app(lb, lo, " edge=\x00" as *u8); lo = pc_appd(lb, lo, se[0]) 183 lo = pc_app(lb, lo, " gap=\x00" as *u8); lo = pc_appd(lb, lo, gap) 184 lo = pc_app(lb, lo, "\x0A\x00" as *u8) 185 let lw: i64 = pc_write_file("knowledge/status/polish_census.log\x00" as *u8, lb, lo) 186 ttl=ttl+1; pc_puts(" C4 census artifacts written: " as *u8) 187 if hw>0 { if lw>0 { pass=pass+1; pc_puts("PASS\n" as *u8) } else { pc_puts("FAIL\n" as *u8) } } else { pc_puts("FAIL\n" as *u8) } 188 189 pc_puts("POLISH-CENSUS-GATE passed " as *u8); pc_num(pass); pc_puts("/" as *u8); pc_num(ttl) 190 if pass==ttl { pc_puts(" verdict=GREEN (the gap is measured; open knowledge/status/polish_census.html)\n" as *u8); sys_exit(0); return 0 } 191 pc_puts(" verdict=RED\n" as *u8); sys_exit(1); return 1 192}