UNPKG

geotiff

Version:

GeoTIFF image decoding in JavaScript

1 lines 551 kB
!function(A,I){"object"==typeof exports&&"undefined"!=typeof module?I(exports):"function"==typeof define&&define.amd?define(["exports"],I):I((A="undefined"!=typeof globalThis?globalThis:A||self).GeoTIFF={})}(this,(function(A){"use strict";function I(A){return(I,...g)=>B(A,I,g)}function g(A,g){return I(i(A,g).get)}const{apply:B,construct:C,defineProperty:Q,get:E,getOwnPropertyDescriptor:i,getPrototypeOf:e,has:t,ownKeys:o,set:a,setPrototypeOf:s}=Reflect,{iterator:r,species:n,toStringTag:h,for:D}=Symbol,w=Object,{create:c,defineProperty:l,freeze:y,is:d}=w,S=Array.prototype[r],G=I(S),f=ArrayBuffer;g(f.prototype,"byteLength");const N="undefined"!=typeof SharedArrayBuffer?SharedArrayBuffer:null;N&&g(N.prototype,"byteLength");const k=e(Uint8Array);k.from;const F=k.prototype;F[r],I(F.keys),I(F.values),I(F.entries),I(F.set),I(F.reverse),I(F.fill),I(F.copyWithin),I(F.sort),I(F.slice),I(F.subarray),g(F,"buffer"),g(F,"byteOffset"),g(F,"length"),g(F,h);const R=Uint8Array,U=Uint16Array,u=Uint32Array,L=Float32Array,Y=e([][r]()),K=I(Y.next),M=I(function*(){}().next),p=e(Y),m=I(DataView.prototype.getUint16),J=WeakMap,H=J.prototype,b=I(H.get),q=I(H.set),x=new J,T=c(null,{next:{value:function(){const A=b(x,this);return K(A)}},[r]:{value:function(){return this}}});const O=new J,_=c(p,{next:{value:function(){const A=b(O,this);return M(A)},writable:!0,configurable:!0}});for(const A of o(Y))"next"!==A&&l(_,A,i(Y,A));const P=new f(4),v=new L(P),Z=new u(P),j=new U(512),V=new R(512);for(let A=0;A<256;++A){const I=A-127;I<-24?(j[A]=0,j[256|A]=32768,V[A]=24,V[256|A]=24):I<-14?(j[A]=1024>>-I-14,j[256|A]=1024>>-I-14|32768,V[A]=-I-1,V[256|A]=-I-1):I<=15?(j[A]=I+15<<10,j[256|A]=I+15<<10|32768,V[A]=13,V[256|A]=13):I<128?(j[A]=31744,j[256|A]=64512,V[A]=24,V[256|A]=24):(j[A]=31744,j[256|A]=64512,V[A]=13,V[256|A]=13)}const W=new u(2048);for(let A=1;A<1024;++A){let I=A<<13,g=0;for(;0==(8388608&I);)I<<=1,g-=8388608;I&=-8388609,g+=947912704,W[A]=I|g}for(let A=1024;A<2048;++A)W[A]=939524096+(A-1024<<13);const z=new u(64);for(let A=1;A<31;++A)z[A]=A<<23;z[31]=1199570944,z[32]=2147483648;for(let A=33;A<63;++A)z[A]=2147483648+(A-32<<23);z[63]=3347054592;const X=new U(64);for(let A=1;A<64;++A)32!==A&&(X[A]=1024);function $(A,I,...g){return function(A){const I=A>>10;return Z[0]=W[X[I]+(1023&A)]+z[I],v[0]}(m(A,I,...function(A){if(A[r]===S&&Y.next===K)return A;const I=c(T);return q(x,I,G(A)),I}(g)))}function AA(A,I,g){const B=g&&g.debug||!1;B&&console.log("[xml-utils] getting "+I+" in "+A);const C="object"==typeof A?A.outer:A,Q=C.slice(0,C.indexOf(">")+1),E=['"',"'"];for(let A=0;A<E.length;A++){const g=E[A],C=I+"\\="+g+"([^"+g+"]*)"+g;B&&console.log("[xml-utils] pattern:",C);const i=new RegExp(C).exec(Q);if(B&&console.log("[xml-utils] match:",i),i)return i[1]}}function IA(A,I,g){const B=new RegExp(I).exec(A.slice(g));return B?g+B.index+B[0].length-1:-1}function gA(A,I){const g=new RegExp(I,"g"),B=A.match(g);return B?B.length:0}function BA(A,I,g){const B=g&&g.debug||!1,C=!(g&&!1===typeof g.nested),Q=g&&g.startIndex||0;B&&console.log("[xml-utils] starting findTagByName with",I," and ",g);const E=function(A,I,g){const B=new RegExp(I).exec(A.slice(g));return B?g+B.index:-1}(A,`<${I}[ \n>/]`,Q);if(B&&console.log("[xml-utils] start:",E),-1===E)return;const i=A.slice(E+I.length);let e=IA(i,"^[^<]*[ /]>",0);const t=-1!==e&&"/"===i[e-1];if(B&&console.log("[xml-utils] selfClosing:",t),!1===t)if(C){let A=0,g=1,B=0;for(;-1!==(e=IA(i,"[ /]"+I+">",A));){const C=i.substring(A,e+1);if(g+=gA(C,"<"+I+"[ \n\t>]"),B+=gA(C,"</"+I+">"),B>=g)break;A=e}}else e=IA(i,"[ /]"+I+">",0);const o=E+I.length+e+1;if(B&&console.log("[xml-utils] end:",o),-1===o)return;const a=A.slice(E,o);let s;return s=t?null:a.slice(a.indexOf(">")+1,a.lastIndexOf("<")),{inner:s,outer:a,start:E,end:o}}const CA={BYTE:1,ASCII:2,SHORT:3,LONG:4,RATIONAL:5,SBYTE:6,UNDEFINED:7,SSHORT:8,SLONG:9,SRATIONAL:10,FLOAT:11,DOUBLE:12,IFD:13,LONG8:16,SLONG8:17,IFD8:18},QA={[CA.BYTE]:1,[CA.ASCII]:1,[CA.SBYTE]:1,[CA.UNDEFINED]:1,[CA.SHORT]:2,[CA.SSHORT]:2,[CA.LONG]:4,[CA.SLONG]:4,[CA.FLOAT]:4,[CA.IFD]:4,[CA.RATIONAL]:8,[CA.SRATIONAL]:8,[CA.DOUBLE]:8,[CA.LONG8]:8,[CA.SLONG8]:8,[CA.IFD8]:8};function EA(A){const I=QA[A];if(void 0===I)throw new RangeError(`Invalid field type: ${A}`);return I}const iA={NewSubfileType:{tag:254,type:CA.LONG,eager:!0},SubfileType:{tag:255,type:CA.SHORT,eager:!0},ImageWidth:{tag:256,type:CA.SHORT,eager:!0},ImageLength:{tag:257,type:CA.SHORT,eager:!0},BitsPerSample:{tag:258,type:CA.SHORT,isArray:!0,eager:!0},Compression:{tag:259,type:CA.SHORT,eager:!0},PhotometricInterpretation:{tag:262,type:CA.SHORT,eager:!0},Threshholding:{tag:263,type:CA.SHORT},CellWidth:{tag:264,type:CA.SHORT},CellLength:{tag:265,type:CA.SHORT},FillOrder:{tag:266,type:CA.SHORT},DocumentName:{tag:269,type:CA.ASCII},ImageDescription:{tag:270,type:CA.ASCII},Make:{tag:271,type:CA.ASCII},Model:{tag:272,type:CA.ASCII},StripOffsets:{tag:273,type:CA.SHORT,isArray:!0},Orientation:{tag:274,type:CA.SHORT},SamplesPerPixel:{tag:277,type:CA.SHORT,eager:!0},RowsPerStrip:{tag:278,type:CA.SHORT,eager:!0},StripByteCounts:{tag:279,type:CA.LONG,isArray:!0},MinSampleValue:{tag:280,type:CA.SHORT,isArray:!0},MaxSampleValue:{tag:281,type:CA.SHORT,isArray:!0},XResolution:{tag:282,type:CA.RATIONAL},YResolution:{tag:283,type:CA.RATIONAL},PlanarConfiguration:{tag:284,type:CA.SHORT,eager:!0},PageName:{tag:285,type:CA.ASCII},XPosition:{tag:286,type:CA.RATIONAL},YPosition:{tag:287,type:CA.RATIONAL},FreeOffsets:{tag:288,type:CA.LONG},FreeByteCounts:{tag:289,type:CA.LONG},GrayResponseUnit:{tag:290,type:CA.SHORT},GrayResponseCurve:{tag:291,type:CA.SHORT,isArray:!0},T4Options:{tag:292,type:CA.LONG},T6Options:{tag:293,type:CA.LONG},ResolutionUnit:{tag:296,type:CA.SHORT},PageNumber:{tag:297,type:CA.SHORT,isArray:!0},TransferFunction:{tag:301,type:CA.SHORT,isArray:!0},Software:{tag:305,type:CA.ASCII},DateTime:{tag:306,type:CA.ASCII},Artist:{tag:315,type:CA.ASCII},HostComputer:{tag:316,type:CA.ASCII},Predictor:{tag:317,type:CA.SHORT},WhitePoint:{tag:318,type:CA.RATIONAL,isArray:!0},PrimaryChromaticities:{tag:319,type:CA.RATIONAL,isArray:!0},ColorMap:{tag:320,type:CA.SHORT,isArray:!0},HalftoneHints:{tag:321,type:CA.SHORT,isArray:!0},TileWidth:{tag:322,type:CA.SHORT,eager:!0},TileLength:{tag:323,type:CA.SHORT,eager:!0},TileOffsets:{tag:324,type:CA.LONG,isArray:!0},TileByteCounts:{tag:325,type:CA.SHORT,isArray:!0},InkSet:{tag:332,type:CA.SHORT},InkNames:{tag:333,type:CA.ASCII},NumberOfInks:{tag:334,type:CA.SHORT},DotRange:{tag:336,type:CA.BYTE,isArray:!0},TargetPrinter:{tag:337,type:CA.ASCII},ExtraSamples:{tag:338,type:CA.BYTE,isArray:!0,eager:!0},SampleFormat:{tag:339,type:CA.SHORT,isArray:!0,eager:!0},SMinSampleValue:{tag:340,isArray:!0},SMaxSampleValue:{tag:341,isArray:!0},TransferRange:{tag:342,type:CA.SHORT,isArray:!0},JPEGProc:{tag:512,type:CA.SHORT},JPEGInterchangeFormat:{tag:513,type:CA.LONG},JPEGInterchangeFormatLngth:{tag:514,type:CA.LONG},JPEGRestartInterval:{tag:515,type:CA.SHORT},JPEGLosslessPredictors:{tag:517,type:CA.SHORT,isArray:!0},JPEGPointTransforms:{tag:518,type:CA.SHORT,isArray:!0},JPEGQTables:{tag:519,type:CA.LONG,isArray:!0},JPEGDCTables:{tag:520,type:CA.LONG,isArray:!0},JPEGACTables:{tag:521,type:CA.LONG,isArray:!0},YCbCrCoefficients:{tag:529,type:CA.RATIONAL,isArray:!0},YCbCrSubSampling:{tag:530,type:CA.SHORT,isArray:!0},YCbCrPositioning:{tag:531,type:CA.SHORT},ReferenceBlackWhite:{tag:532,type:CA.LONG,isArray:!0},Copyright:{tag:33432,type:CA.ASCII},BadFaxLines:{tag:326},CleanFaxData:{tag:327},ClipPath:{tag:343},ConsecutiveBadFaxLines:{tag:328},Decode:{tag:433},DefaultImageColor:{tag:434},Indexed:{tag:346},JPEGTables:{tag:347,isArray:!0,eager:!0},StripRowCounts:{tag:559,isArray:!0},SubIFDs:{tag:330,isArray:!0},XClipPathUnits:{tag:344},YClipPathUnits:{tag:345},ApertureValue:{tag:37378},ColorSpace:{tag:40961},DateTimeDigitized:{tag:36868},DateTimeOriginal:{tag:36867},ExifIFD:{tag:34665,name:"Exif IFD",type:CA.LONG},ExifVersion:{tag:36864},ExposureTime:{tag:33434},FileSource:{tag:41728},Flash:{tag:37385},FlashpixVersion:{tag:40960},FNumber:{tag:33437},ImageUniqueID:{tag:42016},LightSource:{tag:37384},MakerNote:{tag:37500},ShutterSpeedValue:{tag:37377},UserComment:{tag:37510},IPTC:{tag:33723},CZ_LSMINFO:{tag:34412},ICCProfile:{tag:34675,name:"ICC Profile"},XMP:{tag:700},GDAL_METADATA:{tag:42112},GDAL_NODATA:{tag:42113,type:CA.ASCII,eager:!0},Photoshop:{tag:34377},ModelPixelScale:{tag:33550,type:CA.DOUBLE,isArray:!0,eager:!0},ModelTiepoint:{tag:33922,type:CA.DOUBLE,isArray:!0,eager:!0},ModelTransformation:{tag:34264,type:CA.DOUBLE,isArray:!0,eager:!0},GeoKeyDirectory:{tag:34735,type:CA.SHORT,isArray:!0,eager:!0},GeoDoubleParams:{tag:34736,type:CA.DOUBLE,isArray:!0,eager:!0},GeoAsciiParams:{tag:34737,type:CA.ASCII,eager:!0},LercParameters:{tag:50674,eager:!0}},eA={},tA={};function oA(A,I,g,B=!1,C=!1){eA[I]=A,tA[A]={tag:A,name:I,type:"string"==typeof g?CA[g]:g,isArray:B,eager:C}}for(const[A,I]of Object.entries(iA)){const g=I;oA(g.tag,g.name||A,g.type,g.isArray,g.eager)}function aA(A){return"number"==typeof A?A:eA[A]}const sA={256:"SHORT",257:"SHORT",258:"SHORT",259:"SHORT",262:"SHORT",270:"ASCII",271:"ASCII",272:"ASCII",273:"LONG",274:"SHORT",277:"SHORT",278:"LONG",279:"LONG",282:"RATIONAL",283:"RATIONAL",284:"SHORT",286:"SHORT",287:"RATIONAL",296:"SHORT",297:"SHORT",305:"ASCII",306:"ASCII",315:"ASCII",338:"SHORT",339:"SHORT",513:"LONG",514:"LONG",1024:"SHORT",1025:"SHORT",1026:"ASCII",2048:"SHORT",2049:"ASCII",2052:"SHORT",2054:"SHORT",2057:"DOUBLE",2059:"DOUBLE",2060:"SHORT",3072:"SHORT",3073:"ASCII",3076:"SHORT",4096:"SHORT",4097:"ASCII",4099:"SHORT",33432:"ASCII",33550:"DOUBLE",33922:"DOUBLE",34264:"DOUBLE",34665:"LONG",34735:"SHORT",34736:"DOUBLE",34737:"ASCII",42113:"ASCII"},rA={WhiteIsZero:0,BlackIsZero:1,RGB:2,Palette:3,TransparencyMask:4,CMYK:5,YCbCr:6,CIELab:8,ICCLab:9},nA={Unspecified:0,Assocalpha:1,Unassalpha:2},hA={Version:0,AddCompression:1},DA={None:0,Deflate:1,Zstandard:2},wA={1024:"GTModelTypeGeoKey",1025:"GTRasterTypeGeoKey",1026:"GTCitationGeoKey",2048:"GeographicTypeGeoKey",2049:"GeogCitationGeoKey",2050:"GeogGeodeticDatumGeoKey",2051:"GeogPrimeMeridianGeoKey",2052:"GeogLinearUnitsGeoKey",2053:"GeogLinearUnitSizeGeoKey",2054:"GeogAngularUnitsGeoKey",2055:"GeogAngularUnitSizeGeoKey",2056:"GeogEllipsoidGeoKey",2057:"GeogSemiMajorAxisGeoKey",2058:"GeogSemiMinorAxisGeoKey",2059:"GeogInvFlatteningGeoKey",2060:"GeogAzimuthUnitsGeoKey",2061:"GeogPrimeMeridianLongGeoKey",2062:"GeogTOWGS84GeoKey",3072:"ProjectedCSTypeGeoKey",3073:"PCSCitationGeoKey",3074:"ProjectionGeoKey",3075:"ProjCoordTransGeoKey",3076:"ProjLinearUnitsGeoKey",3077:"ProjLinearUnitSizeGeoKey",3078:"ProjStdParallel1GeoKey",3079:"ProjStdParallel2GeoKey",3080:"ProjNatOriginLongGeoKey",3081:"ProjNatOriginLatGeoKey",3082:"ProjFalseEastingGeoKey",3083:"ProjFalseNorthingGeoKey",3084:"ProjFalseOriginLongGeoKey",3085:"ProjFalseOriginLatGeoKey",3086:"ProjFalseOriginEastingGeoKey",3087:"ProjFalseOriginNorthingGeoKey",3088:"ProjCenterLongGeoKey",3089:"ProjCenterLatGeoKey",3090:"ProjCenterEastingGeoKey",3091:"ProjCenterNorthingGeoKey",3092:"ProjScaleAtNatOriginGeoKey",3093:"ProjScaleAtCenterGeoKey",3094:"ProjAzimuthAngleGeoKey",3095:"ProjStraightVertPoleLongGeoKey",3096:"ProjRectifiedGridAngleGeoKey",4096:"VerticalCSTypeGeoKey",4097:"VerticalCitationGeoKey",4098:"VerticalDatumGeoKey",4099:"VerticalUnitsGeoKey"},cA={};for(const[A,I]of Object.entries(wA))cA[I]=parseInt(A,10);var lA=Object.freeze({__proto__:null,fieldTypes:CA,fieldTypeSizes:QA,getFieldTypeSize:EA,tagDictionary:iA,tags:eA,tagDefinitions:tA,registerTag:oA,resolveTag:aA,getTag:function(A){return tA[aA(A)]},fieldTagTypes:sA,photometricInterpretations:rA,ExtraSamplesValues:nA,LercParameters:hA,LercAddCompression:DA,geoKeyNames:wA,geoKeys:cA});function yA(A,I){const{width:g,height:B}=A,C=new Uint8Array(g*B*3);let Q;for(let g=0,B=0;g<A.length;++g,B+=3)Q=256-A[g]/I*256,C[B]=Q,C[B+1]=Q,C[B+2]=Q;return C}function dA(A,I){const{width:g,height:B}=A,C=new Uint8Array(g*B*3);let Q;for(let g=0,B=0;g<A.length;++g,B+=3)Q=A[g]/I*256,C[B]=Q,C[B+1]=Q,C[B+2]=Q;return C}function SA(A,I){const{width:g,height:B}=A,C=new Uint8Array(g*B*3),Q=I.length/3,E=I.length/3*2;for(let g=0,B=0;g<A.length;++g,B+=3){const i=A[g];C[B]=I[i]/65536*256,C[B+1]=I[i+Q]/65536*256,C[B+2]=I[i+E]/65536*256}return C}function GA(A){const{width:I,height:g}=A,B=new Uint8Array(I*g*3);for(let I=0,g=0;I<A.length;I+=4,g+=3){const C=A[I],Q=A[I+1],E=A[I+2],i=A[I+3];B[g]=(255-C)/256*255*((255-i)/256),B[g+1]=(255-Q)/256*255*((255-i)/256),B[g+2]=(255-E)/256*255*((255-i)/256)}return B}function fA(A){const{width:I,height:g}=A,B=new Uint8ClampedArray(I*g*3);for(let I=0,g=0;I<A.length;I+=3,g+=3){const C=A[I],Q=A[I+1],E=A[I+2];B[g]=C+1.402*(E-128),B[g+1]=C-.34414*(Q-128)-.71414*(E-128),B[g+2]=C+1.772*(Q-128)}return B}function NA(A){const{width:I,height:g}=A,B=new Uint8Array(I*g*3);for(let I=0,g=0;I<A.length;I+=3,g+=3){let C,Q,E,i=(A[I+0]+16)/116,e=(A[I+1]<<24>>24)/500+i,t=i-(A[I+2]<<24>>24)/200;e=.95047*(e*e*e>.008856?e*e*e:(e-16/116)/7.787),i=1*(i*i*i>.008856?i*i*i:(i-16/116)/7.787),t=1.08883*(t*t*t>.008856?t*t*t:(t-16/116)/7.787),C=3.2406*e+-1.5372*i+-.4986*t,Q=-.9689*e+1.8758*i+.0415*t,E=.0557*e+-.204*i+1.057*t,C=C>.0031308?1.055*C**(1/2.4)-.055:12.92*C,Q=Q>.0031308?1.055*Q**(1/2.4)-.055:12.92*Q,E=E>.0031308?1.055*E**(1/2.4)-.055:12.92*E,B[g]=255*Math.max(0,Math.min(1,C)),B[g+1]=255*Math.max(0,Math.min(1,Q)),B[g+2]=255*Math.max(0,Math.min(1,E))}return B}var kA=Object.freeze({__proto__:null,fromWhiteIsZero:yA,fromBlackIsZero:dA,fromPalette:SA,fromCMYK:GA,fromYCbCr:fA,fromCIELab:NA});const FA=new Map;async function RA(A){const I=!A.hasTag("StripOffsets");return{tileWidth:I?await A.loadValue("TileWidth"):await A.loadValue("ImageWidth"),tileHeight:I?await A.loadValue("TileLength"):await A.loadValue("RowsPerStrip")||await A.loadValue("ImageLength"),planarConfiguration:await A.loadValue("PlanarConfiguration"),bitsPerSample:await A.loadValue("BitsPerSample"),predictor:await A.loadValue("Predictor")||1}}function UA(A,I,g=RA,B=!0){Array.isArray(A)||(A=[A]),A.forEach((A=>{FA.set(A,{importFn:I,decoderParameterFn:g,preferWorker:B})}))}async function uA(A,I){if(!FA.has(A))throw new Error(`Unknown compression method identifier: ${A}`);const{importFn:g}=FA.get(A);return new(await g())(I)}const LA=[{cases:[void 0,1],importFn:()=>Promise.resolve().then((function(){return XI})).then((A=>A.default)),preferWorker:!1},{cases:5,importFn:()=>Promise.resolve().then((function(){return Ig})).then((A=>A.default))},{cases:6,importFn:()=>{throw new Error("old style JPEG compression is not supported.")}},{cases:7,importFn:()=>Promise.resolve().then((function(){return hg})).then((A=>A.default)),decoderParameterFn:async A=>({...await RA(A),JPEGTables:await A.loadValue("JPEGTables")})},{cases:[8,32946],importFn:()=>Promise.resolve().then((function(){return hQ})).then((A=>A.default))},{cases:32773,importFn:()=>Promise.resolve().then((function(){return DQ})).then((A=>A.default))},{cases:34887,importFn:()=>Promise.resolve().then((function(){return NQ})).then((async A=>(await A.zstd.init(),A))).then((A=>A.default)),decoderParameterFn:async A=>({...await RA(A),LercParameters:await A.loadValue("LercParameters")})},{cases:5e4,importFn:()=>Promise.resolve().then((function(){return KQ})).then((async A=>(await A.zstd.init(),A))).then((A=>A.default))},{cases:50001,importFn:()=>Promise.resolve().then((function(){return MQ})).then((A=>A.default)),decoderParameterFn:async A=>({...await RA(A),samplesPerPixel:Number(await A.loadValue("SamplesPerPixel"))||4}),preferWorker:!1}];for(const A of LA){const{cases:I,importFn:g,decoderParameterFn:B,preferWorker:C}=A;UA(I,g,B,C)}function YA(A,I,g,B=1){return new(Object.getPrototypeOf(A).constructor)(I*g*B)}function KA(A,I,g){return(1-g)*A+g*I}function MA(A,I,g,B,C,Q="nearest"){switch(Q.toLowerCase()){case"nearest":return function(A,I,g,B,C){const Q=I/B,E=g/C;return A.map((A=>{const i=YA(A,B,C);for(let e=0;e<C;++e){const C=Math.min(Math.round(E*e),g-1);for(let g=0;g<B;++g){const E=Math.min(Math.round(Q*g),I-1),t=A[C*I+E];i[e*B+g]=t}}return i}))}(A,I,g,B,C);case"bilinear":case"linear":return function(A,I,g,B,C){const Q=I/B,E=g/C;return A.map((A=>{const i=YA(A,B,C);for(let e=0;e<C;++e){const C=E*e,t=Math.floor(C),o=Math.min(Math.ceil(C),g-1);for(let g=0;g<B;++g){const E=Q*g,a=E%1,s=Math.floor(E),r=Math.min(Math.ceil(E),I-1),n=A[t*I+s],h=A[t*I+r],D=A[o*I+s],w=A[o*I+r],c=KA(KA(n,h,a),KA(D,w,a),C%1);i[e*B+g]=c}}return i}))}(A,I,g,B,C);default:throw new Error(`Unsupported resampling method: '${Q}'`)}}function pA(A,I,g,B,C,Q,E="nearest"){switch(E.toLowerCase()){case"nearest":return function(A,I,g,B,C,Q){const E=I/B,i=g/C,e=YA(A,B,C,Q);for(let t=0;t<C;++t){const C=Math.min(Math.round(i*t),g-1);for(let g=0;g<B;++g){const i=Math.min(Math.round(E*g),I-1);for(let E=0;E<Q;++E){const o=A[C*I*Q+i*Q+E];e[t*B*Q+g*Q+E]=o}}}return e}(A,I,g,B,C,Q);case"bilinear":case"linear":return function(A,I,g,B,C,Q){const E=I/B,i=g/C,e=YA(A,B,C,Q);for(let t=0;t<C;++t){const C=i*t,o=Math.floor(C),a=Math.min(Math.ceil(C),g-1);for(let g=0;g<B;++g){const i=E*g,s=i%1,r=Math.floor(i),n=Math.min(Math.ceil(i),I-1);for(let E=0;E<Q;++E){const i=A[o*I*Q+r*Q+E],h=A[o*I*Q+n*Q+E],D=A[a*I*Q+r*Q+E],w=A[a*I*Q+n*Q+E],c=KA(KA(i,h,s),KA(D,w,s),C%1);e[t*B*Q+g*Q+E]=c}}}return e}(A,I,g,B,C,Q);default:throw new Error(`Unsupported resampling method: '${E}'`)}}function mA(A,I,g){let B=0;for(let C=I;C<g;++C)B+=A[C];return B}function JA(A,I,g){let B;switch(A){case 1:I<=8?B=Uint8Array:I<=16?B=Uint16Array:I<=32&&(B=Uint32Array);break;case 2:8===I?B=Int8Array:16===I?B=Int16Array:32===I&&(B=Int32Array);break;case 3:switch(I){case 16:case 32:B=Float32Array;break;case 64:B=Float64Array}}if(B){if("number"==typeof g)return new B(g);if(g instanceof ArrayBuffer)return new B(g)}throw Error("Unsupported data format/bitsPerSample")}var HA=class{constructor(A,I,g,B){this.fileDirectory=A,this.littleEndian=I,this.tiles=g?[]:null,this.isTiled=!A.hasTag("StripOffsets");const C=A.getValue("PlanarConfiguration")??1;if(1!==C&&2!==C)throw new Error("Invalid planar configuration.");this.planarConfiguration=C,this.source=B}getFileDirectory(){return this.fileDirectory}getGeoKeys(){return this.fileDirectory.parseGeoKeyDirectory()}getWidth(){return this.fileDirectory.getValue("ImageWidth")||0}getHeight(){return this.fileDirectory.getValue("ImageLength")||0}getSamplesPerPixel(){return this.fileDirectory.getValue("SamplesPerPixel")??1}getTileWidth(){return this.isTiled?this.fileDirectory.getValue("TileWidth")||0:this.getWidth()}getTileHeight(){if(this.isTiled)return this.fileDirectory.getValue("TileLength")||0;const A=this.fileDirectory.hasTag("RowsPerStrip")&&this.fileDirectory.getValue("RowsPerStrip");return A?Math.min(A,this.getHeight()):this.getHeight()}getBlockWidth(){return this.getTileWidth()}getBlockHeight(A){return this.isTiled||(A+1)*this.getTileHeight()<=this.getHeight()?this.getTileHeight():this.getHeight()-A*this.getTileHeight()}getBytesPerPixel(){let A=0;const I=this.fileDirectory.getValue("BitsPerSample")||[];for(let g=0;g<I.length;++g)A+=this.getSampleByteSize(g);return A}getSampleByteSize(A){const I=this.fileDirectory.getValue("BitsPerSample")||[];if(A>=I.length)throw new RangeError(`Sample index ${A} is out of range.`);return Math.ceil(I[A]/8)}getReaderForSample(A){const I=this.fileDirectory.getValue("SampleFormat"),g=I?I[A]:1,B=(this.fileDirectory.getValue("BitsPerSample")||[])[A];switch(g){case 1:if(B<=8)return DataView.prototype.getUint8;if(B<=16)return DataView.prototype.getUint16;if(B<=32)return DataView.prototype.getUint32;break;case 2:if(B<=8)return DataView.prototype.getInt8;if(B<=16)return DataView.prototype.getInt16;if(B<=32)return DataView.prototype.getInt32;break;case 3:switch(B){case 16:return function(A,I){return $(this,A,I)};case 32:return DataView.prototype.getFloat32;case 64:return DataView.prototype.getFloat64}}throw Error("Unsupported data format/bitsPerSample")}getSampleFormat(A=0){const I=this.fileDirectory.getValue("SampleFormat");return I?I[A]:1}getBitsPerSample(A=0){const I=this.fileDirectory.getValue("BitsPerSample");return I?I[A]:0}getArrayForSample(A,I){return JA(this.getSampleFormat(A),this.getBitsPerSample(A),I)}async getTileOrStrip(A,I,g,B,C){const Q=Math.ceil(this.getWidth()/this.getTileWidth()),E=Math.ceil(this.getHeight()/this.getTileHeight());let i;const{tiles:e}=this;if(1===this.planarConfiguration?i=I*Q+A:2===this.planarConfiguration&&(i=g*Q*E+I*Q+A),void 0===i)throw new Error("Could not determine tile or strip index.");let t,o;if(this.isTiled?(t=Number(await this.fileDirectory.loadValueIndexed("TileOffsets",i)),o=Number(await this.fileDirectory.loadValueIndexed("TileByteCounts",i))):(t=Number(await this.fileDirectory.loadValueIndexed("StripOffsets",i)),o=Number(await this.fileDirectory.loadValueIndexed("StripByteCounts",i))),0===o){const B=this.getBlockHeight(I)*this.getTileWidth(),C=2===this.planarConfiguration?this.getSampleByteSize(g):this.getBytesPerPixel(),Q=new ArrayBuffer(B*C);return this.getArrayForSample(g,Q).fill(this.getGDALNoData()||0),{x:A,y:I,sample:g,data:Q}}const a=(await this.source.fetch([{offset:t,length:o}],C))[0];let s;return null!==e&&e[i]?s=e[i]:(s=(async()=>{let A=await B.decode(a);const g=this.getSampleFormat(),C=this.getBitsPerSample();return function(A,I){return(1!==A&&2!==A||!(I<=32)||I%8!=0)&&(3!==A||16!==I&&32!==I&&64!==I)}(g,C)&&(A=function(A,I,g,B,C,Q,E){const i=new DataView(A),e=2===g?1:B,t=JA(I,C,2===g?E*Q:E*Q*B),o=parseInt("1".repeat(C),2);if(1===I){let A;A=1===g?B*C:C;let I=Q*A;0!=(7&I)&&(I=I+7&-8);for(let A=0;A<E;++A){const g=A*I;for(let I=0;I<Q;++I){const B=g+I*e*C;for(let g=0;g<e;++g){const E=B+g*C,a=(A*Q+I)*e+g,s=Math.floor(E/8),r=E%8;if(r+C<=8)t[a]=i.getUint8(s)>>8-C-r&o;else if(r+C<=16)t[a]=i.getUint16(s)>>16-C-r&o;else if(r+C<=24){const A=i.getUint16(s)<<8|i.getUint8(s+2);t[a]=A>>24-C-r&o}else t[a]=i.getUint32(s)>>32-C-r&o}}}}return t.buffer}(A,g,this.planarConfiguration,this.getSamplesPerPixel(),C,this.getTileWidth(),this.getBlockHeight(I))),A})(),null!==e&&(e[i]=s)),{x:A,y:I,sample:g,data:await s}}async _readRaster(A,I,g,B,C,Q,E,i,e){const t=this.getTileWidth(),o=this.getTileHeight(),a=this.getWidth(),s=this.getHeight(),r=Math.max(Math.floor(A[0]/t),0),n=Math.min(Math.ceil(A[2]/t),Math.ceil(a/t)),h=Math.max(Math.floor(A[1]/o),0),D=Math.min(Math.ceil(A[3]/o),Math.ceil(s/o)),w=A[2]-A[0];let c=this.getBytesPerPixel();const l=[],y=[];for(let A=0;A<I.length;++A){if(1===this.planarConfiguration){const g=await this.fileDirectory.loadValue("BitsPerSample");if("object"!=typeof g)throw new Error("Expected BitsPerSample to be an array or typed array.");l.push(mA(g,0,I[A])/8)}else l.push(0);y.push(this.getReaderForSample(I[A]))}const d=[],{littleEndian:S}=this;for(let Q=h;Q<D;++Q)for(let E=r;E<n;++E){let i;1===this.planarConfiguration&&(i=this.getTileOrStrip(E,Q,0,C,e));for(let r=0;r<I.length;++r){const n=r,h=I[r];if(2===this.planarConfiguration&&(c=this.getSampleByteSize(h),i=this.getTileOrStrip(E,Q,h,C,e)),!i)throw new Error("Could not get tile or strip data.");const D=i.then((C=>{const Q=C.data,E=new DataView(Q),i=this.getBlockHeight(C.y),e=C.y*o,r=C.x*t,h=e+i,D=(C.x+1)*t,d=y[n],G=Math.min(i,i-(h-A[3]),s-e),f=Math.min(t,t-(D-A[2]),a-r);for(let C=Math.max(0,A[1]-e);C<G;++C)for(let Q=Math.max(0,A[0]-r);Q<f;++Q){const i=(C*t+Q)*c,o=d.call(E,i+l[n],S);let a;B?(a=(C+e-A[1])*w*I.length+(Q+r-A[0])*I.length+n,g[a]=o):(a=(C+e-A[1])*w+Q+r-A[0],g[n][a]=o)}}));d.push(D)}}if(await Promise.all(d),Q&&A[2]-A[0]!==Q||E&&A[3]-A[1]!==E){let C;C=B?pA(g,A[2]-A[0],A[3]-A[1],Q,E,I.length,i):MA(g,A[2]-A[0],A[3]-A[1],Q,E,i);const e=C;return e.width=Q??A[2]-A[0],e.height=E??A[3]-A[1],e}const G=g;return G.width=Q||A[2]-A[0],G.height=E||A[3]-A[1],G}async readRasters(A={}){const{window:I,samples:g=[],pool:B=null,width:C,height:Q,resampleMethod:E,fillValue:i,signal:e}=A,t="interleave"in A&&A.interleave,o=I||[0,0,this.getWidth(),this.getHeight()];if(o[0]>o[2]||o[1]>o[3])throw new Error("Invalid subsets");const a=(o[2]-o[0])*(o[3]-o[1]),s=this.getSamplesPerPixel();if(g&&g.length){for(let A=0;A<g.length;++A)if(g[A]>=s)return Promise.reject(new RangeError(`Invalid sample index '${g[A]}'.`))}else for(let A=0;A<s;++A)g.push(A);let r;if(t){const{fileDirectory:A}=this,I=A.getValue("SampleFormat"),B=I?Math.max.apply(null,Array.from(I)):1;if(1!==B&&2!==B&&3!==B)throw new Error("Unsupported sample format for interleaved data. Must be 1, 2, or 3.");const C=A.getValue("BitsPerSample");if(r=JA(B,C?Math.max.apply(null,Array.from(C)):8,a*g.length),i){if(Array.isArray(i))throw new Error("When reading interleaved data, fillValue must be a single number.");r.fill(i)}}else{r=[];for(let A=0;A<g.length;++A){const I=this.getArrayForSample(g[A],a);Array.isArray(i)&&A<i.length?I.fill(i[A]):i&&!Array.isArray(i)&&I.fill(i),r.push(I)}}const n=this.fileDirectory.getValue("Compression")||1,h=await async function(A,I){if(!FA.has(A))throw new Error(`Unknown compression method identifier: ${A}`);const{decoderParameterFn:g}=FA.get(A);return g(I)}(n,this.fileDirectory),D=B?B.bindParameters(n,h):await uA(n,h);return await this._readRaster(o,g,r,t,D,C,Q,E,e)}async readRGB(A={}){const{window:I,pool:g=null,width:B,height:C,resampleMethod:Q,enableAlpha:E=!1,signal:i}=A,e=("interleave"in A&&A.interleave)??!1,t=I||[0,0,this.getWidth(),this.getHeight()];if(t[0]>t[2]||t[1]>t[3])throw new Error("Invalid subsets");const o=this.fileDirectory.getValue("PhotometricInterpretation");if(o===rA.RGB){let A=[0,1,2];const t=this.fileDirectory.getValue("ExtraSamples");if(t&&t[0]!==nA.Unspecified&&E){A=[];const I=this.fileDirectory.getValue("BitsPerSample")||[];for(let g=0;g<I.length;g+=1)A.push(g)}return this.readRasters({window:I,interleave:e,samples:A,pool:g,width:B,height:C,resampleMethod:Q,signal:i})}let a;switch(o){case rA.WhiteIsZero:case rA.BlackIsZero:case rA.Palette:a=[0];break;case rA.CMYK:a=[0,1,2,3];break;case rA.YCbCr:case rA.CIELab:a=[0,1,2];break;default:throw new Error("Invalid or unsupported photometric interpretation.")}const s={window:t,interleave:!0,samples:a,pool:g,width:B,height:C,resampleMethod:Q,signal:i},{fileDirectory:r}=this,n=await this.readRasters(s),h=2**this.getBitsPerSample(0);let D;switch(o){case rA.WhiteIsZero:D=yA(n,h);break;case rA.BlackIsZero:D=dA(n,h);break;case rA.Palette:D=SA(n,await r.loadValue("ColorMap"));break;case rA.CMYK:D=GA(n);break;case rA.YCbCr:D=fA(n);break;case rA.CIELab:D=NA(n);break;default:throw new Error("Unsupported photometric interpretation.")}if(!e){const A=new Uint8Array(D.length/3),I=new Uint8Array(D.length/3),g=new Uint8Array(D.length/3);for(let B=0,C=0;B<D.length;B+=3,++C)A[C]=D[B],I[C]=D[B+1],g[C]=D[B+2];D=[A,I,g]}const w=D;return w.width=n.width,w.height=n.height,w}async getTiePoints(){if(!this.fileDirectory.hasTag("ModelTiepoint"))return[];const A=await this.fileDirectory.loadValue("ModelTiepoint");if("object"!=typeof A)throw new Error("Expected ModelTiepoint to be an array or typed array.");const I=[];for(let g=0;g<A.length;g+=6)I.push({i:A[g],j:A[g+1],k:A[g+2],x:A[g+3],y:A[g+4],z:A[g+5]});return I}async getGDALMetadata(A=null){const I={};if(!this.fileDirectory.hasTag("GDAL_METADATA"))return null;let g=function(A,I,g){const B=[],C=g&&g.debug||!1,Q=!g||"boolean"!=typeof g.nested||g.nested;let E,i=g&&g.startIndex||0;for(;E=BA(A,I,{debug:C,startIndex:i});)i=Q?E.start+1+I.length:E.end,B.push(E);return C&&console.log("findTagsByName found",B.length,"tags"),B}(await this.fileDirectory.loadValue("GDAL_METADATA"),"Item");g=null===A?g.filter((A=>void 0===AA(A,"sample"))):g.filter((I=>Number(AA(I,"sample"))===A));for(let A=0;A<g.length;++A){const B=g[A];I[AA(B,"name")]=B.inner}return I}getGDALNoData(){const A=this.fileDirectory.hasTag("GDAL_NODATA")&&this.fileDirectory.getValue("GDAL_NODATA");return A?Number(A.substring(0,A.length-1)):null}getOrigin(){const A=this.fileDirectory.getValue("ModelTiepoint"),I=this.fileDirectory.getValue("ModelTransformation");if(A&&6===A.length)return[A[3],A[4],A[5]];if(I)return[I[3],I[7],I[11]];throw new Error("The image does not have an affine transformation.")}getResolution(A=null){const I=this.fileDirectory.getValue("ModelPixelScale"),g=this.fileDirectory.getValue("ModelTransformation");if(I)return[I[0],-I[1],I[2]];if(g)return 0===g[1]&&0===g[4]?[g[0],-g[5],g[10]]:[Math.sqrt(g[0]*g[0]+g[4]*g[4]),-Math.sqrt(g[1]*g[1]+g[5]*g[5]),g[10]];if(A){const[I,g,B]=A.getResolution();return[I*A.getWidth()/this.getWidth(),g*A.getHeight()/this.getHeight(),B*A.getWidth()/this.getWidth()]}throw new Error("The image does not have an affine transformation.")}pixelIsArea(){return 1===this.getGeoKeys()?.GTRasterTypeGeoKey}getBoundingBox(A=!1){const I=this.getHeight(),g=this.getWidth(),B=this.fileDirectory.getValue("ModelTransformation");if(B&&!A){const[A,C,,Q,E,i,,e]=B,t=[[0,0],[0,I],[g,0],[g,I]].map((([I,g])=>[Q+A*I+C*g,e+E*I+i*g])),o=t.map((A=>A[0])),a=t.map((A=>A[1]));return[Math.min(...o),Math.min(...a),Math.max(...o),Math.max(...a)]}{const A=this.getOrigin(),B=this.getResolution(),C=A[0],Q=A[1],E=C+B[0]*g,i=Q+B[1]*I;return[Math.min(C,E),Math.min(Q,i),Math.max(C,E),Math.max(Q,i)]}}};class bA{constructor(A){this._dataView=new DataView(A)}get buffer(){return this._dataView.buffer}getUint64(A,I){const g=this.getUint32(A,I),B=this.getUint32(A+4,I);let C;if(I){if(C=g+2**32*B,!Number.isSafeInteger(C))throw new Error(`${C} exceeds MAX_SAFE_INTEGER. Precision may be lost. Please report if you get this message to https://github.com/geotiffjs/geotiff.js/issues`);return C}if(C=2**32*g+B,!Number.isSafeInteger(C))throw new Error(`${C} exceeds MAX_SAFE_INTEGER. Precision may be lost. Please report if you get this message to https://github.com/geotiffjs/geotiff.js/issues`);return C}getInt64(A,I){let g=0;const B=(128&this._dataView.getUint8(A+(I?7:0)))>0;let C=!0;for(let Q=0;Q<8;Q++){let E=this._dataView.getUint8(A+(I?Q:7-Q));B&&(C?0!==E&&(E=255&~(E-1),C=!1):E=255&~E),g+=E*256**Q}return B&&(g=-g),g}getUint8(A){return this._dataView.getUint8(A)}getInt8(A){return this._dataView.getInt8(A)}getUint16(A,I){return this._dataView.getUint16(A,I)}getInt16(A,I){return this._dataView.getInt16(A,I)}getUint32(A,I){return this._dataView.getUint32(A,I)}getInt32(A,I){return this._dataView.getInt32(A,I)}getFloat16(A,I){return $(this._dataView,A,I)}getFloat32(A,I){return this._dataView.getFloat32(A,I)}getFloat64(A,I){return this._dataView.getFloat64(A,I)}}class qA{constructor(A,I,g,B){this._dataView=new DataView(A),this._sliceOffset=I,this._littleEndian=g,this._bigTiff=B}get sliceOffset(){return this._sliceOffset}get sliceTop(){return this._sliceOffset+this.buffer.byteLength}get littleEndian(){return this._littleEndian}get bigTiff(){return this._bigTiff}get buffer(){return this._dataView.buffer}covers(A,I){return this.sliceOffset<=A&&this.sliceTop>=A+I}readUint8(A){return this._dataView.getUint8(A-this._sliceOffset)}readInt8(A){return this._dataView.getInt8(A-this._sliceOffset)}readUint16(A){return this._dataView.getUint16(A-this._sliceOffset,this._littleEndian)}readInt16(A){return this._dataView.getInt16(A-this._sliceOffset,this._littleEndian)}readUint32(A){return this._dataView.getUint32(A-this._sliceOffset,this._littleEndian)}readInt32(A){return this._dataView.getInt32(A-this._sliceOffset,this._littleEndian)}readFloat32(A){return this._dataView.getFloat32(A-this._sliceOffset,this._littleEndian)}readFloat64(A){return this._dataView.getFloat64(A-this._sliceOffset,this._littleEndian)}readUint64(A){const I=this.readUint32(A),g=this.readUint32(A+4);let B;if(this._littleEndian){if(B=I+2**32*g,!Number.isSafeInteger(B))throw new Error(`${B} exceeds MAX_SAFE_INTEGER. Precision may be lost. Please report if you get this message to https://github.com/geotiffjs/geotiff.js/issues`);return B}if(B=2**32*I+g,!Number.isSafeInteger(B))throw new Error(`${B} exceeds MAX_SAFE_INTEGER. Precision may be lost. Please report if you get this message to https://github.com/geotiffjs/geotiff.js/issues`);return B}readInt64(A){let I=0;const g=(128&this._dataView.getUint8(A+(this._littleEndian?7:0)))>0;let B=!0;for(let C=0;C<8;C++){let Q=this._dataView.getUint8(A+(this._littleEndian?C:7-C));g&&(B?0!==Q&&(Q=255&~(Q-1),B=!1):Q=255&~Q),I+=Q*256**C}return g&&(I=-I),I}readOffset(A){return this._bigTiff?this.readUint64(A):this.readUint32(A)}}var xA="undefined"!=typeof Worker?Worker:void 0;function TA(){const A='const A=new Map;async function I(A){const I=!A.hasTag("StripOffsets");return{tileWidth:I?await A.loadValue("TileWidth"):await A.loadValue("ImageWidth"),tileHeight:I?await A.loadValue("TileLength"):await A.loadValue("RowsPerStrip")||await A.loadValue("ImageLength"),planarConfiguration:await A.loadValue("PlanarConfiguration"),bitsPerSample:await A.loadValue("BitsPerSample"),predictor:await A.loadValue("Predictor")||1}}function g(g,B,C=I,Q=!0){Array.isArray(g)||(g=[g]),g.forEach((I=>{A.set(I,{importFn:B,decoderParameterFn:C,preferWorker:Q})}))}const B=[{cases:[void 0,1],importFn:()=>Promise.resolve().then((function(){return e})).then((A=>A.default)),preferWorker:!1},{cases:5,importFn:()=>Promise.resolve().then((function(){return a})).then((A=>A.default))},{cases:6,importFn:()=>{throw new Error("old style JPEG compression is not supported.")}},{cases:7,importFn:()=>Promise.resolve().then((function(){return w})).then((A=>A.default)),decoderParameterFn:async A=>({...await I(A),JPEGTables:await A.loadValue("JPEGTables")})},{cases:[8,32946],importFn:()=>Promise.resolve().then((function(){return Qg})).then((A=>A.default))},{cases:32773,importFn:()=>Promise.resolve().then((function(){return ig})).then((A=>A.default))},{cases:34887,importFn:()=>Promise.resolve().then((function(){return Rg})).then((async A=>(await A.zstd.init(),A))).then((A=>A.default)),decoderParameterFn:async A=>({...await I(A),LercParameters:await A.loadValue("LercParameters")})},{cases:5e4,importFn:()=>Promise.resolve().then((function(){return Jg})).then((async A=>(await A.zstd.init(),A))).then((A=>A.default))},{cases:50001,importFn:()=>Promise.resolve().then((function(){return Hg})).then((A=>A.default)),decoderParameterFn:async A=>({...await I(A),samplesPerPixel:Number(await A.loadValue("SamplesPerPixel"))||4}),preferWorker:!1}];for(const A of B){const{cases:I,importFn:B,decoderParameterFn:C,preferWorker:Q}=A;g(I,B,C,Q)}const C=globalThis;function Q(A,I){let g=A.length-I,B=0;do{for(let g=I;g>0;g--)A[B+I]+=A[B],B++;g-=I}while(g>0)}function E(A,I,g){let B=0,C=A.length;const Q=C/g;for(;C>I;){for(let g=I;g>0;--g)A[B+I]+=A[B],++B;C-=I}const E=A.slice();for(let I=0;I<Q;++I)for(let B=0;B<g;++B)A[g*I+B]=E[(g-B-1)*Q+I]}C.addEventListener("message",(async I=>{const{compression:g,decoderParameters:B,buffer:Q,...E}=I.data;try{const I=await async function(I,g){if(!A.has(I))throw new Error(`Unknown compression method identifier: ${I}`);const{importFn:B}=A.get(I);return new(await B())(g)}(g,B),i=await I.decode(Q);C.postMessage({decoded:i,...E},[i])}catch(A){A instanceof Error?C.postMessage({error:A.message,...E}):C.postMessage({error:String(A),...E})}}));class i{constructor(A){this.parameters=A}decodeBlock(A){throw new Error("decodeBlock not implemented")}async decode(A){const I=await this.decodeBlock(A),{tileWidth:g,tileHeight:B,predictor:C,bitsPerSample:i,planarConfiguration:e}=this.parameters;if(1!==C){return function(A,I,g,B,C,i){if(!I||1===I)return A;for(let A=0;A<C.length;++A){if(C[A]%8!=0)throw new Error("When decoding with predictor, only multiple of 8 bits are supported.");if(C[A]!==C[0])throw new Error("When decoding with predictor, all samples must have the same size.")}const e=C[0]/8,t=2===i?1:C.length;for(let i=0;i<B&&!(i*t*g*e>=A.byteLength);++i){let B;if(2===I){switch(C[0]){case 8:B=new Uint8Array(A,i*t*g*e,t*g*e);break;case 16:B=new Uint16Array(A,i*t*g*e,t*g*e/2);break;case 32:B=new Uint32Array(A,i*t*g*e,t*g*e/4);break;default:throw new Error(`Predictor 2 not allowed with ${C[0]} bits per sample.`)}Q(B,t)}else 3===I&&(B=new Uint8Array(A,i*t*g*e,t*g*e),E(B,t,e))}return A}(I,C,g,B,Array.isArray(i)||ArrayBuffer.isView(i)?Array.from(i):[i],e)}return I}}var e=Object.freeze({__proto__:null,default:class extends i{decodeBlock(A){return A}}});function t(A,I){for(let g=I.length-1;g>=0;g--)A.push(I[g]);return A}function o(A){const I=new Uint16Array(4093),g=new Uint8Array(4093);for(let A=0;A<=257;A++)I[A]=4096,g[A]=A;let B=258,C=9,Q=0;function E(){B=258,C=9}function i(A){const I=function(A,I,g){const B=I%8,C=Math.floor(I/8),Q=8-B,E=I+g-8*(C+1);let i=8*(C+2)-(I+g);const e=8*(C+2)-I;if(i=Math.max(0,i),C>=A.length)return console.warn("ran off the end of the buffer before finding EOI_CODE (end on input code)"),257;let t=A[C]&2**(8-B)-1;t<<=g-Q;let o=t;if(C+1<A.length){let I=A[C+1]>>>i;I<<=Math.max(0,g-e),o+=I}if(E>8&&C+2<A.length){const B=8*(C+3)-(I+g);o+=A[C+2]>>>B}return o}(A,Q,C);return Q+=C,I}function e(A,C){return g[B]=C,I[B]=A,B++,B-1}function o(A){const B=[];for(let C=A;4096!==C;C=I[C])B.push(g[C]);return B}const a=[];E();const s=new Uint8Array(A);let r,D=i(s);for(;257!==D;){if(256===D){for(E(),D=i(s);256===D;)D=i(s);if(257===D)break;if(D>256)throw new Error(`corrupted code at scanline ${D}`);t(a,o(D)),r=D}else if(D<B){const A=o(D);t(a,A),void 0!==r&&e(r,A[A.length-1]),r=D}else{if(void 0===r)throw new Error(`Invalid LZW code: ${D} with no previous code`);const A=o(r);if(!A)throw new Error(`Bogus entry. Not in dictionary, ${r} / ${B}, position: ${Q}`);t(a,A),a.push(A[A.length-1]),e(r,A[A.length-1]),r=D}B+1>=2**C&&(12===C?r=void 0:C++),D=i(s)}return new Uint8Array(a)}var a=Object.freeze({__proto__:null,default:class extends i{decodeBlock(A){return o(A).buffer}}});const s=new Int32Array([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]);function r(A,I){let g=0;const B=[];let C=16;for(;C>0&&!A[C-1];)--C;B.push({children:[],index:0});let Q,E=B[0];for(let i=0;i<C;i++){for(let C=0;C<A[i];C++){if(E=B.pop(),!E)throw new Error("buildHuffmanTable: codeLength mismatch");for(E.children[E.index]=I[g];E.index>0;)if(E=B.pop(),!E)throw new Error("buildHuffmanTable: codeLength mismatch");for(E.index++,B.push(E);B.length<=i;)B.push(Q={children:[],index:0}),E.children[E.index]=Q.children,E=Q;g++}i+1<C&&(B.push(Q={children:[],index:0}),E.children[E.index]=Q.children,E=Q)}return B[0].children}function D(A,I,g,B,C,Q,E,i,e){const{mcusPerLine:t,progressive:o}=g;if(B.length>1&&(void 0===t||void 0===g.mcusPerColumn))throw new Error("Missing MCU dimensions");if(1===B.length&&(void 0===B[0].blocksPerLine||void 0===B[0].blocksPerColumn))throw new Error("Missing block dimensions");const a=I;let r=I,D=0,n=0;function h(){if(n>0)return n--,D>>n&1;if(D=A[r++],255===D){const I=A[r++];if(I)throw new Error(`unexpected marker: ${(D<<8|I).toString(16)}`)}return n=7,D>>>7}function w(A){if(!A)throw new Error("Huffman table not found");let I,g=A;for(;null!==(I=h());){const A=g[I];if("number"==typeof A)return A;if("object"!=typeof A)throw new Error("invalid huffman sequence");g=A}return null}function y(A){let I=A,g=0;for(;I>0;){const A=h();if(null===A)return;g=g<<1|A,--I}return g}function G(A){const I=y(A);if(void 0!==I)return I>=1<<A-1?I:I+(-1<<A)+1}let S=0;let c,N=0;function d(A,I,g,B,C){const Q=g%t,E=(g/t|0)*A.v+B,i=Q*A.h+C;if(!A.blocks)throw new Error("Missing blocks");I(A,A.blocks[E][i])}function l(A,I,g){const B=g/A.blocksPerLine|0,C=g%A.blocksPerLine;if(!A.blocks)throw new Error("Missing blocks");I(A,A.blocks[B][C])}const k=B.length;let F,R,L,U,f,Y;Y=o?0===Q?0===i?function(A,I){const g=w(A.huffmanTableDC);if(null===g)throw new Error("Huffman error");const B=G(g);if(void 0===B)throw new Error("Unexpected end of data in DC coefficient decoding");const C=0===g?0:B<<e;void 0===A.pred&&(A.pred=0),A.pred+=C,I[0]=A.pred}:function(A,I){const g=h();if(null===g)throw new Error("Unexpected end of data in DC coefficient decoding");I[0]|=g<<e}:0===i?function(A,I){if(S>0)return void S--;let g=Q;const B=E;for(;g<=B;){const B=w(A.huffmanTableAC);if(null===B)throw new Error("Unexpected end of data in AC coefficient decoding");const C=15&B,Q=B>>4;if(0===C){if(Q<15){const A=y(Q);if(void 0===A)throw new Error("Unexpected end of data in AC coefficient decoding");S=A+(1<<Q)-1;break}g+=16}else{g+=Q;const A=s[g],B=G(C);if(void 0===B)throw new Error("Unexpected end of data in AC coefficient decoding");I[A]=B*(1<<e),g++}}}:function(A,I){let g=Q;const B=E;let C=0;for(;g<=B;){const B=s[g],Q=I[B]<0?-1:1;switch(N){case 0:{const I=w(A.huffmanTableAC);if(null===I)throw new Error("Unexpected end of data in AC coefficient decoding");const g=15&I;if(C=I>>4,0===g)if(C<15){const A=y(C);if(void 0===A)throw new Error("Unexpected end of data in AC coefficient decoding");S=A+(1<<C),N=4}else C=16,N=1;else{if(1!==g)throw new Error("invalid ACn encoding");const A=G(g);if(void 0===A)throw new Error("Unexpected end of data in AC coefficient decoding");c=A,N=C?2:3}continue}case 1:case 2:if(I[B]){const A=h();if(null===A)throw new Error("Unexpected end of data in AC coefficient decoding");I[B]+=(A<<e)*Q}else C--,0===C&&(N=2===N?3:0);break;case 3:if(I[B]){const A=h();if(null===A)throw new Error("Unexpected end of data in AC coefficient decoding");I[B]+=(A<<e)*Q}else I[B]=c<<e,N=0;break;case 4:if(I[B]){const A=h();if(null===A)throw new Error("Unexpected end of data in AC coefficient decoding");I[B]+=(A<<e)*Q}}g++}4===N&&(S--,0===S&&(N=0))}:function(A,I){const g=w(A.huffmanTableDC);if(null===g)throw new Error("Huffman error");const B=0===g?0:G(g);if(void 0===B)throw new Error("Unexpected end of stream");void 0===A.pred&&(A.pred=0),A.pred+=B,I[0]=A.pred;let C=1;for(;C<64;){const g=w(A.huffmanTableAC);if(null===g)throw new Error("Unexpected end of data in AC coefficient decoding");const B=15&g,Q=g>>4;if(0===B){if(Q<15)break;C+=16}else{C+=Q;const A=s[C],g=G(B);if(void 0===g)throw new Error("Unexpected end of stream");I[A]=g,C++}}};let K,u,M=0;u=1===k?B[0].blocksPerLine*B[0].blocksPerColumn:t*g.mcusPerColumn;const J=C||u;for(;M<u;){for(R=0;R<k;R++)B[R].pred=0;if(S=0,1===k)for(F=B[0],f=0;f<J;f++)l(F,Y,M),M++;else for(f=0;f<J;f++){for(R=0;R<k;R++){F=B[R];const{h:A,v:I}=F;for(L=0;L<I;L++)for(U=0;U<A;U++)d(F,Y,M,L,U)}if(M++,M===u)break}if(n=0,K=A[r]<<8|A[r+1],K<65280)throw new Error("marker was not found");if(!(K>=65488&&K<=65495))break;r+=2}return r-a}function n(A){const I=[],{blocksPerLine:g,blocksPerColumn:B}=A;if(!g||!B||!A.blocks)throw new Error("Missing component data");const C=g<<3,Q=new Int32Array(64),E=new Uint8Array(64);function i(I,g,B){const C=A.quantizationTable;if(!C)throw new Error("No quantization table found");let Q,E,i,e,t,o,a,s,r;const D=B;let n;for(n=0;n<64;n++)D[n]=I[n]*C[n];for(n=0;n<8;++n){const A=8*n;0!==D[1+A]||0!==D[2+A]||0!==D[3+A]||0!==D[4+A]||0!==D[5+A]||0!==D[6+A]||0!==D[7+A]?(Q=5793*D[0+A]+128>>8,E=5793*D[4+A]+128>>8,i=D[2+A],e=D[6+A],t=2896*(D[1+A]-D[7+A])+128>>8,s=2896*(D[1+A]+D[7+A])+128>>8,o=D[3+A]<<4,a=D[5+A]<<4,r=Q-E+1>>1,Q=Q+E+1>>1,E=r,r=3784*i+1567*e+128>>8,i=1567*i-3784*e+128>>8,e=r,r=t-a+1>>1,t=t+a+1>>1,a=r,r=s+o+1>>1,o=s-o+1>>1,s=r,r=Q-e+1>>1,Q=Q+e+1>>1,e=r,r=E-i+1>>1,E=E+i+1>>1,i=r,r=2276*t+3406*s+2048>>12,t=3406*t-2276*s+2048>>12,s=r,r=799*o+4017*a+2048>>12,o=4017*o-799*a+2048>>12,a=r,D[0+A]=Q+s,D[7+A]=Q-s,D[1+A]=E+a,D[6+A]=E-a,D[2+A]=i+o,D[5+A]=i-o,D[3+A]=e+t,D[4+A]=e-t):(r=5793*D[0+A]+512>>10,D[0+A]=r,D[1+A]=r,D[2+A]=r,D[3+A]=r,D[4+A]=r,D[5+A]=r,D[6+A]=r,D[7+A]=r)}for(n=0;n<8;++n){const A=n;0!==D[8+A]||0!==D[16+A]||0!==D[24+A]||0!==D[32+A]||0!==D[40+A]||0!==D[48+A]||0!==D[56+A]?(Q=5793*D[0+A]+2048>>12,E=5793*D[32+A]+2048>>12,i=D[16+A],e=D[48+A],t=2896*(D[8+A]-D[56+A])+2048>>12,s=2896*(D[8+A]+D[56+A])+2048>>12,o=D[24+A],a=D[40+A],r=Q-E+1>>1,Q=Q+E+1>>1,E=r,r=3784*i+1567*e+2048>>12,i=1567*i-3784*e+2048>>12,e=r,r=t-a+1>>1,t=t+a+1>>1,a=r,r=s+o+1>>1,o=s-o+1>>1,s=r,r=Q-e+1>>1,Q=Q+e+1>>1,e=r,r=E-i+1>>1,E=E+i+1>>1,i=r,r=2276*t+3406*s+2048>>12,t=3406*t-2276*s+2048>>12,s=r,r=799*o+4017*a+2048>>12,o=4017*o-799*a+2048>>12,a=r,D[0+A]=Q+s,D[56+A]=Q-s,D[8+A]=E+a,D[48+A]=E-a,D[16+A]=i+o,D[40+A]=i-o,D[24+A]=e+t,D[32+A]=e-t):(r=5793*B[n+0]+8192>>14,D[0+A]=r,D[8+A]=r,D[16+A]=r,D[24+A]=r,D[32+A]=r,D[40+A]=r,D[48+A]=r,D[56+A]=r)}for(n=0;n<64;++n){const A=128+(D[n]+8>>4);g[n]=A<0?0:A>255?255:A}}for(let e=0;e<B;e++){const B=e<<3;for(let A=0;A<8;A++)I.push(new Uint8Array(C));for(let C=0;C<g;C++){i(A.blocks[e][C],E,Q);let g=0;const t=C<<3;for(let A=0;A<8;A++){const C=I[B+A];for(let A=0;A<8;A++)C[t+A]=E[g++]}}}return I}class h{constructor(){this.jfif=null,this.adobe=null,this.resetInterval=0,this.quantizationTables=[],this.huffmanTablesAC=[],this.huffmanTablesDC=[],this.frames=[]}resetFrames(){this.frames=[]}parse(A){let I=0;function g(){const g=A[I]<<8|A[I+1];return I+=2,g}function B(){const B=g(),C=A.subarray(I,I+B-2);return I+=C.length,C}function C(A){let I,g,B=0,C=0;for(g in A.components)A.components.hasOwnProperty(g)&&(I=A.components[g],B<I.h&&(B=I.h),C<I.v&&(C=I.v));const Q=Math.ceil(A.samplesPerLine/8/B),E=Math.ceil(A.scanLines/8/C);for(g in A.components)if(A.components.hasOwnProperty(g)){I=A.components[g];const i=Math.ceil(Math.ceil(A.samplesPerLine/8)*I.h/B),e=Math.ceil(Math.ceil(A.scanLines/8)*I.v/C),t=Q*I.h,o=E*I.v,a=[];for(let A=0;A<o;A++){const A=[];for(let I=0;I<t;I++)A.push(new Int32Array(64));a.push(A)}I.blocksPerLine=i,I.blocksPerColumn=e,I.blocks=a}A.maxH=B,A.maxV=C,A.mcusPerLine=Q,A.mcusPerColumn=E}let Q=g();if(65496!==Q)throw new Error("SOI not found");for(Q=g();65497!==Q;){switch(Q){case 65280:break;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:{const A=B();65504===Q&&74===A[0]&&70===A[1]&&73===A[2]&&70===A[3]&&0===A[4]&&(this.jfif={version:{major:A[5],minor:A[6]},densityUnits:A[7],xDensity:A[8]<<8|A[9],yDensity:A[10]<<8|A[11],thumbWidth:A[12],thumbHeight:A[13],thumbData:A.subarray(14,14+3*A[12]*A[13])}),65518===Q&&65===A[0]&&100===A[1]&&111===A[2]&&98===A[3]&&101===A[4]&&0===A[5]&&(this.adobe={version:A[6],flags0:A[7]<<8|A[8],flags1:A[9]<<8|A[10],transformCode:A[11]});break}case 65499:{const B=g()+I-2;for(;I<B;){const B=A[I++],C=new Int32Array(64);if(B>>4==0)for(let g=0;g<64;g++){C[s[g]]=A[I++]}else{if(B>>4!=1)throw new Error("DQT: invalid table spec");for(let A=0;A<64;A++){C[s[A]]=g()}}this.quantizationTables[15&B]=C}break}case 65472:case 65473:case 65474:{g();const B={extended:65473===Q,progressive:65474===Q,precision:A[I++],scanLines:g(),samplesPerLine:g(),components:{},componentsOrder:[],maxH:0,maxV:0,mcusPerLine:0,mcusPerColumn:0},E=A[I++];let i;for(let g=0;g<E;g++){i=A[I];const g=A[I+1]>>4,C=15&A[I+1],Q=A[I+2];B.componentsOrder.push(i),B.components[i]={h:g,v:C,quantizationIdx:Q,blocksPerLine:0,blocksPerColumn:0,blocks:[]},I+=3}C(B),this.frames.push(B);break}case 65476:{const B=g();for(let g=2;g<B;){const B=A[I++],C=new Uint8Array(16);let Q=0;for(let g=0;g<16;g++,I++)C[g]=A[I],Q+=C[g];const E=new Uint8Array(Q);for(let g=0;g<Q;g++,I++)E[g]=A[I];g+=17+Q,B>>4==0?this.huffmanTablesDC[15&B]=r(C,E):this.huffmanTablesAC[15&B]=r(C,E)}break}case 65501:g(),this.resetInterval=g();break;case 65498:{g();const B=A[I++],C=[],Q=this.frames[0];for(let g=0;g<B;g++){const g=Q.components[A[I++]],B=A[I++];g.huffmanTableDC=this.huffmanTablesDC[B>>4],g.huffmanTableAC=this.huffmanTablesAC[15&B],C.push(g)}const E=A[I++],i=A[I++],e=A[I++],t=D(A,I,Q,C,this.resetInterval,E,i,e>>4,15&e);I+=t;break}case 65535:255!==A[I]&&I--;break;default:if(255===A[I-3]&&A[I-2]>=192&&A[I-2]<=254){I-=3;break}throw new Error(`unknown JPEG marker ${Q.toString(16)}`)}Q=g()}}getResult(){const{frames:A}=this;if(0===this.frames.length)throw new Error("no frames were decoded");this.frames.length>1&&console.warn("more than one frame is not supported");for(let A=0;A<this.frames.length;A++){const I=this.frames[A].components;for(const A of Object.keys(I)){const g=I[A].quantizationIdx;"number"==typeof g&&(I[A].quantizationTable=this.quantizationTables[g],delete I[A].quantizationIdx)}}const I=A[0];if(!I.maxH||!I.maxV)throw new Error("Invalid frame dimensions");const{components:g,componentsOrder:B}=I,C=[],Q=I.samplesPerLine,E=I.scanLines;for(let A=0;A<B.length;A++){const Q=g[B[A]];C.push({lines:n(Q),scaleX:Q.h/I.maxH,scaleY:Q.v/I.maxV})}const i=new Uint8Array(Q*E*C.length);let e=0;for(let A=0;A<E;++A)for(let I=0;I<Q;++I)for(let g=0;g<C.length;++g){const B=C[g];i[e]=B.lines[0|A*B.scaleY][0|I*B.scaleX],++e}return i}}var w=Object.freeze({__proto__:null,default:class extends i{constructor(A){super(A),this.reader=new h,A.JPEGTables&&this.reader.parse(A.JPEGTables)}decodeBlock(A){return this.reader.resetFrames(),this.reader.parse(new Uint8Array(A)),this.reader.getResult().buffer}}});\n/*! pako 2.0.4 https://github.com/nodeca/pako @license (MIT AND Zlib) */function y(A){let I=A.length;for(;--I>=0;)A[I]=0}const G=new Uint8Array([0,0,0,0,0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,0]),S=new Uint8Array([0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13]),c=new Uint8Array([0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,3,7]),N=new Uint8Array([16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15]),d=new Array(576);y(d);const l=new Array(60);y(l);const k=new Array(512);y(k);const F=new Array(256);y(F);const R=new Array(29);y(R);const L=new Array(30);function U(A,I,g,B,C){this.static_tree=A,this.extra_bits=I,this.extra_base=g,this.elems=B,this.max_length=C,this.has_stree=A&&A.length}let f,Y,K;function u(A,I){this.dyn_tree=A,this.max_code=0,this.stat_desc=I}y(L);const M=A=>A<256?k[A]:k[256+(A>>>7)],J=(A,I)=>{A.pending_buf[A.pending++]=255&I,A.pending_buf[A.pending++]=I>>>8&255},H=(A,I,g)=>{A.bi_valid>16-g?(A.bi_buf|=I<<A.bi_valid&65535,J(A,A.bi_buf),A.bi_buf=I>>16-A.bi_valid,A.bi_valid+=g-16):(A.bi_buf|=I<<A.bi_valid&65535,A.bi_valid+=g)},p=(A,I,g)=>{H(A,g[2*I],g[2*I+1])},m=(A,I)=>{let g=0;do{g|=1&A,A>>>=1,g<<=1}while(--I>0);return g>>>1},q=(A,I,g)=>{const B=new Array(16);let C,Q,E=0;for(C=1;C<=15;C++)B[C]=E=E+g[C-1]<<1;for(Q=0;Q<=I;Q++){let I=A[2*Q+1];0!==I&&(A[2*Q]=m(B[I]++,I))}},b=A=>{let I;for(I=0;I<286;I++)A.dyn_ltree[2*I]=0;for(I=0;I<30;I++)A.dyn_dtree[2*I]=0;for(I=0;I<19;I++)A