nx_salience_test.nx source
↩ module page · 110 lines · 3904 B
1// nx_salience_test.nx -- smoke for V8 salience primitives.
2
3import "syscalls.nx"
4import "nx_image.nx"
5import "nx_salience.nx"
6
7func main() -> i64 {
8 // === Test 1: 2x upsample ===
9 let small: *Image = nx_image_alloc(2, 2, 1)
10 nx_image_set(small, 0, 0, 0, 10)
11 nx_image_set(small, 1, 0, 0, 20)
12 nx_image_set(small, 0, 1, 0, 30)
13 nx_image_set(small, 1, 1, 0, 40)
14 let big: *Image = nx_salience_upsample_2x(small)
15 if big.width != 4 { return 1 }
16 if big.height != 4 { return 2 }
17 // Each 2x2 block should be the source value.
18 if nx_image_get(big, 0, 0, 0) != 10 { return 3 }
19 if nx_image_get(big, 1, 0, 0) != 10 { return 4 }
20 if nx_image_get(big, 0, 1, 0) != 10 { return 5 }
21 if nx_image_get(big, 1, 1, 0) != 10 { return 6 }
22 if nx_image_get(big, 2, 2, 0) != 40 { return 7 }
23 if nx_image_get(big, 3, 3, 0) != 40 { return 8 }
24
25 // === Test 2: abs difference ===
26 let a: *Image = nx_image_alloc(8, 8, 1)
27 let b: *Image = nx_image_alloc(8, 8, 1)
28 var y: i64 = 0
29 while y < 8 {
30 var x: i64 = 0
31 while x < 8 {
32 nx_image_set(a, x, y, 0, 100)
33 nx_image_set(b, x, y, 0, 80)
34 x = x + 1
35 }
36 y = y + 1
37 }
38 let d: *Image = nx_salience_abs_diff(a, b)
39 if nx_image_get(d, 3, 3, 0) != 20 { return 10 }
40
41 // === Test 3: intensity salience on uniform image is flat ~ 0 ===
42 let uni: *Image = nx_image_alloc(32, 32, 1)
43 var y2: i64 = 0
44 while y2 < 32 {
45 var x2: i64 = 0
46 while x2 < 32 {
47 nx_image_set(uni, x2, y2, 0, 100)
48 x2 = x2 + 1
49 }
50 y2 = y2 + 1
51 }
52 let sal_uni: *Image = nx_salience_intensity(uni)
53 // Interior pixel should be 0 (no center-surround difference).
54 if nx_image_get(sal_uni, 16, 16, 0) > 5 { return 20 }
55 // Flatness should be high (mean/max ratio near 1).
56 let flat_uni: i64 = nx_salience_flatness(sal_uni)
57 if flat_uni < 800 { return 21 } // uniform -> very flat
58
59 // === Test 4: intensity salience on image with bright blob ===
60 let img: *Image = nx_image_alloc(32, 32, 1)
61 var y3: i64 = 0
62 while y3 < 32 {
63 var x3: i64 = 0
64 while x3 < 32 {
65 nx_image_set(img, x3, y3, 0, 50)
66 x3 = x3 + 1
67 }
68 y3 = y3 + 1
69 }
70 // Bright 6x6 blob at (16, 16)
71 var yb: i64 = 13
72 while yb < 19 {
73 var xb: i64 = 13
74 while xb < 19 {
75 nx_image_set(img, xb, yb, 0, 250)
76 xb = xb + 1
77 }
78 yb = yb + 1
79 }
80 let sal: *Image = nx_salience_intensity(img)
81
82 // === Test 5: salience peak located near the blob ===
83 let px: *i64 = (sys_mmap(8)) as *i64
84 let py: *i64 = (sys_mmap(8)) as *i64
85 let peak_v: i64 = nx_salience_peak(sal, px, py)
86 if peak_v <= 0 { return 30 }
87 // Peak should be within 6 pixels of blob center (16, 16).
88 let dx: i64 = px[0] - 16
89 let dy: i64 = py[0] - 16
90 let d2: i64 = dx * dx + dy * dy
91 if d2 > 36 { return 31 }
92
93 // === Test 6: flatness on blob image is LESS than on uniform ===
94 let flat_blob: i64 = nx_salience_flatness(sal)
95 if flat_blob >= flat_uni { return 40 } // blob less flat than uniform
96
97 // === Test 7: combined salience (intensity + edge) responds at blob ===
98 let csal: *Image = nx_salience_combined(img)
99 let px2: *i64 = (sys_mmap(8)) as *i64
100 let py2: *i64 = (sys_mmap(8)) as *i64
101 let peak_v2: i64 = nx_salience_peak(csal, px2, py2)
102 if peak_v2 <= 0 { return 50 }
103 // Combined peak should still be near the blob (edge + intensity both elevated).
104 let dx2: i64 = px2[0] - 16
105 let dy2: i64 = py2[0] - 16
106 let d22: i64 = dx2 * dx2 + dy2 * dy2
107 if d22 > 49 { return 51 } // within 7px
108
109 return 0
110}