Back to Blog

Java CMYK HOWTO

24th Nov 2008

Lately, I’ve been playing with printers-trying to control the amount of cyan, magenta, yellow, and black printed by the printer. For various reasons I’ve been doing it in Java,and I’ve run up against some brick walls. There is no good HOWTO available, so I decided to write my own…

Java supports RGB by default and it works well, but it treats everything else like the proverbial red headed step child. To get it to work you have to find an ICC_Profile (CMYK.pf), load the file dynamically, and then do all your image manipulation. I never looked far enough to find out for sure, but I believe you have to do some special work to get it to save as CMYK also. It’s a pain.

The other option is to extend ColorSpace to support CMYK. You still have to save in a funny way, but this is what I decided to do.

First, the ColorSpace extension (available here, note that the code here is all GPL):

public class CMYKColorSpace extends ColorSpace implements Serializable {

    private static final long serialVersionUID = -5982040365555064012L;

    
    public CMYKColorSpace() {
        super(ColorSpace.TYPE_CMYK, 4);
    }

    
    @Override
    public float[] fromCIEXYZ(float[] p_colorvalue) {
        ColorSpace l_cs = ColorSpace.getInstance(ColorSpace.TYPE_RGB);
        float[] l_rgb = l_cs.toCIEXYZ(p_colorvalue);
        return fromRGB(l_rgb);
    }

    
    @Override
    public float[] fromRGB(float[] p_rgbvalue) {
        
        float[] l_res = {0,0,0,0};
        if (p_rgbvalue.length >= 3) {
            l_res[0] = (float)1.0 - p_rgbvalue[0];
            l_res[1] = (float)1.0 - p_rgbvalue[1];
            l_res[2] = (float)1.0 - p_rgbvalue[2];
        }
        return normalize(l_res);
    }

    
    @Override
    public float[] toCIEXYZ(float[] p_colorvalue) {
        float[] l_rgb = toRGB(p_colorvalue);
        ColorSpace l_cs = ColorSpace.getInstance(ColorSpace.TYPE_RGB);
        return l_cs.toCIEXYZ(l_rgb);
    }

    
    @Override
    public float[] toRGB(float[] p_colorvalue) {
        float[] l_res = {0,0,0};
        if (p_colorvalue.length >= 4)
        {
            float l_black = (float)1.0 - p_colorvalue[3];
            l_res[0] = l_black * ((float)1.0 - p_colorvalue[0]);
            l_res[1] = l_black * ((float)1.0 - p_colorvalue[1]);
            l_res[2] = l_black * ((float)1.0 - p_colorvalue[2]);
        }
        return normalize(l_res);
    }

    
    private float[] normalize(float[] p_colors) {
        for (int l_i = 0; l_i < p_colors.length; l_i++) {
            if (p_colors[l_i] > (float)1.0) p_colors[l_i] = (float)1.0;
            else if (p_colors[l_i] < (float)0.0) p_colors[l_i] = (float)0.0;
        }
        return p_colors;
    }
}

The color space is used to convert CMYK to RGB or CIEXYZ on demand, which is done whenever Java displays images on the screen, saves them, or copies them to any context in which the color space is different. It also allows you to create CMYK colors and to store the data in a custom color model/sample color model.

Creating CMYK Colors

This is self explanatory:

float[] l_colorComponents = {1, 0, 0, 0};
CMYKColorSpace l_cs = new CMYKColorSpace();
Color l_cyan = new Color(l_cs, l_colorComponents, 1);

Creating a BufferedImage with CMYK Colors

This piece is the most complicated becase you need to understand how Java models the colors in an Image Object. You may want something in-depth, but here’s a short explanation: Each color in java is represented as a “band,” and it is stored in a way defined by Model and stored in a generic “DataBuffer” object.  To get it working you create a (usually component) ColorModel, and then create a sample model with the specific layout (Interleaved, banded or one color per, etc). The sample model creates a Raster, which then creates the Image object.

CMYKColorSpace l_cs = new CMYKColorSpace();
ComponentColorModel l_ccm = new ComponentColorModel(l_cs, false, false,
                        1, DataBuffer.TYPE_FLOAT);
int[] l_bandoff = {0, 1, 2, 3}; //Index for each color (C, is index 0, etc)
PixelInterleavedSampleModel l_sm = new PixelInterleavedSampleModel(
                           DataBuffer.TYPE_FLOAT,
                           (int)l_width, (int)l_height,
                               4,(int)l_width*4, l_bandoff);
WritableRaster l_raster = WritableRaster.createWritableRaster(l_sm,
                            new Point(0,0));
BufferedImage l_ret = new BufferedImage(l_ccm, l_raster, false, null);

Graphics2D l_g2d = l_ret.createGraphics();

Saving CMYK Images

For this, I chose to use the iText library to save in PDF format. All you have to do is pull the DataBuffer out of the raster and save the bytes. You might also want to save in JPEG or another kind of format, which should be reasonably easy (just send the function the right bytes in the right order) once you’ve seen below:

Raster l_tmpRaster = c_img.getRaster();
DataBuffer l_db = l_tmpRaster.getDataBuffer();
byte[] l_bytes = new byte[l_db.getSize()];
for (int l_i = 0; l_i < l_bytes.length; l_i++) {
    l_bytes[l_i] = (byte)Math.round(l_db.getElemFloat(l_i)*(float)255);
}

com.lowagie.text.Image l_img = com.lowagie.text.Image.getInstance(
                                l_tmpRaster.getWidth(),
                                l_tmpRaster.getHeight(),
                                4, 8, l_bytes);
l_img.setDpi(300, 300);
Document l_doc = new Document(new Rectangle(0,0,l_img.getWidth(), l_img.getHeight()));
l_doc.setMargins(0,0,0,0);
PdfWriter.getInstance(l_doc,
            new FileOutputStream("inkeratorout" + c_c + ".pdf"));
l_doc.open();
l_doc.add(l_img);
l_doc.close();

Everything else should work as normal (just remember that it is calling the colorspace functions to do it’s job).

I wrote this in a bit of a rush (i’m pretty busy today). I haven’t had any problems with it yet, but if you do please feel free to leave a note below.