nx_scene_through_aperture.nx source
↩ module page · 232 lines · 9171 B
1// nx_scene_through_aperture.nx -- Tier 6 background-coherence detector.
2//
3// User feedback 2026-05-16: "the car isnt real and the background
4// is a mess". AI image-gen frequently fails the SCENE-THROUGH-
5// FRAMED-APERTURE check: when looking through a window or door,
6// the content visible beyond should follow physical rules:
7// - sharper foreground, blurrier background (depth-of-field)
8// - cooler / hazier colors (atmospheric perspective)
9// - consistent perspective (vanishing lines align with frame)
10// - recognizable objects, not arbitrary blobs (edge-density
11// distribution typical of real scenes)
12//
13// AI puts random fragments through windows because the gen model
14// has no scene-coherence prior beyond local pixel statistics.
15// Substrate's answer: measure the four physics-grounded
16// signatures and emit a sealed verdict.
17//
18// Inputs: caller pre-extracts the APERTURE region (window/door
19// bounding box content) and the IMMEDIATE FOREGROUND region
20// (interior wall / dashboard / face beside window). Computes:
21//
22// sharpness_ratio_q10 = sharpness(aperture) / sharpness(foreground)
23// color_temp_shift = mean(aperture_b - aperture_r) - mean(foreground_b - foreground_r)
24// edge_density_q10 = edge_count(aperture) / aperture_pixels
25// edge_uniformity_q10 = std(local_edge_density) / mean(local_edge_density)
26//
27// Physics expectations for real-photo background:
28// sharpness_ratio in (0.2, 0.8) -- background blurrier
29// color_temp_shift > 0 -- background bluer (atmospheric)
30// edge_density in (0.05, 0.30) -- some structure, not flat, not noise
31// edge_uniformity > 0.4 -- non-uniform distribution
32//
33// Sealed verdict:
34// COHERENT -- all 4 checks pass
35// DEPTH_FAIL -- sharpness ratio outside physical range
36// ATMOSPHERIC_FAIL -- no color-temp shift
37// FRAGMENTED -- edge metrics outside real-scene distribution
38// IMPLAUSIBLE -- 3+ checks fail (likely AI background)
39//
40// genealogy_id: substrate_scene_through_aperture_2026_05_16
41// lineage_id: tier_6_multi_element_coherence_v1
42
43// nx_safety_envelope:
44// intended_use: AUTO_APPLIED -- primitive-specific tuning queued
45// sil_target: SIL1
46// evidence: [bulk_applied_2026-05-16, see-file-comment-for-detail]
47// verdict: NOT_YET_EVALUATED
48
49import "nx_syscalls.nx"
50import "nx_runtime.nx"
51import "nx_tier.nx"
52
53const NX_STA_Q10: nx_int = 1024
54
55// Sealed verdict IDs.
56const NX_STA_COHERENT: nx_int = 0
57const NX_STA_DEPTH_FAIL: nx_int = 1
58const NX_STA_ATMOSPHERIC_FAIL: nx_int = 2
59const NX_STA_FRAGMENTED: nx_int = 3
60const NX_STA_IMPLAUSIBLE: nx_int = 4
61
62// Physical-range thresholds (Q10).
63const NX_STA_SHARP_MIN_Q10: nx_int = 205 // 0.20
64const NX_STA_SHARP_MAX_Q10: nx_int = 819 // 0.80
65const NX_STA_COLOR_MIN_SHIFT: nx_int = 0 // any non-negative shift acceptable; deep negative = fail
66const NX_STA_EDGE_DENS_MIN_Q10: nx_int = 51 // 0.05
67const NX_STA_EDGE_DENS_MAX_Q10: nx_int = 307 // 0.30
68const NX_STA_EDGE_UNIFORM_MIN_Q10: nx_int = 410 // 0.40
69
70// ===== result struct ==============================================
71
72struct NxApertureResult {
73 sharpness_ratio_q10: nx_int,
74 color_temp_shift: nx_int,
75 edge_density_q10: nx_int,
76 edge_uniformity_q10: nx_int,
77 n_checks_failed: nx_int,
78 check_fail_mask: nx_int,
79 verdict: nx_int,
80}
81
82const NX_STA_RESULT_BYTES: nx_size = 64
83
84// Bit positions in fail mask.
85const NX_STA_BIT_SHARPNESS: nx_int = 0
86const NX_STA_BIT_COLOR: nx_int = 1
87const NX_STA_BIT_EDGE_DENSITY: nx_int = 2
88const NX_STA_BIT_EDGE_UNIFORM: nx_int = 3
89
90// ===== classify ===================================================
91//
92// Caller computes the four scalar measurements upstream (compose
93// against nx_features for sharpness + nx_color for color shift +
94// nx_edge_detect for edge density). This primitive does the
95// physics-rules classification.
96
97func nx_aperture_classify(
98 sharpness_ratio_q10: nx_int,
99 color_temp_shift: nx_int,
100 edge_density_q10: nx_int,
101 edge_uniformity_q10: nx_int) -> *NxApertureResult {
102
103 let r_ptr: *u8 = sys_mmap(NX_STA_RESULT_BYTES)
104 let r: *NxApertureResult = r_ptr as *NxApertureResult
105
106 var fail_mask: nx_int = 0
107 var n_failed: nx_int = 0
108
109 // Sharpness ratio test.
110 var sharp_ok: nx_int = 1
111 if sharpness_ratio_q10 < NX_STA_SHARP_MIN_Q10 { sharp_ok = 0 }
112 if sharpness_ratio_q10 > NX_STA_SHARP_MAX_Q10 { sharp_ok = 0 }
113 if sharp_ok == 0 {
114 let bit: nx_int = 1 << NX_STA_BIT_SHARPNESS
115 fail_mask = fail_mask | bit
116 n_failed = n_failed + 1
117 }
118
119 // Atmospheric color-shift test.
120 if color_temp_shift < NX_STA_COLOR_MIN_SHIFT {
121 let bit: nx_int = 1 << NX_STA_BIT_COLOR
122 fail_mask = fail_mask | bit
123 n_failed = n_failed + 1
124 }
125
126 // Edge density test.
127 var dens_ok: nx_int = 1
128 if edge_density_q10 < NX_STA_EDGE_DENS_MIN_Q10 { dens_ok = 0 }
129 if edge_density_q10 > NX_STA_EDGE_DENS_MAX_Q10 { dens_ok = 0 }
130 if dens_ok == 0 {
131 let bit: nx_int = 1 << NX_STA_BIT_EDGE_DENSITY
132 fail_mask = fail_mask | bit
133 n_failed = n_failed + 1
134 }
135
136 // Edge uniformity test.
137 if edge_uniformity_q10 < NX_STA_EDGE_UNIFORM_MIN_Q10 {
138 let bit: nx_int = 1 << NX_STA_BIT_EDGE_UNIFORM
139 fail_mask = fail_mask | bit
140 n_failed = n_failed + 1
141 }
142
143 r.sharpness_ratio_q10 = sharpness_ratio_q10
144 r.color_temp_shift = color_temp_shift
145 r.edge_density_q10 = edge_density_q10
146 r.edge_uniformity_q10 = edge_uniformity_q10
147 r.n_checks_failed = n_failed
148 r.check_fail_mask = fail_mask
149
150 // Aggregate verdict.
151 var verdict: nx_int = NX_STA_COHERENT
152 if n_failed >= 3 {
153 verdict = NX_STA_IMPLAUSIBLE
154 } else {
155 if n_failed == 0 {
156 verdict = NX_STA_COHERENT
157 } else {
158 // 1-2 failures: classify by WHICH bit.
159 let shift_bit: nx_int = 1 << NX_STA_BIT_SHARPNESS
160 let color_bit: nx_int = 1 << NX_STA_BIT_COLOR
161 let dens_bit: nx_int = 1 << NX_STA_BIT_EDGE_DENSITY
162 let uniform_bit: nx_int = 1 << NX_STA_BIT_EDGE_UNIFORM
163 let edge_bits: nx_int = dens_bit | uniform_bit
164 if (fail_mask & shift_bit) != 0 {
165 verdict = NX_STA_DEPTH_FAIL
166 } else {
167 if (fail_mask & color_bit) != 0 {
168 verdict = NX_STA_ATMOSPHERIC_FAIL
169 } else {
170 if (fail_mask & edge_bits) != 0 {
171 verdict = NX_STA_FRAGMENTED
172 }
173 }
174 }
175 }
176 }
177 r.verdict = verdict
178 return r
179}
180
181// ===== self-test ==================================================
182
183func main() -> nx_int {
184 // ---- COHERENT: real-photo background ----
185 //
186 // sharpness_ratio = 0.5 (background half as sharp as foreground)
187 // color_temp_shift = 5 (slightly cooler / bluer)
188 // edge_density = 0.15
189 // edge_uniformity = 0.6 (non-uniform)
190 let r_ok: *NxApertureResult = nx_aperture_classify(512, 5, 154, 614)
191 if r_ok.verdict != NX_STA_COHERENT { return 1 }
192 if r_ok.n_checks_failed != 0 { return 2 }
193
194 // ---- DEPTH_FAIL: background as sharp as foreground (ratio 0.95) ----
195 let r_depth: *NxApertureResult = nx_aperture_classify(972, 5, 154, 614)
196 if r_depth.verdict != NX_STA_DEPTH_FAIL { return 10 }
197 if r_depth.n_checks_failed != 1 { return 11 }
198
199 // ---- ATMOSPHERIC_FAIL: background warmer than foreground (impossible) ----
200 let r_atmos: *NxApertureResult = nx_aperture_classify(512, -20, 154, 614)
201 if r_atmos.verdict != NX_STA_ATMOSPHERIC_FAIL { return 20 }
202
203 // ---- FRAGMENTED: edge density too low (flat / uniform) ----
204 let r_flat: *NxApertureResult = nx_aperture_classify(512, 5, 20, 614)
205 if r_flat.verdict != NX_STA_FRAGMENTED { return 30 }
206
207 // ---- FRAGMENTED: edge density too high (random noise) ----
208 let r_noisy: *NxApertureResult = nx_aperture_classify(512, 5, 400, 614)
209 if r_noisy.verdict != NX_STA_FRAGMENTED { return 31 }
210
211 // ---- FRAGMENTED: edge uniformity too low (suspiciously uniform) ----
212 let r_uniform: *NxApertureResult = nx_aperture_classify(512, 5, 154, 200)
213 if r_uniform.verdict != NX_STA_FRAGMENTED { return 32 }
214
215 // ---- IMPLAUSIBLE: 3 checks fail (the AI background-mess case) ----
216 //
217 // sharpness too high (AI keeps background sharp) +
218 // color-shift wrong (no atmospheric) + edge metrics bad.
219 let r_ai: *NxApertureResult = nx_aperture_classify(950, -10, 400, 200)
220 if r_ai.verdict != NX_STA_IMPLAUSIBLE { return 40 }
221 if r_ai.n_checks_failed < 3 { return 41 }
222
223 // ---- check_fail_mask bit positions ----
224 // r_depth should have only bit 0 set.
225 let exp_depth: nx_int = 1 << NX_STA_BIT_SHARPNESS
226 if r_depth.check_fail_mask != exp_depth { return 50 }
227 // r_atmos should have only bit 1 set.
228 let exp_atmos: nx_int = 1 << NX_STA_BIT_COLOR
229 if r_atmos.check_fail_mask != exp_atmos { return 51 }
230
231 return 0
232}