code wiki / _hdl_build / nx_bmp_to_png.nx

nx_bmp_to_png.nx source

↩ module page · 46 lines · 1985 B

1// nx_bmp_to_png.nx -- convert a rendered BMP to PNG (top crop) so it can be VIEWED. Reads the styled 2// wiki render, takes the top rows (the visible part), writes a PNG via the sovereign nx_png_write. 3import "nx_syscalls.nx" 4import "nx_png_write.nx" 5const K_MAGIC_5000: i64 = 5000 6 7func bp_u32le(b: *u8, off: i64) -> i64 { 8 return (b[off] & 0xff) | ((b[off+1] & 0xff) << 8) | ((b[off+2] & 0xff) << 16) | ((b[off+3] & 0xff) << 24) 9} 10 11func main() -> i64 { 12 let path: *u8 = "knowledge/status/wiki_styled.bmp\x00" 13 let lp: *i64 = sys_mmap(16) as *i64 14 let bmp: *u8 = sys_read_file(path, lp) 15 let blen: i64 = *lp 16 if blen < 54 { sys_write(2, "bad bmp\n" as *u8, 8); return 1 } 17 let w: i64 = bp_u32le(bmp, 18) 18 let h: i64 = bp_u32le(bmp, 22) 19 let dataoff: i64 = bp_u32le(bmp, 10) 20 let row3: i64 = w * 3 21 let pad: i64 = (4 - (row3 % 4)) % 4 22 let rowstride: i64 = row3 + pad 23 let top_off: i64 = 0 // crop window START (the render already crops to <main>) 24 var ch: i64 = h - top_off 25 if ch > K_MAGIC_5000 { ch = K_MAGIC_5000 } // full (set a smaller cap + top_off for a readable sub-crop) 26 if ch < 0 { ch = 0 } 27 let rgb: *u8 = sys_mmap(w * ch * 3 + 16) 28 var y: i64 = 0 29 while y < ch { 30 let bmprow: i64 = h - 1 - (y + top_off) // BMP is bottom-up; image-top = file-end rows 31 let src: i64 = dataoff + bmprow * rowstride 32 var x: i64 = 0 33 while x < w { 34 let s: i64 = src + x * 3 35 let d: i64 = (y * w + x) * 3 36 rgb[d] = bmp[s + 2] // R (BMP stores BGR) 37 rgb[d+1] = bmp[s + 1] // G 38 rgb[d+2] = bmp[s] // B 39 x = x + 1 40 } 41 y = y + 1 42 } 43 nx_png_write_rgb("knowledge/status/wiki_styled.png\x00" as *u8, rgb, w, ch) 44 sys_write(1, "wrote knowledge/status/wiki_styled.png\n" as *u8, 39) 45 return 0 46}