Skip to content
bindgen'd FFI bindings to libwebp
Rust C++
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src
tests
vendor @ ac54331
.gitignore
.gitmodules
Cargo.toml
README.md
build.rs
wrapper.h

README.md

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

and import useing extern crate:

extern crate libwebp_sys;

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))
}
You can’t perform that action at this time.