// CMYK jpegs are not supported by JAI, so that we have to do the conversion on our own
private BufferedImage convertCMYK2RGB(Raster raster, PDColorSpace colorspace) throws IOException
{
// create a java color space to be used for conversion
ColorSpace cs = colorspace.getJavaColorSpace();
int width = raster.getWidth();
int height = raster.getHeight();
byte[] rgb = new byte[width * height * 3];
int rgbIndex = 0;
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
// get the source color values
float[] srcColorValues = raster.getPixel(j,i, (float[])null);
// convert values from 0..255 to 0..1
for (int k = 0; k < 4; k++)
{
srcColorValues[k] /= 255f;
}
// convert CMYK to RGB
float[] rgbValues = cs.toRGB(srcColorValues);
// convert values from 0..1 to 0..255
for (int k = 0; k < 3; k++)
{
rgb[rgbIndex+k] = (byte)(rgbValues[k] * 255);
}