astc-encoder.js
v1.0.0
Published
WASM bindings for ARM astc-encoder
Maintainers
Readme
astc-encoder.js
WASM bindings for the ARM astc-encoder codec, providing a high-performance JavaScript interface for compressing and decompressing ASTC textures.
Supports:
- ASTC compression
- ASTC decompression
- Quality metrics
- ESM and CommonJS
- Node.js and browsers
Installation
npm install astc-encoder.jsFeatures
- WASM-Powered Core: Leverages WebAssembly for high-performance encoding and decoding.
- Multithreaded Execution: Supports parallel processing in both browser (Web Workers) and Node.js environments.
- Flexible Profiles: Full support for LDR, LDR sRGB, and HDR color profiles.
- Advanced Configuration: Tune compression with custom block sizes, quality presets, and channel swizzling.
Browser Support
This package is fully compatible with modern browsers via the ESM entry point.
To enable multithreaded WebAssembly in the browser, your application must be Cross-Origin Isolated. This requires serving your page with the following HTTP headers:
Cross-Origin-Opener-Policy: same-origin
Cross-Origin-Embedder-Policy: require-corpUsage Example
1. Initialize Configuration and Context
import { ASTCConfig, ASTCContext, ASTCProfile, ASTCQualityPreset } from 'astc-encoder.js';
// Setup configuration for 6x6 block size at medium quality
const config = new ASTCConfig(ASTCProfile.LDR_SRGB, 6, 6, 1, ASTCQualityPreset.MEDIUM);
// Create a single-threaded context
const context = new ASTCContext(config);2. Compress an Image
import { ASTCImage, ASTCSwizzle, ASTCType } from 'astc-encoder.js';
// Prepare uncompressed RGBA data
const image = new ASTCImage(ASTCType.U8, width, height, 1, rgbaUint8Array);
const swizzle = new ASTCSwizzle(); // Default RGBA
const compressedData = await context.compress(image, swizzle);3. Decompress an Image
// Create an empty image container for output
const outImage = new ASTCImage(ASTCType.U8, width, height);
// Decode data back into the image container
await context.decompress(compressedData, outImage, swizzle);
console.log(outImage.data); // Decoded RGBA bytesNote: The compressed data must not contain the 16 byte header from ASTC.
Attribution
This project utilizes the core codec from ARM software's astc-encoder and is heavily inspired by the API design of astc-encoder-py.
