Mercurial > hg > digilib-old
annotate servlet/src/digilib/image/JAIImageLoaderDocuImage.java @ 87:5d44cd2481a5
New version 1.8b4.
Utils without static debuglevel.
author | robcast |
---|---|
date | Mon, 17 Mar 2003 15:23:17 +0100 |
parents | 4e6757e8ccd4 |
children | 6d35c945a5d6 |
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; |
85 | 28 import java.util.Iterator; |
64
5ea1999befd4
New JAI ImageLoader plugin. Currently uses first beta version of the plugin.
robcast
parents:
diff
changeset
|
29 |
85 | 30 import javax.imageio.ImageIO; |
31 import javax.imageio.ImageReadParam; | |
32 import javax.imageio.ImageReader; | |
33 import javax.imageio.stream.ImageInputStream; | |
73 | 34 import javax.media.jai.JAI; |
64
5ea1999befd4
New JAI ImageLoader plugin. Currently uses first beta version of the plugin.
robcast
parents:
diff
changeset
|
35 |
85 | 36 import com.sun.media.jai.operator.ImageReadDescriptor; |
37 | |
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 |
85 | 45 // ImageIO image reader |
46 ImageReader reader; | |
47 | |
48 /* preload is supported. */ | |
49 public boolean isPreloadSupported() { | |
50 return true; | |
51 } | |
52 | |
53 /* loadSubimage is supported. */ | |
54 public boolean isSubimageSupported() { | |
55 return true; | |
56 } | |
57 | |
58 public int getHeight() { | |
59 int h = 0; | |
60 try { | |
61 if (img == null) { | |
62 h = reader.getHeight(0); | |
63 } else { | |
64 h = img.getHeight(); | |
65 } | |
66 } catch (IOException e) { | |
67 e.printStackTrace(); | |
68 } | |
69 return h; | |
70 } | |
71 | |
72 public int getWidth() { | |
73 int w = 0; | |
74 try { | |
75 if (img == null) { | |
76 w = reader.getWidth(0); | |
77 } else { | |
78 w = img.getWidth(); | |
79 } | |
80 } catch (IOException e) { | |
81 e.printStackTrace(); | |
82 } | |
83 return w; | |
84 } | |
64
5ea1999befd4
New JAI ImageLoader plugin. Currently uses first beta version of the plugin.
robcast
parents:
diff
changeset
|
85 |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
86 /* Load an image file into the Object. */ |
73 | 87 public void loadImage(File f) throws FileOpException { |
88 System.gc(); | |
89 img = JAI.create("ImageRead", f.getAbsolutePath()); | |
90 if (img == null) { | |
91 util.dprintln(3, "ERROR(loadImage): unable to load file"); | |
92 throw new FileOpException("Unable to load File!"); | |
93 } | |
94 } | |
64
5ea1999befd4
New JAI ImageLoader plugin. Currently uses first beta version of the plugin.
robcast
parents:
diff
changeset
|
95 |
85 | 96 /* Load an image file into the Object. */ |
97 public void loadSubimage(File f, Rectangle region, int prescale) | |
98 throws FileOpException { | |
99 System.gc(); | |
100 try { | |
101 if (reader == null) { | |
102 preloadImage(f); | |
103 } | |
104 ImageInputStream istream = (ImageInputStream) reader.getInput(); | |
105 ImageReadParam readParam = reader.getDefaultReadParam(); | |
106 readParam.setSourceRegion(region); | |
107 readParam.setSourceSubsampling(prescale, prescale, 0, 0); | |
108 /* Parameter for ImageRead operation: | |
109 Input, ImageChoice, ReadMetadata, ReadThumbnails, VerifyInput, | |
110 Listeners, Locale, ReadParam, Reader, RenderingHints | |
111 */ | |
112 img = | |
113 ImageReadDescriptor.create( | |
114 istream, | |
115 new Integer(0), | |
116 Boolean.TRUE, | |
117 Boolean.FALSE, | |
118 Boolean.FALSE, | |
119 null, | |
120 null, | |
121 readParam, | |
122 reader, | |
123 null); | |
124 } catch (IOException e) { | |
125 util.dprintln(3, "ERROR(loadImage): unable to load file"); | |
126 throw new FileOpException("Unable to load File!"); | |
127 } | |
128 if (img == null) { | |
129 util.dprintln(3, "ERROR(loadImage): unable to load file"); | |
130 throw new FileOpException("Unable to load File!"); | |
131 } | |
132 } | |
133 | |
134 /* Get an ImageReader for the image file. */ | |
135 public void preloadImage(File f) throws FileOpException { | |
136 System.gc(); | |
137 try { | |
138 ImageInputStream istream = ImageIO.createImageInputStream(f); | |
139 Iterator readers = ImageIO.getImageReaders(istream); | |
140 reader = (ImageReader) readers.next(); | |
141 reader.setInput(istream); | |
142 } catch (IOException e) { | |
143 util.dprintln(3, "ERROR(loadImage): unable to load file"); | |
144 throw new FileOpException("Unable to load File!" + e); | |
145 } | |
146 if (reader == null) { | |
147 util.dprintln(3, "ERROR(loadImage): unable to load file"); | |
148 throw new FileOpException("Unable to load File!"); | |
149 } | |
150 } | |
151 | |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
152 /* Write the current image to an OutputStream. */ |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
153 public void writeImage(String mt, OutputStream ostream) |
73 | 154 throws FileOpException { |
155 try { | |
156 // setup output | |
157 ParameterBlock pb3 = new ParameterBlock(); | |
158 pb3.addSource(img); | |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
159 pb3.add(ostream); |
73 | 160 if (mt == "image/jpeg") { |
161 pb3.add("JPEG"); | |
162 } else if (mt == "image/png") { | |
163 pb3.add("PNG"); | |
164 } else { | |
165 // unknown mime type | |
166 util.dprintln(2, "ERROR(writeImage): Unknown mime type " + mt); | |
167 throw new FileOpException("Unknown mime type: " + mt); | |
168 } | |
169 // render output | |
170 JAI.create("ImageWrite", pb3); | |
64
5ea1999befd4
New JAI ImageLoader plugin. Currently uses first beta version of the plugin.
robcast
parents:
diff
changeset
|
171 |
73 | 172 } catch (IOException e) { |
173 throw new FileOpException("Error writing image."); | |
174 } | |
175 } | |
64
5ea1999befd4
New JAI ImageLoader plugin. Currently uses first beta version of the plugin.
robcast
parents:
diff
changeset
|
176 |
5ea1999befd4
New JAI ImageLoader plugin. Currently uses first beta version of the plugin.
robcast
parents:
diff
changeset
|
177 } |