Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Catalog · How the catalog works
Anything · Lazarus or Delphi
Random projectRandom project

jbig2dec-delphi

Delphi port of jbig2dec v0.20 - JBIG2 bi-level image decoder (98/98 tests pass, pixel-exact match with C)

View on GitHub

Member rating

No ratings yet.

Sign in to rate this project.

README

jbig2dec - Delphi Port

A complete Delphi port of jbig2dec v0.20, the open-source JBIG2 bi-level image decoder originally developed by Artifex Software.

JBIG2 is a lossy and lossless compression standard for bi-level (black and white) images, commonly used in PDF documents and document scanning.

Features

  • Full JBIG2 compliance: symbol dictionaries, text regions, generic regions, halftone regions, pattern dictionaries, MMR/Group 4 fax
  • 98/98 test files decoded successfully (0 crashes, 0 timeouts)
  • 86/86 pixel-exact match with C jbig2dec v0.20 output
  • Type 36 intermediate generic regions: implemented beyond the C version (which marks this as "not yet implemented"), correctly decoding 12 additional test files
  • Context reuse ($0100/$0200 segment flags) fully supported
  • BMP output via VCL TBitmap

Build Requirements

  • Delphi 10.1 Berlin or later (Win32 target)
  • No third-party libraries required

Compiling the CLI tool

pascal
dcc32 -Q -B -NSSystem -NSWinapi -NSVcl -U"<RTL_PATH>" jbig2dec.dpr

Where <RTL_PATH> is your Delphi RTL library path, e.g.:

pascal
"C:\Program Files (x86)\Embarcadero\Studio\18.0\lib\win32\release"

Compiling the test suite

pascal
dcc32 -Q -B -NSSystem -NSWinapi -NSVcl -U"<RTL_PATH>" JBIG2DecTest.dpr

CLI Usage

pascal
jbig2dec [options] <file.jbig2>
jbig2dec [options] <global_stream> <page_stream>

Options

Option Description
-h, --help Print usage and exit
-V, --version Print version and exit
-q, --quiet Suppress diagnostic output
-v, --verbose Set verbosity level (0-3, default 1)
-o <file> Output filename (default: input with .bmp extension)
-e, --embedded Expect embedded bitstream without file header

Examples

Decode a JBIG2 file to BMP:

pascal
jbig2dec document.jbig2

Specify output filename:

pascal
jbig2dec -o output.bmp document.jbig2

Decode PDF-embedded streams (global + page):

pascal
jbig2dec globals.jbig2 page1.jbig2

Multi-page files produce numbered outputs: output.bmp, output.page2.bmp, output.page3.bmp, etc.

Library API

For programmatic use, call JBIG2DecodeToBitmap from the JBIG2Dec unit:

pascal
uses JBIG2Dec;

var
  Pages: TArray<TBitmap>;
  Data: TBytes;
begin
  Data := TFile.ReadAllBytes('document.jbig2');
  if JBIG2DecodeToBitmap(@Data[0], Length(Data), Pages) >= 0 then
  begin
    Pages[0].SaveToFile('page1.bmp');
    // ...free bitmaps when done
  end;
end;

Unit Structure

Unit Description
JBIG2Dec.Types Shared types, constants, enums, records
JBIG2Dec.Image Image allocation, pixel access, composition
JBIG2Dec.Arith QM arithmetic decoder, integer and IAID contexts
JBIG2Dec.Huffman Huffman decoder, 15 standard tables (A-O)
JBIG2Dec.MMR MMR/Group 4 fax decoding
JBIG2Dec.Generic Generic and refinement region decoding
JBIG2Dec.SymbolDict Symbol dictionary segments
JBIG2Dec.Text Text region decoding
JBIG2Dec.Halftone Pattern dictionary and halftone regions
JBIG2Dec.pas Main API, context, segment dispatch, page management

License

This project is licensed under the GNU General Public License v3.0 (or later), the same license as the original C jbig2dec. See LICENSE and COPYING for details.

Credits