code wiki / (root) / nx_compose_test.nx

nx_compose_test.nx source

↩ module page · 159 lines · 5147 B

1// nx_compose_test.nx -- smoke for V2 composition primitives. 2 3import "syscalls.nx" 4import "nx_image.nx" 5import "nx_compose.nx" 6 7func main() -> i64 { 8 // === Test 1: rule-of-thirds on uniform image === 9 // Uniform image: mass evenly spread. Bands cover ~4/9 of area 10 // (two W/9 vertical bands + two H/9 horizontal bands minus 11 // overlap). Expected ~ 444..556 Q10 range. 12 let img1: *Image = nx_image_alloc(18, 18, 1) 13 var y: i64 = 0 14 while y < 18 { 15 var x: i64 = 0 16 while x < 18 { 17 nx_image_set(img1, x, y, 0, 100) 18 x = x + 1 19 } 20 y = y + 1 21 } 22 let s1: i64 = nx_compose_thirds_score(img1) 23 if s1 < 300 { return 1 } 24 if s1 > 700 { return 2 } 25 26 // === Test 2: rule-of-thirds on image with subject ON thirds === 27 // Place a bright 4x4 blob centered at (6, 6) which is W/3=6, H/3=6. 28 let img2: *Image = nx_image_alloc(18, 18, 1) 29 // Background dark 30 var y2: i64 = 0 31 while y2 < 18 { 32 var x2: i64 = 0 33 while x2 < 18 { 34 nx_image_set(img2, x2, y2, 0, 10) 35 x2 = x2 + 1 36 } 37 y2 = y2 + 1 38 } 39 // Bright square at thirds intersection (6, 6) 40 var yb: i64 = 4 41 while yb < 8 { 42 var xb: i64 = 4 43 while xb < 8 { 44 nx_image_set(img2, xb, yb, 0, 250) 45 xb = xb + 1 46 } 47 yb = yb + 1 48 } 49 let s2: i64 = nx_compose_thirds_score(img2) 50 // Subject on thirds should score higher than uniform baseline. 51 if s2 <= s1 { return 10 } 52 53 // === Test 3: rule-of-thirds on image with subject OFF thirds === 54 // Place bright blob at center (9, 9) -- not on thirds. 55 let img3: *Image = nx_image_alloc(18, 18, 1) 56 var y3: i64 = 0 57 while y3 < 18 { 58 var x3: i64 = 0 59 while x3 < 18 { 60 nx_image_set(img3, x3, y3, 0, 10) 61 x3 = x3 + 1 62 } 63 y3 = y3 + 1 64 } 65 var yc: i64 = 7 66 while yc < 11 { 67 var xc: i64 = 7 68 while xc < 11 { 69 nx_image_set(img3, xc, yc, 0, 250) 70 xc = xc + 1 71 } 72 yc = yc + 1 73 } 74 let s3: i64 = nx_compose_thirds_score(img3) 75 // Subject in center should score lower than subject on thirds. 76 if s3 >= s2 { return 11 } 77 78 // === Test 4: horizontal symmetry on a SYMMETRIC image === 79 // Mirror about center vertical line. 80 let img4: *Image = nx_image_alloc(16, 16, 1) 81 var y4: i64 = 0 82 while y4 < 16 { 83 var x4: i64 = 0 84 while x4 < 16 { 85 // Symmetric pattern: bright stripes at x=2 and x=13 (mirror) 86 var v: i64 = 0 87 if x4 == 2 { v = 255 } 88 if x4 == 13 { v = 255 } 89 nx_image_set(img4, x4, y4, 0, v) 90 x4 = x4 + 1 91 } 92 y4 = y4 + 1 93 } 94 let h_sym4: i64 = nx_compose_h_symmetry(img4) 95 if h_sym4 < 900 { return 20 } // near 1024 = perfect 96 97 // === Test 5: horizontal asymmetry on a CLEARLY asymmetric image === 98 // Only left half has content. 99 let img5: *Image = nx_image_alloc(16, 16, 1) 100 var y5: i64 = 0 101 while y5 < 16 { 102 var x5: i64 = 0 103 while x5 < 16 { 104 var v: i64 = 0 105 if x5 < 8 { v = 200 } 106 nx_image_set(img5, x5, y5, 0, v) 107 x5 = x5 + 1 108 } 109 y5 = y5 + 1 110 } 111 let h_sym5: i64 = nx_compose_h_symmetry(img5) 112 if h_sym5 > 100 { return 21 } // near 0 = anti-symmetric 113 114 // === Test 6: vertical symmetry === 115 // Top half bright, bottom half dark: low v-symmetry 116 let img6: *Image = nx_image_alloc(16, 16, 1) 117 var y6: i64 = 0 118 while y6 < 16 { 119 var x6: i64 = 0 120 while x6 < 16 { 121 var v: i64 = 0 122 if y6 < 8 { v = 200 } 123 nx_image_set(img6, x6, y6, 0, v) 124 x6 = x6 + 1 125 } 126 y6 = y6 + 1 127 } 128 let v_sym6: i64 = nx_compose_v_symmetry(img6) 129 if v_sym6 > 100 { return 30 } 130 131 // === Test 7: visual centroid on centered bright blob === 132 let img7: *Image = nx_image_alloc(16, 16, 1) 133 nx_image_set(img7, 8, 8, 0, 255) 134 let cx_q: *i64 = (sys_mmap(8)) as *i64 135 let cy_q: *i64 = (sys_mmap(8)) as *i64 136 nx_compose_visual_centroid(img7, cx_q, cy_q) 137 // 8/16 = 0.5 = 512 Q10 138 if cx_q[0] < 480 { return 40 } 139 if cx_q[0] > 544 { return 41 } 140 if cy_q[0] < 480 { return 42 } 141 if cy_q[0] > 544 { return 43 } 142 143 // === Test 8: visual centroid skewed left === 144 let img8: *Image = nx_image_alloc(16, 16, 1) 145 nx_image_set(img8, 2, 8, 0, 255) 146 nx_compose_visual_centroid(img8, cx_q, cy_q) 147 if cx_q[0] > 200 { return 50 } // 2/16 = 128 Q10 148 if cy_q[0] < 480 { return 51 } 149 150 // === Test 9: balance score === 151 // Centered blob -> low balance score (near 0) 152 let b7: i64 = nx_compose_balance_score(img7) 153 if b7 > 80 { return 60 } 154 // Off-center blob -> higher balance score 155 let b8: i64 = nx_compose_balance_score(img8) 156 if b8 <= b7 { return 61 } 157 158 return 0 159}