/*
All material copyright Esri, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/5.0/esri/copyright.txt for details.
*/
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:[-34027999387901484e22,34027999387901484e22]},{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=>{l(e)}),n)}const a={getBlobInfo:null,decode:null};function o(e){return 16+(e>>3<<3)}function s(e,t,n){n.set(e.slice(t,t+n.length))}function l(e){const{_malloc:n,_free:r,memory:l,_lerc_getBlobInfo:i,_lerc_getDataRanges:u,_lerc_decode_4D:c}=e;let f;const y=e=>{const t=e.map(e=>o(e)),r=t.reduce((e,t)=>e+t),a=n(r);f=new Uint8Array(l.buffer);let s=t[0];t[0]=a;for(let n=1;n<t.length;n++){const e=t[n];t[n]=t[n-1]+s,s=e}return t};a.getBlobInfo=e=>{const t=12,n=3,a=new Uint8Array(4*t),o=new Uint8Array(8*n),[c,h,p]=y([e.length,a.length,o.length]);f.set(e,c),f.set(a,h),f.set(o,p);let g=i(c,e.length,h,p,t,n);if(g)throw r(c),new Error(`lerc-getBlobInfo: error code is ${g}`);f=new Uint8Array(l.buffer),s(f,h,a),s(f,p,o);const d=new Uint32Array(a.buffer),w=new Float64Array(o.buffer),[b,A,,m,U,x,C,V,T,D,z]=d,I={version:b,depthCount:D,width:m,height:U,validPixelCount:C,bandCount:x,blobSize:V,maskCount:T,dataType:A,minValue:w[0],maxValue:w[1],maxZerror:w[2],statistics:[],bandCountWithNoData:z};if(z&&D>1)return r(c),I;if(1===D&&1===x)return r(c),I.statistics.push({minValue:w[0],maxValue:w[1]}),I;const F=D*x*8,_=new Uint8Array(F),k=new Uint8Array(F);let B=c,S=0,$=0,v=!1;if(f.byteLength<c+2*F?(r(c),v=!0,[B,S,$]=y([e.length,F,F]),f.set(e,B)):[S,$]=y([F,F]),f.set(_,S),f.set(k,$),g=u(B,e.length,D,x,S,$),g)throw r(B),v||r(S),new Error(`lerc-getDataRanges: error code is ${g}`);f=new Uint8Array(l.buffer),s(f,S,_),s(f,$,k);const E=new Float64Array(_.buffer),M=new Float64Array(k.buffer),O=I.statistics;for(let r=0;r<x;r++)if(D>1){const e=E.slice(r*D,(r+1)*D),t=M.slice(r*D,(r+1)*D),n=Math.min.apply(null,e),a=Math.max.apply(null,t);O.push({minValue:n,maxValue:a,depthStats:{minValues:e,maxValues:t}})}else O.push({minValue:E[r],maxValue:M[r]});return r(B),v||r(S),I},a.decode=(e,n)=>{const{maskCount:a,depthCount:o,bandCount:i,width:u,height:h,dataType:p,bandCountWithNoData:g}=n,d=t[p],w=u*h,b=new Uint8Array(w*i),A=w*o*i*d.size,m=new Uint8Array(A),U=new Uint8Array(i),x=new Uint8Array(8*i),[C,V,T,D,z]=y([e.length,b.length,m.length,U.length,x.length]);f.set(e,C),f.set(b,V),f.set(m,T),f.set(U,D),f.set(x,z);const I=c(C,e.length,a,V,o,u,h,i,p,T,D,z);if(I)throw r(C),new Error(`lerc-decode: error code is ${I}`);f=new Uint8Array(l.buffer),s(f,T,m),s(f,V,b);let F=null;if(g){s(f,D,U),s(f,z,x),F=[];const e=new Float64Array(x.buffer);for(let t=0;t<U.length;t++)F.push(U[t]?e[t]:null)}return r(C),{data:m,maskData:b,noDataValues:F}}}function i(e,t,n,r,a){if(n<2)return e;const o=new r(t*n);for(let s=0,l=0;s<t;s++)for(let r=0,a=s;r<n;r++,a+=t)o[a]=e[l++];return o}function u(e,n={}){const r=n.inputOffset??0,o=e instanceof Uint8Array?e.subarray(r):new Uint8Array(e,r),s=a.getBlobInfo(o),{data:l,maskData:u,noDataValues:c}=a.decode(o,s),{width:f,height:y,bandCount:h,depthCount:p,dataType:g,maskCount:d,statistics:w}=s,b=t[g],A=new b.ctor(l.buffer),m=[],U=[],x=f*y,C=x*p;for(let t=0;t<h;t++){const e=A.subarray(t*C,(t+1)*C);if(n.returnInterleaved)m.push(e);else{const t=i(e,x,p,b.ctor);m.push(t)}U.push(u.subarray(t*C,(t+1)*C))}const V=0===d?null:1===d?U[0]:new Uint8Array(x);if(d>1){V.set(U[0]);for(let e=1;e<U.length;e++){const t=U[e];for(let e=0;e<x;e++)V[e]=V[e]&t[e]}}const{noDataValue:T}=n,D=null!=T&&b.range[0]<=T&&b.range[1]>=T;if(d>0&&D)for(let t=0;t<h;t++){const e=m[t],n=U[t]||V;for(let t=0;t<x;t++)0===n[t]&&(e[t]=T)}const z=d===h&&h>1?U:null,{pixelType:I}=b;return{width:f,height:y,bandCount:h,pixelType:I,depthCount:p,statistics:w,pixels:m,mask:V,bandMasks:z,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};
