webp-animation   ![Build Status] ![Latest Version] ![Docs Version] ![Lines of Code]

A high-level Rust wrapper for decoding and encoding WebP animations

Example

See examples/encode_animation.rs for source code of encoding the above image - example converted to gif for all-browser support, see the example.webp file

Underlying WebP format processing is handled by C-based libwebp library, which is interfaced through Rust libwebp-sys2 crate.

Functional Goals:

Non-functional Goals:

Non-goals

Examples

Decoding

Will take a webp buffer, and try to decode it to frame(s)

```rust use webp_animation::prelude::*;

let buffer = std::fs::read("./data/animated.webp").unwrap(); let decoder = Decoder::new(&buffer).unwrap();

for frame in decoder.intoiter() { asserteq!(frame.dimensions(), (400, 400));

// w * h * rgba assert_eq!(frame.data().len(), 400 * 400 * 4);

// if feature image is enabled (not by default), // one can convert data to [Image::ImageBuffer] asserteq!( frame.intoimage().unwrap().dimensions(), (400, 400) ); } ```

It is also possible to supply more decoding options through Decoder::new_with_options.

Encoding

Will take n frames as an input. WebP binary data is output at the end (wrapped into WebPData which acts as a &[u8])

```rust use webp_animation::prelude::*;

// setup let dimensions = (64, 32); let brightframe = [255, 255, 255, 255].repeat(64 * 32); let darkframe = [0, 0, 0, 255].repeat(64 * 32);

// init encoder. uses by default lossless encoding, // for other alternatives see documentation about // new_with_options let mut encoder = Encoder::new(dimensions).unwrap();

// insert frames to specific (increasing) timestamps for frameidx in 0..5 { let rgbadata = if frameidx % 2 == 0 { &brightframe } else { &dark_frame };

// (presentation) timestamp of the frame, should be in increasing order. represented in milliseconds let frametimestampms = frame_idx * 170;

encoder.addframe(rgbadata, frametimestampms).unwrap(); }

// final timestamp in milliseconds, until to the last frame is shown let finaltimestampms = 1_000;

// get encoded webp data let webpdata = encoder.finalize(finaltimestampms).unwrap(); std::fs::write("myanimation.webp", webp_data).unwrap(); ```

See the documentation for other encoding options, e.g. for lossy encoding. For tuning the options, use the Encoder::new_with_options method.

Future plans

Keep up with upstream libwebp changes.

Possibly provide a compherensive CLI for working with WebP animations in future (conversions, optimizations, etc.)

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the software by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.