package com.pdflib.cookbook.pcos.resources;

import com.pdflib.pCOS;
import com.pdflib.pCOSException;
import com.pdflib.cookbook.pcos.pcos_cookbook_example;

/**
 * Print information about all images in the document.
 * <p>
 * Required software: pCOS interface 3 (pCOS 3.x, PDFlib+PDI/PPS 7.x, TET 2.2,
 * PLOP 3.x) <br>
 * Required data: PDF document
 * 
 * @version $Id: images.java,v 1.9 2010/09/29 16:38:06 tm Exp $
 */
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(pCOS p, String filename) throws pCOSException,
        Exception {
        /* Open the PDF document */
        int doc = p.open_document(filename, "");
        if (doc == -1)
            throw new Exception("Error: " + p.get_errmsg());

        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();
        }

        p.close_document(doc);
    }

    private static void check_colorspace(pCOS p, int doc, int image)
        throws pCOSException {
        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") || cs.equals("Separation")) {
                System.out.print(" "
                    + p.pcos_get_string(doc, "images[" + image
                        + "]/ColorSpace[1]"));
            }
        }
        else if (objtype.equals("null")) {
            System.out.print("ImageMask");
        }
        else {
            System.out.print("(complex ColorSpace)");
        }
    }

    public images(String[] argv, String readable_name, String search_path,
        String full_rcs_file_name, String revision) {
        super(argv, readable_name, search_path, full_rcs_file_name, revision);
    }

    public static void main(String argv[]) {
        images example = new images(argv, "Image XObjects", SEARCH_PATH,
            "$RCSfile: images.java,v $", "$Revision: 1.9 $");
        example.execute();
    }
}

