3

I used the twelvemonkeys collection (extending the imageio functionality (very helpful, thank you for that)) to build an image converter (change format, scale, etc.).
Until this converter has to deal with RGB images, everything works fine. But now I have to convert CMYK images as well. It is no problem to read a CMYK image (automatically converted to RGB) and write it to disk. The problem is that the image has to stay in the CMYK format.

I tried the following options to solve my problem:

a) Use the advanced reading process to set a CMYK color model for the input file:

ImageInputStream input = ImageIO.createImageInputStream(file);
Iterator<ImageReader> readers = ImageIO.getImageReaders(input); // = com.twelvemonkeys.imageio.plugins.jpeg

ImageReader reader = readers.next();

reader.setInput(input);
ImageReadParam param = reader.getDefaultReadParam();

 for (Iterator<ImageTypeSpecifier> iterator = reader.getImageTypes(0); iterator.hasNext();) {   
    ImageTypeSpecifier currStep = iterator.next();

    //Quick and drity test: Search a CMYK image type for the reading process
    if (currStep.getBufferedImageType() == 0) {
        param.setDestinationType(currStep);
        break;
    }   
}

BufferedImage image = reader.read(0, param);
ImageIO.write(image, "jpg", output);
reader.dispose();

Result: A CMYK image with very dark colors was produced.

b) Read the CMYK image the normal way:

BufferedImage img = ImageIO.read(file); //creates an RGB image by befault

and try to save it as a CMYK image:

Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName("jpg");
ImageWriter writer = writers.next();

ImageOutputStream output = ImageIO.createImageOutputStream(outputFile);
writer.setOutput(output);

//Set DestinationType to CMYK image type
ImageWriteParam param = writer.getDefaultWriteParam();
param.setDestinationType(CMYK_IMAGE_TYPE); // Same ImageTypeSpecifier as in the first example used for reading (just for testing)

writer.write(img);
output.close();
writer.dispose();

Result: A CMYK image with wrong colors (a bit orange).

Main question: Is there a way to read a CMYK image and keep the origin color model (no RGB conversion) and is there also a way to save a RGB (read CMYK + auto conversion = RGB by defalut) image as a CMYK image using the advanced writing process (preferably with the TwelveMonkeys collection)?

  • Hi Joern! You are on the right track! Unfortunately, this isn't straight-forward... I'm working on an improved `JPEGImageWriter` that will just do the right thing if given an image in CMYK color model. However, the standard writer that I delegate the actual JPEG encoding to, isn't all that cooperative. I'll try to come up with a solution, in the mean time, you need to look into using the `write` method that takes metadata and an `IIOImage` + somehow adding the ICC profile to the metadata, preferably along with an Adobe APP14-segment. – Harald K Nov 05 '14 at 09:56
  • Also, see the [JPEG metadata documentation](http://docs.oracle.com/javase/7/docs/api/javax/imageio/metadata/doc-files/jpeg_metadata.html#color), I think it will be useful to understand the above task. – Harald K Nov 05 '14 at 09:58
  • Hey Harald, thank you very much for your fast reply. Now i created a metadata object: `IIOMetadata metadata = writer.getDefaultImageMetadata(imageType, param);` and changed the writing function to `writer.write(null, new IIOImage(cmykInput, null, metadata), param);` Exploring the metadata object shows, that it contains an arrayList (marker Sequence) with the AdobeMarkerSegment element. Is this the segment you were talking about? – joern.iwersen Nov 05 '14 at 15:34
  • Yes, that's the APP14-segment. You also need the APP2 "ICC_PROFILE" segment to include your ICC profile. For easier reading, you can use `IIOImage img = reader.readAll(index, param)`. – Harald K Nov 05 '14 at 15:45
  • I've tried the first option, it still converts to sRGB. I tried to use reader.getRawImageType(0) and it does some conversion which results in a grayscale interleave. How do I read a CMYK JPEG from disk and write it to a PDF in a DeviceCMYK color space with PDFBox? The twelvemonkeys project hints that it can be done, but I'm at a loss. – Fuwjax Oct 14 '15 at 03:08

0 Answers0