Truncated JPEG in Rust: What rusty_jpeg Now Does With Half a Photo
A cut-short download used to be an error. Since rusty_jpeg 0.4.10 a truncated JPEG decodes like libjpeg-turbo does, with warnings you can read and a strict switch to refuse.

How does rusty_jpeg handle a truncated JPEG?
Since 0.4.10, rusty_jpeg decodes the part of a truncated JPEG that arrived and fills the rest, the way libjpeg-turbo does. Decoder::is_truncated() and warnings() tell you it happened, and set_strict(true) restores the old error. A file that ends before its first scan has any data is still an error.
A truncated JPEG is what you have when a download died at 70 percent, a camera lost power mid-write, or a video frame arrived short. For most of rusty_jpeg's life that meant an error and no picture. As of rusty_jpeg 0.4.10, published on November 21, 2026, the decoder shows what did arrive, fills the rest, and tells you it did so. This is the short tour: what it does, how to read the warnings, and the one behavior change that can bite an existing caller.
The crate itself is covered in the rusty_jpeg field note, and its repo has a landing page with the Cargo line.
Why a truncated JPEG used to be an error
A JPEG is a header followed by entropy-coded scans and an end-of-image marker. Strict decoders treat anything short of that as corruption. That is a safe default, and it is what rusty_jpeg inherited from jpeg-decoder. But viewers have never worked that way. The libjpeg family answers the end of the data with a fake end-of-image marker, warns that the file ended prematurely, and keeps decoding. That is how a half-downloaded photo can still show its top half in viewers built on libjpeg.
The push to match came from a downstream team that still kept another decoder around for damaged files. Their request is issue 28, which also asks for 12-bit, arithmetic and lossless decoding. When the team compared 0.4.9 against libjpeg-turbo 3.x, the pull request says, 0 of 179 of their truncated files decoded, where libjpeg-turbo decoded 172. Pull request 31 is the fix.
How rusty_jpeg recovers a cut-short file now
The pull request describes one mechanism, not a pile of special cases. libjpeg-turbo's data source inserts an end-of-image marker when the bytes run out, a step visible in its jdatasrc.c. rusty_jpeg now treats "no more bytes" the same way, and the recovery already built for a marker that shows up early does the rest.
What you get for each kind of cut
The change notes in the repo's CHANGES.md list the cases. A missing end-of-image marker alone gives the complete image. A cut inside a baseline scan gives everything decoded so far, with the rest of the scan filled grey. A cut between scans gives the image as the scans so far describe it, and a component that no scan delivered comes out mid-grey. A progressive file keeps every block at the precision the scans that arrived gave it, then gets the same interblock smoothing libjpeg-turbo applies. That is why a half-downloaded progressive photo comes out soft instead of blocky.
A file that ends before its first scan has any data is still an error. There is nothing to show, and a bare header should not make the decoder allocate the full image it declares.
Reading the warnings
Recovery is the default, so you need a way to see it happened. This is the README's own example:
let mut decoder = rusty_jpeg::Decoder::new(&bytes[..]);
let pixels = decoder.decode()?; // Ok: the recoverable part of the image
if decoder.is_truncated() {
// the data ended before the end-of-image marker
}
for warning in decoder.warnings() { // empty for a complete, well-formed file
eprintln!("recovered from: {warning}");
}
Each kind of repair is listed once: Truncated, SegmentEndedEarly, BadHuffmanCode, RestartMarkerMismatch, ExtraneousBytes and MissingScan. An empty list means the decoder read a complete, well-formed stream. A thumbnailer can show the truncated JPEG and quietly log the warnings. An archive tool can flag the file for a human.
Strict mode, and the change that can surprise you
The behavior change is the one to check in your own code. Anything that used Err(UnexpectedEof) to drop a cut-short frame now gets pixels instead. The project's own notes say such code, an MJPEG receiver for example, must either call set_strict(true) or check is_truncated(). With strict on, a truncated stream fails with Error::UnexpectedEof as before, and any other damage fails with Error::Format.
One more guard moved. set_max_decoding_buffer_size is now checked before the first scan is decoded, since a stream can end anywhere and still decode, and an oversized header should be refused before any plane is allocated.
How close is it to libjpeg-turbo, and what is still missing
The README gives a table, and the method matters more than the headline. It compares against libjpeg-turbo 3.1.0's djpeg on 5,544 damaged variants of 210 files: cuts, single-bit flips and garbles. A variant counts as a match when every sample is within 1 grey level, or 3 for colour.
The numbers, with their limits
Of the 5,368 damaged variants rusty_jpeg decodes, 4,875 match, which is 90.8 percent, against 1,454 or 27.1 percent in 0.4.9. On the downstream team's own cut-short corpus, 170 of 179 decode, where libjpeg-turbo decodes 172. The README reports zero panics in both versions. These are the project's measurements, taken from its README and pull request. I did not re-run them.
Where it still falls short
The README names two gaps. The two cut-short files libjpeg-turbo opens and rusty_jpeg does not are progressive files with a damaged Huffman table. And 4:2:2 output differs from djpeg by up to 4 to 5 levels from upsampling rounding, on intact and cut-short files alike. The project's change notes add one known limit: in a sequential scan, a damaged AC table's run-length symbols 1 to 14 are read as an end-of-band run, where libjpeg-turbo reads end of block.
What else landed this week
Version 0.4.14, published on November 25, 2026, decodes 12-bit DCT, arithmetic-coded and two-component lossless JPEGs, the layout DNG uses for some raw tiles. It also fixes lossless images stored with one scan per component, which used to decode wrongly even when intact. Lossless arithmetic coding is still refused with Error::Unsupported, and libjpeg-turbo does not read it either. If you came for speed instead, the 0.4.4 decode note covers that story, and the pure Rust alternative to libjpeg explains the wider case.
Pin 0.4.14 from crates.io, run your own cut-short samples, and decide per call site whether half a photo is a feature or a bug. The remade_ffmpeg_rs note shows where this crate sits in the larger media stack.
FAQ
Quick answers for builders evaluating this technology.
What happens if only the final end-of-image marker is missing?
You get the complete image. The rusty_jpeg 0.4.10 change notes list a missing end-of-image marker as the simplest case, and the warnings list then holds Warning::Truncated. libjpeg-turbo treats the end of the data as an end-of-image marker of its own, and rusty_jpeg now follows the same path.
How do I make rusty_jpeg refuse a damaged file again?
Call set_strict(true) on the decoder before decode. A truncated stream then fails with Error::UnexpectedEof, as before, and other damage fails with Error::Format. This suits a video frame receiver that would rather drop a bad frame and resynchronise than show half a picture.
Does the recovered image match libjpeg-turbo exactly?
Mostly. The README reports that 4,875 of the 5,368 damaged variants rusty_jpeg decodes, 90.8 percent, match libjpeg-turbo's djpeg within a small sample tolerance, up from 27.1 percent in 0.4.9. It also says 4:2:2 output can differ from djpeg by up to 4 to 5 levels from upsampling rounding.
Is the JPEG damage recovery safe against hostile files?
The README reports zero panics across its damaged-file corpus, and set_max_decoding_buffer_size is now enforced before the first scan is decoded, so an oversized header is refused early. That is a test result on the project's own corpus, not a promise about every possible file.
What else changed in rusty_jpeg 0.4.14?
The 0.4.14 release on November 25, 2026 adds decoding of 12-bit DCT, arithmetic-coded and two-component lossless JPEGs, and fixes lossless images stored with one scan per component. Lossless arithmetic coding, SOF11, is still refused with Error::Unsupported, which libjpeg-turbo does not read either.