nx_skinsample_gate.nx source
↩ module page · 139 lines · 8060 B
1// nx_skinsample_gate.nx -- the referee for nx_skinsample_lib (aesthetictwin AT23, 2026-09-16). In-process on synthetic pixels
2// (no network, no real card): a known sRGB skin patch on a blue field measures to its known CIELAB values and typology angle
3// within a stated tolerance, the mask counts exactly the patch, an all-blue image is UNMEASURED rather than a colour, the
4// arctangent reproduces its known answers, white maps to L* 100 with no chroma, and a card write refuses to clobber an
5// existing row while appending four measured rows to a bare card. Values are printed with gv_kv; the verdict line stays LAST.
6// license_tier: ORIGINAL No hw writes (Rule 26).
7import "nx_syscalls.nx"
8import "nx_gate_verdict.nx"
9import "nx_skinsample_lib.nx"
10
11const SG_W: i64 = 100
12const SG_H: i64 = 100
13const SG_PATCH: i64 = 50
14const SG_PATCH_X: i64 = 25
15const SG_PATCH_Y: i64 = 25
16const SG_R: i64 = 200
17const SG_G: i64 = 150
18const SG_B: i64 = 120
19const SG_BLUE: i64 = 255
20const SG_WHITE: i64 = 255
21// CIE L*a*b* of sRGB (200,150,120) under D65, computed from the standard formulae: L* 66.08, a* 14.85, b* 23.12; ITA 34.8
22const SG_EXP_L_E3: i64 = 66080
23const SG_EXP_A_E3: i64 = 14850
24const SG_EXP_B_E3: i64 = 23120
25const SG_EXP_ITA_DEG10: i64 = 348
26const SG_TOL_L_E3: i64 = 400
27const SG_TOL_AB_E3: i64 = 600
28const SG_TOL_ITA_DEG10: i64 = 8
29const SG_WHITE_L_E3: i64 = 100000
30const SG_TOL_WHITE_L_E3: i64 = 600
31// atan(1) = 45.0 deg and atan(0.5) = 26.57 deg, both as deg10
32const SG_ATAN45_L_E3: i64 = 60000
33const SG_ATAN45_B_E3: i64 = 10000
34const SG_ATAN45_DEG10: i64 = 450
35const SG_ATANHALF_L_E3: i64 = 55000
36const SG_ATANHALF_DEG10: i64 = 266
37const SG_TOL_ATAN: i64 = 2
38const SG_DIR: *u8 = "/tmp/skinsample_gate"
39const SG_KDIR: *u8 = "/tmp/skinsample_gate/knowledge"
40const SG_CARD_DIR: *u8 = "/tmp/skinsample_gate/knowledge/twincard"
41const SG_CARD_FULL: *u8 = "/tmp/skinsample_gate/knowledge/twincard/fx.card"
42const SG_CARD_BARE: *u8 = "/tmp/skinsample_gate/knowledge/twincard/fy.card"
43const SG_CARD_FULL_TEXT: *u8 = "card|fx|Fixture Full|2026-09-16|fixture\naxis|skin|skin_L|C|60|observed|fx1|2026-09\n"
44const SG_CARD_BARE_TEXT: *u8 = "card|fy|Fixture Bare|2026-09-16|fixture\n"
45const SG_CARD_ROWS: i64 = 4
46const SG_BPP: i64 = 3
47const SG_MODE644: i64 = 420
48const SG_MODE755: i64 = 493
49const SG_I64: i64 = 8
50
51func sg_fill(rgb: *u8, w: i64, h: i64, x0: i64, y0: i64, pw: i64, ph: i64, r: i64, g: i64, b: i64) -> i64 {
52 var y: i64 = y0
53 while y < y0 + ph {
54 var x: i64 = x0
55 while x < x0 + pw {
56 let o: i64 = (y * w + x) * SG_BPP
57 rgb[o] = r as u8
58 rgb[o + 1] = g as u8
59 rgb[o + 2] = b as u8
60 x = x + 1
61 }
62 y = y + 1
63 }
64 return 0
65}
66func sg_abs(v: i64) -> i64 { if v < 0 { return 0 - v } return v }
67func sg_write(path: *u8, text: *u8) -> i64 {
68 let fd: i64 = sys_openat_wr(path, SG_MODE644)
69 if fd < 0 { return 0 - 1 }
70 let w: i64 = sys_write(fd, text, ri_slen(text))
71 sys_close(fd)
72 return w
73}
74
75func main(argc: i64, argv: *i64) -> i64 {
76 gv_head("nx_skinsample_gate -- skin colour ruler: mask, linear mean, CIELAB, typology angle, card write" as *u8)
77 let ctr: *i64 = gv_ctr()
78 // a blue field with a 50x50 skin patch
79 let rgb: *u8 = sys_mmap(SG_W * SG_H * SG_BPP)
80 sg_fill(rgb, SG_W, SG_H, 0, 0, SG_W, SG_H, 0, 0, SG_BLUE)
81 sg_fill(rgb, SG_W, SG_H, SG_PATCH_X, SG_PATCH_Y, SG_PATCH, SG_PATCH, SG_R, SG_G, SG_B)
82 gv_check("mask-admits-the-skin-triple" as *u8, sks_is_skin(SG_R, SG_G, SG_B), ctr)
83 gv_check("neg-control-mask-rejects-pure-blue" as *u8, (sks_is_skin(0, 0, SG_BLUE) == 0) as i64, ctr)
84 let res: *i64 = sys_mmap(SKS_R_N * SG_I64) as *i64
85 let count: i64 = sks_measure_rgb(rgb, SG_W, SG_H, res)
86 gv_check("fixture-reached-the-condition-mask-counts-exactly-the-patch" as *u8, (count == SG_PATCH * SG_PATCH) as i64, ctr)
87 gv_check("total-pixels-is-the-image" as *u8, (res[SKS_R_TOTAL] == SG_W * SG_H) as i64, ctr)
88 gv_check("lab-L-within-tolerance" as *u8, (sg_abs(res[SKS_R_L] - SG_EXP_L_E3) <= SG_TOL_L_E3) as i64, ctr)
89 gv_check("lab-a-within-tolerance" as *u8, (sg_abs(res[SKS_R_A] - SG_EXP_A_E3) <= SG_TOL_AB_E3) as i64, ctr)
90 gv_check("lab-b-within-tolerance" as *u8, (sg_abs(res[SKS_R_B] - SG_EXP_B_E3) <= SG_TOL_AB_E3) as i64, ctr)
91 gv_check("ita-within-tolerance" as *u8, (sg_abs(res[SKS_R_ITA] - SG_EXP_ITA_DEG10) <= SG_TOL_ITA_DEG10) as i64, ctr)
92 // the blue field alone is UNMEASURED, never a colour
93 let blue: *u8 = sys_mmap(SG_W * SG_H * SG_BPP)
94 sg_fill(blue, SG_W, SG_H, 0, 0, SG_W, SG_H, 0, 0, SG_BLUE)
95 let bres: *i64 = sys_mmap(SKS_R_N * SG_I64) as *i64
96 let bcount: i64 = sks_measure_rgb(blue, SG_W, SG_H, bres)
97 gv_check("neg-control-all-blue-is-unmeasured" as *u8, (bcount == 0) as i64, ctr)
98 gv_check("neg-control-unmeasured-reports-no-colour" as *u8, (bres[SKS_R_L] == 0) as i64 * (bres[SKS_R_ITA] == 0) as i64, ctr)
99 // the angle on known ratios
100 let a45: i64 = sks_ita_deg10(SG_ATAN45_L_E3, SG_ATAN45_B_E3)
101 let ahalf: i64 = sks_ita_deg10(SG_ATANHALF_L_E3, SG_ATAN45_B_E3)
102 gv_check("ita-atan-1-is-45-deg" as *u8, (sg_abs(a45 - SG_ATAN45_DEG10) <= SG_TOL_ATAN) as i64, ctr)
103 gv_check("ita-atan-half-is-26.6-deg" as *u8, (sg_abs(ahalf - SG_ATANHALF_DEG10) <= SG_TOL_ATAN) as i64, ctr)
104 gv_check("ita-below-pivot-is-negative" as *u8, (sks_ita_deg10(SG_ATANHALF_L_E3 - SG_ATAN45_B_E3, SG_ATAN45_B_E3) < 0) as i64, ctr)
105 // white is L* 100 with no chroma
106 let wl: *i64 = sys_mmap(SKS_R_N * SG_I64) as *i64
107 sks_lab_of_rgb(SG_WHITE, SG_WHITE, SG_WHITE, wl)
108 gv_check("white-L-is-100" as *u8, (sg_abs(wl[0] - SG_WHITE_L_E3) <= SG_TOL_WHITE_L_E3) as i64, ctr)
109 gv_check("white-has-no-chroma" as *u8, (sg_abs(wl[1]) <= SG_TOL_AB_E3) as i64 * (sg_abs(wl[2]) <= SG_TOL_AB_E3) as i64, ctr)
110 // card write: refuses an existing row, appends four measured rows to a bare card, refuses an absent card
111 sys_mkdir(SG_DIR, SG_MODE755)
112 sys_mkdir(SG_KDIR, SG_MODE755)
113 sys_mkdir(SG_CARD_DIR, SG_MODE755)
114 let w1: i64 = sg_write(SG_CARD_FULL, SG_CARD_FULL_TEXT)
115 let w2: i64 = sg_write(SG_CARD_BARE, SG_CARD_BARE_TEXT)
116 gv_need("fixture-cards-written" as *u8, (w1 > 0) as i64 * (w2 > 0) as i64, ctr)
117 let cd: i64 = sys_chdir(SG_DIR)
118 gv_need("fixture-chdir" as *u8, (cd == 0) as i64, ctr)
119 gv_check("neg-control-card-write-refuses-existing-row" as *u8, (sks_card_write("fx" as *u8, "fx1" as *u8, "2026-09" as *u8, res) == SKS_E_EXISTS) as i64, ctr)
120 let appended: i64 = sks_card_write("fy" as *u8, "fy1" as *u8, "2026-09" as *u8, res)
121 gv_check("card-write-appends-four-measured-rows" as *u8, (appended == SG_CARD_ROWS) as i64, ctr)
122 let cl: *i64 = sys_mmap(SG_I64) as *i64
123 let card: *u8 = ri_load(SG_CARD_BARE, cl)
124 gv_check("card-carries-measured-ita-row" as *u8, ri_contains(card, cl[0], "axis|skin|ita|C|" as *u8), ctr)
125 gv_check("card-rows-say-measured" as *u8, ri_contains(card, cl[0], "|measured|fy1|2026-09\n" as *u8), ctr)
126 gv_check("card-write-second-time-refused" as *u8, (sks_card_write("fy" as *u8, "fy1" as *u8, "2026-09" as *u8, res) == SKS_E_EXISTS) as i64, ctr)
127 gv_check("neg-control-absent-card-unreadable" as *u8, (sks_card_write("nobody" as *u8, "x" as *u8, "2026-09" as *u8, res) == SKS_E_READ) as i64, ctr)
128 gv_values_head()
129 gv_kv("mask_count" as *u8, count)
130 gv_kv("L_e3" as *u8, res[SKS_R_L])
131 gv_kv("a_e3" as *u8, res[SKS_R_A])
132 gv_kv("b_e3" as *u8, res[SKS_R_B])
133 gv_kv("ita_deg10" as *u8, res[SKS_R_ITA])
134 gv_kv("atan1_deg10" as *u8, a45)
135 gv_kv("atanhalf_deg10" as *u8, ahalf)
136 gv_kv("white_L_e3" as *u8, wl[0])
137 gv_kv("card_rows_appended" as *u8, appended)
138 return gv_verdict("nx_skinsample_gate" as *u8, ctr, "a known skin patch measures to its CIELAB and ITA within tolerance, the mask counts exactly the patch, all-blue is UNMEASURED, the CORDIC angle reproduces atan(1) and atan(0.5), white is L* 100 achromatic, and the card write refuses existing rows and appends four measured rows to a bare card" as *u8)
139}