#codec #webp #image #encoder #decoder

sys no-std libwebp-sys

Bindings to libwebp (bindgen, static linking)

22 releases

0.11.0 Oct 29, 2024
0.9.6 Sep 2, 2024
0.9.5 Feb 19, 2024
0.9.4 Sep 18, 2023
0.1.0 Nov 7, 2017

#37 in Images

Download history 7837/week @ 2024-07-29 9955/week @ 2024-08-05 10429/week @ 2024-08-12 7121/week @ 2024-08-19 5758/week @ 2024-08-26 8525/week @ 2024-09-02 11469/week @ 2024-09-09 12957/week @ 2024-09-16 15464/week @ 2024-09-23 13867/week @ 2024-09-30 11952/week @ 2024-10-07 17516/week @ 2024-10-14 17796/week @ 2024-10-21 18968/week @ 2024-10-28 20789/week @ 2024-11-04 19737/week @ 2024-11-11

79,062 downloads per month
Used in 72 crates (4 directly)

MIT license

2.5MB
52K SLoC

C 48K SLoC // 0.1% comments Rust 3K SLoC // 0.0% comments Automake 385 SLoC // 0.0% comments

libwebp-sys

bindgen'd FFI bindings to libwebp.

libwebp is built with the cc crate. It needs a C compiler, but cmake is not used.

Set RUSTFLAGS="-Ctarget-cpu=native" or your desired CPU architecture to optimize the C code for it.

Usage

Add the following to the Cargo.toml in your project:

[dependencies]
libwebp-sys = "0.9"

or to require no_std support:

libwebp-sys = { version = "0.9", default-features = false, features = ["parallel"] }

The neon, sse41 and avx2 feature flags can be set to force support for Neon, SSE 4.1 and AVX2 respectively, but this is usually unnecessary as it can be set through -Ctarget-feature (e.g. RUSTFLAGS="-Ctarget-feature=avx2") as well.

Examples

Encode

pub fn encode_webp(input_image: &[u8], width: u32, height: u32, quality: i32) -> Result<Vec<u8>> {
    unsafe {
	    let mut out_buf = std::ptr::null_mut();
	    let stride = width as i32 * 4;
	    let len = WebPEncodeRGBA(input_image.as_ptr(), width as i32, height as i32, stride, quality as f32, &mut out_buf);
	    Ok(std::slice::from_raw_parts(out_buf, len as usize).into())
    }
}

Decode

pub fn decode_webp(buf: &[u8]) -> Result<Vec<u8>> {
	let mut width = 0;
	let mut height = 0;
	let len = buf.len();
	unsafe {
		WebPGetInfo(buf.as_ptr(), len, &mut width, &mut height);
		let out_buf = WebPDecodeRGBA(buf.as_ptr(), len, &mut width, &mut height);
	}
	Ok(std::slice::::from_raw_parts(out_buf, width * height * 4).into())
}

No runtime deps