KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: AbstractFopImage.java,v 1.5.2.6 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 // FOP
54
import org.apache.fop.datatypes.ColorSpace;
55 import org.apache.fop.pdf.PDFColor;
56 import org.apache.fop.pdf.PDFFilter;
57 import org.apache.fop.image.analyser.ImageReaderFactory;
58 import org.apache.fop.image.analyser.ImageReader;
59
60 // Java
61
import java.net.URL JavaDoc;
62
63 /**
64  * Base class to implement the FopImage interface.
65  * @author Eric Schaeffer
66  * @author <a HREF="mailto:ebdalqui@mtu.edu">Eric Dalquist</a>
67  * @see FopImage
68  */

69 public abstract class AbstractFopImage implements FopImage {
70
71     /**
72     * Photoshop generated cmykl jpeg's are inverted.
73     */

74     protected boolean m_invertImage = false;
75
76     /**
77      * Image width (in pixel).
78      */

79     protected int m_width = 0;
80
81     /**
82      * Image height (in pixel).
83      */

84     protected int m_height = 0;
85
86     /**
87      * Image URL.
88      */

89     protected URL JavaDoc m_href = null;
90
91     /**
92      * ImageReader object (to obtain image header informations).
93      */

94     protected ImageReader m_imageReader = null;
95
96     /**
97      * Image color space (org.apache.fop.datatypes.ColorSpace).
98      */

99     protected ColorSpace m_colorSpace = null;
100
101     /**
102      * Bits per pixel.
103      */

104     protected int m_bitsPerPixel = 0;
105
106     /**
107      * Image data (uncompressed).
108      */

109     protected byte[] m_bitmaps = null;
110
111     /**
112      * Image data size.
113      */

114     protected int m_bitmapsSize = 0;
115
116     /**
117      * Image transparency.
118      */

119     protected boolean m_isTransparent = false;
120
121     /**
122      * Transparent color (org.apache.fop.pdf.PDFColor).
123      */

124     protected PDFColor m_transparentColor = null;
125
126     /**
127      * Image compression type.
128      * Added by Eric Dalquist
129      */

130     protected PDFFilter m_compressionType = null;
131
132     /**
133      * Constructor.
134      * Construct a new FopImage object and initialize its default properties:
135      * <UL>
136      * <LI>image width
137      * <LI>image height
138      * </UL>
139      * The image data isn't kept in memory.
140      * @param href image URL
141      * @exception FopImageException an error occured during initialization
142      */

143     public AbstractFopImage(URL JavaDoc href) throws FopImageException {
144         this.m_href = href;
145         try {
146             this.m_imageReader =
147                 ImageReaderFactory.Make(this.m_href.toExternalForm(),
148                                         this.m_href.openStream());
149         } catch (Exception JavaDoc e) {
150             throw new FopImageException(e.getMessage());
151         }
152         this.m_width = this.m_imageReader.getWidth();
153         this.m_height = this.m_imageReader.getHeight();
154     }
155
156     /**
157      * Constructor.
158      * Construct a new FopImage object and initialize its default properties:
159      * <UL>
160      * <LI>image width
161      * <LI>image height
162      * </UL>
163      * The image data isn't kept in memory.
164      * @param href image URL
165      * imgReader ImageReader object
166      * @exception FopImageException an error occured during initialization
167      */

168     public AbstractFopImage(URL JavaDoc href,
169                             ImageReader imgReader) throws FopImageException {
170         this.m_href = href;
171         this.m_imageReader = imgReader;
172         this.m_width = this.m_imageReader.getWidth();
173         this.m_height = this.m_imageReader.getHeight();
174     }
175
176     /**
177      * Load image data and initialize its properties.
178      * Subclasses need to implement this method.
179      * @exception FopImageException an error occured during loading
180      */

181     protected abstract void loadImage() throws FopImageException;
182
183
184     /**
185     * If true, image data are inverted
186     */

187     public boolean invertImage() {
188         return m_invertImage;
189     }
190
191     /**
192      * Return the image URL.
193      * @return the image URL (as String)
194      */

195     public String JavaDoc getURL() {
196         return this.m_href.toString();
197     }
198
199     /**
200      * Return the image width.
201      * @return the image width
202      * @exception FopImageException an error occured during property retriaval
203      */

204     public int getWidth() throws FopImageException {
205         synchronized(this) {
206             if (this.m_width == 0)
207                 this.loadImage();
208         }
209
210         return this.m_width;
211     }
212
213     /**
214      * Return the image height.
215      * @return the image height
216      * @exception FopImageException an error occured during property retriaval
217      */

218     public int getHeight() throws FopImageException {
219         synchronized(this) {
220             if (this.m_height == 0)
221                 this.loadImage();
222         }
223
224         return this.m_height;
225     }
226
227     /**
228      * Return the image color space.
229      * @return the image color space (org.apache.fop.datatypes.ColorSpace)
230      * @exception FopImageException an error occured during property retriaval
231      */

232     public ColorSpace getColorSpace() throws FopImageException {
233         synchronized(this) {
234             if (this.m_colorSpace == null)
235                 this.loadImage();
236         }
237
238         return this.m_colorSpace;
239     }
240
241     /**
242      * Return the number of bits per pixel.
243      * @return number of bits per pixel
244      * @exception FopImageException an error occured during property retriaval
245      */

246     public int getBitsPerPixel() throws FopImageException {
247         synchronized(this) {
248             if (this.m_bitsPerPixel == 0)
249                 this.loadImage();
250         }
251
252         return this.m_bitsPerPixel;
253     }
254
255     /**
256      * Return the image transparency.
257      * @return true if the image is transparent
258      * @exception FopImageException an error occured during property retriaval
259      */

260     public boolean isTransparent() throws FopImageException {
261         return this.m_isTransparent;
262     }
263
264     /**
265      * Return the transparent color.
266      * @return the transparent color (org.apache.fop.pdf.PDFColor)
267      * @exception FopImageException an error occured during property retriaval
268      */

269     public PDFColor getTransparentColor() throws FopImageException {
270         return this.m_transparentColor;
271     }
272
273     /**
274      * Return the image data (uncompressed).
275      * @return the image data
276      * @exception FopImageException an error occured during loading
277      */

278     public byte[] getBitmaps() throws FopImageException {
279         synchronized(this) {
280             if (this.m_bitmaps == null)
281                 this.loadImage();
282         }
283
284         return this.m_bitmaps;
285     }
286
287     /**
288      * Return the image data size (uncompressed).
289      * @return the image data size
290      * @exception FopImageException an error occured during loading
291      */

292     public int getBitmapsSize() throws FopImageException {
293         synchronized(this) {
294             if (this.m_bitmapsSize == 0)
295                 this.loadImage();
296         }
297
298         return this.m_bitmapsSize;
299     }
300
301     /**
302      * Return the original image data (compressed).
303      * @return the original image data
304      * @exception FopImageException an error occured during loading
305      */

306     public byte[] getRessourceBytes() throws FopImageException {
307         return null;
308     }
309
310     /**
311      * Return the original image data size (compressed).
312      * @return the original image data size
313      * @exception FopImageException an error occured during loading
314      */

315     public int getRessourceBytesSize() throws FopImageException {
316         return 0;
317     }
318
319     /**
320      * Return the original image compression type.
321      * @return the original image compression type (org.apache.fop.pdf.PDFFilter)
322      * @exception FopImageException an error occured during loading
323      */

324     public PDFFilter getPDFFilter() throws FopImageException {
325
326         /*
327          * Added by Eric Dalquist
328          * Using the bitsPerPixel var as our flag since many imges will
329          * have a null m_compressionType even after being loaded
330          */

331         synchronized(this) {
332             if (this.m_bitsPerPixel == 0)
333                 this.loadImage();
334         }
335
336         return m_compressionType;
337     }
338
339     /**
340      * Free all ressource.
341      */

342     public void close() {
343         //org.apache.fop.messaging.MessageHandler.debug(getClass().getName()+".close(): "+this.m_href);
344
/*
345          * For the moment, only release the bitmaps (image areas
346          * can share the same FopImage object)
347          * Thus, even if it had been called, other properties
348          * are still available.
349          */

350         synchronized(this) {
351             // this.m_width = 0;
352
// this.m_height = 0;
353
// this.m_href = null;
354
// this.m_colorSpace = null;
355
// this.m_bitsPerPixel = 0;
356
this.m_bitmaps = null;
357             this.m_bitmapsSize = 0;
358             // this.m_isTransparent = false;
359
// this.m_transparentColor = null;
360
}
361     }
362
363 }
364
365
Popular Tags