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

Encoder panic #7

Open
optozorax opened this issue Aug 23, 2021 · 10 comments
Open

Encoder panic #7

optozorax opened this issue Aug 23, 2021 · 10 comments
Assignees
Labels
bug Something isn't working

Comments

@optozorax
Copy link

I'm trying to convert this png to webp, but it panics here:

DynamicImage::ImageLuma8(_) => { unreachable!() }

@jaredforth jaredforth self-assigned this Aug 23, 2021
@jaredforth
Copy link
Owner

Thanks for letting me know! I'll work on tackling this today

@jaredforth jaredforth added the bug Something isn't working label Aug 23, 2021
@optozorax
Copy link
Author

Also, this line always panics when I trying convert webp to webp.

@jaredforth
Copy link
Owner

@optozorax, your png link 404s for me. Could you try attaching it again so I can test locally against that specific image?

@optozorax
Copy link
Author

apollon_1

@jaredforth
Copy link
Owner

Thanks! Also, can you share a minumum reproducible example of how you are trying to use the library with this image?

@optozorax
Copy link
Author

I'm trying to convert this image to webp with quality 75 and fit option with width=1280, height=1280 using zola's function resize_image. I can try to create MRE on weekdays.

@jaredforth
Copy link
Owner

The Encoder will now return a Result instead of calling unreachable!() so you can handle errors in 0.2.0 per the commit 25eb155

Once you have an MRE I will continue to work on implementation so that you can use this crate for your desired functionality.

@antonok-edm
Copy link
Contributor

antonok-edm commented Dec 7, 2021

@jaredforth I've run into this through Zola as well, I've extracted it into the following MRE in case it's useful:

use image::{imageops::FilterType, EncodableLayout};

use std::fs::File;
use std::io::Write;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    const INPUT_PATH: &str = "/path/to/image/on/disk.webp";

    let img = image::open(&INPUT_PATH)?;

    let img = img.resize_exact(384, 384, FilterType::Lanczos3);

    let encoder = webp::Encoder::from_image(&img).unwrap();
    let memory = encoder.encode_lossless();

    let mut f = File::create("target.webp")?;
    f.write_all(memory.as_bytes())?;

    Ok(())
}

In my case, I'm attempting to convert my profile image (which is already a WebP, exported from Gimp) to smaller sizes so I can add it to my website.

Edit: for what it's worth, this is also triggering the ImageLuma8 case. I'd recommend mentioning that somewhere in the Err that's returned from Encoder::from_image; right now it's impossible to figure out what isn't implemented without using a modified webp fork.

@antonok-edm
Copy link
Contributor

I dug into this a little bit further. Prior to image-rs/image#1624 (merged recently but still unreleased), the Supported Image Formats section on the image README was as follows:

Format Decoding Encoding
... ... ...
WebP Lossy(Rgb only) No
... ... ...

As it turns out, patching out the image dependency to use the latest master branch allows my example to work. The issue is purely with how image is decoding the input file.

As for the original example provided by @optozorax, it looks like the original source PNG is encoded as grayscale, so it makes sense that it's being passed in as a Luma8 image.

It looks like the WebP format has no special encoding for grayscale images, so I think the best approach here is to just use image's built-in support for converting it to a corresponding RGB image.

@jaredforth
Copy link
Owner

@antonok-edm I am just reading through this thread again now. I have improved the error messages so that it is obvious what case is triggering the error. 6e0ca1a

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants