[Tex/LaTex] Changing colors of included cthek jpg

colorgraphicspdfpdftex

I am trying to convert a pdf containing grayscale pictures and black text into shades of a single color in cmyk color space (for printing). I was thus very happy to learn, from a side remark in one of Heiko Oberdiek's answers about the key decodearray of \includegraphics, which lets us specify a linear transformation on each channel of the given color space. This key places the appropriate /Decode array in the pdf file, and colors are converted when displaying or printing the pdf file.

The approach works fine for grayscale or rgb images, but fails for cmyk images, and here is why. There are two kinds of cmyk color spaces (or of cmyk jpg images?): one for which white is all zeros and one for which it is all ones. To invert colors, pdfTeX uses a /Decode array which maps the interval [0,1] to [1,0] in each channel. This second /Decode array overrides ours. See a recent answer of mine for more details.

The question is to know how I can remove the second /Decode array, or otherwise include a cmyk image with altered colors. My current solution is to post-process the pdf, replacing the second /Decode array by spaces.

Some code: first build a cmyk image

convert `kpsewhich example-image.jpg` -colorspace cmyk /tmp/example-image-cmyk.jpg

then include it

\documentclass{article}
\usepackage{graphicx}
\begin{document}
\includegraphics[decodearray={1 1 0 0 1 0 0 1}]{example-image-cmyk.jpg}
\end{document}

and run pdflatex on this file. Afterwards, look at the pdf file and find the /Decode key twice in the same dictionary, and replace the second one by /Xxxxxx (or whatever key, with the same length). The pdf now has new colors.

EDIT: From an email discussion of pdfTeX developpers 6 years ago, it seems that providing the colorspace keyword to the primitive underlying \includegraphics may help.

Best Answer

Old question but as I just saw it:

A low-level solution seems to be possible. If one use the colorspace keyword the default /Decode array is not inserted and one can add it manually:

\documentclass{article}
\usepackage{graphicx}
\begin{document}
\immediate\pdfobj { /DeviceCMYK }
\edef\deviceobjnum{\pdflastobj}
\leavevmode
\pdfximage width 100pt attr{/Decode [1 1 0 0 1 0 0.2 0.4]} 
           colorspace \deviceobjnum
          {example-image-cmyk.jpg}
\pdfrefximage \pdflastximage\quad
\pdfximage width 100pt attr{/Decode [1 1 0.5 0.5 1 0 0.2 0.4]}
           colorspace \deviceobjnum
          {example-image-cmyk.jpg}
\pdfrefximage \pdflastximage


\end{document}

enter image description here