code wiki / (root) / nx_color_v2_test.nx

nx_color_v2_test.nx source

↩ module page · 127 lines · 4626 B

1// nx_color_v2_test.nx -- smoke for V1 color primitives. 2 3import "syscalls.nx" 4import "nx_image.nx" 5import "nx_color_v2.nx" 6 7func main() -> i64 { 8 // === Test 1: RGB(255, 0, 0) -> HSV(0, 255, 255) === 9 let hsv: *HSVColor = (sys_mmap(24)) as *HSVColor 10 nx_color_rgb_to_hsv(255, 0, 0, hsv) 11 if hsv.h != 0 { return 1 } 12 if hsv.s != 255 { return 2 } 13 if hsv.v != 255 { return 3 } 14 15 // === Test 2: RGB(0, 255, 0) -> HSV(120, 255, 255) === 16 nx_color_rgb_to_hsv(0, 255, 0, hsv) 17 if hsv.h != 120 { return 4 } 18 if hsv.s != 255 { return 5 } 19 20 // === Test 3: RGB(0, 0, 255) -> HSV(240, 255, 255) === 21 nx_color_rgb_to_hsv(0, 0, 255, hsv) 22 if hsv.h != 240 { return 6 } 23 24 // === Test 4: RGB(255, 255, 255) -> HSV(0, 0, 255) (white) === 25 nx_color_rgb_to_hsv(255, 255, 255, hsv) 26 if hsv.s != 0 { return 7 } 27 if hsv.v != 255 { return 8 } 28 29 // === Test 5: RGB(0, 0, 0) -> HSV(0, 0, 0) (black) === 30 nx_color_rgb_to_hsv(0, 0, 0, hsv) 31 if hsv.s != 0 { return 9 } 32 if hsv.v != 0 { return 10 } 33 34 // === Test 6: RGB(128, 64, 32) -> YCbCr(roughly Y in 70-90 range) === 35 let ycc: *YCbCrColor = (sys_mmap(24)) as *YCbCrColor 36 nx_color_rgb_to_ycbcr(128, 64, 32, ycc) 37 if ycc.y < 50 { return 20 } 38 if ycc.y > 110 { return 21 } 39 if ycc.cb >= 128 { return 22 } // less blue -> Cb < 128 40 if ycc.cr <= 128 { return 23 } // more red -> Cr > 128 41 42 // === Test 7: Kovac skin classifier === 43 // Typical skin RGB ~ (200, 150, 130) ish 44 if nx_color_kovac_skin_pixel(200, 150, 130) != 1 { return 30 } 45 // Pure red (not skin) 46 if nx_color_kovac_skin_pixel(255, 0, 0) != 0 { return 31 } 47 // Pure green (not skin) 48 if nx_color_kovac_skin_pixel(0, 255, 0) != 0 { return 32 } 49 // Pure blue (not skin) 50 if nx_color_kovac_skin_pixel(0, 0, 255) != 0 { return 33 } 51 // Black (not skin) 52 if nx_color_kovac_skin_pixel(0, 0, 0) != 0 { return 34 } 53 54 // === Test 8: skin mask on a synthetic image with mixed colors === 55 let img: *Image = nx_image_alloc(16, 16, 3) 56 // Left half: skin tone (200, 150, 130) 57 // Right half: pure blue (0, 0, 255) 58 var y: i64 = 0 59 while y < 16 { 60 var x: i64 = 0 61 while x < 16 { 62 if x < 8 { 63 nx_image_set(img, x, y, 0, 200) 64 nx_image_set(img, x, y, 1, 150) 65 nx_image_set(img, x, y, 2, 130) 66 } 67 if x >= 8 { 68 nx_image_set(img, x, y, 0, 0) 69 nx_image_set(img, x, y, 1, 0) 70 nx_image_set(img, x, y, 2, 255) 71 } 72 x = x + 1 73 } 74 y = y + 1 75 } 76 let mask: *Image = nx_color_skin_mask(img) 77 // Left half should be ~255 (skin), right half should be 0. 78 if nx_image_get(mask, 3, 8, 0) != 255 { return 40 } 79 if nx_image_get(mask, 12, 8, 0) != 0 { return 41 } 80 81 // === Test 9: K-means palette on a 3-color image === 82 let img3: *Image = nx_image_alloc(12, 12, 3) 83 // 3 vertical bands: red (0..3), green (4..7), blue (8..11) 84 var y3: i64 = 0 85 while y3 < 12 { 86 var x3: i64 = 0 87 while x3 < 12 { 88 if x3 < 4 { 89 nx_image_set(img3, x3, y3, 0, 255) 90 nx_image_set(img3, x3, y3, 1, 0) 91 nx_image_set(img3, x3, y3, 2, 0) 92 } 93 if x3 >= 4 { 94 if x3 < 8 { 95 nx_image_set(img3, x3, y3, 0, 0) 96 nx_image_set(img3, x3, y3, 1, 255) 97 nx_image_set(img3, x3, y3, 2, 0) 98 } 99 if x3 >= 8 { 100 nx_image_set(img3, x3, y3, 0, 0) 101 nx_image_set(img3, x3, y3, 1, 0) 102 nx_image_set(img3, x3, y3, 2, 255) 103 } 104 } 105 x3 = x3 + 1 106 } 107 y3 = y3 + 1 108 } 109 let pal: *Palette = nx_color_kmeans_palette(img3, 3, 5) 110 if pal.k != 3 { return 50 } 111 // Each cluster should have ~48 pixels (12*12 = 144 / 3). 112 var has_48: i64 = 0 113 var i: i64 = 0 114 while i < 3 { 115 if pal.counts[i] == 48 { has_48 = has_48 + 1 } 116 i = i + 1 117 } 118 if has_48 != 3 { return 51 } 119 120 // === Test 10: dominant channel === 121 if nx_color_dominant_channel(200, 50, 50) != 0 { return 60 } 122 if nx_color_dominant_channel(50, 200, 50) != 1 { return 61 } 123 if nx_color_dominant_channel(50, 50, 200) != 2 { return 62 } 124 if nx_color_dominant_channel(100, 100, 100) != 3 { return 63 } 125 126 return 0 127}