
Security News
Deno Goes the Way of the Dinosaur
Cloudflare is acquiring Deno’s team, which will provide another year of bug fixes and security updates before ending support for the runtime.
libimagequant-wasm
Advanced tools
WASM bindings for libimagequant image quantization library with Web Worker support
A TypeScript/JavaScript WebAssembly wrapper for the libimagequant image quantization library. This package provides high-quality image color quantization in the browser using Web Workers for optimal performance.
npm install libimagequant-wasm
import LibImageQuant from 'libimagequant-wasm';
const quantizer = new LibImageQuant();
// Fetch a PNG and quantize it
const response = await fetch('image.png');
const pngData = new Uint8Array(await response.arrayBuffer());
const result = await quantizer.quantizePng(pngData, {
maxColors: 64,
quality: { min: 0, target: 100 },
speed: 3,
});
console.log(`Quantized to ${result.paletteLength} colors`);
console.log(`Quality: ${Math.round(result.quality * 100)}%`);
// result.pngBytes - quantized indexed PNG as Uint8Array
// result.imageData - quantized image as ImageData (for canvas rendering)
quantizer.dispose();
import LibImageQuant from 'libimagequant-wasm';
const quantizer = new LibImageQuant();
const canvas = document.getElementById('myCanvas') as HTMLCanvasElement;
const ctx = canvas.getContext('2d')!;
const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
const result = await quantizer.quantizeImageData(imageData, {
maxColors: 32,
speed: 1,
dithering: 0.8,
});
// Draw quantized result back to canvas
ctx.putImageData(result.imageData, 0, 0);
quantizer.dispose();
quantizePng accepts multiple input types:
// Uint8Array
const result = await quantizer.quantizePng(pngBytes);
// ArrayBuffer
const result = await quantizer.quantizePng(arrayBuffer);
// Blob
const result = await quantizer.quantizePng(blob);
const quantizer = new LibImageQuant(options?: LibImageQuantOptions);
| Option | Type | Default | Description |
|---|---|---|---|
workerUrl | string | auto-detected | Custom path to the worker module |
wasmUrl | string | auto-detected | Custom path to the WASM module directory |
initTimeout | number | 10000 | Worker initialization timeout (ms) |
operationTimeout | number | 30000 | Per-operation timeout (ms) |
quantizePng(pngData, options?)Quantizes a PNG image from binary data.
pngData: Uint8Array | ArrayBuffer | Blob - PNG file bytesoptions: QuantizationOptions (optional)Promise<QuantizationResult>quantizeImageData(imageData, options?)Quantizes an ImageData object (e.g. from canvas).
imageData: ImageData - RGBA image dataoptions: QuantizationOptions (optional)Promise<QuantizationResult>dispose()Terminates the worker, rejects any pending operations, and releases resources. Safe to call multiple times.
{
speed?: number; // Speed vs quality (1-10, lower = better quality)
quality?: {
min: number; // Minimum acceptable quality (0-100)
target: number; // Target quality (0-100)
};
maxColors?: number; // Maximum colors in palette (2-256)
dithering?: number; // Dithering level (0.0-1.0)
posterization?: number; // Posterization level (0-4)
}
{
palette: number[][]; // Color palette as [[r,g,b,a], ...]
pngBytes: Uint8Array; // Quantized indexed PNG file bytes
imageData: ImageData; // Quantized RGBA image data (for canvas)
quality: number; // Achieved quality (0.0-1.0)
paletteLength: number; // Number of colors in palette
width: number; // Image width in pixels
height: number; // Image height in pixels
}
wasm32-unknown-unknown target# Install Rust and wasm-pack
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh
rustup target add wasm32-unknown-unknown
# Install dependencies
npm install
# Build
npm run build
| Script | Description |
|---|---|
npm run build | Full build (WASM + TypeScript + types) |
npm run build:wasm | Build WASM module only |
npm run build:types | Generate TypeScript declarations only |
npm run typecheck | Run TypeScript type checking |
npm test | Run tests (vitest + browser) |
npm run test:watch | Run tests in watch mode |
npm run dev | Start Vite dev server |
npm run clean | Remove build output |
Tests run in a real browser via Vitest with @vitest/browser and Playwright. No mocks — the actual WASM module is loaded and executed.
# Build first (tests run against built output)
npm run build
# Run tests (headless Chromium)
npm test
The test suite covers:
LibImageQuant class, all input formats, options, disposal, concurrency, error handlingImageQuantizer, QuantizationResult, PNG encode/decode, memory managementRequires WebAssembly and Web Worker support:
dispose() when done to release the Web WorkerSee examples/test.html for a complete working example with:
MIT License - See LICENSE file for details.
Important Note: This project wraps the libimagequant library, which is dual-licensed under GPL-3.0 and commercial licenses. While this wrapper is MIT licensed, you must ensure you comply with the appropriate libimagequant license for your use case. For commercial use, you may need to obtain a commercial license from the libimagequant authors.
npm run buildnpm testEnsure the worker module is served from the same origin, or pass a custom workerUrl to the constructor.
typeof WebAssembly === 'object'.wasm files (application/wasm)wasmUrl if the WASM files are hosted separatelyspeed settings for real-time applicationsFAQs
WASM bindings for libimagequant image quantization library with Web Worker support
We found that libimagequant-wasm demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.

Security News
Cloudflare is acquiring Deno’s team, which will provide another year of bug fixes and security updates before ending support for the runtime.

Security News
Anthropic’s free OSS Scanner uses Mythos and other Claude models to find open source vulnerabilities and send maintainers reports with proposed fixes.

Security News
On Front Lines’ Early Adopters: CISO podcast, Andrew Becherer shares lessons from Datadog, weighs the long-term costs of AI-built tools, and explains how founders can actually gain a CISO’s trust.