Expand description
§webpx
Complete WebP encoding and decoding with ICC profiles, streaming, and animation support.
webpx provides safe Rust bindings to Google’s libwebp library, offering:
- Static Images: Encode/decode RGB, RGBA, and YUV formats with lossy or lossless compression
- Animations: Create and decode animated WebP files frame-by-frame or in batch
- Metadata: Embed and extract ICC color profiles, EXIF, and XMP data
- Streaming: Incremental decoding for large files or network streams
- Presets: Content-aware optimization (Photo, Picture, Drawing, Icon, Text)
- Advanced Config: Full control over compression parameters, alpha handling, and more
§Quick Start
use webpx::{Encoder, Unstoppable};
// Create a small 2x2 RGBA image (red, green, blue, white)
let rgba_data: Vec<u8> = vec![
255, 0, 0, 255, // red
0, 255, 0, 255, // green
0, 0, 255, 255, // blue
255, 255, 255, 255 // white
];
// Encode to WebP (lossy, quality 85)
let webp_bytes = Encoder::new_rgba(&rgba_data, 2, 2)
.quality(85.0)
.encode(Unstoppable)?;
// Decode back to RGBA
let (pixels, width, height) = webpx::decode_rgba(&webp_bytes)?;
assert_eq!((width, height), (2, 2));§Builder API
For more control, use the builder pattern:
use webpx::{Encoder, Preset, Unstoppable};
let rgba_data: &[u8] = &[0u8; 640 * 480 * 4]; // placeholder
let webp_bytes = Encoder::new_rgba(rgba_data, 640, 480)
.preset(Preset::Photo) // Optimize for photos
.quality(85.0) // 0-100, higher = better quality
.method(4) // 0-6, higher = slower but smaller
.encode(Unstoppable)?;§Feature Flags
| Feature | Default | Description |
|---|---|---|
decode | Yes | Decoding support |
encode | Yes | Encoding support |
std | Yes | Standard library (disable for no_std) |
animation | No | Animated WebP support |
icc | No | ICC/EXIF/XMP metadata |
streaming | No | Incremental processing |
§no_std Support
This crate supports no_std with alloc. Disable the std feature:
webpx = { version = "0.1", default-features = false, features = ["decode", "encode"] }§Compatibility Shims
For easy migration from other WebP crates, see compat::webp and compat::webp_animation.
§Error Handling
All functions return Result<T, At<Error>> where At provides lightweight error
location tracking. See the error module documentation for how to:
- Propagate traces with
.at() - Add your crate’s info to traces with
at_crate! - Convert to plain errors with
.into_inner()
Re-exports§
pub use error::DecodingError;pub use error::EncodingError;pub use error::Error;pub use error::MuxError;pub use error::Result;
Modules§
- compat
- Compatibility shims for migration from other WebP crates.
- error
- Error types for webpx operations.
- heuristics
- Resource estimation heuristics for encoding and decoding operations.
Macros§
- at
- Start tracing an error with crate metadata for repository links.
- at_
crate - Add crate boundary marker to a Result with an
At<E>error.
Structs§
- At
- An error with location tracking - wraps any error type.
- Decoder
- WebP decoder with advanced options.
- Decoder
Config - Decoder configuration.
- Encode
Stats - Encoding statistics returned after compression.
- Encoder
- WebP encoder with full configuration options.
- Encoder
Config - WebP encoder configuration. Dimension-independent, reusable across images.
- Image
Info - Information about a WebP image.
- Unstoppable
- A
Stopimplementation that never stops (no cooperative cancellation). - WebP
Data - Owned WebP data that wraps libwebp’s native memory allocation.
- YuvPlanes
- YUV plane data for planar formats.
- YuvPlanes
Ref - Reference to YUV planes (borrowed version).
Enums§
- Alpha
Filter - Alpha channel filtering method.
- Bitstream
Format - Bitstream format.
- Color
Mode - Color mode for output.
- Image
Hint - Hint about image content for encoder optimization.
- Preset
- Content-aware encoding presets.
- Stop
Reason - Why an operation was stopped.
Traits§
- Result
AtExt - Extension trait for adding location tracking to
Result<T, At<E>>. - Stop
- Cooperative cancellation check.
Functions§
- at
- Wrap any value in
At<E>and capture the caller’s location. - decode
- Decode WebP data to typed pixels.
- decode_
append - Decode WebP data, appending typed pixels to an existing Vec.
- decode_
bgr - Decode WebP data to BGR pixels (no alpha).
- decode_
bgr_ into - Decode WebP data directly into a pre-allocated BGR buffer (zero-copy).
- decode_
bgra - Decode WebP data to BGRA pixels.
- decode_
bgra_ into - Decode WebP data directly into a pre-allocated BGRA buffer (zero-copy).
- decode_
into - Decode WebP data directly into a typed pixel slice.
- decode_
rgb - Decode WebP data to RGB pixels (no alpha).
- decode_
rgb_ into - Decode WebP data directly into a pre-allocated RGB buffer (zero-copy).
- decode_
rgba - Decode WebP data to RGBA pixels.
- decode_
rgba_ into - Decode WebP data directly into a pre-allocated RGBA buffer (zero-copy).
- decode_
to_ img - Decode WebP data to an imgref image.
- decode_
yuv - Decode WebP data to YUV planes.
- version
- Library version information.