!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:()=>G});var r,a,s,n,o={};e.r(o),e.d(o,{DataReader:()=>i,KTX2Decoder:()=>w,KTX2FileReader:()=>d,LiteTranscoder:()=>m,LiteTranscoder_UASTC_ASTC:()=>h,LiteTranscoder_UASTC_BC7:()=>u,LiteTranscoder_UASTC_R8_UNORM:()=>T,LiteTranscoder_UASTC_RG8_UNORM:()=>p,LiteTranscoder_UASTC_RGBA_SRGB:()=>y,LiteTranscoder_UASTC_RGBA_UNORM:()=>f,MSCTranscoder:()=>B,SupercompressionScheme:()=>n,Transcoder:()=>_,TranscoderManager:()=>l,WASMMemoryManager:()=>c,ZSTDDecoder:()=>M}),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 i{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={}));class d{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 d.IsValid(this._data)}parse(){let e=12;const t=new i(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 i(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 i(this._data,r.dfdByteOffset,r.dfdByteLength),d=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};d.numSamples=(d.descriptorBlockSize-24)/16;for(let e=0;e>4,e.channelType=15&e.channelType,d.samples.push(e)}const c=this._supercompressionGlobalData={};if(r.sgdByteLength>0){const e=new i(this._data,r.sgdByteOffset,r.sgdByteLength);c.endpointCount=e.readUint16(),c.selectorCount=e.readUint16(),c.endpointsByteLength=e.readUint32(),c.selectorsByteLength=e.readUint32(),c.tablesByteLength=e.readUint32(),c.extendedByteLength=e.readUint32(),c.imageDescs=[];const t=this._getImageCount();for(let r=0;r>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 c{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=c.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({module:e.instance.exports})))}_loadModule(e=this._wasmBinary){return this._modulePromise=this._modulePromise||(e?Promise.resolve(e):c.LoadWASM(this._modulePath)).then((e=>this._instantiateWebAssembly(e))),this._modulePromise}get memoryManager(){return this._memoryManager}setModulePath(e,t){this._modulePath=_.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 h extends m{static CanTranscode(e,t,s){return e===r.UASTC4x4&&t===a.ASTC_4X4_RGBA}getName(){return h.Name}initialize(){super.initialize(),this.setModulePath(h.WasmModuleURL,h.WasmBinary)}}h.WasmModuleURL="https://cdn.babylonjs.com/ktx2Transcoders/1/uastc_astc.wasm",h.WasmBinary=null,h.Name="UniversalTranscoder_UASTC_ASTC";class u extends m{static CanTranscode(e,t,s){return e===r.UASTC4x4&&t===a.BC7_RGBA}getName(){return u.Name}initialize(){super.initialize(),this.setModulePath(u.WasmModuleURL,u.WasmBinary)}}u.WasmModuleURL="https://cdn.babylonjs.com/ktx2Transcoders/1/uastc_bc7.wasm",u.WasmBinary=null,u.Name="UniversalTranscoder_UASTC_BC7";class f extends m{static CanTranscode(e,t,s){return e===r.UASTC4x4&&t===a.RGBA32&&!s}getName(){return f.Name}initialize(){super.initialize(),this._transcodeInPlace=!1,this.setModulePath(f.WasmModuleURL,f.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}))}}f.WasmModuleURL="https://cdn.babylonjs.com/ktx2Transcoders/1/uastc_rgba8_unorm_v2.wasm",f.WasmBinary=null,f.Name="UniversalTranscoder_UASTC_RGBA_UNORM";class y extends m{static CanTranscode(e,t,s){return e===r.UASTC4x4&&t===a.RGBA32&&s}getName(){return y.Name}initialize(){super.initialize(),this._transcodeInPlace=!1,this.setModulePath(y.WasmModuleURL,y.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}))}}y.WasmModuleURL="https://cdn.babylonjs.com/ktx2Transcoders/1/uastc_rgba8_srgb_v2.wasm",y.WasmBinary=null,y.Name="UniversalTranscoder_UASTC_RGBA_SRGB";class T extends m{static CanTranscode(e,t,s){return e===r.UASTC4x4&&t===a.R8}getName(){return T.Name}initialize(){super.initialize(),this._transcodeInPlace=!1,this.setModulePath(T.WasmModuleURL,T.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}))}}T.WasmModuleURL="https://cdn.babylonjs.com/ktx2Transcoders/1/uastc_r8_unorm.wasm",T.WasmBinary=null,T.Name="UniversalTranscoder_UASTC_R8_UNORM";class p extends m{static CanTranscode(e,t,s){return e===r.UASTC4x4&&t===a.RG8}getName(){return p.Name}initialize(){super.initialize(),this._transcodeInPlace=!1,this.setModulePath(p.WasmModuleURL,p.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}))}}p.WasmModuleURL="https://cdn.babylonjs.com/ktx2Transcoders/1/uastc_rg8_unorm.wasm",p.WasmBinary=null,p.Name="UniversalTranscoder_UASTC_RG8_UNORM";class B extends _{getName(){return B.Name}_getMSCBasisTranscoder(){return this._mscBasisTranscoderPromise||(this._mscBasisTranscoderPromise=(B.WasmBinary?Promise.resolve(B.WasmBinary):c.LoadWASM(_.GetWasmUrl(B.WasmModuleURL))).then((e=>{if(B.JSModule&&"undefined"==typeof MSC_TRANSCODER)globalThis.MSC_TRANSCODER=B.JSModule;else if(B.UseFromWorkerThread)importScripts(_.GetWasmUrl(B.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",_.GetWasmUrl(B.JSModuleURL)),s.onload=()=>{MSC_TRANSCODER({wasmBinary:e}).then((e=>{e.initTranscoders(),this._mscBasisModule=e,t()}))},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 _=!1;return this._getMSCBasisTranscoder().then((()=>{const m=this._mscBasisModule;let h,u,f,y=null;try{h=e===r.UASTC4x4?new m.UastcImageTranscoder:new m.BasisLzEtc1sImageTranscoder;const y=e===r.UASTC4x4?m.TextureFormat.UASTC4x4:m.TextureFormat.ETC1S;u=new m.ImageInfo(y,n,o,s);const T=m.TranscodeTarget[a[t]];if(!m.isFormatSupported(T,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;h.decodePalettes(e.endpointCount,e.endpointsData,e.selectorCount,e.selectorsData),h.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=h.transcodeImage(T,l,u,0,_)}else u.flags=0,u.rgbByteOffset=0,u.rgbByteLength=i,u.alphaByteOffset=0,u.alphaByteLength=0,f=h.transcodeImage(T,l,u,0,d.hasAlpha,_)}finally{h&&h.delete(),u&&u.delete(),f&&f.transcodedImage&&(y=f.transcodedImage.get_typed_memory_view().slice(),f.transcodedImage.delete())}return y}))}}let g,R,C;B.JSModuleURL="https://cdn.babylonjs.com/ktx2Transcoders/1/msc_basis_transcoder.js",B.WasmModuleURL="https://cdn.babylonjs.com/ktx2Transcoders/1/msc_basis_transcoder.wasm",B.WasmBinary=null,B.JSModule=null,B.UseFromWorkerThread=!0,B.Name="MSCTranscoder";const S={env:{emscripten_notify_memory_growth:function(){C=new Uint8Array(R.exports.memory.buffer)}}};class M{init(){return g||(g="undefined"!=typeof fetch?fetch(_.GetWasmUrl(M.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,S))).then(this._init):WebAssembly.instantiateStreaming(fetch(M.WasmModuleURL),S).then(this._init),g)}_init(e){R=e.instance,S.env.emscripten_notify_memory_growth()}decode(e,t=0){if(!R)throw new Error("ZSTDDecoder: Await .init() before decoding.");const r=e.byteLength,a=R.exports.malloc(r);C.set(e,a),t=t||Number(R.exports.ZSTD_findDecompressedSize(a,r));const s=R.exports.malloc(t),n=R.exports.ZSTD_decompress(s,t,a,r),o=C.slice(s,s+n);return R.exports.free(a),R.exports.free(s),o}}M.WasmModuleURL="https://cdn.babylonjs.com/zstddec.wasm";const U={ETC1S:{option:"forceRGBA",yes:{transcodeFormat:a.RGBA32,engineFormat:s.RGBA8Format,roundToMultiple4:!1},no:{cap:"etc2",yes:{alpha:!0,yes:{transcodeFormat:a.ETC2_RGBA,engineFormat:s.COMPRESSED_RGBA8_ETC2_EAC},no:{transcodeFormat:a.ETC1_RGB,engineFormat:s.COMPRESSED_RGB8_ETC2}},no:{cap:"etc1",alpha:!1,yes:{transcodeFormat:a.ETC1_RGB,engineFormat:s.COMPRESSED_RGB_ETC1_WEBGL},no:{cap:"bptc",yes:{transcodeFormat:a.BC7_RGBA,engineFormat:s.COMPRESSED_RGBA_BPTC_UNORM_EXT},no:{cap:"s3tc",yes:{alpha:!0,yes:{transcodeFormat:a.BC3_RGBA,engineFormat:s.COMPRESSED_RGBA_S3TC_DXT5_EXT},no:{transcodeFormat:a.BC1_RGB,engineFormat:s.COMPRESSED_RGB_S3TC_DXT1_EXT}},no:{cap:"pvrtc",needsPowerOfTwo:!0,yes:{alpha:!0,yes:{transcodeFormat:a.PVRTC1_4_RGBA,engineFormat:s.COMPRESSED_RGBA_PVRTC_4BPPV1_IMG},no:{transcodeFormat:a.PVRTC1_4_RGB,engineFormat:s.COMPRESSED_RGB_PVRTC_4BPPV1_IMG}},no:{transcodeFormat:a.RGBA32,engineFormat:s.RGBA8Format,roundToMultiple4:!1}}}}}}},UASTC:{option:"forceRGBA",yes:{transcodeFormat:a.RGBA32,engineFormat:s.RGBA8Format,roundToMultiple4:!1},no:{option:"forceR8",yes:{transcodeFormat:a.R8,engineFormat:s.R8Format,roundToMultiple4:!1},no:{option:"forceRG8",yes:{transcodeFormat:a.RG8,engineFormat:s.RG8Format,roundToMultiple4:!1},no:{cap:"astc",yes:{transcodeFormat:a.ASTC_4X4_RGBA,engineFormat:s.COMPRESSED_RGBA_ASTC_4X4_KHR},no:{cap:"bptc",yes:{transcodeFormat:a.BC7_RGBA,engineFormat:s.COMPRESSED_RGBA_BPTC_UNORM_EXT},no:{option:"useRGBAIfASTCBC7NotAvailableWhenUASTC",yes:{transcodeFormat:a.RGBA32,engineFormat:s.RGBA8Format,roundToMultiple4:!1},no:{cap:"etc2",yes:{alpha:!0,yes:{transcodeFormat:a.ETC2_RGBA,engineFormat:s.COMPRESSED_RGBA8_ETC2_EAC},no:{transcodeFormat:a.ETC1_RGB,engineFormat:s.COMPRESSED_RGB8_ETC2}},no:{cap:"etc1",yes:{transcodeFormat:a.ETC1_RGB,engineFormat:s.COMPRESSED_RGB_ETC1_WEBGL},no:{cap:"s3tc",yes:{alpha:!0,yes:{transcodeFormat:a.BC3_RGBA,engineFormat:s.COMPRESSED_RGBA_S3TC_DXT5_EXT},no:{transcodeFormat:a.BC1_RGB,engineFormat:s.COMPRESSED_RGB_S3TC_DXT1_EXT}},no:{cap:"pvrtc",needsPowerOfTwo:!0,yes:{alpha:!0,yes:{transcodeFormat:a.PVRTC1_4_RGBA,engineFormat:s.COMPRESSED_RGBA_PVRTC_4BPPV1_IMG},no:{transcodeFormat:a.PVRTC1_4_RGB,engineFormat:s.COMPRESSED_RGB_PVRTC_4BPPV1_IMG}},no:{transcodeFormat:a.RGBA32,engineFormat:s.RGBA8Format,roundToMultiple4:!1}}}}}}}}}}}};class A{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(U)}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(A._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 w{constructor(){this._transcoderMgr=new l}decode(e,t,r){const a={...r,...w.DefaultDecoderOptions};return Promise.resolve().then((()=>{const r=new d(e);if(!r.isValid())throw new Error("Invalid KT2 file: wrong signature");return r.parse(),r.needZSTDDecoder?(this._zstdDecoder||(this._zstdDecoder=new M),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 A(d,e.hasAlpha,E(o)&&E(i),t,s);s?.transcodeFormatDecisionTree&&c.parseTree(s?.transcodeFormatDecisionTree);const l=c.transcodeFormat,_=c.engineFormat,m=c.roundToMultiple4,h=this._transcoderMgr.findTranscoder(d,l,e.isInGammaSpace,s?.bypassTranscoders);if(null===h)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:_,mipmaps:u,isInGammaSpace:e.isInGammaSpace,hasAlpha:e.hasAlpha,transcoderName:h.getName()};let T=0;for(let t=0;t0&&(T+=Math.max(e.header.layerCount,1)*e.header.faceCount*Math.max(e.header.pixelDepth>>t-1,1));const r=Math.floor(o/(1<>2)*(a+3>>2)*e.dfdBlock.bytesPlane[0],_=e.levels[t].uncompressedByteLength;let p=e.data.buffer,B=e.levels[t].byteOffset+e.data.byteOffset,g=0;e.header.supercompressionScheme===n.ZStandard&&(p=this._zstdDecoder.decode(new Uint8Array(p,B,e.levels[t].byteLength),_),B=0),0===t&&(y.width=m?r+3&-4:r,y.height=m?a+3&-4:a);for(let o=0;o(m.data=e,e))).catch((e=>(y.errors=y.errors??"",y.errors+=e+"\n"+e.stack+"\n",null)));f.push(R),u.push(m)}}return Promise.all(f).then((()=>y))}}w.DefaultDecoderOptions={},l.RegisterTranscoder(h),l.RegisterTranscoder(u),l.RegisterTranscoder(f),l.RegisterTranscoder(y),l.RegisterTranscoder(T),l.RegisterTranscoder(p),l.RegisterTranscoder(B);const b=void 0!==e.g?e.g:"undefined"!=typeof window?window:void 0;void 0!==b&&(b.KTX2DECODER=w);const G=o;return t.default})())); //# sourceMappingURL=babylon.ktx2Decoder.js.map