Mercurial > hg > digilib-old
annotate common/src/main/java/digilib/image/JAIDocuImage.java @ 912:cbfc94584d3b
reset imgSize in operations that change size.
author | robcast |
---|---|
date | Wed, 08 Jun 2011 17:49:01 +0200 |
parents | 28d007673346 |
children |
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; |
1 | 42 |
466 | 43 import com.sun.media.jai.codec.ImageCodec; |
44 | |
73 | 45 import digilib.io.FileOpException; |
466 | 46 import digilib.io.FileOps; |
579
efd7a223f819
try: ImageInput as interface, ImageFile inherits from Dirent and implements ImageInput
robcast
parents:
574
diff
changeset
|
47 import digilib.io.ImageInput; |
596 | 48 import digilib.util.ImageSize; |
1 | 49 |
73 | 50 /** A DocuImage implementation using Java Advanced Imaging Library. */ |
570 | 51 /** |
52 * @author casties | |
53 * | |
54 */ | |
564 | 55 public class JAIDocuImage extends ImageInfoDocuImage { |
1 | 56 |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
57 protected RenderedImage img; |
466 | 58 |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
59 protected Interpolation interpol = null; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
60 |
470
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
61 /* |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
62 * 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
|
63 * JAI.createTileCache(100*1024*1024); |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
64 * JAI.getDefaultInstance().setTileCache(tc); } |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
65 */ |
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 public boolean isSubimageSupported() { |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
68 return true; |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
69 } |
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 * Real setQuality implementation. Creates the correct Interpolation. |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
73 */ |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
74 public void setQuality(int qual) { |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
75 quality = qual; |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
76 // setup interpolation quality |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
77 if (qual > 1) { |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
78 logger.debug("quality q2"); |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
79 interpol = Interpolation.getInstance(Interpolation.INTERP_BICUBIC); |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
80 } else if (qual == 1) { |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
81 logger.debug("quality q1"); |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
82 interpol = Interpolation.getInstance(Interpolation.INTERP_BILINEAR); |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
83 } else { |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
84 logger.debug("quality q0"); |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
85 interpol = Interpolation.getInstance(Interpolation.INTERP_NEAREST); |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
86 } |
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 |
466 | 89 /* returns a list of supported image formats */ |
531 | 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(); | |
1 | 102 } |
462
03ff7238c9d4
second try for flashpix support (doesn't work currently...)
robcast
parents:
208
diff
changeset
|
103 |
470
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
104 /* Check image size and type and store in ImageFile f */ |
588 | 105 public ImageInput identify(ImageInput input) throws IOException { |
106 this.input = input; | |
563 | 107 // try parent method first |
588 | 108 ImageInput imf = super.identify(input); |
563 | 109 if (imf != null) { |
110 return imf; | |
466 | 111 } |
112 /* | |
113 * try JAI | |
114 */ | |
588 | 115 logger.debug("identifying (JAI) " + input); |
466 | 116 try { |
588 | 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 } | |
466 | 128 ImageSize d = new ImageSize(img.getWidth(), img.getHeight()); |
588 | 129 input.setSize(d); |
130 logger.debug("image size: " + d); | |
131 return input; | |
466 | 132 } catch (Exception e) { |
588 | 133 throw new FileOpException("ERROR: unable to identify image!"); |
466 | 134 } |
135 } | |
136 | |
137 /* Load an image file into the Object. */ | |
588 | 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 } | |
73 | 147 if (img == null) { |
148 throw new FileOpException("Unable to load File!"); | |
149 } | |
150 } | |
1 | 151 |
470
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
152 /* Load an image file into the Object. */ |
588 | 153 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
|
154 logger.debug("loadSubimage"); |
588 | 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 } | |
470
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
163 if ((region.width < img.getWidth()) |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
164 || (region.height < img.getHeight())) { |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
165 // setup Crop |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
166 ParameterBlock cp = new ParameterBlock(); |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
167 cp.addSource(img); |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
168 cp.add((float) region.x); |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
169 cp.add((float) region.y); |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
170 cp.add((float) region.width); |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
171 cp.add((float) region.height); |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
172 logger.debug("loadSubimage: crop"); |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
173 img = JAI.create("crop", cp); |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
174 } |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
175 if (subsample > 1) { |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
176 float sc = 1f / subsample; |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
177 ParameterBlockJAI sp = new ParameterBlockJAI("scale"); |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
178 sp.addSource(img); |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
179 sp.setParameter("xScale", sc); |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
180 sp.setParameter("yScale", sc); |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
181 sp.setParameter("interpolation", Interpolation |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
182 .getInstance(Interpolation.INTERP_NEAREST)); |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
183 // scale |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
184 logger.debug("loadSubimage: scale"); |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
185 img = JAI.create("scale", sp); |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
186 } |
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 |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
189 /* Write the current image to an OutputStream. */ |
906 | 190 public void writeImage(String mt, OutputStream ostream) throws ImageOpException, FileOpException { |
73 | 191 try { |
192 // setup output | |
193 ParameterBlock pb3 = new ParameterBlock(); | |
194 pb3.addSource(img); | |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
195 pb3.add(ostream); |
73 | 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 | |
570 | 202 throw new ImageOpException("Unknown mime type: " + mt); |
73 | 203 } |
204 // render output | |
205 JAI.create("encode", pb3); | |
1 | 206 |
570 | 207 } catch (RuntimeException e) { |
208 // JAI likes to throw RuntimeExceptions | |
906 | 209 throw new FileOpException("Error writing image: "+e); |
73 | 210 } |
211 } | |
1 | 212 |
570 | 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 } | |
1 | 229 |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
230 /* scales the current image */ |
149 | 231 public void scale(double scale, double scaleY) throws ImageOpException { |
208 | 232 logger.debug("scale"); |
466 | 233 if ((scale < 1) && (img.getColorModel().getPixelSize() == 1) |
234 && (quality > 0)) { | |
142 | 235 /* |
236 * "SubsampleBinaryToGray" for downscaling BW | |
237 */ | |
144 | 238 scaleBinary((float) scale); |
142 | 239 } else if ((scale <= 0.5) && (quality > 1)) { |
240 /* | |
241 * blur and "Scale" for downscaling color images | |
242 */ | |
470
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
243 if ((scale <= 0.5) && (quality > 1)) { |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
244 int bl = (int) Math.floor(1 / scale); |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
245 // don't blur more than 3 |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
246 blur(Math.min(bl, 3)); |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
247 } |
144 | 248 scaleAll((float) scale); |
142 | 249 } else { |
250 /* | |
251 * "Scale" for the rest | |
252 */ | |
144 | 253 scaleAll((float) scale); |
142 | 254 } |
1 | 255 |
466 | 256 // DEBUG |
257 logger.debug("SCALE: " + scale + " ->" + img.getWidth() + "x" | |
258 + img.getHeight()); | |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
259 |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
260 } |
1 | 261 |
144 | 262 public void scaleAll(float scale) throws ImageOpException { |
142 | 263 RenderedImage scaledImg; |
466 | 264 // DEBUG |
181 | 265 logger.debug("scaleAll: " + scale); |
142 | 266 ParameterBlockJAI param = new ParameterBlockJAI("Scale"); |
144 | 267 param.addSource(img); |
142 | 268 param.setParameter("xScale", scale); |
269 param.setParameter("yScale", scale); | |
270 param.setParameter("interpolation", interpol); | |
271 // hint with border extender | |
466 | 272 RenderingHints hint = new RenderingHints(JAI.KEY_BORDER_EXTENDER, |
142 | 273 BorderExtender.createInstance(BorderExtender.BORDER_COPY)); |
274 // scale | |
275 scaledImg = JAI.create("Scale", param, hint); | |
144 | 276 |
277 if (scaledImg == null) { | |
278 throw new ImageOpException("Unable to scale"); | |
279 } | |
280 img = scaledImg; | |
142 | 281 } |
282 | |
144 | 283 public void blur(int radius) throws ImageOpException { |
142 | 284 RenderedImage blurredImg; |
285 int klen = Math.max(radius, 2); | |
468
0fc853d98820
Experiment with TileCache size (better leave it...)
robcast
parents:
466
diff
changeset
|
286 logger.debug("blur: " + klen); |
142 | 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"); | |
144 | 295 param.addSource(img); |
142 | 296 param.setParameter("kernel", blur); |
297 // hint with border extender | |
466 | 298 RenderingHints hint = new RenderingHints(JAI.KEY_BORDER_EXTENDER, |
142 | 299 BorderExtender.createInstance(BorderExtender.BORDER_COPY)); |
300 blurredImg = JAI.create("Convolve", param, hint); | |
144 | 301 if (blurredImg == null) { |
302 throw new ImageOpException("Unable to scale"); | |
303 } | |
304 img = blurredImg; | |
142 | 305 } |
306 | |
144 | 307 public void scaleBinary(float scale) throws ImageOpException { |
142 | 308 RenderedImage scaledImg; |
466 | 309 // DEBUG |
181 | 310 logger.debug("scaleBinary: " + scale); |
466 | 311 ParameterBlockJAI param = new ParameterBlockJAI("SubsampleBinaryToGray"); |
144 | 312 param.addSource(img); |
142 | 313 param.setParameter("xScale", scale); |
314 param.setParameter("yScale", scale); | |
315 // hint with border extender | |
466 | 316 RenderingHints hint = new RenderingHints(JAI.KEY_BORDER_EXTENDER, |
142 | 317 BorderExtender.createInstance(BorderExtender.BORDER_COPY)); |
318 // scale | |
319 scaledImg = JAI.create("SubsampleBinaryToGray", param, hint); | |
144 | 320 if (scaledImg == null) { |
321 throw new ImageOpException("Unable to scale"); | |
322 } | |
323 img = scaledImg; | |
142 | 324 } |
325 | |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
326 /* crops the current image */ |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
327 public void crop(int x_off, int y_off, int width, int height) |
466 | 328 throws ImageOpException { |
73 | 329 // setup Crop |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
330 ParameterBlock param = new ParameterBlock(); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
331 param.addSource(img); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
332 param.add((float) x_off); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
333 param.add((float) y_off); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
334 param.add((float) width); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
335 param.add((float) height); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
336 RenderedImage croppedImg = JAI.create("crop", param); |
1 | 337 |
466 | 338 logger.debug("CROP: " + x_off + "," + y_off + ", " + width + "," |
339 + height + " ->" + croppedImg.getWidth() + "x" | |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
340 + croppedImg.getHeight()); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
341 img = croppedImg; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
342 } |
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 /* rotates the current image */ |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
345 public void rotate(double angle) throws ImageOpException { |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
346 RenderedImage rotImg; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
347 // convert degrees to radians |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
348 double rangle = Math.toRadians(angle); |
101 | 349 double x = img.getWidth() / 2; |
350 double y = img.getHeight() / 2; | |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
351 |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
352 // optimize rotation by right angles |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
353 TransposeType rotOp = null; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
354 if (Math.abs(angle - 0) < epsilon) { |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
355 // 0 degree |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
356 return; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
357 } else if (Math.abs(angle - 90) < epsilon) { |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
358 // 90 degree |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
359 rotOp = TransposeDescriptor.ROTATE_90; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
360 } else if (Math.abs(angle - 180) < epsilon) { |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
361 // 180 degree |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
362 rotOp = TransposeDescriptor.ROTATE_180; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
363 } else if (Math.abs(angle - 270) < epsilon) { |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
364 // 270 degree |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
365 rotOp = TransposeDescriptor.ROTATE_270; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
366 } else if (Math.abs(angle - 360) < epsilon) { |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
367 // 360 degree |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
368 return; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
369 } |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
370 if (rotOp != null) { |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
371 // use Transpose operation |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
372 ParameterBlock pb = new ParameterBlock(); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
373 pb.addSource(img); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
374 pb.add(rotOp); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
375 rotImg = JAI.create("transpose", pb); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
376 } else { |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
377 // setup "normal" rotation |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
378 ParameterBlock param = new ParameterBlock(); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
379 param.addSource(img); |
101 | 380 param.add((float) x); |
381 param.add((float) y); | |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
382 param.add((float) rangle); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
383 param.add(interpol); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
384 |
101 | 385 rotImg = JAI.create("rotate", param); |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
386 } |
73 | 387 |
466 | 388 logger.debug("ROTATE: " + x + "," + y + ", " + angle + " (" + rangle |
389 + ")" + " ->" + rotImg.getWidth() + "x" + rotImg.getHeight()); | |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
390 img = rotImg; |
73 | 391 } |
1 | 392 |
466 | 393 /* |
394 * 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
|
395 */ |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
396 public void mirror(double angle) throws ImageOpException { |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
397 RenderedImage mirImg; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
398 // only mirroring by right angles |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
399 TransposeType rotOp = null; |
101 | 400 if (Math.abs(angle) < epsilon) { |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
401 // 0 degree |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
402 rotOp = TransposeDescriptor.FLIP_HORIZONTAL; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
403 } else if (Math.abs(angle - 90) < epsilon) { |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
404 // 90 degree |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
405 rotOp = TransposeDescriptor.FLIP_VERTICAL; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
406 } else if (Math.abs(angle - 180) < epsilon) { |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
407 // 180 degree |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
408 rotOp = TransposeDescriptor.FLIP_HORIZONTAL; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
409 } else if (Math.abs(angle - 270) < epsilon) { |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
410 // 270 degree |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
411 rotOp = TransposeDescriptor.FLIP_VERTICAL; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
412 } else if (Math.abs(angle - 360) < epsilon) { |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
413 // 360 degree |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
414 rotOp = TransposeDescriptor.FLIP_HORIZONTAL; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
415 } |
101 | 416 // use Transpose operation |
417 ParameterBlock param = new ParameterBlock(); | |
418 param.addSource(img); | |
419 param.add(rotOp); | |
420 mirImg = JAI.create("transpose", param); | |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
421 |
101 | 422 if (mirImg == null) { |
423 throw new ImageOpException("Unable to flip"); | |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
424 } |
101 | 425 img = mirImg; |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
426 } |
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 /* contrast and brightness enhancement */ |
86 | 429 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
|
430 RenderedImage enhImg; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
431 double[] ma = { mult }; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
432 double[] aa = { add }; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
433 // use Rescale operation |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
434 ParameterBlock param = new ParameterBlock(); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
435 param.addSource(img); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
436 param.add(ma); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
437 param.add(aa); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
438 enhImg = JAI.create("rescale", param); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
439 |
466 | 440 logger.debug("ENHANCE: *" + mult + ", +" + add + " ->" |
441 + enhImg.getWidth() + "x" + enhImg.getHeight()); | |
442 // DEBUG | |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
443 img = enhImg; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
444 } |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
445 |
466 | 446 /* |
447 * (non-Javadoc) | |
448 * | |
90 | 449 * @see digilib.image.DocuImage#enhanceRGB(float[], float[]) |
450 */ | |
466 | 451 public void enhanceRGB(float[] rgbm, float[] rgba) throws ImageOpException { |
101 | 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); | |
90 | 466 |
466 | 467 logger.debug("ENHANCE_RGB: *" + rgbm + ", +" + rgba + " ->" |
468 + enhImg.getWidth() + "x" + enhImg.getHeight()); | |
101 | 469 img = enhImg; |
90 | 470 } |
471 | |
466 | 472 /* |
473 * (non-Javadoc) | |
474 * | |
208 | 475 * @see digilib.image.DocuImage#dispose() |
476 */ | |
477 public void dispose() { | |
478 img = null; | |
479 } | |
480 | |
1 | 481 } |