images/image_color_manipulation
Manipulate image colors with the 'decode' and 'chromakey' options.
Download Java Code Switch to PHP Code Show Output
/*
* Image color manipulation:
* Manipulate image colors with the "decode" and "chromakey" options.
*
* Required software: PDFlib/PDFlib+PDI/PPS 10
* Required data: image file
*/
package com.pdflib.cookbook.pdflib.images;
import com.pdflib.pdflib;
import com.pdflib.PDFlibException;
public class image_color_manipulation
{
public static void main (String argv[])
{
/* This is where the data files are. Adjust as necessary. */
String searchpath = "../input";
String outfile = "image_color_manipulation.pdf";
String title = "Image color manipulation";
pdflib p = null;
int exitcode = 0;
String imagefile = "zebra.tif";
int image;
int x = 20, y1 = 20, y2 = 800;
try {
p = new pdflib();
String testcases[][] = {
/* image option list effect achieved */
{ "", "original image" },
{ "decode={1 0 1 0 1 0}", "invert image colors (same as 'invert')" },
{ "decode={-0.3 1.3 -0.3 1.3 -0.3 1.3}","increase contrast" },
{ "decode={0.1 0.9 0.1 0.9 0.1 0.9}", "reduce contrast" },
{ "decode={0.2 1.2 0.2 1.2 0.2 1.2}", "lighten all colors by 20%" },
{ "decode={-0.1 0.9 -0.1 0.9 -0.1 0.9}","darken all colors by 10%" },
{ "chromakey={0 77 0 77 0 77}", "treat colors darker than 30% as transparent" },
{ "chromakey={153 255 153 255 153 255}","treat colors lighter than 60% as transparent" },
};
p.set_option("searchpath={" + searchpath + "}");
/* This means we must check return values of load_font() etc. */
p.set_option("errorpolicy=return");
if (p.begin_document(outfile, "") == -1)
throw new Exception("Error: " + p.get_errmsg());
p.set_info("Creator", "PDFlib Cookbook");
p.set_info("Title", title);
for (String[] testcase: testcases)
{
String textopt = "fontname=NotoSerif-Regular fontsize=12";
p.begin_page_ext(0, 0, "width=a4.width height=a4.height");
/* Output option list and description */
if (testcase[0].equals(""))
p.fit_textline("No image option: " + testcase[1], x, y2, textopt);
else
p.fit_textline("Image option '" + testcase[0] + "': " + testcase[1],
x, y2, textopt);
/* Load the image with option */
image = p.load_image("auto", imagefile, testcase[0]);
if (image == -1)
throw new Exception("Error: " + p.get_errmsg());
p.fit_image(image, x, y1, "boxsize={540 800} fitmethod=meet");
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);
}
}
}