code wiki / _hdl_build / nx_charjudge.nx
nx_charjudge.nx source
↩ module page · 56 lines · 2723 B
1// nx_charjudge.nx -- CLI for the v2 character-image judge (core = nx_charjudge_lib, gate =
2// nx_charjudge_gate). Decodes a PNG sovereignly, judges it, prints all axes as JSON.
3// HEADLINE = MIN(composition, palette, contour, face) -- see the lib header for why each axis exists
4// and which adversarial input it kills. Thresholds ride knowledge/charjudge.conf.
5// license_tier: ORIGINAL nx_charjudge <img.png> <label>
6import "nx_syscalls.nx"
7import "nx_itoa_lib.nx"
8import "nx_png_decoder.nx"
9import "nx_charjudge_lib.nx"
10
11func cjc_hw(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
12func cjc_pn(v: i64) -> i64 { nxi_out(v); return 0 }
13
14func main(argc: i64, argv: *i64) -> i64 {
15 let path: *u8 = argv[1] as *u8
16 let label: *u8 = argv[2] as *u8
17 let lenp: *i64 = sys_mmap(16) as *i64
18 let raw: *u8 = sys_read_file(path, lenp)
19 if (raw as i64) == 0 { cjc_hw("{\x22error\x22:\x22read\x22}\n" as *u8); return 2 }
20 let res: *NxPngResult = nx_png_decode(raw, lenp[0])
21 if (res as i64) == 0 { cjc_hw("{\x22error\x22:\x22decode\x22}\n" as *u8); return 3 }
22 if res.error_code != 0 { cjc_hw("{\x22error\x22:\x22decode-rc\x22}\n" as *u8); return 3 }
23 let hdr: *NxPngHeader = res.header
24 let w: i64 = hdr.width
25 let h: i64 = hdr.height
26 let ch: i64 = res.n_channels
27 let px: *u8 = res.pixels
28 if w <= 0 { cjc_hw("{\x22error\x22:\x22dims\x22}\n" as *u8); return 3 }
29 let fb: *i64 = sys_mmap(w*h*8) as *i64
30 var q: i64 = 0
31 while q < w*h {
32 var pr: i64 = 0
33 var pg: i64 = 0
34 var pb: i64 = 0
35 if ch >= 3 { pr = px[q*ch] as i64; pg = px[q*ch+1] as i64; pb = px[q*ch+2] as i64 }
36 if ch <= 2 { pr = px[q*ch] as i64; pg = pr; pb = pr }
37 fb[q] = pr + pg*256 + pb*CHJ_MAGIC_65536
38 q = q + 1
39 }
40 let out: *i64 = sys_mmap(96) as *i64
41 chj_judge(fb, w, h, "knowledge/charjudge.conf" as *u8, out)
42 cjc_hw("{\x22label\x22:\x22" as *u8); cjc_hw(label)
43 cjc_hw("\x22,\x22w\x22:" as *u8); cjc_pn(w); cjc_hw(",\x22h\x22:" as *u8); cjc_pn(h)
44 cjc_hw(",\x22composition\x22:" as *u8); cjc_pn(out[0])
45 cjc_hw(",\x22palette_axis\x22:" as *u8); cjc_pn(out[1])
46 cjc_hw(",\x22contour_axis\x22:" as *u8); cjc_pn(out[2])
47 cjc_hw(",\x22face_axis\x22:" as *u8); cjc_pn(out[3])
48 cjc_hw(",\x22CHARJUDGE\x22:" as *u8); cjc_pn(out[4])
49 cjc_hw(",\x22raw\x22:{\x22smooth_permil\x22:" as *u8); cjc_pn(out[5])
50 cjc_hw(",\x22detail_permil\x22:" as *u8); cjc_pn(out[6])
51 cjc_hw(",\x22palette_buckets\x22:" as *u8); cjc_pn(out[7])
52 cjc_hw(",\x22coherent_permil\x22:" as *u8); cjc_pn(out[8])
53 cjc_hw(",\x22edge_total\x22:" as *u8); cjc_pn(out[9])
54 cjc_hw("}}\n" as *u8)
55 return 0
56}