Scanned PDF Decoding in Rust: rusty_tiff and rusty_jbig2 Inside Freehold PDF
Fax-era CCITT and JBIG2 images hide inside most scanned PDFs. Here's how Freehold PDF moved them onto rusty_tiff and rusty_jbig2, what matched, and the trade-off it took.

Which Rust decoders read scanned PDF images in Freehold PDF?
rusty_tiff 0.1.9 decodes CCITT fax images, rusty_jbig2 0.1.1 decodes JBIG2 and rusty_jpeg 0.4.14 handles JPEGs, for OCR, Optimize, Redact and image export. Old and new builds matched on 11 of 11 CCITT images and 209 of 210 JBIG2 images. Rendered pages still use the renderer's own decoders.
Ship it
Scanned PDFs Read by Pure Rust Decodersrusty_tiff matched 11 of 11 CCITT images byte for byte and rusty_jbig2 matched 209 of 210 JBIG2 images, decoding the set in 1.2 s instead of 7.4 s on one run.
Open almost any scanned PDF from an office, a court or a library, and the page you see is usually a picture. Often it's a black and white picture squeezed with one of two old formats: CCITT Group 4, the fax standard, or JBIG2, its smarter successor. Every tool that runs OCR on a scanned PDF, shrinks it or redacts it has to decode those pictures first.
Freehold PDF is a desktop PDF editor written in Rust. Its last round moved those decoders onto three Remade crates: rusty_tiff, rusty_jbig2 and rusty_jpeg. This note covers what that swap covered, how it was checked, and what it cost.
Where the Decoders Sit
A PDF app decodes images in more places than you'd think. Freehold PDF draws pages with its renderer, and that part didn't change. The renderer keeps its own vendored decoders, so what you see on screen is the same as before.
The swap covered everything outside the renderer. That's OCR input, Optimize, Redact and the image export in Create. Those are the paths that read a picture in order to change it or read text out of it.
One budget for every image
Every image on those paths goes through a single function, ImageBudget::decode. It holds the work allowance: how many pixels and how much effort one file is allowed to cost before the app stops and calls it damaged. That one choke point is what made the swap testable. The old build and the new build could be pointed at the same images and their outputs compared byte for byte.
rusty_tiff 0.1.9 took over CCITT decoding and Group 4 encoding. rusty_jbig2 0.1.1 replaced the vendored JBIG2 decoder. rusty_jpeg 0.4.14 replaced the direct uses of zune-jpeg. CCITT Group 4 is defined in ITU-T T.6, and JBIG2 is ITU-T T.88, so both crates have a public spec to be held to.
What Matched, Image by Image
The gate was simple. Same images, old build against new, and every difference written down before release.
CCITT: 11 of 11
The CCITT set was small but real: a scanned certificate, a scanned book, and the pdf.js and hayro test files. All 11 images came out byte-identical.
One edge case is in the notes. Data marked /EndOfLine with no code before its first row makes rusty_tiff alone return nothing. Freehold PDF handles that by reading it without codes, which is how it behaved before, so the output still matches.
JBIG2: 209 of 210, and faster
The JBIG2 set had 210 images, including a 198-page real scan. 209 matched exactly. The odd one out was a stream that carried a JBIG2 file header. The old decoder refused it, and rusty_jbig2 now decodes it. A failure became a success, which is the kind of mismatch you want.
Total decode time for the set went from 7.4 seconds to 1.2 seconds. That's one run on a busy machine, so treat it as a hint rather than a benchmark. The rusty_jbig2 field note has the crate's own reference results.
JPEG: 176 decodes from real PDFs
rusty_jpeg was checked on 176 decodes of JPEGs pulled from real PDFs: scans, a slide deck and a cookbook. Every image was accepted or refused the same way as before. Across all the samples, 0.007 percent differed by more than 2 levels, and none by more than 11.
The gap is stated plainly too. No CMYK, YCCK or damaged JPEG was in that set. Those paths rely on the crate's own tests for now. If you care about recovery on broken files, truncated JPEG handling in rusty_jpeg covers that side.
The Trade-Off, and How to Check It Yourself
Nothing here came free. The cost is in the work allowance.
Bounded on purpose
rusty_jbig2 can't yet tell its caller how much work it spent on an image. So Freehold PDF charges each JBIG2 image a fixed estimate, min(left, 8 x pixels + 4 Mi), against the budget. That over-charges on purpose. A very large scanned page that needs more than the estimate allows is refused as damaged, where before it might have squeaked through.
A second, smaller change follows the spec. An empty extension segment is now refused, because T.88 gives that segment a 4-byte type, so an empty one is malformed.
Both choices lean toward bounded work on hostile input. For an app whose job is opening PDFs from strangers, that's the right way to lean, and a better work report from rusty_jbig2 would let the estimate go away.
Reproducing the run
The release ran 6,526 tests at its tag, with one failure: a UI-label test that needs a translations directory set in the environment, failing the same way on the previous commit. The OCR side ships its PP-OCRv5 mobile detection and recognition weights inside the Windows download, so OCR on a scanned PDF never needs the network.
To repeat the run, the source and README are at github.com/Remade-With-Rust/freehold-pdf. To just use the app, the Windows and Mac downloads, with checksums, are on mata.network's Freehold PDF page. From a script, freehold-pdf-cli runs the same commands headless, and freehold-pdf-cli mcp serves them to an agent over stdio.
FAQ
Quick answers for builders evaluating this technology.
Does this change how Freehold PDF draws pages on screen?
No. The swap covers the decoders PDF uses outside its renderer: OCR input, Optimize, Redact and image export. The renderer keeps its own vendored decoders, so rendered pages are unchanged.
What was the one JBIG2 image that didn't match?
A JBIG2 stream that carried a file header. The previous decoder refused it, and rusty_jbig2 now decodes it. The other 209 of the 210 test images came out identical.
Why would a large scanned page now be refused as damaged?
rusty_jbig2 can't yet report how much work it spent, so Freehold PDF charges each image min(left, 8 x pixels + 4 Mi) against its work allowance. A page that needs more than that is refused. It's a deliberate over-charge that keeps hostile files bounded.
Were CMYK or damaged JPEGs in the test set?
No. The 176 JPEG decodes came from real PDFs (scans, a slide deck, a cookbook), and none was CMYK, YCCK or damaged. Those paths are covered only by rusty_jpeg's own crate tests, and the release notes say so.
Can I run the same decoders from a script?
Yes. freehold-pdf-cli runs the app's commands headless, including OCR and Redact, and freehold-pdf-cli mcp serves them to an agent over stdio. The app's control channel listens on 127.0.0.1 only.