@doegis/core
Version:
DOE GIS API
3 lines (1 loc) • 1.92 kB
JavaScript
import e from"../../../../core/Logger.js";import{isPowerOfTwo as r}from"../../../../core/mathUtils.js";import{unwrapOrThrow as t}from"../../../../core/maybe.js";import{TextureSamplingMode as o,CompressedTextureFormat as n}from"../../../webgl/enums.js";import{Texture as a}from"../../../webgl/Texture.js";const i=e.getLogger("esri.views.3d.webgl-engine.lib.DDSUtil"),s=542327876,l=131072,m=4;function u(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 h=u("DXT1"),p=u("DXT3"),d=u("DXT5"),g=31,f=0,C=1,w=2,D=3,T=4,_=7,A=20,E=21;function S(e,r,n){const{textureData:i,internalFormat:s,width:l,height:m}=t(M(n,r.hasMipmap??!1));return r.samplingMode=i.levels.length>1?o.LINEAR_MIPMAP_LINEAR:o.LINEAR,r.hasMipmap=i.levels.length>1,r.internalFormat=s,r.width=l,r.height=m,new a(e,r,i)}function M(e,t){const o=new Int32Array(e,0,g);if(o[f]!==s)return i.error("Invalid magic number in DDS header"),null;if(!(o[A]&m))return i.error("Unsupported format, must contain a FourCC code"),null;const a=o[E];let u,S;switch(a){case h:u=8,S=n.COMPRESSED_RGB_S3TC_DXT1_EXT;break;case p:u=16,S=n.COMPRESSED_RGBA_S3TC_DXT3_EXT;break;case d:u=16,S=n.COMPRESSED_RGBA_S3TC_DXT5_EXT;break;default:return i.error("Unsupported FourCC code:",c(a)),null}let M=1,x=o[T],R=o[D];0==(3&x)&&0==(3&R)||(i.warn("Rounding up compressed texture size to nearest multiple of 4."),x=x+3&-4,R=R+3&-4);const b=x,X=R;let I,j;o[w]&l&&!1!==t&&(M=Math.max(1,o[_])),1===M||r(x)&&r(R)||(i.warn("Ignoring mipmaps of non power of two sized compressed texture."),M=1);let v=o[C]+4;const F=[];for(let r=0;r<M;++r)j=(x+3>>2)*(R+3>>2)*u,I=new Uint8Array(e,v,j),F.push(I),v+=j,x=Math.max(1,x>>1),R=Math.max(1,R>>1);return{textureData:{type:"compressed",levels:F},internalFormat:S,width:b,height:X}}export{S as createDDSTexture,M as createDDSTextureData};