1 7 8 package com.sun.imageio.plugins.gif; 9 10 15 16 import javax.imageio.ImageTypeSpecifier ; 17 import javax.imageio.metadata.IIOInvalidTreeException ; 18 import javax.imageio.metadata.IIOMetadata ; 19 import javax.imageio.metadata.IIOMetadataNode ; 20 import javax.imageio.metadata.IIOMetadataFormat ; 21 import javax.imageio.metadata.IIOMetadataFormatImpl ; 22 import org.w3c.dom.Node ; 23 24 class GIFWritableStreamMetadata extends GIFStreamMetadata { 25 26 static final String 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", null, null); 35 36 reset(); 38 } 39 40 public boolean isReadOnly() { 41 return false; 42 } 43 44 public void mergeTree(String formatName, Node root) 45 throws IIOInvalidTreeException { 46 if (formatName.equals(nativeMetadataFormatName)) { 47 if (root == null) { 48 throw new IllegalArgumentException ("root == null!"); 49 } 50 mergeNativeTree(root); 51 } else if (formatName.equals 52 (IIOMetadataFormatImpl.standardMetadataFormatName)) { 53 if (root == null) { 54 throw new IllegalArgumentException ("root == null!"); 55 } 56 mergeStandardTree(root); 57 } else { 58 throw new IllegalArgumentException ("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 root) throws IIOInvalidTreeException { 76 Node 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 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 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 root) 150 throws IIOInvalidTreeException { 151 Node 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 name = node.getNodeName(); 161 162 if (name.equals("Chroma")) { 163 Node childNode = node.getFirstChild(); 164 while(childNode != null) { 165 String 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 childNode = node.getFirstChild(); 181 while(childNode != null) { 182 String 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 childNode = node.getFirstChild(); 194 while(childNode != null) { 195 String 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 childNode = node.getFirstChild(); 221 while(childNode != null) { 222 String childName = childNode.getNodeName(); 223 if (childName.equals("FormatVersion")) { 224 String 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 formatName, Node root) 244 throws IIOInvalidTreeException 245 { 246 reset(); 247 mergeTree(formatName, root); 248 } 249 } 250 | Popular Tags |