/*
All material copyright ESRI, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.24/esri/copyright.txt for details.
*/
define(["exports","../../../../core/Logger","../../../../core/mathUtils","../../../webgl/enums","../../../webgl/Texture"],(function(e,t,r,o,n){"use strict";const a=t.getLogger("esri.views.3d.webgl-engine.lib.DDSUtil"),s=542327876,i=131072,u=4;function l(e){return e.charCodeAt(0)+(e.charCodeAt(1)<<8)+(e.charCodeAt(2)<<16)+(e.charCodeAt(3)<<24)}function c(e){return String.fromCharCode(255&e,e>>8&255,e>>16&255,e>>24&255)}const d=l("DXT1"),m=l("DXT3"),p=l("DXT5"),h=31,g=0,T=1,D=2,w=3,C=4,f=7,S=20,x=21;function _(e,t,r){const{textureData:a,internalFormat:s,width:i,height:u}=M(r,t.hasMipmap);return t.samplingMode=a.levels.length>1?o.TextureSamplingMode.LINEAR_MIPMAP_LINEAR:o.TextureSamplingMode.LINEAR,t.hasMipmap=a.levels.length>1,t.internalFormat=s,t.width=i,t.height=u,new n.Texture(e,t,a)}function M(e,t){const n=new Int32Array(e,0,h);if(n[g]!==s)return a.error("Invalid magic number in DDS header"),null;if(!(n[S]&u))return a.error("Unsupported format, must contain a FourCC code"),null;const l=n[x];let _,M;switch(l){case d:_=8,M=o.CompressedTextureFormat.COMPRESSED_RGB_S3TC_DXT1_EXT;break;case m:_=16,M=o.CompressedTextureFormat.COMPRESSED_RGBA_S3TC_DXT3_EXT;break;case p:_=16,M=o.CompressedTextureFormat.COMPRESSED_RGBA_S3TC_DXT5_EXT;break;default:return a.error("Unsupported FourCC code:",c(l)),null}let A=1,E=n[C],b=n[w];0==(3&E)&&0==(3&b)||(a.warn("Rounding up compressed texture size to nearest multiple of 4."),E=E+3&-4,b=b+3&-4);const R=E,X=b;let F,P;n[D]&i&&!1!==t&&(A=Math.max(1,n[f])),1===A||r.isPowerOfTwo(E)&&r.isPowerOfTwo(b)||(a.warn("Ignoring mipmaps of non power of two sized compressed texture."),A=1);let v=n[T]+4;const I=[];for(let r=0;r<A;++r)P=(E+3>>2)*(b+3>>2)*_,F=new Uint8Array(e,v,P),I.push(F),v+=P,E=Math.max(1,E>>1),b=Math.max(1,b>>1);return{textureData:{type:"compressed",levels:I},internalFormat:M,width:R,height:X}}e.createDDSTexture=_,e.createDDSTextureData=M,Object.defineProperties(e,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})}));
