annotate servlet/src/digilib/image/JAIImageLoaderDocuImage.java @ 339:6d2032b6121d gen2_1

new directory and cache work
author robcast
date Wed, 17 Nov 2004 18:17:34 +0100
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
339
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
1 /* JAIImageLoaderDocuImage -- Image class implementation using JAI's ImageLoader Plugin
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
2
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
3 Digital Image Library servlet components
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
4
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
5 Copyright (C) 2002, 2003 Robert Casties (robcast@mail.berlios.de)
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
6
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
7 This program is free software; you can redistribute it and/or modify it
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
8 under the terms of the GNU General Public License as published by the
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
9 Free Software Foundation; either version 2 of the License, or (at your
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
10 option) any later version.
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
11
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
12 Please read license.txt for the full details. A copy of the GPL
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
13 may be found at http://www.gnu.org/copyleft/lgpl.html
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
14
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
15 You should have received a copy of the GNU General Public License
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
16 along with this program; if not, write to the Free Software
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
18
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
19 */
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
20
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
21 package digilib.image;
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
22
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
23 import java.awt.Rectangle;
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
24 import java.awt.image.renderable.ParameterBlock;
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
25 import java.io.File;
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
26 import java.io.IOException;
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
27 import java.io.OutputStream;
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
28 import java.io.RandomAccessFile;
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
29 import java.util.Iterator;
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
30
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
31 import javax.imageio.ImageIO;
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
32 import javax.imageio.ImageReadParam;
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
33 import javax.imageio.ImageReader;
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
34 import javax.imageio.stream.FileImageInputStream;
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
35 import javax.imageio.stream.ImageInputStream;
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
36 import javax.media.jai.JAI;
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
37
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
38 import digilib.io.FileOpException;
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
39 import digilib.io.ImageFile;
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
40
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
41 /** DocuImage implementation using the Java Advanced Imaging API and the ImageLoader
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
42 * API of Java 1.4.
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
43 */
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
44 public class JAIImageLoaderDocuImage extends JAIDocuImage {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
45
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
46 /** ImageIO image reader */
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
47 protected ImageReader reader;
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
48 /** current image file */
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
49 protected File imgFile;
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
50
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
51 /* loadSubimage is supported. */
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
52 public boolean isSubimageSupported() {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
53 return true;
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
54 }
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
55
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
56 public int getHeight() {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
57 int h = 0;
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
58 try {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
59 if (img == null) {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
60 h = reader.getHeight(0);
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
61 } else {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
62 h = img.getHeight();
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
63 }
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
64 } catch (IOException e) {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
65 logger.debug("error in getHeight", e);
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
66 }
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
67 return h;
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
68 }
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
69
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
70 public int getWidth() {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
71 int w = 0;
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
72 try {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
73 if (img == null) {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
74 w = reader.getWidth(0);
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
75 } else {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
76 w = img.getWidth();
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
77 }
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
78 } catch (IOException e) {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
79 logger.debug("error in getHeight", e);
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
80 }
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
81 return w;
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
82 }
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
83
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
84 /* Load an image file into the Object. */
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
85 public void loadImage(ImageFile f) throws FileOpException {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
86 logger.debug("loadImage: "+f);
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
87 //System.gc();
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
88 img = JAI.create("ImageRead", f.getAbsolutePath());
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
89 if (img == null) {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
90 throw new FileOpException("Unable to load File!");
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
91 }
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
92 }
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
93
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
94 /* Get an ImageReader for the image file. */
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
95 public ImageReader getReader(ImageFile f) throws IOException {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
96 logger.debug("preloadImage: "+f);
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
97 //System.gc();
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
98 RandomAccessFile rf = new RandomAccessFile(f, "r");
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
99 ImageInputStream istream = new FileImageInputStream(rf);
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
100 //Iterator readers = ImageIO.getImageReaders(istream);
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
101 Iterator readers = ImageIO.getImageReadersByMIMEType(f.getMimetype());
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
102 if (! readers.hasNext()) {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
103 throw new FileOpException("Unable to load File!");
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
104 }
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
105 reader = (ImageReader) readers.next();
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
106 logger.debug("JAIImageIO: this reader: " + reader.getClass());
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
107 while (readers.hasNext()) {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
108 logger.debug(" next reader: " + readers.next().getClass());
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
109 }
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
110 reader.setInput(istream);
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
111 return reader;
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
112 }
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
113
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
114 /* Load an image file into the Object. */
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
115 public void loadSubimage(ImageFile f, Rectangle region, int prescale)
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
116 throws FileOpException {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
117 logger.debug("loadSubimage: "+f);
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
118 //System.gc();
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
119 try {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
120 if ((reader == null) || (imgFile != f)) {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
121 getReader(f);
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
122 }
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
123 ImageReadParam readParam = reader.getDefaultReadParam();
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
124 readParam.setSourceRegion(region);
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
125 readParam.setSourceSubsampling(prescale, prescale, 0, 0);
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
126 img = reader.read(0, readParam);
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
127 /* JAI imageread seems to ignore the readParam :-(
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
128 ImageInputStream istream = (ImageInputStream) reader.getInput();
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
129 ParameterBlockJAI pb = new ParameterBlockJAI("imageread");
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
130 pb.setParameter("Input", istream);
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
131 pb.setParameter("ReadParam", readParam);
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
132 pb.setParameter("Reader", reader);
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
133 img = JAI.create("imageread", pb);
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
134 */
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
135 } catch (IOException e) {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
136 throw new FileOpException("Unable to load File!");
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
137 }
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
138 if (img == null) {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
139 throw new FileOpException("Unable to load File!");
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
140 }
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
141 imgFile = f;
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
142 }
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
143
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
144
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
145 /* Write the current image to an OutputStream. */
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
146 public void writeImage(String mt, OutputStream ostream)
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
147 throws FileOpException {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
148 logger.debug("writeImage");
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
149 try {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
150 // setup output
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
151 ParameterBlock pb3 = new ParameterBlock();
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
152 pb3.addSource(img);
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
153 pb3.add(ostream);
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
154 if (mt == "image/jpeg") {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
155 pb3.add("JPEG");
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
156 } else if (mt == "image/png") {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
157 pb3.add("PNG");
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
158 } else {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
159 // unknown mime type
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
160 throw new FileOpException("Unknown mime type: " + mt);
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
161 }
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
162 // render output
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
163 JAI.create("ImageWrite", pb3);
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
164 } catch (IOException e) {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
165 throw new FileOpException("Error writing image.");
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
166 }
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
167 }
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
168
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
169 /* (non-Javadoc)
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
170 * @see java.lang.Object#finalize()
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
171 */
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
172 protected void finalize() throws Throwable {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
173 dispose();
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
174 super.finalize();
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
175 }
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
176
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
177 public void dispose() {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
178 // we must dispose the ImageReader because it keeps the filehandle open!
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
179 reader.dispose();
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
180 reader = null;
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
181 img = null;
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
182 }
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
183
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
184 }