UNPKG

@arcgis/core

Version:

ArcGIS Maps SDK for JavaScript: A complete 2D and 3D mapping and data visualization API

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