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 3.x, PDFlib+PDI/PPS 7.x, TET 2.2,
 * PLOP 3.x) <br>
 * Required data: PDF document
 * 
 * @version $Id: page_colors.java,v 1.8 2010/10/27 07:48:02 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.pcos_get_string(doc, "filename"));

        int pagecount = (int) p.pcos_get_number(doc, "length:pages");

        for (int page = 0; page < pagecount; page++) {
            analyze_colorspaces(p, doc, page);
        }
        p.close_document(doc);
    }

    /**
     * Analyze and print the colorspaces for a given page number.
     * 
     * @param p
     *            pCOS object
     * @param doc
     *            Document handle
     * @param page
     *            Page number
     * 
     * @throws pCOSException
     */
    private void analyze_colorspaces(pCOS p, int doc, int page)
        throws pCOSException {

        int colorspacecount = (int) p.pcos_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++) {
                analyze_colorspace(p, doc, 0,
                    "pages[" + page + "]/colorspaces[" + i + "]");
            }
        }
    }

    /**
     * Analyze and print information about a single colorspace.
     * 
     * @param p
     *            pCOS object
     * @param doc
     *            Document handle
     * @level
     *            Recursion level, used for indenting
     * @param page_colorspace_path
     *            The pCOS path to the colorspace of interest
     * 
     * @throws pCOSException
     */
    private void analyze_colorspace(pCOS p, int doc, int level, String colorspace_path)
            throws pCOSException {
        String prefix = get_prefix(level);
        
        String colorspace = p.pcos_get_string(doc, colorspace_path + "/name");
        System.out.println(prefix + "  " + colorspace);

        int componentcount = (int) p.pcos_get_number(doc, "length:"
            + colorspace_path + "/components");
        System.out.println(prefix + "    " + componentcount + " component"
            + (componentcount > 1 ? "s" : ""));

        if (colorspace.equals("ICCBased")) {
            System.out.println(prefix + "    number of ICC channels: "
                + (int) p.pcos_get_number(doc, colorspace_path
                    + "/csarray[1]/N"));
            System.out.println(prefix + "    size of ICC profile in bytes: "
                + (int) p.pcos_get_number(doc, colorspace_path
                    + "/csarray[1]/Length"));
        }
        else if (colorspace.equals("Lab")) {
            System.out.print(prefix + "    WhitePoint: ");
            for (int j = 0; j < 3; j += 1) {
                System.out.print(p.pcos_get_number(doc, colorspace_path
                    + "/csarray[1]/WhitePoint[" + j + "]")
                    + " ");
            }
            System.out.println();
        }
        else if (colorspace.equals("CalGray") || colorspace.equals("CalRGB")) {
            System.out.print(prefix + "    WhitePoint: ");
            for (int j = 0; j < 3; j += 1) {
                System.out.print(p.pcos_get_number(doc, colorspace_path
                    + "/csarray[1]/WhitePoint[" + j + "]")
                    + " ");
            }
            System.out.println();
        }
        else if (colorspace.equals("Separation")) {
            String colorant = p.pcos_get_string(doc, colorspace_path
                + "/colorantname");
            System.out.println(prefix + "    colorant \"" + colorant + "\"");
            int alternateid = (int) p.pcos_get_number(doc, colorspace_path
                + "/alternateid");
            System.out.println(prefix + "    alternate color space:");
            analyze_colorspace(p, doc, level + 1, "colorspaces[" + alternateid + "]");
        }
        else if (colorspace.equals("DeviceN")) {
            int colorantcount = (int) p.pcos_get_number(doc, "length:"
                + colorspace_path + "/colorantnames");
            System.out.println(prefix + "    colorants:");
            for (int j = 0; j < colorantcount; j += 1) {
                System.out.println(prefix + "      \""
                    + p.pcos_get_string(doc, colorspace_path
                        + "/colorantnames[" + j + "]") + "\"");
            }
            int alternateid = (int) p.pcos_get_number(doc, colorspace_path
                + "/alternateid");
            System.out.println(prefix + "    alternate color space:");
            analyze_colorspace(p, doc, level + 1, "colorspaces[" + alternateid + "]");
        }
        else if (colorspace.equals("Indexed")) {
            int palettesize = (int) p.pcos_get_number(doc, colorspace_path
                + "/csarray[2]");
            System.out.println(prefix + "    palette size: " + palettesize);

            int baseid = (int) p.pcos_get_number(doc, colorspace_path
                + "/baseid");
            System.out.println(prefix + "    base color space: ");
            analyze_colorspace(p, doc, level + 1, "colorspaces[" + baseid + "]");
        }
    }

    /**
     * Get a prefix string consisting of spaces that can be used to indent
     * lines.
     * 
     * @param level
     *            indentation level
     * @return
     *            indentation string          
     * 
     * @throws pCOSException
     */
    private String get_prefix(int level) {
        StringBuilder prefix = new StringBuilder();
        
        for (int i = 0; i < level; i += 1) {
            prefix.append("    ");
        }
        
        return prefix.toString();
    }

    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.8 $");
        example.execute();
    }
}

