DjVu’s small files come from coding each layer of a page with a method made for it: JB2 for the black and white mask, IW44 wavelets for the colour layers. Both use the ZP coder, an adaptive binary arithmetic coder related to the Q-coder of JBIG. The full algorithms take many pages of the DjVu Reference; this page gives each chunk’s layout and what its data means.
JB2: Sjbz, Djbz and WMRM
JB2 is a variation of JBIG2 developed for DjVu. It cuts the mask into connected shapes (letters, mostly), keeps a library of shapes, and codes each shape once, then places copies of it, or refines a similar shape, wherever it appears. Everything is ZP-coded, with no byte-level header. The data is a series of records, each starting with a record type:
| Type | Record |
|---|---|
| 0 | Start of image: the image size, and a flag for the order shapes are placed in. Always first. |
| 1 | New symbol, added to the image and to the library. |
| 2 | New symbol, added to the library only. |
| 3 | New symbol, added to the image only. |
| 4 | Matched symbol with refinement (coded as a change to a library shape), added to the image and the library. |
| 5 | Matched symbol with refinement, added to the library only. |
| 6 | Matched symbol with refinement, added to the image only. |
| 7 | Matched symbol copied to the image as it is. |
| 8 | Non-symbol data: a piece of the image that is not a shape, such as a picture fragment. |
| 9 | Required dictionary size (in Sjbz, how many shapes come from the shared Djbz), or reset of the coder’s statistics. |
| 10 | Comment. |
| 11 | End of data. |
Sjbzcodes the page’s mask. When the page includes a shared dictionary, its first records say how many shapes it takes from there.Djbz, in aFORM:DJVIcomponent, holds only library records (types 2 and 5, with 0, 9, 10, 11): shapes shared by several pages, which saves much space in books.WMRMis JB2 data describing a watermark to subtract from the page.
IW44: BG44, FG44 and TH44
IW44 is a wavelet image coder. An image is sent as a series of slices, each adding detail, spread over one or more chunks of the same ID, so a viewer can show the page blurred first and sharpen it as more chunks arrive. Colour images are converted to a luminance and two chrominance components (YCbCr), and the chrominance can start later than the luminance. Each chunk starts with a header; only the first (serial 0) has the full one.
| Offset | Size | Type | Name | Description |
|---|---|---|---|---|
0 | 1 | UInt8 | serial | The chunk’s number in the series: 0 for the first chunk of this ID on the page. |
1 | 1 | UInt8 | slices | How many slices this chunk holds. |
2 | 1 | UInt8 | majorVersion | Serial 0 only. Bits 0–6: major version, 1. Bit 7: colour type, 0 for three components (colour) or 1 for one (grey). |
3 | 1 | UInt8 | minorVersion | Serial 0 only. 2 in current files (DjVuLibre writes 2; the reference describes version 1.0). |
4 | 2 | UInt16 | width | Serial 0 only. Width of the image in pixels. |
6 | 2 | UInt16 | height | Serial 0 only. Height. |
8 | 1 | UInt8 | chromaDelay | Serial 0 only. Bits 0–6: how many slices to code before the chrominance starts. Bit 7 is ignored and set by encoders. |
| 2 or 9 | rest | bytes | data | The ZP-coded wavelet coefficients for slices slices. |
BG44 holds the background, usually at a third of the page’s resolution
(the INFO size divided by 3, rounded up), and typically in three or four chunks.
FG44 holds the foreground colours, usually at a twelfth of the
resolution, in one chunk. TH44 holds one page’s thumbnail inside
FORM:THUM. The viewer scales each layer up to the page size.
FGbz: foreground palette
Instead of an FG44 image, the foreground can be a palette with one
colour per shape in the Sjbz mask, which keeps coloured text sharp.
| Size | Type | Name | Description |
|---|---|---|---|
| 1 | UInt8 | version | Bits 0–6: 0. Bit 7: colour indices follow the palette. |
| 2 | UInt16 | paletteSize | The number of colours, n. |
| 3 × n | bytes | palette | The colours, three bytes each, in blue, green, red order. |
| 3 | UInt24 | count | If bit 7 is set: the number of indices, one per shape placed by the mask. |
2 × count | UInt16[] | indices | BZZ-compressed: the palette index of each placed shape, in the order the JB2 data places them. |
Smmr: fax-coded mask
A mask coded with CCITT group 4 (MMR), as used by fax machines, for files converted from TIFF G4 without re-coding.
| Size | Type | Name | Description |
|---|---|---|---|
| 3 | char[3] | magic | MMR. |
| 1 | UInt8 | flags | Bit 0: invert (1 means 0 bits are black). Bit 1: striped. |
| 2 | UInt16 | width | Width in pixels. |
| 2 | UInt16 | height | Height in pixels. |
| 2 | UInt16 | rowsPerStrip | Striped only: rows in each strip. |
| rest | bytes | data | Unstriped: one G4 stream. Striped: for each strip, a UInt32 byte count and that many bytes of G4 data. |
JPEG chunks
BGjpandFGjphold a complete JPEG file, used in place ofBG44orFG44. DjVuLibre decodes them when built with libjpeg.BG2kandFG2kare named in the reference for JPEG 2000 data; DjVuLibre does not decode them, and they are rarely if ever found.