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 // Fields from CommentExtension 95 // List of byte[] 96 public List comments = null; // new ArrayList(); 97 98 public BMPMetadata() { 99 super(true, 100 nativeMetadataFormatName, 101 "com.sun.imageio.plugins.bmp.BMPMetadataFormat", 102 null, null); 103 } 104 105 public boolean isReadOnly() { 106 return true; 107 } 108 109 public Node getAsTree(String formatName) { 110 if (formatName.equals(nativeMetadataFormatName)) { 111 return getNativeTree(); 112 } else if (formatName.equals 113 (IIOMetadataFormatImpl.standardMetadataFormatName)) { 114 return getStandardTree(); 115 } else { 116 throw new IllegalArgumentException(I18N.getString("BMPMetadata0")); 117 } 118 } 119 120 private String toISO8859(byte[] data) { 121 try { 122 return new String(data, "ISO-8859-1"); 123 } catch (UnsupportedEncodingException e) { 124 return ""; 125 } 126 } 127 128 private Node getNativeTree() { 129 IIOMetadataNode root = 130 new IIOMetadataNode(nativeMetadataFormatName); 131 132 addChildNode(root, "BMPVersion", bmpVersion); 133 addChildNode(root, "Width", new Integer(width)); 134 addChildNode(root, "Height", new Integer(height)); 135 addChildNode(root, "BitsPerPixel", new Short(bitsPerPixel)); 136 addChildNode(root, "Compression", new Integer(compression)); 137 addChildNode(root, "ImageSize", new Integer(imageSize)); 138 139 IIOMetadataNode node = addChildNode(root, "PixelsPerMeter", null); 140 addChildNode(node, "X", new Integer(xPixelsPerMeter)); 141 addChildNode(node, "Y", new Integer(yPixelsPerMeter)); 142 143 addChildNode(root, "ColorsUsed", new Integer(colorsUsed)); 144 addChildNode(root, "ColorsImportant", new Integer(colorsImportant)); 145 146 int version = 0; 147 for (int i = 0; i < bmpVersion.length(); i++) 148 if (Character.isDigit(bmpVersion.charAt(i))) 149 version = bmpVersion.charAt(i) -'0'; 150 151 if (version >= 4) { 152 node = addChildNode(root, "Mask", null); 153 addChildNode(node, "Red", new Integer(redMask)); 154 addChildNode(node, "Green", new Integer(greenMask)); 155 addChildNode(node, "Blue", new Integer(blueMask)); 156 addChildNode(node, "Alpha", new Integer(alphaMask)); 157 158 addChildNode(root, "ColorSpaceType", new Integer(colorSpace)); 159 160 node = addChildNode(root, "CIEXYZEndPoints", null); 161 addXYZPoints(node, "Red", redX, redY, redZ); 162 addXYZPoints(node, "Green", greenX, greenY, greenZ); 163 addXYZPoints(node, "Blue", blueX, blueY, blueZ); 164 165 node = addChildNode(root, "Intent", new Integer(intent)); 166 } 167 168 // Palette 169 if ((palette != null) && (paletteSize > 0)) { 170 node = addChildNode(root, "Palette", null); 171 int numComps = palette.length / paletteSize; 172 173 for (int i = 0, j = 0; i < paletteSize; i++) { 174 IIOMetadataNode entry = 175 addChildNode(node, "PaletteEntry", null); 176 red = palette[j++] & 0xff; 177 green = palette[j++] & 0xff; 178 blue = palette[j++] & 0xff; 179 addChildNode(entry, "Red", new Byte((byte)red)); 180 addChildNode(entry, "Green", new Byte((byte)green)); 181 addChildNode(entry, "Blue", new Byte((byte)blue)); 182 if (numComps == 4) 183 addChildNode(entry, "Alpha", 184 new Byte((byte)(palette[j++] & 0xff))); 185 } 186 } 187 188 return root; 189 } 190 191 // Standard tree node methods 192 protected IIOMetadataNode getStandardChromaNode() { 193 194 if ((palette != null) && (paletteSize > 0)) { 195 IIOMetadataNode node = new IIOMetadataNode("Chroma"); 196 IIOMetadataNode subNode = new IIOMetadataNode("Palette"); 197 int numComps = palette.length / paletteSize; 198 subNode.setAttribute("value", "" + numComps); 199 200 for (int i = 0, j = 0; i < paletteSize; i++) { 201 IIOMetadataNode subNode1 = new IIOMetadataNode("PaletteEntry"); 202 subNode1.setAttribute("index", ""+i); 203 subNode1.setAttribute("red", "" + palette[j++]); 204 subNode1.setAttribute("green", "" + palette[j++]); 205 subNode1.setAttribute("blue", "" + palette[j++]); 206 if (numComps == 4 && palette[j] != 0) 207 subNode1.setAttribute("alpha", "" + palette[j++]); 208 subNode.appendChild(subNode1); 209 } 210 node.appendChild(subNode); 211 return node; 212 } 213 214 return null; 215 } 216 217 protected IIOMetadataNode getStandardCompressionNode() { 218 IIOMetadataNode node = new IIOMetadataNode("Compression"); 219 220 // CompressionTypeName 221 IIOMetadataNode subNode = new IIOMetadataNode("CompressionTypeName"); 222 subNode.setAttribute("value", BMPCompressionTypes.getName(compression)); 223 node.appendChild(subNode); 224 return node; 225 } 226 227 protected IIOMetadataNode getStandardDataNode() { 228 IIOMetadataNode node = new IIOMetadataNode("Data"); 229 230 String bits = ""; 231 if (bitsPerPixel == 24) 232 bits = "8 8 8 "; 233 else if (bitsPerPixel == 16 || bitsPerPixel == 32) { 234 bits = "" + countBits(redMask) + " " + countBits(greenMask) + 235 countBits(blueMask) + "" + countBits(alphaMask); 236 } 237 238 IIOMetadataNode subNode = new IIOMetadataNode("BitsPerSample"); 239 subNode.setAttribute("value", bits); 240 node.appendChild(subNode); 241 242 return node; 243 } 244 245 protected IIOMetadataNode getStandardDimensionNode() { 246 if (yPixelsPerMeter > 0.0F && xPixelsPerMeter > 0.0F) { 247 IIOMetadataNode node = new IIOMetadataNode("Dimension"); 248 float ratio = yPixelsPerMeter / xPixelsPerMeter; 249 IIOMetadataNode subNode = new IIOMetadataNode("PixelAspectRatio"); 250 subNode.setAttribute("value", "" + ratio); 251 node.appendChild(subNode); 252 253 subNode = new IIOMetadataNode("HorizontalPhysicalPixelSpacing"); 254 subNode.setAttribute("value", "" + (1 / xPixelsPerMeter * 1000)); 255 node.appendChild(subNode); 256 257 subNode = new IIOMetadataNode("VerticalPhysicalPixelSpacing"); 258 subNode.setAttribute("value", "" + (1 / yPixelsPerMeter * 1000)); 259 node.appendChild(subNode); 260 261 return node; 262 } 263 return null; 264 } 265 266 public void setFromTree(String formatName, Node root) { 267 throw new IllegalStateException(I18N.getString("BMPMetadata1")); 268 } 269 270 public void mergeTree(String formatName, Node root) { 271 throw new IllegalStateException(I18N.getString("BMPMetadata1")); 272 } 273 274 public void reset() { 275 throw new IllegalStateException(I18N.getString("BMPMetadata1")); 276 } 277 278 private String countBits(int num) { 279 int count = 0; 280 while(num > 0) { 281 if ((num & 1) == 1) 282 count++; 283 num >>>= 1; 284 } 285 286 return count == 0 ? "" : "" + count; 287 } 288 289 private void addXYZPoints(IIOMetadataNode root, String name, double x, double y, double z) { 290 IIOMetadataNode node = addChildNode(root, name, null); 291 addChildNode(node, "X", new Double(x)); 292 addChildNode(node, "Y", new Double(y)); 293 addChildNode(node, "Z", new Double(z)); 294 } 295 296 private IIOMetadataNode addChildNode(IIOMetadataNode root, 297 String name, 298 Object object) { 299 IIOMetadataNode child = new IIOMetadataNode(name); 300 if (object != null) { 301 child.setUserObject(object); 302 child.setNodeValue(ImageUtil.convertObjectToString(object)); 303 } 304 root.appendChild(child); 305 return child; 306 } 307 }