Mercurial > hg > digilib-old
annotate servlet/src/digilib/image/JAIImageLoaderDocuImage.java @ 89:6d35c945a5d6
New version 1.8b4.
ImageLoader uses RandomAccessFile to setup ImageInputStream.
author | robcast |
---|---|
date | Mon, 17 Mar 2003 15:28:13 +0100 |
parents | 4e6757e8ccd4 |
children | f52a7664a807 |
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; |
89 | 37 import javax.media.jai.ParameterBlockJAI; |
64
5ea1999befd4
New JAI ImageLoader plugin. Currently uses first beta version of the plugin.
robcast
parents:
diff
changeset
|
38 |
89 | 39 import digilib.io.DocuFile; |
73 | 40 import digilib.io.FileOpException; |
89 | 41 import digilib.io.FileOps; |
64
5ea1999befd4
New JAI ImageLoader plugin. Currently uses first beta version of the plugin.
robcast
parents:
diff
changeset
|
42 |
73 | 43 /** DocuImage implementation using the Java Advanced Imaging API and the ImageLoader |
44 * API of Java 1.4. | |
45 */ | |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
46 public class JAIImageLoaderDocuImage extends JAIDocuImage { |
64
5ea1999befd4
New JAI ImageLoader plugin. Currently uses first beta version of the plugin.
robcast
parents:
diff
changeset
|
47 |
89 | 48 /** ImageIO image reader */ |
49 protected ImageReader reader; | |
50 /** current image file */ | |
51 protected File imgFile; | |
85 | 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 { | |
89 | 101 if ((reader == null) || (imgFile != f)) { |
85 | 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); | |
89 | 108 ParameterBlockJAI pb = new ParameterBlockJAI("imageread"); |
109 pb.setParameter("Input", istream); | |
110 pb.setParameter("ReadParam", readParam); | |
111 pb.setParameter("Reader", reader); | |
112 img = JAI.create("imageread", pb); | |
85 | 113 } catch (IOException e) { |
114 util.dprintln(3, "ERROR(loadImage): unable to load file"); | |
115 throw new FileOpException("Unable to load File!"); | |
116 } | |
117 if (img == null) { | |
118 util.dprintln(3, "ERROR(loadImage): unable to load file"); | |
119 throw new FileOpException("Unable to load File!"); | |
120 } | |
89 | 121 imgFile = f; |
85 | 122 } |
123 | |
124 /* Get an ImageReader for the image file. */ | |
89 | 125 public void preloadImage(File f) throws IOException { |
85 | 126 System.gc(); |
89 | 127 RandomAccessFile rf = new RandomAccessFile(f, "r"); |
128 ImageInputStream istream = ImageIO.createImageInputStream(rf); | |
129 Iterator readers = ImageIO.getImageReaders(istream); | |
130 reader = (ImageReader) readers.next(); | |
131 reader.setInput(istream); | |
85 | 132 if (reader == null) { |
133 util.dprintln(3, "ERROR(loadImage): unable to load file"); | |
134 throw new FileOpException("Unable to load File!"); | |
135 } | |
136 } | |
137 | |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
138 /* Write the current image to an OutputStream. */ |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
139 public void writeImage(String mt, OutputStream ostream) |
73 | 140 throws FileOpException { |
141 try { | |
142 // setup output | |
143 ParameterBlock pb3 = new ParameterBlock(); | |
144 pb3.addSource(img); | |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
145 pb3.add(ostream); |
73 | 146 if (mt == "image/jpeg") { |
147 pb3.add("JPEG"); | |
148 } else if (mt == "image/png") { | |
149 pb3.add("PNG"); | |
150 } else { | |
151 // unknown mime type | |
152 util.dprintln(2, "ERROR(writeImage): Unknown mime type " + mt); | |
153 throw new FileOpException("Unknown mime type: " + mt); | |
154 } | |
155 // render output | |
156 JAI.create("ImageWrite", pb3); | |
64
5ea1999befd4
New JAI ImageLoader plugin. Currently uses first beta version of the plugin.
robcast
parents:
diff
changeset
|
157 |
73 | 158 } catch (IOException e) { |
159 throw new FileOpException("Error writing image."); | |
160 } | |
161 } | |
64
5ea1999befd4
New JAI ImageLoader plugin. Currently uses first beta version of the plugin.
robcast
parents:
diff
changeset
|
162 |
89 | 163 /* (non-Javadoc) |
164 * @see digilib.image.DocuImage#checkFile(digilib.io.DocuFile) | |
165 */ | |
166 public boolean checkFile(DocuFile f) throws IOException { | |
167 // see if f is already loaded | |
168 if ((reader == null) || (imgFile != f.getFile())) { | |
169 preloadImage(f.getFile()); | |
170 } | |
171 Dimension d = new Dimension(); | |
172 d.setSize(reader.getWidth(0), reader.getHeight(0)); | |
173 f.setSize(d); | |
174 // String t = reader.getFormatName(); | |
175 String t = FileOps.mimeForFile(f.getFile()); | |
176 f.setMimetype(t); | |
177 f.setChecked(true); | |
178 return true; | |
179 } | |
180 | |
64
5ea1999befd4
New JAI ImageLoader plugin. Currently uses first beta version of the plugin.
robcast
parents:
diff
changeset
|
181 } |