wasm-color-quant
v0.1.2
Published
A WebAssembly-based color quantization module for JavaScript and TypeScript environments. It uses the NEUQUANT algorithm via Rust's `color_quant` library to reduce RGBA pixel data to a limited color palette, returning both indexed pixels and the color map
Readme
wasm-color-quant (NEUQUANT)
A WebAssembly-based color quantization module for JavaScript and TypeScript environments. It uses the NEUQUANT algorithm via Rust's color_quant library to reduce RGBA pixel data to a limited color palette, returning both indexed pixels and the color map.
✨ Features
- Fast and compact NEUQUANT implementation in WebAssembly
- Works directly with
Uint8Arraybuffers - Customizable palette size
- Flat output buffer containing indexed pixels and RGBA palette
📦 Installation
npm install wasm-color-quant🦀 Rust API (WASM)
quantize_with_palette(pixels: &[u8], sample_factor: i32, palette_size: usize) -> Vec<u8>
Quantizes an RGBA image using NEUQUANT and returns a flat buffer containing the indexed pixel data followed by the RGBA palette.
Parameters
| Name | Type | Description |
|----------------|----------|------------------------------------------|
| pixels | Uint8Array | Flat RGBA pixel buffer (4 bytes per pixel) |
| sample_factor| number | Quality/sample trade-off (e.g. 10) |
| palette_size | number | Number of colors to generate (e.g. 256) |
Returns
A Uint8Array where:
- The first
pixels.length / 4bytes are indices. - The remaining
palette_size * 4bytes are the color palette (RGBA).
🧪 Example (JavaScript)
import init, { quantize_with_palette } from "wasm-color-quant";
await init();
const pixels = new Uint8Array([...]); // Flat RGBA input
const sampleFactor = 10;
const paletteSize = 128;
const result = quantize_with_palette(pixels, sampleFactor, paletteSize);
const indexedPixels = result.slice(0, pixels.length / 4);
const colorPalette = result.slice(pixels.length / 4);
console.log("Indexed:", indexedPixels);
console.log("Palette:", colorPalette);🧠 Notes
- NEUQUANT performs best on low-frequency, non-photo images like icons and illustrations.
- For photographic images, you may consider alternative quantization algorithms like K-means or Median Cut.
📄 License
MIT
👩💻 Author
This module was generated via Rust + wasm-bindgen for modern web-based image processing pipelines.
