image-rs / jpeg-decoder

JPEG decoder written in Rust
Apache License 2.0
147 stars 88 forks source link

Failed to open with assertion failed: `(left == right)` #148

Closed ArturKovacs closed 4 years ago

ArturKovacs commented 4 years ago

This issue was originally posted to https://github.com/ArturKovacs/emulsion/issues/6

When opening the following image with image::open() the error seen below is returned.

azerty_mac.zip

assertion failed: `(left == right)`
  left: `224000`,
 right: `230400`: destination and source slices have different lengths
lovasoa commented 4 years ago

This is a 800x280 8-bit grayscale image, so jpeg-decoder should output a 224000-elements u8 array, but it outputs a 230400-elements array. The crash happens in image-rs in :

https://github.com/image-rs/image/blob/master/src/jpeg/decoder.rs#L76

ArturKovacs commented 4 years ago

The following code was tested with the same image with jpeg-decoder = "0.1.19" and fails at save_buffer with

IoError(Custom { kind: Other, error: "wrong data size, expected 672000 got 224000" })
use image;
use jpeg_decoder;

fn main() {
    let args: Vec<_> = std::env::args().collect();
    if let Some(path) = args.get(1) {
        let mut decoder = jpeg_decoder::Decoder::new(std::fs::File::open(path).unwrap());
        let image = decoder.decode().unwrap();
        let meta = decoder.info().unwrap();
        image::save_buffer("cool.png", &image, meta.width as u32, meta.height as u32, image::ColorType::Rgb8).unwrap();
        println!("Image loaded without errors.");
    } else {
        println!("Didn't specify file path. Correct usage:\n{} path/to/image/file", args.get(0).unwrap());
    }
}
HeroicKatora commented 4 years ago

Seems somewhat unrelated and potentially only a user error. The argument image::ColorType::Rgb8 to save_buffer requires that the data is Rgb already but the data returned by decode is likely gray scale. You could check the meta for this information. I would personally advise to use the image bindings of JpegDecoder and DynamicImage::from_decoder instead where possible and no format specific functionality is required.

ArturKovacs commented 4 years ago

You are correct, that was my mistake. This works properly.