Question

I am attempting to convert a jpeg image in rgb to CMYK colorspace. The only problem is my final output is always a black image. But interesting enough the preview application in MAC shows the image correctly. There does not seem to be an example of a successful rgb to cmyk conversion anywhere I've looked so far. Below is the code i'm using to attempt the conversion. This code works fine If i'm performing the conversion to rgb using RGB ICC Profile. Any guidance is greatly appreciated.

import javax.imageio.ImageIO;

public class TestClass {

  public static void main(String[] args) throws Exception {
    BufferedImage cmykImage = ImageIO.read(new File(
            "CMYK_Sample.jpg"));     
    BufferedImage rgbImage = null;

    ColorSpace cpace = new ICC_ColorSpace(ICC_Profile.getInstance(TestClass.class.getClassLoader().getResourceAsStream("icc/USWebCoatedSWOP.icc")));

    ColorConvertOp op = new ColorConvertOp(cpace, null);       
    rgbImage = op.filter(cmykImage, null);

    ImageIO.write(rgbImage, "JPEG", new File("CMYK_Sample_RGB_OUTPUT2.jpg"));

  }
}
Was it helpful?

Solution

CMYK is for printing. So, there are few possibilities to show it, except of pdf and postscript files. JPEG can show almost only RGB. So, in your last line ImageIO.write you are trying to read cmyk as RGB. Here is the problem.

CMYK in JPEG:"Adobe Photoshop and some other prepress-oriented applications will produce four-channel CMYK JPEG files when asked to save a JPEG from CMYK image mode. Hardly anything that's not prepress-savvy will cope with CMYK JPEGs (or any other CMYK format for that matter). When making JPEGs for Web use, be sure to save from RGB or grayscale mode." (http://www.faqs.org/faqs/jpeg-faq/part1/)

As for showing CMYK files in java, use java-2d (http://download.oracle.com/javase/1.3/docs/guide/2d/spec/j2d-color.fm2.html)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top