#image #codec #decoder #encoder #web-p

sys libwebp-sys

Bindings to libwebp (bindgen, static linking)

4 releases (2 breaking)

Uses old Rust 2015

0.3.2 Jun 1, 2021
0.3.0 Mar 31, 2020
0.2.0 Nov 13, 2018
0.1.0 Nov 7, 2017

#17 in Video

Download history 187/week @ 2021-02-25 241/week @ 2021-03-04 249/week @ 2021-03-11 205/week @ 2021-03-18 213/week @ 2021-03-25 214/week @ 2021-04-01 198/week @ 2021-04-08 192/week @ 2021-04-15 197/week @ 2021-04-22 163/week @ 2021-04-29 273/week @ 2021-05-06 300/week @ 2021-05-13 188/week @ 2021-05-20 195/week @ 2021-05-27 255/week @ 2021-06-03 213/week @ 2021-06-10

919 downloads per month
Used in 5 crates (via webp)

MIT license

5MB
65K SLoC

C 63K SLoC // 0.1% comments Rust 1.5K SLoC // 0.0% comments Automake 540 SLoC // 0.0% comments Shell 312 SLoC // 0.1% comments C++ 262 SLoC // 0.2% comments Python 213 SLoC // 0.0% comments Batch 76 SLoC Go 20 SLoC // 0.4% comments

libwebp-sys

bindgen'd FFI bindings to libwebp.

libwebp itself is built with cmake and linked statically.

Add manul ffi.rs

add ffi.rs that generated by bindgen manually to avoid install llvm and clang in server

Usage

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

[dependencies]
libwebp-sys = "0.3"

Example

Encode:

pub fn encode_webp(input_image: &[u8], width: u32, height: u32, quality: i32) -> Result<Vec<u8>> {
    unsafe {
	    let mut out_buf = Box::into_raw(Box::new(0u8)) as *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 as *mut _);
	    Ok(Vec::from_raw_parts(out_buf, len as usize, len as usize))
    }
}

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);
	}
	let len = width * height * 4;
	Ok(Vec::from_raw_parts(out_buf, len, len))
}

No runtime deps