code wiki / (root) / nx_blob_gate.nx

nx_blob_gate.nx source

↩ module page · 125 lines · 5275 B

1// nx_blob_gate.nx -- REFEREE for R2-refine (nx_blob largest connected blob). 2// 3// 64x64 RGB, grid=32 (cell=2px), skin=(200,120,90): 4// COMPACT : a 32x32 skin block at (16,16) -> ONE blob of 16x16=256 cells, 5// bbox grid (8,8..23,23). (localizes to the block, not the frame) 6// FRAMEFILL: whole frame skin -> ONE blob of all 1024 cells. 7// SCATTER : ~221 permille random skin -> few/no cells reach 50% -> largest 8// blob TINY (<< compact). NEG-CONTROL: high skin fraction but no 9// real region -> CCL rejects it (the fix vs global-bbox). 10// TWOBLOCK : two separated 16x16 skin blocks -> largest blob = ONE block 11// (64 cells, bbox width 8), NOT a merged box spanning both. 12// 13// Every measured value PRINTED. stdout + knowledge/status/blob_gate.log. 14// Exit 0 GREEN / 1 RED. Sovereign: syscalls + nx_image + nx_presence + nx_blob. 15// license_tier: ORIGINAL 16import "syscalls.nx" 17import "nx_image.nx" 18import "nx_presence.nx" 19import "nx_blob.nx" 20 21const W: i64 = 64 22const H: i64 = 64 23 24func gp(logfd: i64, s: *u8) -> i64 { 25 var n: i64 = 0 26 while s[n] != (0 as u8) { n = n + 1 } 27 sys_write(1, s, n) 28 if logfd > 0 { sys_write(logfd, s, n) } 29 return 0 30} 31func gn(logfd: i64, v: i64) -> i64 { 32 let bb: *u8 = sys_mmap(28) 33 var m: i64 = v 34 if m < 0 { sys_write(1, "-\x00" as *u8, 1); if logfd > 0 { sys_write(logfd, "-\x00" as *u8, 1) } m = 0 - m } 35 let t: *u8 = sys_mmap(28) 36 var k: i64 = 0 37 if m == 0 { t[0] = 48 as u8; k = 1 } 38 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 } 39 var i: i64 = 0 40 while i < k { bb[i] = t[k - 1 - i]; i = i + 1 } 41 sys_write(1, bb, k) 42 if logfd > 0 { sys_write(logfd, bb, k) } 43 return 0 44} 45func fill_rgb(img: *Image, r: i64, g: i64, b: i64) -> i64 { 46 var y: i64 = 0 47 while y < H { var x: i64 = 0 48 while x < W { nx_image_set(img, x, y, 0, r); nx_image_set(img, x, y, 1, g); nx_image_set(img, x, y, 2, b); x = x + 1 } 49 y = y + 1 } 50 return 0 51} 52func block_rgb(img: *Image, x0: i64, y0: i64, bw: i64, bh: i64, r: i64, g: i64, b: i64) -> i64 { 53 var y: i64 = y0 54 while y < y0 + bh { var x: i64 = x0 55 while x < x0 + bw { nx_image_set(img, x, y, 0, r); nx_image_set(img, x, y, 1, g); nx_image_set(img, x, y, 2, b); x = x + 1 } 56 y = y + 1 } 57 return 0 58} 59func scatter_skin(img: *Image, count: i64, seed: i64) -> i64 { 60 var s: i64 = seed 61 var i: i64 = 0 62 while i < count { 63 s = (s * 1103515245 + 12345) & 0x7fffffff 64 let x: i64 = (s * W) >> 31 65 s = (s * 1103515245 + 12345) & 0x7fffffff 66 let y: i64 = (s * H) >> 31 67 nx_image_set(img, x, y, 0, 200); nx_image_set(img, x, y, 1, 120); nx_image_set(img, x, y, 2, 90) 68 i = i + 1 69 } 70 return 0 71} 72func report(logfd: i64, label: *u8, o: *i64) -> i64 { 73 gp(logfd, label) 74 gp(logfd, " blob_cells=\x00" as *u8); gn(logfd, o[0]) 75 gp(logfd, " gbbox=[\x00" as *u8); gn(logfd, o[1]); gp(logfd, ",\x00" as *u8); gn(logfd, o[2]) 76 gp(logfd, "..\x00" as *u8); gn(logfd, o[3]); gp(logfd, ",\x00" as *u8); gn(logfd, o[4]) 77 gp(logfd, "] bbox_cells=\x00" as *u8); gn(logfd, o[5]); gp(logfd, " occ=\x00" as *u8); gn(logfd, o[6]); gp(logfd, "\n\x00" as *u8) 78 return 0 79} 80 81func main() -> i64 { 82 let logfd: i64 = sys_openat_append("knowledge/status/blob_gate.log\x00" as *u8, 0x1a4) 83 gp(logfd, "BLOB-GATE R2-refine grid=32 frame=64x64\n\x00" as *u8) 84 85 let imc: *Image = nx_image_alloc(W, H, 3) 86 let imf: *Image = nx_image_alloc(W, H, 3) 87 let ims: *Image = nx_image_alloc(W, H, 3) 88 let imt: *Image = nx_image_alloc(W, H, 3) 89 fill_rgb(imc, 128, 128, 128); block_rgb(imc, 16, 16, 32, 32, 200, 120, 90) 90 fill_rgb(imf, 200, 120, 90) 91 fill_rgb(ims, 128, 128, 128); scatter_skin(ims, 905, 1234567) 92 fill_rgb(imt, 128, 128, 128); block_rgb(imt, 8, 8, 16, 16, 200, 120, 90); block_rgb(imt, 40, 8, 16, 16, 200, 120, 90) 93 94 let oc: *i64 = sys_mmap(128) as *i64 95 let of: *i64 = sys_mmap(128) as *i64 96 let os: *i64 = sys_mmap(128) as *i64 97 let ot: *i64 = sys_mmap(128) as *i64 98 nx_blob_largest(imc, oc); report(logfd, " compact \x00" as *u8, oc) 99 nx_blob_largest(imf, of); report(logfd, " framefill\x00" as *u8, of) 100 nx_blob_largest(ims, os); report(logfd, " scatter \x00" as *u8, os) 101 nx_blob_largest(imt, ot); report(logfd, " twoblock \x00" as *u8, ot) 102 103 var ok: i64 = 1 104 if oc[0] != 256 { ok = 0 } // compact: 16x16 cells 105 if oc[1] != 8 { ok = 0 } 106 if oc[2] != 8 { ok = 0 } 107 if oc[3] != 23 { ok = 0 } 108 if oc[4] != 23 { ok = 0 } 109 if of[0] != 1024 { ok = 0 } // framefill: all cells, one blob 110 if os[0] >= 32 { ok = 0 } // scatter: largest blob tiny 111 if oc[0] <= os[0] { ok = 0 } // compact dominates scatter 112 if ot[0] != 64 { ok = 0 } // twoblock: one 8x8-cell block 113 if (ot[3] - ot[1] + 1) != 8 { ok = 0 } // ...bbox width 8, did NOT merge both 114 115 if ok == 1 { 116 gp(logfd, "BLOB-GATE result=ALL-PASS verdict=GREEN\n\x00" as *u8) 117 if logfd > 0 { sys_close(logfd) } 118 sys_exit(0) 119 return 0 120 } 121 gp(logfd, "BLOB-GATE result=FAIL verdict=RED\n\x00" as *u8) 122 if logfd > 0 { sys_close(logfd) } 123 sys_exit(1) 124 return 1 125}