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)?