#webp #codec #decoder #encoder #image #env-var

sys no-std libwebp-sys

Bindings to libwebp (bindgen, static linking)

18 releases

Uses old Rust 2015

0.9.5 Feb 19, 2024
0.9.4 Sep 18, 2023
0.9.2 Jul 12, 2023
0.9.0 Mar 20, 2023
0.1.0 Nov 7, 2017

#97 in Images

Download history 5746/week @ 2024-03-03 6770/week @ 2024-03-10 8106/week @ 2024-03-17 6701/week @ 2024-03-24 6904/week @ 2024-03-31 6808/week @ 2024-04-07 7673/week @ 2024-04-14 7437/week @ 2024-04-21 8301/week @ 2024-04-28 9003/week @ 2024-05-05 9249/week @ 2024-05-12 6079/week @ 2024-05-19 7533/week @ 2024-05-26 5159/week @ 2024-06-02 6597/week @ 2024-06-09 7089/week @ 2024-06-16

26,911 downloads per month
Used in 52 crates (2 directly)

MIT license

5MB
70K SLoC

C 64K SLoC // 0.1% comments Rust 3.5K SLoC // 0.0% comments Shell 1K SLoC // 0.2% comments Automake 580 SLoC // 0.0% comments Python 386 SLoC // 0.1% comments C++ 286 SLoC // 0.2% comments M4 168 SLoC // 0.4% comments Batch 76 SLoC Go 20 SLoC // 0.4% comments

Contains (JAR file, 59KB) vendor/gradle/wrapper/gradle-wrapper.jar, (obscure autoconf code, 29KB) vendor/configure.ac, (JAR file, 3KB) vendor/swig/libwebp.jar

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 TARGET_CPU env var to 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 newer CPUs with SIMD support:

[dependencies]
libwebp-sys = { version = "0.9", features = ["avx2", "sse41", "neon"] }

or to require no_std support:

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

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