JFLib

img_jbig2#

JBIG2 decoder -- crate root lib/img_jbig2/src/lib.rs

img_jbig2 -- JBIG2 decoder (ITU-T T.88 | ISO/IEC 14492): bilevel images as PDF embeds them
(JBIG2Decode, with its JBIG2Globals) and JBIG2 files

  decode_embedded(data, globals, &Options) -> Bitmap / Error   PDF's JBIG2Decode: page 1
  decode_file(data, &Options) -> Vec<Bitmap> / Error           a .jb2 / .jbig2 file: every page
  probe(data) -> bool                                          the file header's id
  Bitmap { width, height, stride, bits }                       1 = black, MSB first, rows
                                                               padded to bytes
  CODEC                                                        img_core's codec (page 1 as an
                                                               8-bit ARGB surface) for img_all

Modules: mq (the arithmetic decoder, integer decoding), huff (the Huffman tables), mmr (T.6),
bitmap (images and combination operators), generic (generic and refinement regions), symbol
(symbol dictionaries, text regions), halftone (pattern dictionaries, halftone regions).

What it decodes: every segment type of a page -- page information (striped pages, pages of
unknown height growing with their stripes, the default pixel and operator), end of stripe, end
of page, end of file, tables; symbol dictionaries (arithmetic and Huffman, refinement /
aggregate coding, collective bitmaps uncompressed or MMR, exports, contexts retained and used
again), text regions (arithmetic and Huffman, refinement, transposed, every reference corner and
combination operator), pattern dictionaries and halftone regions (MMR or arithmetic, skip,
grids at any angle), generic regions (MMR; templates 0 to 3 with adaptive pixels and TPGDON;
data of unknown length ended by its marker) and refinement regions (templates 0 and 1, TPGRON),
each intermediate, immediate or lossless; the combination operators OR, AND, XOR, XNOR and
REPLACE.  The file organisations sequential and random-access.

Not here: colour (JBIG2 Amendment 3: colour palettes and coloured regions are ignored, the page
stays bilevel), extension segments.  The segments are decoded once, in their order: the global
ones where they stand, a page's onto the page their association names (a file's pages may
interleave; PDF's embedded form has one page).  A damaged segment ends its page with what was
decoded before it and that page's further segments are left out; the global ones and the other
pages go on.  Limits (`Options::max_pixels`, symbol counts, the work of a stream: its pages' and
regions' pixels, at most four times `max_pixels` plus 2^22) refuse hostile files before they
cost memory or time, and symbol dictionaries and text regions stop where their arithmetic data
has run out (mq.rs).

img_jbig2 crate root#

  • enum Error why a stream could not be decoded

    • NotJbig2 -- no JBIG2 file header (decode_file)
    • Truncated -- the data ends early
    • Corrupt -- a segment that breaks the standard
    • Unsupported -- valid, but not decoded here
    • Limit -- larger than the limits allow (or than the memory the allocator gives)
    • NoPage -- no page in the data
  • struct Options what to decode

    • max_pixels: u64 -- the largest width * height of the page and of any region
  • struct Bitmap a decoded page: 1 = black, MSB first, stride bytes a row

    • width: u32
    • height: u32
    • stride: usize
    • bits: Vec<u8>

impl Bitmap

  • fn get(&self, x: u32, y: u32) -> bool the pixel at (x, y): true for black

  • fn probe(d: &[u8]) -> bool a JBIG2 file (its header's id)

  • fn decode_embedded(data: &[u8], globals: Option<&[u8]>, opt: &Options) -> Result<Bitmap, Error> PDF's JBIG2Decode: the embedded segments (and those of JBIG2Globals) -> the first page

  • fn decode_file(data: &[u8], opt: &Options) -> Result<Vec<Bitmap>, Error> a JBIG2 file (sequential or random-access) -> its pages, in the order of their page information segments; one budget of work for the file (four times Options::max_pixels plus 2^22): the pages past it are left out

  • static CODEC: Codec jbig2_codec: the first page of a JBIG2 file as 8-bit ARGB (black on white)

Source: docs/API.md in the JFLib repository.