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: */
045: package com.sun.media.imageioimpl.plugins.jpeg2000;
046:
047: import javax.imageio.metadata.IIOInvalidTreeException;
048: import javax.imageio.metadata.IIOMetadataNode;
049: import org.w3c.dom.Node;
050: import org.w3c.dom.NodeList;
051:
052: /** This class is defined to represent a Color Specification Box of JPEG JP2
053: * file format. A Channel Definition Box has a length, and a fixed type
054: * of "cmap". This box exists if and only is a PaletteBox exists. Its
055: * content defines the type LUT output components and their mapping to the
056: * color component.
057: */
058: public class ComponentMappingBox extends Box {
059: /** The data elements. */
060: private short[] components;
061: private byte[] type;
062: private byte[] map;
063:
064: /** Constructs a <code>ComponentMappingBox</code> from the provided
065: * content byte array.
066: */
067: public ComponentMappingBox(byte[] data) {
068: super (8 + data.length, 0x636D6170, data);
069: }
070:
071: /** Constructs a <code>ComponentMappingBox</code> from the provided
072: * component mapping.
073: */
074: public ComponentMappingBox(short[] comp, byte[] t, byte[] m) {
075: super (8 + (comp.length << 2), 0x636D6170, null);
076: this .components = comp;
077: this .type = t;
078: this .map = m;
079: }
080:
081: /** Constructs a <code>ComponentMappingBox</code> based on the provided
082: * <code>org.w3c.dom.Node</code>.
083: */
084: public ComponentMappingBox(Node node)
085: 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] = (short) (((data[j++] & 0xFF) << 8) | (data[j++] & 0xFF));
123: type[i] = data[j++];
124: map[i] = data[j++];
125: }
126: }
127:
128: /** Creates an <code>IIOMetadataNode</code> from this component mapping
129: * box. The format of this node is defined in the XML dtd and xsd
130: * for the JP2 image file.
131: */
132: public IIOMetadataNode getNativeNode() {
133: IIOMetadataNode node = new IIOMetadataNode(Box
134: .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: }
|