Writing a CMYK file

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Writing a CMYK file

Adam Augusta
Without too much difficulty, I managed to convert my sRGB image into
CMYK with my profile by using JNA to utilize Little CMS.  I took the
resulting samples, sanity-checked them, and then stuffed them into a
WritableRaster with a PixelInterleavedSampleModel.

I figured that from here it would be a snap to save to a CMYK image
file, perhaps in TIFF format, but I've been poring through Image I/O
and I don't see how to do it.  Can anyone help?

-Adam
Reply | Threaded
Open this post in threaded view
|

Re: Writing a CMYK file

Adam Augusta
I just wanted to share my prototyped solution to this.  The answer was
staring me in the face, but it may prove useful to someone else.

final DataBufferByte buffer = new DataBufferByte(myCmykByteArray,
myCmykByteArray.length);
final WritableRaster raster = Raster.createInterleavedRaster(buffer,
imageWidth, imageHeight, imageWidth*4, 4, new int[] {0, 1, 2, 3}, new
Point(0,0));


final ICC_Profile profileCmyk;
try {
    profileCmyk = ICC_Profile.getInstance("c:\\myprofile.icc");
} catch (IOException e) {
...
}

final ICC_ColorSpace colorSpaceCmyk = new ICC_ColorSpace(profileCmyk);
final ColorModel model = new ComponentColorModel(colorSpaceCmyk, new
int[] {8,8,8,8}, false, false, ColorModel.OPAQUE,
DataBuffer.TYPE_BYTE);

BufferedImage bufferedImage = new BufferedImage(model, raster, false, null);
final File output = new File("c:\\fileoutput.tif");
final FileImageOutputStream outputStream;
try {
    outputStream = new FileImageOutputStream(output);
} catch (IOException e) {
...
}

final ImageWriter writer =
ImageIO.getImageWritersByMIMEType("image/tiff").next();

writer.setOutput(outputStream);

try {
    writer.write(bufferedImage);
} catch (IOException e) {
    throw new RuntimeException("Oh noes", e);
}

-Adam