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.sun.media.imageioimpl.plugins.jpeg2000; 046 047import javax.imageio.metadata.IIOInvalidTreeException; 048import javax.imageio.metadata.IIOMetadataNode; 049import org.w3c.dom.Node; 050import 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 */ 058public 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) throws IIOInvalidTreeException { 085 super(node); 086 NodeList children = node.getChildNodes(); 087 int len = children.getLength() / 3; 088 components = new short[len]; 089 type = new byte[len]; 090 map = new byte[len]; 091 092 len *= 3; 093 int index = 0; 094 095 for (int i = 0; i < len; i++) { 096 Node child = children.item(i); 097 String name = child.getNodeName(); 098 099 if ("Component".equals(name)) { 100 components[index] = Box.getShortElementValue(child); 101 } 102 103 if ("ComponentType".equals(name)) { 104 type[index] = Box.getByteElementValue(child); 105 } 106 107 if ("ComponentAssociation".equals(name)) { 108 map[index++] = Box.getByteElementValue(child); 109 } 110 } 111 } 112 113 /** Parse the component mapping from the provided content data array. */ 114 protected void parse(byte[] data) { 115 int len = data.length / 4; 116 components = new short[len]; 117 type = new byte[len]; 118 map = new byte[len]; 119 120 for (int i = 0, j = 0; i < len; i++) { 121 components[i] = 122 (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.getName(getType())); 134 setDefaultAttributes(node); 135 136 for (int i = 0; i < components.length; i++) { 137 IIOMetadataNode child = new IIOMetadataNode("Component"); 138 Short obj = new Short(components[i]); 139 child.setUserObject(new Short(components[i])); 140 child.setNodeValue("" + components[i]); 141 node.appendChild(child); 142 143 child = new IIOMetadataNode("ComponentType"); 144 child.setUserObject(new Byte(type[i])); 145 child.setNodeValue("" + type[i]); 146 node.appendChild(child); 147 148 child = new IIOMetadataNode("ComponentAssociation"); 149 child.setUserObject(new Byte(map[i])); 150 child.setNodeValue("" + map[i]); 151 node.appendChild(child); 152 } 153 154 return node; 155 } 156 157 public short[] getComponent() { 158 return components; 159 } 160 161 public byte[] getComponentType() { 162 return type; 163 } 164 165 public byte[] getComponentAssociation() { 166 return map; 167 } 168 169 protected void compose() { 170 if (data != null) 171 return; 172 data = new byte[type.length << 2]; 173 174 for (int i = 0, j = 0; i < type.length; i++) { 175 data[j++] = (byte)(components[i] >> 8); 176 data[j++] = (byte)(components[i] & 0xFF); 177 data[j++] = type[i]; 178 data[j++] = map[i]; 179 } 180 } 181}