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