0N/A/*
2362N/A * Copyright (c) 2003, 2004, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage com.sun.imageio.plugins.bmp;
0N/A
0N/Aimport java.io.UnsupportedEncodingException;
0N/Aimport java.util.ArrayList;
0N/Aimport java.util.Iterator;
0N/Aimport java.util.List;
0N/Aimport javax.imageio.ImageTypeSpecifier;
0N/Aimport javax.imageio.metadata.IIOMetadata;
0N/Aimport javax.imageio.metadata.IIOMetadataNode;
0N/Aimport javax.imageio.metadata.IIOMetadataFormat;
0N/Aimport javax.imageio.metadata.IIOMetadataFormatImpl;
0N/Aimport org.w3c.dom.Node;
0N/Aimport com.sun.imageio.plugins.common.I18N;
0N/A
0N/Aimport com.sun.imageio.plugins.common.ImageUtil;
0N/A
0N/Apublic class BMPMetadata extends IIOMetadata implements BMPConstants {
0N/A public static final String nativeMetadataFormatName =
0N/A "javax_imageio_bmp_1.0";
0N/A
0N/A // Fields for Image Descriptor
0N/A public String bmpVersion;
0N/A public int width ;
0N/A public int height;
0N/A public short bitsPerPixel;
0N/A public int compression;
0N/A public int imageSize;
0N/A
0N/A // Fields for PixelsPerMeter
0N/A public int xPixelsPerMeter;
0N/A public int yPixelsPerMeter;
0N/A
0N/A public int colorsUsed;
0N/A public int colorsImportant;
0N/A
0N/A // Fields for BI_BITFIELDS compression(Mask)
0N/A public int redMask;
0N/A public int greenMask;
0N/A public int blueMask;
0N/A public int alphaMask;
0N/A
0N/A public int colorSpace;
0N/A
0N/A // Fields for CIE XYZ for the LCS_CALIBRATED_RGB color space
0N/A public double redX;
0N/A public double redY;
0N/A public double redZ;
0N/A public double greenX;
0N/A public double greenY;
0N/A public double greenZ;
0N/A public double blueX;
0N/A public double blueY;
0N/A public double blueZ;
0N/A
0N/A // Fields for Gamma values for the LCS_CALIBRATED_RGB color space
0N/A public int gammaRed;
0N/A public int gammaGreen;
0N/A public int gammaBlue;
0N/A
0N/A public int intent;
0N/A
0N/A // Fields for the Palette and Entries
0N/A public byte[] palette = null;
0N/A public int paletteSize;
0N/A public int red;
0N/A public int green;
0N/A public int blue;
0N/A
0N/A // Fields from CommentExtension
0N/A // List of byte[]
0N/A public List comments = null; // new ArrayList();
0N/A
0N/A public BMPMetadata() {
0N/A super(true,
0N/A nativeMetadataFormatName,
0N/A "com.sun.imageio.plugins.bmp.BMPMetadataFormat",
0N/A null, null);
0N/A }
0N/A
0N/A public boolean isReadOnly() {
0N/A return true;
0N/A }
0N/A
0N/A public Node getAsTree(String formatName) {
0N/A if (formatName.equals(nativeMetadataFormatName)) {
0N/A return getNativeTree();
0N/A } else if (formatName.equals
0N/A (IIOMetadataFormatImpl.standardMetadataFormatName)) {
0N/A return getStandardTree();
0N/A } else {
0N/A throw new IllegalArgumentException(I18N.getString("BMPMetadata0"));
0N/A }
0N/A }
0N/A
0N/A private String toISO8859(byte[] data) {
0N/A try {
0N/A return new String(data, "ISO-8859-1");
0N/A } catch (UnsupportedEncodingException e) {
0N/A return "";
0N/A }
0N/A }
0N/A
0N/A private Node getNativeTree() {
0N/A IIOMetadataNode root =
0N/A new IIOMetadataNode(nativeMetadataFormatName);
0N/A
0N/A addChildNode(root, "BMPVersion", bmpVersion);
0N/A addChildNode(root, "Width", new Integer(width));
0N/A addChildNode(root, "Height", new Integer(height));
0N/A addChildNode(root, "BitsPerPixel", new Short(bitsPerPixel));
0N/A addChildNode(root, "Compression", new Integer(compression));
0N/A addChildNode(root, "ImageSize", new Integer(imageSize));
0N/A
0N/A IIOMetadataNode node = addChildNode(root, "PixelsPerMeter", null);
0N/A addChildNode(node, "X", new Integer(xPixelsPerMeter));
0N/A addChildNode(node, "Y", new Integer(yPixelsPerMeter));
0N/A
0N/A addChildNode(root, "ColorsUsed", new Integer(colorsUsed));
0N/A addChildNode(root, "ColorsImportant", new Integer(colorsImportant));
0N/A
0N/A int version = 0;
0N/A for (int i = 0; i < bmpVersion.length(); i++)
0N/A if (Character.isDigit(bmpVersion.charAt(i)))
0N/A version = bmpVersion.charAt(i) -'0';
0N/A
0N/A if (version >= 4) {
0N/A node = addChildNode(root, "Mask", null);
0N/A addChildNode(node, "Red", new Integer(redMask));
0N/A addChildNode(node, "Green", new Integer(greenMask));
0N/A addChildNode(node, "Blue", new Integer(blueMask));
0N/A addChildNode(node, "Alpha", new Integer(alphaMask));
0N/A
0N/A addChildNode(root, "ColorSpaceType", new Integer(colorSpace));
0N/A
0N/A node = addChildNode(root, "CIEXYZEndPoints", null);
0N/A addXYZPoints(node, "Red", redX, redY, redZ);
0N/A addXYZPoints(node, "Green", greenX, greenY, greenZ);
0N/A addXYZPoints(node, "Blue", blueX, blueY, blueZ);
0N/A
0N/A node = addChildNode(root, "Intent", new Integer(intent));
0N/A }
0N/A
0N/A // Palette
0N/A if ((palette != null) && (paletteSize > 0)) {
0N/A node = addChildNode(root, "Palette", null);
0N/A int numComps = palette.length / paletteSize;
0N/A
0N/A for (int i = 0, j = 0; i < paletteSize; i++) {
0N/A IIOMetadataNode entry =
0N/A addChildNode(node, "PaletteEntry", null);
0N/A red = palette[j++] & 0xff;
0N/A green = palette[j++] & 0xff;
0N/A blue = palette[j++] & 0xff;
0N/A addChildNode(entry, "Red", new Byte((byte)red));
0N/A addChildNode(entry, "Green", new Byte((byte)green));
0N/A addChildNode(entry, "Blue", new Byte((byte)blue));
0N/A if (numComps == 4)
0N/A addChildNode(entry, "Alpha",
0N/A new Byte((byte)(palette[j++] & 0xff)));
0N/A }
0N/A }
0N/A
0N/A return root;
0N/A }
0N/A
0N/A // Standard tree node methods
0N/A protected IIOMetadataNode getStandardChromaNode() {
0N/A
0N/A if ((palette != null) && (paletteSize > 0)) {
0N/A IIOMetadataNode node = new IIOMetadataNode("Chroma");
0N/A IIOMetadataNode subNode = new IIOMetadataNode("Palette");
0N/A int numComps = palette.length / paletteSize;
0N/A subNode.setAttribute("value", "" + numComps);
0N/A
0N/A for (int i = 0, j = 0; i < paletteSize; i++) {
0N/A IIOMetadataNode subNode1 = new IIOMetadataNode("PaletteEntry");
0N/A subNode1.setAttribute("index", ""+i);
0N/A subNode1.setAttribute("red", "" + palette[j++]);
0N/A subNode1.setAttribute("green", "" + palette[j++]);
0N/A subNode1.setAttribute("blue", "" + palette[j++]);
0N/A if (numComps == 4 && palette[j] != 0)
0N/A subNode1.setAttribute("alpha", "" + palette[j++]);
0N/A subNode.appendChild(subNode1);
0N/A }
0N/A node.appendChild(subNode);
0N/A return node;
0N/A }
0N/A
0N/A return null;
0N/A }
0N/A
0N/A protected IIOMetadataNode getStandardCompressionNode() {
0N/A IIOMetadataNode node = new IIOMetadataNode("Compression");
0N/A
0N/A // CompressionTypeName
0N/A IIOMetadataNode subNode = new IIOMetadataNode("CompressionTypeName");
0N/A subNode.setAttribute("value", compressionTypeNames[compression]);
0N/A node.appendChild(subNode);
0N/A return node;
0N/A }
0N/A
0N/A protected IIOMetadataNode getStandardDataNode() {
0N/A IIOMetadataNode node = new IIOMetadataNode("Data");
0N/A
0N/A String bits = "";
0N/A if (bitsPerPixel == 24)
0N/A bits = "8 8 8 ";
0N/A else if (bitsPerPixel == 16 || bitsPerPixel == 32) {
0N/A bits = "" + countBits(redMask) + " " + countBits(greenMask) +
0N/A countBits(blueMask) + "" + countBits(alphaMask);
0N/A }
0N/A
0N/A IIOMetadataNode subNode = new IIOMetadataNode("BitsPerSample");
0N/A subNode.setAttribute("value", bits);
0N/A node.appendChild(subNode);
0N/A
0N/A return node;
0N/A }
0N/A
0N/A protected IIOMetadataNode getStandardDimensionNode() {
0N/A if (yPixelsPerMeter > 0.0F && xPixelsPerMeter > 0.0F) {
0N/A IIOMetadataNode node = new IIOMetadataNode("Dimension");
0N/A float ratio = yPixelsPerMeter / xPixelsPerMeter;
0N/A IIOMetadataNode subNode = new IIOMetadataNode("PixelAspectRatio");
0N/A subNode.setAttribute("value", "" + ratio);
0N/A node.appendChild(subNode);
0N/A
0N/A subNode = new IIOMetadataNode("HorizontalPhysicalPixelSpacing");
0N/A subNode.setAttribute("value", "" + (1 / xPixelsPerMeter * 1000));
0N/A node.appendChild(subNode);
0N/A
0N/A subNode = new IIOMetadataNode("VerticalPhysicalPixelSpacing");
0N/A subNode.setAttribute("value", "" + (1 / yPixelsPerMeter * 1000));
0N/A node.appendChild(subNode);
0N/A
0N/A return node;
0N/A }
0N/A return null;
0N/A }
0N/A
0N/A public void setFromTree(String formatName, Node root) {
0N/A throw new IllegalStateException(I18N.getString("BMPMetadata1"));
0N/A }
0N/A
0N/A public void mergeTree(String formatName, Node root) {
0N/A throw new IllegalStateException(I18N.getString("BMPMetadata1"));
0N/A }
0N/A
0N/A public void reset() {
0N/A throw new IllegalStateException(I18N.getString("BMPMetadata1"));
0N/A }
0N/A
0N/A private String countBits(int num) {
0N/A int count = 0;
0N/A while(num > 0) {
0N/A if ((num & 1) == 1)
0N/A count++;
0N/A num >>>= 1;
0N/A }
0N/A
0N/A return count == 0 ? "" : "" + count;
0N/A }
0N/A
0N/A private void addXYZPoints(IIOMetadataNode root, String name, double x, double y, double z) {
0N/A IIOMetadataNode node = addChildNode(root, name, null);
0N/A addChildNode(node, "X", new Double(x));
0N/A addChildNode(node, "Y", new Double(y));
0N/A addChildNode(node, "Z", new Double(z));
0N/A }
0N/A
0N/A private IIOMetadataNode addChildNode(IIOMetadataNode root,
0N/A String name,
0N/A Object object) {
0N/A IIOMetadataNode child = new IIOMetadataNode(name);
0N/A if (object != null) {
0N/A child.setUserObject(object);
0N/A child.setNodeValue(ImageUtil.convertObjectToString(object));
0N/A }
0N/A root.appendChild(child);
0N/A return child;
0N/A }
0N/A}