UNPKG

@arcgis/core

Version:

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

6 lines (5 loc) 3.74 kB
/* All material copyright ESRI, All Rights Reserved, unless otherwise specified. See https://js.arcgis.com/4.33/esri/copyright.txt for details. */ import{getBasisEncoder as e}from"../../libs/basisu/BasisUEncoder.js";import{TextureFormat as t}from"../../libs/basisu/TextureFormat.js";import{getDXTEncoder as n}from"../../libs/dxtEncoder/DXTEncoder.js";import{CompressedTextureFormat as r}from"../webgl/enums.js";let a,s,l=null,i=null;class o{constructor(e,t){this.internalFormat=e,this.compressedTexture=t}}async function u(e){let t;if(t=e.data instanceof ImageBitmap?p(e.data):m(e.data,e.width,e.height,e.components,e.needsFlip),e.hasS3TC){i||await h();const n=new Uint8Array(t.length);if(i?.encode(t,e.width,e.height,e.preMultiplyAlpha,n)){const e=M(n,!0),t=[n.buffer];return{result:new o(e?.internalFormat??null,e?.textureData??null),transferList:t}}return{result:new o(null,null)}}if(e.hasETC){if(l||await c(),e.preMultiplyAlpha&&!i&&await h(),e.preMultiplyAlpha){const n=new Uint8ClampedArray(t.length);i?.premultiply(new Uint8Array(t),e.width,e.height,n),t=n}const n=await d(t,e.width,e.height,e.hasMipmap),r=n?await f(n):null,a=r?.compressedTexture?.levels.map((e=>e.buffer))||[];return{result:new o(r?.internalFormat??null,r?.compressedTexture??null),transferList:a}}return{result:new o(null,null)}}async function c(){l||(l=await(a??=e()))}async function h(){i||(i=await(s??=n()))}async function d(e,t,n,r,a=255,s=0,i=!1,o=!1){if(!l)return null;const u=new l.BasisEncoder;u.setPerceptual(!o),u.setCheckForAlpha(!0),u.setForceAlpha(!1),u.setRenormalize(o),u.setMipGen(r),u.setMipSRGB(!o),u.setCreateKTX2File(!0),u.setKTX2SRGBTransferFunc(!o),u.setQualityLevel(a),u.setCompressionLevel(s);const c=new Uint8Array(e.byteLength);u.setSliceSourceImage(0,new Uint8Array(e),t,n,i);const h=u.encode(c),d=new Uint8Array(c.buffer,0,h),f=new l.KTX2File(new Uint8Array(d));return f.isValid()?(u.delete(),d):(f.close(),f.delete(),u.delete(),null)}async function f(e){if(!l)return new o(null,null);const n=new l.KTX2File(new Uint8Array(e));n.startTranscoding();const[a,s]=n.getHasAlpha()?[t.ETC2_RGBA,r.COMPRESSED_RGBA8_ETC2_EAC]:[t.ETC1_RGB,r.COMPRESSED_RGB8_ETC2],i=n.getLevels(),u=[];for(let t=0;t<i;t++)u.push(new Uint8Array(n.getImageTranscodedSizeInBytes(t,0,0,a))),n.transcodeImage(u[t],t,0,0,a,0,-1,-1);return n.close(),n.delete(),{internalFormat:s,compressedTexture:{type:"compressed",levels:u}}}function p(e){const t=new OffscreenCanvas(e.width,e.height),n=t.getContext("2d");n.drawImage(e,0,0);return n.getImageData(0,0,t.width,t.height).data}function m(e,t,n,r,a){const s=new Uint8ClampedArray(e).subarray(0,t*n*r);if(!a)return s;const l=new Uint8ClampedArray(s.length),i=t*r;for(let o=0;o<n;o++){const e=o*i,t=(n-o-1)*i;l.set(s.subarray(e,e+i),t)}return l}const w=31,y=1,T=2,A=3,C=4,g=7,E=21,b=131072;function S(e){return e.charCodeAt(0)+(e.charCodeAt(1)<<8)+(e.charCodeAt(2)<<16)+(e.charCodeAt(3)<<24)}const _=S("DXT1"),D=S("DXT3"),R=S("DXT5");function M(e,t){const n=new Int32Array(e.buffer,e.byteOffset,w);let a,s;switch(n[E]){case _:a=8,s=r.COMPRESSED_RGB_S3TC_DXT1_EXT;break;case D:a=16,s=r.COMPRESSED_RGBA_S3TC_DXT3_EXT;break;case R:a=16,s=r.COMPRESSED_RGBA_S3TC_DXT5_EXT;break;default:return null}let l=1,i=n[C],o=n[A];(3&i||3&o)&&(i=i+3&-4,o=o+3&-4);const u=i,c=o;let h,d;n[T]&b&&!1!==t&&(l=Math.max(1,n[g]));let f=e.byteOffset+n[y]+4;const p=[];for(let r=0;r<l;++r)d=(i+3>>2)*(o+3>>2)*a,h=new Uint8Array(e.buffer,f,d),p.push(h),f+=d,i=Math.max(1,i>>1),o=Math.max(1,o>>1);return{textureData:{type:"compressed",levels:p},internalFormat:s,width:u,height:c}}export{o as TextureCompressionWorkerOutput,u as compress,d as compressRGBADataToKTX2,f as createTextureDataKTX2,c as initializeBasisEncoder,h as initializeDXTEncoder};