code wiki / (root) / nx_image_load_raw_test.nx

nx_image_load_raw_test.nx source

↩ module page · 62 lines · 2064 B

1// Smoke for the raw-bytes image bridge. Round-trip: allocate Image, 2// pack header+pixels into a buffer, decode header back, verify dims + 3// pixel sample. 4 5import "nx_syscalls.nx" 6import "nx_image.nx" 7import "nx_image_load_raw.nx" 8 9func main() -> i64 { 10 // Build a small synthetic image 4x3 RGB with known pixel pattern. 11 let img: *Image = nx_image_alloc(4, 3, 3) 12 var y: i64 = 0 13 while y < 3 { 14 var x: i64 = 0 15 while x < 4 { 16 nx_image_set(img, x, y, 0, x * 10 + y) 17 nx_image_set(img, x, y, 1, x * 10 + y + 100) 18 nx_image_set(img, x, y, 2, x * 10 + y + 200 - 256 * ((x * 10 + y + 200) / 256)) 19 x = x + 1 20 } 21 y = y + 1 22 } 23 24 // T1: pack into a buffer 25 let total_bytes: i64 = 24 + 4 * 3 * 3 26 let packed: *u8 = sys_mmap(total_bytes + 16) 27 let written: i64 = nx_image_save_raw_pack(img, packed) 28 if written != total_bytes { return 1 } 29 30 // T2: verify width header bytes (little-endian 4) 31 if packed[0] != 4 { return 10 } 32 if packed[1] != 0 { return 11 } 33 if packed[7] != 0 { return 12 } 34 35 // T3: verify height bytes 36 if packed[8] != 3 { return 20 } 37 if packed[9] != 0 { return 21 } 38 39 // T4: verify channels bytes 40 if packed[16] != 3 { return 30 } 41 if packed[17] != 0 { return 31 } 42 43 // T5: verify pixel data at offset 24 44 // pixel (0,0) is R=0, G=100, B=200 45 if packed[24] != 0 { return 40 } 46 if packed[25] != 100 { return 41 } 47 if packed[26] != 200 { return 42 } 48 // pixel (3, 2) is R=32, G=132, B=32+200=232 (since 232 fits in u8) 49 // offset = 24 + (2 * 4 + 3) * 3 = 24 + 33 = 57 50 if packed[57] != 32 { return 50 } 51 if packed[58] != 132 { return 51 } 52 53 // T6: read header back via nx_image_load_read_i64_le 54 let w_read: i64 = nx_image_load_read_i64_le(packed, 0) 55 let h_read: i64 = nx_image_load_read_i64_le(packed, 8) 56 let c_read: i64 = nx_image_load_read_i64_le(packed, 16) 57 if w_read != 4 { return 60 } 58 if h_read != 3 { return 61 } 59 if c_read != 3 { return 62 } 60 61 return 0 62}