Mercurial > hg > digilib-old
annotate servlet/src/digilib/image/JAIDocuImage.java @ 470:16bd70715ac3
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 | e758a49258e8 |
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; |
1 | 43 |
466 | 44 import com.sun.media.jai.codec.ImageCodec; |
45 | |
73 | 46 import digilib.io.FileOpException; |
466 | 47 import digilib.io.FileOps; |
48 import digilib.io.ImageFile; | |
49 import digilib.io.ImageFileset; | |
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 |
470
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
58 /* |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
59 * 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
|
60 * JAI.createTileCache(100*1024*1024); |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
61 * JAI.getDefaultInstance().setTileCache(tc); } |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
62 */ |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
63 |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
64 public boolean isSubimageSupported() { |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
65 return true; |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
66 } |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
67 |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
68 /* |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
69 * Real setQuality implementation. Creates the correct Interpolation. |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
70 */ |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
71 public void setQuality(int qual) { |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
72 quality = qual; |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
73 // setup interpolation quality |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
74 if (qual > 1) { |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
75 logger.debug("quality q2"); |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
76 interpol = Interpolation.getInstance(Interpolation.INTERP_BICUBIC); |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
77 } else if (qual == 1) { |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
78 logger.debug("quality q1"); |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
79 interpol = Interpolation.getInstance(Interpolation.INTERP_BILINEAR); |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
80 } else { |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
81 logger.debug("quality q0"); |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
82 interpol = Interpolation.getInstance(Interpolation.INTERP_NEAREST); |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
83 } |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
84 } |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
85 |
466 | 86 /* returns a list of supported image formats */ |
87 public Iterator getSupportedFormats() { | |
88 Enumeration codecs = ImageCodec.getCodecs(); | |
89 List formats = new ArrayList(5); | |
90 for (Object codec = codecs.nextElement(); codecs.hasMoreElements(); codec = codecs | |
91 .nextElement()) { | |
470
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
92 logger |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
93 .debug("known format:" |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
94 + ((ImageCodec) codec).getFormatName()); |
466 | 95 formats.add(((ImageCodec) codec).getFormatName()); |
96 } | |
470
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
97 logger.debug("tilecachesize:" |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
98 + JAI.getDefaultInstance().getTileCache().getMemoryCapacity()); |
466 | 99 return formats.iterator(); |
100 } | |
462
03ff7238c9d4
second try for flashpix support (doesn't work currently...)
robcast
parents:
208
diff
changeset
|
101 |
470
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
102 /* Check image size and type and store in ImageFile f */ |
466 | 103 public boolean identify(ImageFile imgf) throws IOException { |
104 // try parent method first | |
105 if (super.identify(imgf)) { | |
106 return true; | |
107 } | |
108 // fileset to store the information | |
109 ImageFileset imgfs = imgf.getParent(); | |
110 File f = imgf.getFile(); | |
111 if (f == null) { | |
112 throw new IOException("File not found!"); | |
113 } | |
114 /* | |
115 * try JAI | |
116 */ | |
117 logger.debug("identifying (JAI) " + f); | |
118 try { | |
119 RenderedOp img = JAI.create("fileload", f.getAbsolutePath()); | |
120 ImageSize d = new ImageSize(img.getWidth(), img.getHeight()); | |
121 imgf.setSize(d); | |
122 String t = FileOps.mimeForFile(f); | |
123 imgf.setMimetype(t); | |
124 // logger.debug(" format:"+t); | |
125 if (imgfs != null) { | |
126 imgfs.setAspect(d); | |
127 } | |
128 logger.debug("image size: " + imgf.getSize()); | |
129 return true; | |
130 } catch (Exception e) { | |
131 throw new FileOpException("ERROR: unknown image file format!"); | |
132 } | |
133 } | |
134 | |
135 /* Load an image file into the Object. */ | |
159 | 136 public void loadImage(ImageFile f) throws FileOpException { |
149 | 137 img = JAI.create("fileload", f.getFile().getAbsolutePath()); |
73 | 138 if (img == null) { |
139 throw new FileOpException("Unable to load File!"); | |
140 } | |
141 } | |
1 | 142 |
470
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
143 /* Load an image file into the Object. */ |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
144 public void loadSubimage(ImageFile f, Rectangle region, int subsample) |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
145 throws FileOpException { |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
146 logger.debug("loadSubimage"); |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
147 img = JAI.create("fileload", f.getFile().getAbsolutePath()); |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
148 if ((region.width < img.getWidth()) |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
149 || (region.height < img.getHeight())) { |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
150 // setup Crop |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
151 ParameterBlock cp = new ParameterBlock(); |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
152 cp.addSource(img); |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
153 cp.add((float) region.x); |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
154 cp.add((float) region.y); |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
155 cp.add((float) region.width); |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
156 cp.add((float) region.height); |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
157 logger.debug("loadSubimage: crop"); |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
158 img = JAI.create("crop", cp); |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
159 } |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
160 if (subsample > 1) { |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
161 float sc = 1f / subsample; |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
162 ParameterBlockJAI sp = new ParameterBlockJAI("scale"); |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
163 sp.addSource(img); |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
164 sp.setParameter("xScale", sc); |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
165 sp.setParameter("yScale", sc); |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
166 sp.setParameter("interpolation", Interpolation |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
167 .getInstance(Interpolation.INTERP_NEAREST)); |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
168 // scale |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
169 logger.debug("loadSubimage: scale"); |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
170 img = JAI.create("scale", sp); |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
171 } |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
172 } |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
173 |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
174 /* Write the current image to an OutputStream. */ |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
175 public void writeImage(String mt, OutputStream ostream) |
466 | 176 throws FileOpException { |
73 | 177 try { |
178 // setup output | |
179 ParameterBlock pb3 = new ParameterBlock(); | |
180 pb3.addSource(img); | |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
181 pb3.add(ostream); |
73 | 182 if (mt == "image/jpeg") { |
183 pb3.add("JPEG"); | |
184 } else if (mt == "image/png") { | |
185 pb3.add("PNG"); | |
186 } else { | |
187 // unknown mime type | |
188 throw new FileOpException("Unknown mime type: " + mt); | |
189 } | |
190 // render output | |
191 JAI.create("encode", pb3); | |
1 | 192 |
73 | 193 } catch (IOException e) { |
194 throw new FileOpException("Error writing image."); | |
195 } | |
196 } | |
1 | 197 |
466 | 198 /** |
199 * The width of the curent image in pixel. | |
200 * | |
73 | 201 * @return Image width in pixels. |
202 */ | |
203 public int getWidth() { | |
204 if (img != null) { | |
205 return img.getWidth(); | |
206 } | |
207 return 0; | |
208 } | |
1 | 209 |
466 | 210 /** |
211 * The height of the curent image in pixel. | |
212 * | |
73 | 213 * @return Image height in pixels. |
214 */ | |
215 public int getHeight() { | |
216 if (img != null) { | |
217 return img.getHeight(); | |
218 } | |
219 return 0; | |
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()); |
466 | 333 // DEBUG |
73 | 334 |
335 if (croppedImg == null) { | |
336 throw new ImageOpException("Unable to crop"); | |
337 } | |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
338 img = croppedImg; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
339 } |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
340 |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
341 /* rotates the current image */ |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
342 public void rotate(double angle) throws ImageOpException { |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
343 RenderedImage rotImg; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
344 // convert degrees to radians |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
345 double rangle = Math.toRadians(angle); |
101 | 346 double x = img.getWidth() / 2; |
347 double y = img.getHeight() / 2; | |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
348 |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
349 // optimize rotation by right angles |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
350 TransposeType rotOp = null; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
351 if (Math.abs(angle - 0) < epsilon) { |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
352 // 0 degree |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
353 return; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
354 } else if (Math.abs(angle - 90) < epsilon) { |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
355 // 90 degree |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
356 rotOp = TransposeDescriptor.ROTATE_90; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
357 } else if (Math.abs(angle - 180) < epsilon) { |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
358 // 180 degree |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
359 rotOp = TransposeDescriptor.ROTATE_180; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
360 } else if (Math.abs(angle - 270) < epsilon) { |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
361 // 270 degree |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
362 rotOp = TransposeDescriptor.ROTATE_270; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
363 } else if (Math.abs(angle - 360) < epsilon) { |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
364 // 360 degree |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
365 return; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
366 } |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
367 if (rotOp != null) { |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
368 // use Transpose operation |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
369 ParameterBlock pb = new ParameterBlock(); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
370 pb.addSource(img); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
371 pb.add(rotOp); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
372 rotImg = JAI.create("transpose", pb); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
373 } else { |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
374 // setup "normal" rotation |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
375 ParameterBlock param = new ParameterBlock(); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
376 param.addSource(img); |
101 | 377 param.add((float) x); |
378 param.add((float) y); | |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
379 param.add((float) rangle); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
380 param.add(interpol); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
381 |
101 | 382 rotImg = JAI.create("rotate", param); |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
383 } |
73 | 384 |
466 | 385 logger.debug("ROTATE: " + x + "," + y + ", " + angle + " (" + rangle |
386 + ")" + " ->" + rotImg.getWidth() + "x" + rotImg.getHeight()); | |
387 // DEBUG | |
1 | 388 |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
389 if (rotImg == null) { |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
390 throw new ImageOpException("Unable to rotate"); |
73 | 391 } |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
392 img = rotImg; |
73 | 393 } |
1 | 394 |
466 | 395 /* |
396 * 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
|
397 */ |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
398 public void mirror(double angle) throws ImageOpException { |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
399 RenderedImage mirImg; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
400 // only mirroring by right angles |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
401 TransposeType rotOp = null; |
101 | 402 if (Math.abs(angle) < epsilon) { |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
403 // 0 degree |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
404 rotOp = TransposeDescriptor.FLIP_HORIZONTAL; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
405 } else if (Math.abs(angle - 90) < epsilon) { |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
406 // 90 degree |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
407 rotOp = TransposeDescriptor.FLIP_VERTICAL; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
408 } else if (Math.abs(angle - 180) < epsilon) { |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
409 // 180 degree |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
410 rotOp = TransposeDescriptor.FLIP_HORIZONTAL; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
411 } else if (Math.abs(angle - 270) < epsilon) { |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
412 // 270 degree |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
413 rotOp = TransposeDescriptor.FLIP_VERTICAL; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
414 } else if (Math.abs(angle - 360) < epsilon) { |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
415 // 360 degree |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
416 rotOp = TransposeDescriptor.FLIP_HORIZONTAL; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
417 } |
101 | 418 // use Transpose operation |
419 ParameterBlock param = new ParameterBlock(); | |
420 param.addSource(img); | |
421 param.add(rotOp); | |
422 mirImg = JAI.create("transpose", param); | |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
423 |
101 | 424 if (mirImg == null) { |
425 throw new ImageOpException("Unable to flip"); | |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
426 } |
101 | 427 img = mirImg; |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
428 } |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
429 |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
430 /* contrast and brightness enhancement */ |
86 | 431 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
|
432 RenderedImage enhImg; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
433 double[] ma = { mult }; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
434 double[] aa = { add }; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
435 // use Rescale operation |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
436 ParameterBlock param = new ParameterBlock(); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
437 param.addSource(img); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
438 param.add(ma); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
439 param.add(aa); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
440 enhImg = JAI.create("rescale", param); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
441 |
466 | 442 logger.debug("ENHANCE: *" + mult + ", +" + add + " ->" |
443 + enhImg.getWidth() + "x" + enhImg.getHeight()); | |
444 // DEBUG | |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
445 |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
446 if (enhImg == null) { |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
447 throw new ImageOpException("Unable to enhance"); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
448 } |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
449 img = enhImg; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
450 } |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
451 |
466 | 452 /* |
453 * (non-Javadoc) | |
454 * | |
90 | 455 * @see digilib.image.DocuImage#enhanceRGB(float[], float[]) |
456 */ | |
466 | 457 public void enhanceRGB(float[] rgbm, float[] rgba) throws ImageOpException { |
101 | 458 RenderedImage enhImg; |
459 int nb = rgbm.length; | |
460 double[] ma = new double[nb]; | |
461 double[] aa = new double[nb]; | |
462 for (int i = 0; i < nb; i++) { | |
463 ma[i] = rgbm[i]; | |
464 aa[i] = rgba[i]; | |
465 } | |
466 // use Rescale operation | |
467 ParameterBlock param = new ParameterBlock(); | |
468 param.addSource(img); | |
469 param.add(ma); | |
470 param.add(aa); | |
471 enhImg = JAI.create("rescale", param); | |
90 | 472 |
466 | 473 logger.debug("ENHANCE_RGB: *" + rgbm + ", +" + rgba + " ->" |
474 + enhImg.getWidth() + "x" + enhImg.getHeight()); | |
475 // DEBUG | |
90 | 476 |
101 | 477 if (enhImg == null) { |
478 throw new ImageOpException("Unable to enhanceRGB"); | |
479 } | |
480 img = enhImg; | |
90 | 481 } |
482 | |
466 | 483 /* |
484 * (non-Javadoc) | |
485 * | |
208 | 486 * @see digilib.image.DocuImage#dispose() |
487 */ | |
488 public void dispose() { | |
489 img = null; | |
490 } | |
491 | |
1 | 492 } |