|
JBIG2 Compression Support
Overview
HotPDF now includes comprehensive JBIG2 (Joint Bi-level Image Experts Group 2) compression support
JBIG2 is a lossy or lossless compression standard specifically designed for bi-level (monochrome) images,
commonly used in PDF documents for scanned text pages and line art.
Key Features
- High-efficiency compression for bilevel images
- Support for both lossy and lossless compression modes
- Support for both lossy and lossless compression modes
- Optimised for text and line art compression
- Significant file size reduction compared to traditional compression methods
- Full PDF/A compliance support
- Multi-page document support with shared dictionaries
Compression Benefits
- Cross-page reuse: Repeated symbols are classified once and kept in shared global data
- Symbol matching: With
UseSymbolDictionary, repeated black connected components are stored once as symbols, and LossyLevel lets near-identical components share one symbol
- Memory Efficient: Optimised for processing large scanned documents
Typical Use Cases
- Scanned text documents
- Technical drawings and blueprints
- Forms and invoices
- Maps and diagrams
- Mixed content documents with text and graphics
Technical Implementation
The JBIG2 support is implemented through the HPDFJBIG2.pas unit, which provides:
- THPDFJBIG2Decoder: Core decoder class for JBIG2 compressed data
- Stream Processing: Efficient handling of JBIG2 data streams
- Global Dictionary Support: Shared symbol dictionaries for multi-page documents
- Scanline Access: Row-by-row data access for memory-efficient processing
- Global Dictionary Support: Shared and multi-level chained symbol dictionaries with one-, two-, or four-byte segment identifiers
- Cross-page batch encoder:
THPDFJBIG2Encoder.EncodePagesToByteArrays trains across the complete batch and returns one page stream per input plus shared global data
- Batch contract:
TJBIG2BatchPage describes one packed page, TJBIG2BatchEncodeBackend identifies the selected implementation, and TJBIG2BatchEncodeCallback defines external batch integrations
- Document integration:
THotPDF.AddJBIG2ImageBatch and THotPDF.AddJBIG2BitmapBatch encode before mutating the PDF graph, attach a shared /JBIG2Globals stream, and return image indexes in input order
- Batch telemetry:
TJBIG2BatchEncodeStatistics reports selected backend, input and output sizes, native symbol count, and budget rejection
- Backend recovery:
HPDFJBIG2EncRegisterBackends restores both built-in encoder callbacks after temporary application overrides are cleared
- HPDFJBIG2SplitSymbolDictionaryGlobals: Separates a sequential embedded stream into global dictionaries and page image segments while preserving long-form dependency headers
- Visible-Region Decode:
THPDFJBIG2Decoder.LoadRegionFromByteArray allocates only the requested window, resolves its transitive dictionary dependencies, composes intersecting page regions with clipping, and skips independent off-window regions before entropy decoding
- Region Decode Telemetry:
LastRegionDecodeStats reports decoded and skipped region counts plus decoded source pixels, while UsedIncrementalRegionDecode distinguishes the native window path from complete-decode fallback
- Scanline Access: Row-by-row data access for memory-efficient processing
Usage Example
// Delphi example - Processing JBIG2 compressed data
procedure ProcessJBIG2Data;
var
PDF: THotPDF;
Decoder: THPDFJBIG2Decoder;
ImageData: TBytesArray;
ScanlineData: TBytesArray;
Row: Integer;
begin
PDF := THotPDF.Create(nil);
Decoder := THPDFJBIG2Decoder.Create;
try
PDF.BeginDoc;
PDF.AddPage;
// Load JBIG2 compressed image data
if Decoder.LoadFromByteArray(ImageData) then
begin
// Process scanline by scanline for memory efficiency
for Row := 0 to Decoder.Height - 1 do
begin
if Decoder.GetScanline(Row, ScanlineData) then
begin
// Process scanline data as needed
// Add to PDF page
end;
end;
end;
PDF.EndDoc;
finally
Decoder.Free;
PDF.Free;
end;
end;
Encoder Options and Backends
- THotPDF.JBIG2Options: The document's
THPDFJBIG2Options, which AddImage(..., icJBIG2) converts into TJBIG2EncodeOptions. Its defaults suit scans: Lossless=True, LossyLevel=5, RemoveIsolatedBlackPixels=True and BlackDotSize=3
- Speckle removal: Whilst
RemoveIsolatedBlackPixels is True, black connected components whose width and height are both smaller than BlackDotSize are cleared before encoding, so the defaults remove dots of 2 x 2 pixels and smaller. Switch it off when the output must match the bitmap pixel for pixel
- Encoder choice:
UseExternalEncoder (default True) uses the registered high-performance arithmetic encoder; when it is off, missing or fails, the built-in lossless MMR generic region is written instead. UseNativeArithmeticFallback=True tries the built-in Pascal arithmetic coder before MMR, and with UseSymbolDictionary=True it first attempts a symbol dictionary with text regions
- DefaultJBIG2EncodeOptions: The options a stand-alone
THPDFJBIG2Encoder uses: lossless, with LossyLevel=0 and no speckle removal
- Decoder choice:
THotPDF.UseExternalJBIG2Decoder (default True) decodes loaded /JBIG2Decode images with the high-performance backend where one is available; False uses the built-in Pascal decoder
- Custom backends:
RegisterJBIG2EncoderBackend and RegisterJBIG2DecoderBackend install application callbacks, IsJBIG2EncoderBackendAvailable and IsJBIG2DecoderBackendAvailable report them, and ClearJBIG2Backends removes them again
Class Reference
THPDFJBIG2Decoder
- LoadFromByteArray: Load JBIG2 data from byte array; gives False when the data is incomplete or not supported
- GetScanline: Retrieve specific scanline data
- Width/Height: Image dimensions properties
Performance Optimisation
- A fixed 100-page scan corpus compresses from 158,300 bytes with separate generic regions to 33,600 bytes with external cross-page training, a 78.8 per cent reduction
- The same corpus completes in 11 ms instead of 53 ms on Win64 and 16 ms instead of 77 ms on Win32 in the regression benchmark
- Page-to-symbol lookup is indexed and repeated encoded page streams are cached for constant-time batch extraction
- Memory-efficient scanline processing
- Optimised for large document handling
- Minimal memory footprint during decompression
Standards Compliance
- ITU-T T.88 standard compliance
- ISO/IEC 14492 standard support
- PDF 1.4 or later image XObjects with
/JBIG2Decode and /JBIG2Globals
- HotPDF does not write JBIG2 whilst PDF/A output is active:
AddImage(..., icJBIG2) writes icCCITT42 instead and batch insertion gives False
- Cross-platform consistency
See Also
|