YCCK color conversion in PDF files

There are some ‘odd’ images in PDF files. They pretend to be CMYK but in fact they are not… Here is a description of what they really are and how to handle them.

PDF files can contain image data which is DCT encoded (ie it is a JPEG image). These JPEGs can be any colorspace (sRGB, CMYK, etc). However, not all CMYK images are actually CMYK. If you were to view them (even in a package which can handle CMYK JPEGs), they would look horrible.

ycck image

These images are actually encoded as YCCK – you can tell by looking at the header or use this Java code.

com.sun.image.codec.jpeg.JPEGImageDecoder decoder =
com.sun.image.codec.jpeg.JPEGCodec.createJPEGDecoder(in);
Raster currentRaster = decoder.decodeAsRaster();
//4 is CMYK, 7 is YCCK
int colorType = decoder.getJPEGDecodeParam().getEncodedColorID();

Like CMYK, YCCK is made up of 4 channels but they are not the same.

CMYK consists of a mix of Cyan, Magenta, Yellow and Key (black). YCCK encodes the data so that information less sensitive to the huge eye is discarded (YCbCr), allowing it to keep more of the detail which our eye would notice. This is the YCC bit (K is the same).

So for each pixel value we need to translate the YCC parts into CMY values. Luckily there is a standard formula for doing this, defined in the original Postscript format (the Red Book). Here it is

R = clip(Y + 1.402 * Cr - 179.456);
G=  clip(Y - 0.34414 * Cb - 0.71414 * Cr + 135.45984);
B = clip(Y + 1.772 * Cb - 226.816);

This gives us a value for RGB, which is not the RGB values for the pixel (we have not included the K value). But we can translate it into CMY using anther formula

C = 255 - (int)R;
M = 255 - (int)G;
Y = 255 - (int)B;

This gives us the CMY pixels values which with the unaltered K value gives us CMYK. We can translate this into sRGB using profiles or several formulae. Dealing with colors is a very colorful experience!

Do you have any tips for color conversion?

This post is part of our “Understanding the PDF File Format” series. In each article, we aim to take a specific PDF feature and explain it in simple terms. If you wish to learn more about PDF, we have 13 years worth of PDF knowledge and tips, so click here to visit our series index!

Related Posts:

The following two tabs change content below.

Mark Stephens

System Architect and Lead Developer at IDRSolutions
Mark Stephens has been working with Java and PDF since 1999 and has diversified into HTML5, SVG and JavaFX. He also enjoys speaking at conferences and has been a Speaker at user groups, Business of Software, Seybold and JavaOne conferences. He has a very dry sense of humor and an MA in Medieval History for which he has not yet found a practical use.
Markee174

About Mark Stephens

Mark Stephens has been working with Java and PDF since 1999 and has diversified into HTML5, SVG and JavaFX.

He also enjoys speaking at conferences and has been a Speaker at user groups, Business of Software, Seybold and JavaOne conferences. He has a very dry sense of humor and an MA in Medieval History for which he has not yet found a practical use.

2 thoughts on “YCCK color conversion in PDF files

  1. Anders Simonsen

    ycck jpegs gave me a bit of a headache about a week ago. With a non-aware decoder they can end up looking a bit like negatives, at least it did in my case, and reversing the colors can almost made it look good. Had me chasing down the wrong path.

    I eventually discovered that those jpeg files have a non-standard App14 marker, and then I found the formula for YCCK conversion on Intel’s homepage of all places. Looking in the PostScript specs never crossed my mind.

    Windows fails to properly render those jpegs too, but libjpeg handles them just fine.

    <i<Do you have any tips for color conversion?

    Not really, but for CMYK->RGB conversion my testing shows that using a 48 entry sample table, and plain interpolation, gives fairly close results to a proper algorithm. Good enough for me anyway, and if more accuracy is needed I only have to add more samples later.

  2. libjpeg looks like an interesting tool so I will definitely investigate (I was disappointed with the Info Photoshop gave on YCCK jpegs). Thanks for the tip on CMYK to RGB.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>