1 7 8 package com.sun.imageio.plugins.gif; 9 10 import javax.imageio.ImageTypeSpecifier ; 11 import javax.imageio.metadata.IIOMetadata ; 12 import javax.imageio.metadata.IIOMetadataNode ; 13 import javax.imageio.metadata.IIOMetadataFormat ; 14 import javax.imageio.metadata.IIOMetadataFormatImpl ; 15 import org.w3c.dom.Node ; 16 17 19 22 public class GIFStreamMetadata extends IIOMetadata { 23 24 static final String 26 nativeMetadataFormatName = "javax_imageio_gif_stream_1.0"; 27 28 public static final String [] versionStrings = { "87a", "89a" }; 29 30 public String version; public int logicalScreenWidth; 32 public int logicalScreenHeight; 33 public int colorResolution; public int pixelAspectRatio; 35 36 public int backgroundColorIndex; public boolean sortFlag; 39 public static final String [] colorTableSizes = { 40 "2", "4", "8", "16", "32", "64", "128", "256" 41 }; 42 43 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 getAsTree(String 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 ("Not a recognized format!"); 66 } 67 } 68 69 private Node getNativeTree() { 70 IIOMetadataNode node; IIOMetadataNode root = 72 new IIOMetadataNode (nativeMetadataFormatName); 73 74 node = new IIOMetadataNode ("Version"); 75 node.setAttribute("value", version); 76 root.appendChild(node); 77 78 node = new IIOMetadataNode ("LogicalScreenDescriptor"); 80 node.setAttribute("logicalScreenWidth", 81 Integer.toString(logicalScreenWidth)); 82 node.setAttribute("logicalScreenHeight", 83 Integer.toString(logicalScreenHeight)); 84 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 ("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 entry = 103 new IIOMetadataNode ("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 getStandardChromaNode() { 120 IIOMetadataNode chroma_node = new IIOMetadataNode ("Chroma"); 121 IIOMetadataNode node = null; 123 node = new IIOMetadataNode ("ColorSpaceType"); 124 node.setAttribute("name", "RGB"); 125 chroma_node.appendChild(node); 126 127 node = new IIOMetadataNode ("BlackIsZero"); 128 node.setAttribute("value", "TRUE"); 129 chroma_node.appendChild(node); 130 131 134 if (globalColorTable != null) { 135 node = new IIOMetadataNode ("Palette"); 136 int numEntries = globalColorTable.length/3; 137 for (int i = 0; i < numEntries; i++) { 138 IIOMetadataNode entry = 139 new IIOMetadataNode ("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 node = new IIOMetadataNode ("BackgroundIndex"); 153 node.setAttribute("value", Integer.toString(backgroundColorIndex)); 154 chroma_node.appendChild(node); 155 } 156 157 return chroma_node; 158 } 159 160 public IIOMetadataNode getStandardCompressionNode() { 161 IIOMetadataNode compression_node = new IIOMetadataNode ("Compression"); 162 IIOMetadataNode node = null; 164 node = new IIOMetadataNode ("CompressionTypeName"); 165 node.setAttribute("value", "lzw"); 166 compression_node.appendChild(node); 167 168 node = new IIOMetadataNode ("Lossless"); 169 node.setAttribute("value", "true"); 170 compression_node.appendChild(node); 171 172 175 return compression_node; 176 } 177 178 public IIOMetadataNode getStandardDataNode() { 179 IIOMetadataNode data_node = new IIOMetadataNode ("Data"); 180 IIOMetadataNode node = null; 182 184 node = new IIOMetadataNode ("SampleFormat"); 185 node.setAttribute("value", "Index"); 186 data_node.appendChild(node); 187 188 node = new IIOMetadataNode ("BitsPerSample"); 189 node.setAttribute("value", Integer.toString(colorResolution)); 190 data_node.appendChild(node); 191 192 195 return data_node; 196 } 197 198 public IIOMetadataNode getStandardDimensionNode() { 199 IIOMetadataNode dimension_node = new IIOMetadataNode ("Dimension"); 200 IIOMetadataNode node = null; 202 node = new IIOMetadataNode ("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 ("ImageOrientation"); 211 node.setAttribute("value", "Normal"); 212 dimension_node.appendChild(node); 213 214 223 node = new IIOMetadataNode ("HorizontalScreenSize"); 224 node.setAttribute("value", Integer.toString(logicalScreenWidth)); 225 dimension_node.appendChild(node); 226 227 node = new IIOMetadataNode ("VerticalScreenSize"); 228 node.setAttribute("value", Integer.toString(logicalScreenHeight)); 229 dimension_node.appendChild(node); 230 231 return dimension_node; 232 } 233 234 public IIOMetadataNode getStandardDocumentNode() { 235 IIOMetadataNode document_node = new IIOMetadataNode ("Document"); 236 IIOMetadataNode node = null; 238 node = new IIOMetadataNode ("FormatVersion"); 239 node.setAttribute("value", version); 240 document_node.appendChild(node); 241 242 246 return document_node; 247 } 248 249 public IIOMetadataNode getStandardTextNode() { 250 return null; 252 } 253 254 public IIOMetadataNode getStandardTransparencyNode() { 255 return null; 257 } 258 259 public void setFromTree(String formatName, Node root) { 260 throw new IllegalStateException ("Metadata is read-only!"); 261 } 262 263 public void mergeTree(String formatName, Node root) { 264 throw new IllegalStateException ("Metadata is read-only!"); 265 } 266 267 public void reset() { 268 throw new IllegalStateException ("Metadata is read-only!"); 269 } 270 } 271 | Popular Tags |