Skip to content

CMYK JPEG and TIFF images are broken in PNG exports  #3781

Open

Description

@reknih

When embedding a CMYK JPEG in a Typst document, all sorts of things go wrong.

First, the PNG export and web app preview render the file as if you had let loose a toddler on all sliders in Photoshop:

Oversaturated image of a tiger

This is because the image will convert the DynamicImage's image data to RGB in a naïve way.

But this is nothing against what is going on in the PDF. I converted the tiger.jpg from typst-dev-assets to CMYK with an ICC profile in Photoshop. When displayed with proper color management, the colors in the file closely resemble the original RGB file. I posted a repro project.

When we write the PDF, we are encountering the same problem as in PNG export: image has converted the buffer to RGB. Then, we start writing a color space which is when things go really awry. The image file contains an ICC profile, which we do not read but copy into the PDF. We then say that the profile has three components because we assume this for every color image, even when CMYK ICC profiles have four components. This really flabbergasts readers. Adobe Acrobat refuses to open the file altogether. Other viewers, given the mismatch between the actual profile and its metadata, fall back to the backup profile (sRGB) we wrote (or do not support ICC profiles at all and always fall back to a Device color space, like pdf.js) to show the same horrid color reproduction that we saw in the PNG export.

Proposed solutions

  • To hotfix the PDF and not write corrupt files, we can read the image's color profile with qcms which we depend on anyways and discard it if it does not have the number of components we expect (three or one). This will allow Acrobat to open files made with CMYK images but do nothing about the fact that we convert them to RGB first and screw the colors up in the process. Doing this is not enough to close this issue.
  • To address this problem fully, we should stop relying on image and instead support JPEG, PNG, GIF, and TIFF directly with its underlying crates. This will allow us to correctly detect the number of color channels in an image and perform the color management ourselves for PNG export. For PDF export, this will even provide a speedup (and, in the case of lossy compression, more quality) since we can cease to decode and reencode images throughout the compilation if we do not have to resize them.

When embedding a CMYK image in a SVG, both exports also provide bad (but different) results. I will file separately on svg2pdf.

Activity

  1. added
    bug
    pdf
    png
    on Mar 24, 2024
  2. Enivex commented on Mar 24, 2024

    @Enivex
    Collaborator

    Partial duplicate of #2158 ?

  3. reknih commented on Mar 24, 2024

    @reknih
    MemberAuthor

    Yes. Just found out about the root cause of this on Friday and took the time to write it up. We can close either issue, whatever works best

  4. Enivex commented on Mar 24, 2024

    @Enivex
    Collaborator

    Yes. Just found out about the root cause of this on Friday and took the time to write it up. We can close either issue, whatever works best

    I guess it makes sense to close the other one, even if it's older

  5. added
    visualize
    on Mar 24, 2024
  6. baptiste commented on Mar 24, 2024

    @baptiste

    subscribing to this new one...

  7. Enivex commented on Mar 24, 2024

    @Enivex
    Collaborator

    subscribing to this new one...

    You can subscribe without commenting by the way. There's a button.

  8. LaurenzV commented on Jan 14, 2025

    @LaurenzV
    Collaborator

    Another instance, as reported in #5698:

    test

  9. changed the title [-]CMYK images are broken in PNG and PDF exports [/-] [+]CMYK JPEG and TIFF images are broken in PNG and PDF exports [/+] on Jan 15, 2025
  10. changed the title [-]CMYK JPEG and TIFF images are broken in PNG and PDF exports [/-] [+]CMYK JPEG and TIFF images are broken in PNG exports [/+] on Apr 1, 2025
  11. reknih commented on Apr 1, 2025

    @reknih
    MemberAuthor

    Reopening because the problem persists for PNG export

  12. reopened this on Apr 1, 2025
  13. dessert1 commented on Jul 2, 2025

    @dessert1

    This issue does not only affect PNG exports but PDF as well, as the MWE in #5698 shows. Note that it works fine in the preview, only the export is affected. If necessary I can provide another sample image.

  14. laurmaedje commented on Jul 2, 2025

    @laurmaedje
    Member

    @dessert1 Are using Typst 0.13.1 or the latest build from main? The fix hasn't yet been released.

  15. dessert1 commented on Jul 2, 2025

    @dessert1

    Oh my, you’re right: Fixed for me with development version (June 10, 2025). Thanks for the hint!

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

    bugpdfpngvisualize

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions