lerc
Version:
Rapid decoding of Lerc compressed raster data for any standard pixel type.
17 lines • 11.5 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
*/
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).Lerc={})}(this,function(e){"use strict";var currentScript=globalThis.document?.currentScript;async function t(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("module");var i=e("undefined"==typeof document&&"undefined"==typeof location?new(require("url").URL)("file:"+__filename).href:"undefined"==typeof document?location.href:currentScript&&"SCRIPT"===currentScript.tagName.toUpperCase()&¤tScript.src||new URL("LercDecode.js",document.baseURI).href)}var o,s,u="undefined"==typeof document&&"undefined"==typeof location?new(require("url").URL)("file:"+__filename).href:"undefined"==typeof document?location.href:currentScript&&"SCRIPT"===currentScript.tagName.toUpperCase()&¤tScript.src||new URL("LercDecode.js",document.baseURI).href,l="";if(a){var c=i("fs");u.startsWith("file:")&&(l=i("path").dirname(i("url").fileURLToPath(u))+"/"),s=e=>(e=U(e)?new URL(e):e,c.readFileSync(e)),o=async(e,t=!0)=>(e=U(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{l=new URL(".",u).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)}),o=async e=>{if(U(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,d,p,h,y,m,g,w=console.error.bind(console),b=!1,U=e=>e.startsWith("file://"),v=!1;function A(){var e=k.buffer;h=new Int8Array(e),y=new Uint8Array(e),m=new Uint32Array(e),new BigInt64Array(e),new BigUint64Array(e)}function _(e){t.onAbort?.(e),w(e="Aborted("+e+")"),b=!0,e+=". Build with -sASSERTIONS for more info.";var r=new WebAssembly.RuntimeError(e);throw p?.(r),r}function R(){return t.locateFile?(e="lerc-wasm.wasm",t.locateFile?t.locateFile(e,l):l+e):new URL("lerc-wasm.wasm","undefined"==typeof document&&"undefined"==typeof location?new(require("url").URL)("file:"+__filename).href:"undefined"==typeof document?location.href:currentScript&&"SCRIPT"===currentScript.tagName.toUpperCase()&¤tScript.src||new URL("LercDecode.js",document.baseURI).href).href;var e}async function T(e){if(!f)try{var t=await o(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 C(e,t,r){if(!e&&!U(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}`),_(e)}}(t,r)}var S=e=>{for(;e.length>0;)e.shift()(t)},x=[],I=e=>x.push(e),L=[],D=e=>L.push(e),B=globalThis.TextDecoder&&new TextDecoder,V=(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&&B)return B.decode(e.subarray(t,a));for(var i="";t<a;){var o=e[t++];if(128&o){var s=63&e[t++];if(192!=(224&o)){var u=63&e[t++];if((o=224==(240&o)?(15&o)<<12|s<<6|u:(7&o)<<18|s<<12|u<<6|63&e[t++])<65536)i+=String.fromCharCode(o);else{var l=o-65536;i+=String.fromCharCode(55296|l>>10,56320|1023&l)}}else i+=String.fromCharCode((31&o)<<6|s)}else i+=String.fromCharCode(o)}return i})(y,e,t,r):"";class F{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,h[this.ptr+12]=e}get_caught(){return 0!=h[this.ptr+12]}set_rethrown(e){e=e?1:0,h[this.ptr+13]=e}get_rethrown(){return 0!=h[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 k,P=(e,t)=>Math.ceil(e/t)*t,E=e=>{var t=(e-k.buffer.byteLength+65535)/65536|0;try{return k.grow(t),A(),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 z,M={a:(e,t,r,n)=>_(`Assertion failed: ${V(e)}, at: `+[t?V(t):"unknown filename",r,n?V(n):"unknown function"]),b:(e,t,r)=>{throw new F(e).init(t,r),e},c:()=>_(""),d:e=>{var t=y.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 i=Math.min(r,P(Math.max(e,a),65536));if(E(i))return!0}return!1}};return z=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=k=e.e,e.__indirect_function_table}(z=e.exports),A(),z}var r={a:M};if(t.instantiateWasm)return new Promise((n,a)=>{t.instantiateWasm(r,(t,r)=>{n(e(t))})});g??=R();var n=function(t){return e(t.instance)}(await C(f,g,r));return n}(),function(){function e(){t.calledRun=!0,b||(v=!0,z.f(),d?.(t),t.onRuntimeInitialized?.(),function(){if(t.postRun)for("function"==typeof t.postRun&&(t.postRun=[t.postRun]);t.postRun.length;)I(t.postRun.shift());S(x)}())}!function(){if(t.preRun)for("function"==typeof t.preRun&&(t.preRun=[t.preRun]);t.preRun.length;)D(t.preRun.shift());S(L)}(),t.setStatus?(t.setStatus("Running..."),setTimeout(()=>{setTimeout(()=>t.setStatus(""),1),e()},1)):e()}(),v?t:new Promise((e,t)=>{d=e,p=t})}const r=[{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,a=!1;const i={getBlobInfo:null,decode:null};function o(e,t,r){r.set(e.slice(t,t+r.length))}function s(e,t,r,n,a){if(r<2)return e;const i=new n(t*r);if(a)for(let n=0,a=0;n<t;n++)for(let o=0,s=n;o<r;o++,s+=t)i[s]=e[a++];else for(let n=0,a=0;n<t;n++)for(let o=0,s=n;o<r;o++,s+=t)i[a++]=e[s];return i}function u(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 i.getBlobInfo(a)}e.decode=function(e,t={}){var n,a;const o=null!==(n=t.inputOffset)&&void 0!==n?n:0,u=e instanceof Uint8Array?e.subarray(o):new Uint8Array(e,o),l=i.getBlobInfo(u),{data:c,maskData:f,noDataValues:d}=i.decode(u,l),{width:p,height:h,bandCount:y,dimCount:m,depthCount:g,dataType:w,maskCount:b,statistics:U}=l,v=r[w],A=new v.ctor(c.buffer),_=[],R=[],T=p*h,C=T*g,S=null!==(a=t.returnInterleaved)&&void 0!==a?a:t.returnPixelInterleavedDims;for(let e=0;e<y;e++){const t=A.subarray(e*C,(e+1)*C);if(S)_.push(t);else{const e=s(t,T,g,v.ctor,!0);_.push(e)}R.push(f.subarray(e*C,(e+1)*C))}const x=0===b?null:1===b?R[0]:new Uint8Array(T);if(b>1&&x){x.set(R[0]);for(let e=1;e<R.length;e++){const t=R[e];for(let e=0;e<T;e++)x[e]=x[e]&t[e]}}const{noDataValue:I}=t,L=null!=I&&v.range[0]<=I&&v.range[1]>=I;if(b>0&&L)for(let e=0;e<y;e++){const t=_[e],r=R[e]||x;for(let e=0;e<T;e++)0===r[e]&&(t[e]=I)}const D=b===y&&y>1?R:null,{pixelType:B}=v;return{width:p,height:h,pixelType:B,statistics:U,pixels:_,mask:x,dimCount:m,depthCount:g,bandMasks:D,noDataValues:d}},e.getBandCount=function(e,t={}){return u(e,t).bandCount},e.getBlobInfo=u,e.isLoaded=function(){return a},e.load=function(e={}){if(n)return n;const s=e.locateFile||((e,t)=>`${t}${e}`);return n=t({locateFile:s}).then(e=>{!function(e){const{_malloc:t,_free:n,memory:a,_lerc_getBlobInfo:s,_lerc_getDataRanges:u,_lerc_decode_4D:l}=e;let c;const f=e=>{const r=e.map(e=>16+(e>>3<<3)),n=r.reduce((e,t)=>e+t),i=t(n);c=new Uint8Array(a.buffer);let o=r[0];r[0]=i;for(let e=1;e<r.length;e++){const t=r[e];r[e]=r[e-1]+o,o=t}return r};i.getBlobInfo=e=>{const t=12,r=3,i=new Uint8Array(4*t),l=new Uint8Array(8*r),[d,p,h]=f([e.length,i.length,l.length]);c.set(e,d),c.set(i,p),c.set(l,h);let y=s(d,e.length,p,h,t,r);if(y)throw n(d),new Error(`lerc-getBlobInfo: error code is ${y}`);c=new Uint8Array(a.buffer),o(c,p,i),o(c,h,l);const m=new Uint32Array(i.buffer),g=new Float64Array(l.buffer),[w,b,U,v,A,_,R,T,C,S,x]=m,I={version:w,dimCount:U,width:v,height:A,validPixelCount:R,bandCount:_,blobSize:T,maskCount:C,depthCount:S,dataType:b,minValue:g[0],maxValue:g[1],maxZerror:g[2],statistics:[],bandCountWithNoData:x};if(x&&S>1)return n(d),I;if(1===S&&1===_)return n(d),I.statistics.push({minValue:g[0],maxValue:g[1]}),I;const L=S*_*8,D=new Uint8Array(L),B=new Uint8Array(L);let V=d,F=0,k=0,P=!1;if(c.byteLength<d+2*L?(n(d),P=!0,[V,F,k]=f([e.length,L,L]),c.set(e,V)):[F,k]=f([L,L]),c.set(D,F),c.set(B,k),y=u(V,e.length,S,_,F,k),y)throw n(V),P||n(F),new Error(`lerc-getDataRanges: error code is ${y}`);c=new Uint8Array(a.buffer),o(c,F,D),o(c,k,B);const E=new Float64Array(D.buffer),z=new Float64Array(B.buffer),M=I.statistics;for(let e=0;e<_;e++)if(S>1){const t=E.slice(e*S,(e+1)*S),r=z.slice(e*S,(e+1)*S),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:E[e],maxValue:z[e]});return n(V),P||n(F),I},i.decode=(e,t)=>{const{maskCount:i,depthCount:s,bandCount:u,width:d,height:p,dataType:h,bandCountWithNoData:y}=t,m=r[h],g=d*p,w=new Uint8Array(g*u),b=g*s*u*m.size,U=new Uint8Array(b),v=new Uint8Array(u),A=new Uint8Array(8*u),[_,R,T,C,S]=f([e.length,w.length,U.length,v.length,A.length]);c.set(e,_),c.set(w,R),c.set(U,T),c.set(v,C),c.set(A,S);const x=l(_,e.length,i,R,s,d,p,u,h,T,C,S);if(x)throw n(_),new Error(`lerc-decode: error code is ${x}`);c=new Uint8Array(a.buffer),o(c,T,U),o(c,R,w);let I=null;if(y){o(c,C,v),o(c,S,A),I=[];const e=new Float64Array(A.buffer);for(let t=0;t<v.length;t++)I.push(v[t]?e[t]:null)}return n(_),{data:U,maskData:w,noDataValues:I}}}(e),a=!0}),n},Object.defineProperty(e,"__esModule",{value:!0})});