com.github.jaiimageio.jpeg2000.impl.UUIDListBox Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jai-imageio-jpeg2000 Show documentation
Show all versions of jai-imageio-jpeg2000 Show documentation
JPEG2000 support for Java Advanced Imaging Image I/O Tools API core.
This module is licensed under the [JJ2000 license](LICENSE.txt) and
is therefore NOT compatible with the GPL 3 license. It should be
compatible with the LGPL 2.1 license.
/*
* $RCSfile: UUIDListBox.java,v $
*
*
* Copyright (c) 2005 Sun Microsystems, Inc. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistribution of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistribution in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* Neither the name of Sun Microsystems, Inc. or the names of
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* This software is provided "AS IS," without a warranty of any
* kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
* WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
* EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL
* NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF
* USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
* DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR
* ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL,
* CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND
* REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR
* INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGES.
*
* You acknowledge that this software is not designed or intended for
* use in the design, construction, operation or maintenance of any
* nuclear facility.
*
* $Revision: 1.2 $
* $Date: 2006/10/10 23:48:57 $
* $State: Exp $
*/
package com.github.jaiimageio.jpeg2000.impl;
import javax.imageio.metadata.IIOInvalidTreeException;
import javax.imageio.metadata.IIOMetadataNode;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import com.github.jaiimageio.impl.common.ImageUtil;
/** This class is defined to represent a UUID list Box of JPEG JP2
* file format. This type of box has a length, a type of "ulst". Its
* contents include the number of UUID entry and a list of 16-byte UUIDs.
*/
public class UUIDListBox extends Box {
/** The data elements of this box. */
private short num;
private byte[][] uuids;
/** Constructs a UUIDListBox
from the provided uuid number
* and uuids. The provided uuids should have a size of 16; otherwise,
* Exception
may thrown in later the process. The provided
* number should consistent with the size of the uuid array.
*/
public UUIDListBox(short num, byte[][] uuids) {
super(10 + (uuids.length << 4), 0x756c7374, null);
this.num = num;
this.uuids = uuids;
}
/** Constructs a UUIDListBox
from the provided content
* data array.
*/
public UUIDListBox(byte[] data) {
super(8 + data.length, 0x756c7374, data);
}
/** Constructs a UUIDListBox
based on the provided
* org.w3c.dom.Node
.
*/
public UUIDListBox(Node node) throws IIOInvalidTreeException {
super(node);
NodeList children = node.getChildNodes();
int index = 0;
for (int i = 0; i < children.getLength(); i++) {
Node child = children.item(i);
if ("NumberUUID".equals(child.getNodeName())) {
num = (short)Box.getShortElementValue(child);
uuids = new byte[num][];
}
}
for (int i = 0; i < children.getLength(); i++) {
Node child = children.item(i);
if ("UUID".equals(child.getNodeName()) && index < num) {
uuids[index++] = Box.getByteArrayElementValue(child);
}
}
}
/** Parses the data elements from the provided content data array. */
protected void parse(byte[] data) {
num = (short)(((data[0] & 0xFF) << 8) | (data[1] & 0xFF));
uuids = new byte[num][];
int pos = 2;
for (int i = 0; i < num; i++) {
uuids[i] = new byte[16];
System.arraycopy(data, pos, uuids[i], 0, 16);
pos += 16;
}
}
/** Creates an IIOMetadataNode
from this UUID list
* box. The format of this node is defined in the XML dtd and xsd
* for the JP2 image file.
*/
public IIOMetadataNode getNativeNode() {
IIOMetadataNode node = new IIOMetadataNode(Box.getName(getType()));
setDefaultAttributes(node);
IIOMetadataNode child = new IIOMetadataNode("NumberUUID");
child.setUserObject(new Short(num));
child.setNodeValue("" + num);
node.appendChild(child);
for (int i = 0; i < num; i++) {
child = new IIOMetadataNode("UUID");
child.setUserObject(uuids[i]);
child.setNodeValue(ImageUtil.convertObjectToString(uuids[i]));
node.appendChild(child);
}
return node;
}
protected void compose() {
if (data != null)
return;
data = new byte[2 + num * 16];
data[0] = (byte)(num >> 8);
data[1] = (byte)(num & 0xFF);
for (int i = 0, pos = 2; i < num; i++) {
System.arraycopy(uuids[i], 0, data, pos, 16);
pos += 16;
}
}
}