code wiki / (root) / nx_jpeg_upsample_test.nx

nx_jpeg_upsample_test.nx source

↩ module page · 116 lines · 4888 B

1// nx_jpeg_upsample_test.nx -- KAT for chroma upsampling. 2// 3// Test cases: 4// A. 4:4:4 (no upsample): identity copy 5// B. 4:2:2 (2x1 horizontal): each src pixel duplicated horizontally 6// C. 4:2:0 (2x2): each src pixel becomes a 2x2 block 7// 8// expect_exit: 0 9// license_tier: ORIGINAL 10 11import "nx_syscalls.nx" 12import "nx_jpeg_upsample.nx" 13 14func _fail(n: i64) -> i64 { 15 let b: *u8 = sys_mmap(16) 16 b[0]=0x46; b[1]=0x41; b[2]=0x49; b[3]=0x4C; b[4]=0x3D 17 sys_write(2, b, 5) 18 var x: i64 = n 19 if x < 0 { let m: *u8 = sys_mmap(4); m[0]=0x2D; sys_write(2, m, 1); x = 0 - x } 20 if x == 0 { let z: *u8 = sys_mmap(4); z[0]=0x30; sys_write(2, z, 1) } 21 else { 22 let buf: *u8 = sys_mmap(16) 23 var pos: i64 = 0 24 while x > 0 { buf[pos] = (0x30 + (x % 10)) as u8; x = x / 10; pos = pos + 1 } 25 let out: *u8 = sys_mmap(16) 26 var i: i64 = 0 27 while i < pos { out[i] = buf[pos - 1 - i]; i = i + 1 } 28 sys_write(2, out, pos) 29 } 30 let nl: *u8 = sys_mmap(4); nl[0]=0x0A; sys_write(2, nl, 1) 31 return 0 32} 33 34func main() -> i64 { 35 // ============================================================ 36 // Section A: 4:4:4 identity copy (Hi=Vi=max_h=max_v=1) 37 // Source 2x2, output 2x2 (rep_h=rep_v=1). 38 // ============================================================ 39 let src_a: *u8 = sys_mmap(8) 40 src_a[0]=10; src_a[1]=20; src_a[2]=30; src_a[3]=40 41 let dst_a: *u8 = sys_mmap(8) 42 var i: i64 = 0 43 while i < 4 { dst_a[i] = 0xFF; i = i + 1 } 44 if nx_jpeg_upsample_nearest(src_a, 2, 2, 2, 1, 1, 1, 1, dst_a, 2) != NX_JPEG_UPSAMPLE_OK { _fail(1); return 1 } 45 if dst_a[0] != 10 { _fail(2); return 2 } 46 if dst_a[1] != 20 { _fail(3); return 3 } 47 if dst_a[2] != 30 { _fail(4); return 4 } 48 if dst_a[3] != 40 { _fail(5); return 5 } 49 50 // ============================================================ 51 // Section B: 4:2:2 horizontal 2x1 replicate 52 // Source 2x2 chroma; destination 4x2 (rep_h=2, rep_v=1). 53 // Source [10,20 / 30,40] -> destination [10,10,20,20 / 30,30,40,40]. 54 // ============================================================ 55 let src_b: *u8 = sys_mmap(8) 56 src_b[0]=10; src_b[1]=20; src_b[2]=30; src_b[3]=40 57 let dst_b: *u8 = sys_mmap(16) 58 i = 0 59 while i < 8 { dst_b[i] = 0xFF; i = i + 1 } 60 if nx_jpeg_upsample_nearest(src_b, 2, 2, 2, 1, 1, 2, 1, dst_b, 4) != NX_JPEG_UPSAMPLE_OK { _fail(10); return 10 } 61 // Row 0: 10 10 20 20 62 if dst_b[0] != 10 { _fail(11); return 11 } 63 if dst_b[1] != 10 { _fail(12); return 12 } 64 if dst_b[2] != 20 { _fail(13); return 13 } 65 if dst_b[3] != 20 { _fail(14); return 14 } 66 // Row 1: 30 30 40 40 67 if dst_b[4] != 30 { _fail(15); return 15 } 68 if dst_b[5] != 30 { _fail(16); return 16 } 69 if dst_b[6] != 40 { _fail(17); return 17 } 70 if dst_b[7] != 40 { _fail(18); return 18 } 71 72 // ============================================================ 73 // Section C: 4:2:0 2x2 replicate 74 // Source 2x2 chroma; destination 4x4 (rep_h=rep_v=2). 75 // Source [10,20 / 30,40] -> destination 4x4 of 2x2 blocks. 76 // ============================================================ 77 let src_c: *u8 = sys_mmap(8) 78 src_c[0]=10; src_c[1]=20; src_c[2]=30; src_c[3]=40 79 let dst_c: *u8 = sys_mmap(32) 80 i = 0 81 while i < 16 { dst_c[i] = 0xFF; i = i + 1 } 82 if nx_jpeg_upsample_nearest(src_c, 2, 2, 2, 1, 1, 2, 2, dst_c, 4) != NX_JPEG_UPSAMPLE_OK { _fail(20); return 20 } 83 // Expected layout (dst_stride=4): 84 // row 0: 10 10 20 20 85 // row 1: 10 10 20 20 86 // row 2: 30 30 40 40 87 // row 3: 30 30 40 40 88 if dst_c[0] != 10 { _fail(21); return 21 } 89 if dst_c[1] != 10 { _fail(22); return 22 } 90 if dst_c[2] != 20 { _fail(23); return 23 } 91 if dst_c[3] != 20 { _fail(24); return 24 } 92 if dst_c[4] != 10 { _fail(25); return 25 } 93 if dst_c[5] != 10 { _fail(26); return 26 } 94 if dst_c[6] != 20 { _fail(27); return 27 } 95 if dst_c[7] != 20 { _fail(28); return 28 } 96 if dst_c[8] != 30 { _fail(29); return 29 } 97 if dst_c[9] != 30 { _fail(30); return 30 } 98 if dst_c[10] != 40 { _fail(31); return 31 } 99 if dst_c[11] != 40 { _fail(32); return 32 } 100 if dst_c[12] != 30 { _fail(33); return 33 } 101 if dst_c[13] != 30 { _fail(34); return 34 } 102 if dst_c[14] != 40 { _fail(35); return 35 } 103 if dst_c[15] != 40 { _fail(36); return 36 } 104 105 // ============================================================ 106 // Section D: bad factor (Hi > max_h) 107 // ============================================================ 108 let src_d: *u8 = sys_mmap(4) 109 let dst_d: *u8 = sys_mmap(4) 110 if nx_jpeg_upsample_nearest(src_d, 2, 1, 2, 2, 1, 1, 1, dst_d, 2) != NX_JPEG_UPSAMPLE_BAD_FACTOR { _fail(40); return 40 } 111 112 let pass: *u8 = sys_mmap(16) 113 pass[0]=0x50; pass[1]=0x41; pass[2]=0x53; pass[3]=0x53; pass[4]=0x0A 114 sys_write(1, pass, 5) 115 return 0 116}