code wiki / _hdl_build / nx_webp_mapdump.nx
nx_webp_mapdump.nx source
↩ module page · 111 lines · 4841 B
1// nx_webp_mapdump.nx -- decisive instrument: walk the fixture's transform list and print BOTH
2// 5x5 tile maps pixel-by-pixel (a,r,g,b per pixel), plus the bit-reader byte/bit cursor after
3// each sub-image. The maps' first rows produce correct main-image tiles and later rows do not;
4// the raw values name the failing mechanism. license_tier: ORIGINAL expect_exit: 0
5import "nx_syscalls.nx"
6import "nx_webp_decode.nx"
7import "nx_webp_vp8l.nx"
8
9static md_pmap: i64
10static md_ccmap: i64
11
12func pw(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
13
14func wr_plane(path: *u8, argb: *i64) -> i64 {
15 let ob: *u8 = sys_mmap(40 * 40 * 4 + 16)
16 var i: i64 = 0
17 while i < 1600 {
18 let v: i64 = argb[i]
19 ob[i*4] = (v & 255) as u8
20 ob[i*4+1] = ((v / 256) & 255) as u8
21 ob[i*4+2] = ((v / 65536) & 255) as u8
22 ob[i*4+3] = ((v / 16777216) & 255) as u8
23 i = i + 1
24 }
25 let fd: i64 = sys_openat_wr(path, 420)
26 if fd < 0 { return 0 - 1 }
27 sys_write(fd, ob, 6400)
28 sys_close(fd)
29 return 0
30}
31func pn(v: i64) -> i64 { let b: *u8=sys_mmap(28); var m: i64=v; if m<0{pw("-\x00" as *u8);m=0-m} let t: *u8=sys_mmap(28); var k: i64=0; if m==0{t[0]=48 as u8;k=1} while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1} var i: i64=0; while i<k{b[i]=t[k-1-i];i=i+1} sys_write(1,b,k); return 0 }
32
33func dump_map(tag: *u8, m: *i64) -> i64 {
34 pw(tag)
35 var y: i64 = 0
36 while y < 5 {
37 pw("\n row \x00" as *u8); pn(y); pw(":\x00" as *u8)
38 var x: i64 = 0
39 while x < 5 {
40 let v: i64 = m[y * 5 + x]
41 pw(" (\x00" as *u8); pn((v / 16777216) & 255)
42 pw(",\x00" as *u8); pn((v / 65536) & 255)
43 pw(",\x00" as *u8); pn((v / 256) & 255)
44 pw(",\x00" as *u8); pn(v & 255)
45 pw(")\x00" as *u8)
46 x = x + 1
47 }
48 y = y + 1
49 }
50 pw("\n\x00" as *u8)
51 return 0
52}
53
54func main() -> i64 {
55 let lp: *i64 = sys_mmap(16) as *i64
56 let wb: *u8 = sys_read_file("_build/kat_vp8l.webp\x00" as *u8, lp)
57 if (wb as i64) == 0 { sys_exit(2) }
58 let st: *i64 = sys_mmap(8 * NX_BR_BYTES) as *i64
59 nx_br_init(st, wb, lp[0], 20)
60 nx_br_bits(st, 8)
61 nx_br_bits(st, 14)
62 nx_br_bits(st, 14)
63 nx_br_bits(st, 1)
64 nx_br_bits(st, 3)
65 var scan: i64 = 1
66 var ntx: i64 = 0
67 while scan == 1 {
68 if nx_br_eof(st) == 1 { scan = 0 }
69 else {
70 if nx_br_bit(st) == 0 { scan = 0 }
71 else {
72 let t: i64 = nx_br_bits(st, 2)
73 pw("transform t=\x00" as *u8); pn(t)
74 if t < 2 {
75 let tb: i64 = nx_br_bits(st, 3) + 2
76 pw(" bits=\x00" as *u8); pn(tb)
77 pw("\n\x00" as *u8)
78 let sub: *i64 = _vp8l_sub_image(st, 5, 5)
79 if (sub as i64) == 0 { pw("SUB-NULL\n\x00" as *u8); sys_exit(3) }
80 if t == 0 { dump_map("PREDICTOR MAP (mode = g&15):\x00" as *u8, sub); md_pmap = sub as i64 }
81 if t == 1 { dump_map("CROSS-COLOUR MAP (r2b=r g2b=g g2r=b):\x00" as *u8, sub); md_ccmap = sub as i64 }
82 } else { pw("\n\x00" as *u8) }
83 ntx = ntx + 1
84 if ntx > 4 { scan = 0 }
85 }
86 }
87 }
88 // continue the driver sequence inline: cache bits + entropy flag + groups + MAIN pixel loop,
89 // then dump the PRE-TRANSFORM argb plane (le32) for bisection against the python reference.
90 var cb: i64 = 0
91 if nx_br_bit(st) == 1 { cb = nx_br_bits(st, 4) }
92 let ent: i64 = nx_br_bit(st)
93 pw("main cache=\x00" as *u8); pn(cb)
94 pw(" ent=\x00" as *u8); pn(ent)
95 if ent == 1 { pw(" (entropy image unsupported in this probe)\n\x00" as *u8); sys_exit(4) }
96 let tables: *i64 = sys_mmap(8 * VP8L_TBL_SLOTS * 5) as *i64
97 if nx_vp8l_read_groups(st, 1, cb, tables) < 0 { pw(" GROUPS-FAIL\n\x00" as *u8); sys_exit(5) }
98 let argb: *i64 = sys_mmap(8 * 40 * 40) as *i64
99 if nx_vp8l_decode_pixels(st, 40, 40, argb, tables, 0 as *i64, 0, 0, cb) < 0 { pw(" PIXELS-FAIL\n\x00" as *u8); sys_exit(6) }
100 if wr_plane("_build/our_pretransform.bin\x00" as *u8, argb) < 0 { sys_exit(7) }
101 // apply OUR inverse transforms stage by stage, dumping each plane for the stage bisect
102 if md_ccmap != 0 { nx_vp8l_inv_cross_colour(argb, 40, 40, md_ccmap as *i64, 3, 5) }
103 if wr_plane("_build/our_post_t1.bin\x00" as *u8, argb) < 0 { sys_exit(7) }
104 if md_pmap != 0 { nx_vp8l_inv_predict(argb, 40, 40, md_pmap as *i64, 3, 5) }
105 if wr_plane("_build/our_post_t0.bin\x00" as *u8, argb) < 0 { sys_exit(7) }
106 nx_vp8l_inv_subtract_green(argb, 1600)
107 if wr_plane("_build/our_post_t2.bin\x00" as *u8, argb) < 0 { sys_exit(7) }
108 pw(" wrote pretransform + post_t1/t0/t2 planes\n\x00" as *u8)
109 sys_exit(0)
110 return 0
111}