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