Why are Indexed Colorspaces useful?
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!
Latest posts by Mark Stephens (see all)
- My experience of a Turkish bath (visiting Istanbul for DevFest) - November 24, 2017
- My 5 key takeaways from JavaOne 2017 - October 6, 2017
- My notes and pictures from thursday JavaOne 2017 - October 5, 2017
- My notes and pictures from Wednesday JavaOne 2017 - October 5, 2017
- My notes and pictures from Tuesday JavaOne 2017 - October 4, 2017