code wiki / _hdl_build / nx_reader_tile_gate.nx
nx_reader_tile_gate.nx source
↩ module page · 111 lines · 6380 B
1import "nx_gate_grow.nx"
2import "nx_gate_gn.nx"
3import "nx_gate_read.nx"
4// nx_reader_tile_gate.nx -- SOVEREIGN in-process referee for the tiled-pyramid emitter (rt_emit). No socket/shell:
5// it chdir's to /tmp, SYNTHESIZES a real baseline JPEG (gradient RGB -> jcw_write_color), runs rt_emit on it, and
6// asserts the pyramid ON DISK -- manifest fields, a top-level tile is a valid JPEG of the exact expected size
7// (decoded back with our own decoder), plus two NEGATIVE controls (an out-of-range tile is absent; a non-image
8// input returns a negative error). GREEN iff T1..T6 hold. Sovereign: nx_reader_tile (rt_emit) + the jpeg encoder
9// stack + nx_img_bytes_to_rgb, nx_syscalls. license_tier: ORIGINAL expect_exit: 0
10import "nx_syscalls.nx"
11import "nx_reader_tile.nx" // rt_emit (+ transitively the decode/scale/encode stack)
12import "nx_jpeg_color_write.nx"
13import "nx_quant_table.nx"
14import "nx_jpeg_huff_enc.nx"
15import "nx_zigzag.nx"
16import "nx_dct8.nx"
17import "nx_img_bytes_to_rgb.nx"
18
19func gw(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 }
20
21func g_exists(path: *u8) -> i64 { let fd: i64 = sys_openat_rd(path); if fd < 0 { return 0 } sys_close(fd); return 1 }
22func g_has(hay: *u8, n: i64, needle: *u8) -> i64 {
23 var nn: i64 = 0; while needle[nn] != (0 as u8) { nn = nn + 1 }
24 if nn == 0 { return 0 }
25 var i: i64 = 0
26 while i + nn <= n { var m: i64 = 1; var j: i64 = 0; while j < nn { if (hay[i + j] as i64) != (needle[j] as i64) { m = 0; j = nn } else { j = j + 1 } } if m == 1 { return 1 } i = i + 1 }
27 return 0
28}
29func g_jpeg_magic(path: *u8) -> i64 {
30 let b: *u8 = sys_mmap(16); let n: i64 = g_read(path, b, 16)
31 if n < 3 { return 0 }
32 if b[0] != (255 as u8) { return 0 }
33 if b[1] != (216 as u8) { return 0 }
34 return 1
35}
36
37func main() -> i64 {
38 gw("reader-tile SOVEREIGN in-process gate (synth JPEG -> rt_emit -> on-disk pyramid asserts)\n" as *u8)
39 if sys_chdir("/tmp" as *u8) != 0 { gw("CHDIR /tmp FAIL\n" as *u8); sys_exit(1) }
40
41 // ---- synthesize a real baseline JPEG test page (520x360 gradient, dims multiple of 8) ----
42 let W: i64 = 520; let H: i64 = 360
43 let rgb: *u8 = sys_mmap(W * H * 3 + 16)
44 var y: i64 = 0
45 while y < H {
46 var x: i64 = 0
47 while x < W {
48 let o: i64 = (y * W + x) * 3
49 rgb[o] = ((x * 255) / W) as u8
50 rgb[o + 1] = ((y * 255) / H) as u8
51 rgb[o + 2] = (((x + y) * 255) / (W + H)) as u8
52 x = x + 1
53 }
54 y = y + 1
55 }
56 // encoder tables (same setup rt_emit uses)
57 let dcB: *i64 = sys_mmap(20*8) as *i64; let dcV: *i64 = sys_mmap(20*8) as *i64
58 let acB: *i64 = sys_mmap(20*8) as *i64; let acV: *i64 = sys_mmap(200*8) as *i64
59 let ndc: i64 = jhe_dc_bits(dcB); jhe_dc_val(dcV); let nac: i64 = jhe_ac_bits(acB); jhe_ac_val(acV)
60 let dcCO: *i64 = sys_mmap(256*8) as *i64; let dcSI: *i64 = sys_mmap(256*8) as *i64
61 let acCO: *i64 = sys_mmap(256*8) as *i64; let acSI: *i64 = sys_mmap(256*8) as *i64
62 jhe_gen(dcB, dcV, ndc, dcCO, dcSI); jhe_gen(acB, acV, nac, acCO, acSI)
63 let qtL: *i64 = sys_mmap(64*8) as *i64; nx_qt_luma(qtL, 72)
64 let qtC: *i64 = sys_mmap(64*8) as *i64; nx_qt_chroma(qtC, 72)
65 let to_zz: *i64 = sys_mmap(64*8) as *i64; let from_zz: *i64 = sys_mmap(64*8) as *i64; nx_zigzag_init(to_zz, from_zz)
66 let DM: *i64 = sys_mmap(64*8) as *i64; nx_dct8_init(DM)
67 let scratch: *i64 = sys_mmap(64*8) as *i64; let tti: *i64 = sys_mmap(8*8) as *i64; let tto: *i64 = sys_mmap(8*8) as *i64
68 let jout: *u8 = sys_mmap(W * H * 3 + 8192)
69 let jlen: i64 = jcw_write_color(jout, W, H, rgb, qtL, qtC, dcB, dcV, acB, acV, dcCO, dcSI, acCO, acSI, to_zz, from_zz, DM, scratch, tti, tto)
70 let sfd: i64 = sys_openat_wr("rttest_src.jpg" as *u8, 0x1a4)
71 if sfd >= 0 { sys_write(sfd, jout, jlen); sys_close(sfd) }
72 var pass: i64 = 0
73 pass = pass + grow("T1 synth baseline JPEG encoded (test page)\x00" as *u8, jlen > 0)
74
75 // ---- run the emitter ----
76 let tiles: i64 = rt_emit("rttest_src.jpg" as *u8, "rttest_out" as *u8, 512)
77 gw(" rt_emit tiles=" as *u8); gn(tiles); gw("\n" as *u8)
78 pass = pass + grow("T2 rt_emit produced tiles (>0)\x00" as *u8, tiles > 0)
79
80 // ---- manifest asserts (maxlevel for 520 = 10) ----
81 let mb: *u8 = sys_mmap(2048); let mn: i64 = g_read("rttest_out/dz.json" as *u8, mb, 2048)
82 var t3: i64 = 0
83 if mn > 0 { if g_has(mb, mn, "\"width\":520" as *u8) == 1 { if g_has(mb, mn, "\"tilesize\":512" as *u8) == 1 { if g_has(mb, mn, "\"maxlevel\":10" as *u8) == 1 { t3 = 1 } } } }
84 pass = pass + grow("T3 dz.json manifest has width=520 tilesize=512 maxlevel=10\x00" as *u8, t3)
85
86 // ---- top-level tile is a valid JPEG of the exact expected size (512x360) ----
87 var t4: i64 = 0
88 if g_exists("rttest_out/10/0_0.jpg" as *u8) == 1 { if g_jpeg_magic("rttest_out/10/0_0.jpg" as *u8) == 1 {
89 let tb: *u8 = sys_mmap(1 << 20); let tn: i64 = g_read("rttest_out/10/0_0.jpg" as *u8, tb, 1 << 20)
90 let wh: *i64 = sys_mmap(16) as *i64
91 let dec: *u8 = nx_img_bytes_to_rgb(tb, tn, wh)
92 if (dec as i64) != 0 { if wh[0] == 512 { if wh[1] == 360 { t4 = 1 } } }
93 gw(" tile 10/0_0 decoded w=" as *u8); gn(wh[0]); gw(" h=" as *u8); gn(wh[1]); gw("\n" as *u8)
94 } }
95 pass = pass + grow("T4 top tile 10/0_0.jpg is a valid 512x360 JPEG (round-trips our own decoder)\x00" as *u8, t4)
96
97 // ---- NEG: an out-of-range tile must NOT exist ----
98 var t5: i64 = 0
99 if g_exists("rttest_out/10/9_9.jpg" as *u8) == 0 { t5 = 1 }
100 pass = pass + grow("T5 NEG out-of-range tile 10/9_9.jpg absent (no fabricated tiles)\x00" as *u8, t5)
101
102 // ---- NEG: a non-image input returns a negative error (fail-closed) ----
103 let bfd: i64 = sys_openat_wr("rttest_bad.txt" as *u8, 0x1a4)
104 if bfd >= 0 { sys_write(bfd, "this is not an image" as *u8, 20); sys_close(bfd) }
105 let br: i64 = rt_emit("rttest_bad.txt" as *u8, "rttest_bad_out" as *u8, 512)
106 pass = pass + grow("T6 NEG non-image input -> negative error (fail-closed)\x00" as *u8, br < 0)
107
108 gw("pass=" as *u8); gn(pass); gw("/6\n" as *u8)
109 if pass == 6 { gw("verdict=GREEN (sovereign tiled-pyramid emitter: decode->downscale-levels->/8 tiles->JPEG, on-disk verified, fail-closed)\n" as *u8); sys_exit(0); return 0 }
110 gw("verdict=RED\n" as *u8); sys_exit(1); return 1
111}