mirror of
https://github.com/XiaoDaiGua-Ray/ray-template.git
synced 2026-11-18 00:23:36 +08:00
1 line
9.8 KiB
JavaScript
1 line
9.8 KiB
JavaScript
var e=256,t=e-1,n=4,r=16,i=1<<r,a=10;1<<a;var o=10,s=i>>o,c=i<<a-o,l=e>>3,u=6,d=l*(1<<u),f=30,p=10,m=1<<p,h=8,g=1<<h,_=1<<p+h,v=499,y=491,b=487,x=503,S=3*x;function C(l,p){var h,C,w,T,E;function D(){h=[],C=new Int32Array(256),w=new Int32Array(e),T=new Int32Array(e),E=new Int32Array(e>>3);for(var t=0,r;t<e;t++)r=(t<<n+8)/e,h[t]=new Float64Array([r,r,r,0]),T[t]=i/e,w[t]=0}function O(){for(var t=0;t<e;t++)h[t][0]>>=n,h[t][1]>>=n,h[t][2]>>=n,h[t][3]=t}function k(e,t,n,r,i){h[t][0]-=e*(h[t][0]-n)/m,h[t][1]-=e*(h[t][1]-r)/m,h[t][2]-=e*(h[t][2]-i)/m}function A(t,n,r,i,a){for(var o=Math.abs(n-t),s=Math.min(n+t,e),c=n+1,l=n-1,u=1,d,f;c<s||l>o;)f=E[u++],c<s&&(d=h[c++],d[0]-=f*(d[0]-r)/_,d[1]-=f*(d[1]-i)/_,d[2]-=f*(d[2]-a)/_),l>o&&(d=h[l--],d[0]-=f*(d[0]-r)/_,d[1]-=f*(d[1]-i)/_,d[2]-=f*(d[2]-a)/_)}function j(t,i,l){for(var u=2147483647,d=u,f=-1,p=f,m=0,g,_,v,y;m<e;m++)g=h[m],_=Math.abs(g[0]-t)+Math.abs(g[1]-i)+Math.abs(g[2]-l),_<u&&(u=_,f=m),v=_-(w[m]>>r-n),v<d&&(d=v,p=m),y=T[m]>>o,T[m]-=y,w[m]+=y<<a;return T[f]+=s,w[f]-=c,p}function M(){var n,r,i,a,o,s,c=0,l=0;for(n=0;n<e;n++){for(i=h[n],o=n,s=i[1],r=n+1;r<e;r++)a=h[r],a[1]<s&&(o=r,s=a[1]);if(a=h[o],n!=o&&(r=a[0],a[0]=i[0],i[0]=r,r=a[1],a[1]=i[1],i[1]=r,r=a[2],a[2]=i[2],i[2]=r,r=a[3],a[3]=i[3],i[3]=r),s!=c){for(C[c]=l+n>>1,r=c+1;r<s;r++)C[r]=n;c=s,l=n}}for(C[c]=l+t>>1,r=c+1;r<256;r++)C[r]=t}function N(t,n,r){for(var i,a,o,s=1e3,c=-1,l=C[n],u=l-1;l<e||u>=0;)l<e&&(a=h[l],o=a[1]-n,o>=s?l=e:(l++,o<0&&(o=-o),i=a[0]-t,i<0&&(i=-i),o+=i,o<s&&(i=a[2]-r,i<0&&(i=-i),o+=i,o<s&&(s=o,c=a[3])))),u>=0&&(a=h[u],o=n-a[1],o>=s?u=-1:(u--,o<0&&(o=-o),i=a[0]-t,i<0&&(i=-i),o+=i,o<s&&(i=a[2]-r,i<0&&(i=-i),o+=i,o<s&&(s=o,c=a[3]))));return c}function P(){var e,t=l.length,r=30+(p-1)/3,i=t/(3*p),a=~~(i/100),o=m,s=d,c=s>>u;for(c<=1&&(c=0),e=0;e<c;e++)E[e]=o*((c*c-e*e)*g/(c*c));var h;t<S?(p=1,h=3):h=t%v===0?t%y===0?t%b===0?3*x:3*b:3*y:3*v;var _,C,w,T,D=0;for(e=0;e<i;)if(_=(l[D]&255)<<n,C=(l[D+1]&255)<<n,w=(l[D+2]&255)<<n,T=j(_,C,w),k(o,T,_,C,w),c!==0&&A(c,T,_,C,w),D+=h,D>=t&&(D-=t),e++,a===0&&(a=1),e%a===0)for(o-=o/r,s-=s/f,c=s>>u,c<=1&&(c=0),T=0;T<c;T++)E[T]=o*((c*c-T*T)*g/(c*c))}function F(){D(),P(),O(),M()}this.buildColormap=F;function I(){for(var t=[],n=[],r=0;r<e;r++)n[h[r][3]]=r;for(var i=0,a=0;a<e;a++){var o=n[a];t[i++]=h[o][0],t[i++]=h[o][1],t[i++]=h[o][2]}return t}this.getColormap=I,this.lookupRGB=N}var w=-1,T=12,E=5003,D=[0,1,3,7,15,31,63,127,255,511,1023,2047,4095,8191,16383,32767,65535];function O(e,t,n,r){var i=Math.max(2,r),a=new Uint8Array(256),o=new Int32Array(E),s=new Int32Array(E),c,l=0,u,d=0,f,p=!1,m,h,g,_,v,y;function b(e,t){a[u++]=e,u>=254&&k(t)}function x(e){S(E),d=h+2,p=!0,M(h,e)}function S(e){for(var t=0;t<e;++t)o[t]=-1}function C(e,t){var n,r,i,a,c,l,_;for(m=e,p=!1,y=m,f=A(y),h=1<<e-1,g=h+1,d=h+2,u=0,a=j(),_=0,n=E;n<65536;n*=2)++_;_=8-_,l=E,S(l),M(h,t);outer_loop:for(;(r=j())!=w;)if(n=(r<<T)+a,i=r<<_^a,o[i]===n)a=s[i];else{if(o[i]>=0){c=l-i,i===0&&(c=1);do if((i-=c)<0&&(i+=l),o[i]===n){a=s[i];continue outer_loop}while(o[i]>=0)}M(a,t),a=r,d<4096?(s[i]=d++,o[i]=n):x(t)}M(a,t),M(g,t)}function O(n){n.writeByte(i),_=e*t,v=0,C(i+1,n),n.writeByte(0)}function k(e){u>0&&(e.writeByte(u),e.writeBytes(a,0,u),u=0)}function A(e){return(1<<e)-1}function j(){return _===0?w:(--_,n[v++]&255)}function M(e,t){for(c&=D[l],l>0?c|=e<<l:c=e,l+=y;l>=8;)b(c&255,t),c>>=8,l-=8;if((d>f||p)&&(p?(f=A(y=m),p=!1):(++y,f=y==T?4096:A(y))),e==g){for(;l>0;)b(c&255,t),c>>=8,l-=8;k(t)}}this.encode=O}function k(){this.page=-1,this.pages=[],this.newPage()}k.pageSize=4096,k.charMap={};for(var A=0;A<256;A++)k.charMap[A]=String.fromCharCode(A);k.prototype.newPage=function(){this.pages[++this.page]=new Uint8Array(k.pageSize),this.cursor=0},k.prototype.getData=function(){for(var e=``,t=0;t<this.pages.length;t++)for(var n=0;n<k.pageSize;n++)e+=k.charMap[this.pages[t][n]];return e},k.prototype.toFlattenUint8Array=function(){let e=[];for(var t=0;t<this.pages.length;t++)if(t===this.pages.length-1){let n=Uint8Array.from(this.pages[t].slice(0,this.cursor));e.push(n)}else e.push(this.pages[t]);let n=new Uint8Array(e.reduce((e,t)=>e+t.length,0));return e.reduce((e,t)=>(n.set(t,e),e+t.length),0),n},k.prototype.writeByte=function(e){this.cursor>=k.pageSize&&this.newPage(),this.pages[this.page][this.cursor++]=e},k.prototype.writeUTFBytes=function(e){for(var t=e.length,n=0;n<t;n++)this.writeByte(e.charCodeAt(n))},k.prototype.writeBytes=function(e,t,n){for(var r=n||e.length,i=t||0;i<r;i++)this.writeByte(e[i])};function j(e,t){this.width=~~e,this.height=~~t,this.transparent=null,this.transIndex=0,this.repeat=-1,this.delay=0,this.image=null,this.pixels=null,this.indexedPixels=null,this.colorDepth=null,this.colorTab=null,this.neuQuant=null,this.usedEntry=[],this.palSize=7,this.dispose=-1,this.firstFrame=!0,this.sample=10,this.dither=!1,this.globalPalette=!1,this.out=new k}j.prototype.setDelay=function(e){this.delay=Math.round(e/10)},j.prototype.setFrameRate=function(e){this.delay=Math.round(100/e)},j.prototype.setDispose=function(e){e>=0&&(this.dispose=e)},j.prototype.setRepeat=function(e){this.repeat=e},j.prototype.setTransparent=function(e){this.transparent=e},j.prototype.addFrame=function(e){this.image=e,this.colorTab=this.globalPalette&&this.globalPalette.slice?this.globalPalette:null,this.getImagePixels(),this.analyzePixels(),this.globalPalette===!0&&(this.globalPalette=this.colorTab),this.firstFrame&&(this.writeHeader(),this.writeLSD(),this.writePalette(),this.repeat>=0&&this.writeNetscapeExt()),this.writeGraphicCtrlExt(),this.writeImageDesc(),!this.firstFrame&&!this.globalPalette&&this.writePalette(),this.writePixels(),this.firstFrame=!1},j.prototype.finish=function(){this.out.writeByte(59)},j.prototype.setQuality=function(e){e<1&&(e=1),this.sample=e},j.prototype.setDither=function(e){e===!0&&(e=`FloydSteinberg`),this.dither=e},j.prototype.setGlobalPalette=function(e){this.globalPalette=e},j.prototype.getGlobalPalette=function(){return this.globalPalette&&this.globalPalette.slice&&this.globalPalette.slice(0)||this.globalPalette},j.prototype.writeHeader=function(){this.out.writeUTFBytes(`GIF89a`)},j.prototype.analyzePixels=function(){this.colorTab||=(this.neuQuant=new C(this.pixels,this.sample),this.neuQuant.buildColormap(),this.neuQuant.getColormap()),this.dither?this.ditherPixels(this.dither.replace(`-serpentine`,``),this.dither.match(/-serpentine/)!==null):this.indexPixels(),this.pixels=null,this.colorDepth=8,this.palSize=7,this.transparent!==null&&(this.transIndex=this.findClosest(this.transparent,!0))},j.prototype.indexPixels=function(e){var t=this.pixels.length/3;this.indexedPixels=new Uint8Array(t);for(var n=0,r=0;r<t;r++){var i=this.findClosestRGB(this.pixels[n++]&255,this.pixels[n++]&255,this.pixels[n++]&255);this.usedEntry[i]=!0,this.indexedPixels[r]=i}},j.prototype.ditherPixels=function(e,t){var n={FalseFloydSteinberg:[[3/8,1,0],[3/8,0,1],[2/8,1,1]],FloydSteinberg:[[7/16,1,0],[3/16,-1,1],[5/16,0,1],[1/16,1,1]],Stucki:[[8/42,1,0],[4/42,2,0],[2/42,-2,1],[4/42,-1,1],[8/42,0,1],[4/42,1,1],[2/42,2,1],[1/42,-2,2],[2/42,-1,2],[4/42,0,2],[2/42,1,2],[1/42,2,2]],Atkinson:[[1/8,1,0],[1/8,2,0],[1/8,-1,1],[1/8,0,1],[1/8,1,1],[1/8,0,2]]};if(!e||!n[e])throw`Unknown dithering kernel: `+e;var r=n[e],i=0,a=this.height,o=this.width,s=this.pixels,c=t?-1:1;this.indexedPixels=new Uint8Array(this.pixels.length/3);for(var l=0;l<a;l++){t&&(c*=-1);for(var u=c==1?0:o-1,d=c==1?o:0;u!==d;u+=c){i=l*o+u;var f=i*3,p=s[f],m=s[f+1],h=s[f+2];f=this.findClosestRGB(p,m,h),this.usedEntry[f]=!0,this.indexedPixels[i]=f,f*=3;for(var g=this.colorTab[f],_=this.colorTab[f+1],v=this.colorTab[f+2],y=p-g,b=m-_,x=h-v,S=c==1?0:r.length-1,C=c==1?r.length:0;S!==C;S+=c){var w=r[S][1],T=r[S][2];if(w+u>=0&&w+u<o&&T+l>=0&&T+l<a){var E=r[S][0];f=i+w+T*o,f*=3,s[f]=Math.max(0,Math.min(255,s[f]+y*E)),s[f+1]=Math.max(0,Math.min(255,s[f+1]+b*E)),s[f+2]=Math.max(0,Math.min(255,s[f+2]+x*E))}}}}},j.prototype.findClosest=function(e,t){return this.findClosestRGB((e&16711680)>>16,(e&65280)>>8,e&255,t)},j.prototype.findClosestRGB=function(e,t,n,r){if(this.colorTab===null)return-1;if(this.neuQuant&&!r)return this.neuQuant.lookupRGB(e,t,n);n|t<<8|e<<16;for(var i=0,a=16777216,o=this.colorTab.length,s=0,c=0;s<o;c++){var l=e-(this.colorTab[s++]&255),u=t-(this.colorTab[s++]&255),d=n-(this.colorTab[s++]&255),f=l*l+u*u+d*d;(!r||this.usedEntry[c])&&f<a&&(a=f,i=c)}return i},j.prototype.getImagePixels=function(){var e=this.width,t=this.height;this.pixels=new Uint8Array(e*t*3);for(var n=this.image,r=0,i=0,a=0;a<t;a++)for(var o=0;o<e;o++)this.pixels[i++]=n[r++],this.pixels[i++]=n[r++],this.pixels[i++]=n[r++],r++},j.prototype.writeGraphicCtrlExt=function(){this.out.writeByte(33),this.out.writeByte(249),this.out.writeByte(4);var e,t;this.transparent===null?(e=0,t=0):(e=1,t=2),this.dispose>=0&&(t=this.dispose&7),t<<=2,this.out.writeByte(t|0|e),this.writeShort(this.delay),this.out.writeByte(this.transIndex),this.out.writeByte(0)},j.prototype.writeImageDesc=function(){this.out.writeByte(44),this.writeShort(0),this.writeShort(0),this.writeShort(this.width),this.writeShort(this.height),this.firstFrame||this.globalPalette?this.out.writeByte(0):this.out.writeByte(128|this.palSize)},j.prototype.writeLSD=function(){this.writeShort(this.width),this.writeShort(this.height),this.out.writeByte(240|this.palSize),this.out.writeByte(0),this.out.writeByte(0)},j.prototype.writeNetscapeExt=function(){this.out.writeByte(33),this.out.writeByte(255),this.out.writeByte(11),this.out.writeUTFBytes(`NETSCAPE2.0`),this.out.writeByte(3),this.out.writeByte(1),this.writeShort(this.repeat),this.out.writeByte(0)},j.prototype.writePalette=function(){this.out.writeBytes(this.colorTab);for(var e=768-this.colorTab.length,t=0;t<e;t++)this.out.writeByte(0)},j.prototype.writeShort=function(e){this.out.writeByte(e&255),this.out.writeByte(e>>8&255)},j.prototype.writePixels=function(){new O(this.width,this.height,this.indexedPixels,this.colorDepth).encode(this.out)},j.prototype.stream=function(){return this.out};export{j as default}; |