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, ColorSpaces.hints); /**create RGB colorspace and model*/ComponentColorModel rgbModel = new ComponentColorModel( rgbCS, new int[] { 8, 8, 8 }, false, false, ColorModel.OPAQUE, DataBuffer.TYPE_BYTE); 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); image.setData(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 lastC=C; lastM=M; lastY=Y; lastK=K; } new_data[pixelReached++]=(byte)(rgb[0]*255); new_data[pixelReached++]=(byte)(rgb[1]*255); 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); image.setData(raster);
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?