Convert a .webp to .png

Hi,

does anyone have an idea how to convert a .webp to a .png?
I found this crate https://crates.io/crates/image but I am not sure if I can convert pictures with it.

image has a partial webp implementation in pure Rust but it only supports loading of grayscale part of lossy webp.
I've made a binding GitHub - qnighy/libwebp-rs: Rust binding to libwebp
to the C library for my purpose but it doesn't have a good documentation and has unnecessary dependencies. I'm going to remake it at GitHub - qnighy/webp-rs: Rust libwebp bindings but the project is currently stale. Nevertheless it will suffice your need to decode webp images into typical RGBA representations.

sounds good. do you maybe have an example for me? Sorry I am not very fit at Rust at the moment...

you described how to unwrap the picture if you have the correct representation

let data = b"\
    RIFFV\x00\x00\x00WEBPVP8\x20\
    J\x00\x00\x00\xD0\x01\x00\x9D\x01*\x03\x00\x02\x00\x02\x00\
    4%\xA8\x02t\x01\x0E\xFE\x03\x8E\x00\x00\xFE\xAD\xFF\xF1\
    \x5C\xB4\xF8\xED\xFF\xF0\xC0\xBA\xBF\x93\x05\xEA\x0C\x9F\x93?\
    \xE8\xC0\xBF?\xFF\xA9\xBF\xFF${\xCB\xFFF\x05\xF9\xFF\
    \xFDM\xFE0\xE5\x86\xAA\x071#o\x00\x00\x00";

How do I get that if I already have a picture on my laptop?

I don't know if its correct what I have done so far?

extern crate libwebp;
extern crate image;

use ::std::{*, io::Write, fs::OpenOptions,};
use std::io::Read;
use libwebp::*;
use std::fs::File;
use std::path::Path;
use std::fmt::Debug;

fn main() -> io::Result<()>
{
    let src = "/home/andy/Downloads/8Z.ktnc9tqX.mri";
    let dst = "/home/andy/Downloads/8Z.ktnc9tqX";

    let mut data = fs::read(src)?;
    let n = data.len() + 7;
    let header = [
        82, 73, 70, 70, n as u8, (n >> 8) as u8, (n >> 16) as u8, (n >> 24) as u8, 87, 69, 66, 80, 86, 80, 56,
    ];

    data.iter_mut().for_each(|at_x| *at_x ^= 101); // more efficient

    let newdst = format!("{}.webp", dst);
    let mut f =
        OpenOptions::new()
            .create(true)
            .write(true)
            .truncate(true)
            .open(&newdst)?
        ;
    f.write_all(&header)?;
    f.write_all(&data)?;

    let mut data = File::open(newdst).unwrap();

    let size = data.metadata().unwrap().len() as usize;

    let mut buf = vec![0u8; size];
    data.read(&mut buf).unwrap();

    let (width, height, d) = WebPDecodeRGBA(&buf).unwrap();

    println!("{} {}", width, height);

    //image::save_buffer("image.png", &d, width, height, image::RGB(8)).unwrap();

    let format = image::guess_format(&d);

    println!("{:?}", format);


    Ok(())
}

Pretty sure I did something wrong because if I run the image::save_buffer line I get the following error:

thread 'main' panicked at 'called Result::unwrap() on an Err value: Custom { kind: Other, error: StringError("wrong data size, expected 3544800 got 4726400") }', src/libcore/result.rs:999:5

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.