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