pCOS Cookbook

cookbook

resources/images

Print information about all images in the document.

Download Java Code  Show Output  Show Input (pdfa.pdf) 

/*
 * Print information about images in the document.
 * Simple enumeration of image resources works with all products which include
 * the pCOS interface. However, PDFlib TET offers much more advanced image
 * retrieval features.
 *
 * Required software: pCOS interface 8 (PDFlib+PDI/PPS 9, TET 4.1, PLOP 5.0)
 * Required data: PDF document
 */
package com.pdflib.cookbook.pcos.resources;

import com.pdflib.IpCOS;
import com.pdflib.cookbook.pcos.pcos_cookbook_example;

public class images extends pcos_cookbook_example {

    /* This is where the data files are. Adjust as necessary. */
    private final static String SEARCH_PATH = "../input";

    public void example_code(IpCOS p, int doc) throws Exception {

        System.out.println("File name: " + p.pcos_get_string(doc, "filename"));

        int imagecount = (int) p.pcos_get_number(doc, "length:images");

        System.out.println("Image count: " + imagecount);

        for (int image = 0; image < imagecount; image += 1) {
            System.out.print("  Image " + (image + 1) + ", ");

            String objtype = p.pcos_get_string(doc, "type:images[" + image
                + "]/Filter");

            if (objtype.equals("name")) {
                String filter = p.pcos_get_string(doc, "images[" + image
                    + "]/Filter");
                System.out.print("Filter=" + filter);
            }
            else if (objtype.equals("array")) {
                String filter = p.pcos_get_string(doc, "images[" + image
                    + "]/Filter[0]");
                System.out.print("Filter=" + filter);

                int filtercount = (int) p.pcos_get_number(doc, "length:images["
                    + image + "]/Filter");

                for (int f = 1; f < filtercount; f++) {
                    filter = p.pcos_get_string(doc, "images[" + image
                        + "]/Filter[" + f + "]");
                    System.out.print("+" + filter);
                }
            }

            System.out.print(", "
                + (int) p.pcos_get_number(doc, "images[" + image + "]/Width")
                + "x"
                + (int) p.pcos_get_number(doc, "images[" + image + "]/Height")
                + ", ");

            System.out.print(
                    (int) p.pcos_get_number(doc, "images[" + image + "]/bpc")
                    + " bits per component, ");

            check_colorspace(p, doc, image);

            System.out.println();
        }
    }

    private static void check_colorspace(IpCOS p, int doc, int image)
        throws Exception {
        String objtype = p.pcos_get_string(doc, "type:images[" + image
            + "]/ColorSpace");

        if (objtype.equals("name")) {
            System.out.print(p.pcos_get_string(doc, "images[" + image
                + "]/ColorSpace"));
        }
        else if (objtype.equals("array")) {
            String cs = p.pcos_get_string(doc, "images[" + image
                + "]/ColorSpace[0]");
            System.out.print(cs);

            if (cs.equals("Indexed")) {
                /* Print information about base colorspace */
                String basepath = "images[" + image + "]/ColorSpace[1]";
                objtype = p.pcos_get_string(doc, "type:" + basepath);
                if (objtype.equals("name")) {
                    System.out.print(" " + p.pcos_get_string(doc, basepath));
                }
                else {
                    System.out.print(" (complex base color space)");
                }
            }
            else if (cs.equals("Separation")) {
                /* Print information about alternate colorspace */
                String altpath = "images[" + image + "]/ColorSpace[1]";
                objtype = p.pcos_get_string(doc, "type:" + altpath);
                if (objtype.equals("name")) {
                    System.out.print(" " + p.pcos_get_string(doc, altpath));
                }
                else {
                    System.out.print(" (complex alternate color space)");
                }
            }
        }
        else if (objtype.equals("null")) {
            System.out.print("ImageMask");
        }
        else {
            /* 
             * Look at colorspaces.java for code that dissects
             * color spaces in full detail.
             */
            System.out.print("(complex color space)");
        }
    }

    public images(String[] argv, String readable_name, String search_path) {
        super(argv, readable_name, search_path);
    }

    public static void main(String argv[]) {
        images example = new images(argv, "Image XObjects", SEARCH_PATH);
        example.execute();
    }
}