!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define("babylonjs-ktx2decoder",[],t):"object"==typeof exports?exports["babylonjs-ktx2decoder"]=t():e.KTX2DECODER=t()}("undefined"!=typeof self?self:"undefined"!=typeof global?global:this,(()=>(()=>{"use strict";var e={d:(t,r)=>{for(var a in r)e.o(r,a)&&!e.o(t,a)&&Object.defineProperty(t,a,{enumerable:!0,get:r[a]})}};e.g=function(){if("object"==typeof globalThis)return globalThis;try{return this||new Function("return this")()}catch(e){if("object"==typeof window)return window}}(),e.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t),e.r=e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})};var t={};e.d(t,{default:()=>L});var r,a,s,n,o,i,d,c,l={};e.r(l),e.d(l,{DataReader:()=>m,KTX2Decoder:()=>F,KTX2FileReader:()=>h,LiteTranscoder:()=>y,LiteTranscoder_UASTC_ASTC:()=>p,LiteTranscoder_UASTC_BC7:()=>T,LiteTranscoder_UASTC_R8_UNORM:()=>R,LiteTranscoder_UASTC_RG8_UNORM:()=>C,LiteTranscoder_UASTC_RGBA_SRGB:()=>B,LiteTranscoder_UASTC_RGBA_UNORM:()=>g,MSCTranscoder:()=>S,SupercompressionScheme:()=>n,Transcoder:()=>f,TranscoderManager:()=>u,WASMMemoryManager:()=>_,ZSTDDecoder:()=>w}),function(e){e[e.ETC1S=0]="ETC1S",e[e.UASTC4x4=1]="UASTC4x4"}(r||(r={})),function(e){e[e.ASTC_4X4_RGBA=0]="ASTC_4X4_RGBA",e[e.BC7_RGBA=1]="BC7_RGBA",e[e.BC3_RGBA=2]="BC3_RGBA",e[e.BC1_RGB=3]="BC1_RGB",e[e.PVRTC1_4_RGBA=4]="PVRTC1_4_RGBA",e[e.PVRTC1_4_RGB=5]="PVRTC1_4_RGB",e[e.ETC2_RGBA=6]="ETC2_RGBA",e[e.ETC1_RGB=7]="ETC1_RGB",e[e.RGBA32=8]="RGBA32",e[e.R8=9]="R8",e[e.RG8=10]="RG8"}(a||(a={})),function(e){e[e.COMPRESSED_RGBA_BPTC_UNORM_EXT=36492]="COMPRESSED_RGBA_BPTC_UNORM_EXT",e[e.COMPRESSED_RGBA_ASTC_4X4_KHR=37808]="COMPRESSED_RGBA_ASTC_4X4_KHR",e[e.COMPRESSED_RGB_S3TC_DXT1_EXT=33776]="COMPRESSED_RGB_S3TC_DXT1_EXT",e[e.COMPRESSED_RGBA_S3TC_DXT5_EXT=33779]="COMPRESSED_RGBA_S3TC_DXT5_EXT",e[e.COMPRESSED_RGBA_PVRTC_4BPPV1_IMG=35842]="COMPRESSED_RGBA_PVRTC_4BPPV1_IMG",e[e.COMPRESSED_RGB_PVRTC_4BPPV1_IMG=35840]="COMPRESSED_RGB_PVRTC_4BPPV1_IMG",e[e.COMPRESSED_RGBA8_ETC2_EAC=37496]="COMPRESSED_RGBA8_ETC2_EAC",e[e.COMPRESSED_RGB8_ETC2=37492]="COMPRESSED_RGB8_ETC2",e[e.COMPRESSED_RGB_ETC1_WEBGL=36196]="COMPRESSED_RGB_ETC1_WEBGL",e[e.RGBA8Format=32856]="RGBA8Format",e[e.R8Format=33321]="R8Format",e[e.RG8Format=33323]="RG8Format"}(s||(s={}));class m{get byteOffset(){return this._dataByteOffset}constructor(e,t,r){e.buffer?this._dataView=new DataView(e.buffer,e.byteOffset+(t??0),r??e.byteLength):this._dataView=new DataView(e,t??0,r??e.byteLength),this._dataByteOffset=0}readUint8(){const e=this._dataView.getUint8(this._dataByteOffset);return this._dataByteOffset+=1,e}readInt8(){const e=this._dataView.getInt8(this._dataByteOffset);return this._dataByteOffset+=1,e}readUint16(){const e=this._dataView.getUint16(this._dataByteOffset,!0);return this._dataByteOffset+=2,e}readInt16(){const e=this._dataView.getInt16(this._dataByteOffset,!0);return this._dataByteOffset+=2,e}readUint32(){const e=this._dataView.getUint32(this._dataByteOffset,!0);return this._dataByteOffset+=4,e}readInt32(){const e=this._dataView.getInt32(this._dataByteOffset,!0);return this._dataByteOffset+=4,e}readUint64(){const e=this._dataView.getUint32(this._dataByteOffset,!0)+2**32*this._dataView.getUint32(this._dataByteOffset+4,!0);return this._dataByteOffset+=8,e}readUint8Array(e){const t=new Uint8Array(this._dataView.buffer,this._dataView.byteOffset+this._dataByteOffset,e);return this._dataByteOffset+=e,t}skipBytes(e){return this._dataByteOffset+=e,this}}!function(e){e[e.None=0]="None",e[e.BasisLZ=1]="BasisLZ",e[e.ZStandard=2]="ZStandard",e[e.ZLib=3]="ZLib"}(n||(n={})),function(e){e[e.ETC1S=163]="ETC1S",e[e.UASTC=166]="UASTC"}(o||(o={})),function(e){e[e.RGB=0]="RGB",e[e.RRR=3]="RRR",e[e.GGG=4]="GGG",e[e.AAA=15]="AAA"}(i||(i={})),function(e){e[e.RGB=0]="RGB",e[e.RGBA=3]="RGBA",e[e.RRR=4]="RRR",e[e.RRRG=5]="RRRG"}(d||(d={})),function(e){e[e.linear=1]="linear",e[e.sRGB=2]="sRGB"}(c||(c={}));class h{constructor(e){this._data=e}get data(){return this._data}get header(){return this._header}get levels(){return this._levels}get dfdBlock(){return this._dfdBlock}get supercompressionGlobalData(){return this._supercompressionGlobalData}isValid(){return h.IsValid(this._data)}parse(){let e=12;const t=new m(this._data,e,68),r=this._header={vkFormat:t.readUint32(),typeSize:t.readUint32(),pixelWidth:t.readUint32(),pixelHeight:t.readUint32(),pixelDepth:t.readUint32(),layerCount:t.readUint32(),faceCount:t.readUint32(),levelCount:t.readUint32(),supercompressionScheme:t.readUint32(),dfdByteOffset:t.readUint32(),dfdByteLength:t.readUint32(),kvdByteOffset:t.readUint32(),kvdByteLength:t.readUint32(),sgdByteOffset:t.readUint64(),sgdByteLength:t.readUint64()};if(r.pixelDepth>0)throw new Error("Failed to parse KTX2 file - Only 2D textures are currently supported.");if(r.layerCount>1)throw new Error("Failed to parse KTX2 file - Array textures are not currently supported.");if(r.faceCount>1)throw new Error("Failed to parse KTX2 file - Cube textures are not currently supported.");e+=t.byteOffset;let a=Math.max(1,r.levelCount);const s=new m(this._data,e,3*a*8),n=this._levels=[];for(;a--;)n.push({byteOffset:s.readUint64(),byteLength:s.readUint64(),uncompressedByteLength:s.readUint64()});e+=s.byteOffset;const o=new m(this._data,r.dfdByteOffset,r.dfdByteLength),i=this._dfdBlock={vendorId:o.skipBytes(4).readUint16(),descriptorType:o.readUint16(),versionNumber:o.readUint16(),descriptorBlockSize:o.readUint16(),colorModel:o.readUint8(),colorPrimaries:o.readUint8(),transferFunction:o.readUint8(),flags:o.readUint8(),texelBlockDimension:{x:o.readUint8()+1,y:o.readUint8()+1,z:o.readUint8()+1,w:o.readUint8()+1},bytesPlane:[o.readUint8(),o.readUint8(),o.readUint8(),o.readUint8(),o.readUint8(),o.readUint8(),o.readUint8(),o.readUint8()],numSamples:0,samples:new Array};i.numSamples=(i.descriptorBlockSize-24)/16;for(let e=0;e<i.numSamples;e++){const e={bitOffset:o.readUint16(),bitLength:o.readUint8()+1,channelType:o.readUint8(),channelFlags:0,samplePosition:[o.readUint8(),o.readUint8(),o.readUint8(),o.readUint8()],sampleLower:o.readUint32(),sampleUpper:o.readUint32()};e.channelFlags=(240&e.channelType)>>4,e.channelType=15&e.channelType,i.samples.push(e)}const d=this._supercompressionGlobalData={};if(r.sgdByteLength>0){const e=new m(this._data,r.sgdByteOffset,r.sgdByteLength);d.endpointCount=e.readUint16(),d.selectorCount=e.readUint16(),d.endpointsByteLength=e.readUint32(),d.selectorsByteLength=e.readUint32(),d.tablesByteLength=e.readUint32(),d.extendedByteLength=e.readUint32(),d.imageDescs=[];const t=this._getImageCount();for(let r=0;r<t;r++)d.imageDescs.push({imageFlags:e.readUint32(),rgbSliceByteOffset:e.readUint32(),rgbSliceByteLength:e.readUint32(),alphaSliceByteOffset:e.readUint32(),alphaSliceByteLength:e.readUint32()});const a=r.sgdByteOffset+e.byteOffset,s=a+d.endpointsByteLength,n=s+d.selectorsByteLength,o=n+d.tablesByteLength;d.endpointsData=new Uint8Array(this._data.buffer,this._data.byteOffset+a,d.endpointsByteLength),d.selectorsData=new Uint8Array(this._data.buffer,this._data.byteOffset+s,d.selectorsByteLength),d.tablesData=new Uint8Array(this._data.buffer,this._data.byteOffset+n,d.tablesByteLength),d.extendedData=new Uint8Array(this._data.buffer,this._data.byteOffset+o,d.extendedByteLength)}}_getImageCount(){let e=Math.max(this._header.pixelDepth,1);for(let t=1;t<this._header.levelCount;t++)e+=Math.max(this._header.pixelDepth>>t,1);return Math.max(this._header.layerCount,1)*this._header.faceCount*e}get textureFormat(){return 166===this._dfdBlock.colorModel?r.UASTC4x4:r.ETC1S}get hasAlpha(){switch(this.textureFormat){case r.ETC1S:return 2===this._dfdBlock.numSamples&&(15===this._dfdBlock.samples[0].channelType||15===this._dfdBlock.samples[1].channelType);case r.UASTC4x4:return 3===this._dfdBlock.samples[0].channelType}return!1}get needZSTDDecoder(){return this._header.supercompressionScheme===n.ZStandard}get isInGammaSpace(){return 2===this._dfdBlock.transferFunction}static IsValid(e){if(e.byteLength>=12){const t=new Uint8Array(e.buffer,e.byteOffset,12);if(171===t[0]&&75===t[1]&&84===t[2]&&88===t[3]&&32===t[4]&&50===t[5]&&48===t[6]&&187===t[7]&&13===t[8]&&10===t[9]&&26===t[10]&&10===t[11])return!0}return!1}}class _{static LoadWASM(e){if(this.LoadBinariesFromCurrentThread)return new Promise(((t,r)=>{fetch(e).then((t=>{if(t.ok)return t.arrayBuffer();throw new Error(`Could not fetch the wasm component from "${e}": ${t.status} - ${t.statusText}`)})).then((e=>t(e))).catch((e=>{r(e)}))}));const t=this._RequestId++;return new Promise((r=>{const a=e=>{"wasmLoaded"===e.data.action&&e.data.id===t&&(self.removeEventListener("message",a),r(e.data.wasmBinary))};self.addEventListener("message",a),postMessage({action:"loadWASM",path:e,id:t})}))}constructor(e=_.InitialMemoryPages){this._numPages=e,this._memory=new WebAssembly.Memory({initial:this._numPages}),this._memoryViewByteLength=this._numPages<<16,this._memoryViewOffset=0,this._memoryView=new Uint8Array(this._memory.buffer,this._memoryViewOffset,this._memoryViewByteLength)}get wasmMemory(){return this._memory}getMemoryView(e,t=0,r){return r=r??e<<16,this._numPages<e?(this._memory.grow(e-this._numPages),this._numPages=e,this._memoryView=new Uint8Array(this._memory.buffer,t,r),this._memoryViewByteLength=r,this._memoryViewOffset=t):(this._memoryView=new Uint8Array(this._memory.buffer,t,r),this._memoryViewByteLength=r,this._memoryViewOffset=t),this._memoryView}}_.LoadBinariesFromCurrentThread=!0,_.InitialMemoryPages=16,_._RequestId=0;class u{static RegisterTranscoder(e){u._Transcoders.push(e)}findTranscoder(e,t,s,n){let o=null;const i=r[e]+"_"+a[t];for(let r=0;r<u._Transcoders.length;++r)if(u._Transcoders[r].CanTranscode(e,t,s)&&(!n||n.indexOf(u._Transcoders[r].Name)<0)){o=this._getExistingTranscoder(i,u._Transcoders[r].Name),o||(o=new u._Transcoders[r],o.initialize(),o.needMemoryManager()&&(this._wasmMemoryManager||(this._wasmMemoryManager=new _),o.setMemoryManager(this._wasmMemoryManager)),u._TranscoderInstances[i]||(u._TranscoderInstances[i]=[]),u._TranscoderInstances[i].push(o));break}return o}_getExistingTranscoder(e,t){const r=u._TranscoderInstances[e];if(r)for(let e=0;e<r.length;++e){const a=r[e];if(t===a.getName())return a}return null}}u._Transcoders=[],u._TranscoderInstances={};class f{static CanTranscode(e,t,r){return!1}static GetWasmUrl(e){return f.WasmBaseUrl&&e.startsWith("https://cdn.babylonjs.com/")&&(e=e.replace("https://cdn.babylonjs.com/",f.WasmBaseUrl)),e}getName(){return f.Name}initialize(){}needMemoryManager(){return!1}setMemoryManager(e){}transcode(e,t,r,a,s,n,o,i,d){return Promise.resolve(null)}}f.Name="Transcoder",f.WasmBaseUrl="";class y extends f{constructor(){super(...arguments),this._wasmBinary=null}_instantiateWebAssembly(e){return WebAssembly.instantiate(e,{env:{memory:this._memoryManager.wasmMemory}}).then((e=>({module:e.instance.exports})))}_loadModule(e=this._wasmBinary){return this._modulePromise=this._modulePromise||(e?Promise.resolve(e):_.LoadWASM(this._modulePath)).then((e=>this._instantiateWebAssembly(e))),this._modulePromise}get memoryManager(){return this._memoryManager}setModulePath(e,t){this._modulePath=f.GetWasmUrl(e),this._wasmBinary=t}initialize(){this._transcodeInPlace=!0}needMemoryManager(){return!0}setMemoryManager(e){this._memoryManager=e}transcode(e,t,r,a,s,n,o,i,d){return this._loadModule().then((e=>{const t=e.module,[r,o,i]=this._prepareTranscoding(a,s,n,d);return 0===t.transcode(i)?this._transcodeInPlace?r.slice():o.slice():null}))}_prepareTranscoding(e,t,r,a,s){const n=(e+3>>2)*(t+3>>2);void 0!==s&&(r=e*(t+3>>2)*4*s);const o=1+(16*n+65535+(this._transcodeInPlace?0:r)>>16),i=this.memoryManager.getMemoryView(o,65536,16*n),d=this._transcodeInPlace?null:new Uint8Array(this._memoryManager.wasmMemory.buffer,65536+16*n,void 0!==s?e*t*s:r);return i.set(a),[i,d,n]}}class p extends y{static CanTranscode(e,t,s){return e===r.UASTC4x4&&t===a.ASTC_4X4_RGBA}getName(){return p.Name}initialize(){super.initialize(),this.setModulePath(p.WasmModuleURL,p.WasmBinary)}}p.WasmModuleURL="https://cdn.babylonjs.com/ktx2Transcoders/1/uastc_astc.wasm",p.WasmBinary=null,p.Name="UniversalTranscoder_UASTC_ASTC";class T extends y{static CanTranscode(e,t,s){return e===r.UASTC4x4&&t===a.BC7_RGBA}getName(){return T.Name}initialize(){super.initialize(),this.setModulePath(T.WasmModuleURL,T.WasmBinary)}}T.WasmModuleURL="https://cdn.babylonjs.com/ktx2Transcoders/1/uastc_bc7.wasm",T.WasmBinary=null,T.Name="UniversalTranscoder_UASTC_BC7";class g extends y{static CanTranscode(e,t,s){return e===r.UASTC4x4&&t===a.RGBA32&&!s}getName(){return g.Name}initialize(){super.initialize(),this._transcodeInPlace=!1,this.setModulePath(g.WasmModuleURL,g.WasmBinary)}transcode(e,t,r,a,s,n,o,i,d){return this._loadModule().then((e=>{const t=e.module,[,r]=this._prepareTranscoding(a,s,n,d,4);return 0===t.decode(a,s)?r.slice():null}))}}g.WasmModuleURL="https://cdn.babylonjs.com/ktx2Transcoders/1/uastc_rgba8_unorm_v2.wasm",g.WasmBinary=null,g.Name="UniversalTranscoder_UASTC_RGBA_UNORM";class B extends y{static CanTranscode(e,t,s){return e===r.UASTC4x4&&t===a.RGBA32&&s}getName(){return B.Name}initialize(){super.initialize(),this._transcodeInPlace=!1,this.setModulePath(B.WasmModuleURL,B.WasmBinary)}transcode(e,t,r,a,s,n,o,i,d){return this._loadModule().then((e=>{const t=e.module,[,r]=this._prepareTranscoding(a,s,n,d,4);return 0===t.decode(a,s)?r.slice():null}))}}B.WasmModuleURL="https://cdn.babylonjs.com/ktx2Transcoders/1/uastc_rgba8_srgb_v2.wasm",B.WasmBinary=null,B.Name="UniversalTranscoder_UASTC_RGBA_SRGB";class R extends y{static CanTranscode(e,t,s){return e===r.UASTC4x4&&t===a.R8}getName(){return R.Name}initialize(){super.initialize(),this._transcodeInPlace=!1,this.setModulePath(R.WasmModuleURL,R.WasmBinary)}transcode(e,t,r,a,s,n,o,i,d){return this._loadModule().then((e=>{const t=e.module,[,r]=this._prepareTranscoding(a,s,n,d,1);return 0===t.decode(a,s)?r.slice():null}))}}R.WasmModuleURL="https://cdn.babylonjs.com/ktx2Transcoders/1/uastc_r8_unorm.wasm",R.WasmBinary=null,R.Name="UniversalTranscoder_UASTC_R8_UNORM";class C extends y{static CanTranscode(e,t,s){return e===r.UASTC4x4&&t===a.RG8}getName(){return C.Name}initialize(){super.initialize(),this._transcodeInPlace=!1,this.setModulePath(C.WasmModuleURL,C.WasmBinary)}transcode(e,t,r,a,s,n,o,i,d){return this._loadModule().then((e=>{const t=e.module,[,r]=this._prepareTranscoding(a,s,n,d,2);return 0===t.decode(a,s)?r.slice():null}))}}C.WasmModuleURL="https://cdn.babylonjs.com/ktx2Transcoders/1/uastc_rg8_unorm.wasm",C.WasmBinary=null,C.Name="UniversalTranscoder_UASTC_RG8_UNORM";class S extends f{getName(){return S.Name}_getMSCBasisTranscoder(){return this._mscBasisTranscoderPromise||(this._mscBasisTranscoderPromise=(S.WasmBinary?Promise.resolve(S.WasmBinary):_.LoadWASM(f.GetWasmUrl(S.WasmModuleURL))).then((e=>{if(S.JSModule&&"undefined"==typeof MSC_TRANSCODER)globalThis.MSC_TRANSCODER=S.JSModule;else if(S.UseFromWorkerThread)importScripts(f.GetWasmUrl(S.JSModuleURL));else if("undefined"==typeof MSC_TRANSCODER)return new Promise(((t,r)=>{const a=document.getElementsByTagName("head")[0],s=document.createElement("script");s.setAttribute("type","text/javascript"),s.setAttribute("src",f.GetWasmUrl(S.JSModuleURL)),s.onload=()=>{"undefined"!=typeof MSC_TRANSCODER?MSC_TRANSCODER({wasmBinary:e}).then((e=>{e.initTranscoders(),this._mscBasisModule=e,t()})):r("MSC_TRANSCODER script loaded but MSC_TRANSCODER is not defined.")},s.onerror=()=>{r("Can not load MSC_TRANSCODER script.")},a.appendChild(s)}));return new Promise((t=>{MSC_TRANSCODER({wasmBinary:e}).then((e=>{e.initTranscoders(),this._mscBasisModule=e,t()}))}))}))),this._mscBasisTranscoderPromise}static CanTranscode(e,t,r){return!0}transcode(e,t,s,n,o,i,d,c,l){const m=!1;return this._getMSCBasisTranscoder().then((()=>{const h=this._mscBasisModule;let _,u,f,y=null;try{_=e===r.UASTC4x4?new h.UastcImageTranscoder:new h.BasisLzEtc1sImageTranscoder;const y=e===r.UASTC4x4?h.TextureFormat.UASTC4x4:h.TextureFormat.ETC1S;u=new h.ImageInfo(y,n,o,s);const p=h.TranscodeTarget[a[t]];if(!h.isFormatSupported(p,y))throw new Error(`MSCTranscoder: Transcoding from "${r[e]}" to "${a[t]}" not supported by current transcoder build.`);if(e===r.ETC1S){const e=d.supercompressionGlobalData;_.decodePalettes(e.endpointCount,e.endpointsData,e.selectorCount,e.selectorsData),_.decodeTables(e.tablesData),u.flags=c.imageFlags,u.rgbByteOffset=0,u.rgbByteLength=c.rgbSliceByteLength,u.alphaByteOffset=c.alphaSliceByteOffset>0?c.rgbSliceByteLength:0,u.alphaByteLength=c.alphaSliceByteLength,f=_.transcodeImage(p,l,u,0,m)}else u.flags=0,u.rgbByteOffset=0,u.rgbByteLength=i,u.alphaByteOffset=0,u.alphaByteLength=0,f=_.transcodeImage(p,l,u,0,d.hasAlpha,m)}finally{_&&_.delete(),u&&u.delete(),f&&f.transcodedImage&&(y=f.transcodedImage.get_typed_memory_view().slice(),f.transcodedImage.delete())}return y}))}}let U,M,A;S.JSModuleURL="https://cdn.babylonjs.com/ktx2Transcoders/1/msc_basis_transcoder.js",S.WasmModuleURL="https://cdn.babylonjs.com/ktx2Transcoders/1/msc_basis_transcoder.wasm",S.WasmBinary=null,S.JSModule=null,S.UseFromWorkerThread=!0,S.Name="MSCTranscoder";const b={env:{emscripten_notify_memory_growth:function(){A=new Uint8Array(M.exports.memory.buffer)}}};class w{init(){return U||(U="undefined"!=typeof fetch?fetch(f.GetWasmUrl(w.WasmModuleURL)).then((e=>{if(e.ok)return e.arrayBuffer();throw new Error(`Could not fetch the wasm component for the Zstandard decompression lib: ${e.status} - ${e.statusText}`)})).then((e=>WebAssembly.instantiate(e,b))).then(this._init):WebAssembly.instantiateStreaming(fetch(w.WasmModuleURL),b).then(this._init),U)}_init(e){M=e.instance,b.env.emscripten_notify_memory_growth()}decode(e,t=0){if(!M)throw new Error("ZSTDDecoder: Await .init() before decoding.");const r=e.byteLength,a=M.exports.malloc(r);A.set(e,a),t=t||Number(M.exports.ZSTD_findDecompressedSize(a,r));const s=M.exports.malloc(t),n=M.exports.ZSTD_decompress(s,t,a,r),o=A.slice(s,s+n);return M.exports.free(a),M.exports.free(s),o}}w.WasmModuleURL="https://cdn.babylonjs.com/zstddec.wasm";const G={ETC1S:{option:"forceRGBA",yes:{transcodeFormat:a.RGBA32,engineFormat:32856,roundToMultiple4:!1},no:{cap:"etc2",yes:{alpha:!0,yes:{transcodeFormat:a.ETC2_RGBA,engineFormat:37496},no:{transcodeFormat:a.ETC1_RGB,engineFormat:37492}},no:{cap:"etc1",alpha:!1,yes:{transcodeFormat:a.ETC1_RGB,engineFormat:36196},no:{cap:"bptc",yes:{transcodeFormat:a.BC7_RGBA,engineFormat:36492},no:{cap:"s3tc",yes:{alpha:!0,yes:{transcodeFormat:a.BC3_RGBA,engineFormat:33779},no:{transcodeFormat:a.BC1_RGB,engineFormat:33776}},no:{cap:"pvrtc",needsPowerOfTwo:!0,yes:{alpha:!0,yes:{transcodeFormat:a.PVRTC1_4_RGBA,engineFormat:35842},no:{transcodeFormat:a.PVRTC1_4_RGB,engineFormat:35840}},no:{transcodeFormat:a.RGBA32,engineFormat:32856,roundToMultiple4:!1}}}}}}},UASTC:{option:"forceRGBA",yes:{transcodeFormat:a.RGBA32,engineFormat:32856,roundToMultiple4:!1},no:{option:"forceR8",yes:{transcodeFormat:a.R8,engineFormat:33321,roundToMultiple4:!1},no:{option:"forceRG8",yes:{transcodeFormat:a.RG8,engineFormat:33323,roundToMultiple4:!1},no:{cap:"astc",yes:{transcodeFormat:a.ASTC_4X4_RGBA,engineFormat:37808},no:{cap:"bptc",yes:{transcodeFormat:a.BC7_RGBA,engineFormat:36492},no:{option:"useRGBAIfASTCBC7NotAvailableWhenUASTC",yes:{transcodeFormat:a.RGBA32,engineFormat:32856,roundToMultiple4:!1},no:{cap:"etc2",yes:{alpha:!0,yes:{transcodeFormat:a.ETC2_RGBA,engineFormat:37496},no:{transcodeFormat:a.ETC1_RGB,engineFormat:37492}},no:{cap:"etc1",yes:{transcodeFormat:a.ETC1_RGB,engineFormat:36196},no:{cap:"s3tc",yes:{alpha:!0,yes:{transcodeFormat:a.BC3_RGBA,engineFormat:33779},no:{transcodeFormat:a.BC1_RGB,engineFormat:33776}},no:{cap:"pvrtc",needsPowerOfTwo:!0,yes:{alpha:!0,yes:{transcodeFormat:a.PVRTC1_4_RGBA,engineFormat:35842},no:{transcodeFormat:a.PVRTC1_4_RGB,engineFormat:35840}},no:{transcodeFormat:a.RGBA32,engineFormat:32856,roundToMultiple4:!1}}}}}}}}}}}};class O{static _IsLeafNode(e){return void 0!==e.engineFormat}get transcodeFormat(){return this._transcodeFormat}get engineFormat(){return this._engineFormat}get roundToMultiple4(){return this._roundToMultiple4}constructor(e,t,r,a,s){this._textureFormat=e,this._hasAlpha=t,this._isPowerOfTwo=r,this._caps=a,this._options=s??{},this.parseTree(G)}parseTree(e){const t=this._textureFormat===r.UASTC4x4?e.UASTC:e.ETC1S;return t&&this._parseNode(t),void 0!==t}_parseNode(e){if(e)if(O._IsLeafNode(e))this._transcodeFormat=e.transcodeFormat,this._engineFormat=e.engineFormat,this._roundToMultiple4=e.roundToMultiple4??!0;else{let t=!0;void 0!==e.cap&&(t=t&&!!this._caps[e.cap]),void 0!==e.option&&(t=t&&!!this._options[e.option]),void 0!==e.alpha&&(t=t&&this._hasAlpha===e.alpha),void 0!==e.needsPowerOfTwo&&(t=t&&this._isPowerOfTwo===e.needsPowerOfTwo),void 0!==e.transcodeFormat&&(t=Array.isArray(e.transcodeFormat)?t&&-1!==e.transcodeFormat.indexOf(this._transcodeFormat):t&&e.transcodeFormat===this._transcodeFormat),this._parseNode(t?e.yes:e.no)}}}const E=e=>!(e&e-1)&&0!==e;class F{constructor(){this._transcoderMgr=new u}decode(e,t,r){const a={...r,...F.DefaultDecoderOptions};return Promise.resolve().then((()=>{const r=new h(e);if(!r.isValid())throw new Error("Invalid KT2 file: wrong signature");return r.parse(),r.needZSTDDecoder?(this._zstdDecoder||(this._zstdDecoder=new w),this._zstdDecoder.init().then((()=>this._decodeData(r,t,a)))):this._decodeData(r,t,a)}))}_decodeData(e,t,s){const o=e.header.pixelWidth,i=e.header.pixelHeight,d=e.textureFormat,c=new O(d,e.hasAlpha,E(o)&&E(i),t,s);s?.transcodeFormatDecisionTree&&c.parseTree(s?.transcodeFormatDecisionTree);const l=c.transcodeFormat,m=c.engineFormat,h=c.roundToMultiple4,_=this._transcoderMgr.findTranscoder(d,l,e.isInGammaSpace,s?.bypassTranscoders);if(null===_)throw new Error(`no transcoder found to transcode source texture format "${r[d]}" to format "${a[l]}"`);const u=[],f=[],y={width:0,height:0,transcodedFormat:m,mipmaps:u,isInGammaSpace:e.isInGammaSpace,hasAlpha:e.hasAlpha,transcoderName:_.getName()};let p=0;for(let t=0;t<e.header.levelCount;t++){t>0&&(p+=Math.max(e.header.layerCount,1)*e.header.faceCount*Math.max(e.header.pixelDepth>>t-1,1));const r=Math.floor(o/(1<<t))||1,a=Math.floor(i/(1<<t))||1,s=e.header.faceCount,c=(r+3>>2)*(a+3>>2)*e.dfdBlock.bytesPlane[0],m=e.levels[t].uncompressedByteLength;let T=e.data.buffer,g=e.levels[t].byteOffset+e.data.byteOffset,B=0;e.header.supercompressionScheme===n.ZStandard&&(T=this._zstdDecoder.decode(new Uint8Array(T,g,e.levels[t].byteLength),m),g=0),0===t&&(y.width=h?r+3&-4:r,y.height=h?a+3&-4:a);for(let o=0;o<s;o++){let s,i=null;e.header.supercompressionScheme===n.BasisLZ?(i=e.supercompressionGlobalData.imageDescs[p+o],s=new Uint8Array(T,g+i.rgbSliceByteOffset,i.rgbSliceByteLength+i.alphaSliceByteLength)):(s=new Uint8Array(T,g+B,c),B+=c);const h={data:null,width:r,height:a},R=_.transcode(d,l,t,r,a,m,e,i,s).then((e=>(h.data=e,e))).catch((e=>(y.errors=y.errors??"",y.errors+=e+"\n"+e.stack+"\n",null)));f.push(R),u.push(h)}}return Promise.all(f).then((()=>y))}}F.DefaultDecoderOptions={},u.RegisterTranscoder(p),u.RegisterTranscoder(T),u.RegisterTranscoder(g),u.RegisterTranscoder(B),u.RegisterTranscoder(R),u.RegisterTranscoder(C),u.RegisterTranscoder(S);const P=void 0!==e.g?e.g:"undefined"!=typeof window?window:void 0;void 0!==P&&(P.KTX2DECODER=F);const L=l;return t.default})()));
//# sourceMappingURL=babylon.ktx2Decoder.js.map