Understanding the PDF File Format: Your 5 minute guide to Indexed ColorSpaces

Why are Indexed Colorspaces useful?

indexed colour spaces

Understanding the PDF File Format. Indexed Colour spaces

Indexed colorspaces are very useful if you only have a limited number of colors in an image. If you only use 2 colors, you can create a table to store the 24bit color values (6 bytes) and then reference them as a pointer to which value to use (0 or 1) which only needs 1 bits. So each pixel will use a quarter of a byte as opposed to 3 bytes (a file size saving of 24 times which can be a very big number on large images.

If you have less than 16 colors, you need a 4 bit look-up and even if you have 255 colors you will still save 2 bytes for every pixel.

And the disadvantages?

The main disadvantage of indexed colorspaces is that they make reconstructing the image more complicated. Once you have the raw pixel data, you need to convert this back into the actual color.  Tricks like treating DCTDecode or JPXDecode data as JPEGs do not work because the pixel value is not the color but merely a pointer to the color (stored externally in the PDF file). Java’s ImageIO class allows you to extract and read the Raster directly which you can then use to build the real image.

In Java there is an IndexedImage type or you can use the lookup table to build the original image data. This also gets ‘complicated in Java because it only uses sRGB, so the Indexed color values (which can be in any ColorSpace) need conversion. So you could have a single Indexed value which is used to look-up a 4 byte YCCK color which then needs conversion into CMYK and then sRGB so that you can use it!

Power and flexibility can come at a price. As we were saying at the recent PDF Association conference, the PDF file format gives you lots of powerful tools, but it is up to you how you choose to use (or misuse them)….

Want to learn more about PDF files?

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!

If you’re a first-time reader, or simply want to be notified when we post new articles and updates, you can keep up to date by social media (TwitterFacebook and Google+) or the Blog RSS.

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.

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>