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): Enablesencode_to_writer(). Everything else works without it.simd(default): SIMD optimizations (SSE2/SSE4.1/AVX2 on x86, SIMD128 on WASM).fast-yuv(default): Optimized YUV conversion via theyuvcrate.pixel-types: Type-safe pixel formats via thergbcrate.
§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 convenience functions:
let webp_data: &[u8] = &[]; // your WebP data
let (pixels, width, height) = zenwebp::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)?;§Safety
This crate uses #![forbid(unsafe_code)] to prevent direct unsafe usage in source.
However, when the simd feature is enabled, 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.
Without the simd feature, this crate contains no unsafe code whatsoever.
Re-exports§
pub use decoder::decode_bgr;pub use decoder::decode_bgr_into;pub use decoder::decode_bgra;pub use decoder::decode_bgra_into;pub use decoder::decode_rgb;pub use decoder::decode_rgb_into;pub use decoder::decode_rgba;pub use decoder::decode_rgba_into;pub use decoder::decode_yuv420;pub use decoder::BitstreamFormat;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::LoopCount;pub use decoder::StreamStatus;pub use decoder::StreamingDecoder;pub use decoder::UpsamplingMethod;pub use decoder::WebPDecoder;pub use decoder::YuvPlanes;pub use encoder::ClassifierDiag;pub use encoder::ContentType;pub use encoder::EncodeError;pub use encoder::EncodeProgress;pub use encoder::EncodeRequest;pub use encoder::EncodeResult;pub use encoder::EncodeStats;pub use encoder::EncoderConfig;pub use encoder::ImageMetadata;pub use encoder::LosslessConfig;pub use encoder::LossyConfig;pub use encoder::NoProgress;pub use encoder::PixelLayout;pub use encoder::Preset;pub use mux::AnimFrame;pub use mux::AnimationConfig;pub use mux::AnimationDecoder;pub use mux::AnimationEncoder;pub use mux::AnimationInfo;pub use mux::BlendMethod;pub use mux::DemuxFrame;pub use mux::DisposeMethod;pub use mux::MuxError;pub use mux::MuxFrame;pub use mux::WebPDemuxer;pub use mux::WebPMux;pub use decoder::vp8;
Modules§
- common
- Common types and utilities shared between encoder and decoder
- decoder
- WebP decoder implementation
- 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.
- pixel
- Type-safe pixel format traits for decoding and encoding. Type-safe pixel format traits for decoding and encoding.
Structs§
- Unstoppable
- A
Stopimplementation that never stops (no cooperative cancellation).
Enums§
- Stop
Reason - Why an operation was stopped.
Traits§
- Stop
- Cooperative cancellation check.