code wiki / _hdl_build / nx_vcodec_color_test.nx

nx_vcodec_color_test.nx source

↩ module page · 84 lines · 4203 B

1// nx_vcodec_color_test.nx -- gate for the sovereign integer color foundation: RGB<->YCbCr (BT.601) 2// round-trips within rounding error, gray is exact, plane conversion is per-pixel correct. Pure integer, 3// sovereign, no third-party. license_tier: ORIGINAL 4import "nx_vcodec_color.nx" 5 6func g_puts(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 7func g_pn(v: i64) -> i64 { 8 if v==0 { sys_write(1,"0" as *u8,1); return 0 } 9 let b: *u8 = sys_mmap(28); var x: i64 = v; if x<0 { sys_write(1,"-" as *u8,1); x=0-x } 10 var d: i64=0; var y: i64=x 11 while y>0 { d=d+1; y=y/10 } 12 var i: i64=d-1; y=x 13 while i>=0 { b[i]=(48+(y%10)) as u8; y=y/10; i=i-1 } 14 sys_write(1,b,d); return 0 15} 16func g_check(name: *u8, cond: i64) -> i64 { 17 if cond==1 { g_puts(" PASS " as *u8) } else { g_puts(" FAIL " as *u8) } 18 g_puts(name); g_puts("\n" as *u8); return cond 19} 20func iabs(v: i64) -> i64 { if v<0 { return 0-v } return v } 21// rgb -> yuv -> rgb, within tol on every channel? 22func rt_within(r: i64, g: i64, b: i64, tol: i64) -> i64 { 23 let yuv: *i64 = sys_mmap(8*3) as *i64 24 rgb_to_ycbcr(r, g, b, yuv) 25 let o: *i64 = sys_mmap(8*3) as *i64 26 ycbcr_to_rgb(yuv[0], yuv[1], yuv[2], o) 27 if iabs(o[0]-r) > tol { return 0 } 28 if iabs(o[1]-g) > tol { return 0 } 29 if iabs(o[2]-b) > tol { return 0 } 30 return 1 31} 32 33func main() -> i64 { 34 g_puts("nx_vcodec_color gate -- integer RGB<->YCbCr (BT.601), the sovereign color foundation\n" as *u8) 35 var pass: i64=0; var total: i64=0 36 37 // gray maps to Y=gray, Cb=Cr=128 EXACTLY, and round-trips exactly 38 let yuv: *i64 = sys_mmap(8*3) as *i64 39 rgb_to_ycbcr(128, 128, 128, yuv) 40 var gray_ok: i64=1 41 if yuv[0]!=128 { gray_ok=0 } if yuv[1]!=128 { gray_ok=0 } if yuv[2]!=128 { gray_ok=0 } 42 pass = pass + g_check("gray(128) -> Y=128 Cb=128 Cr=128 (neutral exact)" as *u8, gray_ok); total=total+1 43 pass = pass + g_check("gray round-trips EXACT (luma-only path lossless)" as *u8, rt_within(128,128,128,0)); total=total+1 44 45 // primaries + a mixed color round-trip within rounding error (tol 5) 46 pass = pass + g_check("red (255,0,0) round-trips within +/-5" as *u8, rt_within(255,0,0,5)); total=total+1 47 pass = pass + g_check("green (0,255,0) round-trips within +/-5" as *u8, rt_within(0,255,0,5)); total=total+1 48 pass = pass + g_check("blue (0,0,255) round-trips within +/-5" as *u8, rt_within(0,0,255,5)); total=total+1 49 pass = pass + g_check("mixed (100,150,200) round-trips within +/-5" as *u8, rt_within(100,150,200,5)); total=total+1 50 51 // sweep a grid of colors -- all within tolerance 52 var sweep_ok: i64=1; var rr: i64=0 53 while rr<=255 { 54 var gg: i64=0 55 while gg<=255 { 56 if rt_within(rr, gg, 128, 6) != 1 { sweep_ok=0; gg=256; rr=256 } else { gg=gg+85 } 57 } 58 rr=rr+85 59 } 60 pass = pass + g_check("16-color R/G grid round-trips within +/-6 (no blowups)" as *u8, sweep_ok); total=total+1 61 62 // plane conversion: a 4-pixel RGB frame -> Y plane matches per-pixel 63 let rgb: *u8 = sys_mmap(12) 64 rgb[0]=200 as u8; rgb[1]=50 as u8; rgb[2]=25 as u8 65 rgb[3]=10 as u8; rgb[4]=220 as u8; rgb[5]=60 as u8 66 rgb[6]=40 as u8; rgb[7]=70 as u8; rgb[8]=240 as u8 67 rgb[9]=128 as u8; rgb[10]=128 as u8; rgb[11]=128 as u8 68 let yp: *u8 = sys_mmap(4); let up: *u8 = sys_mmap(4); let vp: *u8 = sys_mmap(4) 69 rgb_frame_to_yuv(rgb, 4, yp, up, vp) 70 var plane_ok: i64=1 71 let t: *i64 = sys_mmap(8*3) as *i64 72 var i: i64=0 73 while i<4 { 74 rgb_to_ycbcr(rgb[i*3] as i64, rgb[i*3+1] as i64, rgb[i*3+2] as i64, t) 75 if (yp[i] as i64)!=t[0] { plane_ok=0 } if (up[i] as i64)!=t[1] { plane_ok=0 } if (vp[i] as i64)!=t[2] { plane_ok=0 } 76 i=i+1 77 } 78 pass = pass + g_check("RGB frame -> Y/Cb/Cr planes per-pixel correct (codec input path)" as *u8, plane_ok); total=total+1 79 80 g_puts("---- vcodec_color gate: passed " as *u8); g_pn(pass); g_puts(" / " as *u8); g_pn(total); g_puts(" ----\n" as *u8) 81 if pass == total { g_puts("VERDICT: GREEN (sovereign integer color RGB<->YCbCr -- color foundation for the s-class codec)\n" as *u8); return 0 } 82 g_puts("VERDICT: RED\n" as *u8) 83 return 1 84}