webpx
Complete WebP encoding and decoding for Rust - safe bindings to Google's libwebp with support for static images, animations, ICC profiles, streaming, and no_std.
Why webpx?
- Full libwebp features - Lossy, lossless, animation, alpha, metadata
- Safe & ergonomic API - Builder patterns, strong types, comprehensive error handling
- High performance - Zero-copy where possible, direct FFI to optimized C code
- Flexible - Works with
no_std, supports WebAssembly via emscripten - Migration-friendly - Compatibility shims for
webpandwebp-animationcrates
Quick Start
[]
= "0.1"
use ;
// Encode RGBA pixels to WebP
let webp = encode_rgba?;
// Decode WebP back to RGBA
let = decode_rgba?;
Features at a Glance
| Feature | Description |
|---|---|
| Lossy Encoding | VP8-based compression with quality 0-100 |
| Lossless Encoding | Exact pixel preservation |
| Alpha Channel | Full transparency support with separate quality control |
| Animation | Multi-frame WebP with timing control |
| ICC Profiles | Embed/extract color profiles |
| EXIF/XMP | Preserve camera metadata |
| Streaming | Decode as data arrives |
| Cropping/Scaling | Decode to any size |
| YUV Support | Direct YUV420 input/output |
| Content Presets | Optimized settings for photos, drawings, icons, text |
| Cancellation | Cooperative cancellation via enough crate |
Examples
Basic Encoding
use ;
// Lossy encoding (quality 0-100)
let webp = encode_rgba?;
// Lossless encoding (exact pixels)
let webp = encode_lossless?;
// RGB without alpha
let webp = encode_rgb?;
Builder API with Options
use ;
let webp = new
.preset // Content-aware optimization
.quality // Higher quality
.method // Better compression (slower)
.alpha_quality // High-quality alpha
.sharp_yuv // Better color accuracy
.encode?;
Advanced Configuration
use EncoderConfig;
// Maximum compression (slow but smallest files)
let config = max_compression;
let webp = config.encode_rgba?;
// Maximum quality lossless
let config = max_compression_lossless;
let webp = config.encode_rgba?;
// Fine-grained control
let config = new
.quality
.method
.filter_strength
.sns_strength
.segments
.pass
.preprocessing;
let = config.encode_rgba_with_stats?;
println!;
Decoding with Processing
use Decoder;
let decoder = new?;
// Get image info without decoding
let info = decoder.info;
println!;
// Decode with cropping and scaling
let = decoder
.crop // Extract region
.scale // Resize
.decode_rgba_raw?;
Animation
use ;
// Create animated WebP
let mut encoder = new?;
encoder.set_quality;
encoder.set_lossless;
encoder.add_frame?; // Start at 0ms
encoder.add_frame?; // Show at 100ms
encoder.add_frame?; // Show at 200ms
let webp = encoder.finish?; // Total duration
// Decode animation
let mut decoder = new?;
let info = decoder.info;
println!;
// Iterate frames
while let Some = decoder.next_frame?
// Or get all at once
decoder.reset;
let frames = decoder.decode_all?;
ICC Profiles & Metadata
use ;
// Embed ICC profile
let webp_with_icc = embed_icc?;
// Extract ICC profile
if let Some = get_icc_profile?
// EXIF data
let webp_with_exif = embed_exif?;
if let Some = get_exif?
Streaming Decode
use ;
let mut decoder = new?;
// Feed data as it arrives
for chunk in network_stream
let = decoder.finish?;
Cooperative Cancellation
Encoding can be cancelled cooperatively using the enough crate:
use ;
use ;
use Arc;
// Create a cancellation flag
let cancelled = new;
let flag = cancelled.clone;
// Custom Stop implementation
;
// In another thread: flag.store(true, Ordering::Relaxed);
match encode_rgba
For ready-to-use cancellation primitives (timeouts, channels, etc.), see the almost-enough crate.
Feature Flags
| Feature | Default | Description |
|---|---|---|
decode |
Yes | WebP decoding |
encode |
Yes | WebP encoding |
std |
Yes | Use std (disable for no_std + alloc) |
animation |
No | Animated WebP support |
icc |
No | ICC/EXIF/XMP metadata |
streaming |
No | Incremental decode/encode |
# All features
= { = "0.1", = ["animation", "icc", "streaming"] }
# no_std
= { = "0.1", = false, = ["decode", "encode"] }
Content Presets
Choose a preset to optimize for your content type:
| Preset | Best For | Characteristics |
|---|---|---|
Default |
General use | Balanced settings |
Photo |
Photographs | Better color, outdoor scenes |
Picture |
Indoor/portraits | Skin tone optimization |
Drawing |
Line art | High contrast, sharp edges |
Icon |
Small images | Color preservation |
Text |
Screenshots | Crisp text rendering |
use ;
let webp = new
.preset
.encode?;
Platform Support
| Platform | Status |
|---|---|
| Linux x64/ARM64 | ✅ Full support |
| macOS x64/ARM64 | ✅ Full support |
| Windows x64/ARM64 | ✅ Full support |
| WebAssembly (emscripten) | ✅ Supported |
| WebAssembly (wasm32-unknown-unknown) | ❌ Not supported* |
*libwebp requires C compilation. For pure-Rust WASM, see image-webp (lossless only).
Building for WebAssembly
# Install emscripten
&& &&
# Add target and build
Migration from Other Crates
From webp crate
// Before
use ;
// After - use compat shim
use ;
// API is compatible, just change the import
From webp-animation crate
// Before
use ;
// After - use compat shim
use ;
// Uses finalize() instead of finish() to match original API
Performance Tips
- Use appropriate
method- Higher values (4-6) give better compression but are slower - Choose the right preset - Presets tune internal parameters for content type
- Consider
sharp_yuv- Better color accuracy at slight speed cost - Batch frames - For animations, encode multiple frames before finalizing
- Pre-allocate buffers - Use
StreamingDecoder::with_buffer()to avoid allocations
Minimum Supported Rust Version
Rust 1.80 or later.
License
Licensed under either of:
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Contributing
Contributions welcome! Please open issues and pull requests on GitHub.
AI-Generated Code Notice
This crate was developed with assistance from Claude (Anthropic). Not all code has been manually reviewed. Please review critical paths before production use.