KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)GIFStreamMetadata.java 1.26 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 javax.imageio.ImageTypeSpecifier JavaDoc;
11 import javax.imageio.metadata.IIOMetadata JavaDoc;
12 import javax.imageio.metadata.IIOMetadataNode JavaDoc;
13 import javax.imageio.metadata.IIOMetadataFormat JavaDoc;
14 import javax.imageio.metadata.IIOMetadataFormatImpl JavaDoc;
15 import org.w3c.dom.Node JavaDoc;
16
17 // TODO - document elimination of globalColorTableFlag
18

19 /**
20  * @version 0.5
21  */

22 public class GIFStreamMetadata extends IIOMetadata JavaDoc {
23
24     // package scope
25
static final String JavaDoc
26         nativeMetadataFormatName = "javax_imageio_gif_stream_1.0";
27
28     public static final String JavaDoc[] versionStrings = { "87a", "89a" };
29
30     public String JavaDoc version; // 87a or 89a
31
public int logicalScreenWidth;
32     public int logicalScreenHeight;
33     public int colorResolution; // 1 to 8
34
public int pixelAspectRatio;
35
36     public int backgroundColorIndex; // Valid if globalColorTable != null
37
public boolean sortFlag; // Valid if globalColorTable != null
38

39     public static final String JavaDoc[] colorTableSizes = {
40         "2", "4", "8", "16", "32", "64", "128", "256"
41     };
42
43     // Set global color table flag in header to 0 if null, 1 otherwise
44
public byte[] globalColorTable = null;
45
46     public GIFStreamMetadata() {
47         super(true,
48               nativeMetadataFormatName,
49               "com.sun.imageio.plugins.gif.GIFStreamMetadataFormat",
50               null, null);
51
52     }
53
54     public boolean isReadOnly() {
55         return true;
56     }
57     
58     public Node JavaDoc getAsTree(String JavaDoc formatName) {
59         if (formatName.equals(nativeMetadataFormatName)) {
60             return getNativeTree();
61         } else if (formatName.equals
62                    (IIOMetadataFormatImpl.standardMetadataFormatName)) {
63             return getStandardTree();
64         } else {
65             throw new IllegalArgumentException JavaDoc("Not a recognized format!");
66         }
67     }
68
69     private Node JavaDoc getNativeTree() {
70         IIOMetadataNode JavaDoc node; // scratch node
71
IIOMetadataNode JavaDoc root =
72             new IIOMetadataNode JavaDoc(nativeMetadataFormatName);
73             
74         node = new IIOMetadataNode JavaDoc("Version");
75         node.setAttribute("value", version);
76         root.appendChild(node);
77         
78         // Image descriptor
79
node = new IIOMetadataNode JavaDoc("LogicalScreenDescriptor");
80         node.setAttribute("logicalScreenWidth",
81                           Integer.toString(logicalScreenWidth));
82         node.setAttribute("logicalScreenHeight",
83                           Integer.toString(logicalScreenHeight));
84         // Stored value plus one
85
node.setAttribute("colorResolution",
86                           Integer.toString(colorResolution));
87         node.setAttribute("pixelAspectRatio",
88                           Integer.toString(pixelAspectRatio));
89         root.appendChild(node);
90
91         if (globalColorTable != null) {
92             node = new IIOMetadataNode JavaDoc("GlobalColorTable");
93             int numEntries = globalColorTable.length/3;
94             node.setAttribute("sizeOfGlobalColorTable",
95                               Integer.toString(numEntries));
96             node.setAttribute("backgroundColorIndex",
97                               Integer.toString(backgroundColorIndex));
98             node.setAttribute("sortFlag",
99                               sortFlag ? "TRUE" : "FALSE");
100
101             for (int i = 0; i < numEntries; i++) {
102                 IIOMetadataNode JavaDoc entry =
103                     new IIOMetadataNode JavaDoc("ColorTableEntry");
104                 entry.setAttribute("index", Integer.toString(i));
105                 int r = globalColorTable[3*i] & 0xff;
106                 int g = globalColorTable[3*i + 1] & 0xff;
107                 int b = globalColorTable[3*i + 2] & 0xff;
108                 entry.setAttribute("red", Integer.toString(r));
109                 entry.setAttribute("green", Integer.toString(g));
110                 entry.setAttribute("blue", Integer.toString(b));
111                 node.appendChild(entry);
112             }
113             root.appendChild(node);
114         }
115
116         return root;
117     }
118
119     public IIOMetadataNode JavaDoc getStandardChromaNode() {
120         IIOMetadataNode JavaDoc chroma_node = new IIOMetadataNode JavaDoc("Chroma");
121         IIOMetadataNode JavaDoc node = null; // scratch node
122

123         node = new IIOMetadataNode JavaDoc("ColorSpaceType");
124         node.setAttribute("name", "RGB");
125         chroma_node.appendChild(node);
126
127         node = new IIOMetadataNode JavaDoc("BlackIsZero");
128         node.setAttribute("value", "TRUE");
129         chroma_node.appendChild(node);
130
131         // NumChannels not in stream
132
// Gamma not in format
133

134         if (globalColorTable != null) {
135             node = new IIOMetadataNode JavaDoc("Palette");
136             int numEntries = globalColorTable.length/3;
137             for (int i = 0; i < numEntries; i++) {
138                 IIOMetadataNode JavaDoc entry =
139                     new IIOMetadataNode JavaDoc("PaletteEntry");
140                 entry.setAttribute("index", Integer.toString(i));
141                 entry.setAttribute("red",
142                            Integer.toString(globalColorTable[3*i] & 0xff));
143                 entry.setAttribute("green",
144                            Integer.toString(globalColorTable[3*i + 1] & 0xff));
145                 entry.setAttribute("blue",
146                            Integer.toString(globalColorTable[3*i + 2] & 0xff));
147                 node.appendChild(entry);
148             }
149             chroma_node.appendChild(node);
150
151             // backgroundColorIndex is valid iff there is a color table
152
node = new IIOMetadataNode JavaDoc("BackgroundIndex");
153             node.setAttribute("value", Integer.toString(backgroundColorIndex));
154             chroma_node.appendChild(node);
155         }
156
157         return chroma_node;
158     }
159
160     public IIOMetadataNode JavaDoc getStandardCompressionNode() {
161         IIOMetadataNode JavaDoc compression_node = new IIOMetadataNode JavaDoc("Compression");
162         IIOMetadataNode JavaDoc node = null; // scratch node
163

164         node = new IIOMetadataNode JavaDoc("CompressionTypeName");
165         node.setAttribute("value", "lzw");
166         compression_node.appendChild(node);
167
168         node = new IIOMetadataNode JavaDoc("Lossless");
169         node.setAttribute("value", "true");
170         compression_node.appendChild(node);
171
172         // NumProgressiveScans not in stream
173
// BitRate not in format
174

175         return compression_node;
176     }
177
178     public IIOMetadataNode JavaDoc getStandardDataNode() {
179         IIOMetadataNode JavaDoc data_node = new IIOMetadataNode JavaDoc("Data");
180         IIOMetadataNode JavaDoc node = null; // scratch node
181

182         // PlanarConfiguration
183

184         node = new IIOMetadataNode JavaDoc("SampleFormat");
185         node.setAttribute("value", "Index");
186         data_node.appendChild(node);
187
188         node = new IIOMetadataNode JavaDoc("BitsPerSample");
189         node.setAttribute("value", Integer.toString(colorResolution));
190         data_node.appendChild(node);
191         
192         // SignificantBitsPerSample
193
// SampleMSB
194

195         return data_node;
196     }
197
198     public IIOMetadataNode JavaDoc getStandardDimensionNode() {
199         IIOMetadataNode JavaDoc dimension_node = new IIOMetadataNode JavaDoc("Dimension");
200         IIOMetadataNode JavaDoc node = null; // scratch node
201

202         node = new IIOMetadataNode JavaDoc("PixelAspectRatio");
203         float aspectRatio = 1.0F;
204         if (pixelAspectRatio != 0) {
205             aspectRatio = (pixelAspectRatio + 15)/64.0F;
206         }
207         node.setAttribute("value", Float.toString(aspectRatio));
208         dimension_node.appendChild(node);
209
210         node = new IIOMetadataNode JavaDoc("ImageOrientation");
211         node.setAttribute("value", "Normal");
212         dimension_node.appendChild(node);
213
214         // HorizontalPixelSize not in format
215
// VerticalPixelSize not in format
216
// HorizontalPhysicalPixelSpacing not in format
217
// VerticalPhysicalPixelSpacing not in format
218
// HorizontalPosition not in format
219
// VerticalPosition not in format
220
// HorizontalPixelOffset not in stream
221
// VerticalPixelOffset not in stream
222

223         node = new IIOMetadataNode JavaDoc("HorizontalScreenSize");
224         node.setAttribute("value", Integer.toString(logicalScreenWidth));
225         dimension_node.appendChild(node);
226         
227         node = new IIOMetadataNode JavaDoc("VerticalScreenSize");
228         node.setAttribute("value", Integer.toString(logicalScreenHeight));
229         dimension_node.appendChild(node);
230         
231         return dimension_node;
232     }
233
234     public IIOMetadataNode JavaDoc getStandardDocumentNode() {
235         IIOMetadataNode JavaDoc document_node = new IIOMetadataNode JavaDoc("Document");
236         IIOMetadataNode JavaDoc node = null; // scratch node
237

238         node = new IIOMetadataNode JavaDoc("FormatVersion");
239         node.setAttribute("value", version);
240         document_node.appendChild(node);
241
242         // SubimageInterpretation not in format
243
// ImageCreationTime not in format
244
// ImageModificationTime not in format
245

246         return document_node;
247     }
248
249     public IIOMetadataNode JavaDoc getStandardTextNode() {
250         // Not in stream
251
return null;
252     }
253
254     public IIOMetadataNode JavaDoc getStandardTransparencyNode() {
255         // Not in stream
256
return null;
257     }
258
259     public void setFromTree(String JavaDoc formatName, Node JavaDoc root) {
260         throw new IllegalStateException JavaDoc("Metadata is read-only!");
261     }
262
263     public void mergeTree(String JavaDoc formatName, Node JavaDoc root) {
264         throw new IllegalStateException JavaDoc("Metadata is read-only!");
265     }
266
267     public void reset() {
268         throw new IllegalStateException JavaDoc("Metadata is read-only!");
269     }
270 }
271
Popular Tags