package com.pdflib.cookbook.pcos.pages;

import com.pdflib.pCOS;
import com.pdflib.pCOSException;
import com.pdflib.cookbook.pcos.pcos_cookbook_example;

/**
 * List colorspaces.
 * <p>
 * Required software: pCOS interface 3 (pCOS 2.x, PDFlib 7.x, TET 2.2,
 * PLOP 3.x)<br>
 * Required data: PDF document
 * 
 * @version $Id: page_colors.java,v 1.3 2007/09/20 14:45:04 stm Exp $
 */
public class page_colors 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.get_string(doc, "filename"));

        int pagecount = (int) p.get_number(doc, "length:pages");

        for (int page = 0; page < pagecount; page++) {
            print_colorspaces(p, doc, page);
        }
        p.close_document(doc);
    }

    /**
     * Print the colorspaces for a given page number.
     * 
     * @param p
     *            pCOS object
     * @param doc
     *            Document handle
     * @param page
     *            Page number
     * 
     * @throws pCOSException
     */
    private void print_colorspaces(pCOS p, int doc, int page)
        throws pCOSException {

        int colorspacecount = (int) p.get_number(doc,
            "length:pages[" + page + "]/colorspaces");

        if (colorspacecount > 0) {
            System.out.println("Page " + (page + 1));

            System.out.print("  " + colorspacecount + " color space");
            if (colorspacecount != 1) {
                System.out.print("s");
            }
            if (colorspacecount > 0) {
                System.out.print(":");
            }
            System.out.println();

            for (int i = 0; i < colorspacecount; i++) {
                String page_colorspace_path =
                    "pages[" + page + "]/colorspaces[" + i + "]";
                print_colorspace(p, doc, page_colorspace_path);
            }
        }
    }

    /**
     * Print information about a single colorspace of a page.
     * 
     * The pCOS path for the colorspace is expected as
     * "pages[i]/colorspaces[j]", with i being the page number and j being the
     * number of the colorspace on that page.
     * 
     * @param p
     *            pCOS object
     * @param doc
     *            Document handle
     * @param page_colorspace_path
     *            The pCOS path to the colorspace of interest
     * 
     * @throws pCOSException
     */
    private void print_colorspace(pCOS p, int doc, String page_colorspace_path)
        throws pCOSException {
        
        String colorspace = p.get_string(doc, page_colorspace_path + "/name");
        System.out.println("    " + colorspace);

        int componentcount = (int) p.get_number(doc, "length:"
            + page_colorspace_path + "/components");
        System.out.println("      " + componentcount + " component"
            + (componentcount > 1 ? "s" : ""));

        if (colorspace.equals("Separation")) {
            String colorant =
                p.get_string(doc, page_colorspace_path + "/colorantname");
            System.out.println("      colorant \"" + colorant + "\"");
        }
        else if (colorspace.equals("DeviceN")) {
            int colorantcount = (int) p.get_number(doc, "length:"
                + page_colorspace_path + "/colorantnames");
            System.out.println("      colorants:");
            for (int j = 0; j < colorantcount; j += 1) {
                System.out.println("        \""
                    + p.get_string(doc,
                        page_colorspace_path + "/colorantnames[" + j + "]")
                    + "\"");
            }
        }
        else if (colorspace.equals("Indexed")) {
            int baseid = (int) p.get_number(doc, 
                page_colorspace_path + "/baseid");
            System.out.println("      base color space: " +
                p.get_string(doc, "colorspaces[" + baseid + "]/name"));
        }
    }

    public page_colors(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[]) {
        page_colors example = new page_colors(argv, "Colorspaces per page",
            SEARCH_PATH, "$RCSfile: page_colors.java,v $", "$Revision: 1.3 $");
        example.execute();
    }
}
