001: /*
002: * $RCSfile: HeaderBox.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 an Image Header Box of JPEG JP2 file
053: * format. An Image Header Box has a length, and a fixed type of "ihdr".
054: *
055: * The content of an image header box contains the width/height, number of
056: * image components, the bit depth (coded with sign/unsign information),
057: * the compression type (7 for JP2 file), the flag to indicate the color
058: * space is known or not, and a flag to indicate whether the intellectual
059: * property information included in this file.
060: */
061: public class HeaderBox extends Box {
062: /** Cache the element names for this box's xml definition */
063: private static String[] elementNames = { "Height", "Width",
064: "NumComponents", "BitDepth", "CompressionType",
065: "UnknownColorspace", "IntellectualProperty" };
066:
067: /** This method will be called by the getNativeNodeForSimpleBox of the
068: * class Box to get the element names.
069: */
070: public static String[] getElementNames() {
071: return elementNames;
072: }
073:
074: /** The element values. */
075: private int width;
076: private int height;
077: private short numComp;
078: private byte bitDepth;
079: private byte compressionType;
080: private byte unknownColor;
081: private byte intelProp;
082:
083: /** Create an Image Header Box from the element values. */
084: public HeaderBox(int height, int width, int numComp, int bitDepth,
085: int compressionType, int unknownColor, int intelProp) {
086: super (22, 0x69686472, null);
087: this .height = height;
088: this .width = width;
089: this .numComp = (short) numComp;
090: this .bitDepth = (byte) bitDepth;
091: this .compressionType = (byte) compressionType;
092: this .unknownColor = (byte) unknownColor;
093: this .intelProp = (byte) intelProp;
094: }
095:
096: /** Create an Image Header Box using the content data. */
097: public HeaderBox(byte[] data) {
098: super (8 + data.length, 0x69686472, data);
099: }
100:
101: /** Constructs an Image Header Box from a Node. */
102: public HeaderBox(Node node) throws IIOInvalidTreeException {
103: super (node);
104: NodeList children = node.getChildNodes();
105:
106: for (int i = 0; i < children.getLength(); i++) {
107: Node child = children.item(i);
108: String name = child.getNodeName();
109:
110: if ("Height".equals(name)) {
111: height = Box.getIntElementValue(child);
112: }
113:
114: if ("Width".equals(name)) {
115: width = Box.getIntElementValue(child);
116: }
117:
118: if ("NumComponents".equals(name)) {
119: numComp = Box.getShortElementValue(child);
120: }
121:
122: if ("BitDepth".equals(name)) {
123: bitDepth = Box.getByteElementValue(child);
124: }
125:
126: if ("CompressionType".equals(name)) {
127: compressionType = Box.getByteElementValue(child);
128: }
129:
130: if ("UnknownColorspace".equals(name)) {
131: unknownColor = Box.getByteElementValue(child);
132: }
133:
134: if ("IntellectualProperty".equals(name)) {
135: intelProp = Box.getByteElementValue(child);
136: }
137: }
138: }
139:
140: /** Parse the data elements from the byte array of the content. */
141: protected void parse(byte[] data) {
142: height = ((data[0] & 0xFF) << 24) | ((data[1] & 0xFF) << 16)
143: | ((data[2] & 0xFF) << 8) | (data[3] & 0xFF);
144: width = ((data[4] & 0xFF) << 24) | ((data[5] & 0xFF) << 16)
145: | ((data[6] & 0xFF) << 8) | (data[7] & 0xFF);
146: numComp = (short) (((data[8] & 0xFF) << 8) | (data[9] & 0xFF));
147: bitDepth = data[10];
148: compressionType = data[11];
149: unknownColor = data[12];
150: intelProp = data[13];
151: }
152:
153: /** Returns the height of the image. */
154: public int getHeight() {
155: return height;
156: }
157:
158: /** Returns the width of the image. */
159: public int getWidth() {
160: return width;
161: }
162:
163: /** Returns the number of image components. */
164: public short getNumComponents() {
165: return numComp;
166: }
167:
168: /** Returns the compression type. */
169: public byte getCompressionType() {
170: return compressionType;
171: }
172:
173: /** Returns the bit depth for all the image components. */
174: public byte getBitDepth() {
175: return bitDepth;
176: }
177:
178: /** Returns the <code>UnknowColorspace</code> flag. */
179: public byte getUnknownColorspace() {
180: return unknownColor;
181: }
182:
183: /** Returns the <code>IntellectualProperty</code> flag. */
184: public byte getIntellectualProperty() {
185: return intelProp;
186: }
187:
188: /** Creates an <code>IIOMetadataNode</code> from this image header box.
189: * The format of this node is defined in the XML dtd and xsd
190: * for the JP2 image file.
191: */
192: public IIOMetadataNode getNativeNode() {
193: return getNativeNodeForSimpleBox();
194: }
195:
196: protected void compose() {
197: if (data != null)
198: return;
199: data = new byte[14];
200: copyInt(data, 0, height);
201: copyInt(data, 4, width);
202:
203: data[8] = (byte) (numComp >> 8);
204: data[9] = (byte) (numComp & 0xFF);
205: data[10] = bitDepth;
206: data[11] = compressionType;
207: data[12] = unknownColor;
208: data[13] = intelProp;
209: }
210: }
|