Skip to content
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

Closed
liftoff opened this issue Oct 25, 2016 · 5 comments · Fixed by #1793
Closed

[Suggestion] WebP Encoding Support #582

liftoff opened this issue Oct 25, 2016 · 5 comments · Fixed by #1793
Labels
hard kind: enhancement topic: formats Towards better encoding format coverage

Comments

@liftoff
Copy link

liftoff commented Oct 25, 2016

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.

@liftoff
Copy link
Author

liftoff commented Oct 28, 2016

For reference purposes (in case anyone wants to know how to encode WebP in the mean time), here's how to encode an ImageBuffer to WebP via libwebp/libc:

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
}

@emirror-de
Copy link

emirror-de commented Feb 13, 2021

Hi there,

the webp crate contains the possibility to encode. It already uses DynamicImage as input struct. Maybe it would be an idea to integrate this (or take this implementation as reference) to support enconding?
There exists also the libwebp crate that contains the bindings.

@HeroicKatora HeroicKatora added the topic: formats Towards better encoding format coverage label Mar 12, 2021
@kruserr
Copy link

kruserr commented May 11, 2021

Any progress on this?
Would love to see this added!

@fintelia
Copy link
Contributor

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.

@Sparkenstein
Copy link

Sparkenstein commented Sep 25, 2022

Awesome 🎉
Thanks for the encoding support, when can we expect next release?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
hard kind: enhancement topic: formats Towards better encoding format coverage
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants