nx_img_place_test.nx source
↩ module page · 61 lines · 2860 B
1// nx_img_place_test.nx -- gate for image luminance + aspect-fit + scale-blit (no PNG dependency; uses a
2// synthetic in-memory decoded image so this gate stays independent of the decoder's own gates).
3// Unique exit codes per invariant:
4// 1x luminance: red->76, green->149, blue->28, white->255 (Rec.601 integer weights)
5// 2x grayscale (n_channels=1) passes the byte through
6// 3x aspect-fit: 200x100 into 100x100 -> 100x50 ; 100x200 into 100x100 -> 50x100
7// 4x nearest-neighbor 2x upscale-blit places each source pixel as a 2x2 block at the right offset
8// expect_exit: 0 license_tier: ORIGINAL
9
10import "nx_syscalls.nx"
11import "nx_img_place.nx"
12
13func t_puts(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 }
14
15func main() -> i64 {
16 // 2x2 RGB image: red, green / blue, white
17 let rgb: *u8 = sys_mmap(64)
18 rgb[0]=255 as u8; rgb[1]=0 as u8; rgb[2]=0 as u8 // red
19 rgb[3]=0 as u8; rgb[4]=255 as u8; rgb[5]=0 as u8 // green
20 rgb[6]=0 as u8; rgb[7]=0 as u8; rgb[8]=255 as u8 // blue
21 rgb[9]=255 as u8; rgb[10]=255 as u8; rgb[11]=255 as u8 // white
22 let g: *u8 = sys_mmap(16)
23 nx_img_to_gray(rgb, 2, 2, 3, 8, g)
24 if (g[0] as i64) != 76 { return 1 } // (255*77)>>8
25 if (g[1] as i64) != 149 { return 2 } // (255*150)>>8
26 if (g[2] as i64) != 28 { return 3 } // (255*29)>>8
27 if (g[3] as i64) != 255 { return 4 } // (255*256)>>8 clamped
28
29 // grayscale passthrough
30 let gr: *u8 = sys_mmap(8)
31 gr[0]=10 as u8; gr[1]=200 as u8
32 let g2: *u8 = sys_mmap(8)
33 nx_img_to_gray(gr, 2, 1, 1, 8, g2)
34 if (g2[0] as i64) != 10 { return 10 }
35 if (g2[1] as i64) != 200 { return 11 }
36
37 // aspect-fit
38 let ow: *i64 = sys_mmap(8) as *i64
39 let oh: *i64 = sys_mmap(8) as *i64
40 nx_img_fit(200, 100, 100, 100, ow, oh)
41 if ow[0] != 100 { return 20 }
42 if oh[0] != 50 { return 21 }
43 nx_img_fit(100, 200, 100, 100, ow, oh)
44 if ow[0] != 50 { return 22 }
45 if oh[0] != 100 { return 23 }
46
47 // nearest-neighbor 2x upscale: place the 2x2 gray into a 4x4 page at (0,0), box 4x4
48 // gray image = [76,149 / 28,255]
49 let page: *u8 = sys_mmap(64)
50 var i: i64 = 0
51 while i < 16 { page[i] = 0 as u8; i = i + 1 }
52 nx_img_blit(page, 4, 4, 0, 0, g, 2, 2, 4, 4)
53 // cols0,1 = src col0; cols2,3 = src col1; rows0,1 = src row0; rows2,3 = src row1
54 if (page[0 * 4 + 0] as i64) != 76 { return 30 } // top-left block = red lum
55 if (page[0 * 4 + 3] as i64) != 149 { return 31 } // top-right block = green lum
56 if (page[3 * 4 + 0] as i64) != 28 { return 32 } // bottom-left = blue lum
57 if (page[3 * 4 + 3] as i64) != 255 { return 33 } // bottom-right = white
58
59 t_puts("nx_img_place: 4/4 KAT PASS (luminance + gray-passthrough + aspect-fit + nearest-neighbor blit)\n")
60 return 0
61}