code wiki / (root) / nx_img_convert_loop_gate.nx

nx_img_convert_loop_gate.nx source

↩ module page · 79 lines · 2831 B

1// nx_img_convert_loop_gate.nx -- full sovereign convert loop, gated. 2// 3// Proves the WHOLE pipeline composes correctly with ZERO 3rd-party: 4// gradient -> nx_png_write (encode) -> file -> ss_readall -> nx_png_decode 5// -> ASSERT decoded == original (byte-exact round-trip) -> nx_img_scale the 6// DECODED pixels -> nx_png_write -> re-decode -> ASSERT dimensions. 7// All organs already existed except the scaler/convert (DRY #15 reuse). 8// Returns 0 iff every step holds; a nonzero code pinpoints the first failure. 9 10import "nx_syscalls.nx" 11import "nx_img_scale.nx" 12import "nx_png_write.nx" 13import "nx_png_decoder.nx" 14import "nx_seg_store.nx" 15 16func main() -> i64 { 17 let sw: i64 = 48 18 let sh: i64 = 32 19 let src: *u8 = sys_mmap(sw * sh * 3 + 16) 20 var y: i64 = 0 21 while y < sh { 22 var x: i64 = 0 23 while x < sw { 24 let o: i64 = (y * sw + x) * 3 25 src[o + 0] = (x * 255 / (sw - 1)) as u8 26 src[o + 1] = (y * 255 / (sh - 1)) as u8 27 src[o + 2] = ((x + y) * 255 / (sw + sh - 2)) as u8 28 x = x + 1 29 } 30 y = y + 1 31 } 32 33 // 1. ENCODE source -> real PNG file 34 let srcpath: *u8 = "/mnt/c/Users/elder/nishi-core/nxc2/_sv_loop_src.png" as *u8 35 if nx_png_write_rgb(srcpath, src, sw, sh) != 0 { return 1 } 36 37 // 2. READ the PNG bytes back 38 let szbox: *i64 = sys_mmap(16) as *i64 39 let png: *u8 = ss_readall(srcpath, szbox) 40 if szbox[0] <= 0 { return 2 } 41 42 // 3. DECODE 43 let r: *NxPngResult = nx_png_decode(png, szbox[0]) 44 if r.error_code != NX_PNG_OK { return 3 } 45 let hd: *NxPngHeader = r.header 46 if hd.width != sw { return 4 } 47 if hd.height != sh { return 5 } 48 if r.n_channels != 3 { return 6 } 49 50 // 4. ROUND-TRIP: decoded pixels must equal the original gradient byte-exact 51 let dpx: *u8 = r.pixels 52 let npx: i64 = sw * sh * 3 53 var i: i64 = 0 54 while i < npx { 55 if dpx[i] != src[i] { return 7 } 56 i = i + 1 57 } 58 59 // 5. SCALE the DECODED image (3x) and ENCODE -> viewable PNG 60 let dw: i64 = 144 61 let dh: i64 = 96 62 let dst: *u8 = sys_mmap(dw * dh * 3 + 16) 63 if nx_img_scale_bilinear(dpx, hd.width, hd.height, r.n_channels, dst, dw, dh) != 0 { return 8 } 64 let outpath: *u8 = "/mnt/c/Users/elder/nishi-core/nxc2/_sv_loop_out.png" as *u8 65 if nx_png_write_rgb(outpath, dst, dw, dh) != 0 { return 9 } 66 67 // 6. RE-DECODE the output and verify the loop closes 68 let szbox2: *i64 = sys_mmap(16) as *i64 69 let png2: *u8 = ss_readall(outpath, szbox2) 70 if szbox2[0] <= 0 { return 10 } 71 let r2: *NxPngResult = nx_png_decode(png2, szbox2[0]) 72 if r2.error_code != NX_PNG_OK { return 11 } 73 let hd2: *NxPngHeader = r2.header 74 if hd2.width != dw { return 12 } 75 if hd2.height != dh { return 13 } 76 if r2.n_channels != 3 { return 14 } 77 78 return 0 79}