color/recombine_color_channels
Colorize images and place all colorized images on top of each other.
Download Java Code Switch to PHP Code Show Output
/*
* Recombine split color channels
*
* This sample code expects N grayscale images as input, colorizes each image
* with Cyan, Magenta, Yellow, and Black, respectively, and places all
* colorized images on top of each other with blendmode=Darken. As a
* result, the full recombined CMYK image is visible on the page.
*
* Using the parameters at the start of the code you can even recombine
* more than four channels, or color channels other than C, M, Y, K.
*
* Required software: PDFlib/PDFlib+PDI/PPS 9
* Required data: TIFF image file, CMYK image file, ICC profile
*/
package com.pdflib.cookbook.pdflib.color;
import com.pdflib.PDFlibException;
import com.pdflib.pdflib;
public class recombine_color_channels {
public static void main(String argv[]) {
final String title = "Channel Recombination";
final String basename = "zebra"; /* zebra_C.tif etc. */
final String suffix = "tif";
final String channelsuffix[] = { "_c", "_m", "_y", "_k" };
final int MAXCHANNEL = channelsuffix.length;
int exitcode = 0;
final String channelnames[] = { "Cyan", "Magenta", "Yellow", "Black" };
/* CMYK "alternate" values for the process color channels */
final double alt[][] = {
{ 1, 0, 0, 0 },
{ 0, 1, 0, 0 },
{ 0, 0, 1, 0 },
{ 0, 0, 0, 1 }
};
final int image[] = new int[MAXCHANNEL];
final int spot[] = new int[MAXCHANNEL];
pdflib p = null;
/* This is where font/image/PDF input files live. Adjust as necessary. */
final String searchpath = "../input";
try {
p = new pdflib();
if (p.begin_document("recombine_color_channels.pdf", "") == -1) {
System.err.println("Error: " + p.get_errmsg());
System.exit(2);
}
/* This means we must check return values of load_font() etc. */
p.set_option("searchpath={" + searchpath + "} errorpolicy=return");
p.set_info("Creator", "PDFlib Cookbook");
p.set_info("Title", title);
/* Load split channel images and colorize with a suitable spot color */
int channel;
for (channel = 0; channel < MAXCHANNEL; channel++) {
p.setcolor("fill", "cmyk", alt[channel][0], alt[channel][1],
alt[channel][2], alt[channel][3]);
spot[channel] = p.makespotcolor(channelnames[channel]);
final String filename = basename + channelsuffix[channel] + "."
+ suffix;
final String optlist = "colorize=" + spot[channel];
image[channel] = p.load_image("auto", filename, optlist);
if (image[channel] == -1) {
System.err.println("Error: " + p.get_errmsg());
System.exit(3);
}
}
/* Set suitable blend mode for overlaying the images */
final int gs = p.create_gstate("blendmode=Darken");
/* Page size may be adjusted by PDF_fit_image() */
p.begin_page_ext(0, 0, "width=a4.width height=a4.height transparencygroup={colorspace=DeviceCMYK}");
p.save();
p.set_gstate(gs);
for (channel = 0; channel < MAXCHANNEL; channel++) {
p.fit_image(image[channel], 0.0, 0.0, "adjustpage");
p.close_image(image[channel]);
}
p.restore();
p.end_page_ext("");
p.end_document("");
}
catch (PDFlibException e) {
System.err.println("PDFlib exception occurred:");
System.err.println("[" + e.get_errnum() + "] " + e.get_apiname()
+ ": " + e.get_errmsg());
exitcode = 1;
}
catch (Exception e) {
System.err.println(e);
exitcode = 1;
}
finally {
if (p != null) {
p.delete();
}
System.exit(exitcode);
}
}
}