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