lerc
Version:
Rapid decoding of Lerc compressed raster data for any standard pixel type.
17 lines • 10.9 kB
JavaScript
/*! Lerc 4.1.1
Copyright 2015 - 2026 Esri
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
A local copy of the license and additional notices are located with the
source distribution at:
http://github.com/Esri/lerc/
Contributors: Thomas Maurer, Wenxue Ju
*/
async function Module(e={}){var t=e,r=!!globalThis.window,n=!!globalThis.WorkerGlobalScope,a=globalThis.process?.versions?.node&&"renderer"!=globalThis.process?.type;if(a){const{createRequire:e}=await import(/*webpackIgnore:true*/"module");var o=e(import.meta.url)}var i,s,l=import.meta.url,u="";if(a){var c=o("fs");l.startsWith("file:")&&(u=o("path").dirname(o("url").fileURLToPath(l))+"/"),s=e=>(e=A(e)?new URL(e):e,c.readFileSync(e)),i=async(e,t=!0)=>(e=A(e)?new URL(e):e,c.readFileSync(e,t?void 0:"utf8")),process.argv.length>1&&process.argv[1].replace(/\\/g,"/"),process.argv.slice(2)}else if(r||n){try{u=new URL(".",l).href}catch{}n&&(s=e=>{var t=new XMLHttpRequest;return t.open("GET",e,!1),t.responseType="arraybuffer",t.send(null),new Uint8Array(t.response)}),i=async e=>{if(A(e))return new Promise((t,r)=>{var n=new XMLHttpRequest;n.open("GET",e,!0),n.responseType="arraybuffer",n.onload=()=>{200==n.status||0==n.status&&n.response?t(n.response):r(n.status)},n.onerror=r,n.send(null)});var t=await fetch(e,{credentials:"same-origin"});if(t.ok)return t.arrayBuffer();throw new Error(t.status+" : "+t.url)}}console.log.bind(console);var f,p,h,y,d,m,g,w=console.error.bind(console),b=!1,A=e=>e.startsWith("file://"),v=!1;function _(){var e=V.buffer;y=new Int8Array(e),d=new Uint8Array(e),m=new Uint32Array(e),new BigInt64Array(e),new BigUint64Array(e)}function U(e){t.onAbort?.(e),w(e="Aborted("+e+")"),b=!0,e+=". Build with -sASSERTIONS for more info.";var r=new WebAssembly.RuntimeError(e);throw h?.(r),r}function x(){return t.locateFile?(e="lerc-wasm.wasm",t.locateFile?t.locateFile(e,u):u+e):new URL("lerc-wasm.wasm",import.meta.url).href;var e}async function T(e){if(!f)try{var t=await i(e);return new Uint8Array(t)}catch{}return function(e){if(e==g&&f)return new Uint8Array(f);if(s)return s(e);throw"both async and sync fetching of the wasm failed"}(e)}async function B(e,t,r){if(!e&&!A(t)&&!a)try{var n=fetch(t,{credentials:"same-origin"});return await WebAssembly.instantiateStreaming(n,r)}catch(e){w(`wasm streaming compile failed: ${e}`),w("falling back to ArrayBuffer instantiation")}return async function(e,t){try{var r=await T(e);return await WebAssembly.instantiate(r,t)}catch(e){w(`failed to asynchronously prepare wasm: ${e}`),U(e)}}(t,r)}var R=e=>{for(;e.length>0;)e.shift()(t)},I=[],C=e=>I.push(e),F=[],L=e=>F.push(e),S=globalThis.TextDecoder&&new TextDecoder,W=(e,t,r)=>e?((e,t=0,r,n)=>{var a=((e,t,r,n)=>{var a=t+r;if(n)return a;for(;e[t]&&!(t>=a);)++t;return t})(e,t,r,n);if(a-t>16&&e.buffer&&S)return S.decode(e.subarray(t,a));for(var o="";t<a;){var i=e[t++];if(128&i){var s=63&e[t++];if(192!=(224&i)){var l=63&e[t++];if((i=224==(240&i)?(15&i)<<12|s<<6|l:(7&i)<<18|s<<12|l<<6|63&e[t++])<65536)o+=String.fromCharCode(i);else{var u=i-65536;o+=String.fromCharCode(55296|u>>10,56320|1023&u)}}else o+=String.fromCharCode((31&i)<<6|s)}else o+=String.fromCharCode(i)}return o})(d,e,t,r):"";class D{constructor(e){this.excPtr=e,this.ptr=e-24}set_type(e){m[this.ptr+4>>2]=e}get_type(){return m[this.ptr+4>>2]}set_destructor(e){m[this.ptr+8>>2]=e}get_destructor(){return m[this.ptr+8>>2]}set_caught(e){e=e?1:0,y[this.ptr+12]=e}get_caught(){return 0!=y[this.ptr+12]}set_rethrown(e){e=e?1:0,y[this.ptr+13]=e}get_rethrown(){return 0!=y[this.ptr+13]}init(e,t){this.set_adjusted_ptr(0),this.set_type(e),this.set_destructor(t)}set_adjusted_ptr(e){m[this.ptr+16>>2]=e}get_adjusted_ptr(){return m[this.ptr+16>>2]}}var V,M=(e,t)=>Math.ceil(e/t)*t,P=e=>{var t=(e-V.buffer.byteLength+65535)/65536|0;try{return V.grow(t),_(),1}catch(e){}};if(t.noExitRuntime&&t.noExitRuntime,t.print&&t.print,t.printErr&&(w=t.printErr),t.wasmBinary&&(f=t.wasmBinary),t.arguments&&t.arguments,t.thisProgram&&t.thisProgram,t.preInit)for("function"==typeof t.preInit&&(t.preInit=[t.preInit]);t.preInit.length>0;)t.preInit.shift()();var k,z={a:(e,t,r,n)=>U(`Assertion failed: ${W(e)}, at: `+[t?W(t):"unknown filename",r,n?W(n):"unknown function"]),b:(e,t,r)=>{throw new D(e).init(t,r),e},c:()=>U(""),d:e=>{var t=d.length,r=2147483648;if((e>>>=0)>r)return!1;for(var n=1;n<=4;n*=2){var a=t*(1+.2/n);a=Math.min(a,e+100663296);var o=Math.min(r,M(Math.max(e,a),65536));if(P(o))return!0}return!1}};return k=await async function(){function e(e,r){return function(e){t._lerc_getBlobInfo=e.g,t._lerc_getDataRanges=e.h,t._lerc_decode_4D=e.i,t._free=e.j,t._malloc=e.k,t.memory=V=e.e,e.__indirect_function_table}(k=e.exports),_(),k}var r={a:z};return t.instantiateWasm?new Promise((n,a)=>{t.instantiateWasm(r,(t,r)=>{n(e(t))})}):(g??=x(),function(t){return e(t.instance)}(await B(f,g,r)))}(),function(){function e(){t.calledRun=!0,b||(v=!0,k.f(),p?.(t),t.onRuntimeInitialized?.(),function(){if(t.postRun)for("function"==typeof t.postRun&&(t.postRun=[t.postRun]);t.postRun.length;)C(t.postRun.shift());R(I)}())}!function(){if(t.preRun)for("function"==typeof t.preRun&&(t.preRun=[t.preRun]);t.preRun.length;)L(t.preRun.shift());R(F)}(),t.setStatus?(t.setStatus("Running..."),setTimeout(()=>{setTimeout(()=>t.setStatus(""),1),e()},1)):e()}(),v?t:new Promise((e,t)=>{p=e,h=t})}const pixelTypeInfoMap=[{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 loadPromise=null,loaded=!1;function load(e={}){if(loadPromise)return loadPromise;const t=e.locateFile||((e,t)=>`${t}${e}`);return loadPromise=Module({locateFile:t}).then(e=>{initLercLib(e),loaded=!0}),loadPromise}function isLoaded(){return loaded}const lercLib={getBlobInfo:null,decode:null};function normalizeByteLength(e){return 16+(e>>3<<3)}function copyBytesFromWasm(e,t,r){r.set(e.slice(t,t+r.length))}function initLercLib(e){const{_malloc:t,_free:r,memory:n,_lerc_getBlobInfo:a,_lerc_getDataRanges:o,_lerc_decode_4D:i}=e;let s;const l=e=>{const r=e.map(e=>normalizeByteLength(e)),a=r.reduce((e,t)=>e+t),o=t(a);s=new Uint8Array(n.buffer);let i=r[0];r[0]=o;for(let e=1;e<r.length;e++){const t=r[e];r[e]=r[e-1]+i,i=t}return r};lercLib.getBlobInfo=e=>{const t=new Uint8Array(48),i=new Uint8Array(24),[u,c,f]=l([e.length,t.length,i.length]);s.set(e,u),s.set(t,c),s.set(i,f);let p=a(u,e.length,c,f,12,3);if(p)throw r(u),new Error(`lerc-getBlobInfo: error code is ${p}`);s=new Uint8Array(n.buffer),copyBytesFromWasm(s,c,t),copyBytesFromWasm(s,f,i);const h=new Uint32Array(t.buffer),y=new Float64Array(i.buffer),[d,m,g,w,b,A,v,_,U,x,T]=h,B={version:d,dimCount:g,width:w,height:b,validPixelCount:v,bandCount:A,blobSize:_,maskCount:U,depthCount:x,dataType:m,minValue:y[0],maxValue:y[1],maxZerror:y[2],statistics:[],bandCountWithNoData:T};if(T&&x>1)return r(u),B;if(1===x&&1===A)return r(u),B.statistics.push({minValue:y[0],maxValue:y[1]}),B;const R=x*A*8,I=new Uint8Array(R),C=new Uint8Array(R);let F=u,L=0,S=0,W=!1;if(s.byteLength<u+2*R?(r(u),W=!0,[F,L,S]=l([e.length,R,R]),s.set(e,F)):[L,S]=l([R,R]),s.set(I,L),s.set(C,S),p=o(F,e.length,x,A,L,S),p)throw r(F),W||r(L),new Error(`lerc-getDataRanges: error code is ${p}`);s=new Uint8Array(n.buffer),copyBytesFromWasm(s,L,I),copyBytesFromWasm(s,S,C);const D=new Float64Array(I.buffer),V=new Float64Array(C.buffer),M=B.statistics;for(let e=0;e<A;e++)if(x>1){const t=D.slice(e*x,(e+1)*x),r=V.slice(e*x,(e+1)*x),n=Math.min.apply(null,t),a=Math.max.apply(null,r);M.push({minValue:n,maxValue:a,dimStats:{minValues:t,maxValues:r},depthStats:{minValues:t,maxValues:r}})}else M.push({minValue:D[e],maxValue:V[e]});return r(F),W||r(L),B},lercLib.decode=(e,t)=>{const{maskCount:a,depthCount:o,bandCount:u,width:c,height:f,dataType:p,bandCountWithNoData:h}=t,y=pixelTypeInfoMap[p],d=c*f,m=new Uint8Array(d*u),g=d*o*u*y.size,w=new Uint8Array(g),b=new Uint8Array(u),A=new Uint8Array(8*u),[v,_,U,x,T]=l([e.length,m.length,w.length,b.length,A.length]);s.set(e,v),s.set(m,_),s.set(w,U),s.set(b,x),s.set(A,T);const B=i(v,e.length,a,_,o,c,f,u,p,U,x,T);if(B)throw r(v),new Error(`lerc-decode: error code is ${B}`);s=new Uint8Array(n.buffer),copyBytesFromWasm(s,U,w),copyBytesFromWasm(s,_,m);let R=null;if(h){copyBytesFromWasm(s,x,b),copyBytesFromWasm(s,T,A),R=[];const e=new Float64Array(A.buffer);for(let t=0;t<b.length;t++)R.push(b[t]?e[t]:null)}return r(v),{data:w,maskData:m,noDataValues:R}}}function swapDepthValuesOrder(e,t,r,n,a){if(r<2)return e;const o=new n(t*r);if(a)for(let n=0,a=0;n<t;n++)for(let i=0,s=n;i<r;i++,s+=t)o[s]=e[a++];else for(let n=0,a=0;n<t;n++)for(let i=0,s=n;i<r;i++,s+=t)o[a++]=e[s];return o}function decode(e,t={}){var r,n;const a=null!==(r=t.inputOffset)&&void 0!==r?r:0,o=e instanceof Uint8Array?e.subarray(a):new Uint8Array(e,a),i=lercLib.getBlobInfo(o),{data:s,maskData:l,noDataValues:u}=lercLib.decode(o,i),{width:c,height:f,bandCount:p,dimCount:h,depthCount:y,dataType:d,maskCount:m,statistics:g}=i,w=pixelTypeInfoMap[d],b=new w.ctor(s.buffer),A=[],v=[],_=c*f,U=_*y,x=null!==(n=t.returnInterleaved)&&void 0!==n?n:t.returnPixelInterleavedDims;for(let e=0;e<p;e++){const t=b.subarray(e*U,(e+1)*U);if(x)A.push(t);else{const e=swapDepthValuesOrder(t,_,y,w.ctor,!0);A.push(e)}v.push(l.subarray(e*U,(e+1)*U))}const T=0===m?null:1===m?v[0]:new Uint8Array(_);if(m>1&&T){T.set(v[0]);for(let e=1;e<v.length;e++){const t=v[e];for(let e=0;e<_;e++)T[e]=T[e]&t[e]}}const{noDataValue:B}=t,R=null!=B&&w.range[0]<=B&&w.range[1]>=B;if(m>0&&R)for(let e=0;e<p;e++){const t=A[e],r=v[e]||T;for(let e=0;e<_;e++)0===r[e]&&(t[e]=B)}const I=m===p&&p>1?v:null,{pixelType:C}=w;return{width:c,height:f,pixelType:C,statistics:g,pixels:A,mask:T,dimCount:h,depthCount:y,bandMasks:I,noDataValues:u}}function getBlobInfo(e,t={}){var r;const n=null!==(r=t.inputOffset)&&void 0!==r?r:0,a=e instanceof Uint8Array?e.subarray(n):new Uint8Array(e,n);return lercLib.getBlobInfo(a)}function getBandCount(e,t={}){return getBlobInfo(e,t).bandCount}export{decode,getBandCount,getBlobInfo,isLoaded,load};