nx_jpeg_ppm_dump_test.nx source
↩ module page · 72 lines · 2439 B
1// nx_jpeg_ppm_dump_test.nx -- COLOR proof dumper for the bits-up
2// JPEG decoder. Reads the staged real source.jpg, decodes it to
3// full-resolution RGB via nx_jpeg_decode_rgb (decode + chroma
4// upsample + YCbCr->RGB), and writes a binary PPM (P6) so the
5// Windows GDI+ decode can be diffed against it per-channel.
6//
7// Asserts dims == 239x178, then emits the PPM.
8//
9// expect_exit: 0
10// license_tier: ORIGINAL
11
12import "nx_syscalls.nx"
13import "nx_jpeg_ascii.nx"
14
15func _put_dec(buf: *u8, pos: i64, v: i64) -> i64 {
16 if v == 0 { buf[pos] = 0x30 as u8; return pos + 1 }
17 var x: i64 = v
18 let tmp: *u8 = sys_mmap(24)
19 var n: i64 = 0
20 while x > 0 { tmp[n] = (0x30 + (x % 10)) as u8; x = x / 10; n = n + 1 }
21 var i: i64 = 0
22 var p: i64 = pos
23 while i < n { buf[p] = tmp[n - 1 - i]; p = p + 1; i = i + 1 }
24 return p
25}
26
27func main() -> i64 {
28 let in_path: *u8 = "/mnt/c/Users/elder/nishi-browser-proofs/source.jpg\x00"
29 let len_p: *i64 = sys_mmap(8) as *i64
30 let jpeg: *u8 = sys_read_file(in_path, len_p)
31 let jpeg_len: i64 = len_p[0]
32 if (jpeg as i64) == 0 { return 10 }
33 if jpeg_len < 100 { return 11 }
34
35 let pp_rgb: *i64 = sys_mmap(8) as *i64
36 let pp_w: *i64 = sys_mmap(8) as *i64
37 let pp_h: *i64 = sys_mmap(8) as *i64
38 let dr: i64 = nx_jpeg_decode_rgb(jpeg, jpeg_len, pp_rgb, pp_w, pp_h)
39 if dr != NX_JPEG_ASCII_OK { return 20 + dr }
40 let rgb: *u8 = pp_rgb[0] as *u8
41 let w: i64 = pp_w[0]
42 let h: i64 = pp_h[0]
43 if w != 239 { return 41 }
44 if h != 178 { return 42 }
45
46 // PPM header: "P6\n<w> <h>\n255\n"
47 let hdr: *u8 = sys_mmap(32)
48 var hp: i64 = 0
49 hdr[hp]=0x50 as u8; hp=hp+1 // 'P'
50 hdr[hp]=0x36 as u8; hp=hp+1 // '6'
51 hdr[hp]=0x0A as u8; hp=hp+1
52 hp = _put_dec(hdr, hp, w)
53 hdr[hp]=0x20 as u8; hp=hp+1
54 hp = _put_dec(hdr, hp, h)
55 hdr[hp]=0x0A as u8; hp=hp+1
56 hdr[hp]=0x32 as u8; hp=hp+1 // '2'
57 hdr[hp]=0x35 as u8; hp=hp+1 // '5'
58 hdr[hp]=0x35 as u8; hp=hp+1 // '5'
59 hdr[hp]=0x0A as u8; hp=hp+1
60
61 let out_path: *u8 = "/mnt/c/Users/elder/nishi-browser-proofs/nishi_rgb.ppm\x00"
62 let ofd: i64 = sys_openat_wr(out_path, 0x1A4)
63 if ofd <= 0 { return 70 }
64 sys_write(ofd, hdr, hp)
65 sys_write(ofd, rgb, w * h * 3)
66 sys_close(ofd)
67
68 let pass: *u8 = sys_mmap(16)
69 pass[0]=0x50; pass[1]=0x41; pass[2]=0x53; pass[3]=0x53; pass[4]=0x0A
70 sys_write(1, pass, 5)
71 return 0
72}