code wiki / (root) / nx_region_edge_audit_t278.nx

nx_region_edge_audit_t278.nx source

↩ module page · 54 lines · 3090 B

1import "nx_nxa_texbake_region_lib.nx" 2import "nx_png_decoder.nx" 3func eb_h(p:*u8,res:i64,x:i64,y:i64)->i64{let o:i64=(y*res+x)*3;return ntr_h_dec((p[o] as i64)*256+(p[o+1] as i64))} 4func eb_sign(v:i64)->i64{if v<0{return 0-1};if v>0{return 1};return 0} 5func main(argc:i64,argv:*i64)->i64{ 6 if argc!=2{return 2} 7 let lp:*i64=sys_mmap_try(__size_of(i64)) as *i64;let b:*u8=sys_map_file(argv[1] as *u8,lp) 8 if (b as i64)<=0{return 3} 9 let at:i64=ntr_texr_find(b,lp[0]);if at<0{return 4} 10 let w:*i64=b as *i64;let ns:i64=w[at+NTR_W_NSETS];let nj:i64=w[at+NTR_W_NJ];let stride:i64=w[at+NTR_W_SETW];let res:i64=w[at+NTR_W_RES] 11 let blob:i64=(at+NTR_HDR_WORDS+nj*NTR_JOINT_WORDS+ns*stride)*NTR_WORD 12 var s:i64=0;var allcross:i64=0;var allflip:i64=0 13 while s<ns{ 14 let set:i64=at+NTR_HDR_WORDS+nj*NTR_JOINT_WORDS+s*stride 15 let gr:i64=w[set+NTR_S_GR];let tp:i64=w[set+NTR_S_TPX];let count:i64=w[set+NTR_S_NJR] 16 if gr<=0||tp<2||count<=0||tp>res/gr{return 5} 17 let hr:i64=set+NTR_SET_META+NTR_MAP_DISP*NTR_MAPREC;let nr:i64=set+NTR_SET_META+NTR_MAP_NORMAL*NTR_MAPREC 18 let h:*NxPngResult=nx_png_decode(((b as i64)+blob+w[hr+1]) as *u8,w[hr+2]) 19 let n:*NxPngResult=nx_png_decode(((b as i64)+blob+w[nr+1]) as *u8,w[nr+2]) 20 if h.error_code!=0||n.error_code!=0{return 6} 21 if h.pixels_size!=res*res*3||n.pixels_size!=res*res*3{return 7} 22 var cross:i64=0;var changed:i64=0;var flips:i64=0;var maxdelta:i64=0 23 var rk:i64=0 24 while rk<count{ 25 let x0:i64=(rk%gr)*tp;let y0:i64=(rk/gr)*tp 26 var side:i64=0 27 while side<4{ 28 var q:i64=0 29 while q<tp{ 30 var x:i64=x0;var y:i64=y0+q;var axis:i64=0 31 if side==1{x=x0+tp-1} 32 if side>=2{axis=1;x=x0+q;y=y0;if side==3{y=y0+tp-1}} 33 var a:i64=x;var lo:i64=x0;if axis==1{a=y;lo=y0} 34 let hi:i64=lo+tp-1;let om:i64=ntr_clamp(a-1,0,res-1);let op:i64=ntr_clamp(a+1,0,res-1) 35 let cm:i64=ntr_clamp(a-1,lo,hi);let cp:i64=ntr_clamp(a+1,lo,hi) 36 if om<lo||op>hi{cross=cross+1 37 var od:i64=0;var cd:i64=0 38 if axis==0{od=eb_h(h.pixels,res,op,y)-eb_h(h.pixels,res,om,y);cd=(eb_h(h.pixels,res,cp,y)-eb_h(h.pixels,res,cm,y))*2/(cp-cm)} 39 else{od=eb_h(h.pixels,res,x,op)-eb_h(h.pixels,res,x,om);cd=(eb_h(h.pixels,res,x,cp)-eb_h(h.pixels,res,x,cm))*2/(cp-cm)} 40 if od!=cd{changed=changed+1} 41 let delta:i64=ntr_abs(od-cd);if delta>maxdelta{maxdelta=delta} 42 let normal:i64=(n.pixels[(y*res+x)*3+axis] as i64)-NTB_N_MID 43 if normal!=0&&eb_sign(normal)==eb_sign(0-od)&&eb_sign(normal)!=eb_sign(0-cd){flips=flips+1} 44 } 45 q=q+1 46 };side=side+1 47 };rk=rk+1 48 } 49 nt_outs("REGION-EDGE region=");nt_outn(w[set+NTR_S_REGION]);nt_outs(" cross_chart_stencils=");nt_outn(cross);nt_outs(" gradient_changed=");nt_outn(changed);nt_outs(" stored_normal_direction_disagrees_local=");nt_outn(flips);nt_outs(" max_gradient_delta_um_per_two_texels=");nt_outn(maxdelta);nt_outs("\n") 50 allcross=allcross+cross;allflip=allflip+flips;s=s+1 51 } 52 nt_outs("REGION-EDGE observed_stencils=");nt_outn(allcross);nt_outs(" observed_direction_disagreements=");nt_outn(allflip);nt_outs("\n") 53 if allcross<=0||allflip<=0{return 8};return 0 54}