/*
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 t}from"../../../assets.js";const n=new Set(["uint8","int8","uint16","int16","uint32","int32","int64","uint64"]),r=new Map([["uint8",{byteCount:1,ctor:Uint8Array}],["int8",{byteCount:1,ctor:Int8Array}],["uint16",{byteCount:2,ctor:Uint16Array}],["int16",{byteCount:2,ctor:Int16Array}],["uint32",{byteCount:4,ctor:Uint32Array}],["int32",{byteCount:4,ctor:Int32Array}],["uint64",{byteCount:8,ctor:BigUint64Array}],["int64",{byteCount:8,ctor:BigInt64Array}]]);let e;function o(){return e??=import("../../../chunks/qb3-wasm.js").then(({default:n})=>n({locateFile:n=>t(`esri/layers/raster/formats/${n}`)})).then(t=>{u(t)}),e}const i={getBlobInfo:null,decode:null};function a(t){if(!r.has(t))throw new Error("Unsupported data type: "+t);return r.get(t)}function u(t){const{_GetInfo:r,_decode:e,_malloc:o,_free:u,writeArrayToMemory:c,UTF8ToString:l}=t;i.getBlobInfo=t=>{t.length>1e3&&(t=t.slice(0,1e3));const n=o(t.length);c(t,n);const e=r(n,t.length);if(u(n),!e)return null;const i=l(e);u(e);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=(r,o)=>{const{dataType:i,width:u,height:c,bandCount:l}=o;if(!n.has(i))throw new Error("Unsupported data type: "+i);const{_malloc:s,_free:y}=t,d=s(r.length);t.writeArrayToMemory(r,d);const{ctor:f,byteCount:h}=a(i),g=u*c*l*h,b=s(g),w=s(1024);if(0===e(d,r.length,b,w))throw y(d),y(b),y(w),new Error("Decoding failed: "+t.UTF8ToString(w));const A=new f(t.HEAPU8.slice(b,b+g).buffer);if(y(d),y(b),y(w),A instanceof BigInt64Array||A instanceof BigUint64Array){const t=new Float64Array(A.length);for(let n=0;n<A.length;n++)t[n]=Number(A[n]);return t}return A}}function c(t){const n=new Uint8Array(t),r=i.getBlobInfo(n);if(!r)return null;const e=i.decode(n,r);if(!e)return null;const{width:o,height:u,bandCount:c,dataType:l}=r,{ctor:s}=a(l),y=s===BigInt64Array||s===BigUint64Array?Float64Array:s,d=Array.from({length:c},()=>new y(o*u));for(let i=0,a=0;i<o*u;i++)for(let t=0;t<c;t++)d[t][i]=e[a++];return{width:o,height:u,pixels:d}}export{c as decode,o as load};
