nx_region_edge_compare_t278.nx source
↩ module page · 81 lines · 5059 B
1// nx_region_edge_compare_t278.nx -- Compares texture regions and their associated data between two binary files for equality.
2import "nx_nxa_texbake_region_lib.nx"
3import "nx_png_decoder.nx"
4func ec_equal(a:*u8,b:*u8,n:i64)->i64{var i:i64=0;while i<n{if a[i]!=b[i]{return 0};i=i+1};return 1}
5func ec_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))}
6func ec_sign(v:i64)->i64{if v<0{return 0-1};if v>0{return 1};return 0}
7func main(argc:i64,argv:*i64)->i64{
8 if argc!=3{return 2}
9 let la:*i64=sys_mmap_try(__size_of(i64)) as *i64;let lb:*i64=sys_mmap_try(__size_of(i64)) as *i64
10 let a:*u8=sys_map_file(argv[1] as *u8,la);let b:*u8=sys_map_file(argv[2] as *u8,lb)
11 if (a as i64)<=0||(b as i64)<=0{return 3}
12 let ao:i64=ntr_texr_find(a,la[0]);let bo:i64=ntr_texr_find(b,lb[0]);if ao<0||bo<0{return 4}
13 let aw:*i64=a as *i64;let bw:*i64=b as *i64
14 var carried:i64=0;var ti:i64=0
15 while ti<aw[2]{
16 let te:i64=4+ti*4;let tag:i64=aw[te]
17 if tag!=nxa_tag4("TEXR"){
18 let other:i64=nxa_section_entry(b,lb[0],tag);if other<0{return 5}
19 let words:i64=aw[te+2];if words!=bw[other+2]{return 5}
20 if ec_equal(((a as i64)+aw[te+1]) as *u8,((b as i64)+bw[other+1]) as *u8,words*NTR_WORD)==0{return 5}
21 carried=carried+1
22 };ti=ti+1
23 }
24 let res:i64=aw[ao+NTR_W_RES];let nj:i64=aw[ao+NTR_W_NJ];let ns:i64=aw[ao+NTR_W_NSETS];let stride:i64=aw[ao+NTR_W_SETW]
25 if bw[bo+NTR_W_RES]!=res||bw[bo+NTR_W_NJ]!=nj||bw[bo+NTR_W_NSETS]!=ns||bw[bo+NTR_W_SETW]!=stride{return 6}
26 let ablob:i64=(ao+NTR_HDR_WORDS+nj*NTR_JOINT_WORDS+ns*stride)*NTR_WORD
27 let bblob:i64=(bo+NTR_HDR_WORDS+nj*NTR_JOINT_WORDS+ns*stride)*NTR_WORD
28 var s:i64=0;var preserved:i64=0;var allInterior:i64=0;var allBoundary:i64=0;var allChanged:i64=0;var allDisagree:i64=0
29 while s<ns{
30 let aset:i64=ao+NTR_HDR_WORDS+nj*NTR_JOINT_WORDS+s*stride
31 let bset:i64=ntr_texr_set(b,lb[0],aw[aset+NTR_S_REGION]);if bset<0{return 7}
32 if ec_equal(((a as i64)+aset*NTR_WORD) as *u8,((b as i64)+bset*NTR_WORD) as *u8,NTR_SET_META*NTR_WORD)==0{return 8}
33 var m:i64=0
34 while m<NTR_NMAPS{
35 if m!=NTR_MAP_NORMAL{
36 let ar:i64=aset+NTR_SET_META+m*NTR_MAPREC;let br:i64=bset+NTR_SET_META+m*NTR_MAPREC
37 if aw[ar+2]!=bw[br+2]{return 9}
38 if ec_equal(((a as i64)+ablob+aw[ar+1]) as *u8,((b as i64)+bblob+bw[br+1]) as *u8,aw[ar+2])==0{return 9}
39 preserved=preserved+1
40 };m=m+1
41 }
42 let anr:i64=aset+NTR_SET_META+NTR_MAP_NORMAL*NTR_MAPREC
43 let bnr:i64=bset+NTR_SET_META+NTR_MAP_NORMAL*NTR_MAPREC
44 let hr:i64=bset+NTR_SET_META+NTR_MAP_DISP*NTR_MAPREC
45 let old:*NxPngResult=nx_png_decode(((a as i64)+ablob+aw[anr+1]) as *u8,aw[anr+2])
46 let now:*NxPngResult=nx_png_decode(((b as i64)+bblob+bw[bnr+1]) as *u8,bw[bnr+2])
47 let h:*NxPngResult=nx_png_decode(((b as i64)+bblob+bw[hr+1]) as *u8,bw[hr+2])
48 if old.error_code!=0||now.error_code!=0||h.error_code!=0{return 10}
49 if old.pixels_size!=res*res*3||now.pixels_size!=res*res*3||h.pixels_size!=res*res*3{return 10}
50 let gr:i64=bw[bset+NTR_S_GR];let tp:i64=bw[bset+NTR_S_TPX];let charts:i64=bw[bset+NTR_S_NJR]
51 if gr<=0||tp<=0||tp>res/gr{return 11}
52 var interior:i64=0;var boundary:i64=0;var changed:i64=0;var disagree:i64=0;var y:i64=0
53 while y<res{
54 var x:i64=0
55 while x<res{
56 let idx:i64=(y*res+x)*3
57 let same:i64=ec_equal(((old.pixels as i64)+idx) as *u8,((now.pixels as i64)+idx) as *u8,3)
58 let tx:i64=x/tp;let ty:i64=y/tp;let rank:i64=ty*gr+tx
59 var ownedEdge:i64=0
60 if tx<gr&&rank<charts{if x%tp==0||x%tp==tp-1||y%tp==0||y%tp==tp-1{ownedEdge=1}}
61 if ownedEdge==0{if same==0{return 12};interior=interior+1}
62 else{
63 boundary=boundary+1;if same==0{changed=changed+1}
64 let x0:i64=tx*tp;let y0:i64=ty*tp
65 let xp:i64=ntr_min(x+1,x0+tp-1);let xm:i64=ntr_max(x-1,x0)
66 let yp:i64=ntr_min(y+1,y0+tp-1);let ym:i64=ntr_max(y-1,y0)
67 let dx:i64=ec_h(h.pixels,res,xp,y)-ec_h(h.pixels,res,xm,y)
68 let dy:i64=ec_h(h.pixels,res,x,yp)-ec_h(h.pixels,res,x,ym)
69 let nx:i64=(now.pixels[idx] as i64)-NTB_N_MID;let ny:i64=(now.pixels[idx+1] as i64)-NTB_N_MID
70 if nx!=0&&ec_sign(nx)!=ec_sign(0-dx){disagree=disagree+1}
71 if ny!=0&&ec_sign(ny)!=ec_sign(0-dy){disagree=disagree+1}
72 }
73 x=x+1
74 };y=y+1
75 }
76 nt_outs("EDGE-COMPARE region=");nt_outn(aw[aset+NTR_S_REGION]);nt_outs(" unchanged_nonedge_pixels=");nt_outn(interior);nt_outs(" boundary_pixels=");nt_outn(boundary);nt_outs(" changed_boundary_pixels=");nt_outn(changed);nt_outs(" new_direction_disagreements=");nt_outn(disagree);nt_outs("\n")
77 allInterior=allInterior+interior;allBoundary=allBoundary+boundary;allChanged=allChanged+changed;allDisagree=allDisagree+disagree;s=s+1
78 }
79 nt_outs("EDGE-COMPARE preserved_non_normal_maps=");nt_outn(preserved);nt_outs(" carried_sections=");nt_outn(carried);nt_outs(" unchanged_nonedge_pixels=");nt_outn(allInterior);nt_outs(" boundary_pixels=");nt_outn(allBoundary);nt_outs(" changed_boundary_pixels=");nt_outn(allChanged);nt_outs(" new_direction_disagreements=");nt_outn(allDisagree);nt_outs("\n")
80 if allChanged<=0||allDisagree!=0{return 13};return 0
81}