nx_img_scale_gate.nx source
↩ module page · 80 lines · 2630 B
1// nx_img_scale_gate.nx -- KAT gate for the sovereign bilinear resampler.
2//
3// Known-answer tests hand-computed from the standard half-pixel bilinear
4// formula (the independent oracle): identity, 1D down, 1D up, 2D down,
5// bad-args, and N-channel + 1x1 broadcast. Returns 0 iff every pixel matches;
6// a nonzero code pinpoints the first failure.
7
8import "nx_syscalls.nx"
9import "nx_img_scale.nx"
10
11func main() -> i64 {
12 // ---- Test 1: identity 3x2x1 -> byte-exact copy (scale=1 => frac=0) ----
13 let s1: *u8 = sys_mmap(16)
14 s1[0] = 10 as u8
15 s1[1] = 20 as u8
16 s1[2] = 30 as u8
17 s1[3] = 40 as u8
18 s1[4] = 50 as u8
19 s1[5] = 60 as u8
20 let d1: *u8 = sys_mmap(16)
21 if nx_img_scale_bilinear(s1, 3, 2, 1, d1, 3, 2) != 0 { return 1 }
22 var i: i64 = 0
23 while i < 6 {
24 if d1[i] != s1[i] { return 2 }
25 i = i + 1
26 }
27
28 // ---- Test 2: 1D DOWN-scale [0,60,120,180] 4->2 = [30,150] ----
29 let s2: *u8 = sys_mmap(16)
30 s2[0] = 0 as u8
31 s2[1] = 60 as u8
32 s2[2] = 120 as u8
33 s2[3] = 180 as u8
34 let d2: *u8 = sys_mmap(16)
35 if nx_img_scale_bilinear(s2, 4, 1, 1, d2, 2, 1) != 0 { return 3 }
36 if d2[0] != (30 as u8) { return 4 }
37 if d2[1] != (150 as u8) { return 5 }
38
39 // ---- Test 3: 1D UP-scale [0,100] 2->4 = [0,25,75,100] ----
40 let s3: *u8 = sys_mmap(16)
41 s3[0] = 0 as u8
42 s3[1] = 100 as u8
43 let d3: *u8 = sys_mmap(16)
44 if nx_img_scale_bilinear(s3, 2, 1, 1, d3, 4, 1) != 0 { return 6 }
45 if d3[0] != (0 as u8) { return 7 }
46 if d3[1] != (25 as u8) { return 8 }
47 if d3[2] != (75 as u8) { return 9 }
48 if d3[3] != (100 as u8) { return 10 }
49
50 // ---- Test 4: 2D DOWN-scale [[0,100],[100,200]] 2x2->1x1 = [100] ----
51 let s4: *u8 = sys_mmap(16)
52 s4[0] = 0 as u8
53 s4[1] = 100 as u8
54 s4[2] = 100 as u8
55 s4[3] = 200 as u8
56 let d4: *u8 = sys_mmap(16)
57 if nx_img_scale_bilinear(s4, 2, 2, 1, d4, 1, 1) != 0 { return 11 }
58 if d4[0] != (100 as u8) { return 12 }
59
60 // ---- Test 5: bad args (dw=0) -> -1 ----
61 let d5: *u8 = sys_mmap(16)
62 if nx_img_scale_bilinear(s4, 2, 2, 1, d5, 0, 1) != -1 { return 13 }
63
64 // ---- Test 6: N-channel + 1x1 broadcast: [11,22,33] 1x1x3 -> 2x2x3 ----
65 let s6: *u8 = sys_mmap(16)
66 s6[0] = 11 as u8
67 s6[1] = 22 as u8
68 s6[2] = 33 as u8
69 let d6: *u8 = sys_mmap(32)
70 if nx_img_scale_bilinear(s6, 1, 1, 3, d6, 2, 2) != 0 { return 14 }
71 var j: i64 = 0
72 while j < 4 {
73 if d6[j * 3 + 0] != (11 as u8) { return 15 }
74 if d6[j * 3 + 1] != (22 as u8) { return 16 }
75 if d6[j * 3 + 2] != (33 as u8) { return 17 }
76 j = j + 1
77 }
78
79 return 0
80}