utif.module.js 102 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579
  1. ;(function(){
  2. var UTIF = {};
  3. // Make available for import by `require()`
  4. if (typeof module == "object") {module.exports = UTIF;}
  5. else {self.UTIF = UTIF;}
  6. var pako = (typeof require === "function") ? require("pako") : self.pako;
  7. function log() { if (typeof process=="undefined" || process.env.NODE_ENV=="development") console.log.apply(console, arguments); }
  8. (function(UTIF, pako){
  9. // Following lines add a JPEG decoder to UTIF.JpegDecoder
  10. (function(){"use strict";var W=function a1(){function W(p){this.message="JPEG error: "+p}W.prototype=new Error;W.prototype.name="JpegError";W.constructor=W;return W}(),ak=function ag(){var p=new Uint8Array([0,1,8,16,9,2,3,10,17,24,32,25,18,11,4,5,12,19,26,33,40,48,41,34,27,20,13,6,7,14,21,28,35,42,49,56,57,50,43,36,29,22,15,23,30,37,44,51,58,59,52,45,38,31,39,46,53,60,61,54,47,55,62,63]),t=4017,ac=799,ah=3406,ao=2276,ar=1567,ai=3784,s=5793,ad=2896;function ak(Q){if(Q==null)Q={};if(Q.w==null)Q.w=-1;this.V=Q.n;this.N=Q.w}function a5(Q,h){var f=0,G=[],n,E,a=16,F;while(a>0&&!Q[a-1]){a--}G.push({children:[],index:0});var C=G[0];for(n=0;n<a;n++)
  11. {for(E=0;E<Q[n];E++){C=G.pop();C.children[C.index]=h[f];while(C.index>0){C=G.pop()}C.index++;G.push(C);while(G.length<=n){G.push(F={children:[],index:0});C.children[C.index]=F.children;C=F}f++}if(n+1<a){G.push(F={children:[],index:0});C.children[C.index]=F.children;C=F}}return G[0].children}function a2(Q,h,f){return 64*((Q.P+1)*h+f)}function a7(Q,h,f,G,n,E,a,C,F,d){if(d==null)d=!1;var T=f.m,U=f.Z,z=h,J=0,V=0,r=0,D=0,a8,q=0,X,O,_,N,e,K,x=0,k,g,R,c;function Y(){if(V>0){V--;return J>>V&1}J=Q[h++];if(J===255){var I=Q[h++];if(I){if(I===220&&d){h+=2;var l=Z(Q,h);h+=2;if(l>0&&l!==f.s){throw new DNLMarkerError("Found DNL marker (0xFFDC) while parsing scan data",l)}}else if(I===217){if(d){var M=q*8;
  12. if(M>0&&M<f.s/10){throw new DNLMarkerError("Found EOI marker (0xFFD9) while parsing scan data, "+"possibly caused by incorrect `scanLines` parameter",M)}}throw new EOIMarkerError("Found EOI marker (0xFFD9) while parsing scan data")}throw new W("unexpected marker")}}V=7;return J>>>7}function u(I){var l=I;while(!0){l=l[Y()];switch(typeof l){case"number":return l;case"object":continue}throw new W("invalid huffman sequence")}}function m(I){var e=0;while(I>0){e=e<<1|Y();I--}return e}function j(I){if(I===1){return Y()===1?1:-1}var e=m(I);if(e>=1<<I-1){return e}return e+(-1<<I)+1}function v(X,I){var l=u(X.J),M=l===0?0:j(l),N=1;
  13. X.D[I]=X.Q+=M;while(N<64){var S=u(X.i),i=S&15,A=S>>4;if(i===0){if(A<15){break}N+=16;continue}N+=A;var o=p[N];X.D[I+o]=j(i);N++}}function $(X,I){var l=u(X.J),M=l===0?0:j(l)<<F;X.D[I]=X.Q+=M}function b(X,I){X.D[I]|=Y()<<F}function P(X,I){if(r>0){r--;return}var N=E,l=a;while(N<=l){var M=u(X.i),S=M&15,i=M>>4;if(S===0){if(i<15){r=m(i)+(1<<i)-1;break}N+=16;continue}N+=i;var A=p[N];X.D[I+A]=j(S)*(1<<F);N++}}function a4(X,I){var N=E,l=a,M=0,S,i;while(N<=l){var A=I+p[N],o=X.D[A]<0?-1:1;switch(D){case 0:i=u(X.i);S=i&15;M=i>>4;if(S===0){if(M<15){r=m(M)+(1<<M);D=4}else{M=16;D=1}}else{if(S!==1){throw new W("invalid ACn encoding")}a8=j(S);D=M?2:3}continue;case 1:case 2:if(X.D[A]){X.D[A]+=o*(Y()<<F)}else{M--;if(M===0){D=D===2?3:0}}break;case 3:if(X.D[A]){X.D[A]+=o*(Y()<<F)}else{X.D[A]=a8<<F;
  14. D=0}break;case 4:if(X.D[A]){X.D[A]+=o*(Y()<<F)}break}N++}if(D===4){r--;if(r===0){D=0}}}function H(X,I,x,l,M){var S=x/T|0,i=x%T;q=S*X.A+l;var A=i*X.h+M,o=a2(X,q,A);I(X,o)}function w(X,I,x){q=x/X.P|0;var l=x%X.P,M=a2(X,q,l);I(X,M)}var y=G.length;if(U){if(E===0){K=C===0?$:b}else{K=C===0?P:a4}}else{K=v}if(y===1){g=G[0].P*G[0].c}else{g=T*f.R}while(x<=g){var L=n?Math.min(g-x,n):g;if(L>0){for(O=0;O<y;O++){G[O].Q=0}r=0;if(y===1){X=G[0];for(e=0;e<L;e++){w(X,K,x);x++}}else{for(e=0;e<L;
  15. e++){for(O=0;O<y;O++){X=G[O];R=X.h;c=X.A;for(_=0;_<c;_++){for(N=0;N<R;N++){H(X,K,x,_,N)}}}x++}}}V=0;k=an(Q,h);if(!k){break}if(k.u){var a6=L>0?"unexpected":"excessive";h=k.offset}if(k.M>=65488&&k.M<=65495){h+=2}else{break}}return h-z}function al(Q,h,f){var G=Q.$,n=Q.D,E,a,C,F,d,T,U,z,J,V,Y,u,m,j,v,$,b;if(!G){throw new W("missing required Quantization Table.")}for(var r=0;r<64;r+=8){J=n[h+r];V=n[h+r+1];Y=n[h+r+2];u=n[h+r+3];m=n[h+r+4];j=n[h+r+5];v=n[h+r+6];$=n[h+r+7];J*=G[r];if((V|Y|u|m|j|v|$)===0){b=s*J+512>>10;f[r]=b;f[r+1]=b;f[r+2]=b;f[r+3]=b;f[r+4]=b;f[r+5]=b;f[r+6]=b;f[r+7]=b;continue}V*=G[r+1];Y*=G[r+2];u*=G[r+3];m*=G[r+4];j*=G[r+5];v*=G[r+6];$*=G[r+7];E=s*J+128>>8;a=s*m+128>>8;C=Y;F=v;d=ad*(V-$)+128>>8;z=ad*(V+$)+128>>8;
  16. T=u<<4;U=j<<4;E=E+a+1>>1;a=E-a;b=C*ai+F*ar+128>>8;C=C*ar-F*ai+128>>8;F=b;d=d+U+1>>1;U=d-U;z=z+T+1>>1;T=z-T;E=E+F+1>>1;F=E-F;a=a+C+1>>1;C=a-C;b=d*ao+z*ah+2048>>12;d=d*ah-z*ao+2048>>12;z=b;b=T*ac+U*t+2048>>12;T=T*t-U*ac+2048>>12;U=b;f[r]=E+z;f[r+7]=E-z;f[r+1]=a+U;f[r+6]=a-U;f[r+2]=C+T;f[r+5]=C-T;f[r+3]=F+d;f[r+4]=F-d}for(var P=0;P<8;++P){J=f[P];V=f[P+8];Y=f[P+16];u=f[P+24];m=f[P+32];j=f[P+40];v=f[P+48];$=f[P+56];if((V|Y|u|m|j|v|$)===0){b=s*J+8192>>14;if(b<-2040){b=0}else if(b>=2024){b=255}else{b=b+2056>>4}n[h+P]=b;n[h+P+8]=b;n[h+P+16]=b;n[h+P+24]=b;n[h+P+32]=b;n[h+P+40]=b;n[h+P+48]=b;n[h+P+56]=b;continue}E=s*J+2048>>12;a=s*m+2048>>12;C=Y;F=v;d=ad*(V-$)+2048>>12;z=ad*(V+$)+2048>>12;T=u;U=j;E=(E+a+1>>1)+4112;a=E-a;b=C*ai+F*ar+2048>>12;C=C*ar-F*ai+2048>>12;F=b;d=d+U+1>>1;U=d-U;z=z+T+1>>1;T=z-T;E=E+F+1>>1;F=E-F;a=a+C+1>>1;C=a-C;b=d*ao+z*ah+2048>>12;d=d*ah-z*ao+2048>>12;z=b;
  17. b=T*ac+U*t+2048>>12;T=T*t-U*ac+2048>>12;U=b;J=E+z;$=E-z;V=a+U;v=a-U;Y=C+T;j=C-T;u=F+d;m=F-d;if(J<16){J=0}else if(J>=4080){J=255}else{J>>=4}if(V<16){V=0}else if(V>=4080){V=255}else{V>>=4}if(Y<16){Y=0}else if(Y>=4080){Y=255}else{Y>>=4}if(u<16){u=0}else if(u>=4080){u=255}else{u>>=4}if(m<16){m=0}else if(m>=4080){m=255}else{m>>=4}if(j<16){j=0}else if(j>=4080){j=255}else{j>>=4}if(v<16){v=0}else if(v>=4080){v=255}else{v>>=4}if($<16){$=0}else if($>=4080){$=255}else{$>>=4}n[h+P]=J;
  18. n[h+P+8]=V;n[h+P+16]=Y;n[h+P+24]=u;n[h+P+32]=m;n[h+P+40]=j;n[h+P+48]=v;n[h+P+56]=$}}function a0(Q,h){var f=h.P,G=h.c,n=new Int16Array(64);for(var E=0;E<G;E++){for(var a=0;a<f;a++){var C=a2(h,E,a);al(h,C,n)}}return h.D}function an(Q,h,f){if(f==null)f=h;var G=Q.length-1,n=f<h?f:h;if(h>=G){return null}var E=Z(Q,h);if(E>=65472&&E<=65534){return{u:null,M:E,offset:h}}var a=Z(Q,n);while(!(a>=65472&&a<=65534)){if(++n>=G){return null}a=Z(Q,n)}return{u:E.toString(16),M:a,offset:n}}ak.prototype={parse(Q,h){if(h==null)h={};
  19. var f=h.F,E=0,a=null,C=null,F,d,T=0;function G(){var o=Z(Q,E);E+=2;var B=E+o-2,V=an(Q,B,E);if(V&&V.u){B=V.offset}var ab=Q.subarray(E,B);E+=ab.length;return ab}function n(F){var o=Math.ceil(F.o/8/F.X),B=Math.ceil(F.s/8/F.B);for(var Y=0;Y<F.W.length;Y++){R=F.W[Y];var ab=Math.ceil(Math.ceil(F.o/8)*R.h/F.X),af=Math.ceil(Math.ceil(F.s/8)*R.A/F.B),ap=o*R.h,aq=B*R.A,ae=64*aq*(ap+1);R.D=new Int16Array(ae);R.P=ab;R.c=af}F.m=o;F.R=B}var U=[],z=[],J=[],V=Z(Q,E);E+=2;if(V!==65496){throw new W("SOI not found")}V=Z(Q,E);
  20. E+=2;markerLoop:while(V!==65497){var Y,u,m;switch(V){case 65504:case 65505:case 65506:case 65507:case 65508:case 65509:case 65510:case 65511:case 65512:case 65513:case 65514:case 65515:case 65516:case 65517:case 65518:case 65519:case 65534:var j=G();if(V===65504){if(j[0]===74&&j[1]===70&&j[2]===73&&j[3]===70&&j[4]===0){a={version:{d:j[5],T:j[6]},K:j[7],j:j[8]<<8|j[9],H:j[10]<<8|j[11],S:j[12],I:j[13],C:j.subarray(14,14+3*j[12]*j[13])}}}if(V===65518){if(j[0]===65&&j[1]===100&&j[2]===111&&j[3]===98&&j[4]===101){C={version:j[5]<<8|j[6],k:j[7]<<8|j[8],q:j[9]<<8|j[10],a:j[11]}}}break;
  21. case 65499:var v=Z(Q,E),b;E+=2;var $=v+E-2;while(E<$){var r=Q[E++],P=new Uint16Array(64);if(r>>4===0){for(u=0;u<64;u++){b=p[u];P[b]=Q[E++]}}else if(r>>4===1){for(u=0;u<64;u++){b=p[u];P[b]=Z(Q,E);E+=2}}else{throw new W("DQT - invalid table spec")}U[r&15]=P}break;case 65472:case 65473:case 65474:if(F){throw new W("Only single frame JPEGs supported")}E+=2;F={};F.G=V===65473;F.Z=V===65474;F.precision=Q[E++];var D=Z(Q,E),a4,q=0,H=0;E+=2;F.s=f||D;F.o=Z(Q,E);E+=2;F.W=[];F._={};var a8=Q[E++];for(Y=0;Y<a8;Y++){a4=Q[E];var w=Q[E+1]>>4,y=Q[E+1]&15;if(q<w){q=w}if(H<y){H=y}var X=Q[E+2];m=F.W.push({h:w,A:y,L:X,$:null});F._[a4]=m-1;E+=3}F.X=q;F.B=H;n(F);break;case 65476:var O=Z(Q,E);E+=2;
  22. for(Y=2;Y<O;){var _=Q[E++],N=new Uint8Array(16),e=0;for(u=0;u<16;u++,E++){e+=N[u]=Q[E]}var K=new Uint8Array(e);for(u=0;u<e;u++,E++){K[u]=Q[E]}Y+=17+e;(_>>4===0?J:z)[_&15]=a5(N,K)}break;case 65501:E+=2;d=Z(Q,E);E+=2;break;case 65498:var x=++T===1&&!f,R;E+=2;var k=Q[E++],g=[];for(Y=0;Y<k;Y++){var c=Q[E++],L=F._[c];R=F.W[L];R.index=c;var a6=Q[E++];R.J=J[a6>>4];R.i=z[a6&15];g.push(R)}var I=Q[E++],l=Q[E++],M=Q[E++];try{var S=a7(Q,E,F,g,d,I,l,M>>4,M&15,x);E+=S}catch(ex){if(ex instanceof DNLMarkerError){return this.parse(Q,{F:ex.s})}else if(ex instanceof EOIMarkerError){break markerLoop}throw ex}break;case 65500:E+=4;break;case 65535:if(Q[E]!==255){E--}break;default:var i=an(Q,E-2,E-3);if(i&&i.u){E=i.offset;break}if(E>=Q.length-1){break markerLoop}throw new W("JpegImage.parse - unknown marker: "+V.toString(16))}V=Z(Q,E);E+=2}this.width=F.o;this.height=F.s;this.g=a;this.b=C;this.W=[];for(Y=0;Y<F.W.length;Y++){R=F.W[Y];
  23. var A=U[R.L];if(A){R.$=A}this.W.push({index:R.index,e:a0(F,R),l:R.h/F.X,t:R.A/F.B,P:R.P,c:R.c})}this.p=this.W.length;return undefined},Y(Q,h,f){if(f==null)f=!1;var G=this.width/Q,n=this.height/h,E,a,C,F,d,T,U,z,J,V,Y=0,u,m=this.W.length,j=Q*h*m,v=new Uint8ClampedArray(j),$=new Uint32Array(Q),b=4294967288,r;for(U=0;U<m;U++){E=this.W[U];a=E.l*G;C=E.t*n;Y=U;u=E.e;F=E.P+1<<3;if(a!==r){for(d=0;d<Q;d++){z=0|d*a;$[d]=(z&b)<<3|z&7}r=a}for(T=0;T<h;T++){z=0|T*C;V=F*(z&b)|(z&7)<<3;for(d=0;d<Q;d++){v[Y]=u[V+$[d]];Y+=m}}}var P=this.V;if(!f&&m===4&&!P){P=new Int32Array([-256,255,-256,255,-256,255,-256,255])}if(P){for(U=0;U<j;){for(z=0,J=0;z<m;z++,U++,J+=2){v[U]=(v[U]*P[J]>>8)+P[J+1]}}}return v},get f(){if(this.b){return!!this.b.a}if(this.p===3){if(this.N===0){return!1}else if(this.W[0].index===82&&this.W[1].index===71&&this.W[2].index===66){return!1}return!0}if(this.N===1){return!0}return!1},z:function aj(Q){var h,f,G;
  24. for(var n=0,E=Q.length;n<E;n+=3){h=Q[n];f=Q[n+1];G=Q[n+2];Q[n]=h-179.456+1.402*G;Q[n+1]=h+135.459-.344*f-.714*G;Q[n+2]=h-226.816+1.772*f}return Q},O:function aa(Q){var h,f,G,n,E=0;for(var a=0,C=Q.length;a<C;a+=4){h=Q[a];f=Q[a+1];G=Q[a+2];n=Q[a+3];Q[E++]=-122.67195406894+f*(-660635669420364e-19*f+.000437130475926232*G-54080610064599e-18*h+.00048449797120281*n-.154362151871126)+G*(-.000957964378445773*G+.000817076911346625*h-.00477271405408747*n+1.53380253221734)+h*(.000961250184130688*h-.00266257332283933*n+.48357088451265)+n*(-.000336197177618394*n+.484791561490776);
  25. Q[E++]=107.268039397724+f*(219927104525741e-19*f-.000640992018297945*G+.000659397001245577*h+.000426105652938837*n-.176491792462875)+G*(-.000778269941513683*G+.00130872261408275*h+.000770482631801132*n-.151051492775562)+h*(.00126935368114843*h-.00265090189010898*n+.25802910206845)+n*(-.000318913117588328*n-.213742400323665);Q[E++]=-20.810012546947+f*(-.000570115196973677*f-263409051004589e-19*G+.0020741088115012*h-.00288260236853442*n+.814272968359295)+G*(-153496057440975e-19*G-.000132689043961446*h+.000560833691242812*n-.195152027534049)+h*(.00174418132927582*h-.00255243321439347*n+.116935020465145)+n*(-.000343531996510555*n+.24165260232407)}return Q.subarray(0,E)},r:function a3(Q){var h,f,G;
  26. for(var n=0,E=Q.length;n<E;n+=4){h=Q[n];f=Q[n+1];G=Q[n+2];Q[n]=434.456-h-1.402*G;Q[n+1]=119.541-h+.344*f+.714*G;Q[n+2]=481.816-h-1.772*f}return Q},U:function as(Q){var h,f,G,n,E=0;for(var a=0,C=Q.length;a<C;a+=4){h=Q[a];f=Q[a+1];G=Q[a+2];n=Q[a+3];Q[E++]=255+h*(-6747147073602441e-20*h+.0008379262121013727*f+.0002894718188643294*G+.003264231057537806*n-1.1185611867203937)+f*(26374107616089404e-21*f-8626949158638572e-20*G-.0002748769067499491*n-.02155688794978967)+G*(-3878099212869363e-20*G-.0003267808279485286*n+.0686742238595345)-n*(.0003361971776183937*n+.7430659151342254);
  27. Q[E++]=255+h*(.00013596372813588848*h+.000924537132573585*f+.00010567359618683593*G+.0004791864687436512*n-.3109689587515875)+f*(-.00023545346108370344*f+.0002702845253534714*G+.0020200308977307156*n-.7488052167015494)+G*(6834815998235662e-20*G+.00015168452363460973*n-.09751927774728933)-n*(.0003189131175883281*n+.7364883807733168);Q[E++]=255+h*(13598650411385308e-21*h+.00012423956175490851*f+.0004751985097583589*G-36729317476630424e-22*n-.05562186980264034)+f*(.00016141380598724676*f+.0009692239130725186*G+.0007782692450036253*n-.44015232367526463)+G*(5.068882914068769e-7*G+.0017778369011375071*n-.7591454649749609)-n*(.0003435319965105553*n+.7063770186160144)}return Q.subarray(0,E)},getData:function(Q){var h=Q.width,f=Q.height,G=Q.forceRGB,n=Q.isSourcePDF;
  28. if(this.p>4){throw new W("Unsupported color mode")}var E=this.Y(h,f,n);if(this.p===1&&G){var a=E.length,C=new Uint8ClampedArray(a*3),F=0;for(var d=0;d<a;d++){var T=E[d];C[F++]=T;C[F++]=T;C[F++]=T}return C}else if(this.p===3&&this.f){return this.z(E)}else if(this.p===4){if(this.f){if(G){return this.O(E)}return this.r(E)}else if(G){return this.U(E)}}return E}};return ak}();function a9(p,t){return p[t]<<24>>24}function Z(p,t){return p[t]<<8|p[t+1]}function am(p,t){return(p[t]<<24|p[t+1]<<16|p[t+2]<<8|p[t+3])>>>0}UTIF.JpegDecoder=ak}());
  29. //UTIF.JpegDecoder = PDFJS.JpegImage;
  30. UTIF.encodeImage = function(rgba, w, h, metadata)
  31. {
  32. var idf = { "t256":[w], "t257":[h], "t258":[8,8,8,8], "t259":[1], "t262":[2], "t273":[1000], // strips offset
  33. "t277":[4], "t278":[h], /* rows per strip */ "t279":[w*h*4], // strip byte counts
  34. "t282":[[72,1]], "t283":[[72,1]], "t284":[1], "t286":[[0,1]], "t287":[[0,1]], "t296":[1], "t305": ["Photopea (UTIF.js)"], "t338":[1]
  35. };
  36. if (metadata) for (var i in metadata) idf[i] = metadata[i];
  37. var prfx = new Uint8Array(UTIF.encode([idf]));
  38. var img = new Uint8Array(rgba);
  39. var data = new Uint8Array(1000+w*h*4);
  40. for(var i=0; i<prfx.length; i++) data[i] = prfx[i];
  41. for(var i=0; i<img .length; i++) data[1000+i] = img[i];
  42. return data.buffer;
  43. }
  44. UTIF.encode = function(ifds)
  45. {
  46. var LE = false;
  47. var data = new Uint8Array(20000), offset = 4, bin = LE ? UTIF._binLE : UTIF._binBE;
  48. data[0]=data[1]=LE?73:77; bin.writeUshort(data,2,42);
  49. var ifdo = 8;
  50. bin.writeUint(data, offset, ifdo); offset+=4;
  51. for(var i=0; i<ifds.length; i++)
  52. {
  53. var noffs = UTIF._writeIFD(bin, UTIF._types.basic, data, ifdo, ifds[i]);
  54. ifdo = noffs[1];
  55. if(i<ifds.length-1) {
  56. if((ifdo&3)!=0) ifdo+=(4-(ifdo&3)); // make each IFD start at multiple of 4
  57. bin.writeUint(data, noffs[0], ifdo);
  58. }
  59. }
  60. return data.slice(0, ifdo).buffer;
  61. }
  62. UTIF.decode = function(buff, prm)
  63. {
  64. if(prm==null) prm = {parseMN:true, debug:false}; // read MakerNote, debug
  65. var data = new Uint8Array(buff), offset = 0;
  66. var id = UTIF._binBE.readASCII(data, offset, 2); offset+=2;
  67. var bin = id=="II" ? UTIF._binLE : UTIF._binBE;
  68. var num = bin.readUshort(data, offset); offset+=2;
  69. var ifdo = bin.readUint(data, offset); offset+=4;
  70. var ifds = [];
  71. while(true) {
  72. var cnt = bin.readUshort(data,ifdo), typ = bin.readUshort(data,ifdo+4); if(cnt!=0) if(typ<1 || 13<typ) { log("error in TIFF"); break };
  73. UTIF._readIFD(bin, data, ifdo, ifds, 0, prm);
  74. ifdo = bin.readUint(data, ifdo+2+cnt*12);
  75. if(ifdo==0) break;
  76. }
  77. return ifds;
  78. }
  79. UTIF.decodeImage = function(buff, img, ifds)
  80. {
  81. if(img.data) return;
  82. var data = new Uint8Array(buff);
  83. var id = UTIF._binBE.readASCII(data, 0, 2);
  84. if(img["t256"]==null) return; // No width => probably not an image
  85. img.isLE = id=="II";
  86. img.width = img["t256"][0]; //delete img["t256"];
  87. img.height = img["t257"][0]; //delete img["t257"];
  88. var cmpr = img["t259"] ? img["t259"][0] : 1; //delete img["t259"];
  89. var fo = img["t266"] ? img["t266"][0] : 1; //delete img["t266"];
  90. if(img["t284"] && img["t284"][0]==2) log("PlanarConfiguration 2 should not be used!");
  91. if(cmpr==7 && img["t258"] && img["t258"].length>3) img["t258"]=img["t258"].slice(0,3);
  92. var spp = img["t277"]?img["t277"][0]:1;
  93. var bps = img["t258"]?img["t258"][0]:1;
  94. var bipp = bps*spp; // bits per pixel
  95. /*
  96. var bipp; // bits per pixel
  97. if(img["t258"]) bipp = Math.min(32,img["t258"][0])*img["t258"].length;
  98. else bipp = (img["t277"]?img["t277"][0]:1);
  99. */
  100. // Some .NEF files have t258==14, even though they use 16 bits per pixel
  101. if(cmpr==1 && img["t279"]!=null && img["t278"] && img["t262"][0]==32803) {
  102. bipp = Math.round((img["t279"][0]*8)/(img.width*img["t278"][0]));
  103. }
  104. if(img["t50885"] && img["t50885"][0]==4) bipp = img["t258"][0]*3; // RAW_CANON_40D_SRAW_V103.CR2
  105. var bipl = Math.ceil(img.width*bipp/8)*8;
  106. var soff = img["t273"]; if(soff==null || img["t322"]) soff = img["t324"];
  107. var bcnt = img["t279"]; if(cmpr==1 && soff.length==1) bcnt = [img.height*(bipl>>>3)]; if(bcnt==null || img["t322"]) bcnt = img["t325"];
  108. //bcnt[0] = Math.min(bcnt[0], data.length); // Hasselblad, "RAW_HASSELBLAD_H3D39II.3FR"
  109. var bytes = new Uint8Array(img.height*(bipl>>>3)), bilen = 0;
  110. if(img["t322"]!=null) // tiled
  111. {
  112. var tw = img["t322"][0], th = img["t323"][0];
  113. var tx = Math.floor((img.width + tw - 1) / tw);
  114. var ty = Math.floor((img.height + th - 1) / th);
  115. var tbuff = new Uint8Array(Math.ceil(tw*th*bipp/8)|0);
  116. console.log("====", tx,ty);
  117. for(var y=0; y<ty; y++)
  118. for(var x=0; x<tx; x++)
  119. {
  120. var i = y*tx+x; tbuff.fill(0);
  121. UTIF.decode._decompress(img,ifds, data, soff[i], bcnt[i], cmpr, tbuff, 0, fo);
  122. // Might be required for 7 too. Need to check
  123. if (cmpr==6) bytes = tbuff;
  124. else UTIF._copyTile(tbuff, Math.ceil(tw*bipp/8)|0, th, bytes, Math.ceil(img.width*bipp/8)|0, img.height, Math.ceil(x*tw*bipp/8)|0, y*th);
  125. }
  126. bilen = bytes.length*8;
  127. }
  128. else // stripped
  129. {
  130. if(soff==null) return;
  131. var rps = img["t278"] ? img["t278"][0] : img.height; rps = Math.min(rps, img.height);
  132. console.log("====", img.width, rps);
  133. for(var i=0; i<soff.length; i++)
  134. {
  135. UTIF.decode._decompress(img,ifds, data, soff[i], bcnt[i], cmpr, bytes, Math.ceil(bilen/8)|0, fo);
  136. bilen += bipl * rps;
  137. }
  138. bilen = Math.min(bilen, bytes.length*8);
  139. }
  140. img.data = new Uint8Array(bytes.buffer, 0, Math.ceil(bilen/8)|0);
  141. }
  142. UTIF.decode._decompress = function(img,ifds, data, off, len, cmpr, tgt, toff, fo) // fill order
  143. {
  144. //console.log("compression", cmpr);
  145. //var time = Date.now();
  146. if(false) {}
  147. else if(cmpr==1/* || (len==tgt.length && cmpr!=32767)*/) for(var j=0; j<len; j++) tgt[toff+j] = data[off+j];
  148. else if(cmpr==2) UTIF.decode._decodeG2 (data, off, len, tgt, toff, img.width, fo);
  149. else if(cmpr==3) UTIF.decode._decodeG3 (data, off, len, tgt, toff, img.width, fo, img["t292"]?((img["t292"][0]&1)==1):false);
  150. else if(cmpr==4) UTIF.decode._decodeG4 (data, off, len, tgt, toff, img.width, fo);
  151. else if(cmpr==5) UTIF.decode._decodeLZW(data, off, len, tgt, toff,8);
  152. else if(cmpr==6) UTIF.decode._decodeOldJPEG(img, data, off, len, tgt, toff);
  153. else if(cmpr==7 || cmpr==34892) UTIF.decode._decodeNewJPEG(img, data, off, len, tgt, toff);
  154. else if(cmpr==8 || cmpr==32946) { var src = new Uint8Array(data.buffer,off+2,len-6); var bin = pako["inflateRaw"](src); tgt.set(bin,toff); }
  155. else if(cmpr==9) UTIF.decode._decodeVC5(data,off,len,tgt,toff);
  156. else if(cmpr==32767) UTIF.decode._decodeARW(img, data, off, len, tgt, toff);
  157. else if(cmpr==32773) UTIF.decode._decodePackBits(data, off, len, tgt, toff);
  158. else if(cmpr==32809) UTIF.decode._decodeThunder (data, off, len, tgt, toff);
  159. else if(cmpr==34316) UTIF.decode._decodePanasonic(img,data,off, len, tgt, toff);
  160. else if(cmpr==34713) //for(var j=0; j<len; j++) tgt[toff+j] = data[off+j];
  161. UTIF.decode._decodeNikon (img,ifds, data, off, len, tgt, toff);
  162. else if(cmpr==34676) UTIF.decode._decodeLogLuv32(img,data, off, len, tgt, toff);
  163. else log("Unknown compression", cmpr);
  164. //console.log(Date.now()-time);
  165. var bps = (img["t258"]?Math.min(32,img["t258"][0]):1);
  166. var noc = (img["t277"]?img["t277"][0]:1), bpp=(bps*noc)>>>3, h = (img["t278"] ? img["t278"][0] : img.height), bpl = Math.ceil(bps*noc*img.width/8);
  167. // convert to Little Endian /*
  168. if(bps==16 && !img.isLE && img["t33422"]==null) // not DNG
  169. for(var y=0; y<h; y++) {
  170. //console.log("fixing endianity");
  171. var roff = toff+y*bpl;
  172. for(var x=1; x<bpl; x+=2) { var t=tgt[roff+x]; tgt[roff+x]=tgt[roff+x-1]; tgt[roff+x-1]=t; }
  173. } //*/
  174. if(img["t317"] && img["t317"][0]==2)
  175. {
  176. for(var y=0; y<h; y++)
  177. {
  178. var ntoff = toff+y*bpl;
  179. if(bps==16) for(var j=bpp; j<bpl; j+=2) {
  180. var nv = ((tgt[ntoff+j+1]<<8)|tgt[ntoff+j]) + ((tgt[ntoff+j-bpp+1]<<8)|tgt[ntoff+j-bpp]);
  181. tgt[ntoff+j] = nv&255; tgt[ntoff+j+1] = (nv>>>8)&255;
  182. }
  183. else if(noc==3) for(var j= 3; j<bpl; j+=3)
  184. {
  185. tgt[ntoff+j ] = (tgt[ntoff+j ] + tgt[ntoff+j-3])&255;
  186. tgt[ntoff+j+1] = (tgt[ntoff+j+1] + tgt[ntoff+j-2])&255;
  187. tgt[ntoff+j+2] = (tgt[ntoff+j+2] + tgt[ntoff+j-1])&255;
  188. }
  189. else for(var j=bpp; j<bpl; j++) tgt[ntoff+j] = (tgt[ntoff+j] + tgt[ntoff+j-bpp])&255;
  190. }
  191. }
  192. }
  193. UTIF.decode._decodePanasonic = function(img,data,off, len, tgt, toff){
  194. var img_buffer = data.buffer;
  195. var rawWidth = img["t2"][0];
  196. var rawHeight = img["t3"][0];
  197. var bitsPerSample = img["t10"][0];
  198. var RW2_Format = img["t45"][0];
  199. var bidx = 0;
  200. var imageIndex = 0;
  201. var vpos = 0;
  202. var byte = 0;
  203. var arr_a, arr_b;
  204. var bytes = (RW2_Format == 6 ? new Uint32Array(14) : new Uint8Array(16));
  205. var i, j, sh, pred=[0,0], nonz=[0,0], isOdd, idx = 0, pixel_base;
  206. var row, col, crow;
  207. var buffer = new Uint8Array(0x4000);
  208. var result = new Uint16Array(tgt.buffer);
  209. function getDataRaw(bits){
  210. if (vpos == 0) {
  211. var arr_a = new Uint8Array(img_buffer, off+imageIndex + 0x1ff8, 0x4000-0x1ff8);
  212. var arr_b = new Uint8Array(img_buffer, off+imageIndex, 0x1ff8);
  213. buffer.set(arr_a); buffer.set(arr_b, arr_a.length);
  214. imageIndex += 0x4000;
  215. }
  216. if(RW2_Format == 5) {
  217. for (i = 0; i < 16; i++){
  218. bytes[i] = buffer[vpos++];
  219. vpos &= 0x3FFF;
  220. }
  221. } else {
  222. vpos = (vpos - bits) & 0x1ffff;
  223. byte = vpos >> 3 ^ 0x3ff0;
  224. return (buffer[byte] | buffer[byte + 1] << 8) >> (vpos & 7) & ~((-1) << bits);
  225. }
  226. }
  227. // Raw Format 6
  228. function getBufferDataRW6(i) {
  229. return buffer[vpos + 15 - i];
  230. }
  231. function readPageRW6() {
  232. bytes[0] = (getBufferDataRW6(0) << 6) | (getBufferDataRW6(1) >> 2); // 14 bit
  233. bytes[1] = (((getBufferDataRW6(1) & 0x3) << 12) | (getBufferDataRW6(2) << 4) | (getBufferDataRW6(3) >> 4)) & 0x3fff;
  234. bytes[2] = (getBufferDataRW6(3) >> 2) & 0x3;
  235. bytes[3] = ((getBufferDataRW6(3) & 0x3) << 8) | getBufferDataRW6(4);
  236. bytes[4] = (getBufferDataRW6(5) << 2) | (getBufferDataRW6(6) >> 6);
  237. bytes[5] = ((getBufferDataRW6(6) & 0x3f) << 4) | (getBufferDataRW6(7) >> 4);
  238. bytes[6] = (getBufferDataRW6(7) >> 2) & 0x3;
  239. bytes[7] = ((getBufferDataRW6(7) & 0x3) << 8) | getBufferDataRW6(8);
  240. bytes[8] = ((getBufferDataRW6(9) << 2) & 0x3fc) | (getBufferDataRW6(10) >> 6);
  241. bytes[9] = ((getBufferDataRW6(10) << 4) | (getBufferDataRW6(11) >> 4)) & 0x3ff;
  242. bytes[10] = (getBufferDataRW6(11) >> 2) & 0x3;
  243. bytes[11] = ((getBufferDataRW6(11) & 0x3) << 8) | getBufferDataRW6(12);
  244. bytes[12] = (((getBufferDataRW6(13) << 2) & 0x3fc) | getBufferDataRW6(14) >> 6) & 0x3ff;
  245. bytes[13] = ((getBufferDataRW6(14) << 4) | (getBufferDataRW6(15) >> 4)) & 0x3ff;
  246. vpos += 16;
  247. byte = 0;
  248. }
  249. // Main loop
  250. function resetPredNonzeros(){
  251. pred[0]=0; pred[1]=0;
  252. nonz[0]=0; nonz[1]=0;
  253. }
  254. if (RW2_Format == 7) {
  255. throw RW2_Format;
  256. // Skatch of version 7
  257. /*
  258. var pixels_per_block = bitsPerSample == 14 ? 9 : 10;
  259. rowbytes = 0|(rawWidth / pixels_per_block * 16);
  260. for (row = 0; row < rawHeight - 15; row += 16) {
  261. var rowstoread = Math.min(16, rawHeight - row);
  262. var readlen = rowbytes*rowstoread;
  263. buffer = new Uint8Array(image.slice(bidx, bidx+readlen));
  264. vpos = 0;
  265. bidx += readlen;
  266. i = 0;
  267. for (crow = 0; crow < rowstoread; crow++) {
  268. idx = (row + crow) * rawWidth;
  269. for (col = 0; col <= rawWidth - pixels_per_block; col += pixels_per_block) {
  270. for(j=0; j < pixels_per_block; j++) bytes[j] = buffer[i++];
  271. if (bitsPerSample == 12) {
  272. result[idx ] = ((bytes[1] & 0xF) << 8) + bytes[0];
  273. result[idx + 1] = 16 * bytes[2] + (bytes[1] >> 4);
  274. result[idx + 2] = ((bytes[4] & 0xF) << 8) + bytes[3];
  275. result[idx + 3] = 16 * bytes[5] + (bytes[4] >> 4);
  276. result[idx + 4] = ((bytes[7] & 0xF) << 8) + bytes[6];
  277. result[idx + 5] = 16 * bytes[8] + (bytes[7] >> 4);
  278. result[idx + 6] = ((bytes[10] & 0xF) << 8) + bytes[9];
  279. result[idx + 7] = 16 * bytes[11] + (bytes[10] >> 4);
  280. result[idx + 8] = ((bytes[13] & 0xF) << 8) + bytes[12];
  281. result[idx + 9] = 16 * bytes[14] + (bytes[13] >> 4);
  282. } else if (bitsPerSample == 14) {
  283. result[idx] = bytes[0] + ((bytes[1] & 0x3F) << 8);
  284. result[idx + 1] = (bytes[1] >> 6) + 4 * (bytes[2]) + ((bytes[3] & 0xF) << 10);
  285. result[idx + 2] = (bytes[3] >> 4) + 16 * (bytes[4]) + ((bytes[5] & 3) << 12);
  286. result[idx + 3] = ((bytes[5] & 0xFC) >> 2) + (bytes[6] << 6);
  287. result[idx + 4] = bytes[7] + ((bytes[8] & 0x3F) << 8);
  288. result[idx + 5] = (bytes[8] >> 6) + 4 * bytes[9] + ((bytes[10] & 0xF) << 10);
  289. result[idx + 6] = (bytes[10] >> 4) + 16 * bytes[11] + ((bytes[12] & 3) << 12);
  290. result[idx + 7] = ((bytes[12] & 0xFC) >> 2) + (bytes[13] << 6);
  291. result[idx + 8] = bytes[14] + ((bytes[15] & 0x3F) << 8);
  292. }
  293. }
  294. }
  295. }
  296. */
  297. }
  298. else if(RW2_Format == 6) {
  299. var blocksperrow = Math.floor(rawWidth / 11),
  300. rowbytes = blocksperrow * 16;
  301. for (row = 0; row < rawHeight - 15; row += 16) {
  302. var rowstoread = Math.min(16, rawHeight - row);
  303. var readlen = rowbytes*rowstoread;
  304. buffer = new Uint8Array(img_buffer, off+bidx, readlen);//new Uint8Array(image.slice(bidx, bidx+readlen));
  305. vpos = 0;
  306. bidx += readlen;
  307. for (crow = 0, col = 0; crow < rowstoread; crow++, col = 0) {
  308. idx = (row + crow) * rawWidth;
  309. for (var rblock = 0; rblock < blocksperrow; rblock++) {
  310. readPageRW6();
  311. resetPredNonzeros();
  312. sh=0; pixel_base=0;
  313. for (i = 0; i < 11; i++){
  314. isOdd = i & 1;
  315. if (i % 3 == 2) {
  316. var base = byte < 14 ? bytes[byte++] : 0;
  317. if (base == 3) base = 4;
  318. pixel_base = 0x200 << base;
  319. sh = 1 << base;
  320. }
  321. var epixel = byte < 14 ? bytes[byte++] : 0;
  322. if (pred[isOdd]) {
  323. epixel *= sh;
  324. if (pixel_base < 0x2000 && nonz[isOdd] > pixel_base)
  325. epixel += nonz[isOdd] - pixel_base;
  326. nonz[isOdd] = epixel;
  327. } else {
  328. pred[isOdd] = epixel;
  329. if (epixel)
  330. nonz[isOdd] = epixel;
  331. else
  332. epixel = nonz[isOdd];
  333. }
  334. result[idx + col++] = (epixel - 0xf) <= 0xffff ? (epixel - 0xf) & 0xffff : ((epixel + 0x7ffffff1) >> 0x1f) & 0x3fff;
  335. }
  336. }
  337. }
  338. }
  339. }
  340. else if (RW2_Format == 5) {
  341. var blockSize = bitsPerSample == 12 ? 10 : 9;
  342. for (row = 0; row < rawHeight; row++) {
  343. for (col = 0; col < rawWidth; col+=blockSize) {
  344. getDataRaw(0);
  345. // Tuhle podminku pouziva i RW2_Format 7
  346. if (bitsPerSample == 12) {
  347. result[idx++] = ((bytes[1] & 0xF) << 8) + bytes[0];
  348. result[idx++] = 16 * bytes[2] + (bytes[1] >> 4);
  349. result[idx++] = ((bytes[4] & 0xF) << 8) + bytes[3];
  350. result[idx++] = 16 * bytes[5] + (bytes[4] >> 4);
  351. result[idx++] = ((bytes[7] & 0xF) << 8) + bytes[6];
  352. result[idx++] = 16 * bytes[8] + (bytes[7] >> 4);
  353. result[idx++] = ((bytes[10] & 0xF) << 8) + bytes[9];
  354. result[idx++] = 16 * bytes[11] + (bytes[10] >> 4);
  355. result[idx++] = ((bytes[13] & 0xF) << 8) + bytes[12];
  356. result[idx++] = 16 * bytes[14] + (bytes[13] >> 4);
  357. } else if (bitsPerSample == 14) {
  358. result[idx++] = bytes[0] + ((bytes[1] & 0x3F) << 8);
  359. result[idx++] = (bytes[1] >> 6) + 4 * (bytes[2]) + ((bytes[3] & 0xF) << 10);
  360. result[idx++] = (bytes[3] >> 4) + 16 * (bytes[4]) + ((bytes[5] & 3) << 12);
  361. result[idx++] = ((bytes[5] & 0xFC) >> 2) + (bytes[6] << 6);
  362. result[idx++] = bytes[7] + ((bytes[8] & 0x3F) << 8);
  363. result[idx++] = (bytes[8] >> 6) + 4 * bytes[9] + ((bytes[10] & 0xF) << 10);
  364. result[idx++] = (bytes[10] >> 4) + 16 * bytes[11] + ((bytes[12] & 3) << 12);
  365. result[idx++] = ((bytes[12] & 0xFC) >> 2) + (bytes[13] << 6);
  366. result[idx++] = bytes[14] + ((bytes[15] & 0x3F) << 8);
  367. }
  368. }
  369. }
  370. //console.log(result[1000000 - 1])
  371. } else if(RW2_Format == 4) {
  372. for (row = 0; row < rawHeight; row++){
  373. for(col = 0; col < rawWidth; col++){
  374. i = col % 14;
  375. isOdd = i & 1;
  376. if (i==0) resetPredNonzeros();
  377. if (i%3 == 2)
  378. sh = 4 >> (3 - getDataRaw(2));
  379. if (nonz[isOdd]) {
  380. j = getDataRaw(8);
  381. if(j != 0){
  382. pred[isOdd] -= 0x80 << sh;
  383. if (pred[isOdd] < 0 || sh == 4)
  384. pred[isOdd] &= ~((-1) << sh);
  385. pred[isOdd] += j << sh;
  386. }
  387. } else {
  388. nonz[isOdd] = getDataRaw(8);
  389. if(nonz[isOdd] || i > 11)
  390. pred[isOdd] = nonz[isOdd] << 4 | getDataRaw(4);
  391. }
  392. result[idx++] = pred[col & 1];
  393. }
  394. }
  395. }
  396. else throw RW2_Format;
  397. }
  398. UTIF.decode._decodeVC5 = UTIF.decode._decodeVC5=function(){var e=[1,0,1,0,2,2,1,1,3,7,1,2,5,25,1,3,6,48,1,4,6,54,1,5,7,111,1,8,7,99,1,6,7,105,12,0,7,107,1,7,8,209,20,0,8,212,1,9,8,220,1,10,9,393,1,11,9,394,32,0,9,416,1,12,9,427,1,13,10,887,1,18,10,784,1,14,10,790,1,15,10,835,60,0,10,852,1,16,10,885,1,17,11,1571,1,19,11,1668,1,20,11,1669,100,0,11,1707,1,21,11,1772,1,22,12,3547,1,29,12,3164,1,24,12,3166,1,25,12,3140,1,23,12,3413,1,26,12,3537,1,27,12,3539,1,28,13,7093,1,35,13,6283,1,30,13,6331,1,31,13,6335,180,0,13,6824,1,32,13,7072,1,33,13,7077,320,0,13,7076,1,34,14,12565,1,36,14,12661,1,37,14,12669,1,38,14,13651,1,39,14,14184,1,40,15,28295,1,46,15,28371,1,47,15,25320,1,42,15,25336,1,43,15,25128,1,41,15,27300,1,44,15,28293,1,45,16,50259,1,48,16,50643,1,49,16,50675,1,50,16,56740,1,53,16,56584,1,51,16,56588,1,52,17,113483,1,61,17,113482,1,60,17,101285,1,55,17,101349,1,56,17,109205,1,57,17,109207,1,58,17,100516,1,54,17,113171,1,59,18,202568,1,62,18,202696,1,63,18,218408,1,64,18,218412,1,65,18,226340,1,66,18,226356,1,67,18,226358,1,68,19,402068,1,69,19,405138,1,70,19,405394,1,71,19,436818,1,72,19,436826,1,73,19,452714,1,75,19,452718,1,76,19,452682,1,74,20,804138,1,77,20,810279,1,78,20,810790,1,79,20,873638,1,80,20,873654,1,81,20,905366,1,82,20,905430,1,83,20,905438,1,84,21,1608278,1,85,21,1620557,1,86,21,1621582,1,87,21,1621583,1,88,21,1747310,1,89,21,1810734,1,90,21,1810735,1,91,21,1810863,1,92,21,1810879,1,93,22,3621725,1,99,22,3621757,1,100,22,3241112,1,94,22,3494556,1,95,22,3494557,1,96,22,3494622,1,97,22,3494623,1,98,23,6482227,1,102,23,6433117,1,101,23,6989117,1,103,23,6989119,1,105,23,6989118,1,104,23,7243449,1,106,23,7243512,1,107,24,13978233,1,111,24,12964453,1,109,24,12866232,1,108,24,14486897,1,113,24,13978232,1,110,24,14486896,1,112,24,14487026,1,114,24,14487027,1,115,25,25732598,1,225,25,25732597,1,189,25,25732596,1,188,25,25732595,1,203,25,25732594,1,202,25,25732593,1,197,25,25732592,1,207,25,25732591,1,169,25,25732590,1,223,25,25732589,1,159,25,25732522,1,235,25,25732579,1,152,25,25732575,1,192,25,25732489,1,179,25,25732573,1,201,25,25732472,1,172,25,25732576,1,149,25,25732488,1,178,25,25732566,1,120,25,25732571,1,219,25,25732577,1,150,25,25732487,1,127,25,25732506,1,211,25,25732548,1,125,25,25732588,1,158,25,25732486,1,247,25,25732467,1,238,25,25732508,1,163,25,25732552,1,228,25,25732603,1,183,25,25732513,1,217,25,25732587,1,168,25,25732520,1,122,25,25732484,1,128,25,25732562,1,249,25,25732505,1,187,25,25732504,1,186,25,25732483,1,136,25,25928905,1,181,25,25732560,1,255,25,25732500,1,230,25,25732482,1,135,25,25732555,1,233,25,25732568,1,222,25,25732583,1,145,25,25732481,1,134,25,25732586,1,167,25,25732521,1,248,25,25732518,1,209,25,25732480,1,243,25,25732512,1,216,25,25732509,1,164,25,25732547,1,140,25,25732479,1,157,25,25732544,1,239,25,25732574,1,191,25,25732564,1,251,25,25732478,1,156,25,25732546,1,139,25,25732498,1,242,25,25732557,1,133,25,25732477,1,162,25,25732515,1,213,25,25732584,1,165,25,25732514,1,212,25,25732476,1,227,25,25732494,1,198,25,25732531,1,236,25,25732530,1,234,25,25732529,1,117,25,25732528,1,215,25,25732527,1,124,25,25732526,1,123,25,25732525,1,254,25,25732524,1,253,25,25732523,1,148,25,25732570,1,218,25,25732580,1,146,25,25732581,1,147,25,25732569,1,224,25,25732533,1,143,25,25732540,1,184,25,25732541,1,185,25,25732585,1,166,25,25732556,1,132,25,25732485,1,129,25,25732563,1,250,25,25732578,1,151,25,25732501,1,119,25,25732502,1,193,25,25732536,1,176,25,25732496,1,245,25,25732553,1,229,25,25732516,1,206,25,25732582,1,144,25,25732517,1,208,25,25732558,1,137,25,25732543,1,241,25,25732466,1,237,25,25732507,1,190,25,25732542,1,240,25,25732551,1,131,25,25732554,1,232,25,25732565,1,252,25,25732475,1,171,25,25732493,1,205,25,25732492,1,204,25,25732491,1,118,25,25732490,1,214,25,25928904,1,180,25,25732549,1,126,25,25732602,1,182,25,25732539,1,175,25,25732545,1,141,25,25732559,1,138,25,25732537,1,177,25,25732534,1,153,25,25732503,1,194,25,25732606,1,160,25,25732567,1,121,25,25732538,1,174,25,25732497,1,246,25,25732550,1,130,25,25732572,1,200,25,25732474,1,170,25,25732511,1,221,25,25732601,1,196,25,25732532,1,142,25,25732519,1,210,25,25732495,1,199,25,25732605,1,155,25,25732535,1,154,25,25732499,1,244,25,25732510,1,220,25,25732600,1,195,25,25732607,1,161,25,25732604,1,231,25,25732473,1,173,25,25732599,1,226,26,51465122,1,116,26,51465123,0,1],x,u,H,d=[3,3,3,3,2,2,2,1,1,1],a=24576,a7=16384,K=8192,ai=a7|K;
  399. function A(B){var P=B[1],D=B[0][P>>>3]>>>7-(P&7)&1;B[1]++;return D}function aj(B,P){if(x==null){x={};
  400. for(var D=0;D<e.length;D+=4)x[e[D+1]]=e.slice(D,D+4)}var U=A(B),X=x[U];while(X==null){U=U<<1|A(B);X=x[U]}var y=X[3];
  401. if(y!=0)y=A(B)==0?y:-y;P[0]=X[2];P[1]=y}function Q(B,P){for(var D=0;D<P;D++){if((B&1)==1)B++;B=B>>>1}return B}function c(B,P){return B>>P}function N(B,P,D,U,X,y){P[D]=c(c(11*B[X]-4*B[X+y]+B[X+y+y]+4,3)+B[U],1);
  402. P[D+y]=c(c(5*B[X]+4*B[X+y]-B[X+y+y]+4,3)-B[U],1)}function g(B,P,D,U,X,y){var n=B[X-y]-B[X+y],S=B[X],O=B[U];
  403. P[D]=c(c(n+4,3)+S+O,1);P[D+y]=c(c(-n+4,3)+S-O,1)}function L(B,P,D,U,X,y){P[D]=c(c(5*B[X]+4*B[X-y]-B[X-y-y]+4,3)+B[U],1);
  404. P[D+y]=c(c(11*B[X]-4*B[X-y]+B[X-y-y]+4,3)-B[U],1)}function t(B){B=B<0?0:B>4095?4095:B;B=H[B]>>>2;return B}function ab(B,P,D,U,X){U=new Uint16Array(U.buffer);
  405. var y=Date.now(),n=UTIF._binBE,S=P+D,O,q,i,M,m,aA,T,a8,a0,am,au,a3,aw,ao,v,ax,p,k;P+=4;while(P<S){var W=n.readShort(B,P),E=n.readUshort(B,P+2);
  406. P+=4;if(W==12)O=E;else if(W==20)q=E;else if(W==21)i=E;else if(W==48)M=E;else if(W==53)m=E;else if(W==35)aA=E;
  407. else if(W==62)T=E;else if(W==101)a8=E;else if(W==109)a0=E;else if(W==84)am=E;else if(W==106)au=E;else if(W==107)a3=E;
  408. else if(W==108)aw=E;else if(W==102)ao=E;else if(W==104)v=E;else if(W==105)ax=E;else{var C=W<0?-W:W,F=C&65280,o=0;
  409. if(C&ai){if(C&K){o=E&65535;o+=(C&255)<<16}else{o=E&65535}}if((C&a)==a){if(p==null){p=[];for(var f=0;
  410. f<4;f++)p[f]=new Int16Array((q>>>1)*(i>>>1));k=new Int16Array((q>>>1)*(i>>>1));u=new Int16Array(1024);
  411. for(var f=0;f<1024;f++){var aF=f-512,j=Math.abs(aF),O=Math.floor(768*j*j*j/(255*255*255))+j;u[f]=Math.sign(aF)*O}H=new Uint16Array(4096);
  412. var al=(1<<16)-1;for(var f=0;f<4096;f++){var ad=f,az=al*(Math.pow(113,ad/4095)-1)/112;H[f]=Math.min(az,al)}}var Z=p[T],V=Q(q,1+d[M]),z=Q(i,1+d[M]);
  413. if(M==0){for(var b=0;b<z;b++)for(var G=0;G<V;G++){var w=P+(b*V+G)*2;Z[b*(q>>>1)+G]=B[w]<<8|B[w+1]}}else{var aC=[B,P*8],aq=[],a5=0,ae=V*z,I=[0,0],s=0,E=0;
  414. while(a5<ae){aj(aC,I);s=I[0];E=I[1];while(s>0){aq[a5++]=E;s--}}var $=(M-1)%3,aE=$!=1?V:0,as=$!=0?z:0;
  415. for(var b=0;b<z;b++){var ay=(b+as)*(q>>>1)+aE,aa=b*V;for(var G=0;G<V;G++)Z[ay+G]=u[aq[aa+G]+512]*m}if($==2){var v=q>>>1,an=V*2,at=z*2;
  416. for(var b=0;b<z;b++){for(var G=0;G<an;G++){var f=b*2*v+G,_=b*v+G,l=z*v+_;if(b==0)N(Z,k,f,l,_,v);else if(b==z-1)L(Z,k,f,l,_,v);
  417. else g(Z,k,f,l,_,v)}}var h=Z;Z=k;k=h;for(var b=0;b<at;b++){for(var G=0;G<V;G++){var f=b*v+2*G,_=b*v+G,l=V+_;
  418. if(G==0)N(Z,k,f,l,_,1);else if(G==V-1)L(Z,k,f,l,_,1);else g(Z,k,f,l,_,1)}}var h=Z;Z=k;k=h;var a6=[],aD=2-~~((M-1)/3);
  419. for(var r=0;r<3;r++)a6[r]=a0>>14-r*2&3;var af=a6[aD];if(af!=0)for(var b=0;b<at;b++)for(var G=0;G<an;
  420. G++){var f=b*v+G;Z[f]=Z[f]<<af}}}if(M==9&&T==3){var a2=p[0],ar=p[1],ah=p[2],a1=p[3];for(var b=0;b<i;
  421. b+=2)for(var G=0;G<q;G+=2){var J=b*q+G,w=(b>>>1)*(q>>>1)+(G>>>1),R=a2[w],ak=ar[w]-2048,aB=ah[w]-2048,av=a1[w]-2048,a4=(ak<<1)+R,a9=(aB<<1)+R,ap=R+av,ag=R-av;
  422. U[J]=t(a4);U[J+1]=t(ap);U[J+q]=t(ag);U[J+q+1]=t(a9)}}P+=o*4}else if(C==16388){P+=o*4}else if(F==8192||F==8448||F==9216){}else throw C.toString(16)}}console.log(Date.now()-y)}return ab}()
  423. UTIF.decode._decodeLogLuv32 = function(img, data, off, len, tgt, toff) {
  424. var w = img.width, qw=w*4;
  425. var io = 0, out = new Uint8Array(qw);
  426. while(io<len) {
  427. var oo=0;
  428. while(oo<qw) {
  429. var c = data[off+io]; io++;
  430. if(c<128) { for(var j=0; j<c; j++) out[oo+j] = data[off+io+j]; oo+=c; io+=c; }
  431. else { c=c-126; for(var j=0; j<c; j++) out[oo+j] = data[off+io ]; oo+=c; io++ ; }
  432. }
  433. for(var x=0; x<w; x++) {
  434. tgt[toff+0] = out[x];
  435. tgt[toff+1] = out[x+w];
  436. tgt[toff+2] = out[x+w*2];
  437. tgt[toff+4] = out[x+w*3];
  438. toff+=6;
  439. }
  440. }
  441. }
  442. UTIF.decode._ljpeg_diff = function(data, prm, huff) {
  443. var getbithuff = UTIF.decode._getbithuff;
  444. var len, diff;
  445. len = getbithuff(data, prm, huff[0], huff);
  446. diff = getbithuff(data, prm, len, 0);
  447. if ((diff & (1 << (len-1))) == 0) diff -= (1 << len) - 1;
  448. return diff;
  449. }
  450. UTIF.decode._decodeARW = function(img, inp, off, src_length, tgt, toff) {
  451. var raw_width = img["t256"][0], height=img["t257"][0], tiff_bps=img["t258"][0];
  452. var bin=(img.isLE ? UTIF._binLE : UTIF._binBE);
  453. //console.log(raw_width, height, tiff_bps, raw_width*height, src_length);
  454. var arw2 = (raw_width*height == src_length) || (raw_width*height*1.5 == src_length);
  455. //arw2 = true;
  456. //console.log("ARW2: ", arw2, raw_width*height, src_length, tgt.length);
  457. if(!arw2) { //"sony_arw_load_raw"; // not arw2
  458. height+=8;
  459. var prm = [off,0,0,0];
  460. var huff = new Uint16Array(32770);
  461. var tab = [ 0xf11,0xf10,0xe0f,0xd0e,0xc0d,0xb0c,0xa0b,0x90a,0x809,
  462. 0x708,0x607,0x506,0x405,0x304,0x303,0x300,0x202,0x201 ];
  463. var i, c, n, col, row, sum=0;
  464. var ljpeg_diff = UTIF.decode._ljpeg_diff;
  465. huff[0] = 15;
  466. for (n=i=0; i < 18; i++) {
  467. var lim = 32768 >>> (tab[i] >>> 8);
  468. for(var c=0; c<lim; c++) huff[++n] = tab[i];
  469. }
  470. for (col = raw_width; col--; )
  471. for (row=0; row < height+1; row+=2) {
  472. if (row == height) row = 1;
  473. sum += ljpeg_diff(inp, prm, huff);
  474. if (row < height) {
  475. var clr = (sum)&4095;
  476. UTIF.decode._putsF(tgt, (row*raw_width+col)*tiff_bps, clr<<(16-tiff_bps));
  477. }
  478. }
  479. return;
  480. }
  481. if(raw_width*height*1.5==src_length) {
  482. //console.log("weird compression");
  483. for(var i=0; i<src_length; i+=3) { var b0=inp[off+i+0], b1=inp[off+i+1], b2=inp[off+i+2];
  484. tgt[toff+i]=(b1<<4)|(b0>>>4); tgt[toff+i+1]=(b0<<4)|(b2>>>4); tgt[toff+i+2]=(b2<<4)|(b1>>>4); }
  485. return;
  486. }
  487. var pix = new Uint16Array(16);
  488. var row, col, val, max, min, imax, imin, sh, bit, i, dp;
  489. var data = new Uint8Array(raw_width+1);
  490. for (row=0; row < height; row++) {
  491. //fread (data, 1, raw_width, ifp);
  492. for(var j=0; j<raw_width; j++) data[j]=inp[off++];
  493. for (dp=0, col=0; col < raw_width-30; dp+=16) {
  494. max = 0x7ff & (val = bin.readUint(data,dp));
  495. min = 0x7ff & (val >>> 11);
  496. imax = 0x0f & (val >>> 22);
  497. imin = 0x0f & (val >>> 26);
  498. for (sh=0; sh < 4 && 0x80 << sh <= max-min; sh++);
  499. for (bit=30, i=0; i < 16; i++)
  500. if (i == imax) pix[i] = max;
  501. else if (i == imin) pix[i] = min;
  502. else {
  503. pix[i] = ((bin.readUshort(data, dp+(bit >> 3)) >>> (bit & 7) & 0x7f) << sh) + min;
  504. if (pix[i] > 0x7ff) pix[i] = 0x7ff;
  505. bit += 7;
  506. }
  507. for (i=0; i < 16; i++, col+=2) {
  508. //RAW(row,col) = curve[pix[i] << 1] >> 2;
  509. var clr = pix[i]<<1; //clr = 0xffff;
  510. UTIF.decode._putsF(tgt, (row*raw_width+col)*tiff_bps, clr<<(16-tiff_bps));
  511. }
  512. col -= col & 1 ? 1:31;
  513. }
  514. }
  515. }
  516. UTIF.decode._decodeNikon = function(img,imgs, data, off, src_length, tgt, toff)
  517. {
  518. var nikon_tree = [
  519. [ 0, 0,1,5,1,1,1,1,1,1,2,0,0,0,0,0,0, /* 12-bit lossy */
  520. 5,4,3,6,2,7,1,0,8,9,11,10,12 ],
  521. [ 0, 0,1,5,1,1,1,1,1,1,2,0,0,0,0,0,0, /* 12-bit lossy after split */
  522. 0x39,0x5a,0x38,0x27,0x16,5,4,3,2,1,0,11,12,12 ],
  523. [ 0, 0,1,4,2,3,1,2,0,0,0,0,0,0,0,0,0, /* 12-bit lossless */
  524. 5,4,6,3,7,2,8,1,9,0,10,11,12 ],
  525. [ 0, 0,1,4,3,1,1,1,1,1,2,0,0,0,0,0,0, /* 14-bit lossy */
  526. 5,6,4,7,8,3,9,2,1,0,10,11,12,13,14 ],
  527. [ 0, 0,1,5,1,1,1,1,1,1,1,2,0,0,0,0,0, /* 14-bit lossy after split */
  528. 8,0x5c,0x4b,0x3a,0x29,7,6,5,4,3,2,1,0,13,14 ],
  529. [ 0, 0,1,4,2,2,3,1,2,0,0,0,0,0,0,0,0, /* 14-bit lossless */
  530. 7,6,8,5,9,4,10,3,11,12,2,0,1,13,14 ] ];
  531. var raw_width = img["t256"][0], height=img["t257"][0], tiff_bps=img["t258"][0];
  532. var tree = 0, split = 0;
  533. var make_decoder = UTIF.decode._make_decoder;
  534. var getbithuff = UTIF.decode._getbithuff;
  535. var mn = imgs[0].exifIFD.makerNote, md = mn["t150"]?mn["t150"]:mn["t140"], mdo=0; //console.log(mn,md);
  536. //console.log(md[0].toString(16), md[1].toString(16), tiff_bps);
  537. var ver0 = md[mdo++], ver1 = md[mdo++];
  538. if (ver0 == 0x49 || ver1 == 0x58) mdo+=2110;
  539. if (ver0 == 0x46) tree = 2;
  540. if (tiff_bps == 14) tree += 3;
  541. var vpred = [[0,0],[0,0]], bin=(img.isLE ? UTIF._binLE : UTIF._binBE);
  542. for(var i=0; i<2; i++) for(var j=0; j<2; j++) { vpred[i][j] = bin.readShort(md,mdo); mdo+=2; } // not sure here ... [i][j] or [j][i]
  543. //console.log(vpred);
  544. var max = 1 << tiff_bps & 0x7fff, step=0;
  545. var csize = bin.readShort(md,mdo); mdo+=2;
  546. if (csize > 1) step = Math.floor(max / (csize-1));
  547. if (ver0 == 0x44 && ver1 == 0x20 && step > 0) split = bin.readShort(md,562);
  548. var i;
  549. var row, col;
  550. var len, shl, diff;
  551. var min_v = 0;
  552. var hpred = [0,0];
  553. var huff = make_decoder(nikon_tree[tree]);
  554. //var g_input_offset=0, bitbuf=0, vbits=0, reset=0;
  555. var prm = [off,0,0,0];
  556. //console.log(split); split = 170;
  557. for (min_v=row=0; row < height; row++) {
  558. if (split && row == split) {
  559. //free (huff);
  560. huff = make_decoder (nikon_tree[tree+1]);
  561. //max_v += (min_v = 16) << 1;
  562. }
  563. for (col=0; col < raw_width; col++) {
  564. i = getbithuff(data,prm,huff[0],huff);
  565. len = i & 15;
  566. shl = i >>> 4;
  567. diff = (((getbithuff(data,prm,len-shl,0) << 1) + 1) << shl) >>> 1;
  568. if ((diff & (1 << (len-1))) == 0)
  569. diff -= (1 << len) - (shl==0?1:0);
  570. if (col < 2) hpred[col] = vpred[row & 1][col] += diff;
  571. else hpred[col & 1] += diff;
  572. var clr = Math.min(Math.max(hpred[col & 1],0),(1<<tiff_bps)-1);
  573. var bti = (row*raw_width+col)*tiff_bps;
  574. UTIF.decode._putsF(tgt, bti, clr<<(16-tiff_bps));
  575. }
  576. }
  577. }
  578. // put 16 bits
  579. UTIF.decode._putsF= function(dt, pos, val) { val = val<<(8-(pos&7)); var o=(pos>>>3); dt[o]|=val>>>16; dt[o+1]|=val>>>8; dt[o+2]|=val; }
  580. UTIF.decode._getbithuff = function(data,prm,nbits, huff) {
  581. var zero_after_ff = 0;
  582. var get_byte = UTIF.decode._get_byte;
  583. var c;
  584. var off=prm[0], bitbuf=prm[1], vbits=prm[2], reset=prm[3];
  585. //if (nbits > 25) return 0;
  586. //if (nbits < 0) return bitbuf = vbits = reset = 0;
  587. if (nbits == 0 || vbits < 0) return 0;
  588. while (!reset && vbits < nbits && (c = data[off++]) != -1 &&
  589. !(reset = zero_after_ff && c == 0xff && data[off++])) {
  590. //console.log("byte read into c");
  591. bitbuf = (bitbuf << 8) + c;
  592. vbits += 8;
  593. }
  594. c = (bitbuf << (32-vbits)) >>> (32-nbits);
  595. if (huff) {
  596. vbits -= huff[c+1] >>> 8; //console.log(c, huff[c]>>8);
  597. c = huff[c+1]&255;
  598. } else
  599. vbits -= nbits;
  600. if (vbits < 0) throw "e";
  601. prm[0]=off; prm[1]=bitbuf; prm[2]=vbits; prm[3]=reset;
  602. return c;
  603. }
  604. UTIF.decode._make_decoder = function(source) {
  605. var max, len, h, i, j;
  606. var huff = [];
  607. for (max=16; max!=0 && !source[max]; max--);
  608. var si=17;
  609. huff[0] = max;
  610. for (h=len=1; len <= max; len++)
  611. for (i=0; i < source[len]; i++, ++si)
  612. for (j=0; j < 1 << (max-len); j++)
  613. if (h <= 1 << max)
  614. huff[h++] = (len << 8) | source[si];
  615. return huff;
  616. }
  617. UTIF.decode._decodeNewJPEG = function(img, data, off, len, tgt, toff)
  618. {
  619. len = Math.min(len, data.length-off);
  620. var tables = img["t347"], tlen = tables ? tables.length : 0, buff = new Uint8Array(tlen + len);
  621. if (tables) {
  622. var SOI = 216, EOI = 217, boff = 0;
  623. for (var i=0; i<(tlen-1); i++)
  624. {
  625. // Skip EOI marker from JPEGTables
  626. if (tables[i]==255 && tables[i+1]==EOI) break;
  627. buff[boff++] = tables[i];
  628. }
  629. // Skip SOI marker from data
  630. var byte1 = data[off], byte2 = data[off + 1];
  631. if (byte1!=255 || byte2!=SOI)
  632. {
  633. buff[boff++] = byte1;
  634. buff[boff++] = byte2;
  635. }
  636. for (var i=2; i<len; i++) buff[boff++] = data[off+i];
  637. }
  638. else for (var i=0; i<len; i++) buff[i] = data[off+i];
  639. if(img["t262"][0]==32803 || (img["t259"][0]==7 && img["t262"][0]==34892)) // lossless JPEG (used in DNG files)
  640. {
  641. var bps = img["t258"][0];//, dcdr = new LosslessJpegDecoder();
  642. //var time = Date.now();
  643. var out = UTIF.LosslessJpegDecode(buff), olen=out.length; //console.log(olen);
  644. //var out = ULLJPG(buff), olen=out.length; //console.log(olen);
  645. //console.log(Date.now()-time);
  646. if(false) {}
  647. else if(bps==16) {
  648. if(img.isLE) for(var i=0; i<olen; i++ ) { tgt[toff+(i<<1)] = (out[i]&255); tgt[toff+(i<<1)+1] = (out[i]>>>8); }
  649. else for(var i=0; i<olen; i++ ) { tgt[toff+(i<<1)] = (out[i]>>>8); tgt[toff+(i<<1)+1] = (out[i]&255); }
  650. }
  651. else if(bps==14 || bps==12) { // 4 * 14 == 56 == 7 * 8
  652. var rst = 16-bps;
  653. for(var i=0; i<olen; i++) UTIF.decode._putsF(tgt, i*bps, out[i]<<rst);
  654. }
  655. else if(bps==8) {
  656. for(var i=0; i<olen; i++) tgt[toff+i]=out[i];
  657. }
  658. else throw new Error("unsupported bit depth "+bps);
  659. }
  660. else
  661. {
  662. var parser = new UTIF.JpegDecoder(); parser.parse(buff);
  663. var decoded = parser.getData({"width":parser.width,"height":parser.height,"forceRGB":true,"isSourcePDF":false});
  664. for (var i=0; i<decoded.length; i++) tgt[toff + i] = decoded[i];
  665. }
  666. // PhotometricInterpretation is 6 (YCbCr) for JPEG, but after decoding we populate data in
  667. // RGB format, so updating the tag value
  668. if(img["t262"][0] == 6) img["t262"][0] = 2;
  669. }
  670. UTIF.decode._decodeOldJPEGInit = function(img, data, off, len)
  671. {
  672. var SOI = 216, EOI = 217, DQT = 219, DHT = 196, DRI = 221, SOF0 = 192, SOS = 218;
  673. var joff = 0, soff = 0, tables, sosMarker, isTiled = false, i, j, k;
  674. var jpgIchgFmt = img["t513"], jifoff = jpgIchgFmt ? jpgIchgFmt[0] : 0;
  675. var jpgIchgFmtLen = img["t514"], jiflen = jpgIchgFmtLen ? jpgIchgFmtLen[0] : 0;
  676. var soffTag = img["t324"] || img["t273"] || jpgIchgFmt;
  677. var ycbcrss = img["t530"], ssx = 0, ssy = 0;
  678. var spp = img["t277"]?img["t277"][0]:1;
  679. var jpgresint = img["t515"];
  680. if(soffTag)
  681. {
  682. soff = soffTag[0];
  683. isTiled = (soffTag.length > 1);
  684. }
  685. if(!isTiled)
  686. {
  687. if(data[off]==255 && data[off+1]==SOI) return { jpegOffset: off };
  688. if(jpgIchgFmt!=null)
  689. {
  690. if(data[off+jifoff]==255 && data[off+jifoff+1]==SOI) joff = off+jifoff;
  691. else log("JPEGInterchangeFormat does not point to SOI");
  692. if(jpgIchgFmtLen==null) log("JPEGInterchangeFormatLength field is missing");
  693. else if(jifoff >= soff || (jifoff+jiflen) <= soff) log("JPEGInterchangeFormatLength field value is invalid");
  694. if(joff != null) return { jpegOffset: joff };
  695. }
  696. }
  697. if(ycbcrss!=null) { ssx = ycbcrss[0]; ssy = ycbcrss[1]; }
  698. if(jpgIchgFmt!=null)
  699. if(jpgIchgFmtLen!=null)
  700. if(jiflen >= 2 && (jifoff+jiflen) <= soff)
  701. {
  702. if(data[off+jifoff+jiflen-2]==255 && data[off+jifoff+jiflen-1]==SOI) tables = new Uint8Array(jiflen-2);
  703. else tables = new Uint8Array(jiflen);
  704. for(i=0; i<tables.length; i++) tables[i] = data[off+jifoff+i];
  705. log("Incorrect JPEG interchange format: using JPEGInterchangeFormat offset to derive tables");
  706. }
  707. else log("JPEGInterchangeFormat+JPEGInterchangeFormatLength > offset to first strip or tile");
  708. if(tables == null)
  709. {
  710. var ooff = 0, out = [];
  711. out[ooff++] = 255; out[ooff++] = SOI;
  712. var qtables = img["t519"];
  713. if(qtables==null) throw new Error("JPEGQTables tag is missing");
  714. for(i=0; i<qtables.length; i++)
  715. {
  716. out[ooff++] = 255; out[ooff++] = DQT; out[ooff++] = 0; out[ooff++] = 67; out[ooff++] = i;
  717. for(j=0; j<64; j++) out[ooff++] = data[off+qtables[i]+j];
  718. }
  719. for(k=0; k<2; k++)
  720. {
  721. var htables = img[(k == 0) ? "t520" : "t521"];
  722. if(htables==null) throw new Error(((k == 0) ? "JPEGDCTables" : "JPEGACTables") + " tag is missing");
  723. for(i=0; i<htables.length; i++)
  724. {
  725. out[ooff++] = 255; out[ooff++] = DHT;
  726. //out[ooff++] = 0; out[ooff++] = 67; out[ooff++] = i;
  727. var nc = 19;
  728. for(j=0; j<16; j++) nc += data[off+htables[i]+j];
  729. out[ooff++] = (nc >>> 8); out[ooff++] = nc & 255;
  730. out[ooff++] = (i | (k << 4));
  731. for(j=0; j<16; j++) out[ooff++] = data[off+htables[i]+j];
  732. for(j=0; j<nc; j++) out[ooff++] = data[off+htables[i]+16+j];
  733. }
  734. }
  735. out[ooff++] = 255; out[ooff++] = SOF0;
  736. out[ooff++] = 0; out[ooff++] = 8 + 3*spp; out[ooff++] = 8;
  737. out[ooff++] = (img.height >>> 8) & 255; out[ooff++] = img.height & 255;
  738. out[ooff++] = (img.width >>> 8) & 255; out[ooff++] = img.width & 255;
  739. out[ooff++] = spp;
  740. if(spp==1) { out[ooff++] = 1; out[ooff++] = 17; out[ooff++] = 0; }
  741. else for(i=0; i<3; i++)
  742. {
  743. out[ooff++] = i + 1;
  744. out[ooff++] = (i != 0) ? 17 : (((ssx & 15) << 4) | (ssy & 15));
  745. out[ooff++] = i;
  746. }
  747. if(jpgresint!=null && jpgresint[0]!=0)
  748. {
  749. out[ooff++] = 255; out[ooff++] = DRI; out[ooff++] = 0; out[ooff++] = 4;
  750. out[ooff++] = (jpgresint[0] >>> 8) & 255;
  751. out[ooff++] = jpgresint[0] & 255;
  752. }
  753. tables = new Uint8Array(out);
  754. }
  755. var sofpos = -1;
  756. i = 0;
  757. while(i < (tables.length - 1)) {
  758. if(tables[i]==255 && tables[i+1]==SOF0) { sofpos = i; break; }
  759. i++;
  760. }
  761. if(sofpos == -1)
  762. {
  763. var tmptab = new Uint8Array(tables.length + 10 + 3*spp);
  764. tmptab.set(tables);
  765. var tmpoff = tables.length;
  766. sofpos = tables.length;
  767. tables = tmptab;
  768. tables[tmpoff++] = 255; tables[tmpoff++] = SOF0;
  769. tables[tmpoff++] = 0; tables[tmpoff++] = 8 + 3*spp; tables[tmpoff++] = 8;
  770. tables[tmpoff++] = (img.height >>> 8) & 255; tables[tmpoff++] = img.height & 255;
  771. tables[tmpoff++] = (img.width >>> 8) & 255; tables[tmpoff++] = img.width & 255;
  772. tables[tmpoff++] = spp;
  773. if(spp==1) { tables[tmpoff++] = 1; tables[tmpoff++] = 17; tables[tmpoff++] = 0; }
  774. else for(i=0; i<3; i++)
  775. {
  776. tables[tmpoff++] = i + 1;
  777. tables[tmpoff++] = (i != 0) ? 17 : (((ssx & 15) << 4) | (ssy & 15));
  778. tables[tmpoff++] = i;
  779. }
  780. }
  781. if(data[soff]==255 && data[soff+1]==SOS)
  782. {
  783. var soslen = (data[soff+2]<<8) | data[soff+3];
  784. sosMarker = new Uint8Array(soslen+2);
  785. sosMarker[0] = data[soff]; sosMarker[1] = data[soff+1]; sosMarker[2] = data[soff+2]; sosMarker[3] = data[soff+3];
  786. for(i=0; i<(soslen-2); i++) sosMarker[i+4] = data[soff+i+4];
  787. }
  788. else
  789. {
  790. sosMarker = new Uint8Array(2 + 6 + 2*spp);
  791. var sosoff = 0;
  792. sosMarker[sosoff++] = 255; sosMarker[sosoff++] = SOS;
  793. sosMarker[sosoff++] = 0; sosMarker[sosoff++] = 6 + 2*spp; sosMarker[sosoff++] = spp;
  794. if(spp==1) { sosMarker[sosoff++] = 1; sosMarker[sosoff++] = 0; }
  795. else for(i=0; i<3; i++)
  796. {
  797. sosMarker[sosoff++] = i+1; sosMarker[sosoff++] = (i << 4) | i;
  798. }
  799. sosMarker[sosoff++] = 0; sosMarker[sosoff++] = 63; sosMarker[sosoff++] = 0;
  800. }
  801. return { jpegOffset: off, tables: tables, sosMarker: sosMarker, sofPosition: sofpos };
  802. }
  803. UTIF.decode._decodeOldJPEG = function(img, data, off, len, tgt, toff)
  804. {
  805. var i, dlen, tlen, buff, buffoff;
  806. var jpegData = UTIF.decode._decodeOldJPEGInit(img, data, off, len);
  807. if(jpegData.jpegOffset!=null)
  808. {
  809. dlen = off+len-jpegData.jpegOffset;
  810. buff = new Uint8Array(dlen);
  811. for(i=0; i<dlen; i++) buff[i] = data[jpegData.jpegOffset+i];
  812. }
  813. else
  814. {
  815. tlen = jpegData.tables.length;
  816. buff = new Uint8Array(tlen + jpegData.sosMarker.length + len + 2);
  817. buff.set(jpegData.tables);
  818. buffoff = tlen;
  819. buff[jpegData.sofPosition+5] = (img.height >>> 8) & 255; buff[jpegData.sofPosition+6] = img.height & 255;
  820. buff[jpegData.sofPosition+7] = (img.width >>> 8) & 255; buff[jpegData.sofPosition+8] = img.width & 255;
  821. if(data[off]!=255 || data[off+1]!=SOS)
  822. {
  823. buff.set(jpegData.sosMarker, buffoff);
  824. buffoff += sosMarker.length;
  825. }
  826. for(i=0; i<len; i++) buff[buffoff++] = data[off+i];
  827. buff[buffoff++] = 255; buff[buffoff++] = EOI;
  828. }
  829. var parser = new UTIF.JpegDecoder(); parser.parse(buff);
  830. var decoded = parser.getData({"width":parser.width,"height":parser.height,"forceRGB":true,"isSourcePDF":false});
  831. for (var i=0; i<decoded.length; i++) tgt[toff + i] = decoded[i];
  832. // PhotometricInterpretation is 6 (YCbCr) for JPEG, but after decoding we populate data in
  833. // RGB format, so updating the tag value
  834. if(img["t262"] && img["t262"][0] == 6) img["t262"][0] = 2;
  835. }
  836. UTIF.decode._decodePackBits = function(data, off, len, tgt, toff)
  837. {
  838. var sa = new Int8Array(data.buffer), ta = new Int8Array(tgt.buffer), lim = off+len;
  839. while(off<lim)
  840. {
  841. var n = sa[off]; off++;
  842. if(n>=0 && n<128) for(var i=0; i< n+1; i++) { ta[toff]=sa[off]; toff++; off++; }
  843. if(n>=-127 && n<0) { for(var i=0; i<-n+1; i++) { ta[toff]=sa[off]; toff++; } off++; }
  844. }
  845. return toff;
  846. }
  847. UTIF.decode._decodeThunder = function(data, off, len, tgt, toff)
  848. {
  849. var d2 = [ 0, 1, 0, -1 ], d3 = [ 0, 1, 2, 3, 0, -3, -2, -1 ];
  850. var lim = off+len, qoff = toff*2, px = 0;
  851. while(off<lim)
  852. {
  853. var b = data[off], msk = (b>>>6), n = (b&63); off++;
  854. if(msk==3) { px=(n&15); tgt[qoff>>>1] |= (px<<(4*(1-qoff&1))); qoff++; }
  855. if(msk==0) for(var i=0; i<n; i++) { tgt[qoff>>>1] |= (px<<(4*(1-qoff&1))); qoff++; }
  856. if(msk==2) for(var i=0; i<2; i++) { var d=(n>>>(3*(1-i)))&7; if(d!=4) { px+=d3[d]; tgt[qoff>>>1] |= (px<<(4*(1-qoff&1))); qoff++; } }
  857. if(msk==1) for(var i=0; i<3; i++) { var d=(n>>>(2*(2-i)))&3; if(d!=2) { px+=d2[d]; tgt[qoff>>>1] |= (px<<(4*(1-qoff&1))); qoff++; } }
  858. }
  859. }
  860. UTIF.decode._dmap = { "1":0,"011":1,"000011":2,"0000011":3, "010":-1,"000010":-2,"0000010":-3 };
  861. UTIF.decode._lens = ( function()
  862. {
  863. var addKeys = function(lens, arr, i0, inc) { for(var i=0; i<arr.length; i++) lens[arr[i]] = i0 + i*inc; }
  864. var termW = "00110101,000111,0111,1000,1011,1100,1110,1111,10011,10100,00111,01000,001000,000011,110100,110101," // 15
  865. + "101010,101011,0100111,0001100,0001000,0010111,0000011,0000100,0101000,0101011,0010011,0100100,0011000,00000010,00000011,00011010," // 31
  866. + "00011011,00010010,00010011,00010100,00010101,00010110,00010111,00101000,00101001,00101010,00101011,00101100,00101101,00000100,00000101,00001010," // 47
  867. + "00001011,01010010,01010011,01010100,01010101,00100100,00100101,01011000,01011001,01011010,01011011,01001010,01001011,00110010,00110011,00110100";
  868. var termB = "0000110111,010,11,10,011,0011,0010,00011,000101,000100,0000100,0000101,0000111,00000100,00000111,000011000," // 15
  869. + "0000010111,0000011000,0000001000,00001100111,00001101000,00001101100,00000110111,00000101000,00000010111,00000011000,000011001010,000011001011,000011001100,000011001101,000001101000,000001101001," // 31
  870. + "000001101010,000001101011,000011010010,000011010011,000011010100,000011010101,000011010110,000011010111,000001101100,000001101101,000011011010,000011011011,000001010100,000001010101,000001010110,000001010111," // 47
  871. + "000001100100,000001100101,000001010010,000001010011,000000100100,000000110111,000000111000,000000100111,000000101000,000001011000,000001011001,000000101011,000000101100,000001011010,000001100110,000001100111";
  872. var makeW = "11011,10010,010111,0110111,00110110,00110111,01100100,01100101,01101000,01100111,011001100,011001101,011010010,011010011,011010100,011010101,011010110,"
  873. + "011010111,011011000,011011001,011011010,011011011,010011000,010011001,010011010,011000,010011011";
  874. var makeB = "0000001111,000011001000,000011001001,000001011011,000000110011,000000110100,000000110101,0000001101100,0000001101101,0000001001010,0000001001011,0000001001100,"
  875. + "0000001001101,0000001110010,0000001110011,0000001110100,0000001110101,0000001110110,0000001110111,0000001010010,0000001010011,0000001010100,0000001010101,0000001011010,"
  876. + "0000001011011,0000001100100,0000001100101";
  877. var makeA = "00000001000,00000001100,00000001101,000000010010,000000010011,000000010100,000000010101,000000010110,000000010111,000000011100,000000011101,000000011110,000000011111";
  878. termW = termW.split(","); termB = termB.split(","); makeW = makeW.split(","); makeB = makeB.split(","); makeA = makeA.split(",");
  879. var lensW = {}, lensB = {};
  880. addKeys(lensW, termW, 0, 1); addKeys(lensW, makeW, 64,64); addKeys(lensW, makeA, 1792,64);
  881. addKeys(lensB, termB, 0, 1); addKeys(lensB, makeB, 64,64); addKeys(lensB, makeA, 1792,64);
  882. return [lensW, lensB];
  883. } )();
  884. UTIF.decode._decodeG4 = function(data, off, slen, tgt, toff, w, fo)
  885. {
  886. var U = UTIF.decode, boff=off<<3, len=0, wrd=""; // previous starts with 1
  887. var line=[], pline=[]; for(var i=0; i<w; i++) pline.push(0); pline=U._makeDiff(pline);
  888. var a0=0, a1=0, a2=0, b1=0, b2=0, clr=0;
  889. var y=0, mode="", toRead=0;
  890. var bipl = Math.ceil(w/8)*8;
  891. while((boff>>>3)<off+slen)
  892. {
  893. b1 = U._findDiff(pline, a0+(a0==0?0:1), 1-clr), b2 = U._findDiff(pline, b1, clr); // could be precomputed
  894. var bit =0;
  895. if(fo==1) bit = (data[boff>>>3]>>>(7-(boff&7)))&1;
  896. if(fo==2) bit = (data[boff>>>3]>>>( (boff&7)))&1;
  897. boff++; wrd+=bit;
  898. if(mode=="H")
  899. {
  900. if(U._lens[clr][wrd]!=null)
  901. {
  902. var dl=U._lens[clr][wrd]; wrd=""; len+=dl;
  903. if(dl<64) { U._addNtimes(line,len,clr); a0+=len; clr=1-clr; len=0; toRead--; if(toRead==0) mode=""; }
  904. }
  905. }
  906. else
  907. {
  908. if(wrd=="0001") { wrd=""; U._addNtimes(line,b2-a0,clr); a0=b2; }
  909. if(wrd=="001" ) { wrd=""; mode="H"; toRead=2; }
  910. if(U._dmap[wrd]!=null) { a1 = b1+U._dmap[wrd]; U._addNtimes(line, a1-a0, clr); a0=a1; wrd=""; clr=1-clr; }
  911. }
  912. if(line.length==w && mode=="")
  913. {
  914. U._writeBits(line, tgt, toff*8+y*bipl);
  915. clr=0; y++; a0=0;
  916. pline=U._makeDiff(line); line=[];
  917. }
  918. //if(wrd.length>150) { log(wrd); break; throw "e"; }
  919. }
  920. }
  921. UTIF.decode._findDiff = function(line, x, clr) { for(var i=0; i<line.length; i+=2) if(line[i]>=x && line[i+1]==clr) return line[i]; }
  922. UTIF.decode._makeDiff = function(line)
  923. {
  924. var out = []; if(line[0]==1) out.push(0,1);
  925. for(var i=1; i<line.length; i++) if(line[i-1]!=line[i]) out.push(i, line[i]);
  926. out.push(line.length,0,line.length,1); return out;
  927. }
  928. UTIF.decode._decodeG2 = function(data, off, slen, tgt, toff, w, fo)
  929. {
  930. var U = UTIF.decode, boff=off<<3, len=0, wrd="";
  931. var line=[];
  932. var clr = 0;
  933. var y=0;
  934. var bipl = Math.ceil(w/8)*8;
  935. while((boff>>>3)<off+slen) {
  936. var bit =0;
  937. if(fo==1) bit = (data[boff>>>3]>>>(7-(boff&7)))&1;
  938. if(fo==2) bit = (data[boff>>>3]>>>( (boff&7)))&1;
  939. boff++; wrd+=bit;
  940. len = U._lens[clr][wrd];
  941. if(len!=null) {
  942. U._addNtimes(line,len,clr); wrd="";
  943. if(len<64) clr = 1-clr;
  944. if(line.length==w) { U._writeBits(line, tgt, toff*8+y*bipl); line=[]; y++; clr=0; if((boff&7)!=0) boff+=8-(boff&7); if(len>=64) boff+=8; }
  945. }
  946. }
  947. }
  948. UTIF.decode._decodeG3 = function(data, off, slen, tgt, toff, w, fo, twoDim)
  949. {
  950. var U = UTIF.decode, boff=off<<3, len=0, wrd="";
  951. var line=[], pline=[]; for(var i=0; i<w; i++) line.push(0);
  952. var a0=0, a1=0, a2=0, b1=0, b2=0, clr=0;
  953. var y=-1, mode="", toRead=0, is1D=true;
  954. var bipl = Math.ceil(w/8)*8;
  955. while((boff>>>3)<off+slen)
  956. {
  957. b1 = U._findDiff(pline, a0+(a0==0?0:1), 1-clr), b2 = U._findDiff(pline, b1, clr); // could be precomputed
  958. var bit =0;
  959. if(fo==1) bit = (data[boff>>>3]>>>(7-(boff&7)))&1;
  960. if(fo==2) bit = (data[boff>>>3]>>>( (boff&7)))&1;
  961. boff++; wrd+=bit;
  962. if(is1D)
  963. {
  964. if(U._lens[clr][wrd]!=null)
  965. {
  966. var dl=U._lens[clr][wrd]; wrd=""; len+=dl;
  967. if(dl<64) { U._addNtimes(line,len,clr); clr=1-clr; len=0; }
  968. }
  969. }
  970. else
  971. {
  972. if(mode=="H")
  973. {
  974. if(U._lens[clr][wrd]!=null)
  975. {
  976. var dl=U._lens[clr][wrd]; wrd=""; len+=dl;
  977. if(dl<64) { U._addNtimes(line,len,clr); a0+=len; clr=1-clr; len=0; toRead--; if(toRead==0) mode=""; }
  978. }
  979. }
  980. else
  981. {
  982. if(wrd=="0001") { wrd=""; U._addNtimes(line,b2-a0,clr); a0=b2; }
  983. if(wrd=="001" ) { wrd=""; mode="H"; toRead=2; }
  984. if(U._dmap[wrd]!=null) { a1 = b1+U._dmap[wrd]; U._addNtimes(line, a1-a0, clr); a0=a1; wrd=""; clr=1-clr; }
  985. }
  986. }
  987. if(wrd.endsWith("000000000001")) // needed for some files
  988. {
  989. if(y>=0) U._writeBits(line, tgt, toff*8+y*bipl);
  990. if(twoDim) {
  991. if(fo==1) is1D = ((data[boff>>>3]>>>(7-(boff&7)))&1)==1;
  992. if(fo==2) is1D = ((data[boff>>>3]>>>( (boff&7)))&1)==1;
  993. boff++;
  994. }
  995. //log("EOL",y, "next 1D:", is1D);
  996. wrd=""; clr=0; y++; a0=0;
  997. pline=U._makeDiff(line); line=[];
  998. }
  999. }
  1000. if(line.length==w) U._writeBits(line, tgt, toff*8+y*bipl);
  1001. }
  1002. UTIF.decode._addNtimes = function(arr, n, val) { for(var i=0; i<n; i++) arr.push(val); }
  1003. UTIF.decode._writeBits = function(bits, tgt, boff)
  1004. {
  1005. for(var i=0; i<bits.length; i++) tgt[(boff+i)>>>3] |= (bits[i]<<(7-((boff+i)&7)));
  1006. }
  1007. UTIF.decode._decodeLZW=UTIF.decode._decodeLZW=function(){var e,U,Z,u,K=0,V=0,g=0,N=0,O=function(){var S=e>>>3,A=U[S]<<16|U[S+1]<<8|U[S+2],j=A>>>24-(e&7)-V&(1<<V)-1;
  1008. e+=V;return j},h=new Uint32Array(4096*4),w=0,m=function(S){if(S==w)return;w=S;g=1<<S;N=g+1;for(var A=0;
  1009. A<N+1;A++){h[4*A]=h[4*A+3]=A;h[4*A+1]=65535;h[4*A+2]=1}},i=function(S){V=S+1;K=N+1},D=function(S){var A=S<<2,j=h[A+2],a=u+j-1;
  1010. while(A!=65535){Z[a--]=h[A];A=h[A+1]}u+=j},L=function(S,A){var j=K<<2,a=S<<2;h[j]=h[(A<<2)+3];h[j+1]=a;
  1011. h[j+2]=h[a+2]+1;h[j+3]=h[a+3];K++;if(K+1==1<<V&&V!=12)V++},T=function(S,A,j,a,n,q){e=A<<3;U=S;Z=a;u=n;
  1012. var B=A+j<<3,_=0,t=0;m(q);i(q);while(e<B&&(_=O())!=N){if(_==g){i(q);_=O();if(_==N)break;D(_)}else{if(_<K){D(_);
  1013. L(t,_)}else{L(t,t);D(K-1)}}t=_}return u};return T}();
  1014. UTIF.tags = {};
  1015. //UTIF.ttypes = { 256:3,257:3,258:3, 259:3, 262:3, 273:4, 274:3, 277:3,278:4,279:4, 282:5, 283:5, 284:3, 286:5,287:5, 296:3, 305:2, 306:2, 338:3, 513:4, 514:4, 34665:4 };
  1016. // start at tag 250
  1017. UTIF._types = function() {
  1018. var main = new Array(250); main.fill(0);
  1019. main = main.concat([0,0,0,0,4,3,3,3,3,3,0,0,3,0,0,0,3,0,0,2,2,2,2,4,3,0,0,3,4,4,3,3,5,5,3,2,5,5,0,0,0,0,4,4,0,0,3,3,0,0,0,0,0,0,0,2,2,0,0,0,0,0,0,0,0,2,2,3,5,5,3,0,3,3,4,4,4,3,4,0,0,0,0,0,0,0,0,0,3,3,0,0,0,0,0,0,0,7,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,3,5,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,7,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]);
  1020. var rest = {33432: 2, 33434: 5, 33437: 5, 34665: 4, 34850: 3, 34853: 4, 34855: 3, 34864: 3, 34866: 4, 36864: 7, 36867: 2, 36868: 2, 37121: 7, 37377: 10, 37378: 5, 37380: 10, 37381: 5, 37383: 3, 37384: 3, 37385: 3, 37386: 5, 37510: 7, 37520: 2, 37521: 2, 37522: 2, 40960: 7, 40961: 3, 40962: 4, 40963: 4, 40965: 4, 41486: 5, 41487: 5, 41488: 3, 41985: 3, 41986: 3, 41987: 3, 41988: 5, 41989: 3, 41990: 3, 41993: 3, 41994: 3, 41995: 7, 41996: 3, 42032: 2, 42033: 2, 42034: 5, 42036: 2, 42037: 2, 59932: 7};
  1021. return {
  1022. basic: {
  1023. main: main,
  1024. rest: rest
  1025. },
  1026. gps: {
  1027. main: [1,2,5,2,5,1,5,5,0,9],
  1028. rest: {18:2,29:2}
  1029. }
  1030. }
  1031. }();
  1032. UTIF._readIFD = function(bin, data, offset, ifds, depth, prm)
  1033. {
  1034. var cnt = bin.readUshort(data, offset); offset+=2;
  1035. var ifd = {};
  1036. if(prm.debug) log(" ".repeat(depth),ifds.length-1,">>>----------------");
  1037. for(var i=0; i<cnt; i++)
  1038. {
  1039. var tag = bin.readUshort(data, offset); offset+=2;
  1040. var type = bin.readUshort(data, offset); offset+=2;
  1041. var num = bin.readUint (data, offset); offset+=4;
  1042. var voff = bin.readUint (data, offset); offset+=4;
  1043. var arr = [];
  1044. //ifd["t"+tag+"-"+UTIF.tags[tag]] = arr;
  1045. if(type== 1 || type==7) { arr = new Uint8Array(data.buffer, (num<5 ? offset-4 : voff), num); }
  1046. if(type== 2) { var o0 = (num<5 ? offset-4 : voff), c=data[o0], len=Math.max(0, Math.min(num-1,data.length-o0));
  1047. if(c<128 || len==0) arr.push( bin.readASCII(data, o0, len) );
  1048. else arr = new Uint8Array(data.buffer, o0, len); }
  1049. if(type== 3) { for(var j=0; j<num; j++) arr.push(bin.readUshort(data, (num<3 ? offset-4 : voff)+2*j)); }
  1050. if(type== 4
  1051. || type==13) { for(var j=0; j<num; j++) arr.push(bin.readUint (data, (num<2 ? offset-4 : voff)+4*j)); }
  1052. if(type== 5 || type==10) {
  1053. var ri = type==5 ? bin.readUint : bin.readInt;
  1054. for(var j=0; j<num; j++) arr.push([ri(data, voff+j*8), ri(data,voff+j*8+4)]); }
  1055. if(type== 8) { for(var j=0; j<num; j++) arr.push(bin.readShort (data, (num<3 ? offset-4 : voff)+2*j)); }
  1056. if(type== 9) { for(var j=0; j<num; j++) arr.push(bin.readInt (data, (num<2 ? offset-4 : voff)+4*j)); }
  1057. if(type==11) { for(var j=0; j<num; j++) arr.push(bin.readFloat (data, voff+j*4)); }
  1058. if(type==12) { for(var j=0; j<num; j++) arr.push(bin.readDouble(data, voff+j*8)); }
  1059. if(num!=0 && arr.length==0) { log(tag, "unknown TIFF tag type: ", type, "num:",num); if(i==0)return; continue; }
  1060. if(prm.debug) log(" ".repeat(depth), tag, type, UTIF.tags[tag], arr);
  1061. ifd["t"+tag] = arr;
  1062. if(tag==330 && ifd["t272"] && ifd["t272"][0]=="DSLR-A100") { }
  1063. else if(tag==330 || tag==34665 || tag==34853 || (tag==50740 && bin.readUshort(data,bin.readUint(arr,0))<300 ) ||tag==61440) {
  1064. var oarr = tag==50740 ? [bin.readUint(arr,0)] : arr;
  1065. var subfd = [];
  1066. for(var j=0; j<oarr.length; j++) UTIF._readIFD(bin, data, oarr[j], subfd, depth+1, prm);
  1067. if(tag== 330) ifd.subIFD = subfd;
  1068. if(tag==34665) ifd.exifIFD = subfd[0];
  1069. if(tag==34853) ifd.gpsiIFD = subfd[0]; //console.log("gps", subfd[0]); }
  1070. if(tag==50740) ifd.dngPrvt = subfd[0];
  1071. if(tag==61440) ifd.fujiIFD = subfd[0];
  1072. }
  1073. if(tag==37500 && prm.parseMN) {
  1074. var mn = arr;
  1075. //console.log(bin.readASCII(mn,0,mn.length), mn);
  1076. if(bin.readASCII(mn,0,5)=="Nikon") ifd.makerNote = UTIF["decode"](mn.slice(10).buffer)[0];
  1077. else if(bin.readUshort(data,voff)<300 && bin.readUshort(data,voff+4)<=12){
  1078. var subsub=[]; UTIF._readIFD(bin, data, voff, subsub, depth+1, prm);
  1079. ifd.makerNote = subsub[0];
  1080. }
  1081. }
  1082. }
  1083. ifds.push(ifd);
  1084. if(prm.debug) log(" ".repeat(depth),"<<<---------------");
  1085. return offset;
  1086. }
  1087. UTIF._writeIFD = function(bin, types, data, offset, ifd)
  1088. {
  1089. var keys = Object.keys(ifd), knum=keys.length; if(ifd["exifIFD"]) knum--; if(ifd["gpsiIFD"]) knum--;
  1090. bin.writeUshort(data, offset, knum); offset+=2;
  1091. var eoff = offset + knum*12 + 4;
  1092. for(var ki=0; ki<keys.length; ki++)
  1093. {
  1094. var key = keys[ki]; if(key=="t34665" || key=="t34853") continue;
  1095. if(key=="exifIFD") key="t34665"; if(key=="gpsiIFD") key="t34853";
  1096. var tag = parseInt(key.slice(1)), type = types.main[tag]; if(type==null) type=types.rest[tag];
  1097. if(type==null || type==0) throw new Error("unknown type of tag: "+tag);
  1098. //console.log(offset+":", tag, type, eoff);
  1099. var val = ifd[key];
  1100. if(tag==34665) {
  1101. var outp = UTIF._writeIFD(bin, types, data, eoff, ifd["exifIFD"]);
  1102. val = [eoff]; eoff = outp[1];
  1103. }
  1104. if(tag==34853) {
  1105. var outp = UTIF._writeIFD(bin, UTIF._types.gps, data, eoff, ifd["gpsiIFD"]);
  1106. val = [eoff]; eoff = outp[1];
  1107. }
  1108. if(type==2) val=val[0]+"\u0000"; var num = val.length;
  1109. bin.writeUshort(data, offset, tag ); offset+=2;
  1110. bin.writeUshort(data, offset, type); offset+=2;
  1111. bin.writeUint (data, offset, num ); offset+=4;
  1112. var dlen = [-1, 1, 1, 2, 4, 8, 0, 1, 0, 4, 8, 0, 8][type] * num; //if(dlen<1) throw "e";
  1113. var toff = offset;
  1114. if(dlen>4) { bin.writeUint(data, offset, eoff); toff=eoff; }
  1115. if (type== 1 || type==7) { for(var i=0; i<num; i++) data[toff+i] = val[i]; }
  1116. else if(type== 2) { bin.writeASCII(data, toff, val); }
  1117. else if(type== 3) { for(var i=0; i<num; i++) bin.writeUshort(data, toff+2*i, val[i]); }
  1118. else if(type== 4) { for(var i=0; i<num; i++) bin.writeUint (data, toff+4*i, val[i]); }
  1119. else if(type== 5 || type==10) {
  1120. var wr = type==5?bin.writeUint:bin.writeInt;
  1121. for(var i=0; i<num; i++) {
  1122. var v=val[i],nu=v[0],de=v[1]; if(nu==null) throw "e"; wr(data, toff+8*i, nu); wr(data, toff+8*i+4, de); } }
  1123. else if(type== 9) { for(var i=0; i<num; i++) bin.writeInt (data, toff+4*i, val[i]); }
  1124. else if(type==12) { for(var i=0; i<num; i++) bin.writeDouble(data, toff+8*i, val[i]); }
  1125. else throw type;
  1126. if(dlen>4) { dlen += (dlen&1); eoff += dlen; }
  1127. offset += 4;
  1128. }
  1129. return [offset, eoff];
  1130. }
  1131. UTIF.toRGBA8 = function(out, scl)
  1132. {
  1133. var w = out.width, h = out.height, area = w*h, qarea = area*4, data = out.data;
  1134. var img = new Uint8Array(area*4);
  1135. //console.log(out);
  1136. // 0: WhiteIsZero, 1: BlackIsZero, 2: RGB, 3: Palette color, 4: Transparency mask, 5: CMYK
  1137. var intp = (out["t262"] ? out["t262"][0]: 2), bps = (out["t258"]?Math.min(32,out["t258"][0]):1);
  1138. if(out["t262"]==null && bps==1) intp=0;
  1139. //log("interpretation: ", intp, "bps", bps, out);
  1140. if(false) {}
  1141. else if(intp==0)
  1142. {
  1143. var bpl = Math.ceil(bps*w/8);
  1144. for(var y=0; y<h; y++) {
  1145. var off = y*bpl, io = y*w;
  1146. if(bps== 1) for(var i=0; i<w; i++) { var qi=(io+i)<<2, px=((data[off+(i>>3)])>>(7- (i&7)))& 1; img[qi]=img[qi+1]=img[qi+2]=( 1-px)*255; img[qi+3]=255; }
  1147. if(bps== 4) for(var i=0; i<w; i++) { var qi=(io+i)<<2, px=((data[off+(i>>1)])>>(4-4*(i&1)))&15; img[qi]=img[qi+1]=img[qi+2]=(15-px)* 17; img[qi+3]=255; }
  1148. if(bps== 8) for(var i=0; i<w; i++) { var qi=(io+i)<<2, px=data[off+i]; img[qi]=img[qi+1]=img[qi+2]=255-px; img[qi+3]=255; }
  1149. }
  1150. }
  1151. else if(intp==1)
  1152. {
  1153. var smpls = out["t258"]?out["t258"].length : 1;
  1154. var bpl = Math.ceil(smpls*bps*w/8);
  1155. if(scl==null) scl=1/256;
  1156. for(var y=0; y<h; y++) {
  1157. var off = y*bpl, io = y*w;
  1158. if(bps== 1) for(var i=0; i<w; i++) { var qi=(io+i)<<2, px=((data[off+(i>>3)])>>(7- (i&7)))&1; img[qi]=img[qi+1]=img[qi+2]=(px)*255; img[qi+3]=255; }
  1159. if(bps== 2) for(var i=0; i<w; i++) { var qi=(io+i)<<2, px=((data[off+(i>>2)])>>(6-2*(i&3)))&3; img[qi]=img[qi+1]=img[qi+2]=(px)* 85; img[qi+3]=255; }
  1160. if(bps== 8) for(var i=0; i<w; i++) { var qi=(io+i)<<2, px=data[off+i*smpls]; img[qi]=img[qi+1]=img[qi+2]= px; img[qi+3]=255; }
  1161. if(bps==16) for(var i=0; i<w; i++) { var qi=(io+i)<<2, o=off+(2*i), px=(data[o+1]<<8)|data[o]; img[qi]=img[qi+1]=img[qi+2]= Math.min(255,~~(px*scl)); img[qi+3]=255; } // ladoga.tif
  1162. }
  1163. }
  1164. else if(intp==2)
  1165. {
  1166. var smpls = out["t277"]?out["t277"][0] : (out["t258"]?out["t258"].length : 3);
  1167. if(bps== 8)
  1168. {
  1169. if(smpls==1) for(var i=0; i<area; i++) { img[4*i]=img[4*i+1]=img[4*i+2]=data[i]; img[4*i+3]=255; }
  1170. if(smpls==4) for(var i=0; i<qarea; i++) img[i] = data[i];
  1171. if(smpls==3) for(var i=0; i<area; i++) { var qi=i<<2, ti=i*3; img[qi]=data[ti]; img[qi+1]=data[ti+1]; img[qi+2]=data[ti+2]; img[qi+3]=255; }
  1172. }
  1173. else if(bps==16){ // 3x 16-bit channel
  1174. if(smpls==4) for(var i=0; i<area; i++) { var qi=i<<2, ti=i*8+1; img[qi]=data[ti]; img[qi+1]=data[ti+2]; img[qi+2]=data[ti+4]; img[qi+3]=data[ti+6]; }
  1175. if(smpls==3) for(var i=0; i<area; i++) { var qi=i<<2, ti=i*6+1; img[qi]=data[ti]; img[qi+1]=data[ti+2]; img[qi+2]=data[ti+4]; img[qi+3]=255; }
  1176. }
  1177. else if(bps==32){
  1178. if(!img.isLE) for(var i=0; i<data.length; i+=4) { var t=data[i]; data[i]=data[i+3]; data[i+3]=t; t=data[i+1]; data[i+1]=data[i+2]; data[i+2]=t; }
  1179. var ndt = new Float32Array(data.buffer); // t339 is [3,3,3]
  1180. if(smpls==3) for(var i=0; i<area; i++) { var qi=i<<2, ti=i*3;
  1181. img[qi]=~~(0.5+ndt[ti]*255); img[qi+1]=~~(0.5+ndt[ti+1]*255); img[qi+2]=~~(0.5+ndt[ti+2]*255); img[qi+3]=255; }
  1182. else throw smpls;
  1183. }
  1184. else throw bps;
  1185. }
  1186. else if(intp==3)
  1187. {
  1188. var map = out["t320"];
  1189. var smpls = out["t277"]?out["t277"][0] : (out["t258"]?out["t258"].length : 1);
  1190. var bpl = Math.ceil(smpls*bps*w/8);
  1191. var cn = 1<<bps;
  1192. var nexta = bps==8 && smpls>1 && out["t338"] && out["t338"][0]!=0;
  1193. for(var y=0; y<h; y++)
  1194. for(var x=0; x<w; x++) {
  1195. var i = y*w+x;
  1196. var qi=i<<2, mi=0;
  1197. var dof = y*bpl;
  1198. if(false) {}
  1199. else if(bps==1) mi=(data[dof+(x>>>3)]>>>(7- (x&7)))& 1;
  1200. else if(bps==2) mi=(data[dof+(x>>>2)]>>>(6-2*(x&3)))& 3;
  1201. else if(bps==4) mi=(data[dof+(x>>>1)]>>>(4-4*(x&1)))&15;
  1202. else if(bps==8) mi= data[dof+x*smpls];
  1203. else throw bps;
  1204. img[qi]=(map[mi]>>8); img[qi+1]=(map[cn+mi]>>8); img[qi+2]=(map[cn+cn+mi]>>8); img[qi+3]=nexta ? data[dof+x*smpls+1] : 255;
  1205. }
  1206. }
  1207. else if(intp==5)
  1208. {
  1209. var smpls = out["t258"]?out["t258"].length : 4;
  1210. var gotAlpha = smpls>4 ? 1 : 0;
  1211. for(var i=0; i<area; i++) {
  1212. var qi=i<<2, si=i*smpls;
  1213. if(UDOC) {
  1214. var C=data[si], M=data[si+1], Y=data[si+2], K=data[si+3];
  1215. var c = UDOC.C.cmykToRgb([C*(1/255), M*(1/255), Y*(1/255), K*(1/255)]);
  1216. img[qi] = ~~(0.5+255*c[0]); img[qi+1] = ~~(0.5+255*c[1]); img[qi+2] = ~~(0.5+255*c[2]);
  1217. }
  1218. else {
  1219. var C=255-data[si], M=255-data[si+1], Y=255-data[si+2], K=(255-data[si+3])*(1/255);
  1220. img[qi]=~~(C*K+0.5); img[qi+1]=~~(M*K+0.5); img[qi+2]=~~(Y*K+0.5);
  1221. }
  1222. img[qi+3]=255*(1-gotAlpha)+data[si+4]*gotAlpha;
  1223. }
  1224. }
  1225. else if(intp==6 && out["t278"]) { // only for DSC_1538.TIF
  1226. var rps = out["t278"][0];
  1227. for(var y=0; y<h; y+=rps) {
  1228. var i=(y*w), len = rps*w;
  1229. for(var j=0; j<len; j++) {
  1230. var qi = 4*(i+j), si = 3*i+4*(j>>>1);
  1231. var Y = data[si+(j&1)], Cb=data[si+2]-128, Cr=data[si+3]-128;
  1232. var r = Y + ( (Cr >> 2) + (Cr >> 3) + (Cr >> 5) ) ;
  1233. var g = Y - ( (Cb >> 2) + (Cb >> 4) + (Cb >> 5)) - ( (Cr >> 1) + (Cr >> 3) + (Cr >> 4) + (Cr >> 5)) ;
  1234. var b = Y + ( Cb + (Cb >> 1) + (Cb >> 2) + (Cb >> 6)) ;
  1235. img[qi ]=Math.max(0,Math.min(255,r));
  1236. img[qi+1]=Math.max(0,Math.min(255,g));
  1237. img[qi+2]=Math.max(0,Math.min(255,b));
  1238. img[qi+3]=255;
  1239. }
  1240. }
  1241. }
  1242. else if(intp==32845) {
  1243. function gamma(x) { return x < 0.0031308 ? 12.92 * x : 1.055 * Math.pow(x, 1.0 / 2.4) - 0.055; }
  1244. for(var y=0; y<h; y++)
  1245. for(var x=0; x<w; x++) {
  1246. var si = (y*w+x)*6, qi=(y*w+x)*4;
  1247. var L = (data[si+1]<<8) | data[si];
  1248. var L = Math.pow(2, (L + 0.5) / 256 - 64);
  1249. var u = (data[si+3] + 0.5) / 410;
  1250. var v = (data[si+5] + 0.5) / 410;
  1251. // Luv to xyY
  1252. var sX = (9 * u) / (6 * u - 16 * v + 12);
  1253. var sY = (4 * v) / (6 * u - 16 * v + 12);
  1254. var bY = L;
  1255. // xyY to XYZ
  1256. var X = (sX*bY)/sY, Y = bY, Z = (1-sX-sY)*bY/sY;
  1257. var r = 2.690*X -1.276*Y -0.414*Z
  1258. var g = -1.022*X +1.978*Y +0.044*Z
  1259. var b = 0.061*X -0.224*Y +1.163*Z
  1260. img[qi ] = gamma(Math.min(r,1))*255;
  1261. img[qi+1] = gamma(Math.min(g,1))*255;
  1262. img[qi+2] = gamma(Math.min(b,1))*255;
  1263. img[qi+3] = 255;
  1264. }
  1265. }
  1266. else log("Unknown Photometric interpretation: "+intp);
  1267. return img;
  1268. }
  1269. UTIF.replaceIMG = function(imgs)
  1270. {
  1271. if(imgs==null) imgs = document.getElementsByTagName("img");
  1272. var sufs = ["tif","tiff","dng","cr2","nef"]
  1273. for (var i=0; i<imgs.length; i++)
  1274. {
  1275. var img=imgs[i], src=img.getAttribute("src"); if(src==null) continue;
  1276. var suff=src.split(".").pop().toLowerCase();
  1277. if(sufs.indexOf(suff)==-1) continue;
  1278. var xhr = new XMLHttpRequest(); UTIF._xhrs.push(xhr); UTIF._imgs.push(img);
  1279. xhr.open("GET", src); xhr.responseType = "arraybuffer";
  1280. xhr.onload = UTIF._imgLoaded; xhr.send();
  1281. }
  1282. }
  1283. UTIF._xhrs = []; UTIF._imgs = [];
  1284. UTIF._imgLoaded = function(e) {
  1285. var ind = UTIF._xhrs.indexOf(e.target), img = UTIF._imgs[ind];
  1286. UTIF._xhrs.splice(ind,1); UTIF._imgs.splice(ind,1);
  1287. img.setAttribute("src",UTIF.bufferToURI(e.target.response));
  1288. }
  1289. UTIF.bufferToURI = function(buff) {
  1290. var ifds = UTIF.decode(buff); //console.log(ifds);
  1291. var vsns = ifds, ma=0, page=vsns[0]; if(ifds[0].subIFD) vsns = vsns.concat(ifds[0].subIFD);
  1292. for(var i=0; i<vsns.length; i++) {
  1293. var img = vsns[i];
  1294. if(img["t258"]==null || img["t258"].length<3) continue;
  1295. var ar = img["t256"]*img["t257"];
  1296. if(ar>ma) { ma=ar; page=img; }
  1297. }
  1298. UTIF.decodeImage(buff, page, ifds);
  1299. var rgba = UTIF.toRGBA8(page), w=page.width, h=page.height;
  1300. var cnv = document.createElement("canvas"); cnv.width=w; cnv.height=h;
  1301. var ctx = cnv.getContext("2d");
  1302. var imgd = new ImageData(new Uint8ClampedArray(rgba.buffer),w,h);
  1303. ctx.putImageData(imgd,0,0);
  1304. return cnv.toDataURL();
  1305. }
  1306. UTIF._binBE =
  1307. {
  1308. nextZero : function(data, o) { while(data[o]!=0) o++; return o; },
  1309. readUshort : function(buff, p) { return (buff[p]<< 8) | buff[p+1]; },
  1310. readShort : function(buff, p) { var a=UTIF._binBE.ui8; a[0]=buff[p+1]; a[1]=buff[p+0]; return UTIF._binBE. i16[0]; },
  1311. readInt : function(buff, p) { var a=UTIF._binBE.ui8; a[0]=buff[p+3]; a[1]=buff[p+2]; a[2]=buff[p+1]; a[3]=buff[p+0]; return UTIF._binBE. i32[0]; },
  1312. readUint : function(buff, p) { var a=UTIF._binBE.ui8; a[0]=buff[p+3]; a[1]=buff[p+2]; a[2]=buff[p+1]; a[3]=buff[p+0]; return UTIF._binBE.ui32[0]; },
  1313. readASCII : function(buff, p, l) { var s = ""; for(var i=0; i<l; i++) s += String.fromCharCode(buff[p+i]); return s; },
  1314. readFloat : function(buff, p) { var a=UTIF._binBE.ui8; for(var i=0;i<4;i++) a[i]=buff[p+3-i]; return UTIF._binBE.fl32[0]; },
  1315. readDouble : function(buff, p) { var a=UTIF._binBE.ui8; for(var i=0;i<8;i++) a[i]=buff[p+7-i]; return UTIF._binBE.fl64[0]; },
  1316. writeUshort: function(buff, p, n) { buff[p] = (n>> 8)&255; buff[p+1] = n&255; },
  1317. writeInt : function(buff, p, n) { var a=UTIF._binBE.ui8; UTIF._binBE.i32[0]=n; buff[p+3]=a[0]; buff[p+2]=a[1]; buff[p+1]=a[2]; buff[p+0]=a[3]; },
  1318. writeUint : function(buff, p, n) { buff[p] = (n>>24)&255; buff[p+1] = (n>>16)&255; buff[p+2] = (n>>8)&255; buff[p+3] = (n>>0)&255; },
  1319. writeASCII : function(buff, p, s) { for(var i = 0; i < s.length; i++) buff[p+i] = s.charCodeAt(i); },
  1320. writeDouble: function(buff, p, n)
  1321. {
  1322. UTIF._binBE.fl64[0] = n;
  1323. for (var i = 0; i < 8; i++) buff[p + i] = UTIF._binBE.ui8[7 - i];
  1324. }
  1325. }
  1326. UTIF._binBE.ui8 = new Uint8Array (8);
  1327. UTIF._binBE.i16 = new Int16Array (UTIF._binBE.ui8.buffer);
  1328. UTIF._binBE.i32 = new Int32Array (UTIF._binBE.ui8.buffer);
  1329. UTIF._binBE.ui32 = new Uint32Array (UTIF._binBE.ui8.buffer);
  1330. UTIF._binBE.fl32 = new Float32Array(UTIF._binBE.ui8.buffer);
  1331. UTIF._binBE.fl64 = new Float64Array(UTIF._binBE.ui8.buffer);
  1332. UTIF._binLE =
  1333. {
  1334. nextZero : UTIF._binBE.nextZero,
  1335. readUshort : function(buff, p) { return (buff[p+1]<< 8) | buff[p]; },
  1336. readShort : function(buff, p) { var a=UTIF._binBE.ui8; a[0]=buff[p+0]; a[1]=buff[p+1]; return UTIF._binBE. i16[0]; },
  1337. readInt : function(buff, p) { var a=UTIF._binBE.ui8; a[0]=buff[p+0]; a[1]=buff[p+1]; a[2]=buff[p+2]; a[3]=buff[p+3]; return UTIF._binBE. i32[0]; },
  1338. readUint : function(buff, p) { var a=UTIF._binBE.ui8; a[0]=buff[p+0]; a[1]=buff[p+1]; a[2]=buff[p+2]; a[3]=buff[p+3]; return UTIF._binBE.ui32[0]; },
  1339. readASCII : UTIF._binBE.readASCII,
  1340. readFloat : function(buff, p) { var a=UTIF._binBE.ui8; for(var i=0;i<4;i++) a[i]=buff[p+ i]; return UTIF._binBE.fl32[0]; },
  1341. readDouble : function(buff, p) { var a=UTIF._binBE.ui8; for(var i=0;i<8;i++) a[i]=buff[p+ i]; return UTIF._binBE.fl64[0]; },
  1342. writeUshort: function(buff, p, n) { buff[p] = (n)&255; buff[p+1] = (n>>8)&255; },
  1343. writeInt : function(buff, p, n) { var a=UTIF._binBE.ui8; UTIF._binBE.i32[0]=n; buff[p+0]=a[0]; buff[p+1]=a[1]; buff[p+2]=a[2]; buff[p+3]=a[3]; },
  1344. writeUint : function(buff, p, n) { buff[p] = (n>>>0)&255; buff[p+1] = (n>>>8)&255; buff[p+2] = (n>>>16)&255; buff[p+3] = (n>>>24)&255; },
  1345. writeASCII : UTIF._binBE.writeASCII
  1346. }
  1347. UTIF._copyTile = function(tb, tw, th, b, w, h, xoff, yoff)
  1348. {
  1349. //log("copyTile", tw, th, w, h, xoff, yoff);
  1350. var xlim = Math.min(tw, w-xoff);
  1351. var ylim = Math.min(th, h-yoff);
  1352. for(var y=0; y<ylim; y++)
  1353. {
  1354. var tof = (yoff+y)*w+xoff;
  1355. var sof = y*tw;
  1356. for(var x=0; x<xlim; x++) b[tof+x] = tb[sof+x];
  1357. }
  1358. }
  1359. UTIF.LosslessJpegDecode =function(){var o,_;function u(){return o[_++]}function G(){return o[_++]<<8|o[_++]}function Z(l){var A=u(),g=[0,0,0,255],K=[],L=8;
  1360. for(var t=0;t<16;t++)K[t]=u();for(var t=0;t<16;t++){for(var F=0;F<K[t];F++){var $=P(g,0,t+1,1);g[$+3]=u()}}var w=new Uint8Array(1<<L);
  1361. l[A]=[new Uint8Array(g),w];for(var t=0;t<1<<L;t++){var r=L,M=t,X=0,y=0;while(g[X+3]==255&&r!=0){y=M>>--r&1;
  1362. X=g[X+y]}w[t]=X}}function P(l,A,g,K){if(l[A+3]!=255)return 0;if(g==0)return A;for(var t=0;t<2;t++){if(l[A+t]==0){l[A+t]=l.length;
  1363. l.push(0,0,K,255)}var F=P(l,l[A+t],g-1,K+1);if(F!=0)return F}return 0}function f(l){var A=l.b,g=l.a;
  1364. while(A<25&&l.e<l.d){var K=l.data[l.e++];if(!l.c)l.e+=K+1>>>8;g=g<<8|K;A+=8}if(A<0)throw"e";l.b=A;l.a=g}function O(l,A){if(A.b<l)f(A);
  1365. return A.a>>(A.b-=l)&65535>>16-l}function B(l,A){var g=l[0],K=0,t=255,F=0;if(A.b<16)f(A);var $=A.a>>A.b-8&255;
  1366. K=l[1][$];t=g[K+3];A.b-=g[K+2];while(t==255){F=A.a>>--A.b&1;K=g[K+F];t=g[K+3]}return t}function D(l,A){if(l<32768>>16-A)l+=-(1<<A)+1;
  1367. return l}function h(l,A){var g=B(l,A);if(g==0)return 0;if(g==16)return-32768;var K=O(g,A);return D(K,g)}function a(l,A,g,K,t,F){for(var $=0;
  1368. $<F;$++){var _=$*A;for(var L=0;L<A;L+=t)for(var r=0;r<t;r++)l[_+L+r]=h(K[r],g)}}function j(l,A){return D(O(l,A),l)}function b(l,A,g,K,t){var F=o.length-_;
  1369. for(var $=0;$<F;$+=4){var L=o[_+$];o[_+$]=o[_+$+3];o[_+$+3]=L;var L=o[_+$+1];o[_+$+1]=o[_+$+2];o[_+$+2]=L}for(var w=0;
  1370. w<t;w++){var r=32768,M=32768;for(var X=0;X<A;X+=2){var y=B(K,g),z=B(K,g);if(y!=0)r+=j(y,g);if(z!=0)M+=j(z,g);
  1371. l[w*A+X]=r&65535;l[w*A+X+1]=M&65535}}}function x(l){o=l;_=0;if(G()!=65496)throw"e";var A=[],g=0,K=0,t=[],F=[],$=[],L=0,w=0,r=0;
  1372. while(!0){var M=G();if(M==65535){_--;continue}var X=G();if(M==65475){K=u();w=G();r=G();L=u();for(var y=0;
  1373. y<L;y++){var z=u(),q=u(),n=u();if(n!=0)throw"e";A[z]=[y,q>>4,q&15]}}else if(M==65476){var m=_+X-2;while(_<m)Z(F)}else if(M==65498){_++;
  1374. for(var y=0;y<L;y++){var p=u(),i=A[p];$[i[0]]=F[u()>>>4];t[i[0]]=i.slice(1)}g=u();_+=2;break}else{_+=X-2}}var T=K>8?Uint16Array:Uint8Array,Y=new T(w*r*L),d={b:0,a:0,c:g==8,e:_,data:o,d:o.length};
  1375. if(d.c)b(Y,r*L,d,$[0],w);else{var v=[],Q=0,C=0;for(var y=0;y<L;y++){var U=t[y],s=U[0],H=U[1];if(s>Q)Q=s;
  1376. if(H>C)C=H;v.push(s*H)}if(Q!=1||C!=1){var E=[],c=0;for(var y=0;y<L;y++){for(var R=0;R<v[y];R++)E.push($[y]);
  1377. c+=v[y]}var k=r/Q,e=w/C;a(Y,k*c,d,E,c,e);J(Y,g,k,e,c-2,c,c,K);var V=Y.slice(0);for(var H=0;H<w;H++)for(var s=0;
  1378. s<r;s++){var a5=(H*r+s)*L,a3=~~(H/C),a1=~~(s/Q),a6=a3*k+a1,I=0;for(var y=0;y<L;y++){var N=s&1,a2=H&1,a0=C==2?H&1:0,a4=a6*c+I+(y==0?C==1?N:N*2+a2:0);
  1379. Y[a5+y]=V[a4];I+=v[y]}}J(Y,g,r,w,0,1,L,K)}else{a(Y,r*L,d,$,L,w);J(Y,g,r,w,0,L,L,K)}}return Y}function J(l,A,g,K,t,F,L,w){var r=g*L;
  1380. for(var M=t;M<F;M++)l[M]+=1<<w-1;for(var X=L;X<r;X+=L)for(var M=t;M<F;M++)l[X+M]+=l[X+M-L];for(var y=1;
  1381. y<K;y++){var z=y*r;for(var M=t;M<F;M++)l[z+M]+=l[z+M-r];for(var X=L;X<r;X+=L){for(var M=t;M<F;M++){var q=z+X+M,n=q-r,m=l[q-L],S=0;
  1382. if(A==0)S=0;else if(A==1)S=m;else if(A==2)S=l[n];else if(A==3)S=l[n-L];else if(A==4)S=m+(l[n]-l[n-L]);
  1383. else if(A==5)S=m+(l[n]-l[n-L]>>>1);else if(A==6)S=l[n]+(m-l[n-L]>>>1);else if(A==7)S=m+l[n]>>>1;else throw A;
  1384. l[q]+=S}}}}return x}();
  1385. (function(){var G=0,F=1,i=2,b=3,J=4,N=5,E=6,s=7,c=8,T=9,a3=10,f=11,q=12,M=13,m=14,x=15,L=16,$=17,p=18;
  1386. function a5(t){var Z=UTIF._binBE.readUshort,u={b:Z(t,0),i:t[2],C:t[3],u:t[4],q:Z(t,5),k:Z(t,7),e:Z(t,9),l:Z(t,11),s:t[13],d:Z(t,14)};
  1387. if(u.b!=18771||u.i>1||u.q<6||u.q%6||u.e<768||u.e%24||u.l!=768||u.k<u.l||u.k%u.l||u.k-u.e>=u.l||u.s>16||u.s!=u.k/u.l||u.s!=Math.ceil(u.e/u.l)||u.d!=u.q/6||u.u!=12&&u.u!=14&&u.u!=16||u.C!=16&&u.C!=0){throw"Invalid data"}if(u.i==0){throw"Not implemented. We need this file!"}u.h=u.C==16;
  1388. u.m=(u.h?u.l*2/3:u.l>>>1)|0;u.A=u.m+2;u.f=64;u.g=(1<<u.u)-1;u.n=4*u.u;return u}function a7(t,Z){var u=new Array(Z.s),e=4*Z.s,Q=16+e;
  1389. if(e&12)Q+=16-(e&12);for(var V=0,O=16;V<Z.s;O+=4){var o=UTIF._binBE.readUint(t,O);u[V]=t.slice(Q,Q+o);u[V].j=0;u[V].a=0;
  1390. Q+=o;V++}if(Q!=t.length)throw"Invalid data";return u}function a6(t,Z){for(var u=-Z[4],e=0;u<=Z[4];e++,u++){t[e]=u<=-Z[3]?-4:u<=-Z[2]?-3:u<=-Z[1]?-2:u<-Z[0]?-1:u<=Z[0]?0:u<Z[1]?1:u<Z[2]?2:u<Z[3]?3:4}}function a1(t,Z,u){var e=[Z,3*Z+18,5*Z+67,7*Z+276,u];
  1391. t.o=Z;t.w=(e[4]+2*Z)/(2*Z+1)+1|0;t.v=Math.ceil(Math.log2(t.w));t.t=9;a6(t.c,e)}function a2(t){var Z={c:new Int8Array(2<<t.u)};
  1392. a1(Z,0,t.g);return Z}function D(t){var Z=[[],[],[]],u=Math.max(2,t.w+32>>>6);for(var e=0;e<3;e++){for(var Q=0;
  1393. Q<41;Q++){Z[e][Q]=[u,1]}}return Z}function a4(t){for(var Z=-1,u=0;!u;Z++){u=t[t.j]>>>7-t.a&1;t.a++;t.a&=7;
  1394. if(!t.a)t.j++}return Z}function K(t,Z){var u=0,e=8-t.a,Q=t.j,V=t.a;if(Z){if(Z>=e){do{u<<=e;Z-=e;u|=t[t.j]&(1<<e)-1;
  1395. t.j++;e=8}while(Z>=8)}if(Z){u<<=Z;e-=Z;u|=t[t.j]>>>e&(1<<Z)-1}t.a=8-e}return u}function a0(t,Z){var u=0;
  1396. if(Z<t){while(u<=14&&Z<<++u<t);}return u}function r(t,Z,u,e,Q,V,O,o){if(o==null)o=0;var X=V+1,k=X%2,j=0,I=0,a=0,l,R,w=e[Q],S=e[Q-1],H=e[Q-2][X],g=S[X-1],Y=S[X],P=S[X+1],A=w[X-1],v=w[X+1],y=Math.abs,d,C,n,h;
  1397. if(k){d=y(P-Y);C=y(H-Y);n=y(g-Y)}if(k){h=d>n&&C<d?H+g:d<n&&C<n?H+P:P+g;h=h+2*Y>>>2;if(o){w[X]=h;return}l=Z.t*Z.c[t.g+Y-H]+Z.c[t.g+g-Y]}else{h=Y>g&&Y>P||Y<g&&Y<P?v+A+2*Y>>>2:A+v>>>1;
  1398. l=Z.t*Z.c[t.g+Y-g]+Z.c[t.g+g-A]}R=y(l);var W=a4(u);if(W<t.n-Z.v-1){var z=a0(O[R][0],O[R][1]);a=K(u,z)+(W<<z)}else{a=K(u,Z.v)+1}a=a&1?-1-(a>>>1):a>>>1;
  1399. O[R][0]+=y(a);if(O[R][1]==t.f){O[R][0]>>>=1;O[R][1]>>>=1}O[R][1]++;h=l<0?h-a:h+a;if(t.i){if(h<0)h+=Z.w;
  1400. else if(h>t.g)h-=Z.w}w[X]=h>=0?Math.min(h,t.g):0}function U(t,Z,u){var e=t[0].length;for(var Q=Z;Q<=u;
  1401. Q++){t[Q][0]=t[Q-1][1];t[Q][e-1]=t[Q-1][e-2]}}function B(t){U(t,s,q);U(t,i,J);U(t,x,$)}function _(t,Z,u,e,Q,V,O,o,X,k,j,I,a){var l=0,R=1,w=Q<M&&Q>J;
  1402. while(R<t.m){if(l<t.m){r(t,Z,u,e,Q,l,O[X],t.h&&(w&&k||!w&&(j||(l&I)==a)));r(t,Z,u,e,V,l,O[X],t.h&&(!w&&k||w&&(j||(l&I)==a)));
  1403. l+=2}if(l>8){r(t,Z,u,e,Q,R,o[X]);r(t,Z,u,e,V,R,o[X]);R+=2}}B(e)}function a8(t,Z,u,e,Q,V){_(t,Z,u,e,i,s,Q,V,0,0,1,0,8);
  1404. _(t,Z,u,e,c,x,Q,V,1,0,1,0,8);_(t,Z,u,e,b,T,Q,V,2,1,0,3,0);_(t,Z,u,e,a3,L,Q,V,0,0,0,3,2);_(t,Z,u,e,J,f,Q,V,1,0,0,3,2);
  1405. _(t,Z,u,e,q,$,Q,V,2,1,0,3,0)}function a9(t,Z,u,e,Q,V){var O=V.length,o=t.l;if(Q+1==t.s)o=t.e-Q*t.l;var X=6*t.e*e+Q*t.l;
  1406. for(var k=0;k<6;k++){for(var j=0;j<o;j++){var I=V[k%O][j%O],a;if(I==0){a=i+(k>>>1)}else if(I==2){a=x+(k>>>1)}else{a=s+k}var l=t.h?(j*2/3&2147483646|j%3&1)+(j%3>>>1):j>>>1;
  1407. Z[X+j]=u[a][l+1]}X+=t.e}}UTIF._decompressRAF=function(t,Z){var u=a5(t),e=a7(t,u),Q=a2(u),V=new Int16Array(u.e*u.q);
  1408. if(Z==null){Z=u.h?[[1,1,0,1,1,2],[1,1,2,1,1,0],[2,0,1,0,2,1],[1,1,2,1,1,0],[1,1,0,1,1,2],[0,2,1,2,0,1]]:[[0,1],[3,2]]}var O=[[G,b],[F,J],[N,f],[E,q],[M,L],[m,$]],o=[];
  1409. for(var X=0;X<p;X++){o[X]=new Uint16Array(u.A)}for(var k=0;k<u.s;k++){var j=D(Q),I=D(Q);for(var X=0;
  1410. X<p;X++){for(var a=0;a<u.A;a++){o[X][a]=0}}for(var l=0;l<u.d;l++){a8(u,Q,e[k],o,j,I);for(var X=0;X<6;
  1411. X++){for(var a=0;a<u.A;a++){o[O[X][0]][a]=o[O[X][1]][a]}}a9(u,V,o,l,k,Z);for(var X=i;X<p;X++){if([N,E,M,m].indexOf(X)==-1){for(var a=0;
  1412. a<u.A;a++){o[X][a]=0}}}B(o)}}return V}}())
  1413. })(UTIF, pako);
  1414. })();
  1415. export default UTIF;