extract -mode image: CCITTFaxDecode image fails with ccitt: "Height" required when Width/Height/Columns/Rows are indirect objects
Extracting a CCITTFaxDecode image fails when the image dictionary and its DecodeParms reference their integer values indirectly (/Width 6 0 R, /Height 7 0 R, /Columns 6 0 R, /Rows 7 0 R). This is legal PDF and is produced by some scanner software (the real-world file that triggered this is a 1-page 9936x31513 G4 scan, PDF 1.3, no Producer). validate reports the file as valid and images list shows the correct width/height, but extract -mode image (and images extract) fails:
$ pdfcpu extract -mode image -pages 1 -- repro_ccitt_indirect.pdf out
extracting images from repro_ccitt_indirect.pdf into out/ ...
optimizing...
pages: 1
pdfcpu: ccitt: "Height" requiredThe same file with the four values written directly extracts fine (see control file below).
Root cause (master)
pkg/pdfcpu/types/streamdict.go:
parmsForFilter(d Dict)builds themap[string]intfor the filter fromDecodeParmsand only acceptsIntegerandBooleanvalues. AnIndirectRefis silently skipped, soColumnsandRowsare lost (Columnsthen falls back to 1728 inccittDecode.DecodeLength, which would corrupt the image even ifRowswere present).fixParmsthen triessd.IntEntry("Height")as a replacement for the missingRows.IntEntrydoes not dereference either, returns nil, and the errorccitt: "Height" requiredis raised.
Neither function has access to the XRefTable, so they cannot dereference. The image extraction path (pdfImage in pkg/pdfcpu/writeImage.go) does dereference Width/Height correctly via xRefTable.DereferenceInteger, but the stream is decoded before/independently of that.
Suggested fix
Resolve indirect references before the filter parameters are built, where the xref table is available, for example in the image extraction path before sd.Decode():
- dereference
Width,Heightand every entry of eachDecodeParmsdict of the pipeline (xRefTable.DereferenceInteger/DereferenceBoolean) and write the resolved values back intosd.Dict/f.DecodeParms(sd.Update),
or give parmsForFilter/fixParms an XRefTable (or a resolver callback) so that IndirectRef values are resolved instead of dropped. As a fallback, fixParms could also accept Columns/Rows from the resolved Width/Height of the image dict.
Minimal reproduction (pure ASCII, CCITT G4 data hex encoded)
Attached: repro_ccitt_indirect.pdf (64x48 px, G4, /Filter [/ASCIIHexDecode /CCITTFaxDecode], Width/Height/Columns/Rows as indirect objects 6 and 7). Its full content, if you prefer to paste it into a file:
%PDF-1.3
1 0 obj
<</Type/Catalog/Pages 2 0 R>>
endobj
2 0 obj
<</Type/Pages/Kids[3 0 R]/Count 1>>
endobj
3 0 obj
<</Type/Page/Parent 2 0 R/MediaBox[0 0 64 48]/Resources<</XObject<</Im1 5 0 R>>>>/Contents 4 0 R>>
endobj
4 0 obj
<</Length 28>>
stream
q 64 0 0 48 0 0 cm /Im1 Do Q
endstream
endobj
5 0 obj
<</Type/XObject/Subtype/Image/Name/Im1/Width 6 0 R/Height 7 0 R/ColorSpace/DeviceGray/BitsPerComponent 1/Filter[/ASCIIHexDecode/CCITTFaxDecode]/DecodeParms[null<</K -1/Columns 6 0 R/Rows 7 0 R>>]/Length 277>>
stream
26a0786ffcb59e60093c238ffb238297d97064f8647057fb2e0abf0c8e0a9f6470dbfb2e0dbf0c8e0d9f65c35be191c357ecb869fc3238697d91c1afecb834fc3238347d970ccf86470cbfb238657d9702ff0c8e0a3ecb86cf86470d1f6470cff65c18f864705fecb83fc323827d970bf0c8e13ec8e27d971fc32397d977f0c8f7d82fdf8f1fe0020020>
endstream
endobj
6 0 obj
64
endobj
7 0 obj
48
endobj
xref
0 8
0000000000 65535 f
0000000009 00000 n
0000000054 00000 n
0000000105 00000 n
0000000219 00000 n
0000000295 00000 n
0000000814 00000 n
0000000832 00000 n
trailer
<</Size 8/Root 1 0 R>>
startxref
850
%%EOFReplacing /Width 6 0 R/Height 7 0 R by /Width 64/Height 48 and /Columns 6 0 R/Rows 7 0 R by /Columns 64/Rows 48 makes the extraction succeed (repro_ccitt_direct.pdf, also attached).
Environment
- pdfcpu: v0.10.2 dev, commit: 23a991a (2025-04-23T12:26:44Z) (windows/386) and the current master source (
fixParmsunchanged). - Windows 10.
Side note
On the same real-world file, after patching the values to direct integers, the 386 build dies with fatal error: out of memory because the 313 Mpx 1-bit image is expanded to 8-bit gray before being re-encoded. Writing CCITT streams as-is into a TIFF container (Compression 3/4, single strip), as pdfimages does, would avoid both the decode and the memory blow-up for large scans.
Source: pdfcpu/pdfcpu