Skip to main content

Crate webpx

Crate webpx 

Source
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

FeatureDefaultDescription
decodeYesDecoding support
encodeYesEncoding support
stdYesStandard library (disable for no_std)
animationNoAnimated WebP support
iccNoICC/EXIF/XMP metadata
streamingNoIncremental 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.
DecoderConfig
Decoder configuration.
EncodeStats
Encoding statistics returned after compression.
Encoder
WebP encoder with full configuration options.
EncoderConfig
WebP encoder configuration. Dimension-independent, reusable across images.
ImageInfo
Information about a WebP image.
Unstoppable
A Stop implementation that never stops (no cooperative cancellation).
WebPData
Owned WebP data that wraps libwebp’s native memory allocation.
YuvPlanes
YUV plane data for planar formats.
YuvPlanesRef
Reference to YUV planes (borrowed version).

Enums§

AlphaFilter
Alpha channel filtering method.
BitstreamFormat
Bitstream format.
ColorMode
Color mode for output.
ImageHint
Hint about image content for encoder optimization.
Preset
Content-aware encoding presets.
StopReason
Why an operation was stopped.

Traits§

ResultAtExt
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.