code wiki / _hdl_build / nx_reader_page_prep.nx

nx_reader_page_prep.nx source

↩ module page · 41 lines · 2698 B

1// nx_reader_page_prep.nx -- one-shot per-page PREP for the intelligent-zoom reader: compose the two gated libs 2// (nx_reader_tile rt_emit = DZI-style tile pyramid; nx_reader_panels rp_emit = guided-view panel rects) so ONE 3// fork produces <outdir>/dz.json + <outdir>/<level>/<col>_<row>.jpg + <outdir>/panels.json for a page image. 4// The reader daemon forks this per page (mmaps die with the child), exactly the nx_galx_thumb deploy contract. 5// usage: nx_reader_page_prep <src.jpg|png> <outdir> [rtl=1] [tilesize=512] 6// exit: 0 ok (tiles>0; panels may honestly be 0 -> viewer falls back to whole-page zoom) 7// 1 bad args ; 2 tile emit failed ; 3 panel emit failed (read/decode) 8// license_tier: ORIGINAL 9import "nx_syscalls.nx" 10import "nx_itoa_lib.nx" // shared MSB-first emitter (zero-alloc) 11import "nx_reader_tile.nx" // rt_emit 12import "nx_reader_panels.nx" // rp_emit 13 14func pp_w(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 } 15// MIGRATED to the shared emitter (debt 1785563586). The old body mmapped a scratch buffer 16// per call and never freed it. At PAGE granularity that is 4096B leaked PER CALL -- the 17// defect that took 28.5GB of a 36GB host in nx_ts_lumadiff (2MB input, ~3.66M calls). 18// nxi_* is MSB-first, allocates NOTHING, and emits identical bytes including the sign. 19func pp_n(v: i64) -> i64 { nxi_out(v); return 0 } 20func pp_atoi(s: *u8) -> i64 { var v: i64 = 0; var i: i64 = 0; while s[i] != (0 as u8) { let c: i64 = s[i] as i64; if c < 48 { return v } if c > 57 { return v } v = v * 10 + (c - 48); i = i + 1 } return v } 21 22func main(argc: i64, argv: *i64) -> i64 { 23 if argc < 3 { pp_w("usage: nx_reader_page_prep <src> <outdir> [rtl=1] [tilesize=512]\n" as *u8); sys_exit(1); return 1 } 24 let src: *u8 = argv[1] as *u8 25 let outdir: *u8 = argv[2] as *u8 26 var rtl: i64 = 1 27 if argc > 3 { rtl = pp_atoi(argv[3] as *u8); if rtl != 0 { rtl = 1 } } 28 var ts: i64 = 512 29 if argc > 4 { let t: i64 = pp_atoi(argv[4] as *u8); if t >= 64 { ts = t } } 30 31 let tiles: i64 = rt_emit(src, outdir, ts) 32 if tiles == 0 - 7 { pp_w("PAGEPREP RAW (untileable format -> browser-native display, dz.json{raw:1} written)\n" as *u8); sys_exit(0); return 0 } 33 if tiles <= 0 { pp_w("PAGEPREP-FAIL tiles rc=" as *u8); pp_n(tiles); pp_w("\n" as *u8); sys_exit(2); return 2 } 34 let np: i64 = rp_emit(src, outdir, rtl) 35 if np < 0 { pp_w("PAGEPREP-FAIL panels rc=" as *u8); pp_n(np); pp_w("\n" as *u8); sys_exit(3); return 3 } 36 pp_w("PAGEPREP OK tiles=" as *u8); pp_n(tiles) 37 pp_w(" panels=" as *u8); pp_n(np) 38 pp_w(" rtl=" as *u8); pp_n(rtl) 39 pp_w("\n" as *u8) 40 sys_exit(0); return 0 41}