/* COPYRIGHT Esri - https://js.arcgis.com/5.0.18/LICENSE.txt */
import{getAssetUrl as t}from"../../../assets.js";const e=new Set(["uint8","int8","uint16","int16","uint32","int32","int64","uint64"]),n=new Map([["uint8",{byteCount:1,pixelType:"u8",ctor:Uint8Array}],["int8",{byteCount:1,pixelType:"s8",ctor:Int8Array}],["uint16",{byteCount:2,pixelType:"u16",ctor:Uint16Array}],["int16",{byteCount:2,pixelType:"s16",ctor:Int16Array}],["uint32",{byteCount:4,pixelType:"u32",ctor:Uint32Array}],["int32",{byteCount:4,pixelType:"s32",ctor:Int32Array}],["uint64",{byteCount:8,pixelType:"f64",ctor:BigUint64Array}],["int64",{byteCount:8,pixelType:"f64",ctor:BigInt64Array}]]);let r;function o(){return r??=import("../../../chunks/qb3-wasm.js").then(({default:e})=>e({locateFile:e=>t(`esri/layers/raster/formats/${e}`)})).then(t=>{l(t)}),r}const i={getBlobInfo:null,decode:null};function a(t){if(!n.has(t))throw new Error("Unsupported data type: "+t);return n.get(t)}function l(t){const{_GetInfo:n,_decode:r,_malloc:o,_free:l,writeArrayToMemory:u,UTF8ToString:y}=t;i.getBlobInfo=t=>{t.length>1e3&&(t=t.slice(0,1e3));const e=o(t.length);u(t,e);const r=n(e,t.length);if(l(e),!r)return null;const i=y(r);l(r);try{const t=JSON.parse(i);return{width:t.xsize,height:t.ysize,bandCount:t.nbands,dataType:t.dtype,mode:t.mode,bandMap:t.bandmap}}catch{return null}},i.decode=(n,o)=>{const{dataType:i,width:l,height:u,bandCount:y}=o;if(!e.has(i))throw new Error("Unsupported data type: "+i);const{_malloc:c,_free:s}=t,p=c(n.length);t.writeArrayToMemory(n,p);const{ctor:d,byteCount:f}=a(i),h=l*u*y*f,g=c(h),b=c(1024);if(0===r(p,n.length,g,b))throw s(p),s(g),s(b),new Error("Decoding failed: "+t.UTF8ToString(b));const w=new d(t.HEAPU8.slice(g,g+h).buffer);if(s(p),s(g),s(b),w instanceof BigInt64Array||w instanceof BigUint64Array){const t=new Float64Array(w.length);for(let e=0;e<w.length;e++)t[e]=Number(w[e]);return t}return w}}function u(t){const e=new Uint8Array(t),n=i.getBlobInfo(e);if(!n)return null;const r=i.decode(e,n);if(!r)return null;const{width:o,height:l,bandCount:u,dataType:y}=n,{ctor:c,pixelType:s}=a(y),p=c===BigInt64Array||c===BigUint64Array?Float64Array:c,d=Array.from({length:u},()=>new p(o*l));for(let i=0,a=0;i<o*l;i++)for(let t=0;t<u;t++)d[t][i]=r[a++];return{width:o,height:l,pixelType:s,pixels:d}}export{u as decode,o as load};
