KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: TiffImage.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.io.InputStream JavaDoc;
56
57 // Sun codec
58
import com.sun.media.jai.codec.FileCacheSeekableStream;
59 // FOP
60
import org.apache.fop.datatypes.ColorSpace;
61 import org.apache.fop.pdf.CCFFilter;
62 import org.apache.fop.pdf.DCTFilter;
63 import org.apache.fop.image.analyser.ImageReader;
64
65 /**
66  * FopImage object using JAI for TIFF images.
67  * Allows to store certain compresssed TIFF images directly
68  * in the output stream. Does revert to the standard JAI
69  * image for anything it doesn't understand.
70  * @author Manuel MALL
71  * @see AbstractFopImage
72  * @see FopImage
73  */

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

91             com.sun.media.jai.codec.FileCacheSeekableStream seekableInput =
92                 new FileCacheSeekableStream(inputStream);
93
94             com.sun.media.jai.codec.TIFFDirectory ifd = new com.sun.media.jai.codec.TIFFDirectory(seekableInput, 0);
95             com.sun.media.jai.codec.TIFFField fld = null;
96
97             this.m_height = (int)ifd.getFieldAsLong(0x101);
98             this.m_width = (int)ifd.getFieldAsLong(0x100);
99
100             fld = ifd.getField(0x115); // The samples per pixel field
101
if (fld != null && fld.getAsInt(0) != 1) {
102                 throw new FopImageException("Error while loading image "
103                                             + this.m_href.toString() + " : "
104                                             + this.getClass() + " - "
105                                             + "unsupported samples per pixel value " + fld.getAsInt(0));
106             }
107
108             this.m_bitsPerPixel = 1;
109             fld = ifd.getField(0x102); // The bits per sample field
110
if (fld != null) {
111                 this.m_bitsPerPixel = fld.getAsInt(0);
112             }
113
114             this.m_colorSpace = new ColorSpace(ColorSpace.DEVICE_GRAY);
115             fld = ifd.getField(0x106); // The photometric interpretation field
116
if (fld != null) {
117                 if (fld.getAsInt(0) == 0) {
118                     // All is fine
119
}
120                 else if (fld.getAsInt(0) == 2) {
121                     this.m_colorSpace = new ColorSpace(ColorSpace.DEVICE_RGB);
122                 }
123                 else {
124                     throw new FopImageException("Error while loading image "
125                                                 + this.m_href.toString() + " : "
126                                                 + this.getClass() + " - "
127                                                 + "unsupported photometric interpretation value " + fld.getAsInt(0));
128                 }
129             }
130
131             this.m_isTransparent = false;
132
133             int comp = 1;
134             fld = ifd.getField(0x103); // The compression field
135
if (fld != null) {
136                 comp = fld.getAsInt(0);
137             }
138             if (comp == 1) {
139             }
140             else if (comp == 3) {
141                 this.m_compressionType = new CCFFilter();
142                 this.m_compressionType.setApplied(true);
143             }
144             else if (comp == 4) {
145                 CCFFilter ccf = new CCFFilter();
146                 this.m_compressionType = ccf;
147                 this.m_compressionType.setApplied(true);
148                 ccf.setDecodeParms("<< /K -1 /Columns " + this.m_width + " >>");
149             }
150             else if (comp == 6) {
151                 this.m_compressionType = new DCTFilter();
152                 this.m_compressionType.setApplied(true);
153             }
154             else {
155                 throw new FopImageException("Error while loading image "
156                                             + this.m_href.toString() + " : "
157                                             + this.getClass() + " - "
158                                             + "unsupported compression value " + comp);
159             }
160
161             fld = ifd.getField(0x116); // The rows per strip field
162
if (fld != null) {
163                 if (this.m_height != fld.getAsLong(0)) {
164                     throw new FopImageException("Error while loading image "
165                                                 + this.m_href.toString() + " : "
166                                                 + this.getClass() + " - "
167                                                 + "only single strip images supported ");
168                 }
169             }
170             long offset = ifd.getFieldAsLong(0x111);
171             long length = ifd.getFieldAsLong(0x117);
172
173             byte[] readBuf = new byte[(int)length];
174             int bytes_read;
175
176             inputStream.close();
177             inputStream = this.m_href.openStream();
178             inputStream.skip(offset);
179             bytes_read = inputStream.read(readBuf);
180             if (bytes_read != length) {
181                 throw new FopImageException("Error while loading image "
182                                             + this.m_href.toString() + " : "
183                                             + this.getClass() + " - "
184                                             + "length mismatch on read");
185             }
186
187             this.m_bitmaps = readBuf;
188         } catch (FopImageException fie) {
189             org.apache.fop.messaging.MessageHandler.logln("Reverting to TIFF image handling through JAI: "
190                                                           + fie.getMessage());
191             this.m_compressionType = null;
192             super.loadImage();
193         } catch (Exception JavaDoc ex) {
194             throw new FopImageException("Error while loading image "
195                                         + this.m_href.toString() + " : "
196                                         + ex.getClass() + " - "
197                                         + ex.getMessage());
198         }
199     }
200
201 }
202
203
Popular Tags