Expand description
A dependency-free Rust decoder for JBIG2 black-and-white pages.
Development status: this is an early release and is not yet suitable for untrusted production documents. Implemented features are not a claim of complete JBIG2 or Annex F conformance.
§Decode a standalone file
Decoder provides the ordinary Rust API; neither Verus nor the repository’s
proof files are needed to build or use it. Reader entry points apply the
decoder’s configured input limit while reading.
use std::fs::File;
use jbig2_decoder::{Decoder, Limits};
let decoder = Decoder::new(Limits::default());
let mut input = File::open("document.jb2")?;
let document = decoder.decode_reader(&mut input)?;
for page in document.pages() {
let bitmap = page.bitmap();
println!("page {}: {} x {}", page.number(), bitmap.width(), bitmap.height());
}§Choose the right entry point
Decoderinspects or decodes standalone documents from bytes or a reader.DecoderContextreuses scratch storage across calls; it can release that storage explicitly when reuse is no longer needed.GlobalSegmentssupports embedded JBIG2 streams with reusable globals, as used by PDF containers. This crate does not parse PDF documents.PageSinkreceives completed pages without requiring the caller to collect every decoded page in oneDecodedDocument. Internal reference retention still follows the stream and configured limits.
§Pixels, limits and errors
Bitmap stores packed, row-major, one-bit pixels, with the leftmost pixel
in the most significant bit. Its stride is measured in bytes, not pixels.
Use Bitmap::pixel for bounds-checked access or Bitmap::bytes together
with the dimensions and stride for bulk processing.
Limits is the place to set application-specific input, image, memory and
work policies. These policies are not an operating-system process-memory
limit and do not replace the crate’s remaining release/safety review.
DecodeError distinguishes malformed or unsupported input, configured
limits, allocation failures and I/O errors; callers should handle those
explicitly rather than treating a rejected document as an empty image.
§Implementation and verification
The same Rust implementation is built by ordinary Cargo and by the pinned
verification toolchain. Detailed models and proof helpers live separately
in the repository; small verus_keep_ghost annotations attach them to the
implementation and are inactive in ordinary builds.
Structs§
- Bitmap
- An owned row-major, one-bit-per-pixel bitmap.
- Colour
Page - One completely decoded, opaque RGB page.
- Decode
Result - Completely decoded output with every compatibility exception reported.
- Decoded
Colour Document - Completely decoded pages in source page-information order.
- Decoded
Document - Fully collected decoded document.
- Decoder
- High-level decoder configuration.
- Decoder
Context - Reusable decoding state for repeated documents with one
Decoder. - Document
Info - Validated document-level metadata.
- Embedded
Recovery - One allowlisted metadata or complete-marker override.
- Embedded
Result - Successful embedded output and explicit recovery diagnostics.
- File
Header - Parsed standalone JBIG2 file-header metadata.
- Global
Segments - Immutable validated metadata for reusable embedded global segments.
- Limit
Exceeded - Details of a resource-limit failure.
- Limits
- Explicit resource limits for untrusted JBIG2 input.
- Page
- One fully decoded page.
- Page
Info - Public page metadata established by page and stripe boundary segments.
- Recovery
- A recovery location in the original input, or page closure at its boundary.
- RgbBitmap
- Packed, row-major, opaque RGB8 pixels, in red/green/blue channel order.
Enums§
- Decode
Error - Error returned by parsing and decoding operations.
- Decode
Policy - Validation policy for standalone files. Existing entry points remain strict.
- Embedded
Policy - Caller-selected policy for embedded JBIG2 streams. Existing APIs stay strict.
- Embedded
Recovery Kind - A metadata inconsistency explicitly recovered by the embedded policy.
- Generic
Region Error Kind - Detailed malformed generic or refinement region classification.
- Halftone
Error Kind - Detailed malformed pattern-dictionary or halftone classification.
- Huffman
Error Kind - Detailed malformed Huffman table or code classification.
- IoError
Kind - Stable deterministic I/O failure classification.
- Limit
Kind - The resource whose configured limit was exceeded.
- Malformed
Error - A deterministic classification of malformed input.
- MmrError
Kind - Detailed malformed MMR stream classification.
- Organization
- Segment and payload organization for a standalone JBIG2 file.
- Recovery
Kind - An explicit standalone compatibility exception, not partial-output decoding.
- Unsupported
Feature - A feature that is valid JBIG2 syntax but outside the supported profile.
Traits§
- Page
Sink - Streaming destination for completed pages.
Functions§
- inspect_
file_ header - Parses a standalone JBIG2 file header without decoding its segments.