001: /*
002: * $RCSfile: GIFWritableStreamMetadata.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.2 $
042: * $Date: 2006/03/24 22:30:11 $
043: * $State: Exp $
044: */
045:
046: package com.sun.media.imageioimpl.plugins.gif;
047:
048: /*
049: * The source for this class was copied verbatim from the source for
050: * package com.sun.imageio.plugins.gif.GIFImageMetadata and then modified
051: * to make the class read-write capable.
052: */
053:
054: import javax.imageio.ImageTypeSpecifier;
055: import javax.imageio.metadata.IIOInvalidTreeException;
056: import javax.imageio.metadata.IIOMetadata;
057: import javax.imageio.metadata.IIOMetadataNode;
058: import javax.imageio.metadata.IIOMetadataFormat;
059: import javax.imageio.metadata.IIOMetadataFormatImpl;
060: import org.w3c.dom.Node;
061:
062: class GIFWritableStreamMetadata extends GIFStreamMetadata {
063:
064: // package scope
065: static final String NATIVE_FORMAT_NAME = "javax_imageio_gif_stream_1.0";
066:
067: public GIFWritableStreamMetadata() {
068: super (
069: true,
070: NATIVE_FORMAT_NAME,
071: "com.sun.media.imageioimpl.plugins.gif.GIFStreamMetadataFormat",
072: null, null);
073:
074: // initialize metadata fields by default values
075: reset();
076: }
077:
078: public boolean isReadOnly() {
079: return false;
080: }
081:
082: public void mergeTree(String formatName, Node root)
083: throws IIOInvalidTreeException {
084: if (formatName.equals(nativeMetadataFormatName)) {
085: if (root == null) {
086: throw new IllegalArgumentException("root == null!");
087: }
088: mergeNativeTree(root);
089: } else if (formatName
090: .equals(IIOMetadataFormatImpl.standardMetadataFormatName)) {
091: if (root == null) {
092: throw new IllegalArgumentException("root == null!");
093: }
094: mergeStandardTree(root);
095: } else {
096: throw new IllegalArgumentException(
097: "Not a recognized format!");
098: }
099: }
100:
101: public void reset() {
102: version = null;
103:
104: logicalScreenWidth = UNDEFINED_INTEGER_VALUE;
105: logicalScreenHeight = UNDEFINED_INTEGER_VALUE;
106: colorResolution = UNDEFINED_INTEGER_VALUE;
107: pixelAspectRatio = 0;
108:
109: backgroundColorIndex = 0;
110: sortFlag = false;
111: globalColorTable = null;
112: }
113:
114: protected void mergeNativeTree(Node root)
115: throws IIOInvalidTreeException {
116: Node node = root;
117: if (!node.getNodeName().equals(nativeMetadataFormatName)) {
118: fatal(node, "Root must be " + nativeMetadataFormatName);
119: }
120:
121: node = node.getFirstChild();
122: while (node != null) {
123: String name = node.getNodeName();
124:
125: if (name.equals("Version")) {
126: version = getStringAttribute(node, "value", null, true,
127: versionStrings);
128: } else if (name.equals("LogicalScreenDescriptor")) {
129: /* NB: At the moment we use empty strings to support undefined
130: * integer values in tree representation.
131: * We need to add better support for undefined/default values
132: * later.
133: */
134: logicalScreenWidth = getIntAttribute(node,
135: "logicalScreenWidth", UNDEFINED_INTEGER_VALUE,
136: true, true, 1, 65535);
137:
138: logicalScreenHeight = getIntAttribute(node,
139: "logicalScreenHeight", UNDEFINED_INTEGER_VALUE,
140: true, true, 1, 65535);
141:
142: colorResolution = getIntAttribute(node,
143: "colorResolution", UNDEFINED_INTEGER_VALUE,
144: true, true, 1, 8);
145:
146: pixelAspectRatio = getIntAttribute(node,
147: "pixelAspectRatio", 0, true, true, 0, 255);
148: } else if (name.equals("GlobalColorTable")) {
149: int sizeOfGlobalColorTable = getIntAttribute(node,
150: "sizeOfGlobalColorTable", true, 2, 256);
151: if (sizeOfGlobalColorTable != 2
152: && sizeOfGlobalColorTable != 4
153: && sizeOfGlobalColorTable != 8
154: && sizeOfGlobalColorTable != 16
155: && sizeOfGlobalColorTable != 32
156: && sizeOfGlobalColorTable != 64
157: && sizeOfGlobalColorTable != 128
158: && sizeOfGlobalColorTable != 256) {
159: fatal(node,
160: "Bad value for GlobalColorTable attribute sizeOfGlobalColorTable!");
161: }
162:
163: backgroundColorIndex = getIntAttribute(node,
164: "backgroundColorIndex", 0, true, true, 0, 255);
165:
166: sortFlag = getBooleanAttribute(node, "sortFlag", false,
167: true);
168:
169: globalColorTable = getColorTable(node,
170: "ColorTableEntry", true, sizeOfGlobalColorTable);
171: } else {
172: fatal(node, "Unknown child of root node!");
173: }
174:
175: node = node.getNextSibling();
176: }
177: }
178:
179: protected void mergeStandardTree(Node root)
180: throws IIOInvalidTreeException {
181: Node node = root;
182: if (!node.getNodeName().equals(
183: IIOMetadataFormatImpl.standardMetadataFormatName)) {
184: fatal(node, "Root must be "
185: + IIOMetadataFormatImpl.standardMetadataFormatName);
186: }
187:
188: node = node.getFirstChild();
189: while (node != null) {
190: String name = node.getNodeName();
191:
192: if (name.equals("Chroma")) {
193: Node childNode = node.getFirstChild();
194: while (childNode != null) {
195: String childName = childNode.getNodeName();
196: if (childName.equals("Palette")) {
197: globalColorTable = getColorTable(childNode,
198: "PaletteEntry", false, -1);
199:
200: } else if (childName.equals("BackgroundIndex")) {
201: backgroundColorIndex = getIntAttribute(
202: childNode, "value", -1, true, true, 0,
203: 255);
204: }
205: childNode = childNode.getNextSibling();
206: }
207: } else if (name.equals("Data")) {
208: Node childNode = node.getFirstChild();
209: while (childNode != null) {
210: String childName = childNode.getNodeName();
211: if (childName.equals("BitsPerSample")) {
212: colorResolution = getIntAttribute(childNode,
213: "value", -1, true, true, 1, 8);
214: break;
215: }
216: childNode = childNode.getNextSibling();
217: }
218: } else if (name.equals("Dimension")) {
219: Node childNode = node.getFirstChild();
220: while (childNode != null) {
221: String childName = childNode.getNodeName();
222: if (childName.equals("PixelAspectRatio")) {
223: float aspectRatio = getFloatAttribute(
224: childNode, "value");
225: if (aspectRatio == 1.0F) {
226: pixelAspectRatio = 0;
227: } else {
228: int ratio = (int) (aspectRatio * 64.0F - 15.0F);
229: pixelAspectRatio = Math.max(Math.min(ratio,
230: 255), 0);
231: }
232: } else if (childName.equals("HorizontalScreenSize")) {
233: logicalScreenWidth = getIntAttribute(childNode,
234: "value", -1, true, true, 1, 65535);
235: } else if (childName.equals("VerticalScreenSize")) {
236: logicalScreenHeight = getIntAttribute(
237: childNode, "value", -1, true, true, 1,
238: 65535);
239: }
240: childNode = childNode.getNextSibling();
241: }
242: } else if (name.equals("Document")) {
243: Node childNode = node.getFirstChild();
244: while (childNode != null) {
245: String childName = childNode.getNodeName();
246: if (childName.equals("FormatVersion")) {
247: String formatVersion = getStringAttribute(
248: childNode, "value", null, true, null);
249: for (int i = 0; i < versionStrings.length; i++) {
250: if (formatVersion.equals(versionStrings[i])) {
251: version = formatVersion;
252: break;
253: }
254: }
255: break;
256: }
257: childNode = childNode.getNextSibling();
258: }
259: }
260:
261: node = node.getNextSibling();
262: }
263: }
264:
265: public void setFromTree(String formatName, Node root)
266: throws IIOInvalidTreeException {
267: reset();
268: mergeTree(formatName, root);
269: }
270: }
|