/* COPYRIGHT Esri - https://js.arcgis.com/5.0/LICENSE.txt */
import e from"../../../../core/Logger.js";import{CompressedTextureFormat as t}from"../../../webgl/enums.js";import r from"../../../webgl/Texture.js";const n=()=>e.getLogger("esri.views.3d.webgl-engine.lib.DDSUtil"),o=542327876,a=131072,i=4;function s(e){return e.charCodeAt(0)+(e.charCodeAt(1)<<8)+(e.charCodeAt(2)<<16)+(e.charCodeAt(3)<<24)}function l(e){return String.fromCharCode(255&e,e>>8&255,e>>16&255,e>>24&255)}const u=s("DXT1"),c=s("DXT3"),h=s("DXT5"),m=31,f=0,d=1,p=2,g=3,D=4,C=7,w=20,S=21;function T(e,t,n){const o=b(n,t.hasMipmap??!1);if(null==o)throw new Error("DDS texture data is null");const{textureData:a,internalFormat:i,width:s,height:l}=o;return t.samplingMode=a.levels.length>1?9987:9729,t.hasMipmap=a.levels.length>1,t.internalFormat=i,t.width=s,t.height=l,new r(e,t,a)}function b(e,r){const s=new Int32Array(e.buffer,e.byteOffset,m);if(s[f]!==o)return n().error("Invalid magic number in DDS header"),null;if(!(s[w]&i))return n().error("Unsupported format, must contain a FourCC code"),null;const T=s[S];let b,_;switch(T){case u:b=8,_=t.COMPRESSED_RGB_S3TC_DXT1_EXT;break;case c:b=16,_=t.COMPRESSED_RGBA_S3TC_DXT3_EXT;break;case h:b=16,_=t.COMPRESSED_RGBA_S3TC_DXT5_EXT;break;default:return n().error("Unsupported FourCC code:",l(T)),null}let E=1,x=s[D],M=s[g];(3&x||3&M)&&(n().warn("Rounding up compressed texture size to nearest multiple of 4."),x=x+3&-4,M=M+3&-4);const X=x,A=M;let R,v;s[p]&a&&!1!==r&&(E=Math.max(1,s[C]));let y=e.byteOffset+s[d]+4;const F=[];for(let t=0;t<E;++t)v=(x+3>>2)*(M+3>>2)*b,R=new Uint8Array(e.buffer,y,v),F.push(R),y+=v,x=Math.max(1,x>>1),M=Math.max(1,M>>1);return{textureData:{type:"compressed",levels:F},internalFormat:_,width:X,height:A}}export{T as createDDSTexture,b as createDDSTextureData};
