1 7 8 package com.sun.imageio.plugins.bmp; 9 10 import java.io.UnsupportedEncodingException ; 11 import java.util.ArrayList ; 12 import java.util.Iterator ; 13 import java.util.List ; 14 import javax.imageio.ImageTypeSpecifier ; 15 import javax.imageio.metadata.IIOMetadata ; 16 import javax.imageio.metadata.IIOMetadataNode ; 17 import javax.imageio.metadata.IIOMetadataFormat ; 18 import javax.imageio.metadata.IIOMetadataFormatImpl ; 19 import org.w3c.dom.Node ; 20 import com.sun.imageio.plugins.common.I18N; 21 22 import com.sun.imageio.plugins.common.ImageUtil; 23 24 public class BMPMetadata extends IIOMetadata implements BMPConstants { 25 public static final String nativeMetadataFormatName = 26 "javax_imageio_bmp_1.0"; 27 28 public String bmpVersion; 30 public int width ; 31 public int height; 32 public short bitsPerPixel; 33 public int compression; 34 public int imageSize; 35 36 public int xPixelsPerMeter; 38 public int yPixelsPerMeter; 39 40 public int colorsUsed; 41 public int colorsImportant; 42 43 public int redMask; 45 public int greenMask; 46 public int blueMask; 47 public int alphaMask; 48 49 public int colorSpace; 50 51 public double redX; 53 public double redY; 54 public double redZ; 55 public double greenX; 56 public double greenY; 57 public double greenZ; 58 public double blueX; 59 public double blueY; 60 public double blueZ; 61 62 public int gammaRed; 64 public int gammaGreen; 65 public int gammaBlue; 66 67 public int intent; 68 69 public byte[] palette = null; 71 public int paletteSize; 72 public int red; 73 public int green; 74 public int blue; 75 76 public List comments = null; 80 public BMPMetadata() { 81 super(true, 82 nativeMetadataFormatName, 83 "com.sun.imageio.plugins.bmp.BMPMetadataFormat", 84 null, null); 85 } 86 87 public boolean isReadOnly() { 88 return true; 89 } 90 91 public Node getAsTree(String formatName) { 92 if (formatName.equals(nativeMetadataFormatName)) { 93 return getNativeTree(); 94 } else if (formatName.equals 95 (IIOMetadataFormatImpl.standardMetadataFormatName)) { 96 return getStandardTree(); 97 } else { 98 throw new IllegalArgumentException (I18N.getString("BMPMetadata0")); 99 } 100 } 101 102 private String toISO8859(byte[] data) { 103 try { 104 return new String (data, "ISO-8859-1"); 105 } catch (UnsupportedEncodingException e) { 106 return ""; 107 } 108 } 109 110 private Node getNativeTree() { 111 IIOMetadataNode root = 112 new IIOMetadataNode (nativeMetadataFormatName); 113 114 addChildNode(root, "BMPVersion", bmpVersion); 115 addChildNode(root, "Width", new Integer (width)); 116 addChildNode(root, "Height", new Integer (height)); 117 addChildNode(root, "BitsPerPixel", new Short (bitsPerPixel)); 118 addChildNode(root, "Compression", new Integer (compression)); 119 addChildNode(root, "ImageSize", new Integer (imageSize)); 120 121 IIOMetadataNode node = addChildNode(root, "PixelsPerMeter", null); 122 addChildNode(node, "X", new Integer (xPixelsPerMeter)); 123 addChildNode(node, "Y", new Integer (yPixelsPerMeter)); 124 125 addChildNode(root, "ColorsUsed", new Integer (colorsUsed)); 126 addChildNode(root, "ColorsImportant", new Integer (colorsImportant)); 127 128 int version = 0; 129 for (int i = 0; i < bmpVersion.length(); i++) 130 if (Character.isDigit(bmpVersion.charAt(i))) 131 version = bmpVersion.charAt(i) -'0'; 132 133 if (version >= 4) { 134 node = addChildNode(root, "Mask", null); 135 addChildNode(node, "Red", new Integer (redMask)); 136 addChildNode(node, "Green", new Integer (greenMask)); 137 addChildNode(node, "Blue", new Integer (blueMask)); 138 addChildNode(node, "Alpha", new Integer (alphaMask)); 139 140 addChildNode(root, "ColorSpaceType", new Integer (colorSpace)); 141 142 node = addChildNode(root, "CIEXYZEndPoints", null); 143 addXYZPoints(node, "Red", redX, redY, redZ); 144 addXYZPoints(node, "Green", greenX, greenY, greenZ); 145 addXYZPoints(node, "Blue", blueX, blueY, blueZ); 146 147 node = addChildNode(root, "Intent", new Integer (intent)); 148 } 149 150 if ((palette != null) && (paletteSize > 0)) { 152 node = addChildNode(root, "Palette", null); 153 int numComps = palette.length / paletteSize; 154 155 for (int i = 0, j = 0; i < paletteSize; i++) { 156 IIOMetadataNode entry = 157 addChildNode(node, "PaletteEntry", null); 158 red = palette[j++] & 0xff; 159 green = palette[j++] & 0xff; 160 blue = palette[j++] & 0xff; 161 addChildNode(entry, "Red", new Byte ((byte)red)); 162 addChildNode(entry, "Green", new Byte ((byte)green)); 163 addChildNode(entry, "Blue", new Byte ((byte)blue)); 164 if (numComps == 4) 165 addChildNode(entry, "Alpha", 166 new Byte ((byte)(palette[j++] & 0xff))); 167 } 168 } 169 170 return root; 171 } 172 173 protected IIOMetadataNode getStandardChromaNode() { 175 176 if ((palette != null) && (paletteSize > 0)) { 177 IIOMetadataNode node = new IIOMetadataNode ("Chroma"); 178 IIOMetadataNode subNode = new IIOMetadataNode ("Palette"); 179 int numComps = palette.length / paletteSize; 180 subNode.setAttribute("value", "" + numComps); 181 182 for (int i = 0, j = 0; i < paletteSize; i++) { 183 IIOMetadataNode subNode1 = new IIOMetadataNode ("PaletteEntry"); 184 subNode1.setAttribute("index", ""+i); 185 subNode1.setAttribute("red", "" + palette[j++]); 186 subNode1.setAttribute("green", "" + palette[j++]); 187 subNode1.setAttribute("blue", "" + palette[j++]); 188 if (numComps == 4 && palette[j] != 0) 189 subNode1.setAttribute("alpha", "" + palette[j++]); 190 subNode.appendChild(subNode1); 191 } 192 node.appendChild(subNode); 193 return node; 194 } 195 196 return null; 197 } 198 199 protected IIOMetadataNode getStandardCompressionNode() { 200 IIOMetadataNode node = new IIOMetadataNode ("Compression"); 201 202 IIOMetadataNode subNode = new IIOMetadataNode ("CompressionTypeName"); 204 subNode.setAttribute("value", compressionTypeNames[compression]); 205 node.appendChild(subNode); 206 return node; 207 } 208 209 protected IIOMetadataNode getStandardDataNode() { 210 IIOMetadataNode node = new IIOMetadataNode ("Data"); 211 212 String bits = ""; 213 if (bitsPerPixel == 24) 214 bits = "8 8 8 "; 215 else if (bitsPerPixel == 16 || bitsPerPixel == 32) { 216 bits = "" + countBits(redMask) + " " + countBits(greenMask) + 217 countBits(blueMask) + "" + countBits(alphaMask); 218 } 219 220 IIOMetadataNode subNode = new IIOMetadataNode ("BitsPerSample"); 221 subNode.setAttribute("value", bits); 222 node.appendChild(subNode); 223 224 return node; 225 } 226 227 protected IIOMetadataNode getStandardDimensionNode() { 228 if (yPixelsPerMeter > 0.0F && xPixelsPerMeter > 0.0F) { 229 IIOMetadataNode node = new IIOMetadataNode ("Dimension"); 230 float ratio = yPixelsPerMeter / xPixelsPerMeter; 231 IIOMetadataNode subNode = new IIOMetadataNode ("PixelAspectRatio"); 232 subNode.setAttribute("value", "" + ratio); 233 node.appendChild(subNode); 234 235 subNode = new IIOMetadataNode ("HorizontalPhysicalPixelSpacing"); 236 subNode.setAttribute("value", "" + (1 / xPixelsPerMeter * 1000)); 237 node.appendChild(subNode); 238 239 subNode = new IIOMetadataNode ("VerticalPhysicalPixelSpacing"); 240 subNode.setAttribute("value", "" + (1 / yPixelsPerMeter * 1000)); 241 node.appendChild(subNode); 242 243 return node; 244 } 245 return null; 246 } 247 248 public void setFromTree(String formatName, Node root) { 249 throw new IllegalStateException (I18N.getString("BMPMetadata1")); 250 } 251 252 public void mergeTree(String formatName, Node root) { 253 throw new IllegalStateException (I18N.getString("BMPMetadata1")); 254 } 255 256 public void reset() { 257 throw new IllegalStateException (I18N.getString("BMPMetadata1")); 258 } 259 260 private String countBits(int num) { 261 int count = 0; 262 while(num > 0) { 263 if ((num & 1) == 1) 264 count++; 265 num >>>= 1; 266 } 267 268 return count == 0 ? "" : "" + count; 269 } 270 271 private void addXYZPoints(IIOMetadataNode root, String name, double x, double y, double z) { 272 IIOMetadataNode node = addChildNode(root, name, null); 273 addChildNode(node, "X", new Double (x)); 274 addChildNode(node, "Y", new Double (y)); 275 addChildNode(node, "Z", new Double (z)); 276 } 277 278 private IIOMetadataNode addChildNode(IIOMetadataNode root, 279 String name, 280 Object object) { 281 IIOMetadataNode child = new IIOMetadataNode (name); 282 if (object != null) { 283 child.setUserObject(object); 284 child.setNodeValue(ImageUtil.convertObjectToString(object)); 285 } 286 root.appendChild(child); 287 return child; 288 } 289 } 290 | Popular Tags |