Java: read CMYK image into BufferdImage and keep correct color model
I used the twelvemonkeys collection (an extension of the imageio functionality (very helpful, thanks for that)) to create an image converter (resize format, scale, etc.).
As long as this converter doesn't deal with RGB images, everything works fine. But now I need to convert the CMYK images too. There is no point in reading a CMYK image (automatically converted to RGB) and writing it to disk. The problem is that the image must remain in CMYK format.
I tried the following solutions to my problem:
a) Use the advanced reading process to set the 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 created.
b) Read the CMYK image in the usual 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 incorrect colors (slightly orange).
Basic question: is there a way to read a CMYK image and keep the original color model (no RGB conversion), and also is there a way to save an RGB image (read CMYK + auto conversion = RGB by defalut) as CMYK using an advanced writing process (preferably with TwelveMonkeys )?
source to share
No one has answered this question yet
Check out similar questions: