Mercurial > hg > digilib-old
annotate servlet/src/digilib/image/JAIImageLoaderDocuImage.java @ 142:f52a7664a807
Servlet version 1.12b3
- JAIImageLoaderDocuImage working again
- smoother scaling with JAI and JAIImageLoader
- Quality default now 1
author | robcast |
---|---|
date | Mon, 18 Aug 2003 11:38:15 +0200 |
parents | 6d35c945a5d6 |
children | 837a633a0407 |
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 |
89 | 23 import java.awt.Dimension; |
85 | 24 import java.awt.Rectangle; |
73 | 25 import java.awt.image.renderable.ParameterBlock; |
26 import java.io.File; | |
27 import java.io.IOException; | |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
28 import java.io.OutputStream; |
89 | 29 import java.io.RandomAccessFile; |
85 | 30 import java.util.Iterator; |
64
5ea1999befd4
New JAI ImageLoader plugin. Currently uses first beta version of the plugin.
robcast
parents:
diff
changeset
|
31 |
85 | 32 import javax.imageio.ImageIO; |
33 import javax.imageio.ImageReadParam; | |
34 import javax.imageio.ImageReader; | |
35 import javax.imageio.stream.ImageInputStream; | |
73 | 36 import javax.media.jai.JAI; |
64
5ea1999befd4
New JAI ImageLoader plugin. Currently uses first beta version of the plugin.
robcast
parents:
diff
changeset
|
37 |
89 | 38 import digilib.io.DocuFile; |
73 | 39 import digilib.io.FileOpException; |
89 | 40 import digilib.io.FileOps; |
64
5ea1999befd4
New JAI ImageLoader plugin. Currently uses first beta version of the plugin.
robcast
parents:
diff
changeset
|
41 |
73 | 42 /** DocuImage implementation using the Java Advanced Imaging API and the ImageLoader |
43 * API of Java 1.4. | |
44 */ | |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
45 public class JAIImageLoaderDocuImage extends JAIDocuImage { |
64
5ea1999befd4
New JAI ImageLoader plugin. Currently uses first beta version of the plugin.
robcast
parents:
diff
changeset
|
46 |
89 | 47 /** ImageIO image reader */ |
48 protected ImageReader reader; | |
49 /** current image file */ | |
50 protected File imgFile; | |
85 | 51 |
52 /* loadSubimage is supported. */ | |
53 public boolean isSubimageSupported() { | |
54 return true; | |
55 } | |
56 | |
57 public int getHeight() { | |
58 int h = 0; | |
59 try { | |
60 if (img == null) { | |
61 h = reader.getHeight(0); | |
62 } else { | |
63 h = img.getHeight(); | |
64 } | |
65 } catch (IOException e) { | |
66 e.printStackTrace(); | |
67 } | |
68 return h; | |
69 } | |
70 | |
71 public int getWidth() { | |
72 int w = 0; | |
73 try { | |
74 if (img == null) { | |
75 w = reader.getWidth(0); | |
76 } else { | |
77 w = img.getWidth(); | |
78 } | |
79 } catch (IOException e) { | |
80 e.printStackTrace(); | |
81 } | |
82 return w; | |
83 } | |
64
5ea1999befd4
New JAI ImageLoader plugin. Currently uses first beta version of the plugin.
robcast
parents:
diff
changeset
|
84 |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
85 /* Load an image file into the Object. */ |
73 | 86 public void loadImage(File f) throws FileOpException { |
87 System.gc(); | |
88 img = JAI.create("ImageRead", f.getAbsolutePath()); | |
89 if (img == null) { | |
90 util.dprintln(3, "ERROR(loadImage): unable to load file"); | |
91 throw new FileOpException("Unable to load File!"); | |
92 } | |
93 } | |
64
5ea1999befd4
New JAI ImageLoader plugin. Currently uses first beta version of the plugin.
robcast
parents:
diff
changeset
|
94 |
142 | 95 /* Get an ImageReader for the image file. */ |
96 public void preloadImage(File f) throws IOException { | |
97 System.gc(); | |
98 RandomAccessFile rf = new RandomAccessFile(f, "r"); | |
99 ImageInputStream istream = ImageIO.createImageInputStream(rf); | |
100 Iterator readers = ImageIO.getImageReaders(istream); | |
101 reader = (ImageReader) readers.next(); | |
102 reader.setInput(istream); | |
103 if (reader == null) { | |
104 util.dprintln(3, "ERROR(loadImage): unable to load file"); | |
105 throw new FileOpException("Unable to load File!"); | |
106 } | |
107 } | |
108 | |
85 | 109 /* Load an image file into the Object. */ |
110 public void loadSubimage(File f, Rectangle region, int prescale) | |
111 throws FileOpException { | |
112 System.gc(); | |
113 try { | |
89 | 114 if ((reader == null) || (imgFile != f)) { |
85 | 115 preloadImage(f); |
116 } | |
117 ImageInputStream istream = (ImageInputStream) reader.getInput(); | |
118 ImageReadParam readParam = reader.getDefaultReadParam(); | |
119 readParam.setSourceRegion(region); | |
120 readParam.setSourceSubsampling(prescale, prescale, 0, 0); | |
142 | 121 img = reader.read(0, readParam); |
122 /* JAI imageread seems to ignore the readParam :-( | |
89 | 123 ParameterBlockJAI pb = new ParameterBlockJAI("imageread"); |
124 pb.setParameter("Input", istream); | |
125 pb.setParameter("ReadParam", readParam); | |
126 pb.setParameter("Reader", reader); | |
127 img = JAI.create("imageread", pb); | |
142 | 128 */ |
85 | 129 } catch (IOException e) { |
130 util.dprintln(3, "ERROR(loadImage): unable to load file"); | |
131 throw new FileOpException("Unable to load File!"); | |
132 } | |
133 if (img == null) { | |
134 util.dprintln(3, "ERROR(loadImage): unable to load file"); | |
135 throw new FileOpException("Unable to load File!"); | |
136 } | |
89 | 137 imgFile = f; |
85 | 138 } |
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) |
165 * @see digilib.image.DocuImage#checkFile(digilib.io.DocuFile) | |
166 */ | |
167 public boolean checkFile(DocuFile f) throws IOException { | |
168 // see if f is already loaded | |
169 if ((reader == null) || (imgFile != f.getFile())) { | |
170 preloadImage(f.getFile()); | |
171 } | |
172 Dimension d = new Dimension(); | |
173 d.setSize(reader.getWidth(0), reader.getHeight(0)); | |
174 f.setSize(d); | |
175 // String t = reader.getFormatName(); | |
176 String t = FileOps.mimeForFile(f.getFile()); | |
177 f.setMimetype(t); | |
178 f.setChecked(true); | |
179 return true; | |
180 } | |
181 | |
64
5ea1999befd4
New JAI ImageLoader plugin. Currently uses first beta version of the plugin.
robcast
parents:
diff
changeset
|
182 } |