nx_legacy_codec_gate.nx source
↩ module page · 289 lines · 11681 B
1// nx_legacy_codec_gate.nx -- gate for the 1990s reader set: TGA, PCX, ICO/CUR
2// and PNM, plus their chokepoint wiring.
3//
4// NO THIRD-PARTY FILE OF ANY OF THESE FORMATS EXISTS ANYWHERE UNDER knowledge/
5// (measured: a full extension census returned zero, and a magic scan over
6// 40,000 files found only our own containers). So the witness is a MATURE
7// INDEPENDENT ENCODER -- PIL/libtga/libpcx wrote every fixture in
8// knowledge/fixtures/legacy_codec, and the expected pixels below are that
9// encoder's OWN read-back, not our assumption about what it wrote. Same
10// standing as the BMP writer/reader round-trip: two independent
11// implementations agreeing byte-exactly.
12//
13// The probe image is 7x5 -- odd, and not a multiple of 8 -- so every stride
14// pad, plane split and row-order bug has somewhere to show. Its pixels vary
15// in BOTH axes, so a vertical flip, a horizontal flip and a transpose are all
16// separately detectable; a symmetric fixture would pass while mirrored.
17//
18// license_tier: ORIGINAL expect_exit: 0
19import "nx_syscalls.nx"
20import "nx_gate_verdict.nx"
21import "nx_tga_decode.nx"
22import "nx_pcx_decode.nx"
23import "nx_ico_decode.nx"
24import "nx_pnm_decode.nx"
25import "nx_img_bytes_to_rgb.nx"
26
27const LG_DIR: i64 = 0
28
29// check one pixel of a decoded RGB buffer
30func lg_px(rgb: *u8, w: i64, x: i64, y: i64, r: i64, g: i64, b: i64) -> i64 {
31 let o: i64 = (y*w + x) * 3
32 if ((rgb[o] as i64) & 255) != r { return 0 }
33 if ((rgb[o+1] as i64) & 255) != g { return 0 }
34 if ((rgb[o+2] as i64) & 255) != b { return 0 }
35 return 1
36}
37
38// read a fixture; returns bytes in box[0], pointer as the result
39func lg_read(name: *u8, box: *i64) -> *u8 {
40 return sys_read_file(name, box)
41}
42
43func main() -> i64 {
44 let ctr: *i64 = gv_ctr()
45 gv_head("nx_legacy_codec_gate -- 1990s roster: TGA/PCX/ICO/PNM + chokepoint" as *u8)
46
47 let box: *i64 = sys_mmap(16) as *i64
48 let wh: *i64 = sys_mmap(32) as *i64
49
50 // ---------- T1 TGA uncompressed 24-bit ----------
51 var t1: i64 = 0
52 let f1: *u8 = lg_read("knowledge/fixtures/legacy_codec/t_plain.tga" as *u8, box)
53 let n1: i64 = box[0]
54 var rgb1: *u8 = 0 as *u8
55 if f1 != (0 as *u8) {
56 wh[0]=0
57 wh[1]=0
58 rgb1 = tga_decode_rgb(f1, n1, wh)
59 if rgb1 != (0 as *u8) { if wh[0]==7 { if wh[1]==5 {
60 var ok: i64 = 1
61 if lg_px(rgb1,7,0,0,5,90,3)==0 { ok=0 }
62 if lg_px(rgb1,7,6,0,227,90,69)==0 { ok=0 }
63 if lg_px(rgb1,7,0,4,5,46,31)==0 { ok=0 }
64 if lg_px(rgb1,7,6,4,227,46,97)==0 { ok=0 }
65 if lg_px(rgb1,7,3,2,116,196,178)==0 { ok=0 }
66 if lg_px(rgb1,7,1,2,42,196,156)==0 { ok=0 }
67 t1 = ok
68 } } }
69 }
70 gv_check("T1 TGA 24-bit uncompressed: 6 pixels exact vs independent encoder" as *u8, t1, ctr)
71
72 // ---------- T2 TGA RLE == uncompressed (wire-different files) ----------
73 var t2: i64 = 0
74 let f2: *u8 = lg_read("knowledge/fixtures/legacy_codec/t_rle.tga" as *u8, box)
75 let n2: i64 = box[0]
76 if f2 != (0 as *u8) { if rgb1 != (0 as *u8) {
77 wh[0]=0
78 wh[1]=0
79 let rgb2: *u8 = tga_decode_rgb(f2, n2, wh)
80 if rgb2 != (0 as *u8) { if wh[0]==7 { if wh[1]==5 {
81 // vacuity guard: the two files MUST differ on the wire, else this
82 // check proves nothing about the RLE path at all
83 var differ: i64 = 0
84 if n1 != n2 { differ = 1 }
85 var same: i64 = 1
86 var i: i64 = 0
87 while i < 7*5*3 { if rgb1[i] != rgb2[i] { same = 0; i = 7*5*3 } else { i = i + 1 } }
88 if differ == 1 { if same == 1 { t2 = 1 } }
89 } } }
90 } }
91 gv_check("T2 TGA RLE decodes identical to uncompressed (wire-different)" as *u8, t2, ctr)
92
93 // ---------- T3 TGA 8-bit grayscale ----------
94 var t3: i64 = 0
95 let f3: *u8 = lg_read("knowledge/fixtures/legacy_codec/t_gray.tga" as *u8, box)
96 if f3 != (0 as *u8) {
97 wh[0]=0
98 wh[1]=0
99 let r3: *u8 = tga_decode_rgb(f3, box[0], wh)
100 if r3 != (0 as *u8) { if wh[0]==7 { if wh[1]==5 {
101 var ok: i64 = 1
102 if lg_px(r3,7,0,0,55,55,55)==0 { ok=0 }
103 if lg_px(r3,7,6,0,129,129,129)==0 { ok=0 }
104 if lg_px(r3,7,0,4,32,32,32)==0 { ok=0 }
105 if lg_px(r3,7,6,4,106,106,106)==0 { ok=0 }
106 if lg_px(r3,7,3,2,170,170,170)==0 { ok=0 }
107 t3 = ok
108 } } }
109 }
110 gv_check("T3 TGA 8-bit grayscale: 5 pixels exact" as *u8, t3, ctr)
111
112 // ---------- T4 PCX 24-bit planar ----------
113 var t4: i64 = 0
114 let f4: *u8 = lg_read("knowledge/fixtures/legacy_codec/p_rgb.pcx" as *u8, box)
115 if f4 != (0 as *u8) {
116 wh[0]=0
117 wh[1]=0
118 let r4: *u8 = pcx_decode_rgb(f4, box[0], wh)
119 if r4 != (0 as *u8) { if wh[0]==7 { if wh[1]==5 {
120 var ok: i64 = 1
121 if lg_px(r4,7,0,0,5,90,3)==0 { ok=0 }
122 if lg_px(r4,7,6,0,227,90,69)==0 { ok=0 }
123 if lg_px(r4,7,0,4,5,46,31)==0 { ok=0 }
124 if lg_px(r4,7,6,4,227,46,97)==0 { ok=0 }
125 if lg_px(r4,7,1,2,42,196,156)==0 { ok=0 }
126 t4 = ok
127 } } }
128 }
129 gv_check("T4 PCX 24-bit planar (plane split, not RGB triples)" as *u8, t4, ctr)
130
131 // ---------- T5 PCX 8-bit palette (trailing 769-byte palette) ----------
132 var t5: i64 = 0
133 let f5: *u8 = lg_read("knowledge/fixtures/legacy_codec/p_pal.pcx" as *u8, box)
134 if f5 != (0 as *u8) {
135 wh[0]=0
136 wh[1]=0
137 let r5: *u8 = pcx_decode_rgb(f5, box[0], wh)
138 if r5 != (0 as *u8) { if wh[0]==7 { if wh[1]==5 {
139 var ok: i64 = 1
140 if lg_px(r5,7,0,0,5,90,3)==0 { ok=0 }
141 if lg_px(r5,7,6,0,227,90,69)==0 { ok=0 }
142 if lg_px(r5,7,0,4,5,46,31)==0 { ok=0 }
143 if lg_px(r5,7,6,4,227,46,97)==0 { ok=0 }
144 t5 = ok
145 } } }
146 }
147 gv_check("T5 PCX 8-bit palette via trailing 0x0C palette block" as *u8, t5, ctr)
148
149 // ---------- T6 ICO: LARGEST frame chosen from a 16+32 multi-size file ----------
150 var t6: i64 = 0
151 let f6: *u8 = lg_read("knowledge/fixtures/legacy_codec/i_multi.ico" as *u8, box)
152 if f6 != (0 as *u8) {
153 wh[0]=0
154 wh[1]=0
155 let r6: *u8 = ico_decode_rgb(f6, box[0], wh)
156 if r6 != (0 as *u8) { if wh[0]==32 { if wh[1]==32 {
157 var ok: i64 = 1
158 if lg_px(r6,32,0,0,5,90,3)==0 { ok=0 }
159 if lg_px(r6,32,31,0,227,90,69)==0 { ok=0 }
160 if lg_px(r6,32,0,31,5,46,31)==0 { ok=0 }
161 if lg_px(r6,32,31,31,227,46,97)==0 { ok=0 }
162 if lg_px(r6,32,16,16,116,196,178)==0 { ok=0 }
163 t6 = ok
164 } } }
165 }
166 gv_check("T6 ICO picks the 32x32 frame over the 16x16 and decodes it" as *u8, t6, ctr)
167
168 // ---------- T7 PNM P6 binary colour ----------
169 var t7: i64 = 0
170 let f7: *u8 = lg_read("knowledge/fixtures/legacy_codec/n_p6.ppm" as *u8, box)
171 if f7 != (0 as *u8) {
172 wh[0]=0
173 wh[1]=0
174 let r7: *u8 = pnm_decode_rgb(f7, box[0], wh)
175 if r7 != (0 as *u8) { if wh[0]==7 { if wh[1]==5 {
176 var ok: i64 = 1
177 if lg_px(r7,7,0,0,5,90,3)==0 { ok=0 }
178 if lg_px(r7,7,6,0,227,90,69)==0 { ok=0 }
179 if lg_px(r7,7,6,4,227,46,97)==0 { ok=0 }
180 if lg_px(r7,7,1,2,42,196,156)==0 { ok=0 }
181 t7 = ok
182 } } }
183 }
184 gv_check("T7 PNM P6 binary colour (single-whitespace payload start)" as *u8, t7, ctr)
185
186 // ---------- T8 PNM P5 gray + P4 bitmap (1 = BLACK) ----------
187 var t8a: i64 = 0
188 let f8: *u8 = lg_read("knowledge/fixtures/legacy_codec/n_p5.pgm" as *u8, box)
189 if f8 != (0 as *u8) {
190 wh[0]=0
191 wh[1]=0
192 let r8: *u8 = pnm_decode_rgb(f8, box[0], wh)
193 if r8 != (0 as *u8) { if wh[0]==7 {
194 var ok: i64 = 1
195 if lg_px(r8,7,0,0,55,55,55)==0 { ok=0 }
196 if lg_px(r8,7,6,0,129,129,129)==0 { ok=0 }
197 if lg_px(r8,7,3,2,170,170,170)==0 { ok=0 }
198 t8a = ok
199 } }
200 }
201 var t8b: i64 = 0
202 let f9: *u8 = lg_read("knowledge/fixtures/legacy_codec/n_p4.pbm" as *u8, box)
203 if f9 != (0 as *u8) {
204 wh[0]=0
205 wh[1]=0
206 let r9: *u8 = pnm_decode_rgb(f9, box[0], wh)
207 if r9 != (0 as *u8) { if wh[0]==7 { if wh[1]==5 {
208 var ok: i64 = 1
209 if lg_px(r9,7,0,0,0,0,0)==0 { ok=0 }
210 if lg_px(r9,7,6,4,255,255,255)==0 { ok=0 }
211 if lg_px(r9,7,3,2,255,255,255)==0 { ok=0 }
212 if lg_px(r9,7,1,2,0,0,0)==0 { ok=0 }
213 t8b = ok
214 } } }
215 }
216 var t8: i64 = 0
217 if t8a == 1 { if t8b == 1 { t8 = 1 } }
218 gv_check("T8 PNM P5 gray and P4 bitmap (1 means black)" as *u8, t8, ctr)
219
220 // ---------- T9 BITE: truncated PCX refused, intact decodes ----------
221 var b9bad: i64 = 0
222 var b9good: i64 = 1
223 if f4 != (0 as *u8) {
224 wh[0]=0
225 wh[1]=0
226 if pcx_decode_rgb(f4, 140, wh) == (0 as *u8) { b9bad = 1 }
227 wh[0]=0
228 wh[1]=0
229 let g: *u8 = lg_read("knowledge/fixtures/legacy_codec/p_rgb.pcx" as *u8, box)
230 if pcx_decode_rgb(g, box[0], wh) != (0 as *u8) { b9good = 0 }
231 }
232 gv_bite("T9 truncated-PCX refusal" as *u8, b9bad, b9good, ctr)
233
234 // ---------- T10 BITE: TGA with an unknown image type refused ----------
235 var b10bad: i64 = 0
236 var b10good: i64 = 1
237 if f1 != (0 as *u8) {
238 let cp: *u8 = sys_mmap(n1 + 64)
239 var i: i64 = 0
240 while i < n1 { cp[i] = f1[i]; i = i + 1 }
241 cp[2] = 32 as u8 // type 32 = Huffman+delta, deliberately unsupported
242 wh[0]=0
243 wh[1]=0
244 if tga_decode_rgb(cp, n1, wh) == (0 as *u8) { b10bad = 1 }
245 wh[0]=0
246 wh[1]=0
247 if tga_decode_rgb(f1, n1, wh) != (0 as *u8) { b10good = 0 }
248 }
249 gv_bite("T10 unsupported-TGA-type refusal (never a wrong image)" as *u8, b10bad, b10good, ctr)
250
251 // ---------- T11 chokepoint inherits all four, BY MAGIC not extension ----------
252 var t11: i64 = 0
253 if f1 != (0 as *u8) {
254 var c: i64 = 0
255 wh[0]=0
256 wh[1]=0
257 if nx_img_bytes_to_rgb(f1, n1, wh) != (0 as *u8) { if wh[0]==7 { if wh[1]==5 { c = c + 1 } } }
258 let g4: *u8 = lg_read("knowledge/fixtures/legacy_codec/p_rgb.pcx" as *u8, box)
259 wh[0]=0
260 wh[1]=0
261 if nx_img_bytes_to_rgb(g4, box[0], wh) != (0 as *u8) { if wh[0]==7 { c = c + 1 } }
262 let g6: *u8 = lg_read("knowledge/fixtures/legacy_codec/i_multi.ico" as *u8, box)
263 wh[0]=0
264 wh[1]=0
265 if nx_img_bytes_to_rgb(g6, box[0], wh) != (0 as *u8) { if wh[0]==32 { c = c + 1 } }
266 let g7: *u8 = lg_read("knowledge/fixtures/legacy_codec/n_p6.ppm" as *u8, box)
267 wh[0]=0
268 wh[1]=0
269 if nx_img_bytes_to_rgb(g7, box[0], wh) != (0 as *u8) { if wh[0]==7 { c = c + 1 } }
270 if c == 4 { t11 = 1 }
271 }
272 gv_check("T11 chokepoint decodes TGA+PCX+ICO+PNM by magic sniff" as *u8, t11, ctr)
273
274 // ---------- T12 no REGRESSION: PNG still reaches the PNG fallback ----------
275 // The TGA sniff is magicless and runs LAST; this proves it did not swallow
276 // a format that must fall through to a later branch.
277 var t12: i64 = 0
278 let fp: *u8 = lg_read("knowledge/media/julia_kyoka/1e3abfbdd077fad1bbcbb2158887f76a1e913fa3fafb722ad28ccadba7fc796b.png" as *u8, box)
279 if fp != (0 as *u8) {
280 wh[0]=0
281 wh[1]=0
282 if nx_img_bytes_to_rgb(fp, box[0], wh) != (0 as *u8) { if wh[0]>0 { if wh[1]>0 { t12 = 1 } } }
283 }
284 gv_check("T12 PNG still decodes (magicless TGA sniff swallowed nothing)" as *u8, t12, ctr)
285
286 let rc: i64 = gv_verdict("LEGACY-CODEC-GATE" as *u8, ctr, "1990s roster: TGA/PCX/ICO/PNM, independent-encoder witnesses" as *u8)
287 sys_exit(rc)
288 return rc
289}