Mark Stephens Mark has been working with Java and PDF since 1999 and is a big NetBeans fan. He enjoys speaking at conferences. He has an MA in Medieval History and a passion for reading.

YCCK color conversion in PDF files

1 min read

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!

IDRsolutions develop a Java PDF Viewer and SDK, an Adobe forms to HTML5 forms converter, a PDF to HTML5 converter and a Java ImageIO replacement. On the blog our team post anything interesting they learn about.

Mark Stephens Mark has been working with Java and PDF since 1999 and is a big NetBeans fan. He enjoys speaking at conferences. He has an MA in Medieval History and a passion for reading.

3 reasons Java developers switch to JDeli from ImageIO

ImageIO is build into the JDK and provides basic image support in Java. JDeli is a commercial image library for Java Developers from IDRsolutions....
Mark Stephens
1 min read

Why we wrote our own Java jpeg2000 libraries

JPEG2000 is an important image file format which offers significant benefits over JPEG. For our specific usage it does generate significantly smaller file sizes...
Mark Stephens
52 sec read

How to choose JPG versus JPEG2000 for image files

Since we started to support both JPG and JPG2000 as image file outputs in our software, we have found that this is a very...
Mark Stephens
1 min read

2 Replies to “YCCK color conversion in PDF files”

  1. 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 *

IDRsolutions Ltd 2019. All rights reserved.