code wiki / (root) / nx_urf_make.nx

nx_urf_make.nx source

↩ module page · 99 lines · 4335 B

1// nx_urf_make.nx -- generate a real test-pattern page and write a sovereign .urf artifact to disk. 2// Validates the encoder at full-page scale (US Letter @300dpi grayscale) and yields a concrete file 3// ready to print. Writes only a LOCAL file -- no network, no printing. license_tier: ORIGINAL 4 5import "nx_syscalls.nx" 6import "nx_pwg_rle.nx" 7import "nx_urf.nx" 8const K_MAGIC_2550: i64 = 2550 9const K_MAGIC_3300: i64 = 3300 10const K_MAGIC_1200: i64 = 1200 11const K_MAGIC_1212: i64 = 1212 12const K_MAGIC_1850: i64 = 1850 13const K_MAGIC_2088: i64 = 2088 14const K_MAGIC_2100: i64 = 2100 15const K_MAGIC_1838: i64 = 1838 16const K_MAGIC_65536: i64 = 65536 17 18func p(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 } 19func pn(v: i64) -> i64 { 20 if v == 0 { sys_write(1, "0" as *u8, 1); return 0 } 21 var m: i64 = v; if m < 0 { sys_write(1, "-" as *u8, 1); m = 0 - m } 22 let t: *u8 = sys_mmap(24); var k: i64 = 0 23 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 } 24 let b: *u8 = sys_mmap(24); var i: i64 = 0 25 while i < k { b[i] = t[k - 1 - i]; i = i + 1 } 26 sys_write(1, b, k); return 0 27} 28 29// fill rows [y0,y1) cols [x0,x1) with val 30func fill_rect(bm: *u8, W: i64, y0: i64, y1: i64, x0: i64, x1: i64, val: i64) -> i64 { 31 var y: i64 = y0 32 while y < y1 { 33 let base: i64 = y * W 34 var x: i64 = x0 35 while x < x1 { bm[base + x] = val as u8; x = x + 1 } 36 y = y + 1 37 } 38 return 0 39} 40 41func write_all(fd: i64, buf: *u8, len: i64) -> i64 { 42 var sent: i64 = 0 43 while sent < len { 44 let w: i64 = sys_write(fd, ((buf as i64) + sent) as *u8, len - sent) 45 if w <= 0 { return 0 - 1 } 46 sent = sent + w 47 } 48 return 0 49} 50 51func main() -> i64 { 52 let W: i64 = K_MAGIC_2550 // 8.5in * 300dpi 53 let H: i64 = K_MAGIC_3300 // 11in * 300dpi 54 let npix: i64 = W * H 55 let bm: *u8 = sys_mmap(npix) 56 // sGray: 0xFF assumed white, 0x00 black (final orientation confirmed by the live print) 57 fill_rect(bm, W, 0, H, 0, W, 0xFF) // white page 58 fill_rect(bm, W, 0, 8, 0, W, 0x00) // top border 59 fill_rect(bm, W, H - 8, H, 0, W, 0x00) // bottom border 60 fill_rect(bm, W, 0, H, 0, 8, 0x00) // left border 61 fill_rect(bm, W, 0, H, W - 8, W, 0x00) // right border 62 // four grayscale bars near the top (proves 8-bit gray levels survive the encode) -- thin, toner-light 63 fill_rect(bm, W, 80, 130, 80, W - 80, 0x00) 64 fill_rect(bm, W, 150, 200, 80, W - 80, 0x40) 65 fill_rect(bm, W, 220, 270, 80, W - 80, 0x80) 66 fill_rect(bm, W, 290, 340, 80, W - 80, 0xC0) 67 // a HOLLOW box in the center (outline only -- proves geometry, sips toner) 68 fill_rect(bm, W, K_MAGIC_1200, K_MAGIC_1212, 700, K_MAGIC_1850, 0x00) 69 fill_rect(bm, W, K_MAGIC_2088, K_MAGIC_2100, 700, K_MAGIC_1850, 0x00) 70 fill_rect(bm, W, K_MAGIC_1200, K_MAGIC_2100, 700, 712, 0x00) 71 fill_rect(bm, W, K_MAGIC_1200, K_MAGIC_2100, K_MAGIC_1838, K_MAGIC_1850, 0x00) 72 // thin diagonal X across the page (1px -- minimal toner, clearly "ours") 73 var t: i64 = 0 74 while t < W { 75 let yd: i64 = t * H / W 76 if yd >= 0 { if yd < H { bm[yd * W + t] = 0 as u8; bm[(H - 1 - yd) * W + t] = 0 as u8 } } 77 t = t + 1 78 } 79 80 let cap: i64 = npix + K_MAGIC_65536 81 let out: *u8 = sys_mmap(cap) 82 let len: i64 = nx_urf_encode_gray_page(out, bm, W, H, 300, NX_URF_DUP_LONG, NX_URF_Q_NORMAL) 83 84 p("URF page "); pn(W); p("x"); pn(H); p(" @300dpi gray raw="); pn(npix) 85 p("B encoded="); pn(len); p("B ratio="); pn(npix / len); p("x\n") 86 87 let fd: i64 = sys_openat_wr("knowledge/fetched/nishi_test.urf" as *u8, 0x1a4) 88 if fd < 0 { p("write failed (dir missing?)\n"); sys_exit(1); return 1 } 89 if write_all(fd, out, len) != 0 { p("write incomplete\n"); sys_close(fd); sys_exit(1); return 1 } 90 sys_close(fd) 91 p("SAVED knowledge/fetched/nishi_test.urf ("); pn(len); p(" bytes)\n") 92 93 // self-check: decode the embedded RLE back and confirm the pixel count round-trips 94 let chk: *u8 = sys_mmap(npix) 95 let dl: i64 = nx_rle_decode(((out as i64) + 44) as *u8, len - 44, W, H, 1, chk, npix) 96 p("decode round-trip pixels="); pn(dl); p(" (expect "); pn(npix); p(")\n") 97 if dl != npix { sys_exit(2); return 2 } 98 sys_exit(0); return 0 99}