KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > imageio > plugins > bmp > BMPMetadata


1 /*
2  * @(#)BMPMetadata.java 1.3 04/03/19 12:28:41
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package com.sun.imageio.plugins.bmp;
9
10 import java.io.UnsupportedEncodingException JavaDoc;
11 import java.util.ArrayList JavaDoc;
12 import java.util.Iterator JavaDoc;
13 import java.util.List JavaDoc;
14 import javax.imageio.ImageTypeSpecifier JavaDoc;
15 import javax.imageio.metadata.IIOMetadata JavaDoc;
16 import javax.imageio.metadata.IIOMetadataNode JavaDoc;
17 import javax.imageio.metadata.IIOMetadataFormat JavaDoc;
18 import javax.imageio.metadata.IIOMetadataFormatImpl JavaDoc;
19 import org.w3c.dom.Node JavaDoc;
20 import com.sun.imageio.plugins.common.I18N;
21
22 import com.sun.imageio.plugins.common.ImageUtil;
23
24 public class BMPMetadata extends IIOMetadata JavaDoc implements BMPConstants {
25     public static final String JavaDoc nativeMetadataFormatName =
26         "javax_imageio_bmp_1.0";
27
28     // Fields for Image Descriptor
29
public String JavaDoc bmpVersion;
30     public int width ;
31     public int height;
32     public short bitsPerPixel;
33     public int compression;
34     public int imageSize;
35
36     // Fields for PixelsPerMeter
37
public int xPixelsPerMeter;
38     public int yPixelsPerMeter;
39
40     public int colorsUsed;
41     public int colorsImportant;
42
43     // Fields for BI_BITFIELDS compression(Mask)
44
public int redMask;
45     public int greenMask;
46     public int blueMask;
47     public int alphaMask;
48
49     public int colorSpace;
50
51     // Fields for CIE XYZ for the LCS_CALIBRATED_RGB color space
52
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     // Fields for Gamma values for the LCS_CALIBRATED_RGB color space
63
public int gammaRed;
64     public int gammaGreen;
65     public int gammaBlue;
66
67     public int intent;
68
69     // Fields for the Palette and Entries
70
public byte[] palette = null;
71     public int paletteSize;
72     public int red;
73     public int green;
74     public int blue;
75
76     // Fields from CommentExtension
77
// List of byte[]
78
public List JavaDoc comments = null; // new ArrayList();
79

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 JavaDoc getAsTree(String JavaDoc 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 JavaDoc(I18N.getString("BMPMetadata0"));
99         }
100     }
101
102     private String JavaDoc toISO8859(byte[] data) {
103         try {
104             return new String JavaDoc(data, "ISO-8859-1");
105         } catch (UnsupportedEncodingException JavaDoc e) {
106             return "";
107         }
108     }
109
110     private Node JavaDoc getNativeTree() {
111         IIOMetadataNode JavaDoc root =
112             new IIOMetadataNode JavaDoc(nativeMetadataFormatName);
113
114         addChildNode(root, "BMPVersion", bmpVersion);
115         addChildNode(root, "Width", new Integer JavaDoc(width));
116         addChildNode(root, "Height", new Integer JavaDoc(height));
117         addChildNode(root, "BitsPerPixel", new Short JavaDoc(bitsPerPixel));
118         addChildNode(root, "Compression", new Integer JavaDoc(compression));
119         addChildNode(root, "ImageSize", new Integer JavaDoc(imageSize));
120
121         IIOMetadataNode JavaDoc node = addChildNode(root, "PixelsPerMeter", null);
122         addChildNode(node, "X", new Integer JavaDoc(xPixelsPerMeter));
123         addChildNode(node, "Y", new Integer JavaDoc(yPixelsPerMeter));
124
125         addChildNode(root, "ColorsUsed", new Integer JavaDoc(colorsUsed));
126         addChildNode(root, "ColorsImportant", new Integer JavaDoc(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 JavaDoc(redMask));
136             addChildNode(node, "Green", new Integer JavaDoc(greenMask));
137             addChildNode(node, "Blue", new Integer JavaDoc(blueMask));
138             addChildNode(node, "Alpha", new Integer JavaDoc(alphaMask));
139
140             addChildNode(root, "ColorSpaceType", new Integer JavaDoc(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 JavaDoc(intent));
148         }
149
150         // Palette
151
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 JavaDoc 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 JavaDoc((byte)red));
162                 addChildNode(entry, "Green", new Byte JavaDoc((byte)green));
163                 addChildNode(entry, "Blue", new Byte JavaDoc((byte)blue));
164                 if (numComps == 4)
165                     addChildNode(entry, "Alpha",
166                                  new Byte JavaDoc((byte)(palette[j++] & 0xff)));
167             }
168         }
169
170         return root;
171     }
172
173     // Standard tree node methods
174
protected IIOMetadataNode JavaDoc getStandardChromaNode() {
175
176         if ((palette != null) && (paletteSize > 0)) {
177             IIOMetadataNode JavaDoc node = new IIOMetadataNode JavaDoc("Chroma");
178             IIOMetadataNode JavaDoc subNode = new IIOMetadataNode JavaDoc("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 JavaDoc subNode1 = new IIOMetadataNode JavaDoc("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 JavaDoc getStandardCompressionNode() {
200         IIOMetadataNode JavaDoc node = new IIOMetadataNode JavaDoc("Compression");
201
202         // CompressionTypeName
203
IIOMetadataNode JavaDoc subNode = new IIOMetadataNode JavaDoc("CompressionTypeName");
204         subNode.setAttribute("value", compressionTypeNames[compression]);
205         node.appendChild(subNode);
206         return node;
207     }
208
209     protected IIOMetadataNode JavaDoc getStandardDataNode() {
210         IIOMetadataNode JavaDoc node = new IIOMetadataNode JavaDoc("Data");
211
212         String JavaDoc 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 JavaDoc subNode = new IIOMetadataNode JavaDoc("BitsPerSample");
221         subNode.setAttribute("value", bits);
222         node.appendChild(subNode);
223
224         return node;
225     }
226
227     protected IIOMetadataNode JavaDoc getStandardDimensionNode() {
228         if (yPixelsPerMeter > 0.0F && xPixelsPerMeter > 0.0F) {
229             IIOMetadataNode JavaDoc node = new IIOMetadataNode JavaDoc("Dimension");
230             float ratio = yPixelsPerMeter / xPixelsPerMeter;
231             IIOMetadataNode JavaDoc subNode = new IIOMetadataNode JavaDoc("PixelAspectRatio");
232             subNode.setAttribute("value", "" + ratio);
233             node.appendChild(subNode);
234             
235             subNode = new IIOMetadataNode JavaDoc("HorizontalPhysicalPixelSpacing");
236             subNode.setAttribute("value", "" + (1 / xPixelsPerMeter * 1000));
237             node.appendChild(subNode);
238             
239             subNode = new IIOMetadataNode JavaDoc("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 JavaDoc formatName, Node JavaDoc root) {
249         throw new IllegalStateException JavaDoc(I18N.getString("BMPMetadata1"));
250     }
251
252     public void mergeTree(String JavaDoc formatName, Node JavaDoc root) {
253         throw new IllegalStateException JavaDoc(I18N.getString("BMPMetadata1"));
254     }
255
256     public void reset() {
257         throw new IllegalStateException JavaDoc(I18N.getString("BMPMetadata1"));
258     }
259
260     private String JavaDoc 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 JavaDoc root, String JavaDoc name, double x, double y, double z) {
272         IIOMetadataNode JavaDoc node = addChildNode(root, name, null);
273         addChildNode(node, "X", new Double JavaDoc(x));
274         addChildNode(node, "Y", new Double JavaDoc(y));
275         addChildNode(node, "Z", new Double JavaDoc(z));
276     }
277
278     private IIOMetadataNode JavaDoc addChildNode(IIOMetadataNode JavaDoc root,
279                                          String JavaDoc name,
280                                          Object JavaDoc object) {
281         IIOMetadataNode JavaDoc child = new IIOMetadataNode JavaDoc(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