Skip to content

Kernel: the JPEG and JPEG 2000 loaders write a device colour space from the component count, and pass on frames that DCTDecode does not decode #763

In review

Description

@Tim81

Why

ISO 32000-2, 7.4.8, says DCTDecode data has one, three or four colour components and holds baseline JPEG or the progressive extension (ISO 32000-1 allows two components as well). Table 87 says that when a JPXDecode image has a ColorSpace, the colour specification inside the JPEG 2000 data is ignored. ISO 32000-1 and 32000-2 8.9.3 say the number of colour components is inferred from the colour space in the image dictionary, so a DCT stream of 2 or 5 components under /DeviceRGB gives a reader 3. 7.4.9 (the ColorSpace bullet) says the colour channels of the JPEG 2000 data shall match the colour space's components, and that the writer shall keep the samples consistent with that colour space.

JpegImageLoader.Load maps the frame's component count with 1 => DeviceGray, 4 => DeviceCmyk, _ => DeviceRgb, and reads any start-of-frame marker. JpxImageLoader does the same in FallbackFromComponents and EnumCsForComponents, and always writes /ColorSpace.

Measured on origin/main b2791ba, each input saved through LayoutImage.FromBytes with no claim (the tests are Iso32000_2_7_4_8_Pins... and Iso32000_2_7_4_9_Pins... in ConformanceAuditG4Tests):

Input Written
JPEG frame header with 2 components /ColorSpace /DeviceRGB /BitsPerComponent 8 /Filter /DCTDecode, body unchanged
JPEG frame header with 5 components the same
JPEG with SOF3 (lossless) or SOF9 (arithmetic) /Filter /DCTDecode, body unchanged
bare JPEG 2000 codestream with 2 channels /ColorSpace /DeviceRGB, and a JP2 whose ihdr says 2 channels and whose colr says enumerated sRGB (16); under PDF/A-2b veraPDF fails the file on 6.2.8.3-1
JP2 file whose colr is enumerated sYCC (18), 3 channels /ColorSpace /DeviceRGB, so a reader ignores the colr and reads YCC samples as RGB

veraPDF 1.30.4 passes the other rows under PDF/A-2b (it has no rule for the JPEG frame, and none for the colr box against ColorSpace). Progressive JPEG (SOF2) is accepted correctly, and a 4-channel JP2 whose colr is sRGB keeps DeviceRGB because the loader reads the enumerated space first.

