Skip to main content

Crate zenwebp

Crate zenwebp 

Source
Expand description

Decoding and Encoding of WebP Images

Copyright (C) 2025 Imazen LLC

This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

For commercial licensing inquiries: support@imazen.io

This crate provides both encoding and decoding of WebP images.

§Features

  • std (default): Enables encode_to_writer(). Everything else works without it.
  • pixel-types: Type-safe pixel formats via the rgb crate.

§no_std Support

Both encoding and decoding work in no_std environments (requires alloc):

[dependencies]
zenwebp = { version = "...", default-features = false }

Only EncodeRequest::encode_to requires std (for std::io::Write).

§Encoding

Use LossyConfig or LosslessConfig with EncodeRequest:

use zenwebp::{EncodeRequest, LossyConfig, PixelLayout};

let config = LossyConfig::new().with_quality(85.0).with_method(4);
let rgba_data = vec![255u8; 4 * 4 * 4];
let webp = EncodeRequest::lossy(&config, &rgba_data, PixelLayout::Rgba8, 4, 4)
    .encode()?;

§Decoding

Use the oneshot convenience functions:

let webp_data: &[u8] = &[]; // your WebP data
let (pixels, width, height) = zenwebp::oneshot::decode_rgba(webp_data)?;

Or WebPDecoder for two-phase decoding (inspect headers before allocating):

use zenwebp::WebPDecoder;

let webp_data: &[u8] = &[]; // your WebP data
let mut decoder = WebPDecoder::build(webp_data)?;
let info = decoder.info();
println!("{}x{}, alpha={}", info.width, info.height, info.has_alpha);

let mut output = vec![0u8; decoder.output_buffer_size().unwrap()];
decoder.read_image(&mut output)?;

§ICC Color Profiles

WebP supports embedded ICC profiles via the ICCP chunk (VP8X extended format). zenwebp preserves ICC profiles through encode and decode but does not apply color management — pixels are returned in whatever color space they were encoded in. This matches libwebp’s behavior.

Decoding: Use ImageInfo::icc_profile to extract the ICC profile after probing headers. Pass it to your color management library (e.g., lcms2) to convert pixels to your target color space.

let webp_data: &[u8] = &[];
let info = zenwebp::ImageInfo::from_webp(webp_data)?;
if let Some(icc) = &info.icc_profile {
    // Pass icc bytes to your CMS for color conversion
}

Encoding: Embed an ICC profile with EncodeRequest::with_icc_profile():

use zenwebp::{EncodeRequest, LossyConfig, PixelLayout};
let webp = EncodeRequest::lossy(&LossyConfig::new(), &rgba_data, PixelLayout::Rgba8, 4, 4)
    .with_icc_profile(icc_bytes)
    .encode()?;

Post-hoc: The metadata module can extract, embed, or remove ICC profiles from already-encoded WebP data without re-encoding pixels.

§Safety

This crate uses #![forbid(unsafe_code)] to prevent direct unsafe usage in source. We rely on the archmage crate for safe SIMD intrinsics. The #[arcane] proc macro generates unsafe blocks internally (which bypass the forbid lint due to proc-macro span handling). The soundness of our SIMD code depends on archmage’s token-based safety model being correct.

Re-exports§

pub use decoder::DecodeConfig;
pub use decoder::DecodeError;
pub use decoder::DecodeRequest;
pub use decoder::DecodeResult;
pub use decoder::ImageInfo;
pub use decoder::Limits;
pub use decoder::StreamingDecoder;
pub use decoder::WebPDecoder;
pub use encoder::EncodeError;
pub use encoder::EncodeRequest;
pub use encoder::EncodeResult;
pub use encoder::EncoderConfig;
pub use encoder::ImageMetadata;
pub use encoder::LosslessConfig;
pub use encoder::LossyConfig;
pub use encoder::PixelLayout;
pub use encoder::Preset;

Modules§

decoder
WebP decoder implementation
detect
Encoder detection and quality estimation from WebP file headers. WebP encoder detection and quality estimation.
encoder
WebP encoder implementation.
heuristics
Resource estimation heuristics for encoding and decoding operations. Resource estimation heuristics for encoding and decoding operations.
metadata
Standalone metadata convenience functions for already-encoded WebP data.
mux
WebP mux/demux and animation encoding. WebP mux/demux and animation encoding.
oneshot
One-shot decode convenience functions (decode_rgba, decode_rgb, etc.). One-shot decode convenience functions.
pixel
Type-safe pixel format traits for decoding and encoding. Type-safe pixel format traits for decoding and encoding.

Structs§

SharpYuvConfig
Re-export sharp YUV configuration from zenyuv. Configuration for Sharp YUV chroma optimization.

Enums§

Orientation
Image orientation — the 8-element D4 dihedral group.