PDFlib Cookbook

cookbook

color/softmask

Download Java Code       Switch to PHP Code      Show Output PDF

/*
 * $Id: softmask.java,v 1.2 2017/01/25 10:50:17 stm Exp $
 * 
 * Softmask applications:
 * Create a template as luminosity softmask in an extended gstate. These
 * softmasks are very powerful and more general than simple clipping operations.
 * 
 * The following softmask applications are demonstrated:
 * - use an image as softmask; paint appears only in the light image areas
 * - create a "fade out" effect (opacity gradient) and apply it to an image
 * - colorize an RGB image with a blend mode
 *
 * Required software: PDFlib 9
 * Required data: none
 */
package com.pdflib.cookbook.pdflib.color;

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

public class softmask {
    public static void main(String argv[]) {
        /* This is where the data files are. Adjust if necessary. */
        String searchpath = "../input";
        String outfile = "softmask.pdf";
        String title = "Softmask";

        double page_size = 400;
        double font_size = 14;
        double border = 20;
	int exitcode = 0;
        
        pdflib p = null;

        try {
    	    int templ;
            double image_box_size, image_box_llx, image_box_lly;
            double image_llx, image_lly;
            double image_x1, image_y1;
            double image_width, image_height;
            String image_fit_opts;
            int image;
        	
            p = new pdflib();

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

            /* This means that errors in load_font() etc. throw an exception */
            p.set_option("errorpolicy=exception");

            p.begin_document(outfile, "");

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

            /* Load the font. */
            int font = p.load_font("Helvetica", "unicode", "");

            /* Load content which will be colorized; this could be a raster
             * image, SVG graphics, PDF page or other contents.
             */
            image = p.load_image("auto", "zebra.tif", "");
            
            /*
             * The image is placed centered on the page, the fitbox being
             * half the size of the page.
             */
            image_box_size = page_size / 2;
            image_box_llx = page_size / 4;
            image_box_lly = page_size / 4;
            image_fit_opts = "boxsize={" + image_box_size + " "
                + image_box_size + "} fitmethod=meet position=center";
            
            /* Determine position and size of the fitted image. */
            image_x1 = p.info_image(image, "x1", image_fit_opts);
            image_y1 = p.info_image(image, "y1", image_fit_opts);
            image_width = p.info_image(image, "width", image_fit_opts);
            image_height = p.info_image(image, "height", image_fit_opts);
            
            image_llx = image_box_llx + image_x1;
            image_lly = image_box_lly + image_y1;


            /* ------------------- Use image as a softmask ------------------ */
            p.begin_page_ext(page_size, page_size, "");

            /*
             * Create a template that will be used for the soft mask gstate.
             * 
             * The soft mask is defined by the image contents, where light
             * areas let the fill color pass through, while dark areas block
             * the fill color. You can add arbitrary content (e.g. text)
             * to the soft mask template.
             * 
             * This technique can be used to create clipping areas by
             * adding multiple areas, which is not possible with the clip()
             * operation.
             */
            templ = p.begin_template_ext(page_size, page_size,
                            "transparencygroup={colorspace=DeviceGray}");

            p.fit_image(image, image_box_llx, image_box_lly, image_fit_opts);
            
            p.end_template_ext(0, 0);
            
            /* Create gstate that uses the template as luminosity soft mask. */
            int gstate = p.create_gstate("softmask={template=" + templ 
                                                + " type=luminosity}");
            
            p.save();
            
            /* Apply the gstate containing the soft mask */
            p.set_gstate(gstate);
            
            /*
             * Fill the whole page with a blue background, which is clipped
             * by the soft mask gstate.
             */
            p.set_graphics_option("fillcolor=blue");
            p.rect(0, 0, page_size, page_size);
            p.fill();
            p.restore();
            
            int textflow = p.create_textflow(
                "Create a soft mask from an image with a template as " +
                "luminosity soft mask in an extended gstate and paint a " +
                "blue rectangle on top of it. The blue color appears only " +
                "in the light areas of the image.",
                "font=" + font + " fontsize=" + font_size +
                " alignment=center");
            
            p.fit_textflow(textflow,
                border, border, page_size - border, page_size / 5,
                "fitmethod=auto");
            
            p.delete_textflow(textflow);
            
            p.end_page_ext("");

            
            /* ----- Create "fade out" effect and apply it to an image ------ */
            p.begin_page_ext(0, 0, "width=" + page_size
                + " height=" + page_size);
            
            image = p.load_image("auto", "zebra.tif", "");

            /*
             * Create template that is filled with an axial shading from
             * black to white.
             */
            templ = p.begin_template_ext(page_size, page_size,
                            "transparencygroup={colorspace=DeviceGray}");
            
            /*
             * The axial shading is defined to reach the full white value a
             * little bit before the right border of the image and to extend
             * beyond that point with full white, so we get the image clearly
             * without any transparency at the right border.
             */
            int sh = p.shading("axial",
            		image_llx, image_lly,
            		image_llx + 0.9 * image_width, image_lly,
                1.0, 0.0, 0.0, 0.0, "startcolor={gray 0} extend1=true");

            int shp = p.shading_pattern(sh, "");
            p.set_graphics_option("fillcolor={pattern " + shp + "}");
            p.rect(image_llx, image_lly, 2*image_width, image_height);
            p.fill();
            
            p.end_template_ext(0, 0);
            
            /* Create gstate that uses the template as luminosity soft mask. */
            gstate = p.create_gstate("softmask={template=" + templ 
                + " type=luminosity}");
            
            p.save();
            
            /*
             * Apply the gstate in a save/restore bracket so it won't affect
             * other graphic content.
             */
            p.set_gstate(gstate);
            
            p.fit_image(image, image_box_llx, image_box_lly, image_fit_opts);
            
            p.restore();
            
            textflow = p.create_textflow(
                "Image with an opacity gradient created by using a " +
                "template as luminosity soft mask in an extended gstate, " +
                "resulting in a fade-out effect.",
                "font=" + font + " fontsize=" + font_size +
                " alignment=center");
            p.fit_textflow(textflow,
                border, border, page_size - border, page_size / 5,
                "fitmethod=auto");
            p.delete_textflow(textflow);

            p.end_page_ext("");


            /* ----------- Colorize an RGB image with a blend mode ---------- */
            p.begin_page_ext(0, 0, "width=" + page_size
                + " height=" + page_size);
            
            /* Load content which will be colorized; this could be a raster
             * image, SVG graphics, PDF page or other contents.
             */
            image = p.load_image("auto", "zebra.tif", "");

            /* Create a gstate with blend mode for color inversion */
            int gstate_invert = p.create_gstate("blendmode=Difference");

            /* Create the template which will be used to define the soft mask */
            templ = p.begin_template_ext(595, 842,
            		"transparencygroup={colorspace=devicergb}");

            /* Place white rectangle to initialize the whole area to visible */
            p.set_graphics_option("fillcolor=white");
            p.rect(0, 0, 595, 842);
            p.fill();

            /* Place arbitrary contents, e.g. image */
            p.fit_image(image, image_box_llx, image_box_lly, image_fit_opts);

            /* Invert the template luminosity to preserve dark and light areas */
            p.set_gstate(gstate_invert);
            p.set_graphics_option("fillcolor=white");
            p.rect(0, 0, 595, 842);
            p.fill();
            p.end_template_ext(0, 0);

            /* Create a gstate with soft mask based on the template */
            int gstate_softmask = p.create_gstate(
            		"softmask={type=luminosity template=" + templ + "}");

            /* Activate the gstate with soft mask, and apply color
             * Result: content is colorized with red
             */
            p.save();
            p.set_gstate(gstate_softmask);
            p.set_graphics_option("fillcolor={rgb 1 0 0}");
            p.rect(0, 0, 595, 842);
            p.fill();
            p.restore();

            textflow = p.create_textflow(
                "Apply blend mode \"Difference\" and a luminosity soft mask " +
                "in an extended gstate to an image and paint a red rectangle " +
                "on top of it. Dark image areas get more red color " +
                "than light areas.",
                "font=" + font + " fontsize=" + font_size +
                " alignment=center");
            p.fit_textflow(textflow,
                border, border, page_size - border, page_size / 5,
                "fitmethod=auto");
            p.delete_textflow(textflow);

            p.end_page_ext("");
            
            p.close_image(image);
            
            p.end_document("");
        }
        catch (PDFlibException e) {
            System.err.print("PDFlib exception occurred:\n");
            System.err.print("[" + e.get_errnum() + "] " + e.get_apiname()
                + ": " + e.get_errmsg() + "\n");
	    exitcode = 1;
        }
        catch (Exception e) {
            System.err.println(e.getMessage());
	    exitcode = 1;
        }
        finally {
            if (p != null) {
                p.delete();
            }
	    System.exit(exitcode);
        }
    }
}