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