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