PDFlib
BASKET
PDFlib

color/recombine_color_channels

Download Java Code     Switch to PHP Code     Show Output PDF

/*

 * $Id: recombine_color_channels.java,v 1.4 2013/01/15 10:11:57 stm 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 overprintfill=true. 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.

 *

 * Caveats: Overprint Preview/Simulation is required for correct rendering!

 *

 * Screen display in Acrobat:

 * Edit, Preferences, [General], Page Display, Use Overprint Preview

 * If this is set to "Never", only the last (Black) channel will be visible.

 * We create PDF/X so that it works with Acrobat's default "Only for PDF/X

 * Files".

 *

 * Printing with Acrobat 10.1:

 * Print, Advanced, Output, Simulate Overprinting must be enabled!

 *

 * 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;


        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", "pdfx=PDF/X-4") == -1) {

                System.err.println("Error: " + p.get_errmsg());

                System.exit(2);

            }


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


            p.set_info("Creator", "PDFlib Cookbook");

            p.set_info("Title", title + " $Revision: 1.4 $");


            /* Set output intent ICC profile for PDF/X-4 */

            if (p.load_iccprofile("ISOcoated.icc", "usage=outputintent") == -1) {

                System.err.println("Error: " + p.get_errmsg());

                System.err.println("Please install the ICC profile package from "

                    + "www.pdflib.com");

                p.delete();

                System.exit(2);

            }


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

                }

            }


            /* Enable overprint fill mode (applies to all images) */

            final int gs = p.create_gstate("overprintfill=true");


            /* dummy page size, will be adjusted by PDF_fit_image() */

            p.begin_page_ext(10, 10, "");


            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.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());

        }

        catch (Exception e) {

            System.err.println(e.getMessage());

        }

        finally {

            if (p != null) {

                p.delete();

            }

        }

    }

}