code wiki / _hdl_build / nx_zpng_test.nx

nx_zpng_test.nx source

↩ module page · 24 lines · 2108 B

1// nx_zpng_test.nx -- prove the compressing PNG encoder is CORRECT (decodes) + measure the size win on a 2// flat-region image (gradient + fills, like our generated art). license_tier: ORIGINAL expect_exit: 0 3import "nx_zpng.nx" 4import "nx_shade.nx" 5import "nx_syscalls.nx" 6 7func aw(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 8func an(v: i64) -> i64 { var m: i64=v; if m==0{sys_write(1,"0" as *u8,1);return 0} let t:*u8=sys_mmap(24); var k:i64=0; while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1} let o:*u8=sys_mmap(24); var i:i64=0; while i<k{o[i]=t[k-1-i];i=i+1} sys_write(1,o,k); return 0 } 9func frect(fb: *i64, w: i64, h: i64, x0: i64, y0: i64, x1: i64, y1: i64, c: i64) -> i64 { var yy: i64=y0; if yy<0{yy=0} var ye: i64=y1; if ye>h{ye=h} while yy<ye{ var xx: i64=x0; if xx<0{xx=0} var xe: i64=x1; if xe>w{xe=w} while xx<xe{fb[yy*w+xx]=c; xx=xx+1} yy=yy+1 } return 0 } 10func efill(fb: *i64, w: i64, h: i64, cx: i64, cy: i64, rx: i64, ry: i64, c: i64) -> i64 { var y: i64=cy-ry; while y<=cy+ry{ var x: i64=cx-rx; while x<=cx+rx{ let dx: i64=x-cx; let dy: i64=y-cy; if dx*dx*ry*ry+dy*dy*rx*rx<=rx*rx*ry*ry{ if x>=0{if x<w{if y>=0{if y<h{fb[y*w+x]=c}}}} } x=x+1 } y=y+1 } return 0 } 11 12func main(argc: i64, argv: *i64) -> i64 { 13 let W: i64 = 320; let H: i64 = 320 14 let fb: *i64 = sys_mmap(8*W*H) as *i64 15 var y: i64 = 0 16 while y < H*7/10 { let t: i64 = y*256/(H*7/10); frect(fb, W, H, 0, y, W, y+1, sh_rgb(20 + 60*t/256, 24 + 30*t/256, 60 + 30*t/256)); y = y + 1 } // gradient sky 17 frect(fb, W, H, 0, H*7/10, W, H, sh_rgb(34, 70, 46)) // flat ground 18 efill(fb, W, H, 240, 70, 34, 34, sh_rgb(255, 226, 120)) // sun 19 frect(fb, W, H, 40, 150, 120, 230, sh_rgb(150, 60, 60)) // a building 20 let rawlen: i64 = H*(1 + W*3) 21 let sz: i64 = write_png_z(fb, W, H, "knowledge/staging/game/ztest.png" as *u8) 22 aw("nx_zpng_test: compressed PNG = "); an(sz); aw(" B (uncompressed would be ~"); an(rawlen); aw(" B raw) -> ratio ~"); an(rawlen/sz); aw("x. View ztest.png to confirm it decodes.\n" as *u8) 23 sys_exit(0); return 0 24}