A high-level Rust wrapper for decoding and encoding WebP animations
See examples/encode_animation.rs
for source code of encoding the above image
Underlying WebP format processing is handled by C-based libwebp library, which is interfaced through Rust libwebp-sys2 crate.
Functional Goals:
* Easy-to-use API that looks like Rust
* Enable decoding and encoding of WebP streams
* All configuration flags provided by libwebp
should be usable
Non-functional Goals:
* High performance (approach libwebp
performance without large overhead)
* Write compherensive test cases, and test by automation
* Ensure safety (no memory leaks or UB). Fuzz the API's. Safe to use for end users
Non-goals * Provide other WebP/libwebp -related functionality (such as image en/decoding or muxing). For this functionality, see e.g. libwebp-image or webp
Will take a webp buffer, and try to decode it to frame(s)
```rust use webp_animation::Decoder;
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)); assert_eq!(frame.data().len(), 400 * 400 * 4); // w * h * rgba
// if feature `image` is enabled (not by default), one can convert data to [`Image::ImageBuffer`]
assert_eq!(frame.into_image().unwrap().dimensions(), (400, 400));
} ```
It is also possible to supply more decoding options through Decoder::new_with_options
.
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::{Encoder, Frame};
// 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 let mut encoder = Encoder::new(dimensions).unwrap();
// insert frames to specific (increasing) timestamps for i in 0..5 { let rgbadata = if i % 2 == 0 { &brightframe } else { &darkframe }; let frametimestamp = i * 170;
encoder.addframe(rgbadata, frame_timestamp).unwrap(); }
// get encoded webp data let finaltimestamp = 1000; let webpdata = encoder.finalize(finaltimestamp).unwrap(); std::fs::write("myanimation.webp", webpdata).unwrap(); ```
TODO: add about encoder options
Keep up with upstream libwebp
changes.
Possibly provide a compherensive CLI for working with WebP animations in future (conversions, optimizations, etc.)
Licensed under either of * Apache License, Version 2.0 or * MIT license
at your option.
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.