Mercurial > hg > digilib-old
comparison common-jai/src/main/java/digilib/image/JAIDocuImage.java @ 960:b2d97b842612
moved DocuImage implementations with non-standard toolkits (JAI, ImgeJ) into separate Maven modules.
default build uses jai-imageio (now only one additional JAR in webapp!).
author | robcast |
---|---|
date | Wed, 25 Jan 2012 15:13:06 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
959:f197e7760154 | 960:b2d97b842612 |
---|---|
1 /* JAIDocuImage -- Image class implementation using JAI (Java Advanced Imaging) | |
2 | |
3 Digital Image Library servlet components | |
4 | |
5 Copyright (C) 2001, 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.RenderingHints; | |
25 import java.awt.image.RenderedImage; | |
26 import java.awt.image.renderable.ParameterBlock; | |
27 import java.io.IOException; | |
28 import java.io.OutputStream; | |
29 import java.util.ArrayList; | |
30 import java.util.Enumeration; | |
31 import java.util.Iterator; | |
32 import java.util.List; | |
33 | |
34 import javax.media.jai.BorderExtender; | |
35 import javax.media.jai.Interpolation; | |
36 import javax.media.jai.JAI; | |
37 import javax.media.jai.KernelJAI; | |
38 import javax.media.jai.ParameterBlockJAI; | |
39 import javax.media.jai.RenderedOp; | |
40 import javax.media.jai.operator.TransposeDescriptor; | |
41 import javax.media.jai.operator.TransposeType; | |
42 | |
43 import com.sun.media.jai.codec.ImageCodec; | |
44 | |
45 import digilib.io.FileOpException; | |
46 import digilib.io.FileOps; | |
47 import digilib.io.ImageInput; | |
48 import digilib.util.ImageSize; | |
49 | |
50 /** A DocuImage implementation using Java Advanced Imaging Library. */ | |
51 /** | |
52 * @author casties | |
53 * | |
54 */ | |
55 public class JAIDocuImage extends ImageInfoDocuImage { | |
56 | |
57 protected RenderedImage img; | |
58 | |
59 protected Interpolation interpol = null; | |
60 | |
61 /* | |
62 * static { // we could set our own tile cache size here TileCache tc = | |
63 * JAI.createTileCache(100*1024*1024); | |
64 * JAI.getDefaultInstance().setTileCache(tc); } | |
65 */ | |
66 | |
67 public boolean isSubimageSupported() { | |
68 return true; | |
69 } | |
70 | |
71 /* | |
72 * Real setQuality implementation. Creates the correct Interpolation. | |
73 */ | |
74 public void setQuality(int qual) { | |
75 quality = qual; | |
76 // setup interpolation quality | |
77 if (qual > 1) { | |
78 logger.debug("quality q2"); | |
79 interpol = Interpolation.getInstance(Interpolation.INTERP_BICUBIC); | |
80 } else if (qual == 1) { | |
81 logger.debug("quality q1"); | |
82 interpol = Interpolation.getInstance(Interpolation.INTERP_BILINEAR); | |
83 } else { | |
84 logger.debug("quality q0"); | |
85 interpol = Interpolation.getInstance(Interpolation.INTERP_NEAREST); | |
86 } | |
87 } | |
88 | |
89 /* returns a list of supported image formats */ | |
90 @SuppressWarnings("unchecked") // ImageCodec.getCodecs() returns a naked Enumeration | |
91 public Iterator<String> getSupportedFormats() { | |
92 Enumeration<ImageCodec> codecs = ImageCodec.getCodecs(); | |
93 List<String> formats = new ArrayList<String>(); | |
94 for (ImageCodec codec = codecs.nextElement(); codecs.hasMoreElements(); codec = codecs | |
95 .nextElement()) { | |
96 logger.debug("known format:"+codec.getFormatName()); | |
97 formats.add(codec.getFormatName()); | |
98 } | |
99 logger.debug("tilecachesize:" | |
100 + JAI.getDefaultInstance().getTileCache().getMemoryCapacity()); | |
101 return formats.iterator(); | |
102 } | |
103 | |
104 /* Check image size and type and store in ImageFile f */ | |
105 public ImageInput identify(ImageInput input) throws IOException { | |
106 this.input = input; | |
107 // try parent method first | |
108 ImageInput imf = super.identify(input); | |
109 if (imf != null) { | |
110 return imf; | |
111 } | |
112 /* | |
113 * try JAI | |
114 */ | |
115 logger.debug("identifying (JAI) " + input); | |
116 try { | |
117 RenderedOp img = null; | |
118 if (input.hasFile()) { | |
119 String t = FileOps.mimeForFile(input.getFile()); | |
120 input.setMimetype(t); | |
121 img = JAI.create("fileload", input.getFile().getAbsolutePath()); | |
122 } else if (input.hasInputStream()) { | |
123 img = JAI.create("stream", input.getInputStream()); | |
124 // FIXME: where do we get the mimetype? | |
125 } else { | |
126 throw new FileOpException("unable to get data for image!"); | |
127 } | |
128 ImageSize d = new ImageSize(img.getWidth(), img.getHeight()); | |
129 input.setSize(d); | |
130 logger.debug("image size: " + d); | |
131 return input; | |
132 } catch (Exception e) { | |
133 throw new FileOpException("ERROR: unable to identify image!"); | |
134 } | |
135 } | |
136 | |
137 /* Load an image file into the Object. */ | |
138 public void loadImage(ImageInput ii) throws FileOpException { | |
139 this.input = ii; | |
140 if (ii.hasFile()) { | |
141 img = JAI.create("fileload", ii.getFile().getAbsolutePath()); | |
142 } else if (ii.hasInputStream()) { | |
143 img = JAI.create("stream", ii.getInputStream()); | |
144 } else { | |
145 throw new FileOpException("unable to get data for image!"); | |
146 } | |
147 if (img == null) { | |
148 throw new FileOpException("Unable to load File!"); | |
149 } | |
150 } | |
151 | |
152 /* Load an image file into the Object. */ | |
153 public void loadSubimage(ImageInput ii, Rectangle region, int subsample) throws FileOpException { | |
154 logger.debug("loadSubimage"); | |
155 this.input = ii; | |
156 if (ii.hasFile()) { | |
157 img = JAI.create("fileload", ii.getFile().getAbsolutePath()); | |
158 } else if (ii.hasInputStream()) { | |
159 img = JAI.create("stream", ii.getInputStream()); | |
160 } else { | |
161 throw new FileOpException("unable to get data for image!"); | |
162 } | |
163 if ((region.width < img.getWidth()) | |
164 || (region.height < img.getHeight())) { | |
165 // setup Crop | |
166 ParameterBlock cp = new ParameterBlock(); | |
167 cp.addSource(img); | |
168 cp.add((float) region.x); | |
169 cp.add((float) region.y); | |
170 cp.add((float) region.width); | |
171 cp.add((float) region.height); | |
172 logger.debug("loadSubimage: crop"); | |
173 img = JAI.create("crop", cp); | |
174 } | |
175 if (subsample > 1) { | |
176 float sc = 1f / subsample; | |
177 ParameterBlockJAI sp = new ParameterBlockJAI("scale"); | |
178 sp.addSource(img); | |
179 sp.setParameter("xScale", sc); | |
180 sp.setParameter("yScale", sc); | |
181 sp.setParameter("interpolation", Interpolation | |
182 .getInstance(Interpolation.INTERP_NEAREST)); | |
183 // scale | |
184 logger.debug("loadSubimage: scale"); | |
185 img = JAI.create("scale", sp); | |
186 } | |
187 } | |
188 | |
189 /* Write the current image to an OutputStream. */ | |
190 public void writeImage(String mt, OutputStream ostream) throws ImageOpException, FileOpException { | |
191 try { | |
192 // setup output | |
193 ParameterBlock pb3 = new ParameterBlock(); | |
194 pb3.addSource(img); | |
195 pb3.add(ostream); | |
196 if (mt == "image/jpeg") { | |
197 pb3.add("JPEG"); | |
198 } else if (mt == "image/png") { | |
199 pb3.add("PNG"); | |
200 } else { | |
201 // unknown mime type | |
202 throw new ImageOpException("Unknown mime type: " + mt); | |
203 } | |
204 // render output | |
205 JAI.create("encode", pb3); | |
206 | |
207 } catch (RuntimeException e) { | |
208 // JAI likes to throw RuntimeExceptions | |
209 throw new FileOpException("Error writing image: "+e); | |
210 } | |
211 } | |
212 | |
213 /* returns the current image size | |
214 * @see digilib.image.DocuImageImpl#getSize() | |
215 */ | |
216 public ImageSize getSize() { | |
217 ImageSize is = null; | |
218 // TODO: do we want to cache imgSize? | |
219 int h = 0; | |
220 int w = 0; | |
221 if (img != null) { | |
222 // get size from image | |
223 h = img.getHeight(); | |
224 w = img.getWidth(); | |
225 is = new ImageSize(w, h); | |
226 } | |
227 return is; | |
228 } | |
229 | |
230 /* scales the current image */ | |
231 public void scale(double scale, double scaleY) throws ImageOpException { | |
232 logger.debug("scale"); | |
233 if ((scale < 1) && (img.getColorModel().getPixelSize() == 1) | |
234 && (quality > 0)) { | |
235 /* | |
236 * "SubsampleBinaryToGray" for downscaling BW | |
237 */ | |
238 scaleBinary((float) scale); | |
239 } else if ((scale <= 0.5) && (quality > 1)) { | |
240 /* | |
241 * blur and "Scale" for downscaling color images | |
242 */ | |
243 if ((scale <= 0.5) && (quality > 1)) { | |
244 int bl = (int) Math.floor(1 / scale); | |
245 // don't blur more than 3 | |
246 blur(Math.min(bl, 3)); | |
247 } | |
248 scaleAll((float) scale); | |
249 } else { | |
250 /* | |
251 * "Scale" for the rest | |
252 */ | |
253 scaleAll((float) scale); | |
254 } | |
255 | |
256 // DEBUG | |
257 logger.debug("SCALE: " + scale + " ->" + img.getWidth() + "x" | |
258 + img.getHeight()); | |
259 | |
260 } | |
261 | |
262 public void scaleAll(float scale) throws ImageOpException { | |
263 RenderedImage scaledImg; | |
264 // DEBUG | |
265 logger.debug("scaleAll: " + scale); | |
266 ParameterBlockJAI param = new ParameterBlockJAI("Scale"); | |
267 param.addSource(img); | |
268 param.setParameter("xScale", scale); | |
269 param.setParameter("yScale", scale); | |
270 param.setParameter("interpolation", interpol); | |
271 // hint with border extender | |
272 RenderingHints hint = new RenderingHints(JAI.KEY_BORDER_EXTENDER, | |
273 BorderExtender.createInstance(BorderExtender.BORDER_COPY)); | |
274 // scale | |
275 scaledImg = JAI.create("Scale", param, hint); | |
276 | |
277 if (scaledImg == null) { | |
278 throw new ImageOpException("Unable to scale"); | |
279 } | |
280 img = scaledImg; | |
281 } | |
282 | |
283 public void blur(int radius) throws ImageOpException { | |
284 RenderedImage blurredImg; | |
285 int klen = Math.max(radius, 2); | |
286 logger.debug("blur: " + klen); | |
287 int ksize = klen * klen; | |
288 float f = 1f / ksize; | |
289 float[] kern = new float[ksize]; | |
290 for (int i = 0; i < ksize; i++) { | |
291 kern[i] = f; | |
292 } | |
293 KernelJAI blur = new KernelJAI(klen, klen, kern); | |
294 ParameterBlockJAI param = new ParameterBlockJAI("Convolve"); | |
295 param.addSource(img); | |
296 param.setParameter("kernel", blur); | |
297 // hint with border extender | |
298 RenderingHints hint = new RenderingHints(JAI.KEY_BORDER_EXTENDER, | |
299 BorderExtender.createInstance(BorderExtender.BORDER_COPY)); | |
300 blurredImg = JAI.create("Convolve", param, hint); | |
301 if (blurredImg == null) { | |
302 throw new ImageOpException("Unable to scale"); | |
303 } | |
304 img = blurredImg; | |
305 } | |
306 | |
307 public void scaleBinary(float scale) throws ImageOpException { | |
308 RenderedImage scaledImg; | |
309 // DEBUG | |
310 logger.debug("scaleBinary: " + scale); | |
311 ParameterBlockJAI param = new ParameterBlockJAI("SubsampleBinaryToGray"); | |
312 param.addSource(img); | |
313 param.setParameter("xScale", scale); | |
314 param.setParameter("yScale", scale); | |
315 // hint with border extender | |
316 RenderingHints hint = new RenderingHints(JAI.KEY_BORDER_EXTENDER, | |
317 BorderExtender.createInstance(BorderExtender.BORDER_COPY)); | |
318 // scale | |
319 scaledImg = JAI.create("SubsampleBinaryToGray", param, hint); | |
320 if (scaledImg == null) { | |
321 throw new ImageOpException("Unable to scale"); | |
322 } | |
323 img = scaledImg; | |
324 } | |
325 | |
326 /* crops the current image */ | |
327 public void crop(int x_off, int y_off, int width, int height) | |
328 throws ImageOpException { | |
329 // setup Crop | |
330 ParameterBlock param = new ParameterBlock(); | |
331 param.addSource(img); | |
332 param.add((float) x_off); | |
333 param.add((float) y_off); | |
334 param.add((float) width); | |
335 param.add((float) height); | |
336 RenderedImage croppedImg = JAI.create("crop", param); | |
337 | |
338 logger.debug("CROP: " + x_off + "," + y_off + ", " + width + "," | |
339 + height + " ->" + croppedImg.getWidth() + "x" | |
340 + croppedImg.getHeight()); | |
341 img = croppedImg; | |
342 } | |
343 | |
344 /* rotates the current image */ | |
345 public void rotate(double angle) throws ImageOpException { | |
346 RenderedImage rotImg; | |
347 // convert degrees to radians | |
348 double rangle = Math.toRadians(angle); | |
349 double x = img.getWidth() / 2; | |
350 double y = img.getHeight() / 2; | |
351 | |
352 // optimize rotation by right angles | |
353 TransposeType rotOp = null; | |
354 if (Math.abs(angle - 0) < epsilon) { | |
355 // 0 degree | |
356 return; | |
357 } else if (Math.abs(angle - 90) < epsilon) { | |
358 // 90 degree | |
359 rotOp = TransposeDescriptor.ROTATE_90; | |
360 } else if (Math.abs(angle - 180) < epsilon) { | |
361 // 180 degree | |
362 rotOp = TransposeDescriptor.ROTATE_180; | |
363 } else if (Math.abs(angle - 270) < epsilon) { | |
364 // 270 degree | |
365 rotOp = TransposeDescriptor.ROTATE_270; | |
366 } else if (Math.abs(angle - 360) < epsilon) { | |
367 // 360 degree | |
368 return; | |
369 } | |
370 if (rotOp != null) { | |
371 // use Transpose operation | |
372 ParameterBlock pb = new ParameterBlock(); | |
373 pb.addSource(img); | |
374 pb.add(rotOp); | |
375 rotImg = JAI.create("transpose", pb); | |
376 } else { | |
377 // setup "normal" rotation | |
378 ParameterBlock param = new ParameterBlock(); | |
379 param.addSource(img); | |
380 param.add((float) x); | |
381 param.add((float) y); | |
382 param.add((float) rangle); | |
383 param.add(interpol); | |
384 | |
385 rotImg = JAI.create("rotate", param); | |
386 } | |
387 | |
388 logger.debug("ROTATE: " + x + "," + y + ", " + angle + " (" + rangle | |
389 + ")" + " ->" + rotImg.getWidth() + "x" + rotImg.getHeight()); | |
390 img = rotImg; | |
391 } | |
392 | |
393 /* | |
394 * mirrors the current image works only horizontal and vertical | |
395 */ | |
396 public void mirror(double angle) throws ImageOpException { | |
397 RenderedImage mirImg; | |
398 // only mirroring by right angles | |
399 TransposeType rotOp = null; | |
400 if (Math.abs(angle) < epsilon) { | |
401 // 0 degree | |
402 rotOp = TransposeDescriptor.FLIP_HORIZONTAL; | |
403 } else if (Math.abs(angle - 90) < epsilon) { | |
404 // 90 degree | |
405 rotOp = TransposeDescriptor.FLIP_VERTICAL; | |
406 } else if (Math.abs(angle - 180) < epsilon) { | |
407 // 180 degree | |
408 rotOp = TransposeDescriptor.FLIP_HORIZONTAL; | |
409 } else if (Math.abs(angle - 270) < epsilon) { | |
410 // 270 degree | |
411 rotOp = TransposeDescriptor.FLIP_VERTICAL; | |
412 } else if (Math.abs(angle - 360) < epsilon) { | |
413 // 360 degree | |
414 rotOp = TransposeDescriptor.FLIP_HORIZONTAL; | |
415 } | |
416 // use Transpose operation | |
417 ParameterBlock param = new ParameterBlock(); | |
418 param.addSource(img); | |
419 param.add(rotOp); | |
420 mirImg = JAI.create("transpose", param); | |
421 | |
422 if (mirImg == null) { | |
423 throw new ImageOpException("Unable to flip"); | |
424 } | |
425 img = mirImg; | |
426 } | |
427 | |
428 /* contrast and brightness enhancement */ | |
429 public void enhance(float mult, float add) throws ImageOpException { | |
430 RenderedImage enhImg; | |
431 double[] ma = { mult }; | |
432 double[] aa = { add }; | |
433 // use Rescale operation | |
434 ParameterBlock param = new ParameterBlock(); | |
435 param.addSource(img); | |
436 param.add(ma); | |
437 param.add(aa); | |
438 enhImg = JAI.create("rescale", param); | |
439 | |
440 logger.debug("ENHANCE: *" + mult + ", +" + add + " ->" | |
441 + enhImg.getWidth() + "x" + enhImg.getHeight()); | |
442 // DEBUG | |
443 img = enhImg; | |
444 } | |
445 | |
446 /* | |
447 * (non-Javadoc) | |
448 * | |
449 * @see digilib.image.DocuImage#enhanceRGB(float[], float[]) | |
450 */ | |
451 public void enhanceRGB(float[] rgbm, float[] rgba) throws ImageOpException { | |
452 RenderedImage enhImg; | |
453 int nb = rgbm.length; | |
454 double[] ma = new double[nb]; | |
455 double[] aa = new double[nb]; | |
456 for (int i = 0; i < nb; i++) { | |
457 ma[i] = rgbm[i]; | |
458 aa[i] = rgba[i]; | |
459 } | |
460 // use Rescale operation | |
461 ParameterBlock param = new ParameterBlock(); | |
462 param.addSource(img); | |
463 param.add(ma); | |
464 param.add(aa); | |
465 enhImg = JAI.create("rescale", param); | |
466 | |
467 logger.debug("ENHANCE_RGB: *" + rgbm + ", +" + rgba + " ->" | |
468 + enhImg.getWidth() + "x" + enhImg.getHeight()); | |
469 img = enhImg; | |
470 } | |
471 | |
472 /* | |
473 * (non-Javadoc) | |
474 * | |
475 * @see digilib.image.DocuImage#dispose() | |
476 */ | |
477 public void dispose() { | |
478 img = null; | |
479 } | |
480 | |
481 } |