code wiki / _hdl_build / nx_imgdecode_test.nx
nx_imgdecode_test.nx source
↩ module page · 33 lines · 1519 B
1// nx_imgdecode_test.nx -- isolate the shared image decoder: read an encoded image file and run it
2// through nx_img_bytes_to_rgb (the EXACT decode the GUI uses), report success + dimensions + first pixels.
3import "nx_syscalls.nx"
4import "nx_img_bytes_to_rgb.nx"
5
6func pw(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
7func pn(v: i64) -> i64 {
8 if v==0 { pw("0" as *u8); return 0 }
9 var m: i64=v; if m<0 { pw("-" as *u8); m=0-m }
10 let b: *u8=sys_mmap(24); var k: i64=0
11 while m>0 { b[k]=(48+(m%10)) as u8; m=m/10; k=k+1 }
12 var i: i64=k-1; while i>=0 { sys_write(1,((b as i64)+i) as *u8,1); i=i-1 }
13 return 0
14}
15
16func main(argc: i64, argv: *i64) -> i64 {
17 var path: *u8 = "/tmp/img.bin\x00" as *u8
18 if argc >= 2 { path = argv[1] as *u8 }
19 let lenp: *i64 = sys_mmap(8) as *i64
20 let buf: *u8 = sys_read_file(path, lenp)
21 if (buf as i64)==0 { pw("READ FAIL\n" as *u8); return 1 }
22 let blen: i64 = lenp[0]
23 pw("bytes=" as *u8); pn(blen); pw(" magic=" as *u8)
24 var q: i64 = 0
25 while q < 4 { pn(buf[q]&0xff); pw(" " as *u8); q=q+1 }
26 pw("\n" as *u8)
27 let wh: *i64 = sys_mmap(16) as *i64
28 let rgb: *u8 = nx_img_bytes_to_rgb(buf, blen, wh)
29 if (rgb as i64)==0 { pw("DECODE FAILED (rgb=0)\n" as *u8); return 2 }
30 pw("DECODE OK w=" as *u8); pn(wh[0]); pw(" h=" as *u8); pn(wh[1])
31 pw(" firstpx(rgb)=" as *u8); pn(rgb[0]&0xff); pw("," as *u8); pn(rgb[1]&0xff); pw("," as *u8); pn(rgb[2]&0xff); pw("\n" as *u8)
32 return 0
33}