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