package com.pdflib.cookbook.pcos.resources;

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: colorspaces.java,v 1.7 2007/09/20 14:39:37 stm Exp $
 */
public class colorspaces 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 colorspacecount = (int) p.get_number(doc, "length:colorspaces");
        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 colorspace =
                p.get_string(doc, "colorspaces[" + i + "]/name");
            System.out.println("  " + colorspace);

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

        p.close_document(doc);
    }

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