pCOS Cookbook

cookbook

resources/colorspaces

List color spaces.

Download Java Code     Show Output     Show Input PDF

package com.pdflib.cookbook.pcos.resources;

import com.pdflib.IpCOS;
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.17 2016/06/21 10:59:54 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(IpCOS p, int doc) throws Exception {
        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();

        int id;
        for (id = 0; id < colorspacecount; id++) {
            print_colorspace(p, doc, 0, "colorspaces[" + id + "]");
        }
        
        System.out.println();
        
        int patterncount = (int) p.pcos_get_number(doc, "length:patterns");
        System.out.print(patterncount + " pattern");
        if (patterncount != 1) {
            System.out.print("s");
        }
        if (patterncount > 0) {
            System.out.print(":");
        }
        System.out.println();
        
        for (id = 0; id < patterncount; id++) {
            print_pattern(p, doc, "patterns[" + id + "]");
        }
    }

    /**
     * Analyze and print information about a single colorspace.
     * 
     * @param p
     *            IpCOS object
     * @param doc
     *            Document handle
     * @level
     *            Recursion level, used for indenting
     * @param colorspace_path
     *            The pCOS path to the colorspace of interest
     * 
     * @throws Exception
     */
    private void print_colorspace(IpCOS p, int doc, int level, String colorspace_path)
        throws Exception {
        String prefix = get_prefix(level);
        
        String colorspace = p.pcos_get_string(doc, colorspace_path + "/name");
        System.out.println(prefix + "  " + colorspace);
        
        int pcosinterface = (int) p.pcos_get_number(doc, "pcosinterface");
        
        int componentcount = (int) p.pcos_get_number(doc,
            colorspace_path + "/components");
        System.out.println(prefix + "    " + componentcount + " component"
            + (componentcount == 1 ? "" : "s"));
        
        if (colorspace.equals("ICCBased")) {
            System.out.println(prefix + "    size of ICC profile in bytes: "
                + (int) p.pcos_get_number(doc, colorspace_path + "/Length"));
            
            if (pcosinterface >= 10) {
                int iccprofileid = (int) p.pcos_get_number(doc,
                    colorspace_path + "/iccprofileid");
                
                String errormessage = p.pcos_get_string(doc,
                    "iccprofiles[" + iccprofileid + "]/errormessage");
                
                /* Check whether the embedded profile is damaged */
                if (errormessage != null && errormessage.length() > 0) {
                    System.out.println(" (" + errormessage + ")");
                }
                else {
                    String profilename = p.pcos_get_string(doc,
                        "iccprofiles[" + iccprofileid + "]/profilename");
                    System.out.println(prefix + "    name: '" + profilename + "'");
                    
                    String profilecs = p.pcos_get_string(doc,
                        "iccprofiles[" + iccprofileid + "]/profilecs");
                    System.out.println(prefix + "    cs: '" + profilecs + "'");
                    
                    String deviceclass = p.pcos_get_string(doc,
                        "iccprofiles[" + iccprofileid + "]/deviceclass");
                    System.out.println(prefix + "    deviceclass: '" + deviceclass + "'");
                    
                    String iccversion = p.pcos_get_string(doc,
                        "iccprofiles[" + iccprofileid + "]/iccversion");
                    System.out.println(prefix + "    iccversion: '" + iccversion + "'");
                }
            }
        }
        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:");
            print_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:");
            print_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: ");
            print_colorspace(p, doc, level + 1, "colorspaces[" + baseid + "]");
        }
        else if (colorspace.equals("Pattern")) {
            /*
             * Check whether the Pattern color space has an underlying color
             * space, and print it recursively if present.
             * 
             * This only works with a pcosinterface >= 10, for pcosinterface
             * < 10 Pattern colorspaces are not present in the colorspaces[]
             * array.
             */
            int baseid = (int) p.pcos_get_number(doc, colorspace_path
                + "/baseid");
            if (baseid != -1) {
                System.out.println(prefix + "    underlying color space:");
                print_colorspace(p, doc, level + 1, "colorspaces[" + baseid + "]");
            }            
        }
    }
    
    /**
     * Analyze and print information about a single pattern.
     * 
     * @param p
     *            IpCOS object
     * @param doc
     *            Document handle
     * @param pattern_path
     *            The pCOS path to the pattern of interest
     * 
     * @throws Exception
     */
    private void print_pattern(IpCOS p, int doc, String pattern_path)
            throws Exception {
        int patterntype = (int) p.pcos_get_number(doc, pattern_path + "/PatternType");
        
        switch (patterntype)
        {
        case 1:
            System.out.print("  Tiling Pattern ");
            int painttype =  (int) p.pcos_get_number(doc, pattern_path + "/PaintType");
            switch (painttype)
            {
            case 1:
                System.out.println("(colored)");
                break;
            case 2:
                System.out.println("(uncolored)");
                break;
            default:
                System.out.println("(unknown PaintType " + painttype + ")");
                break;
            }
            break;
            
        case 2:
            System.out.println("  Shading Pattern");
            break;
            
        default:
            System.out.println("  Pattern with unknown PatternType " + patterntype);
            break;
        }
    }

    /**
     * Get a prefix string consisting of spaces that can be used to indent
     * lines.
     * 
     * @param level
     *            indentation level
     * @return
     *            indentation string          
     * 
     * @throws Exception
     */
    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.17 $");
        example.execute();
    }
}