PDFlib Cookbook

cookbook

color/colorize_image_with_DeviceN_color

Create a DeviceN color space based on two Pantone spot colors to colorize a 2-channel image in raw format.

Download Java Code  Switch to PHP Code  Show Output 

/*
 * Colorize image with DeviceN color
 * 
 * Create a DeviceN color space based on two Pantone spot colors
 * and use it to colorize a 2-channel image in raw format.
 * 
 * We create an NChannel color space which makes correct screen display
 * independent from Acrobat's "Overprint Preview" setting.
 *
 * Required software: PDFlib/PDFlib+PDI/PPS 9.1
 * Required data: raw image file
 */
package com.pdflib.cookbook.pdflib.color;

import com.pdflib.PDFlibException;
import com.pdflib.pdflib;

public class colorize_image_with_DeviceN_color {

    static final String transformFunc =
        "% DeviceN transform function for N=2 in CIE L*a*b* color space\n"
        + "% Copyright (c) 2016 PDFlib GmbH\n"
        + "% Lab color values of input colors must be listed here:\n"
        + "80 28 75             % color 1: PANTONE 123 U\n"
        + "31.76 0 -17          % color 2: PANTONE 289 U\n"
        + "% blend L values\n"
        + "7 index 6 index mul	% t1*L1\n"
        + "7 index 4 index mul	% t2*L2\n"
        + "add\n"
        + "9 1 roll             % bottom: L\n"
        + "% blend a values\n"
        + "7 index 5 index mul	% t1*a1\n"
        + "7 index 3 index mul	% t2*a2\n"
        + "add\n"
        + "9 1 roll             % bottom: a\n"
        + "% blend b values\n"
        + "7 index 4 index mul	% t1*b1\n"
        + "7 index 2 index mul	% t2*b2\n"
        + "add\n"
        + "9 1 roll             % bottom: b\n"
        + "pop pop pop pop pop pop pop pop\n";

    public static void main(String argv[]) {
        final String title = "Colorize image with DeviceN color";

        /*
         * The sample image contains a simulated blend with values 0x00..0xFF
         * from left to right. The upper blend uses only the first channel and
         * the lower blend uses the second channel.
         */
        final String filename = "blend-256x50x2.raw";
        int exitcode = 0;
        int image, devicen;
        final String optlist;

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

            /* Page size may be adjusted by PDF_fit_image() */
            p.begin_page_ext(0, 0, "width=a4.width height=a4.height");

            devicen = p.create_devicen("names={{PANTONE 123 U} {PANTONE 289 U}} "
                    + "alternate=lab transform={{" + transformFunc + "}} "
                    + "subtype=nchannel process={colorspace=devicecmyk components={Cyan Magenta Yellow Black}}");
            if (devicen == -1) {
                System.err.println("Error: " + p.get_errmsg());
                System.exit(3);
            }
            /*
             * Load raw image; we must provide the image parameters in the
             * option list. The number of color components is deduced from the
             * DeviceN color handle.
             */
            optlist = "width=256 height=50 bpc=8 colorize=" + devicen;
            image = p.load_image("raw", filename, optlist);

            if (image == -1) {
                System.err.println("Error: " + p.get_errmsg());
                System.exit(4);
            }

            p.fit_image(image, 0.0, 0.0, "adjustpage");
            p.close_image(image);

            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);
        }
    }
}