zenquant
Color quantization with perceptual masking. Reduces truecolor images to 256-color indexed palettes in OKLab space, using butteraugli-inspired adaptive quantization (AQ) weights to concentrate palette entries where human vision is most sensitive.
Honest comparison: quantette's k-means mode is really good — it leads on per-pixel metrics (highest SSIMULACRA2, lowest DSSIM), though it has strong error diffusion artifacts in the red channel on some images, producing bright red pixels in smooth regions. imagequant is honestly the best-looking to the human eye, even when it's sometimes slightly behind on the numbers. zenquant focuses on doing a solid visual job while prioritizing file size; the size advantage is less obvious when paired with zenpng's aggressive compression, but grows at faster encode speeds or with typical codecs like the png crate.
What it does
Most quantizers treat every pixel equally. zenquant spends palette entries on smooth gradients, skin tones, and other regions where banding is visible — and wastes fewer entries on noisy textures where the eye can't tell the difference.
The pipeline: histogram in OKLab → median cut → k-means refinement with AQ weights → format-aware palette sorting → adaptive Floyd-Steinberg dithering → optional Viterbi DP for run-length optimization.
Usage
Add to your Cargo.toml:
[]
= "0.1"
Quantize an RGB image
use ;
let config = new;
let result = quantize.unwrap;
let palette = result.palette; // &[[u8; 3]] — sRGB
let indices = result.indices; // &[u8] — row-major
Quantize RGBA (GIF with transparency)
use ;
let config = new;
let result = quantize_rgba.unwrap;
// Binary transparency: one palette entry reserved for transparent pixels
if let Some = result.transparent_index
Write an indexed PNG
use ;
let config = new;
let result = quantize.unwrap;
let mut encoder = new;
encoder.set_color;
encoder.set_depth;
encoder.set_palette;
if let Some = result.alpha_table
let mut writer = encoder.write_header.unwrap;
writer.write_image_data.unwrap;
Shared palette for animations
Build one palette from multiple frames, then remap each frame against it:
use ;
let config = new;
// Build shared palette from representative frames
let frames: = frame_data.iter
.map
.collect;
let shared = build_palette_rgba.unwrap;
// Remap each frame
for frame_pixels in &frame_data
For animation encoders (APNG, GIF), you can enforce per-frame quality with with_min_ssim2 on the remap config. Frames that fail the quality floor return QualityNotMet, letting the encoder decide whether to fall back to truecolor for that frame:
let remap_config = new
.with_min_ssim2;
for frame_pixels in &frame_data
Quality targets
Specify quality in SSIMULACRA2 units instead of manually tuning compression knobs. zenquant auto-selects the internal quality preset, dither strength, and run priority to maximize compression while staying above your target.
use ;
// Auto-tune compression: stay above SSIM2 80, compress as hard as possible
let config = new
.with_max_colors
.with_target_ssim2;
let result = quantize.unwrap;
// Quality metrics are computed automatically when a target is set
let ssim2 = result.ssimulacra2_estimate.unwrap; // 0–100, higher = better
let ba = result.butteraugli_estimate.unwrap; // 0+, lower = better
Set a hard quality floor with with_min_ssim2. Returns QuantizeError::QualityNotMet if the result falls below — useful for animation encoders that need to decide per-frame whether to fall back to truecolor:
use ;
let config = new
.with_max_colors
.with_min_ssim2;
match quantize
Quality metrics and with_min_ssim2 enforcement also work on the remap() path, so you get per-frame quality measurement when using shared palettes for animation.
Quality presets
use Quality;
// Fast — ~30ms for 512x512. No AQ masking or k-means refinement.
let config = new.with_quality;
// Balanced — ~60ms. AQ masking + 2 k-means iterations.
let config = new.with_quality;
// Best — ~120ms. AQ masking + 8 k-means iterations + Viterbi DP. (default)
let config = new.with_quality;
When target_ssim2 is set, it overrides the quality preset, run priority, and dither strength with auto-tuned values based on calibrated compression tier data.
Output formats
The OutputFormat controls palette sorting and dither tuning for each format's compression algorithm:
Gif— LZW compression. Delta-minimize palette sort + post-remap frequency reorder. Binary transparency.Png— Deflate + scanline filters. Luminance sort for spatial locality. Full alpha via tRNS.WebpLossless— VP8L delta palette encoding. Delta-minimize sort.
Benchmarks
Averaged over 50 images from three corpora (CID22, CLIC 2025, screenshots). All quantizers configured for 256 colors with default dithering. PNG sizes use aggressive deflate via zenpng. Sorted by DSSIM.
| Quantizer | Butteraugli | SSIMULACRA2 | DSSIM | PNG size | GIF size | ~ms |
|---|---|---|---|---|---|---|
| quantette (k-means) | 3.86 | 83.9 | 0.00050 | 616 KB | 799 KB | 265 |
| imagequant s1 d100 | 4.10 | 82.2 | 0.00056 | 637 KB | 848 KB | 546 |
| imagequant s4 d100 | 4.39 | 81.9 | 0.00057 | 640 KB | 854 KB | 315 |
| zenquant (Best) | 3.17 | 82.9 | 0.00058 | 586 KB | 764 KB | 542 |
| imagequant s1 d50 | 4.15 | 82.0 | 0.00060 | 627 KB | 836 KB | 465 |
| zenquant (Balanced) | 3.21 | 82.9 | 0.00064 | 579 KB | 751 KB | 453 |
| zenquant (Fast) | 3.29 | 82.6 | 0.00069 | 582 KB | 749 KB | 321 |
| quantizr | 4.44 | 79.7 | 0.00098 | 584 KB | 764 KB | 544 |
| color_quant | 8.96 | 72.1 | 0.00141 | 625 KB | 841 KB | 180 |
Lower butteraugli/DSSIM = better. Higher SSIMULACRA2 = better. Smaller file size = better.
Interactive visual comparison (9 quantizers, 50 images) — slider, diff, and zoom views with per-image metrics. Keyboard shortcuts: 1 = original, 2–0 = variants.
zenquant's advantage is most visible on images with smooth gradients and subtle color transitions, where AQ masking prevents banding that other quantizers miss.
Reproduce the benchmarks
The comparison tool generates an interactive HTML report with cached results. Add --benchmark for rigorous sequential timing (min-of-5 runs).
Integration
zenquant is used as the default quantizer in:
- zenpng — PNG/APNG codec (
features = ["quantize"]) - zengif — GIF codec (
features = ["zenquant"]) - zenwebp — WebP codec (
features = ["quantize"])
Features
std(default) — enablesstdon archmage/magetypes for platform-optimized mathjoint— joint deflate+quantization optimization for PNG_dev— exposes internal modules for profiling (not public API)
Always no_std + alloc. Uses core::error::Error (Rust 1.81+). SIMD acceleration (AVX2+FMA, NEON) via archmage with automatic scalar fallback. Fully functional without std.
MSRV
The minimum supported Rust version is 1.92.
AI-Generated Code Notice
Developed with Claude (Anthropic). Not all code manually reviewed. Review critical paths before production use.
License
AGPL-3.0-or-later. Commercial licenses available at imazen.io/pricing.