/* COPYRIGHT Esri - https://js.arcgis.com/5.1.2/LICENSE.txt */
import{getAssetUrl as e}from"../../../assets.js";const t=[{pixelType:"S8",size:1,ctor:Int8Array,range:[-128,127]},{pixelType:"U8",size:1,ctor:Uint8Array,range:[0,255]},{pixelType:"S16",size:2,ctor:Int16Array,range:[-32768,32767]},{pixelType:"U16",size:2,ctor:Uint16Array,range:[0,65536]},{pixelType:"S32",size:4,ctor:Int32Array,range:[-2147483648,2147483647]},{pixelType:"U32",size:4,ctor:Uint32Array,range:[0,4294967296]},{pixelType:"F32",size:4,ctor:Float32Array,range:[-34028234663852886e22,34028234663852886e22]},{pixelType:"F64",size:8,ctor:Float64Array,range:[-17976931348623157e292,17976931348623157e292]}];let n=null;function r(){return n||(n=import("../../../chunks/lerc-wasm.js").then(({default:t})=>t({locateFile:t=>e(`esri/layers/raster/formats/${t}`)})).then(e=>{s(e)}),n)}const a={getBlobInfo:null,decode:null};function l(e){return 16+(e>>3<<3)}function o(e,t,n){n.set(e.slice(t,t+n.length))}function s(e){const{_malloc:n,_free:r,memory:s,_lerc_getBlobInfo:i,_lerc_getDataRanges:u,_lerc_decode_4D:c}=e;let f;const y=e=>{const t=e.map(e=>l(e)),r=t.reduce((e,t)=>e+t),a=n(r);f=new Uint8Array(s.buffer);let o=t[0];t[0]=a;for(let n=1;n<t.length;n++){const e=t[n];t[n]=t[n-1]+o,o=e}return t};a.getBlobInfo=e=>{const t=12,n=3,a=new Uint8Array(4*t),l=new Uint8Array(8*n),[c,p,h]=y([e.length,a.length,l.length]);f.set(e,c),f.set(a,p),f.set(l,h);let d=i(c,e.length,p,h,t,n);if(d)throw r(c),new Error(`lerc-getBlobInfo: error code is ${d}`);f=new Uint8Array(s.buffer),o(f,p,a),o(f,h,l);const g=new Uint32Array(a.buffer),w=new Float64Array(l.buffer),[b,A,,m,x,U,C,T,V,D,z]=g,I=0===C,k={version:b,depthCount:D,width:m,height:x,validPixelCount:C,bandCount:U,blobSize:T,maskCount:V,dataType:A,minValue:w[0],maxValue:w[1],maxZerror:w[2],statistics:[],bandCountWithNoData:z};if(z&&D>1)return r(c),k;if(1===D&&1===U){r(c);const e=I?0:w[0],t=I?0:w[1];return k.statistics.push({minValue:e,maxValue:t}),k}const F=D*U*8,_=new Uint8Array(F),v=new Uint8Array(F);let B=c,P=0,S=0,$=!1;if(f.byteLength<c+2*F?(r(c),$=!0,[B,P,S]=y([e.length,F,F]),f.set(e,B)):[P,S]=y([F,F]),f.set(_,P),f.set(v,S),d=u(B,e.length,D,U,P,S),d)throw r(B),$||r(P),new Error(`lerc-getDataRanges: error code is ${d}`);f=new Uint8Array(s.buffer),o(f,P,_),o(f,S,v);const E=new Float64Array(_.buffer),M=new Float64Array(v.buffer),O=k.statistics;for(let r=0;r<U;r++)if(D>1){const e=E.slice(r*D,(r+1)*D),t=M.slice(r*D,(r+1)*D),n=I?0:Math.min.apply(null,e),a=I?0:Math.max.apply(null,t);O.push({minValue:n,maxValue:a,depthStats:{minValues:e,maxValues:t}})}else O.push({minValue:I?0:E[r],maxValue:I?0:M[r]});return r(B),$||r(P),k},a.decode=(e,n)=>{const{maskCount:a,depthCount:l,bandCount:i,width:u,height:p,dataType:h,bandCountWithNoData:d,validPixelCount:g}=n,w=t[h],b=u*p,A=new Uint8Array(b*i),m=b*l*i*w.size,x=new Uint8Array(m),U=new Uint8Array(i),C=new Uint8Array(8*i);if(0===g)return{data:x,maskData:A,noDataValues:null};const[T,V,D,z,I]=y([e.length,A.length,x.length,U.length,C.length]);f.set(e,T),f.set(A,V),f.set(x,D),f.set(U,z),f.set(C,I);const k=c(T,e.length,a,V,l,u,p,i,h,D,z,I);if(k)throw r(T),new Error(`lerc-decode: error code is ${k}`);f=new Uint8Array(s.buffer),o(f,D,x),o(f,V,A);let F=null;if(d){o(f,z,U),o(f,I,C),F=[];const e=new Float64Array(C.buffer);for(let t=0;t<U.length;t++)F.push(U[t]?e[t]:null)}return r(T),{data:x,maskData:A,noDataValues:F}}}function i(e,t,n,r,a){if(n<2)return e;const l=new r(t*n);for(let o=0,s=0;o<t;o++)for(let r=0,a=o;r<n;r++,a+=t)l[a]=e[s++];return l}function u(e,n={}){const r=n.inputOffset??0,l=e instanceof Uint8Array?e.subarray(r):new Uint8Array(e,r),o=a.getBlobInfo(l),{data:s,maskData:u,noDataValues:c}=a.decode(l,o),{width:f,height:y,bandCount:p,depthCount:h,dataType:d,maskCount:g,statistics:w}=o,b=t[d],A=new b.ctor(s.buffer),m=[],x=[],U=f*y,C=U*h,T=t.find(e=>e.pixelType===n.outputPixelType)??b,V=T.pixelType!==b.pixelType;for(let t=0;t<p;t++){const e=t*C,r=e+C,a=A.subarray(e,r),l=V?new T.ctor(a):a;if(n.returnInterleaved)m.push(l);else{const e=i(l,U,h,T.ctor);m.push(e)}x.push(u.subarray(e,r))}const D=0===g?null:1===g?x[0]:new Uint8Array(U);if(g>1){D.set(x[0]);for(let e=1;e<x.length;e++){const t=x[e];for(let e=0;e<U;e++)D[e]=D[e]&t[e]}}const{noDataValue:z}=n,I=null!=z&&T.range[0]<=z&&T.range[1]>=z,{validPixelCount:k}=o;if(g>0&&I)for(let t=0;t<p;t++){const e=m[t];if(0===k)e.fill(z);else{const n=x[t]||D;for(let t=0;t<U;t++)0===n[t]&&(e[t]=z)}}const F=g===p&&p>1?x:null;return{width:f,height:y,bandCount:p,pixelType:T.pixelType,depthCount:h,statistics:w,pixels:m,mask:D,validPixelCount:k,bandMasks:F,noDataValues:c}}function c(e,t={}){const n=e instanceof Uint8Array?e.subarray(t.inputOffset??0):new Uint8Array(e,t.inputOffset??0);return a.getBlobInfo(n)}export{u as decode,c as getBlobInfo,r as load,t as pixelTypeInfoMap};
