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 PHP Code  Switch to Java Code  Show Output 

<?php
/*
 *
 * 
 * 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
 */
$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";

$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.
 */
$filename = "blend-256x50x2.raw";
$exitcode = 0;

/*
 * This is where font/image/PDF input files live. Adjust as necessary.
 */
$searchpath = dirname(__FILE__,3)."/input";

try {
    $p = new pdflib();

    if ($p->begin_document("", "") == 0) {
        throw new Exception("Error: " . $p->get_errmsg());
    }

    $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 == 0) {
        throw new Exception("Error: " . $p->get_errmsg());
    }
    /*
     * 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 == 0) {
        throw new Exception("Error: " . $p->get_errmsg());
    }

    $p->fit_image($image, 0.0, 0.0, "adjustpage");
    $p->close_image($image);

    $p->end_page_ext("");

    $p->end_document("");

    $buf = $p->get_buffer();
    $len = strlen($buf);

    header("Content-type: application/pdf");
    header("Content-Length: $len");
    header("Content-Disposition: inline; filename=colorize_image_with_DeviceN_color.pdf");
    print $buf;

}
catch (PDFlibException $e) {
    echo("PDFlib exception occurred in colorize_image_with_DeviceN_color sample:" .
    "[" . $e->get_errnum() . "] " . $e->get_apiname()
            . ": " . $e->get_errmsg());
    exit(1);
}
catch (Throwable $e) {
    echo($e);
    exit(1);
}
$p= 0;