| 5 | from scipy.spatial import cKDTree |
| 6 | from PIL import Image |
| 7 |
| 8 | def initialize(records, originals): |
| 9 | global R, O, NE, N, M, RGBA, IDX, ALPHA |
| 10 | R = records; O = originals; NE = len(O); N = len(R) |
| 11 | M = {d['id']: NE + i for i, d in enumerate(R[NE:])} |
| 12 | RGBA = np.array([d['rgba'] for d in R[:NE]]) |
| 13 | for i, d in enumerate(R[:NE]): |
| 14 | if d['mask'] != 65535: RGBA[i, :, :, 3] = R[M[d['mask']]]['rgba'][:, :, 0] |
| 15 | IDX = np.array([d['idx'] for d in R]); ALPHA = RGBA[:, :, :, 3] |
| 16 |
| 17 | def trans(a,t): |
| 18 | if t&4:a=a.swapaxes(0,1) |
| 19 | if t&2:a=a[::-1] |
| 20 | if t&1:a=a[:,::-1] |
| 21 | return a |
| 22 |
| 23 | def features(a): |
| 24 | alpha=a[:,:,3:]/255.;rgb=a[:,:,:3]*alpha |
| 25 | g=rgb.mean(axis=2);g=g/(g.std()+10) |
| 26 | return np.concatenate([g.reshape(16,4,16,4).mean(axis=(1,3)).ravel(),alpha.reshape(16,4,16,4).mean(axis=(1,3)).ravel()*2]).astype(np.float32) |
| 27 |
| 28 | def make_candidates(): |
| 29 | global C |
| 30 | F=np.array([features(a) for a in RGBA]);TREE=cKDTree(F) |
| 31 | C=[[] for _ in range(NE)] |
| 32 | for t in [0,1,2,3,4,5,6,7]: |
| 33 | f=np.array([features(trans(a,t)) for a in RGBA]);_,js=TREE.query(f,k=9) |
| 34 | for j,row in enumerate(js): |
| 35 | for i in row: |
| 36 | if i!=j:C[i].append((j,t)) |
| 37 | # Add direct near candidates; mirror query inversion above is exact involution only for 0..4,7. |
| 38 | # Here the candidate is transformed j, queried against normal i, so all eight are correct. |
| 39 | return C |
| 40 |
| 41 | def metrics(a,o): |
| 42 | a=a.astype(np.float32)/255;o=o.astype(np.float32)/255 |
| 43 | ms=ss=p99=0. |
| 44 | for bg in [0,1]: |
| 45 | x=a[:,:,:3]*a[:,:,3:]+bg*(1-a[:,:,3:]);y=o[:,:,:3]*o[:,:,3:]+bg*(1-o[:,:,3:]);err=(x-y)**2 |
| 46 | ms+=float(np.mean(err));p99+=float(np.quantile(err,.99)) |
| 47 | mx=uniform_filter(x,size=(7,7,1));my=uniform_filter(y,size=(7,7,1));vx=uniform_filter(x*x,size=(7,7,1))-mx*mx;vy=uniform_filter(y*y,size=(7,7,1))-my*my;cv=uniform_filter(x*y,size=(7,7,1))-mx*my |
| 48 | ss+=float(np.mean(((2*mx*my+.0001)*(2*cv+.0009))/((mx*mx+my*my+.0001)*(vx+vy+.0009)))) |
| 49 | return ms/2,ss/2,p99/2 |
| 50 |
| 51 | def fit(i,j,t): |
| 52 | ix=trans(IDX[j],t);n=len(R[j]['pal']);a=ALPHA[i].astype(np.float32)/255;o=O[i].astype(np.float32);ao=o[:,:,3]/255 |
| 53 | weight=a*a;wanted=np.zeros((64,64,3),np.float32);valid=a>0 |
| 54 | wanted[valid]=(o[valid,:3]*ao[valid,None]+127.5*(a[valid,None]-ao[valid,None]))/a[valid,None] |
| 55 | den=np.bincount(ix.ravel(),weight.ravel(),minlength=n) |
| 56 | pal=np.array([np.bincount(ix.ravel(),(wanted[:,:,c]*weight).ravel(),minlength=n)/np.maximum(den,1e-6) for c in range(3)]).T |
| 57 | pal=np.clip(np.round(pal),0,255).astype(np.uint8) |
| 58 | output=np.empty((64,64,4),np.uint8);output[:,:,:3]=pal[ix];output[:,:,3]=ALPHA[i] |
| 59 | return output,pal |
| 60 |
| 61 | def template_job(i): |
| 62 | if R[i]['mask']==65535:return [],None |
| 63 | ref=metrics(RGBA[i],O[i]);options=[] |
| 64 | for j,t in C[i]: |
| 65 | if len(R[j]['pal'])>150:continue |
| 66 | a,pal=fit(i,j,t);met=metrics(a,O[i]);good=(met[0]<=ref[0] and met[1]>=ref[1] and met[2]<=ref[2]) |
| 67 | if good:options.append((len(pal)*3,j,t,pal,met)) |
| 68 | options.sort(key=lambda x:x[0]);return options[:8],ref |
| 69 |
| 70 | def half_features(a): |
| 71 | alpha=a[:,:,3:]/255.;g=(a[:,:,:3]*alpha).mean(axis=2);g/=g.std()+10 |
| 72 | return np.concatenate([g.reshape(16,4,8,4).mean(axis=(1,3)).ravel(),alpha.reshape(16,4,8,4).mean(axis=(1,3)).ravel()*2]).astype(np.float32) |
| 73 | def make_half_candidates(): |
| 74 | global HC |
| 75 | HC=[[[] for _ in range(NE)] for _ in range(2)] |
| 76 | for side in [0,1]: |
| 77 | target=np.array([half_features(a[:,side*32:(side+1)*32]) for a in RGBA]);tree=cKDTree(target) |
| 78 | for t in [0,1]: |
| 79 | f=np.array([half_features(trans(a,t)[:,side*32:(side+1)*32]) for a in RGBA]);_,js=tree.query(f,k=12) |
| 80 | for j,row in enumerate(js): |
| 81 | for i in row: |
| 82 | if i!=j:HC[side][i].append((j,t)) |
| 83 | return HC |
| 84 |
| 85 | def fit_half(i,j,t,side): |
| 86 | x=side*32;ix=trans(IDX[j],t)[:,x:x+32];n=len(R[j]['pal']);a=ALPHA[i,:,x:x+32].astype(np.float32)/255;o=O[i,:,x:x+32].astype(np.float32);ao=o[:,:,3]/255 |
| 87 | wt=a*a;valid=a>0;wanted=np.zeros((64,32,3),np.float32) |
| 88 | wanted[valid]=(o[valid,:3]*ao[valid,None]+127.5*(a[valid,None]-ao[valid,None]))/a[valid,None] |
| 89 | den=np.bincount(ix.ravel(),wt.ravel(),minlength=n) |
| 90 | pal=np.array([np.bincount(ix.ravel(),(wanted[:,:,c]*wt).ravel(),minlength=n)/np.maximum(den,1e-6) for c in range(3)]).T |
| 91 | pal=np.clip(np.round(pal),0,255).astype(np.uint8) |
| 92 | out=np.empty((64,32,4),np.uint8);out[:,:,:3]=pal[ix];out[:,:,3]=ALPHA[i,:,x:x+32] |
| 93 | return out,pal |
| 94 |
| 95 | def half_template_job(i): |
| 96 | if R[i]['mask']==65535:return [] |
| 97 | halves=[] |
| 98 | for side in [0,1]: |
| 99 | x=side*32;original=O[i,:,x:x+32];ref=metrics(RGBA[i,:,x:x+32],original);opts=[] |
| 100 | for j,t in HC[side][i]: |
| 101 | if len(R[j]['pal'])>150:continue |
| 102 | out,pal=fit_half(i,j,t,side);met=metrics(out,original) |
| 103 | if met[0]<=ref[0] and met[1]>=ref[1] and met[2]<=ref[2]:opts.append((len(np.unique(pal,axis=0)),j,t,pal,met)) |
| 104 | opts.sort(key=lambda x:x[0]);halves.append(opts[:4]) |
| 105 | if not halves[0] or not halves[1]:return [] |
| 106 | options=[] |
| 107 | for l in halves[0]: |
| 108 | for r in halves[1]: |
| 109 | pal,inv=np.unique(np.concatenate([l[3],r[3]]),axis=0,return_inverse=True) |
| 110 | if len(pal)>256:continue |
| 111 | raw=inv.astype(np.uint8).tobytes()+bytes(4096);zz=zlib.compress(raw,9) |
| 112 | options.append((len(pal)*3+len(zz),l[1],r[1],l[2],r[2],pal,zz,l[4],r[4])) |
| 113 | return sorted(options,key=lambda x:x[0])[:6] |
| 114 |
| 115 | def webp(a): |
| 116 | b=io.BytesIO();Image.fromarray(a).save(b,format='WEBP',lossless=True,quality=100,method=6,exact=True);return b.getvalue() |
| 117 |
| 118 | def mirror_job(i): |
| 119 | a=IDX[i];visible=ALPHA[i]>0 if i<NE and R[i]['mask']!=65535 else np.ones((64,64),bool) |
| 120 | cand=set(C[i]) if i<NE else set() |
| 121 | cand.update((x[1],t) for x in D[i][1] for t in range(8)) |
| 122 | options=[] |
| 123 | for j,t in cand: |
| 124 | base=trans(IDX[j],t);nb=len(R[j]['pal']) |
| 125 | cnt=np.bincount((base[visible].astype(np.int32)*256+a[visible]).ravel(),minlength=65536).reshape(256,256) |
| 126 | lut=cnt.argmax(axis=1).astype(np.uint8)[:nb];p=lut[base] |
| 127 | for mode in [1,2]: |
| 128 | delta=(a^p) if mode==1 else (a-p);delta[~visible]=0 |
| 129 | zz=zlib.compress(lut.tobytes()+delta.tobytes(),9) |
| 130 | options.append((len(zz),j,t,mode,zz,lut.tobytes(),delta)) |
| 131 | options.sort(key=lambda x:x[0]);out=[] |
| 132 | for _,j,t,mode,zz,lut,delta in options[:6]: |
| 133 | wb=webp(delta) |
| 134 | out.append(dict(parent=j,transform=t,pred=mode,zlib=zz,webp=wb,lut=lut)) |
| 135 | return out |
| 136 |
| 137 | def native_webp_job(i): |
| 138 | d=R[i];src=O[i].copy();ref=d['rgba'].copy() |
| 139 | if d['mask']!=65535:ref[:,:,3]=R[M[d['mask']]]['idx'] |
| 140 | # Keep alpha exactly as current reference. Recover source RGB gradients before WebP. |
| 141 | src[:,:,3]=ref[:,:,3] |
| 142 | m=src[:,:,3]<16 |
| 143 | if np.any(~m): |
| 144 | iy,ix=distance_transform_edt(m,return_distances=False,return_indices=True);src[m,:3]=src[iy[m],ix[m],:3] |
| 145 | encode=src.copy() |
| 146 | if d['mask']!=65535:encode[:,:,3]=255 |
| 147 | rr=metrics(ref,O[i]);out=[];chosen=None |
| 148 | for q in [10,20,30,40,50,60,70,80,90,95,100]: |
| 149 | b=io.BytesIO();Image.fromarray(encode).save(b,format='WEBP',quality=q,method=6,alpha_quality=100);b=b.getvalue();a=np.array(Image.open(io.BytesIO(b)).convert('RGBA'));a[:,:,3]=ref[:,:,3] |
| 150 | sc=metrics(a,O[i]);good=sc[0]<=rr[0] and sc[1]>=rr[1] and sc[2]<=rr[2] |
| 151 | out.append((q,len(b),sc,good)) |
| 152 | if good: |
| 153 | chosen=(q,b,sc);break |
| 154 | return chosen,rr,out |
| 155 | def error(a,o): |
| 156 | a=a.astype(np.float32)/255;o=o.astype(np.float32)/255 |
| 157 | da=a[:,:,3:]-o[:,:,3:];rgb=a[:,:,:3]*a[:,:,3:]-o[:,:,:3]*o[:,:,3:] |
| 158 | return ((rgb*rgb+(rgb-da)*(rgb-da))/2).mean(axis=2) |
| 159 |
| 160 | def structure(a,o): |
| 161 | a=a.astype(np.float32)/255;o=o.astype(np.float32)/255;s=0 |
| 162 | for bg in [0,1]: |
| 163 | x=a[:,:,:3]*a[:,:,3:]+bg*(1-a[:,:,3:]);y=o[:,:,:3]*o[:,:,3:]+bg*(1-o[:,:,3:]) |
| 164 | mx=uniform_filter(x,size=(5,5,1));my=uniform_filter(y,size=(5,5,1));vx=uniform_filter(x*x,size=(5,5,1))-mx*mx;vy=uniform_filter(y*y,size=(5,5,1))-my*my;cv=uniform_filter(x*y,size=(5,5,1))-mx*my |
| 165 | s+=(((2*mx*my+.0001)*(2*cv+.0009))/((mx*mx+my*my+.0001)*(vx+vy+.0009))).mean(axis=2) |
| 166 | return s/2 |
| 167 |
| 168 |
| 169 |
| 170 | def passes_global(image, original, reference_metrics): |
| 171 | m = metrics(image, original) |
| 172 | return m[0] <= reference_metrics[0] and m[1] >= reference_metrics[1] and m[2] <= reference_metrics[2] |
| 173 |
| 174 |
| 175 | def passes_local(image, original, reference_error, reference_structure): |
| 176 | e = uniform_filter(error(image, original), size=5) |
| 177 | s = structure(image, original) |
| 178 | return bool(np.all(e <= np.maximum(reference_error * 3, (8 / 255) ** 2)) |
| 179 | and np.all(s >= reference_structure - 0.1)) |
| 180 |