KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)GIFImageMetadata.java 1.28 03/12/19
3  *
4  * Copyright 2004 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 import java.io.UnsupportedEncodingException JavaDoc;
11 import java.util.ArrayList JavaDoc;
12 import java.util.Iterator JavaDoc;
13 import java.util.List JavaDoc;
14 import javax.imageio.ImageTypeSpecifier JavaDoc;
15 import javax.imageio.metadata.IIOMetadata JavaDoc;
16 import javax.imageio.metadata.IIOMetadataNode JavaDoc;
17 import javax.imageio.metadata.IIOMetadataFormat JavaDoc;
18 import javax.imageio.metadata.IIOMetadataFormatImpl JavaDoc;
19 import org.w3c.dom.Node JavaDoc;
20
21 /**
22  * @version 0.5
23  */

24 public class GIFImageMetadata extends IIOMetadata JavaDoc {
25
26     // package scope
27
static final String JavaDoc
28         nativeMetadataFormatName = "javax_imageio_gif_image_1.0";
29
30     static final String JavaDoc[] disposalMethodNames = {
31         "none",
32         "doNotDispose",
33         "restoreToBackgroundColor",
34         "restoreToPrevious",
35         "undefinedDisposalMethod4",
36         "undefinedDisposalMethod5",
37         "undefinedDisposalMethod6",
38         "undefinedDisposalMethod7"
39     };
40
41     // Fields from Image Descriptor
42
public int imageLeftPosition;
43     public int imageTopPosition;
44     public int imageWidth;
45     public int imageHeight;
46     public boolean interlaceFlag = false;
47     public boolean sortFlag = false;
48     public byte[] localColorTable = null;
49
50     // Fields from Graphic Control Extension
51
public int disposalMethod = 0;
52     public boolean userInputFlag = false;
53     public boolean transparentColorFlag = false;
54     public int delayTime = 0;
55     public int transparentColorIndex = 0;
56
57     // Fields from Plain Text Extension
58
public boolean hasPlainTextExtension = false;
59     public int textGridLeft;
60     public int textGridTop;
61     public int textGridWidth;
62     public int textGridHeight;
63     public int characterCellWidth;
64     public int characterCellHeight;
65     public int textForegroundColor;
66     public int textBackgroundColor;
67     public byte[] text;
68
69     // Fields from ApplicationExtension
70
// List of byte[]
71
public List JavaDoc applicationIDs = null; // new ArrayList();
72

73     // List of byte[]
74
public List JavaDoc authenticationCodes = null; // new ArrayList();
75

76     // List of byte[]
77
public List JavaDoc applicationData = null; // new ArrayList();
78

79     // Fields from CommentExtension
80
// List of byte[]
81
public List JavaDoc comments = null; // new ArrayList();
82

83     public GIFImageMetadata() {
84         super(true,
85               nativeMetadataFormatName,
86               "com.sun.imageio.plugins.gif.GIFImageMetadataFormat",
87               null, null);
88     }
89     
90     public boolean isReadOnly() {
91         return true;
92     }
93
94     public Node JavaDoc getAsTree(String JavaDoc formatName) {
95         if (formatName.equals(nativeMetadataFormatName)) {
96             return getNativeTree();
97         } else if (formatName.equals
98                    (IIOMetadataFormatImpl.standardMetadataFormatName)) {
99             return getStandardTree();
100         } else {
101             throw new IllegalArgumentException JavaDoc("Not a recognized format!");
102         }
103     }
104
105     private String JavaDoc toISO8859(byte[] data) {
106         try {
107             return new String JavaDoc(data, "ISO-8859-1");
108         } catch (UnsupportedEncodingException JavaDoc e) {
109             return "";
110         }
111     }
112
113     private Node JavaDoc getNativeTree() {
114         IIOMetadataNode JavaDoc node; // scratch node
115
IIOMetadataNode JavaDoc root =
116             new IIOMetadataNode JavaDoc(nativeMetadataFormatName);
117
118         // Image descriptor
119
node = new IIOMetadataNode JavaDoc("ImageDescriptor");
120         node.setAttribute("imageLeftPosition",
121                           Integer.toString(imageLeftPosition));
122         node.setAttribute("imageTopPosition",
123                           Integer.toString(imageTopPosition));
124         node.setAttribute("imageWidth", Integer.toString(imageWidth));
125         node.setAttribute("imageHeight", Integer.toString(imageHeight));
126         node.setAttribute("interlaceFlag",
127                           interlaceFlag ? "true" : "false");
128         root.appendChild(node);
129
130         // Local color table
131
if (localColorTable != null) {
132             node = new IIOMetadataNode JavaDoc("LocalColorTable");
133             int numEntries = localColorTable.length/3;
134             node.setAttribute("sizeOfLocalColorTable",
135                               Integer.toString(numEntries));
136             node.setAttribute("sortFlag",
137                               sortFlag ? "TRUE" : "FALSE");
138             
139             for (int i = 0; i < numEntries; i++) {
140                 IIOMetadataNode JavaDoc entry =
141                     new IIOMetadataNode JavaDoc("ColorTableEntry");
142                 entry.setAttribute("index", Integer.toString(i));
143                 int r = localColorTable[3*i] & 0xff;
144                 int g = localColorTable[3*i + 1] & 0xff;
145                 int b = localColorTable[3*i + 2] & 0xff;
146                 entry.setAttribute("red", Integer.toString(r));
147                 entry.setAttribute("green", Integer.toString(g));
148                 entry.setAttribute("blue", Integer.toString(b));
149                 node.appendChild(entry);
150             }
151             root.appendChild(node);
152         }
153
154         // Graphic control extension
155
node = new IIOMetadataNode JavaDoc("GraphicControlExtension");
156         node.setAttribute("disposalMethod",
157                           disposalMethodNames[disposalMethod]);
158         node.setAttribute("userInputFlag",
159                           userInputFlag ? "true" : "false");
160         node.setAttribute("transparentColorFlag",
161                           transparentColorFlag ? "true" : "false");
162         node.setAttribute("delayTime",
163                           Integer.toString(delayTime));
164         node.setAttribute("transparentColorIndex",
165                           Integer.toString(transparentColorIndex));
166         root.appendChild(node);
167
168         if (hasPlainTextExtension) {
169             node = new IIOMetadataNode JavaDoc("PlainTextExtension");
170             node.setAttribute("textGridLeft",
171                               Integer.toString(textGridLeft));
172             node.setAttribute("textGridTop",
173                               Integer.toString(textGridTop));
174             node.setAttribute("textGridWidth",
175                               Integer.toString(textGridWidth));
176             node.setAttribute("textGridHeight",
177                               Integer.toString(textGridHeight));
178             node.setAttribute("characterCellWidth",
179                               Integer.toString(characterCellWidth));
180             node.setAttribute("characterCellHeight",
181                               Integer.toString(characterCellHeight));
182             node.setAttribute("textForegroundColor",
183                               Integer.toString(textForegroundColor));
184             node.setAttribute("textBackgroundColor",
185                               Integer.toString(textBackgroundColor));
186             node.setAttribute("text", toISO8859(text));
187
188             root.appendChild(node);
189         }
190
191         // Application extensions
192
int numAppExtensions = applicationIDs == null ?
193             0 : applicationIDs.size();
194         if (numAppExtensions > 0) {
195             node = new IIOMetadataNode JavaDoc("ApplicationExtensions");
196             for (int i = 0; i < numAppExtensions; i++) {
197                 IIOMetadataNode JavaDoc appExtNode =
198                     new IIOMetadataNode JavaDoc("ApplicationExtension");
199                 byte[] applicationID = (byte[])applicationIDs.get(i);
200                 appExtNode.setAttribute("applicationID",
201                                         toISO8859(applicationID));
202                 byte[] authenticationCode = (byte[])authenticationCodes.get(i);
203                 appExtNode.setAttribute("authenticationCode",
204                                         toISO8859(authenticationCode));
205                 byte[] appData = (byte[])applicationData.get(i);
206                 appExtNode.setUserObject((byte[])appData.clone());
207                 node.appendChild(appExtNode);
208             }
209
210             root.appendChild(node);
211         }
212
213         // Comment extensions
214
int numComments = comments == null ? 0 : comments.size();
215         if (numComments > 0) {
216             node = new IIOMetadataNode JavaDoc("CommentExtensions");
217             for (int i = 0; i < numComments; i++) {
218                 IIOMetadataNode JavaDoc commentNode =
219                     new IIOMetadataNode JavaDoc("CommentExtension");
220                 byte[] comment = (byte[])comments.get(i);
221                 commentNode.setAttribute("value", toISO8859(comment));
222                 node.appendChild(commentNode);
223             }
224
225             root.appendChild(node);
226         }
227
228         return root;
229     }
230
231     public IIOMetadataNode JavaDoc getStandardChromaNode() {
232         IIOMetadataNode JavaDoc chroma_node = new IIOMetadataNode JavaDoc("Chroma");
233         IIOMetadataNode JavaDoc node = null; // scratch node
234

235         node = new IIOMetadataNode JavaDoc("ColorSpaceType");
236         node.setAttribute("name", "RGB");
237         chroma_node.appendChild(node);
238
239         node = new IIOMetadataNode JavaDoc("NumChannels");
240         node.setAttribute("value", transparentColorFlag ? "4" : "3");
241         chroma_node.appendChild(node);
242
243         // Gamma not in format
244

245         node = new IIOMetadataNode JavaDoc("BlackIsZero");
246         node.setAttribute("value", "TRUE");
247         chroma_node.appendChild(node);
248
249         if (localColorTable != null) {
250             node = new IIOMetadataNode JavaDoc("Palette");
251             int numEntries = localColorTable.length/3;
252             for (int i = 0; i < numEntries; i++) {
253                 IIOMetadataNode JavaDoc entry =
254                     new IIOMetadataNode JavaDoc("PaletteEntry");
255                 entry.setAttribute("index", Integer.toString(i));
256                 entry.setAttribute("red",
257                            Integer.toString(localColorTable[3*i] & 0xff));
258                 entry.setAttribute("green",
259                            Integer.toString(localColorTable[3*i + 1] & 0xff));
260                 entry.setAttribute("blue",
261                            Integer.toString(localColorTable[3*i + 2] & 0xff));
262                 node.appendChild(entry);
263             }
264             chroma_node.appendChild(node);
265         }
266
267         // BackgroundIndex not in image
268
// BackgroundColor not in format
269

270         return chroma_node;
271     }
272
273     public IIOMetadataNode JavaDoc getStandardCompressionNode() {
274         IIOMetadataNode JavaDoc compression_node = new IIOMetadataNode JavaDoc("Compression");
275         IIOMetadataNode JavaDoc node = null; // scratch node
276

277         node = new IIOMetadataNode JavaDoc("CompressionTypeName");
278         node.setAttribute("value", "lzw");
279         compression_node.appendChild(node);
280
281         node = new IIOMetadataNode JavaDoc("Lossless");
282         node.setAttribute("value", "TRUE");
283         compression_node.appendChild(node);
284
285         node = new IIOMetadataNode JavaDoc("NumProgressiveScans");
286         node.setAttribute("value", interlaceFlag ? "4" : "1");
287         compression_node.appendChild(node);
288
289         // BitRate not in format
290

291         return compression_node;
292     }
293
294     public IIOMetadataNode JavaDoc getStandardDataNode() {
295         IIOMetadataNode JavaDoc data_node = new IIOMetadataNode JavaDoc("Data");
296         IIOMetadataNode JavaDoc node = null; // scratch node
297

298         // PlanarConfiguration not in format
299

300         node = new IIOMetadataNode JavaDoc("SampleFormat");
301         node.setAttribute("value", "Index");
302         data_node.appendChild(node);
303
304         // BitsPerSample not in image
305
// SignificantBitsPerSample not in format
306
// SampleMSB not in format
307

308         return data_node;
309     }
310
311     public IIOMetadataNode JavaDoc getStandardDimensionNode() {
312         IIOMetadataNode JavaDoc dimension_node = new IIOMetadataNode JavaDoc("Dimension");
313         IIOMetadataNode JavaDoc node = null; // scratch node
314

315         // PixelAspectRatio not in image
316

317         node = new IIOMetadataNode JavaDoc("ImageOrientation");
318         node.setAttribute("value", "Normal");
319         dimension_node.appendChild(node);
320
321         // HorizontalPixelSize not in format
322
// VerticalPixelSize not in format
323
// HorizontalPhysicalPixelSpacing not in format
324
// VerticalPhysicalPixelSpacing not in format
325
// HorizontalPosition not in format
326
// VerticalPosition not in format
327

328         node = new IIOMetadataNode JavaDoc("HorizontalPixelOffset");
329         node.setAttribute("value", Integer.toString(imageLeftPosition));
330         dimension_node.appendChild(node);
331
332         node = new IIOMetadataNode JavaDoc("VerticalPixelOffset");
333         node.setAttribute("value", Integer.toString(imageTopPosition));
334         dimension_node.appendChild(node);
335
336         // HorizontalScreenSize not in image
337
// VerticalScreenSize not in image
338

339         return dimension_node;
340     }
341
342     // Document not in image
343

344     public IIOMetadataNode JavaDoc getStandardTextNode() {
345         if (comments == null) {
346             return null;
347         }
348         Iterator JavaDoc commentIter = comments.iterator();
349         if (!commentIter.hasNext()) {
350             return null;
351         }
352
353         IIOMetadataNode JavaDoc text_node = new IIOMetadataNode JavaDoc("Text");
354         IIOMetadataNode JavaDoc node = null; // scratch node
355

356         while (commentIter.hasNext()) {
357             byte[] comment = (byte[])commentIter.next();
358             String JavaDoc s = null;
359             try {
360                 s = new String JavaDoc(comment, "ISO-8859-1");
361             } catch (UnsupportedEncodingException JavaDoc e) {
362                 throw new RuntimeException JavaDoc("Encoding ISO-8859-1 unknown!");
363             }
364
365             node = new IIOMetadataNode JavaDoc("TextEntry");
366             node.setAttribute("value", s);
367             node.setAttribute("encoding", "ISO-8859-1");
368             node.setAttribute("compression", "none");
369             text_node.appendChild(node);
370         }
371
372         return text_node;
373     }
374
375     public IIOMetadataNode JavaDoc getStandardTransparencyNode() {
376         if (!transparentColorFlag) {
377             return null;
378         }
379         
380         IIOMetadataNode JavaDoc transparency_node =
381             new IIOMetadataNode JavaDoc("Transparency");
382         IIOMetadataNode JavaDoc node = null; // scratch node
383

384         // Alpha not in format
385

386         node = new IIOMetadataNode JavaDoc("TransparentIndex");
387         node.setAttribute("value",
388                           Integer.toString(transparentColorIndex));
389         transparency_node.appendChild(node);
390
391         // TransparentColor not in format
392
// TileTransparencies not in format
393
// TileOpacities not in format
394

395         return transparency_node;
396     }
397
398     public void setFromTree(String JavaDoc formatName, Node JavaDoc root) {
399         throw new IllegalStateException JavaDoc("Metadata is read-only!");
400     }
401
402     public void mergeTree(String JavaDoc formatName, Node JavaDoc root) {
403         throw new IllegalStateException JavaDoc("Metadata is read-only!");
404     }
405
406     public void reset() {
407         throw new IllegalStateException JavaDoc("Metadata is read-only!");
408     }
409 }
410
Popular Tags