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