001: /*
002: * $RCSfile: PCXMetadata.java,v $
003: *
004: *
005: * Copyright (c) 2007 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: 2007/09/07 19:12:25 $
043: * $State: Exp $
044: */
045: package com.sun.media.imageioimpl.plugins.pcx;
046:
047: import javax.imageio.metadata.IIOInvalidTreeException;
048: import javax.imageio.metadata.IIOMetadata;
049: import javax.imageio.metadata.IIOMetadataFormatImpl;
050: import javax.imageio.metadata.IIOMetadataNode;
051:
052: import org.w3c.dom.NamedNodeMap;
053: import org.w3c.dom.Node;
054:
055: import com.sun.media.imageioimpl.common.ImageUtil;
056:
057: public class PCXMetadata extends IIOMetadata implements Cloneable,
058: PCXConstants {
059:
060: short version;
061: byte bitsPerPixel;
062: boolean gotxmin, gotymin;
063: short xmin, ymin;
064: int vdpi, hdpi;
065: int hsize, vsize;
066:
067: PCXMetadata() {
068: super (true, null, null, null, null);
069: reset();
070: }
071:
072: public Node getAsTree(String formatName) {
073: if (formatName
074: .equals(IIOMetadataFormatImpl.standardMetadataFormatName)) {
075: return getStandardTree();
076: } else {
077: throw new IllegalArgumentException(
078: "Not a recognized format!");
079: }
080: }
081:
082: public boolean isReadOnly() {
083: return false;
084: }
085:
086: public void mergeTree(String formatName, Node root)
087: throws IIOInvalidTreeException {
088: if (formatName
089: .equals(IIOMetadataFormatImpl.standardMetadataFormatName)) {
090: if (root == null) {
091: throw new IllegalArgumentException("root == null!");
092: }
093: mergeStandardTree(root);
094: } else {
095: throw new IllegalArgumentException(
096: "Not a recognized format!");
097: }
098: }
099:
100: public void reset() {
101: version = VERSION_3_0;
102: bitsPerPixel = 0;
103: gotxmin = false;
104: gotymin = false;
105: xmin = 0;
106: ymin = 0;
107: vdpi = 72;
108: hdpi = 72;
109: hsize = 0;
110: vsize = 0;
111: }
112:
113: public IIOMetadataNode getStandardDocumentNode() {
114: String versionString;
115: switch (version) {
116: case VERSION_2_5:
117: versionString = "2.5";
118: break;
119: case VERSION_2_8_W_PALETTE:
120: versionString = "2.8 with palette";
121: break;
122: case VERSION_2_8_WO_PALETTE:
123: versionString = "2.8 without palette";
124: break;
125: case VERSION_PC_WINDOWS:
126: versionString = "PC Paintbrush for Windows";
127: break;
128: case VERSION_3_0:
129: versionString = "3.0";
130: break;
131: default:
132: // unknown
133: versionString = null;
134: }
135:
136: IIOMetadataNode documentNode = null;
137: if (versionString != null) {
138: documentNode = new IIOMetadataNode("Document");
139: IIOMetadataNode node = new IIOMetadataNode("FormatVersion");
140: node.setAttribute("value", versionString);
141: documentNode.appendChild(node);
142: }
143:
144: return documentNode;
145: }
146:
147: public IIOMetadataNode getStandardDimensionNode() {
148: IIOMetadataNode dimensionNode = new IIOMetadataNode("Dimension");
149: IIOMetadataNode node = null; // scratch node
150:
151: node = new IIOMetadataNode("HorizontalPixelOffset");
152: node.setAttribute("value", String.valueOf(xmin));
153: dimensionNode.appendChild(node);
154:
155: node = new IIOMetadataNode("VerticalPixelOffset");
156: node.setAttribute("value", String.valueOf(ymin));
157: dimensionNode.appendChild(node);
158:
159: node = new IIOMetadataNode("HorizontalPixelSize");
160: node.setAttribute("value", String.valueOf(254.0 / hdpi));
161: dimensionNode.appendChild(node);
162:
163: node = new IIOMetadataNode("VerticalPixelSize");
164: node.setAttribute("value", String.valueOf(254.0 / vdpi));
165: dimensionNode.appendChild(node);
166:
167: if (hsize != 0) {
168: node = new IIOMetadataNode("HorizontalScreenSize");
169: node.setAttribute("value", String.valueOf(hsize));
170: dimensionNode.appendChild(node);
171: }
172:
173: if (vsize != 0) {
174: node = new IIOMetadataNode("VerticalScreenSize");
175: node.setAttribute("value", String.valueOf(vsize));
176: dimensionNode.appendChild(node);
177: }
178:
179: return dimensionNode;
180: }
181:
182: private void mergeStandardTree(Node root)
183: throws IIOInvalidTreeException {
184: Node node = root;
185: if (!node.getNodeName().equals(
186: IIOMetadataFormatImpl.standardMetadataFormatName))
187: throw new IIOInvalidTreeException("Root must be "
188: + IIOMetadataFormatImpl.standardMetadataFormatName,
189: node);
190:
191: node = node.getFirstChild();
192: while (node != null) {
193: String name = node.getNodeName();
194:
195: if (name.equals("Dimension")) {
196: Node child = node.getFirstChild();
197:
198: while (child != null) {
199: String childName = child.getNodeName();
200: if (childName.equals("HorizontalPixelOffset")) {
201: String hpo = getAttribute(child, "value");
202: xmin = Short.valueOf(hpo).shortValue();
203: gotxmin = true;
204: } else if (childName.equals("VerticalPixelOffset")) {
205: String vpo = getAttribute(child, "value");
206: ymin = Short.valueOf(vpo).shortValue();
207: gotymin = true;
208: } else if (childName.equals("HorizontalPixelSize")) {
209: String hps = getAttribute(child, "value");
210: hdpi = (int) (254.0F / Float.parseFloat(hps) + 0.5F);
211: } else if (childName.equals("VerticalPixelSize")) {
212: String vps = getAttribute(child, "value");
213: vdpi = (int) (254.0F / Float.parseFloat(vps) + 0.5F);
214: } else if (childName.equals("HorizontalScreenSize")) {
215: String hss = getAttribute(child, "value");
216: hsize = Integer.valueOf(hss).intValue();
217: } else if (childName.equals("VerticalScreenSize")) {
218: String vss = getAttribute(child, "value");
219: vsize = Integer.valueOf(vss).intValue();
220: }
221:
222: child = child.getNextSibling();
223: }
224: }
225:
226: node = node.getNextSibling();
227: }
228: }
229:
230: private static String getAttribute(Node node, String attrName) {
231: NamedNodeMap attrs = node.getAttributes();
232: Node attr = attrs.getNamedItem(attrName);
233: return attr != null ? attr.getNodeValue() : null;
234: }
235: }
|