texture-codec
100% OSI-licensed, zero-dependency pure-ESM GPU texture codec, container protocol parser, Texture Doctor, and cancelable Module Worker SDK for Node >= 20, Vite, and Browsers.
Supports all 8 GPU texture containers (ASTC, Basis, CRN, DDS, KTX, KTX2, PKM, PVR), including ASTC/BC6H HDR, BC1–BC7, ETC1/ETC2/EAC (including signed EAC via Apache-2.0 etc2comp + ISC detex), PVRTC, and Basis/KTX2 UASTC/ETC1S.
Install
pnpm add texture-codec
# or: npm install texture-codec
Subpath Exports
| Entrypoint |
Purpose |
WASM Included in Bundle? |
texture-codec |
Default all-in-one entry (core + codecs), zero-config in Node >= 20 and Vite |
Lazy per-codec |
texture-codec/core (also /protocols, /capabilities, /doctor, /archive, /errors) |
Pure TS container protocols (ASTC, Basis, DDS, KTX, PKM, PVR), CODEC_REGISTRY, Texture Doctor, ZIP builder, HDR tone mapper, TextureCodecError |
No (0 WASM, 0 DOM) |
texture-codec/codecs / /codecs/{astc,basis,crn,dds,ktx,ktx2,pkm,pvr} |
Tree-shakeable single-container plugins + createTextureCodecFacade |
Only imported codecs |
texture-codec/codecs/cdn |
External CDN / self-hosted WASM facade (zero .wasm bundled into app build) |
No |
texture-codec/decoders / /decoders/cdn / /decoders/{facade,astc,basis,crn,dds,ktx,ktx2,pkm,pvr} |
Decode-only container subpaths & TextureDecodeFacade (zero *Encoder, zero CompressorRegistry, zero encoder WASMs) |
Decode-only WASMs (cdn & per-format: No) |
texture-codec/decompressors / /decompressors/{astc,bcn,etc,pvrtc,raw} |
Pure block-level decompressors routed to lightweight *-decoder / bcdec WASMs without _malloc/_free glue |
No (configure via runtime or /decoders) |
texture-codec/worker |
Zero-config Web Module Worker pool (TextureCodecWorkerFacade) for Vite |
Lazy per-codec |
texture-codec/worker/pool & /worker/worker-cdn |
Custom-worker pool (TextureCodecWorkerPool, attachTextureCodecWorker) for Next.js / CDN with zero duplicate .wasm emission |
No |
texture-codec/wasm & /wasm/{astc-decoder,etc-decoder,crunch-decoder,bcdec,astc,etc,bcn,ktx2,...} |
Typed pure-ES6 Emscripten factories (including lightweight decode-only modules with ≤ 4 MB initial linear memory), wasmUrl, provenance.json, sbom.cdx.json |
Direct WASM assets |
Runtime Quickstarts
1. Node >= 20 (Pure ESM, Zero-Config)
The node conditional export automatically wires node:fs/promises for bundled .wasm loading—no baseUrl or fetch polyfill needed.
import {
inspectTextureContainer,
sniffTextureContainer,
textureCodecFacade,
} from 'texture-codec';
const rgba = { width: 64, height: 64, data: new Uint8Array(64 * 64 * 4) };
// Encode to KTX2 (UASTC)
const encoded = await textureCodecFacade.encode('ktx2', rgba, { format: 'UASTC' });
console.log(sniffTextureContainer(encoded.data)); // 'ktx2'
console.log(inspectTextureContainer(encoded.data));
// Decode preview (RGBA8) or structured TextureAsset (mips / layers / faces / float)
const preview = await textureCodecFacade.decodePreview('ktx2', encoded.data);
const asset = await textureCodecFacade.decodeAsset('ktx2', encoded.data);
2. Vite (Dev + Production Build + Module Worker)
Add optimizeDeps.exclude and worker.format = 'es' in vite.config.ts so Vite preserves import.meta.url WASM assets and ES Module Worker code-splitting:
// vite.config.ts
import { defineConfig } from 'vite';
export default defineConfig({
optimizeDeps: {
exclude: ['texture-codec'],
},
worker: {
format: 'es',
},
build: {
target: 'es2022',
},
});
import { TextureCodecWorkerFacade } from 'texture-codec/worker';
const worker = new TextureCodecWorkerFacade({ maxWorkers: 2 });
const encoded = await worker.encode('dds', rgba, { format: 'BC7' });
const compared = await worker.compareFormat('astc', rgba, { blockX: 4, blockY: 4 });
console.log('PSNR (dB):', compared.metrics.psnrRgb);
worker.dispose();
3. Next.js / Browser / Custom CDN Hosting (Zero Duplicate .wasm Emission)
When hosting WASM files externally (e.g., public/assets/wasm or a CDN), import from texture-codec/codecs/cdn and texture-codec/worker/pool so Next.js / Turbopack emits zero duplicate .wasm copies into _next/static/media:
// app-codec.worker.ts
import {
TextureCodecFacade,
configureWasmRuntime,
decodeHdrComparePixels,
} from 'texture-codec/codecs/cdn';
import { attachTextureCodecWorker } from 'texture-codec/worker/pool';
configureWasmRuntime({
baseUrl: '/assets/wasm',
cacheKey: process.env.NEXT_PUBLIC_WASM_VERSION,
});
attachTextureCodecWorker({
facade: new TextureCodecFacade(),
configureWasmRuntime,
decodeHdrComparePixels,
});
// Main thread pool
import { TextureCodecWorkerPool } from 'texture-codec/worker/pool';
const pool = new TextureCodecWorkerPool({
workerFactory: () => new Worker(new URL('./app-codec.worker.ts', import.meta.url)),
maxWorkers: 2,
wasm: { baseUrl: '/assets/wasm', cacheKey: process.env.NEXT_PUBLIC_WASM_VERSION },
});
4. Tree-Shakeable Single-Container & Decode-Only Usage
// Decode-only all-container viewer (excludes all 7 encoder WASMs & all *Encoder code)
import { textureDecodeFacade } from 'texture-codec/decoders';
const preview = await textureDecodeFacade.decodeAuto(fileBytes);
// Single-format decode-only bundle (e.g. ASTC only: emits only 42 KB astc-decoder.wasm)
import { registerBundledWasmLoaders } from 'texture-codec/codecs/runtime';
import { TextureDecodeFacade } from 'texture-codec/decoders/facade';
import { createAstcDecoder } from 'texture-codec/decoders/astc';
registerBundledWasmLoaders({
'astc-decoder': () => import('texture-codec/wasm/astc-decoder'),
});
const astcViewer = new TextureDecodeFacade({ decoders: [createAstcDecoder()] });
const decoded = await astcViewer.decodePreview('astc', astcBytes);
// Full encode + decode single-container plugin (lazy-loadable on export dialog)
import { createTextureCodecFacade } from 'texture-codec/codecs';
import { createAstcCodec } from 'texture-codec/codecs/astc';
const astcOnly = createTextureCodecFacade({
codecs: [createAstcCodec()],
});
Supply Chain & 100% OSI Licensing
Every shipped WASM binary is built from pinned official upstream commits (third_party/* gitlinks in .gitmodules) plus small, auditable in-repo patches (scripts/wasm/patches/) and native C/C++ glue (scripts/wasm/native/).
- Zero personal forks: All upstream components point directly to official repositories.
- Zero non-OSI codecs: Legacy non-OSI
ETCPACK has been retired and replaced by google/etc2comp (Apache-2.0) + hglm/detex (ISC), and libktx is compiled with -DLIBKTX_FEATURE_ETC_UNPACK=OFF.
- Verifiable artifacts: See
docs/wasm-provenance.json, docs/wasm-sbom.cdx.json, and THIRD_PARTY_NOTICES.md.
Development & Verification
pnpm install
pnpm run build # Build JS/DTS (tsup) + WASM wrappers (tsc)
pnpm run typecheck # Type-check all source & WASM modules
pnpm run lint:pkg # Run publint (--strict) + attw (--profile esm-only) on packed tarball
pnpm run wasm:provenance # Verify SHA-256 hashes, strict-CSP glue & decode memory limits of all 28 WASM artifacts
pnpm run wasm:dependencies # Verify frozen codec & upstream pin policy
pnpm run test:node # Verify Node >= 20 pure ESM across all 8 containers, HDR & signed EAC
pnpm run test:pack # Verify offline isolated tarball consumer
pnpm run test:vite # Verify Vite production build + Headless Chrome Dev/Preview Worker E2E
Rebuilding WASM from Source
git submodule update --init --recursive
pnpm run verify:wasm-source
pnpm run wasm:build
License
MIT — see LICENSE and THIRD_PARTY_NOTICES.md for bundled third-party WASM component licenses.