KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: JimiImage.java,v 1.8.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.awt.image.ImageProducer JavaDoc;
56 import java.awt.image.ColorModel JavaDoc;
57 import java.awt.image.IndexColorModel JavaDoc;
58
59 // Jimi
60
import com.sun.jimi.core.*;
61
62 // FOP
63
import org.apache.fop.datatypes.ColorSpace;
64 import org.apache.fop.pdf.PDFColor;
65 import org.apache.fop.image.analyser.ImageReader;
66
67 /**
68  * FopImage object for several images types, using Jimi.
69  * See Jimi documentation for supported image types.
70  * @author Eric SCHAEFFER
71  * @see AbstractFopImage
72  * @see FopImage
73  */

74 public class JimiImage extends AbstractFopImage {
75     public JimiImage(URL JavaDoc href) throws FopImageException {
76         super(href);
77         try {
78             Class JavaDoc c = Class.forName("com.sun.jimi.core.Jimi");
79         } catch (ClassNotFoundException JavaDoc e) {
80             throw new FopImageException("Jimi image library not available");
81         }
82     }
83
84     public JimiImage(URL JavaDoc href,
85                      ImageReader imgReader) throws FopImageException {
86         super(href, imgReader);
87         try {
88             Class JavaDoc c = Class.forName("com.sun.jimi.core.Jimi");
89         } catch (ClassNotFoundException JavaDoc e) {
90             throw new FopImageException("Jimi image library not available");
91         }
92     }
93
94     protected void loadImage() throws FopImageException {
95         int[] tmpMap = null;
96         try {
97             ImageProducer JavaDoc ip = Jimi.getImageProducer(this.m_href.openStream(),
98                                                      Jimi.SYNCHRONOUS
99                                                      | Jimi.IN_MEMORY);
100             FopImageConsumer consumer = new FopImageConsumer(ip);
101             ip.startProduction(consumer);
102
103             while (!consumer.isImageReady()) {
104                 Thread.sleep(500);
105             }
106             this.m_height = consumer.getHeight();
107             this.m_width = consumer.getWidth();
108
109             try {
110                 tmpMap = consumer.getImage();
111             } catch (Exception JavaDoc ex) {
112                 throw new FopImageException("Image grabbing interrupted : "
113                                             + ex.getMessage());
114             }
115
116             ColorModel JavaDoc cm = consumer.getColorModel();
117             this.m_bitsPerPixel = 8;
118             // this.m_bitsPerPixel = cm.getPixelSize();
119
this.m_colorSpace = new ColorSpace(ColorSpace.DEVICE_RGB);
120             if (cm.hasAlpha()) {
121                 int transparencyType =
122                     cm.getTransparency(); // java.awt.Transparency. BITMASK or OPAQUE or TRANSLUCENT
123
if (transparencyType == java.awt.Transparency.OPAQUE) {
124                     this.m_isTransparent = false;
125                 } else if (transparencyType
126                            == java.awt.Transparency.BITMASK) {
127                     if (cm instanceof IndexColorModel JavaDoc) {
128                         this.m_isTransparent = false;
129                         byte[] alphas =
130                             new byte[((IndexColorModel JavaDoc)cm).getMapSize()];
131                         byte[] reds =
132                             new byte[((IndexColorModel JavaDoc)cm).getMapSize()];
133                         byte[] greens =
134                             new byte[((IndexColorModel JavaDoc)cm).getMapSize()];
135                         byte[] blues =
136                             new byte[((IndexColorModel JavaDoc)cm).getMapSize()];
137                         ((IndexColorModel JavaDoc)cm).getAlphas(alphas);
138                         ((IndexColorModel JavaDoc)cm).getReds(reds);
139                         ((IndexColorModel JavaDoc)cm).getGreens(greens);
140                         ((IndexColorModel JavaDoc)cm).getBlues(blues);
141                         for (int i = 0;
142                                 i < ((IndexColorModel JavaDoc)cm).getMapSize(); i++) {
143                             if ((alphas[i] & 0xFF) == 0) {
144                                 this.m_isTransparent = true;
145                                 this.m_transparentColor =
146                                     new PDFColor((int)(reds[i] & 0xFF),
147                                                  (int)(greens[i] & 0xFF),
148                                                  (int)(blues[i] & 0xFF));
149                                 break;
150                             }
151                         }
152                     } else {
153                         // TRANSLUCENT
154
/*
155                          * this.m_isTransparent = false;
156                          * for (int i = 0; i < this.m_width * this.m_height; i++) {
157                          * if (cm.getAlpha(tmpMap[i]) == 0) {
158                          * this.m_isTransparent = true;
159                          * this.m_transparentColor = new PDFColor(cm.getRed(tmpMap[i]), cm.getGreen(tmpMap[i]), cm.getBlue(tmpMap[i]));
160                          * break;
161                          * }
162                          * }
163                          */

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