PDFlib Cookbook

cookbook

color/recombine_color_channels

Download Java Code     Switch to PHP Code     Show Output PDF

/*
 * $Id: recombine_color_channels.java,v 1.7 2017/01/03 14:55:00 rp Exp $
 * 
 * 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);
            }

            p.set_option("searchpath={" + searchpath + "} errorpolicy=return");

            p.set_info("Creator", "PDFlib Cookbook");
            p.set_info("Title", title + " $Revision: 1.7 $");

            /* 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");

            /* dummy page size, will be adjusted by PDF_fit_image() */
            p.begin_page_ext(10, 10, "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.getMessage());
	    exitcode = 1;
        }
        finally {
            if (p != null) {
                p.delete();
            }
	    System.exit(exitcode);
        }
    }
}