nx_img_convert.nx source
↩ module page · 48 lines · 2205 B
1// nx_img_convert.nx -- sovereign image CONVERT (resize -> encode), end-to-end.
2//
3// Composes the sovereign resampler (nx_img_scale) + the sovereign PNG encoder
4// (nx_png_write) -- ZERO 3rd-party. This is the "upscaling/downscaling when
5// media is converted" path: pixels in -> scaled -> real PNG out that any viewer
6// can see. Reuses existing organs (DRY #15) rather than rebuilding decode/
7// encode that a parallel media-stack workstream already shipped.
8//
9// The input-decode side (real PNG/JPEG -> pixels via nx_png_decode /
10// nx_jpeg_decode -> here) is the immediate next rung.
11// license_tier: ORIGINAL
12
13import "nx_syscalls.nx"
14import "nx_img_scale.nx"
15import "nx_png_write.nx"
16
17// Scale an 8-bit interleaved RGB image to dw x dh and write it as a PNG file.
18// Returns 0 ok, negative on error.
19func nx_img_convert_rgb_to_png(src: *u8, sw: i64, sh: i64,
20 dst_path: *u8, dw: i64, dh: i64) -> i64 {
21 let dst: *u8 = sys_mmap(dw * dh * 3 + 16)
22 if nx_img_scale_bilinear(src, sw, sh, 3, dst, dw, dh) != 0 { return 0 - 1 }
23 return nx_png_write_rgb(dst_path, dst, dw, dh)
24}
25
26// Demo: build a 48x32 RGB gradient, write it + a 3x UPSCALE + a 1/2 DOWNSCALE
27// as real PNGs (viewable), proving the sovereign convert path both directions.
28func main() -> i64 {
29 let sw: i64 = 48
30 let sh: i64 = 32
31 let src: *u8 = sys_mmap(sw * sh * 3 + 16)
32 var y: i64 = 0
33 while y < sh {
34 var x: i64 = 0
35 while x < sw {
36 let o: i64 = (y * sw + x) * 3
37 src[o + 0] = (x * 255 / (sw - 1)) as u8 // R ramps across x
38 src[o + 1] = (y * 255 / (sh - 1)) as u8 // G ramps down y
39 src[o + 2] = ((x + y) * 255 / (sw + sh - 2)) as u8 // B diagonal
40 x = x + 1
41 }
42 y = y + 1
43 }
44 if nx_png_write_rgb("/mnt/c/Users/elder/nishi-core/nxc2/_sv_orig.png" as *u8, src, sw, sh) != 0 { return 1 }
45 if nx_img_convert_rgb_to_png(src, sw, sh, "/mnt/c/Users/elder/nishi-core/nxc2/_sv_up.png" as *u8, 144, 96) != 0 { return 2 }
46 if nx_img_convert_rgb_to_png(src, sw, sh, "/mnt/c/Users/elder/nishi-core/nxc2/_sv_down.png" as *u8, 24, 16) != 0 { return 3 }
47 return 0
48}