-
Notifications
You must be signed in to change notification settings - Fork 622
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Suggestion] WebP Encoding Support #582
Comments
For reference purposes (in case anyone wants to know how to encode WebP in the mean time), here's how to encode an extern crate libc;
use libc::{c_int, c_float, size_t};
use std::io::{Write, BufWriter};
use std::fs::File;
use std::path::Path;
#[link(name = "webp")]
extern {
// Here's the C lib's function def:
// size_t WebPEncodeRGB(const uint8_t* rgb, int width, int height, int stride, float quality_factor, uint8_t** output);
// size_t WebPEncodeLosslessRGB(const uint8_t* rgb, int width, int height, int stride, uint8_t** output);
fn WebPEncodeRGB(rgb: *const u8, width: c_int, height: c_int, stride: c_int, quality_factor: c_float, output: *mut *mut u8) -> size_t;
fn WebPEncodeLosslessRGB(rgb: *const u8, width: c_int, height: c_int, stride: c_int, output: *mut *mut u8) -> size_t;
fn WebPFree(ptr: *const u8);
}
let filetype = "webp";
let ref path_str = format!("/tmp/test.{}", filetype);
let path = &Path::new(path_str);
// Pretend this screenshot() function returns ImageBuffer<Rgb<u8>, Vec<u8>>:
let img = screenshot();
let stride = width * 3;
let lossless = false; // Set to true for lossless (Warning: CPU intensive/slow)
let quality: c_float = 75.0; // Quality level of the WebP image
let mut output: *mut u8 = std::ptr::null_mut();
let raw = img.clone().into_raw();
unsafe {
let length: usize;
if lossless {
length = WebPEncodeLosslessRGB(raw.as_ptr(), width as c_int, height as c_int, stride as c_int, &mut output);
} else {
length = WebPEncodeRGB(raw.as_ptr(), width as c_int, height as c_int, stride as c_int, quality, &mut output);
}
let mut fout = BufWriter::new(File::create(&path).unwrap());
let slice = std::slice::from_raw_parts(output, length);
let _ = fout.write_all(slice).unwrap();
let _ = fout.flush();
let _ = WebPFree(output); // IMPORTANT: Make sure we free that memory
} |
Any progress on this? |
Hm, I don't think there's been any progress on a pure Rust WebP encoder, but the webp crate linked above looks like a viable option if you want a safe wrapper for the FFI bindings in libwebp-sys. |
Awesome 🎉 |
The crate currently supports WebP decoding but it would be nice if it also supported encoding as well. I'd definitely take advantage of it in one of my forthcoming projects.
The text was updated successfully, but these errors were encountered: