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.

Java CMYK to RGB conversion – speed comparison of DIY versus letting Java do it

1 min read

Some optimisations are best left to Java. For example a System.arraycopy is the fastest way to create a new version because it is optimised in hardware inside Java. So in general, any function which might have hardware optimisation is best left to the JVM.

So what about image data conversion? Is it better to delegate it to Java or Do It Yourself? I have been looking at a file which was slow to decode. The bottleneck turned out to be the CMYK to RGB conversion so I decided to have  a look…

My original code used a ColorConvertOp to do the image conversion on the raw CMYK data and then create an RGB image with the converted data. Here is the code.

int[] bands = { 0, 1, 2, 3 };

/**turn it into a BufferedImage so we can filter*/DataBuffer db = new DataBufferByte(data, data.length);

/**create RGB colorspace and model*/ColorSpace rgbCS=GenericColorSpace.getColorSpaceInstance();

/**define the conversion. hints can be replaced with null*/ColorConvertOp CSToRGB = new ColorConvertOp(
   DeviceCMYKColorSpace.getColorSpaceInstance(), rgbCS, 

/**create RGB colorspace and model*/ComponentColorModel rgbModel = new ComponentColorModel(
   rgbCS, new int[] { 8, 8, 8 }, false, false, ColorModel.OPAQUE, 

image =new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);

WritableRaster rgbRaster=rgbModel.
   createCompatibleWritableRaster(width, height);

CSToRGB.filter(Raster.createInterleavedRaster(db, width, 
   height, width*4, 4, bands, null), rgbRaster);


On the sample page it takes 73 seconds on my fast Mac.

Version 2 does the pixel conversion manually and then builds the image from that. A major advantage of this is that I can add optimisations. In this case I cache the last value so I only need to do the conversion if the value changes – on an empty page of 10,000 white pixels I will get a massive boost as I only need to convert the value once and reuse it.

ColorSpace CMYK=DeviceCMYKColorSpace.getColorSpaceInstance();

BufferedImage image = null;

byte[] new_data = new byte[w * h * 3];

int pixelCount = w * h*4;

float lastC=-1,lastM=-1,lastY=-1,lastK=-1;
float C, M, Y, K;
float[] rgb=new float[3];

* loop through each pixel changing CMYK values to RGB
*/int pixelReached = 0;

for (int i = 0; i < pixelCount; i = i + 4) {

C = (buffer[i]&0xff)/255f;
M = (buffer[i + 1]&0xff)/255f;
Y = (buffer[i + 2]&0xff)/255f;
K = (buffer[i + 3]&0xff)/255f;

if(lastC==C && lastM==M && lastY==Y && lastK==K){
//use existing values if not changed
}else{//work out new

rgb=CMYK.toRGB(new float[]{C,M,Y,K});

//cache values


new_data[pixelReached++]=(byte) (rgb[2]*255);

* turn data into RGB image
*/image =new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB);
Raster raster = createInterleavedRaster(new_data, w, h);

The method is longer, but what is the speed difference? On my Mac this takes an astonishing 11 seconds! My next task is to see whether it is the same on other platforms…

I’ve kept the old code (in the hope Oracle one day optimises the filter function), but for the moment you can guess which version I am using…

Have you found any similar cases where a DIY approach is much faster?

Did you know...

IDRsolutions offers a whole range of online file converters to convert PDF and Microsoft Excel, Word and Office Documents to HTML5, SVG or image formats?

It is free to use for single file conversions and also includes Developer links if you want to use our commercial software for bulk conversions. Find out more on this page

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.

How to read HEIC image files in Java with…

In this article, I will explain how to read HEIC files into Java as a BufferedImage. ImageIO does not read HEIC file types so...
Mark Stephens
1 min read

How to convert WMF files to SVG in java…

This article will show you how to convert WMF files into SVG files using our JDeli Java Image library. What is WMF? WMF is...
Amy Pearson
1 min read

How to write WebP images in Java

In this article, I will walk you through how to write out images as WebP images in Java. ImageIO does not support WebP images...
Mark Stephens
1 min read

Leave a Reply

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

IDRsolutions Ltd 2020. All rights reserved.