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