KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > image > ImageIOImage


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 /* $Id: ImageIOImage.java 428037 2006-08-02 15:50:20Z jeremias $ */
19
20 package org.apache.fop.image;
21
22 // AWT
23
import java.awt.Color JavaDoc;
24 import java.awt.color.ColorSpace JavaDoc;
25 import java.awt.image.ColorModel JavaDoc;
26 import java.awt.image.IndexColorModel JavaDoc;
27 import java.awt.image.BufferedImage JavaDoc;
28 import java.util.Iterator JavaDoc;
29
30 // ImageIO
31
import javax.imageio.ImageIO JavaDoc;
32 import javax.imageio.ImageReadParam JavaDoc;
33 import javax.imageio.ImageReader JavaDoc;
34 import javax.imageio.metadata.IIOMetadata JavaDoc;
35 import javax.imageio.stream.ImageInputStream JavaDoc;
36
37 import org.apache.commons.io.IOUtils;
38 import org.apache.fop.util.UnitConv;
39 import org.w3c.dom.Element JavaDoc;
40 import org.w3c.dom.NodeList JavaDoc;
41
42 /**
43  * FopImage object using ImageIO.
44  * @see AbstractFopImage
45  * @see FopImage
46  */

47 public class ImageIOImage extends AbstractFopImage {
48
49     /**
50      * Creates a new ImageIOImage.
51      * @param info the image info from the ImageReader
52      */

53     public ImageIOImage(FopImage.ImageInfo info) {
54         super(info);
55         if ("image/png".equals(info.mimeType)
56                 || "image/tiff".equals(info.mimeType)) {
57             this.loaded = 0; //TODO The PNG and TIFF Readers cannot read the resolution, yet.
58
}
59     }
60
61     /**
62      * @see org.apache.fop.image.AbstractFopImage#loadDimensions()
63      */

64     protected boolean loadDimensions() {
65         if (this.bitmaps == null) {
66             return loadBitmap();
67         }
68         return true;
69     }
70     
71     private Element JavaDoc getChild(Element JavaDoc el, String JavaDoc name) {
72         NodeList JavaDoc nodes = el.getElementsByTagName(name);
73         if (nodes.getLength() > 0) {
74             return (Element JavaDoc)nodes.item(0);
75         } else {
76             return null;
77         }
78     }
79     
80     /** @see org.apache.fop.image.AbstractFopImage#loadBitmap() */
81     protected boolean loadBitmap() {
82         if (this.bitmaps != null) {
83             return true;
84         }
85         try {
86             inputStream.reset();
87             ImageInputStream JavaDoc imgStream = ImageIO.createImageInputStream(inputStream);
88             Iterator JavaDoc iter = ImageIO.getImageReaders(imgStream);
89             if (!iter.hasNext()) {
90                 log.error("No ImageReader found.");
91                 return false;
92             }
93             ImageReader JavaDoc reader = (ImageReader JavaDoc)iter.next();
94             ImageReadParam JavaDoc param = reader.getDefaultReadParam();
95             reader.setInput(imgStream, true, false);
96             BufferedImage JavaDoc imageData = reader.read(0, param);
97             
98             //Read image resolution
99
IIOMetadata JavaDoc iiometa = reader.getImageMetadata(0);
100             if (iiometa != null && iiometa.isStandardMetadataFormatSupported()) {
101                 Element JavaDoc metanode = (Element JavaDoc)iiometa.getAsTree("javax_imageio_1.0");
102                 Element JavaDoc dim = getChild(metanode, "Dimension");
103                 if (dim != null) {
104                     Element JavaDoc child;
105                     child = getChild(dim, "HorizontalPixelSize");
106                     if (child != null) {
107                         this.dpiHorizontal = UnitConv.IN2MM
108                                 / Float.parseFloat(child.getAttribute("value"));
109                     }
110                     child = getChild(dim, "VerticalPixelSize");
111                     if (child != null) {
112                         this.dpiVertical = UnitConv.IN2MM
113                                 / Float.parseFloat(child.getAttribute("value"));
114                     }
115                 }
116             }
117             imgStream.close();
118             reader.dispose();
119             
120             this.height = imageData.getHeight();
121             this.width = imageData.getWidth();
122
123             ColorModel JavaDoc cm = imageData.getColorModel();
124             this.bitsPerPixel = cm.getComponentSize(0); //only use first, we assume all are equal
125
//this.colorSpace = cm.getColorSpace();
126
//We currently force the image to sRGB
127
this.colorSpace = ColorSpace.getInstance(ColorSpace.CS_sRGB);
128
129             int[] tmpMap = imageData.getRGB(0, 0, this.width,
130                                             this.height, null, 0, this.width);
131
132             if (cm.hasAlpha()) {
133                 // java.awt.Transparency. BITMASK or OPAQUE or TRANSLUCENT
134
int transparencyType = cm.getTransparency();
135                 
136                 if (transparencyType == java.awt.Transparency.OPAQUE) {
137                     this.isTransparent = false;
138                 } else if (transparencyType == java.awt.Transparency.BITMASK) {
139                     if (cm instanceof IndexColorModel JavaDoc) {
140                         this.isTransparent = false;
141                         byte[] alphas = new byte[
142                                           ((IndexColorModel JavaDoc) cm).getMapSize()];
143                         byte[] reds = new byte[
144                                         ((IndexColorModel JavaDoc) cm).getMapSize()];
145                         byte[] greens = new byte[
146                                           ((IndexColorModel JavaDoc) cm).getMapSize()];
147                         byte[] blues = new byte[
148                                          ((IndexColorModel JavaDoc) cm).getMapSize()];
149                         ((IndexColorModel JavaDoc) cm).getAlphas(alphas);
150                         ((IndexColorModel JavaDoc) cm).getReds(reds);
151                         ((IndexColorModel JavaDoc) cm).getGreens(greens);
152                         ((IndexColorModel JavaDoc) cm).getBlues(blues);
153                         for (int i = 0;
154                                 i < ((IndexColorModel JavaDoc) cm).getMapSize();
155                                 i++) {
156                             if ((alphas[i] & 0xFF) == 0) {
157                                 this.isTransparent = true;
158                                 this.transparentColor = new Color JavaDoc(
159                                                             (int)(reds[i] & 0xFF),
160                                                             (int)(greens[i] & 0xFF),
161                                                             (int)(blues[i] & 0xFF));
162                                 break;
163                             }
164                         }
165                     } else {
166                         // TRANSLUCENT
167
/*
168                          * this.isTransparent = false;
169                          * for (int i = 0; i < this.width * this.height; i++) {
170                          * if (cm.getAlpha(tmpMap[i]) == 0) {
171                          * this.isTransparent = true;
172                          * this.transparentColor = new PDFColor(cm.getRed(tmpMap[i]),
173                          * cm.getGreen(tmpMap[i]), cm.getBlue(tmpMap[i]));
174                          * break;
175                          * }
176                          * }
177                          * // or use special API...
178                          */

179                         this.isTransparent = false;
180                     }
181                 } else {
182                     this.isTransparent = false;
183                 }
184             } else {
185                 this.isTransparent = false;
186             }
187
188             // Should take care of the ColorSpace and bitsPerPixel
189
this.bitmaps = new byte[this.width * this.height * 3];
190             for (int i = 0; i < this.height; i++) {
191                 for (int j = 0; j < this.width; j++) {
192                     int p = tmpMap[i * this.width + j];
193                     int r = (p >> 16) & 0xFF;
194                     int g = (p >> 8) & 0xFF;
195                     int b = (p) & 0xFF;
196                     this.bitmaps[3 * (i * this.width + j)]
197                         = (byte)(r & 0xFF);
198                     this.bitmaps[3 * (i * this.width + j) + 1]
199                         = (byte)(g & 0xFF);
200                     this.bitmaps[3 * (i * this.width + j) + 2]
201                         = (byte)(b & 0xFF);
202                 }
203             }
204
205         } catch (Exception JavaDoc ex) {
206             log.error("Error while loading image: " + ex.getMessage(), ex);
207             return false;
208         } finally {
209             IOUtils.closeQuietly(inputStream);
210             inputStream = null;
211         }
212         return true;
213     }
214
215     /** @see org.apache.fop.image.AbstractFopImage#loadOriginalData() */
216     protected boolean loadOriginalData() {
217         return loadDefaultOriginalData();
218     }
219     
220 }
221
222
Popular Tags