NoXF/libwebp-sys

bindgen'd FFI bindings to libwebp

Rust

48

103 commits

updated Apr 30, 2026

See the code

README

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.13"

or to require no_std support:

libwebp-sys = { version = "0.12", 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())
}
libwebp
rust

Project health

Overall

2.6/10

Maintained

0/10

Code review

2/10
OpenSSF Scorecard

crates.io

Latest release

0.14.4 · Apr 29, 2026

Releases this year

5

Significant stargazers

Yisi

2,419 followers · starred Apr 2022

alula

558 followers · starred Dec 2024

Chen Yangjian

422 followers · starred Apr 2022

NoXF/libwebp-sys

bindgen'd FFI bindings to libwebp

Rust

48

103 commits

updated Apr 30, 2026

See the code

README

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.13"

or to require no_std support:

libwebp-sys = { version = "0.12", 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())
}
libwebp
rust

Project health

Overall

2.6/10

Maintained

0/10

Code review

2/10
OpenSSF Scorecard

crates.io

Latest release

0.14.4 · Apr 29, 2026

Releases this year

5

Significant stargazers

Yisi

2,419 followers · starred Apr 2022

alula

558 followers · starred Dec 2024

Chen Yangjian

422 followers · starred Apr 2022