KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: GifImage.java,v 1.1.2.2 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 // FOP
60
import org.apache.fop.datatypes.ColorSpace;
61 import org.apache.fop.pdf.PDFColor;
62 import org.apache.fop.image.analyser.ImageReader;
63
64 /**
65  * FopImage object for GIF images, using Java native classes.
66  * @author Eric SCHAEFFER
67  * @author Modified by Eric Dalquist - 9/14/2001 - ebdalqui@mtu.edu
68  * @see AbstractFopImage
69  * @see FopImage
70  */

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

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