image-rs / image

Encoding and decoding images in Rust
Apache License 2.0
4.79k stars 593 forks source link

Format error decoding Jpeg: invalid JPEG format: JPGn(7) marker found where not allowed #2249

Closed AlexanderProd closed 3 weeks ago

AlexanderProd commented 1 month ago

I'm trying to decode a jpeg image from a thermal camera, the binary information is retrieved from exiftool.

let binary = Command::new("exiftool")
            .arg("-b")
            .arg("-RawThermalImage")
            .arg(temp_file.path())
            .output()?
            .stdout;

let format = guess_format(&binary)?;
assert_eq!(format, ImageFormat::Jpeg);

let image = load_from_memory_with_format(&binary, ImageFormat::Jpeg)?;

guess_format detects it to be of Jpeg format, which I guess is correct.

Unfortunately decoding it fails with this error: Error extracting data: Format error decoding Jpeg: invalid JPEG format: JPGn(7) marker found where not allowed.

I'm quite certain that the jpeg data is valid and not corrupted. When I do the same operation in python, decoding the result from exiftool, using the decode function from the libjpeg library I get the pixel information.

I have made sure, by generating a SHA checksum, that the the binary information I'm sending to decode in python is the same as in Rust.

Also, the Jpeg struct from the img-parts crate can be properly created from this binary using the from_bytes method.

let jpeg = Jpeg::from_bytes(Bytes::from(binary.clone()))?;
// jpeg: Jpeg { segments: [JpegSegment { marker: 247 }, JpegSegment { marker: 248 }, JpegSegment { marker: 0 }, JpegSegment { marker: 218 }] }

You can download the binary from here https://drive.google.com/file/d/1itK8DpyGjXvQL_rcuM2tIMCqBCiV6nk4/view?usp=drive_link the SHA-512 checksum should be e3cce4b1883f97b398e3219ccc4931ecbb201aa9b339a624f7c6cc78a81ce54d7e452eb7627ea85c7ed3ca892b34ad9fbf1af03f55dbc1db734d47192238e65e

I'm using image version 0.24.6 with rust 1.77.2 on macOS 14.5.

fintelia commented 1 month ago

We switched to an entirely different jpeg decoder in the 0.25 release. Might be worth seeing if the problem still happens there?

AlexanderProd commented 1 month ago

We switched to an entirely different jpeg decoder in the 0.25 release. Might be worth seeing if the problem still happens there?

I just tried it with 0.25.1 and the error is different but still not able to decode the image.

Error extracting data: Format error decoding Jpeg: "Error decoding SOF Marker, Number of components cannot be zero."
fintelia commented 1 month ago

In that case, you could try opening an issue against https://github.com/etemesi254/zune-image (the error you are seeing is coming from the zune-jpeg crate). That error looks suspiciously like there might actually be a problem with your image, but perhaps it is something recoverable, or some feature that isn't implemented

AlexanderProd commented 1 month ago

Related to https://github.com/etemesi254/zune-image/issues/208

etemesi254 commented 1 month ago

The image is of type jpeg ls (https://jpeg.org/jpegls/), which is currently not supported by the decoder