KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: JAIImage.java,v 1.2.2.1 2003/02/25 13:38:22 jeremias Exp $
3  * ============================================================================
4  * The Apache Software License, Version 1.1
5  * ============================================================================
6  *
7  * Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without modifica-
10  * tion, are permitted provided that the following conditions are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright notice,
16  * this list of conditions and the following disclaimer in the documentation
17  * and/or other materials provided with the distribution.
18  *
19  * 3. The end-user documentation included with the redistribution, if any, must
20  * include the following acknowledgment: "This product includes software
21  * developed by the Apache Software Foundation (http://www.apache.org/)."
22  * Alternately, this acknowledgment may appear in the software itself, if
23  * and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. The names "FOP" and "Apache Software Foundation" must not be used to
26  * endorse or promote products derived from this software without prior
27  * written permission. For written permission, please contact
28  * apache@apache.org.
29  *
30  * 5. Products derived from this software may not be called "Apache", nor may
31  * "Apache" appear in their name, without prior written permission of the
32  * Apache Software Foundation.
33  *
34  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
35  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
36  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
37  * APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
38  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
39  * DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
40  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
41  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
42  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
43  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44  * ============================================================================
45  *
46  * This software consists of voluntary contributions made by many individuals
47  * on behalf of the Apache Software Foundation and was originally created by
48  * James Tauber <jtauber@jtauber.com>. For more information on the Apache
49  * Software Foundation, please see <http://www.apache.org/>.
50  */

51 package org.apache.fop.image;
52
53 // Java
54
import java.net.URL JavaDoc;
55 import java.io.InputStream JavaDoc;
56
57 // AWT
58
import java.awt.image.ColorModel JavaDoc;
59 import java.awt.image.IndexColorModel JavaDoc;
60 import java.awt.image.BufferedImage JavaDoc;
61
62 // JAI
63
import javax.media.jai.JAI;
64 import javax.media.jai.RenderedOp;
65 // Sun codec
66
import com.sun.media.jai.codec.FileCacheSeekableStream;
67 // FOP
68
import org.apache.fop.datatypes.ColorSpace;
69 import org.apache.fop.pdf.PDFColor;
70 import org.apache.fop.image.analyser.ImageReader;
71
72 /**
73  * FopImage object using JAI.
74  * @author Eric SCHAEFFER
75  * @see AbstractFopImage
76  * @see FopImage
77  */

78 public class JAIImage extends AbstractFopImage {
79     public JAIImage(URL JavaDoc href) throws FopImageException {
80         super(href);
81     }
82
83     public JAIImage(URL JavaDoc href,
84                     ImageReader imgReader) throws FopImageException {
85         super(href, imgReader);
86     }
87
88     protected void loadImage() throws FopImageException {
89         try {
90             InputStream JavaDoc inputStream = this.m_href.openStream();
91             /*
92              * BufferedInputStream inputStream = this.m_imageReader.getInputStream();
93              * inputStream.reset();
94              */

95             com.sun.media.jai.codec.FileCacheSeekableStream seekableInput =
96                 new FileCacheSeekableStream(inputStream);
97             RenderedOp imageOp = JAI.create("stream", seekableInput);
98
99             this.m_height = imageOp.getHeight();
100             this.m_width = imageOp.getWidth();
101
102             ColorModel JavaDoc cm = imageOp.getColorModel();
103             this.m_bitsPerPixel = 8;
104             // this.m_bitsPerPixel = cm.getPixelSize();
105
this.m_colorSpace = new ColorSpace(ColorSpace.DEVICE_RGB);
106
107             BufferedImage JavaDoc imageData = imageOp.getAsBufferedImage();
108             int[] tmpMap = imageData.getRGB(0, 0, this.m_width,
109                                             this.m_height, null, 0,
110                                             this.m_width);
111
112             if (cm.hasAlpha()) {
113                 int transparencyType =
114                     cm.getTransparency(); // java.awt.Transparency. BITMASK or OPAQUE or TRANSLUCENT
115
if (transparencyType == java.awt.Transparency.OPAQUE) {
116                     this.m_isTransparent = false;
117                 } else if (transparencyType
118                            == java.awt.Transparency.BITMASK) {
119                     if (cm instanceof IndexColorModel JavaDoc) {
120                         this.m_isTransparent = false;
121                         byte[] alphas =
122                             new byte[((IndexColorModel JavaDoc)cm).getMapSize()];
123                         byte[] reds =
124                             new byte[((IndexColorModel JavaDoc)cm).getMapSize()];
125                         byte[] greens =
126                             new byte[((IndexColorModel JavaDoc)cm).getMapSize()];
127                         byte[] blues =
128                             new byte[((IndexColorModel JavaDoc)cm).getMapSize()];
129                         ((IndexColorModel JavaDoc)cm).getAlphas(alphas);
130                         ((IndexColorModel JavaDoc)cm).getReds(reds);
131                         ((IndexColorModel JavaDoc)cm).getGreens(greens);
132                         ((IndexColorModel JavaDoc)cm).getBlues(blues);
133                         for (int i = 0;
134                                 i < ((IndexColorModel JavaDoc)cm).getMapSize(); i++) {
135                             if ((alphas[i] & 0xFF) == 0) {
136                                 this.m_isTransparent = true;
137                                 this.m_transparentColor =
138                                     new PDFColor((int)(reds[i] & 0xFF),
139                                                  (int)(greens[i] & 0xFF),
140                                                  (int)(blues[i] & 0xFF));
141                                 break;
142                             }
143                         }
144                     } else {
145                         // TRANSLUCENT
146
/*
147                          * this.m_isTransparent = false;
148                          * for (int i = 0; i < this.m_width * this.m_height; i++) {
149                          * if (cm.getAlpha(tmpMap[i]) == 0) {
150                          * this.m_isTransparent = true;
151                          * this.m_transparentColor = new PDFColor(cm.getRed(tmpMap[i]), cm.getGreen(tmpMap[i]), cm.getBlue(tmpMap[i]));
152                          * break;
153                          * }
154                          * }
155                          * // or use special API...
156                          */

157                         this.m_isTransparent = false;
158                     }
159                 } else {
160                     this.m_isTransparent = false;
161                 }
162             } else {
163                 this.m_isTransparent = false;
164             }
165
166             // Should take care of the ColorSpace and bitsPerPixel
167
this.m_bitmapsSize = this.m_width * this.m_height * 3;
168             this.m_bitmaps = new byte[this.m_bitmapsSize];
169             for (int i = 0; i < this.m_height; i++) {
170                 for (int j = 0; j < this.m_width; j++) {
171                     int p = tmpMap[i * this.m_width + j];
172                     int r = (p >> 16) & 0xFF;
173                     int g = (p >> 8) & 0xFF;
174                     int b = (p) & 0xFF;
175                     this.m_bitmaps[3 * (i * this.m_width + j)] = (byte)(r
176                             & 0xFF);
177                     this.m_bitmaps[3 * (i * this.m_width + j) + 1] = (byte)(g
178                             & 0xFF);
179                     this.m_bitmaps[3 * (i * this.m_width + j) + 2] = (byte)(b
180                             & 0xFF);
181                 }
182             }
183
184         } catch (Exception JavaDoc ex) {
185             throw new FopImageException("Error while loading image "
186                                         + this.m_href.toString() + " : "
187                                         + ex.getClass() + " - "
188                                         + ex.getMessage());
189         }
190     }
191
192 }
193
194
Popular Tags