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 3.x, PDFlib+PDI/PPS 7.x, TET 2.2,
 * PLOP 3.x) <br>
 * Required data: PDF document
 * 
 * @version $Id: colorspaces.java,v 1.13 2010/10/27 07:48:04 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.pcos_get_string(doc, "filename"));

        int colorspacecount = (int) p
            .pcos_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 id = 0; id < colorspacecount; id++) {
            analyze_colorspace(p, doc, 0, "colorspaces[" + id + "]");
        }

        p.close_document(doc);
    }

    /**
     * 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 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.13 $");
        example.execute();
    }
}