Sweep: image XObjects are written in VellumPdf.Kernel (Images/*, PdfDocument) and read by VellumPdf.Layout and VellumPdf.Reader; git grep -lE "ImageLoader|PdfImageXObject" src outside Kernel/Images finds PdfDocument.cs, four Layout files and the Reader's decoder. Barcodes, Cli, Signing, Conformance and Fonts.Standard14 write no image. Within the Kernel the PNG, BMP, GIF and TIFF loaders choose the colour space from the format's own fields (colour type, photometric), not from a default arm, so the two loaders above are the whole class.

Proposal

  • JPEG: refuse a frame header with a component count other than 1, 3 or 4, and a start-of-frame of SOF3, SOF5 to SOF7 or SOF9 to SOF15, with InvalidDataException. Keep SOF0, SOF1 and SOF2.
  • JPEG 2000: write /ColorSpace only when the colr box names sRGB, greyscale or CMYK and the channel count matches (or when it is a bare codestream with 1, 3 or 4 channels); otherwise leave it out so the colr box decides, and refuse a bare codestream whose channel count is not 1, 3 or 4. What PDF/A-2 6.2.8.3 needs of the colr box when ColorSpace is absent needs a check against the ISO 19005-2 text first.
  • Document each refusal on the loader, and add the "Do not pass" paragraph naming 3.0 where a kept input changes output.

Acceptance

  • Iso32000_2_7_4_8_ADctImageOfAnotherComponentCountIsNotWrittenAsDeviceRgb, Iso32000_2_7_4_8_AJpegOfAFrameTypeTheFilterDoesNotDecodeIsNotWrittenAsDctDecode and Iso32000_2_7_4_9_AJp2ColourSpecificationThatIsNotADeviceSpaceIsNotOverriddenByADeviceName lose their skip and pass; the three Pins tests are rewritten.
  • Baseline, progressive, 1, 3 and 4 component JPEGs and the sRGB, grey and CMYK JP2 files write the same bytes as before.

Activity

  1. added
    bug
    on Nov 29, 2026
  2. Tim81 commented on Nov 29, 2026

    @Tim81
    OwnerAuthor

    The ground in the Why section above is corrected: 8.9.5.1 speaks of entries inconsistent with each other or with the current colour space, not with the data; the binding clauses are 8.9.3, 7.4.9 and Table 89 (DCTDecode always delivers 8-bit samples).

    Three inputs the issue does not list (measured at 8d1f6a2, LayoutImage.FromBytes)

    Input Written veraPDF 1.30.4
    JPEG frame header SOF1 with precision 12, 3 components (SOF0 and SOF2 with precision 12 give the same) /BitsPerComponent 8 /ColorSpace /DeviceRGB /Filter /DCTDecode: the loader never reads the precision byte 2b: no failure
    JP2 file (not a bare codestream) whose ihdr has 2 channels and whose colr is greyscale (17) /ColorSpace /DeviceGray, file passed through 2b: fails 6.2.8.3-1
    JP2 file whose ihdr BPC is 0xFF with a bpcc box (channels of different depths) /BitsPerComponent 8 /ColorSpace /DeviceRGB, file passed through 2b: fails 6.2.8.3-5

    The last two are PDF/A-2b files that veraPDF rejects: like the bare-codestream row, they are invalid under the claim the file makes.
    The proposal's JPEG 2000 bullet refuses only "a bare codestream whose channel count is not 1, 3 or 4"; it does not cover a JP2
    file with 2 channels or a bpcc box, which keep their bytes and the claim.

    Also, TiffImageLoader sends a Compression 7 strip to JpegImageLoader.Load (TiffImageLoader.cs, the
    CompressionJpegNewStyle branch), so the Sweep sentence "the PNG, BMP, GIF and TIFF loaders choose the colour space from the
    format's own fields" is not true of that path. The fix in JpegImageLoader covers it, because the path delegates.

    Proposal additions

    Acceptance additions

    • A precision-12 frame is refused; a precision-8 frame writes the same bytes as before.
    • The two JP2 inputs above no longer give a PDF/A-2b file that veraPDF fails.
  3. Tim81 commented on Nov 29, 2026

    @Tim81
    OwnerAuthor

    Two more points for the fix, from the audit's second review.

    CIEJab (enumerated colour space 19). ISO 32000-2 7.4.9 says a JPEG 2000 image within a PDF file shall have one of the baseline JPX colour spaces excluding CIEJab, CMYK (12), or at least one ICC profile valid within PDF. A JP2 file whose only colour specification is CIEJab is written today as /ColorSpace /DeviceRGB, which reads Jab samples as RGB. Leaving ColorSpace out, as the proposal does for a colour specification that is not a device space, would still put a CIEJab-only image in the file, and under PDF/A-2b veraPDF then fails 6.2.8.3-4. Neither output is valid, so a CIEJab-only JP2 file is refused (InvalidDataException); one that also carries a valid ICC profile keeps it, with ColorSpace left out.

    Bit depths per channel (bpcc). The case to fix is a JP2 file whose channels have different depths (a bpcc box with, for example, 7, 7 and 3 bits), written today as /BitsPerComponent 8 /ColorSpace /DeviceRGB and passed through; veraPDF fails it on 6.2.8.3-5 under PDF/A-2b. The acceptance tests should assert the clause's outcome (the file is refused, or the image is written so that its dictionary and data agree and veraPDF accepts the PDF/A-2b file), not one mechanism, so either fix shape can satisfy them.

  4. Tim81 commented on Nov 29, 2026

    @Tim81
    OwnerAuthor

    The tests that pin and accept this issue were renamed during the audit's review rounds; the acceptance section above names earlier ones. Current names, all in ConformanceAuditG4Tests (tests/VellumPdf.Kernel.Tests/ConformanceAuditG4Tests.*.cs, PR #765). A pin passes today and is rewritten by the fix; an accept test is skipped now and loses its skip with the fix; a keep test passes before and after.

    Pins:

    • Iso32000_2_7_4_8_PinsTodayAJpegOfAnotherComponentCountIsWrittenAsDeviceRgb (2 and 5 components)
    • Iso32000_2_7_4_8_PinsTodayAFrameTypeOtherThanBaselineOrProgressiveIsWrittenAsDctDecode (SOF3, SOF9)
    • Iso32000_1_Table89_PinsTodayAJpegOfTwelveBitPrecisionIsWrittenAsEightBitDct (SOF1, precision 12)
    • Iso32000_2_7_4_9_PinsTodayAJp2WithASyccColourSpecificationIsWrittenAsDeviceRgb
    • Iso32000_2_7_4_9_PinsTodayABareCodestreamOfTwoChannelsGetsAnSrgbColourSpecification (also asserts veraPDF 6.2.8.3-1 under PDF/A-2b)
    • Iso19005_2_6_2_8_3_PinsTodayAJp2FileOfTwoChannelsIsPassedThroughAndFailsVeraPdf
    • Iso19005_2_6_2_8_3_PinsTodayAJp2FileWithChannelsOfDifferentDepthsIsPassedThroughAndFailsVeraPdf (bpcc 07 07 03, veraPDF 6.2.8.3-5)
    • Iso32000_2_7_4_9_PinsTodayAJp2WithACieJabColourSpecificationIsWrittenAsDeviceRgbAndVeraPdfDoesNotLook (colr 19 kept, veraPDF passes)

    Accept (skipped):

    • Iso32000_2_7_4_8_ADctImageOfAnotherComponentCountIsRefused (InvalidDataException)
    • Iso32000_2_7_4_8_AJpegOfAFrameTypeTheFilterDoesNotDecodeIsNotWrittenAsDctDecode (InvalidDataException)
    • Iso32000_1_Table89_AJpegOfAPrecisionOtherThanEightIsRefused (InvalidDataException)
    • Iso32000_2_7_4_9_AJp2ColourSpecificationThatIsNotADeviceSpaceIsNotOverriddenByADeviceName (refusal, or ColorSpace absent)
    • Iso19005_2_6_2_8_3_ABareCodestreamOfTwoChannelsIsRefusedOrWrittenWithoutAVeraPdfFailure
    • Iso19005_2_6_2_8_3_AJp2FileOfTwoChannelsIsRefusedOrWrittenWithoutAVeraPdfFailure
    • Iso19005_2_6_2_8_3_AJp2FileWithChannelsOfDifferentDepthsIsRefusedOrWrittenWithoutAVeraPdfFailure
    • Iso32000_2_7_4_9_AJp2FileWhoseOnlyColourSpecificationIsCieJabIsRefusedOrBackedByAnIccProfile (refusal, or an ICC colr with ColorSpace absent)

    The three JPEG 2000 "RefusedOrWritten..." tests accept a refusal (InvalidDataException) or a PDF/A-2b file with no ColorSpace entry that veraPDF passes.

    Keep: Iso32000_2_7_4_8_AProgressiveJpegIsAcceptedBecauseTheFilterDecodesIt, Iso32000_1_7_4_8_AJpegPassesThroughAsDctDecodeWithTheColourSpaceItsFrameHeaderImplies, Iso19005_2_6_2_8_3_AJpxImageIsAJp2FileWithOneColourSpecificationOfAnAllowedMethod, Iso32000_2_7_4_9_TheJpxCodestreamIsTheSourceByteForByte, Iso19005_2_6_2_8_TheOutputOfEveryLoaderThatNeedsNoCmykIntentPassesVeraPdfAndQpdf.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions