KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > imageio > plugins > gif > GIFWritableStreamMetadata


1 /*
2  * @(#)GIFWritableStreamMetadata.java 1.4 05/11/17
3  *
4  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package com.sun.imageio.plugins.gif;
9
10 /*
11  * The source for this class was copied verbatim from the source for
12  * package com.sun.imageio.plugins.gif.GIFImageMetadata and then modified
13  * to make the class read-write capable.
14  */

15
16 import javax.imageio.ImageTypeSpecifier JavaDoc;
17 import javax.imageio.metadata.IIOInvalidTreeException JavaDoc;
18 import javax.imageio.metadata.IIOMetadata JavaDoc;
19 import javax.imageio.metadata.IIOMetadataNode JavaDoc;
20 import javax.imageio.metadata.IIOMetadataFormat JavaDoc;
21 import javax.imageio.metadata.IIOMetadataFormatImpl JavaDoc;
22 import org.w3c.dom.Node JavaDoc;
23
24 class GIFWritableStreamMetadata extends GIFStreamMetadata {
25
26     // package scope
27
static final String JavaDoc
28     NATIVE_FORMAT_NAME = "javax_imageio_gif_stream_1.0";
29
30     public GIFWritableStreamMetadata() {
31         super(true,
32               NATIVE_FORMAT_NAME,
33               "com.sun.imageio.plugins.gif.GIFStreamMetadataFormat", // XXX J2SE
34
null, null);
35
36         // initialize metadata fields by default values
37
reset();
38     }
39
40     public boolean isReadOnly() {
41         return false;
42     }
43
44     public void mergeTree(String JavaDoc formatName, Node JavaDoc root)
45       throws IIOInvalidTreeException JavaDoc {
46         if (formatName.equals(nativeMetadataFormatName)) {
47             if (root == null) {
48                 throw new IllegalArgumentException JavaDoc("root == null!");
49             }
50             mergeNativeTree(root);
51         } else if (formatName.equals
52                    (IIOMetadataFormatImpl.standardMetadataFormatName)) {
53             if (root == null) {
54                 throw new IllegalArgumentException JavaDoc("root == null!");
55             }
56             mergeStandardTree(root);
57         } else {
58             throw new IllegalArgumentException JavaDoc("Not a recognized format!");
59         }
60     }
61
62     public void reset() {
63         version = null;
64
65         logicalScreenWidth = UNDEFINED_INTEGER_VALUE;
66         logicalScreenHeight = UNDEFINED_INTEGER_VALUE;
67         colorResolution = UNDEFINED_INTEGER_VALUE;
68         pixelAspectRatio = 0;
69
70         backgroundColorIndex = 0;
71         sortFlag = false;
72         globalColorTable = null;
73     }
74
75     protected void mergeNativeTree(Node JavaDoc root) throws IIOInvalidTreeException JavaDoc {
76         Node JavaDoc node = root;
77         if (!node.getNodeName().equals(nativeMetadataFormatName)) {
78             fatal(node, "Root must be " + nativeMetadataFormatName);
79         }
80
81         node = node.getFirstChild();
82         while (node != null) {
83             String JavaDoc name = node.getNodeName();
84
85             if (name.equals("Version")) {
86                 version = getStringAttribute(node, "value", null,
87                                              true, versionStrings);
88             } else if (name.equals("LogicalScreenDescriptor")) {
89                 /* NB: At the moment we use empty strings to support undefined
90                  * integer values in tree representation.
91                  * We need to add better support for undefined/default values
92                  * later.
93                  */

94                 logicalScreenWidth = getIntAttribute(node,
95                                                      "logicalScreenWidth",
96                                                      UNDEFINED_INTEGER_VALUE,
97                                                      true,
98                                                      true, 1, 65535);
99
100                 logicalScreenHeight = getIntAttribute(node,
101                                                       "logicalScreenHeight",
102                                                       UNDEFINED_INTEGER_VALUE,
103                                                       true,
104                                                       true, 1, 65535);
105
106                 colorResolution = getIntAttribute(node,
107                                                   "colorResolution",
108                                                   UNDEFINED_INTEGER_VALUE,
109                                                   true,
110                                                   true, 1, 8);
111
112                 pixelAspectRatio = getIntAttribute(node,
113                                                    "pixelAspectRatio",
114                                                    0, true,
115                                                    true, 0, 255);
116             } else if (name.equals("GlobalColorTable")) {
117                 int sizeOfGlobalColorTable =
118                     getIntAttribute(node, "sizeOfGlobalColorTable",
119                                     true, 2, 256);
120                 if (sizeOfGlobalColorTable != 2 &&
121                    sizeOfGlobalColorTable != 4 &&
122                    sizeOfGlobalColorTable != 8 &&
123                    sizeOfGlobalColorTable != 16 &&
124                    sizeOfGlobalColorTable != 32 &&
125                    sizeOfGlobalColorTable != 64 &&
126                    sizeOfGlobalColorTable != 128 &&
127                    sizeOfGlobalColorTable != 256) {
128                     fatal(node,
129                           "Bad value for GlobalColorTable attribute sizeOfGlobalColorTable!");
130                 }
131
132                 backgroundColorIndex = getIntAttribute(node,
133                                                        "backgroundColorIndex",
134                                                        0, true,
135                                                        true, 0, 255);
136
137                 sortFlag = getBooleanAttribute(node, "sortFlag", false, true);
138
139                 globalColorTable = getColorTable(node, "ColorTableEntry",
140                                                  true, sizeOfGlobalColorTable);
141             } else {
142                 fatal(node, "Unknown child of root node!");
143             }
144
145             node = node.getNextSibling();
146         }
147     }
148
149     protected void mergeStandardTree(Node JavaDoc root)
150       throws IIOInvalidTreeException JavaDoc {
151         Node JavaDoc node = root;
152         if (!node.getNodeName()
153             .equals(IIOMetadataFormatImpl.standardMetadataFormatName)) {
154             fatal(node, "Root must be " +
155                   IIOMetadataFormatImpl.standardMetadataFormatName);
156         }
157
158         node = node.getFirstChild();
159         while (node != null) {
160             String JavaDoc name = node.getNodeName();
161
162             if (name.equals("Chroma")) {
163                 Node JavaDoc childNode = node.getFirstChild();
164                 while(childNode != null) {
165                     String JavaDoc childName = childNode.getNodeName();
166                     if (childName.equals("Palette")) {
167                         globalColorTable = getColorTable(childNode,
168                                                          "PaletteEntry",
169                                                          false, -1);
170
171                     } else if (childName.equals("BackgroundIndex")) {
172                         backgroundColorIndex = getIntAttribute(childNode,
173                                                                "value",
174                                                                -1, true,
175                                                                true, 0, 255);
176                     }
177                     childNode = childNode.getNextSibling();
178                 }
179             } else if (name.equals("Data")) {
180                 Node JavaDoc childNode = node.getFirstChild();
181                 while(childNode != null) {
182                     String JavaDoc childName = childNode.getNodeName();
183                     if (childName.equals("BitsPerSample")) {
184                         colorResolution = getIntAttribute(childNode,
185                                                           "value",
186                                                           -1, true,
187                                                           true, 1, 8);
188                         break;
189                     }
190                     childNode = childNode.getNextSibling();
191                 }
192             } else if (name.equals("Dimension")) {
193                 Node JavaDoc childNode = node.getFirstChild();
194                 while(childNode != null) {
195                     String JavaDoc childName = childNode.getNodeName();
196                     if (childName.equals("PixelAspectRatio")) {
197                         float aspectRatio = getFloatAttribute(childNode,
198                                                               "value");
199                         if (aspectRatio == 1.0F) {
200                             pixelAspectRatio = 0;
201                         } else {
202                             int ratio = (int)(aspectRatio*64.0F - 15.0F);
203                             pixelAspectRatio =
204                                 Math.max(Math.min(ratio, 255), 0);
205                         }
206                     } else if (childName.equals("HorizontalScreenSize")) {
207                         logicalScreenWidth = getIntAttribute(childNode,
208                                                              "value",
209                                                              -1, true,
210                                                              true, 1, 65535);
211                     } else if (childName.equals("VerticalScreenSize")) {
212                         logicalScreenHeight = getIntAttribute(childNode,
213                                                               "value",
214                                                               -1, true,
215                                                               true, 1, 65535);
216                     }
217                     childNode = childNode.getNextSibling();
218                 }
219             } else if (name.equals("Document")) {
220                 Node JavaDoc childNode = node.getFirstChild();
221                 while(childNode != null) {
222                     String JavaDoc childName = childNode.getNodeName();
223                     if (childName.equals("FormatVersion")) {
224                         String JavaDoc formatVersion =
225                             getStringAttribute(childNode, "value", null,
226                                                true, null);
227                         for (int i = 0; i < versionStrings.length; i++) {
228                             if (formatVersion.equals(versionStrings[i])) {
229                                 version = formatVersion;
230                                 break;
231                             }
232                         }
233                         break;
234                     }
235                     childNode = childNode.getNextSibling();
236                 }
237             }
238
239             node = node.getNextSibling();
240         }
241     }
242
243     public void setFromTree(String JavaDoc formatName, Node JavaDoc root)
244         throws IIOInvalidTreeException JavaDoc
245     {
246         reset();
247         mergeTree(formatName, root);
248     }
249 }
250
Popular Tags