Expand description
§Image codecs (ISO 32000-1 §7.4.6–§7.4.9) — the terminal filter stage
The second tier of pdfcer’s two-tier filter architecture
(docs/decisions/005-image-codecs.md §4.6, rule R23).
pdfcer_model::filters::decode_stream handles byte-stream filters —
bytes in, bytes out, chainable. This module handles the four
image codecs, which are a different kind of object:
| Filter | Clause | ITU-T | Pass | Status |
|---|---|---|---|---|
DCTDecode | §7.4.8 | T.81 (JPEG) | 2.1 | implemented (dct) |
CCITTFaxDecode | §7.4.6 | T.4 / T.6 | 2.2 | implemented (ccitt) |
JBIG2Decode | §7.4.7 | T.88 | 2.2 | implemented (jbig2) |
JPXDecode | §7.4.9 | T.800 (JPEG 2000) | 2.3 | implemented (jpx) |
§Why these cannot be Vec<u8>-returning filters
Three independent, mechanical reasons — any one of them would force a different signature (decision 005 §1.2, §5.2):
- §8.9.5 Table 89 inverts the dictionary’s authority for JPX.
/ColorSpacebecomes optional and, when present, “any colour space specifications in the JPEG2000 data shall be ignored”;/BitsPerComponentis “optional and shall be ignored if present” because “the bit depth is determined by the conforming reader in the process of decoding.” A function returning bytes has nowhere to put the bit depth or colour space it just discovered, so the caller would have to re-parse hostile input to recover what the decoder already knew. - §7.4.8’s DCT samples carry a colour model that depends on the
JPEG’s own Adobe APP14 marker, which Table 13 makes outrank
the
/DecodeParmsentry. - JBIG2 needs
/JBIG2Globals— a stream reference, requiring aDocument— and CCITT needs/Heightfrom the image dictionary when/Rowsis 0 or absent. Neither is reachable from a(dict, raw)pair.
So decode_image returns a CodedImage: samples plus the
geometry and colour model the codestream itself declares.
§Who wins a disagreement is not the same for every codec
CodedImage reports what the codestream says. It does not decide
whose account is authoritative, because the spec answers that
differently per filter, and getting the direction backwards is a
wrong-colour bug rather than a crash:
DCTDecode (and the bilevel pair) | JPXDecode | |
|---|---|---|
/ColorSpace | Required. The dictionary is the only source; a component-count disagreement is a producer bug. | Optional. Present → dictionary wins and “the colour space specifications in the JPEG2000 data shall be ignored”. Absent → codestream wins. |
/BitsPerComponent | Required, and must agree (DCT is always 8). | “Optional and shall be ignored if present” — the codestream wins outright, and honouring a stated value is explicitly wrong. |
/Decode | Applied (§8.9.5.2) — for DCT it is the only sanctioned polarity control (rules R29/R30). | Ignored unless /ImageMask is true. |
/Width, /Height | Dictionary is authoritative. | “shall match”, with no conflict rule given. |
So “the codestream is authoritative for JPX” is true only where the
dictionary is silent or disqualified; a present /ColorSpace still
wins. In every case the divergence itself is counted
(CodecNotes::geometry_mismatch) and never silently absorbed.
pdfcer-render implements the per-filter split: the dictionary’s
/Width///Height size the pixmap (§8.9.4 maps the image onto the
unit square regardless of sample count), while the codestream’s
width, component count and bit depth drive the row stride, because
that is the physical layout of the bytes in hand.
§The layer boundary (rule R26): this module never decides colour
pdfcer-core hands pdfcer-render the codec’s own samples and its
declared colour model. Only pdfcer-render applies /Decode
(§8.9.5.2), resolves /ColorSpace, and reconciles the two. Nothing
here applies a /Decode array, an “Adobe CMYK inversion”, or any
polarity flip of its own — and per decision 006 the “Adobe CMYK
inversion” half of that clause is now permanent and sourced
(rule R29: there is no such inversion to apply; /Decode is the
sole polarity control). One clarification from the same decision:
a codec adapter may observe the image dictionary to classify
diagnostics (e.g. /Decode presence for the R30 counter) —
observing is not applying. The one named future exception is
CCITT’s /BlackIs1, which is a Table 11 filter parameter and
therefore genuinely belongs to the adapter.
§Unsupported sub-features are named, never grey boxes (rule R27)
Arithmetic-coded JPEG, 12-bit JPEG, lossless JPEG, an unknown Adobe
transform byte: each is a distinct
ImageCodecError::FeatureUnsupported with a stable &'static str
key, counted by name in the renderer’s diagnostics. An operator must
be able to tell which feature is missing without reading the code.
§Resource ceilings are pdfcer’s, never the vendor’s (rule R25)
zune-core’s DecoderOptions::default() caps dimensions at 16,384
pixels. That is sensible for a general image library and a bug
here: a 20,000-pixel-wide scanned engineering drawing is legal, is
under MAX_IMAGE_PIXELS, and would be silently refused. Worse, an
inherited bad guard never appears in a diff as a number anyone chose
— and this project has already been bitten twice by
guard-by-intuition (MAX_TOKEN_LEN, MAX_XOBJECT_DEPTH). Every
ceiling below is therefore set explicitly by pdfcer and documented
with its reasoning.
Modules§
- ccitt
- CCITTFaxDecode (ISO 32000-1 §7.4.6, Table 11; ITU-T T.4/T.6) — the fax adapter
- dct
- DCTDecode (ISO 32000-1 §7.4.8, Table 13; ITU-T T.81) — the JPEG adapter
- jbig2
- JBIG2Decode (ISO 32000-1 §7.4.7, Table 12; ITU-T T.88) — the JBIG2 adapter
- jpx
- JPXDecode (ISO 32000-1 §7.4.9, §8.9.5 Table 89; ITU-T T.800) — the JPEG 2000 adapter
- jxl
JXLDecode— JPEG XL (ISO/IEC 18181) image data, a non-standard filter.- peak
- Peak-memory prediction for image decodes, and the limits a prediction is checked against (decision 227).
Structs§
- Codec
Notes - Per-image honesty counters produced by the codec layer (decision 005 §6.4; the same disclosure discipline as rule R20).
- Coded
Image - A decoded image as the codec describes it — before any PDF-level interpretation.
- Decode
Options - Per-decode options for
crate::decode_image_view_with.
Enums§
- Cmyk
Jpeg Polarity - How to read a four-component
DCTDecodeimage that declares no/Decodearray (spec ambiguityDCT-A1). - Codec
- Which terminal image codec a
/Filterchain ends in. - Codec
Color Model - The colour model of
CodedImage::samples, as the codec declares it — not a PDF colour space. - Declared
Geometry - The geometry a refused codestream declared, as named in
ImageCodecError::DeclaredGeometryExceedsData. - Image
Codec Error - Why an image stream could not be turned into samples.
- Size
Limit - Which limit refused an image (
ImageCodecError::LimitExceeded).
Constants§
- MAX_
DECODER_ WORKING_ BYTES - Ceiling on a decoder’s internal working set, in bytes: the JPX and JXL decoders’ sample planes, and the JPEG remux’s coefficient grid.
- MAX_
IMAGE_ DIMENSION - Maximum width or height, in samples, accepted from a codestream.
- MAX_
IMAGE_ PIXELS - Maximum
width × heightaccepted for a single decoded image (pdfcer policy, ARCHITECTURE.md §10.1 — no Annex C limit exists). - MAX_
IMAGE_ SAMPLE_ BYTES - Maximum decoded sample-buffer size, in bytes, for a single image.
Functions§
- decode_
image - Decode an image stream through its
/Filterchain. - decode_
image_ view decode_imageover an explicitDocumentView— the general form.- decode_
image_ view_ cancellable decode_image_view_with, abandoning the decode oncecancelledanswerstrue.- decode_
image_ view_ with decode_image_viewwith explicitDecodeOptions: theDCT-A1polarity rule (R169) and thepeak::DecodeLimitsthe image is checked against (decision 227).- terminal_
codec - Which terminal codec a
/Filterchain ends in, if any — so callers can route without decoding.