1 /* 2 * Copyright (c) 2003, 2004, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package com.sun.imageio.plugins.bmp; 27 28 import java.io.UnsupportedEncodingException; 29 import java.util.ArrayList; 30 import java.util.Iterator; 31 import java.util.List; 32 import javax.imageio.ImageTypeSpecifier; 33 import javax.imageio.metadata.IIOMetadata; 34 import javax.imageio.metadata.IIOMetadataNode; 35 import javax.imageio.metadata.IIOMetadataFormat; 36 import javax.imageio.metadata.IIOMetadataFormatImpl; 37 import org.w3c.dom.Node; 38 import com.sun.imageio.plugins.common.I18N; 39 40 import com.sun.imageio.plugins.common.ImageUtil; 41 42 public class BMPMetadata extends IIOMetadata implements BMPConstants { 43 public static final String nativeMetadataFormatName = 44 "javax_imageio_bmp_1.0"; 45 46 // Fields for Image Descriptor 47 public String bmpVersion; 48 public int width ; 49 public int height; 50 public short bitsPerPixel; 51 public int compression; 52 public int imageSize; 53 54 // Fields for PixelsPerMeter 55 public int xPixelsPerMeter; 56 public int yPixelsPerMeter; 57 58 public int colorsUsed; 59 public int colorsImportant; 60 61 // Fields for BI_BITFIELDS compression(Mask) 62 public int redMask; 63 public int greenMask; 64 public int blueMask; 65 public int alphaMask; 66 67 public int colorSpace; 68 69 // Fields for CIE XYZ for the LCS_CALIBRATED_RGB color space 70 public double redX; 71 public double redY; 72 public double redZ; 73 public double greenX; 74 public double greenY; 75 public double greenZ; 76 public double blueX; 77 public double blueY; 78 public double blueZ; 79 80 // Fields for Gamma values for the LCS_CALIBRATED_RGB color space 81 public int gammaRed; 82 public int gammaGreen; 83 public int gammaBlue; 84 85 public int intent; 86 87 // Fields for the Palette and Entries 88 public byte[] palette = null; 89 public int paletteSize; 90 public int red; 91 public int green; 92 public int blue; 93 94 public BMPMetadata() { 95 super(true, 96 nativeMetadataFormatName, 97 "com.sun.imageio.plugins.bmp.BMPMetadataFormat", 98 null, null); 99 } 100 101 public boolean isReadOnly() { 102 return true; 103 } 104 105 public Node getAsTree(String formatName) { 106 if (formatName.equals(nativeMetadataFormatName)) { 107 return getNativeTree(); 108 } else if (formatName.equals 109 (IIOMetadataFormatImpl.standardMetadataFormatName)) { 110 return getStandardTree(); 111 } else { 112 throw new IllegalArgumentException(I18N.getString("BMPMetadata0")); 113 } 114 } 115 116 private String toISO8859(byte[] data) { 117 try { 118 return new String(data, "ISO-8859-1"); 119 } catch (UnsupportedEncodingException e) { 120 return ""; 121 } 122 } 123 124 private Node getNativeTree() { 125 IIOMetadataNode root = 126 new IIOMetadataNode(nativeMetadataFormatName); 127 128 addChildNode(root, "BMPVersion", bmpVersion); 129 addChildNode(root, "Width", new Integer(width)); 130 addChildNode(root, "Height", new Integer(height)); 131 addChildNode(root, "BitsPerPixel", new Short(bitsPerPixel)); 132 addChildNode(root, "Compression", new Integer(compression)); 133 addChildNode(root, "ImageSize", new Integer(imageSize)); 134 135 IIOMetadataNode node = addChildNode(root, "PixelsPerMeter", null); 136 addChildNode(node, "X", new Integer(xPixelsPerMeter)); 137 addChildNode(node, "Y", new Integer(yPixelsPerMeter)); 138 139 addChildNode(root, "ColorsUsed", new Integer(colorsUsed)); 140 addChildNode(root, "ColorsImportant", new Integer(colorsImportant)); 141 142 int version = 0; 143 for (int i = 0; i < bmpVersion.length(); i++) 144 if (Character.isDigit(bmpVersion.charAt(i))) 145 version = bmpVersion.charAt(i) -'0'; 146 147 if (version >= 4) { 148 node = addChildNode(root, "Mask", null); 149 addChildNode(node, "Red", new Integer(redMask)); 150 addChildNode(node, "Green", new Integer(greenMask)); 151 addChildNode(node, "Blue", new Integer(blueMask)); 152 addChildNode(node, "Alpha", new Integer(alphaMask)); 153 154 addChildNode(root, "ColorSpaceType", new Integer(colorSpace)); 155 156 node = addChildNode(root, "CIEXYZEndPoints", null); 157 addXYZPoints(node, "Red", redX, redY, redZ); 158 addXYZPoints(node, "Green", greenX, greenY, greenZ); 159 addXYZPoints(node, "Blue", blueX, blueY, blueZ); 160 161 node = addChildNode(root, "Intent", new Integer(intent)); 162 } 163 164 // Palette 165 if ((palette != null) && (paletteSize > 0)) { 166 node = addChildNode(root, "Palette", null); 167 int numComps = palette.length / paletteSize; 168 169 for (int i = 0, j = 0; i < paletteSize; i++) { 170 IIOMetadataNode entry = 171 addChildNode(node, "PaletteEntry", null); 172 red = palette[j++] & 0xff; 173 green = palette[j++] & 0xff; 174 blue = palette[j++] & 0xff; 175 addChildNode(entry, "Red", new Byte((byte)red)); 176 addChildNode(entry, "Green", new Byte((byte)green)); 177 addChildNode(entry, "Blue", new Byte((byte)blue)); 178 if (numComps == 4) 179 addChildNode(entry, "Alpha", 180 new Byte((byte)(palette[j++] & 0xff))); 181 } 182 } 183 184 return root; 185 } 186 187 // Standard tree node methods 188 protected IIOMetadataNode getStandardChromaNode() { 189 190 if ((palette != null) && (paletteSize > 0)) { 191 IIOMetadataNode node = new IIOMetadataNode("Chroma"); 192 IIOMetadataNode subNode = new IIOMetadataNode("Palette"); 193 int numComps = palette.length / paletteSize; 194 subNode.setAttribute("value", "" + numComps); 195 196 for (int i = 0, j = 0; i < paletteSize; i++) { 197 IIOMetadataNode subNode1 = new IIOMetadataNode("PaletteEntry"); 198 subNode1.setAttribute("index", ""+i); 199 subNode1.setAttribute("red", "" + palette[j++]); 200 subNode1.setAttribute("green", "" + palette[j++]); 201 subNode1.setAttribute("blue", "" + palette[j++]); 202 if (numComps == 4 && palette[j] != 0) 203 subNode1.setAttribute("alpha", "" + palette[j++]); 204 subNode.appendChild(subNode1); 205 } 206 node.appendChild(subNode); 207 return node; 208 } 209 210 return null; 211 } 212 213 protected IIOMetadataNode getStandardCompressionNode() { 214 IIOMetadataNode node = new IIOMetadataNode("Compression"); 215 216 // CompressionTypeName 217 IIOMetadataNode subNode = new IIOMetadataNode("CompressionTypeName"); 218 subNode.setAttribute("value", BMPCompressionTypes.getName(compression)); 219 node.appendChild(subNode); 220 return node; 221 } 222 223 protected IIOMetadataNode getStandardDataNode() { 224 IIOMetadataNode node = new IIOMetadataNode("Data"); 225 226 String bits = ""; 227 if (bitsPerPixel == 24) 228 bits = "8 8 8 "; 229 else if (bitsPerPixel == 16 || bitsPerPixel == 32) { 230 bits = "" + countBits(redMask) + " " + countBits(greenMask) + 231 countBits(blueMask) + "" + countBits(alphaMask); 232 } 233 234 IIOMetadataNode subNode = new IIOMetadataNode("BitsPerSample"); 235 subNode.setAttribute("value", bits); 236 node.appendChild(subNode); 237 238 return node; 239 } 240 241 protected IIOMetadataNode getStandardDimensionNode() { 242 if (yPixelsPerMeter > 0.0F && xPixelsPerMeter > 0.0F) { 243 IIOMetadataNode node = new IIOMetadataNode("Dimension"); 244 float ratio = yPixelsPerMeter / xPixelsPerMeter; 245 IIOMetadataNode subNode = new IIOMetadataNode("PixelAspectRatio"); 246 subNode.setAttribute("value", "" + ratio); 247 node.appendChild(subNode); 248 249 subNode = new IIOMetadataNode("HorizontalPhysicalPixelSpacing"); 250 subNode.setAttribute("value", "" + (1 / xPixelsPerMeter * 1000)); 251 node.appendChild(subNode); 252 253 subNode = new IIOMetadataNode("VerticalPhysicalPixelSpacing"); 254 subNode.setAttribute("value", "" + (1 / yPixelsPerMeter * 1000)); 255 node.appendChild(subNode); 256 257 return node; 258 } 259 return null; 260 } 261 262 public void setFromTree(String formatName, Node root) { 263 throw new IllegalStateException(I18N.getString("BMPMetadata1")); 264 } 265 266 public void mergeTree(String formatName, Node root) { 267 throw new IllegalStateException(I18N.getString("BMPMetadata1")); 268 } 269 270 public void reset() { 271 throw new IllegalStateException(I18N.getString("BMPMetadata1")); 272 } 273 274 private String countBits(int num) { 275 int count = 0; 276 while(num > 0) { 277 if ((num & 1) == 1) 278 count++; 279 num >>>= 1; 280 } 281 282 return count == 0 ? "" : "" + count; 283 } 284 285 private void addXYZPoints(IIOMetadataNode root, String name, double x, double y, double z) { 286 IIOMetadataNode node = addChildNode(root, name, null); 287 addChildNode(node, "X", new Double(x)); 288 addChildNode(node, "Y", new Double(y)); 289 addChildNode(node, "Z", new Double(z)); 290 } 291 292 private IIOMetadataNode addChildNode(IIOMetadataNode root, 293 String name, 294 Object object) { 295 IIOMetadataNode child = new IIOMetadataNode(name); 296 if (object != null) { 297 child.setUserObject(object); 298 child.setNodeValue(ImageUtil.convertObjectToString(object)); 299 } 300 root.appendChild(child); 301 return child; 302 } 303 }