Skip to main content

Crate pdfcer_image_codec

Crate pdfcer_image_codec 

Source
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:

FilterClauseITU-TPassStatus
DCTDecode§7.4.8T.81 (JPEG)2.1implemented (dct)
CCITTFaxDecode§7.4.6T.4 / T.62.2implemented (ccitt)
JBIG2Decode§7.4.7T.882.2implemented (jbig2)
JPXDecode§7.4.9T.800 (JPEG 2000)2.3implemented (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):

  1. §8.9.5 Table 89 inverts the dictionary’s authority for JPX. /ColorSpace becomes optional and, when present, “any colour space specifications in the JPEG2000 data shall be ignored”; /BitsPerComponent is “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.
  2. §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 /DecodeParms entry.
  3. JBIG2 needs /JBIG2Globals — a stream reference, requiring a Document — and CCITT needs /Height from the image dictionary when /Rows is 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
/ColorSpaceRequired. 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.
/BitsPerComponentRequired, 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.
/DecodeApplied (§8.9.5.2) — for DCT it is the only sanctioned polarity control (rules R29/R30).Ignored unless /ImageMask is true.
/Width, /HeightDictionary 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.

Structs§

CodecNotes
Per-image honesty counters produced by the codec layer (decision 005 §6.4; the same disclosure discipline as rule R20).
CodedImage
A decoded image as the codec describes it — before any PDF-level interpretation.

Enums§

CmykJpegPolarity
How to read a four-component DCTDecode image that declares no /Decode array (spec ambiguity DCT-A1).
Codec
Which terminal image codec a /Filter chain ends in.
CodecColorModel
The colour model of CodedImage::samples, as the codec declares it — not a PDF colour space.
ImageCodecError
Why an image stream could not be turned into samples.

Constants§

MAX_IMAGE_DIMENSION
Maximum width or height, in samples, accepted from a codestream.
MAX_IMAGE_PIXELS
Maximum width × height accepted 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 /Filter chain.
decode_image_view
decode_image over an explicit DocumentView — the general form.
decode_image_view_with
decode_image_view with an explicit DCT-A1 polarity rule (R169).
terminal_codec
Which terminal codec a /Filter chain ends in, if any — so callers can route without decoding.