001/*
002 * $RCSfile: ComponentMappingBox.java,v $
003 *
004 * 
005 * Copyright (c) 2005 Sun Microsystems, Inc. All  Rights Reserved.
006 * 
007 * Redistribution and use in source and binary forms, with or without
008 * modification, are permitted provided that the following conditions
009 * are met: 
010 * 
011 * - Redistribution of source code must retain the above copyright 
012 *   notice, this  list of conditions and the following disclaimer.
013 * 
014 * - Redistribution in binary form must reproduce the above copyright
015 *   notice, this list of conditions and the following disclaimer in 
016 *   the documentation and/or other materials provided with the
017 *   distribution.
018 * 
019 * Neither the name of Sun Microsystems, Inc. or the names of 
020 * contributors may be used to endorse or promote products derived 
021 * from this software without specific prior written permission.
022 * 
023 * This software is provided "AS IS," without a warranty of any 
024 * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND 
025 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, 
026 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
027 * EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL 
028 * NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF 
029 * USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
030 * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR 
031 * ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL,
032 * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND
033 * REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR
034 * INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
035 * POSSIBILITY OF SUCH DAMAGES. 
036 * 
037 * You acknowledge that this software is not designed or intended for 
038 * use in the design, construction, operation or maintenance of any 
039 * nuclear facility. 
040 *
041 * $Revision: 1.1 $
042 * $Date: 2005/02/11 05:01:32 $
043 * $State: Exp $
044 */
045package com.github.jaiimageio.jpeg2000.impl;
046
047import javax.imageio.metadata.IIOInvalidTreeException;
048import javax.imageio.metadata.IIOMetadataNode;
049
050import org.w3c.dom.Node;
051import org.w3c.dom.NodeList;
052
053/** This class is defined to represent a Color Specification Box of JPEG JP2
054 *  file format.  A Channel Definition Box has a length, and a fixed type
055 *  of "cmap".  This box exists if and only is a PaletteBox exists.  Its
056 *  content defines the type LUT output components and their mapping to the
057 *  color component.
058 */
059public class ComponentMappingBox extends Box {
060    /** The data elements. */
061    private short[] components;
062    private byte[] type;
063    private byte[] map;
064
065    /** Constructs a <code>ComponentMappingBox</code> from the provided
066     *  content byte array.
067     */
068    public ComponentMappingBox(byte[] data) {
069        super(8 + data.length, 0x636D6170, data);
070    }
071
072    /** Constructs a <code>ComponentMappingBox</code> from the provided
073     *  component mapping.
074     */
075    public ComponentMappingBox(short[] comp, byte[] t, byte[] m) {
076        super(8 + (comp.length << 2), 0x636D6170, null);
077        this.components = comp;
078        this.type = t;
079        this.map = m;
080    }
081
082    /** Constructs a <code>ComponentMappingBox</code> based on the provided
083     *  <code>org.w3c.dom.Node</code>.
084     */
085    public ComponentMappingBox(Node node) throws IIOInvalidTreeException {
086        super(node);
087        NodeList children = node.getChildNodes();
088        int len = children.getLength() / 3;
089        components = new short[len];
090        type = new byte[len];
091        map = new byte[len];
092
093        len *= 3;
094        int index = 0;
095
096        for (int i = 0; i < len; i++) {
097            Node child = children.item(i);
098            String name = child.getNodeName();
099
100            if ("Component".equals(name)) {
101                components[index] = Box.getShortElementValue(child);
102            }
103
104            if ("ComponentType".equals(name)) {
105                type[index] = Box.getByteElementValue(child);
106            }
107
108            if ("ComponentAssociation".equals(name)) {
109                map[index++] = Box.getByteElementValue(child);
110            }
111        }
112    }
113
114    /** Parse the component mapping from the provided content data array. */
115    protected void parse(byte[] data) {
116        int len = data.length / 4;
117        components = new short[len];
118        type = new byte[len];
119        map = new byte[len];
120
121        for (int i = 0, j = 0; i < len; i++) {
122            components[i] =
123                (short)(((data[j++] & 0xFF) << 8) | (data[j++] & 0xFF));
124            type[i] = data[j++];
125            map[i] = data[j++];
126        }
127    }
128
129    /** Creates an <code>IIOMetadataNode</code> from this component mapping
130     *  box.  The format of this node is defined in the XML dtd and xsd
131     *  for the JP2 image file.
132     */
133    public IIOMetadataNode getNativeNode() {
134        IIOMetadataNode node = new IIOMetadataNode(Box.getName(getType()));
135        setDefaultAttributes(node);
136
137        for (int i = 0; i < components.length; i++) {
138            IIOMetadataNode child = new IIOMetadataNode("Component");
139            Short obj = new Short(components[i]);
140            child.setUserObject(new Short(components[i]));
141            child.setNodeValue("" + components[i]);
142            node.appendChild(child);
143
144            child = new IIOMetadataNode("ComponentType");
145            child.setUserObject(new Byte(type[i]));
146            child.setNodeValue("" + type[i]);
147            node.appendChild(child);
148
149            child = new IIOMetadataNode("ComponentAssociation");
150            child.setUserObject(new Byte(map[i]));
151            child.setNodeValue("" + map[i]);
152            node.appendChild(child);
153        }
154
155        return node;
156    }
157
158    public short[] getComponent() {
159        return components;
160    }
161
162    public byte[] getComponentType() {
163        return type;
164    }
165
166    public byte[] getComponentAssociation() {
167        return map;
168    }
169
170    protected void compose() {
171        if (data != null)
172            return;
173        data = new byte[type.length << 2];
174
175        for (int i = 0, j = 0; i < type.length; i++) {
176            data[j++] = (byte)(components[i] >> 8);
177            data[j++] = (byte)(components[i] & 0xFF);
178            data[j++] = type[i];
179            data[j++] = map[i];
180        }
181    }
182}