image-rs / jpeg-decoder

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

Introduces functionality for optionally returning incomplete pixel data #252

Open quilan1 opened 2 years ago

quilan1 commented 2 years ago

Thought I might start the conversation around a manner in which one could recover partially rendered image pixel data from a corrupt or partially invalid jpeg file.

Still to do / discuss:

Just wanted to throw out a minimally invasive rough draft, perhaps to start some thinking about things.

======================

Currently, this library is rather rigid, with respect to the specification. If an error occurs while decoding the stream, the error is returned and recovery of the partially decoded image is impossible.

However, there are some occasions where it may be possible, to return the potentially incomplete image data. In this case, the try_recover function may be used on the result of the decode function. If the data supports recovery, the incomplete pixels are returned, but if not, it simply returns the error.

Added the TryRecover trait, the Error::Recoverable condition and implemented a simple example usage -- if an image does not have an EOI segment, it yields a recoverable error.

Usage of the functionality is straightforward:

use jpeg_decoder::TryRecover;

let mut decoder = Decoder::new(File::open("missing-eoi.jpg")?);

// Bails with an IO error about UnexpectedEof
// let pixels = decoder.decode()?;

// Returns the incomplete pixel data of the image
let pixels = decoder.decode().try_recover()?;
HeroicKatora commented 2 years ago

I've added my thoughts on idiomatic code style above but in terms of functionality, I do believe this is already decently covers a few cases. We clearly signal when a result is not standard compliant (except for implementation bugs) but have an interface to return the main decoding result as far as possible nevertheless.

Finding a non-license encumbered test is always a hassle. However, as a guidance you can add a subfolder tests/recover structured similar to reftest. I'm not sure which properties we even want to test for these images, though. Maybe start with asserting that a recoverable error is returned without checking the exact contents?