nx_jpeg_sof_test.nx source
↩ module page · 160 lines · 6958 B
1// nx_jpeg_sof_test.nx -- KAT for SOF0 parser.
2//
3// Test cases:
4// A. Standard 4:2:0 YCbCr SOF0 (Y H=2 V=2, Cb H=1 V=1, Cr H=1 V=1)
5// at 640x480.
6// B. Grayscale 1-component SOF0 at 320x240.
7// C. CMYK 4-component SOF0 at 100x100.
8// D. Malformed: bad precision (16 instead of 8).
9// E. Malformed: Nf = 0.
10// F. Malformed: zero dimension.
11// G. MCU dimensions + counts derived correctly.
12//
13// expect_exit: 0
14// license_tier: ORIGINAL
15
16import "nx_syscalls.nx"
17import "nx_jpeg_sof.nx"
18
19func _fail(n: i64) -> i64 {
20 let b: *u8 = sys_mmap(16)
21 b[0]=0x46; b[1]=0x41; b[2]=0x49; b[3]=0x4C; b[4]=0x3D
22 sys_write(2, b, 5)
23 var x: i64 = n
24 if x < 0 { let m: *u8 = sys_mmap(4); m[0]=0x2D; sys_write(2, m, 1); x = 0 - x }
25 if x == 0 { let z: *u8 = sys_mmap(4); z[0]=0x30; sys_write(2, z, 1) }
26 else {
27 let buf: *u8 = sys_mmap(16)
28 var pos: i64 = 0
29 while x > 0 { buf[pos] = (0x30 + (x % 10)) as u8; x = x / 10; pos = pos + 1 }
30 let out: *u8 = sys_mmap(16)
31 var i: i64 = 0
32 while i < pos { out[i] = buf[pos - 1 - i]; i = i + 1 }
33 sys_write(2, out, pos)
34 }
35 let nl: *u8 = sys_mmap(4); nl[0]=0x0A; sys_write(2, nl, 1)
36 return 0
37}
38
39func main() -> i64 {
40 let frame: *NxJpegFrame = sys_mmap(NX_JPEG_FRAME_BYTES) as *NxJpegFrame
41 let comps: *NxJpegSofComponent = sys_mmap(NX_JPEG_SOF_COMP_BYTES * 4) as *NxJpegSofComponent
42 frame.components = comps
43
44 // ============================================================
45 // Section A: 4:2:0 YCbCr 640x480
46 // P=8, Y=480 (0x01E0), X=640 (0x0280), Nf=3
47 // Y: id=1, H=2 V=2, Tq=0 -> hv=0x22
48 // Cb: id=2, H=1 V=1, Tq=1 -> hv=0x11
49 // Cr: id=3, H=1 V=1, Tq=1 -> hv=0x11
50 // Total payload = 6 + 3*3 = 15 bytes
51 // ============================================================
52 let p_a: *u8 = sys_mmap(32)
53 p_a[0] = 8
54 p_a[1] = 0x01; p_a[2] = 0xE0 // Y=480
55 p_a[3] = 0x02; p_a[4] = 0x80 // X=640
56 p_a[5] = 3
57 p_a[6] = 1; p_a[7] = 0x22; p_a[8] = 0
58 p_a[9] = 2; p_a[10] = 0x11; p_a[11] = 1
59 p_a[12] = 3; p_a[13] = 0x11; p_a[14] = 1
60
61 if nx_jpeg_sof_parse(p_a, 15, frame) != NX_JPEG_SOF_OK { _fail(1); return 1 }
62 if frame.precision != 8 { _fail(2); return 2 }
63 if frame.height != 480 { _fail(3); return 3 }
64 if frame.width != 640 { _fail(4); return 4 }
65 if frame.n_components != 3 { _fail(5); return 5 }
66 if frame.max_h != 2 { _fail(6); return 6 }
67 if frame.max_v != 2 { _fail(7); return 7 }
68
69 let c0: *NxJpegSofComponent = comps
70 let c1: *NxJpegSofComponent = (comps as i64 + NX_JPEG_SOF_COMP_BYTES) as *NxJpegSofComponent
71 let c2: *NxJpegSofComponent = (comps as i64 + 2 * NX_JPEG_SOF_COMP_BYTES) as *NxJpegSofComponent
72 if c0.ci != 1 { _fail(10); return 10 }
73 if c0.hi != 2 { _fail(11); return 11 }
74 if c0.vi != 2 { _fail(12); return 12 }
75 if c0.tqi != 0 { _fail(13); return 13 }
76 if c1.ci != 2 { _fail(14); return 14 }
77 if c1.hi != 1 { _fail(15); return 15 }
78 if c1.vi != 1 { _fail(16); return 16 }
79 if c1.tqi != 1 { _fail(17); return 17 }
80 if c2.ci != 3 { _fail(18); return 18 }
81 if c2.tqi != 1 { _fail(19); return 19 }
82
83 // MCU dimensions for 4:2:0: 16x16. 640/16 = 40 cols; 480/16 = 30 rows.
84 if nx_jpeg_sof_mcu_pixel_width(frame) != 16 { _fail(20); return 20 }
85 if nx_jpeg_sof_mcu_pixel_height(frame) != 16 { _fail(21); return 21 }
86 if nx_jpeg_sof_mcu_cols(frame) != 40 { _fail(22); return 22 }
87 if nx_jpeg_sof_mcu_rows(frame) != 30 { _fail(23); return 23 }
88
89 // ============================================================
90 // Section B: grayscale 320x240
91 // P=8, Y=240, X=320, Nf=1, comp{id=1, H=1 V=1, Tq=0}
92 // ============================================================
93 let p_b: *u8 = sys_mmap(16)
94 p_b[0] = 8
95 p_b[1] = 0x00; p_b[2] = 0xF0 // Y=240
96 p_b[3] = 0x01; p_b[4] = 0x40 // X=320
97 p_b[5] = 1
98 p_b[6] = 1; p_b[7] = 0x11; p_b[8] = 0
99
100 if nx_jpeg_sof_parse(p_b, 9, frame) != NX_JPEG_SOF_OK { _fail(30); return 30 }
101 if frame.height != 240 { _fail(31); return 31 }
102 if frame.width != 320 { _fail(32); return 32 }
103 if frame.n_components != 1 { _fail(33); return 33 }
104 if frame.max_h != 1 { _fail(34); return 34 }
105 if frame.max_v != 1 { _fail(35); return 35 }
106 if nx_jpeg_sof_mcu_pixel_width(frame) != 8 { _fail(36); return 36 }
107 if nx_jpeg_sof_mcu_cols(frame) != 40 { _fail(37); return 37 }
108 if nx_jpeg_sof_mcu_rows(frame) != 30 { _fail(38); return 38 }
109
110 // ============================================================
111 // Section C: CMYK 100x100, 4 components
112 // ============================================================
113 let p_c: *u8 = sys_mmap(32)
114 p_c[0] = 8
115 p_c[1] = 0x00; p_c[2] = 0x64 // Y=100
116 p_c[3] = 0x00; p_c[4] = 0x64 // X=100
117 p_c[5] = 4
118 p_c[6] = 1; p_c[7] = 0x11; p_c[8] = 0
119 p_c[9] = 2; p_c[10] = 0x11; p_c[11] = 1
120 p_c[12] = 3; p_c[13] = 0x11; p_c[14] = 2
121 p_c[15] = 4; p_c[16] = 0x11; p_c[17] = 3
122
123 if nx_jpeg_sof_parse(p_c, 18, frame) != NX_JPEG_SOF_OK { _fail(40); return 40 }
124 if frame.n_components != 4 { _fail(41); return 41 }
125 let c4_0: *NxJpegSofComponent = (frame.components as i64 + 3 * NX_JPEG_SOF_COMP_BYTES) as *NxJpegSofComponent
126 if c4_0.ci != 4 { _fail(42); return 42 }
127 if c4_0.tqi != 3 { _fail(43); return 43 }
128
129 // ============================================================
130 // Section D: bad precision (P=16 instead of 8)
131 // ============================================================
132 let p_d: *u8 = sys_mmap(16)
133 p_d[0] = 16
134 p_d[1] = 0x00; p_d[2] = 0x10; p_d[3] = 0x00; p_d[4] = 0x10; p_d[5] = 1
135 p_d[6] = 1; p_d[7] = 0x11; p_d[8] = 0
136 if nx_jpeg_sof_parse(p_d, 9, frame) != NX_JPEG_SOF_BAD_PRECISION { _fail(50); return 50 }
137
138 // ============================================================
139 // Section E: Nf = 0
140 // ============================================================
141 let p_e: *u8 = sys_mmap(16)
142 p_e[0] = 8
143 p_e[1] = 0x00; p_e[2] = 0x10; p_e[3] = 0x00; p_e[4] = 0x10; p_e[5] = 0
144 if nx_jpeg_sof_parse(p_e, 6, frame) != NX_JPEG_SOF_BAD_NF { _fail(60); return 60 }
145
146 // ============================================================
147 // Section F: zero dimension (X = 0)
148 // ============================================================
149 let p_f: *u8 = sys_mmap(16)
150 p_f[0] = 8
151 p_f[1] = 0x00; p_f[2] = 0x10
152 p_f[3] = 0x00; p_f[4] = 0x00 // X = 0
153 p_f[5] = 1; p_f[6] = 1; p_f[7] = 0x11; p_f[8] = 0
154 if nx_jpeg_sof_parse(p_f, 9, frame) != NX_JPEG_SOF_ZERO_DIM { _fail(70); return 70 }
155
156 let pass: *u8 = sys_mmap(16)
157 pass[0]=0x50; pass[1]=0x41; pass[2]=0x53; pass[3]=0x53; pass[4]=0x0A
158 sys_write(1, pass, 5)
159 return 0
160}