5

our web app let users download dynamically generated images in different formats (bmp, png and jpeg). Some of our users download the images for printing, thus we would like to allow them to choose between RGB or CMYK. Is there a way to specify the color model when creating a RenderedImage/BufferedImage? If not, what is the default color model and how can I change it to another? Code snippets are welcome :)

Thanks,

Olivier.

Olivier
  • 305
  • 1
  • 4
  • 17

4 Answers4

0

It appears that it's not simple and you'll have to either load up a color profile to do it or extend the colorspace to support CYMK.

Daniel DiPaolo
  • 55,313
  • 14
  • 116
  • 115
  • Loading up a color profile seems to be a better solution, because I used a class that represents a CMYK color space but the resulting image had the "wrong" colors, whereas when I loaded a CMYK profile, the resulting image had colors comparable to the original image. – Olivier Dec 29 '10 at 20:35
0

Some image formats doesn't allow CMYK color spaces (PNG, JPEG/JFIF, GIF...) and for normal users printing in RGB is desirable.

What are the reasons you need to provide CMYK images to your customers?

David Oliván
  • 2,717
  • 1
  • 19
  • 26
-1

To convert RGB image to CMYK image by Java, one of the easiest way is to use JAI (Java Advanced Image).

Download JAI: http://download.java.net/media/jai/builds/release/1_1_3/

DownLoad JAI ImageIO: http://download.java.net/media/jai-imageio/builds/release/1.1/

Here is the code:

public static void rgbToCmyk() throws IOException{

    BufferedImage rgbImage = ImageIO.read(new File("C://Users//Public//Pictures//Sample Pictures//RGB_IMAGE.jpg"));
    BufferedImage cmykImage = null;
    ColorSpace cpace = new ICC_ColorSpace(ICC_Profile.getInstance(RbgToCmyk.class.getClassLoader().getResourceAsStream("ISOcoated.icc")));
    ColorConvertOp op = new ColorConvertOp(rgbImage.getColorModel().getColorSpace(), cpace, null);       
    cmykImage = op.filter(rgbImage, null);

    JAI.create("filestore", cmykImage, "c:/tmp/CMYK_IMAGE.TIF", "TIFF");
}

NOTE: "ISOcoated.icc" is my ICC profile. You can get it from your printer or somewhere else.

lovelywib
  • 559
  • 6
  • 10
-2

Suggest using fromRGB() - see http://download.oracle.com/javase/1.4.2/docs/api/java/awt/color/ColorSpace.html

Sample code:

java.awt.color.ColorSpace

ColorSpace cmyk = new ColorSpace(ColorSpace.TYPE_CMYK, 4);
float[] values = cmyk.fromRGB(rgbFloatArray);

public abstract float[] fromRGB(float[] rgbvalue)

Transforms a color value assumed to be in the default CS_sRGB color space into this ColorSpace.

This method transforms color values using algorithms designed to produce the best perceptual match between input and output colors. In order to do colorimetric conversion of color values, you should use the toCIEXYZ method of the CS_sRGB color space to first convert from the input color space to the CS_CIEXYZ color space, and then use the fromCIEXYZ method of this color space to convert from CS_CIEXYZ to the output color space. See toCIEXYZ and fromCIEXYZ for further information.

Mark Mayo
  • 12,230
  • 12
  • 54
  • 85
  • No, you can't do that. ColorSpace is an abstract class! – dogbane Dec 17 '10 at 16:36
  • 1
    oh blast, well spotted. In that case I suggest working with some of the examples and code from this thread: http://www.java.net/forum/topic/javadesktop/java-desktop-technologies/jai-imageio/jai-imageio-covnert-cmyk-rgb-0 – Mark Mayo Dec 17 '10 at 17:33
  • Mark, this thread actually helped me a lot after I studied the gazillion classes in the JAI API. My code is working nut needs some cleanup, I will post it when it's done. Thanks. – Olivier Dec 29 '10 at 20:19
  • Awesome, glad something I posted helped in the end ;) – Mark Mayo Dec 30 '10 at 14:08
  • You can use http://download.oracle.com/javase/6/docs/api/java/awt/color/ColorSpace.html#getInstance(int) to get an instance of the CMYK colorspace, and then use it to convert the pixel data of your image object. – SEK Jan 17 '11 at 13:15