BASKET
Search
PDFlib

color/colorize_image_with_DeviceN_color

Download Java Code     Switch to PHP Code     Show Output PDF

/*

 * $Id: colorize_image_with_DeviceN_color.java,v 1.3 2017/01/03 13:28:11 rp Exp $

 *

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

            }


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


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

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


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

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


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


            /*

             * 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(3);

            }


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

            exitcode = 1;

        }

        finally {

            if (p != null) {

                p.delete();

            }

            System.exit(exitcode);

        }

    }

}