001/* 002 * $RCSfile: ColorSpecificationBox.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 java.awt.color.ICC_Profile; 048 049import javax.imageio.metadata.IIOInvalidTreeException; 050import javax.imageio.metadata.IIOMetadataNode; 051 052import org.w3c.dom.Node; 053import org.w3c.dom.NodeList; 054 055/** This class is defined to represent a Color Specification Box of JPEG JP2 056 * file format. A Channel Definition Box has a length, and a fixed type 057 * of "colr". Its content contains the method to define the color space, 058 * the precedence and approximation accuracy (0 for JP2 files), the 059 * enumerated color space, and the ICC color profile if any. 060 */ 061public class ColorSpecificationBox extends Box { 062 /** The enumerated color space defined in JP2 file format. */ 063 public static final int ECS_sRGB = 16; 064 public static final int ECS_GRAY = 17; 065 public static final int ECS_YCC = 18; 066 067 /** Cache the element names for this box's xml definition */ 068 private static String[] elementNames = {"Method", "Precedence", 069 "ApproximationAccuracy", 070 "EnumeratedColorSpace", 071 "ICCProfile"}; 072 073 /** This method will be called by the getNativeNodeForSimpleBox of the 074 * class Box to get the element names. 075 */ 076 public static String[] getElementNames() { 077 return elementNames; 078 } 079 080 /** The elements' values. */ 081 private byte method; 082 private byte precedence; 083 private byte approximation; 084 private int ecs; 085 private ICC_Profile profile; 086 087 /** Computes the length of this box when profile is present. */ 088 private static int computeLength(byte m, ICC_Profile profile) { 089 int ret = 15; 090 if (m == 2 && profile != null) { 091 ret += profile.getData().length; 092 } 093 return ret; 094 } 095 096 /** Creates a <code>ColorSpecificationBox</code> from the provided data 097 * elements. 098 */ 099 public ColorSpecificationBox(byte m, byte p, byte a, int ecs, 100 ICC_Profile profile) { 101 super(computeLength(m, profile), 0x636F6C72, null); 102 this.method = m; 103 this.precedence = p; 104 this.approximation = a; 105 this.ecs = ecs; 106 this.profile = profile; 107 } 108 109 /** Creates a <code>ColorSpecificationBox</code> from the provided byte 110 * array. 111 */ 112 public ColorSpecificationBox(byte[] data) { 113 super(8 + data.length, 0x636F6C72, data); 114 } 115 116 /** Constructs a <code>ColorSpecificationBox</code> based on the provided 117 * <code>org.w3c.dom.Node</code>. 118 */ 119 public ColorSpecificationBox(Node node) throws IIOInvalidTreeException { 120 super(node); 121 NodeList children = node.getChildNodes(); 122 123 for (int i = 0; i < children.getLength(); i++) { 124 Node child = children.item(i); 125 String name = child.getNodeName(); 126 127 if ("Method".equals(name)) { 128 method = Box.getByteElementValue(child); 129 } 130 131 if ("Precedence".equals(name)) { 132 precedence = Box.getByteElementValue(child); 133 } 134 135 if ("ApproximationAccuracy".equals(name)) { 136 approximation = Box.getByteElementValue(child); 137 } 138 139 if ("EnumeratedColorSpace".equals(name)) { 140 ecs = Box.getIntElementValue(child); 141 } 142 143 if ("ICCProfile".equals(name)) { 144 if (child instanceof IIOMetadataNode) 145 profile = 146 (ICC_Profile)((IIOMetadataNode)child).getUserObject(); 147 else { 148 String value = node.getNodeValue(); 149 if (value != null) 150 profile = ICC_Profile.getInstance(Box.parseByteArray(value)); 151 } 152 } 153 } 154 } 155 156 /** Returns the method to define the color space. */ 157 public byte getMethod() { 158 return method; 159 } 160 161 /** Returns <code>Precedence</code>. */ 162 public byte getPrecedence() { 163 return precedence; 164 } 165 166 /** Returns <code>ApproximationAccuracy</code>. */ 167 public byte getApproximationAccuracy() { 168 return approximation; 169 } 170 171 /** Returns the enumerated color space. */ 172 public int getEnumeratedColorSpace() { 173 return ecs; 174 } 175 176 /** Returns the ICC color profile in this color specification box. */ 177 public ICC_Profile getICCProfile() { 178 return profile; 179 } 180 181 /** Creates an <code>IIOMetadataNode</code> from this color specification 182 * box. The format of this node is defined in the XML dtd and xsd 183 * for the JP2 image file. 184 */ 185 public IIOMetadataNode getNativeNode() { 186 return getNativeNodeForSimpleBox(); 187 } 188 189 protected void parse(byte[] data) { 190 method = data[0]; 191 precedence = data[1]; 192 approximation = data[2]; 193 if (method == 2) { 194 byte[] proData = new byte[data.length - 3]; 195 System.arraycopy(data, 3, proData, 0, data.length - 3); 196 profile = ICC_Profile.getInstance(proData); 197 } else 198 ecs = ((data[3] & 0xFF) << 24) | ((data[4] & 0xFF) << 16) | 199 ((data[5] & 0xFF) << 8) | (data[6] & 0xFF); 200 201 } 202 203 protected void compose() { 204 if (data != null) 205 return; 206 int len = 7; 207 byte[] profileData = null; 208 if (profile != null) { 209 profileData = profile.getData(); 210 len += profileData.length; 211 } 212 213 data = new byte[len]; 214 215 data[0] = (byte)method; 216 data[1] = (byte)precedence; 217 data[2] = (byte)approximation; 218 219 copyInt(data, 3, ecs); 220 221 if (profile != null) 222 System.arraycopy(profileData, 0, data, 7, len - 7); 223 } 224}