Mercurial > hg > digilib-old
annotate servlet/src/digilib/image/JAIDocuImage.java @ 568:34701340922e stream
more starting stream support
author | robcast |
---|---|
date | Tue, 21 Dec 2010 12:16:33 +0100 |
parents | 50f291d808b1 |
children | beeedf90cb81 |
rev | line source |
---|---|
1 | 1 /* JAIDocuImage -- Image class implementation using JAI (Java Advanced Imaging) |
2 | |
466 | 3 Digital Image Library servlet components |
1 | 4 |
466 | 5 Copyright (C) 2001, 2002, 2003 Robert Casties (robcast@mail.berlios.de) |
1 | 6 |
466 | 7 This program is free software; you can redistribute it and/or modify it |
8 under the terms of the GNU General Public License as published by the | |
9 Free Software Foundation; either version 2 of the License, or (at your | |
10 option) any later version. | |
11 | |
12 Please read license.txt for the full details. A copy of the GPL | |
13 may be found at http://www.gnu.org/copyleft/lgpl.html | |
1 | 14 |
466 | 15 You should have received a copy of the GNU General Public License |
16 along with this program; if not, write to the Free Software | |
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
1 | 18 |
466 | 19 */ |
1 | 20 |
21 package digilib.image; | |
22 | |
470
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
23 import java.awt.Rectangle; |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
24 import java.awt.RenderingHints; |
73 | 25 import java.awt.image.RenderedImage; |
26 import java.awt.image.renderable.ParameterBlock; | |
462
03ff7238c9d4
second try for flashpix support (doesn't work currently...)
robcast
parents:
208
diff
changeset
|
27 import java.io.File; |
73 | 28 import java.io.IOException; |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
29 import java.io.OutputStream; |
466 | 30 import java.util.ArrayList; |
31 import java.util.Enumeration; | |
32 import java.util.Iterator; | |
33 import java.util.List; | |
1 | 34 |
142 | 35 import javax.media.jai.BorderExtender; |
36 import javax.media.jai.Interpolation; | |
37 import javax.media.jai.JAI; | |
38 import javax.media.jai.KernelJAI; | |
39 import javax.media.jai.ParameterBlockJAI; | |
462
03ff7238c9d4
second try for flashpix support (doesn't work currently...)
robcast
parents:
208
diff
changeset
|
40 import javax.media.jai.RenderedOp; |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
41 import javax.media.jai.operator.TransposeDescriptor; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
42 import javax.media.jai.operator.TransposeType; |
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; | |
566 | 50 import digilib.io.ImageInput; |
1 | 51 |
73 | 52 /** A DocuImage implementation using Java Advanced Imaging Library. */ |
564 | 53 public class JAIDocuImage extends ImageInfoDocuImage { |
1 | 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 */ |
566 | 103 public ImageInput identify(ImageFile imageFile) throws IOException { |
563 | 104 // try parent method first |
566 | 105 ImageInput imf = super.identify(imageFile); |
563 | 106 if (imf != null) { |
107 return imf; | |
466 | 108 } |
109 // fileset to store the information | |
563 | 110 ImageFileset imgfs = imageFile.getParent(); |
111 File f = imageFile.getFile(); | |
466 | 112 if (f == null) { |
113 throw new IOException("File not found!"); | |
114 } | |
115 /* | |
116 * try JAI | |
117 */ | |
118 logger.debug("identifying (JAI) " + f); | |
119 try { | |
120 RenderedOp img = JAI.create("fileload", f.getAbsolutePath()); | |
121 ImageSize d = new ImageSize(img.getWidth(), img.getHeight()); | |
563 | 122 imageFile.setSize(d); |
466 | 123 String t = FileOps.mimeForFile(f); |
563 | 124 imageFile.setMimetype(t); |
466 | 125 // logger.debug(" format:"+t); |
126 if (imgfs != null) { | |
127 imgfs.setAspect(d); | |
128 } | |
563 | 129 logger.debug("image size: " + imageFile.getSize()); |
130 return imageFile; | |
466 | 131 } catch (Exception e) { |
132 throw new FileOpException("ERROR: unknown image file format!"); | |
133 } | |
134 } | |
135 | |
136 /* Load an image file into the Object. */ | |
159 | 137 public void loadImage(ImageFile f) throws FileOpException { |
149 | 138 img = JAI.create("fileload", f.getFile().getAbsolutePath()); |
73 | 139 if (img == null) { |
140 throw new FileOpException("Unable to load File!"); | |
141 } | |
543
919e008ab1fb
more steps towards more standard java.util.concurrent design
robcast
parents:
536
diff
changeset
|
142 mimeType = f.getMimetype(); |
73 | 143 } |
1 | 144 |
470
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
145 /* Load an image file into the Object. */ |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
146 public void loadSubimage(ImageFile f, Rectangle region, int subsample) |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
147 throws FileOpException { |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
148 logger.debug("loadSubimage"); |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
149 img = JAI.create("fileload", f.getFile().getAbsolutePath()); |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
150 if ((region.width < img.getWidth()) |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
151 || (region.height < img.getHeight())) { |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
152 // setup Crop |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
153 ParameterBlock cp = new ParameterBlock(); |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
154 cp.addSource(img); |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
155 cp.add((float) region.x); |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
156 cp.add((float) region.y); |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
157 cp.add((float) region.width); |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
158 cp.add((float) region.height); |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
159 logger.debug("loadSubimage: crop"); |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
160 img = JAI.create("crop", cp); |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
161 } |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
162 if (subsample > 1) { |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
163 float sc = 1f / subsample; |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
164 ParameterBlockJAI sp = new ParameterBlockJAI("scale"); |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
165 sp.addSource(img); |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
166 sp.setParameter("xScale", sc); |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
167 sp.setParameter("yScale", sc); |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
168 sp.setParameter("interpolation", Interpolation |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
169 .getInstance(Interpolation.INTERP_NEAREST)); |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
170 // scale |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
171 logger.debug("loadSubimage: scale"); |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
172 img = JAI.create("scale", sp); |
543
919e008ab1fb
more steps towards more standard java.util.concurrent design
robcast
parents:
536
diff
changeset
|
173 mimeType = f.getMimetype(); |
470
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 } |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
176 |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
177 /* Write the current image to an OutputStream. */ |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
178 public void writeImage(String mt, OutputStream ostream) |
466 | 179 throws FileOpException { |
73 | 180 try { |
181 // setup output | |
182 ParameterBlock pb3 = new ParameterBlock(); | |
183 pb3.addSource(img); | |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
184 pb3.add(ostream); |
73 | 185 if (mt == "image/jpeg") { |
186 pb3.add("JPEG"); | |
187 } else if (mt == "image/png") { | |
188 pb3.add("PNG"); | |
189 } else { | |
190 // unknown mime type | |
191 throw new FileOpException("Unknown mime type: " + mt); | |
192 } | |
193 // render output | |
194 JAI.create("encode", pb3); | |
1 | 195 |
73 | 196 } catch (IOException e) { |
197 throw new FileOpException("Error writing image."); | |
198 } | |
199 } | |
1 | 200 |
466 | 201 /** |
202 * The width of the curent image in pixel. | |
203 * | |
73 | 204 * @return Image width in pixels. |
205 */ | |
206 public int getWidth() { | |
207 if (img != null) { | |
208 return img.getWidth(); | |
209 } | |
210 return 0; | |
211 } | |
1 | 212 |
466 | 213 /** |
214 * The height of the curent image in pixel. | |
215 * | |
73 | 216 * @return Image height in pixels. |
217 */ | |
218 public int getHeight() { | |
219 if (img != null) { | |
220 return img.getHeight(); | |
221 } | |
222 return 0; | |
223 } | |
1 | 224 |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
225 /* scales the current image */ |
149 | 226 public void scale(double scale, double scaleY) throws ImageOpException { |
208 | 227 logger.debug("scale"); |
466 | 228 if ((scale < 1) && (img.getColorModel().getPixelSize() == 1) |
229 && (quality > 0)) { | |
142 | 230 /* |
231 * "SubsampleBinaryToGray" for downscaling BW | |
232 */ | |
144 | 233 scaleBinary((float) scale); |
142 | 234 } else if ((scale <= 0.5) && (quality > 1)) { |
235 /* | |
236 * blur and "Scale" for downscaling color images | |
237 */ | |
470
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
238 if ((scale <= 0.5) && (quality > 1)) { |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
239 int bl = (int) Math.floor(1 / scale); |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
240 // don't blur more than 3 |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
241 blur(Math.min(bl, 3)); |
16bd70715ac3
Improved performance of JAIDocuImage for large images
robcast
parents:
468
diff
changeset
|
242 } |
144 | 243 scaleAll((float) scale); |
142 | 244 } else { |
245 /* | |
246 * "Scale" for the rest | |
247 */ | |
144 | 248 scaleAll((float) scale); |
142 | 249 } |
1 | 250 |
466 | 251 // DEBUG |
252 logger.debug("SCALE: " + scale + " ->" + img.getWidth() + "x" | |
253 + img.getHeight()); | |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
254 |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
255 } |
1 | 256 |
144 | 257 public void scaleAll(float scale) throws ImageOpException { |
142 | 258 RenderedImage scaledImg; |
466 | 259 // DEBUG |
181 | 260 logger.debug("scaleAll: " + scale); |
142 | 261 ParameterBlockJAI param = new ParameterBlockJAI("Scale"); |
144 | 262 param.addSource(img); |
142 | 263 param.setParameter("xScale", scale); |
264 param.setParameter("yScale", scale); | |
265 param.setParameter("interpolation", interpol); | |
266 // hint with border extender | |
466 | 267 RenderingHints hint = new RenderingHints(JAI.KEY_BORDER_EXTENDER, |
142 | 268 BorderExtender.createInstance(BorderExtender.BORDER_COPY)); |
269 // scale | |
270 scaledImg = JAI.create("Scale", param, hint); | |
144 | 271 |
272 if (scaledImg == null) { | |
273 throw new ImageOpException("Unable to scale"); | |
274 } | |
275 img = scaledImg; | |
142 | 276 } |
277 | |
144 | 278 public void blur(int radius) throws ImageOpException { |
142 | 279 RenderedImage blurredImg; |
280 int klen = Math.max(radius, 2); | |
468
0fc853d98820
Experiment with TileCache size (better leave it...)
robcast
parents:
466
diff
changeset
|
281 logger.debug("blur: " + klen); |
142 | 282 int ksize = klen * klen; |
283 float f = 1f / ksize; | |
284 float[] kern = new float[ksize]; | |
285 for (int i = 0; i < ksize; i++) { | |
286 kern[i] = f; | |
287 } | |
288 KernelJAI blur = new KernelJAI(klen, klen, kern); | |
289 ParameterBlockJAI param = new ParameterBlockJAI("Convolve"); | |
144 | 290 param.addSource(img); |
142 | 291 param.setParameter("kernel", blur); |
292 // hint with border extender | |
466 | 293 RenderingHints hint = new RenderingHints(JAI.KEY_BORDER_EXTENDER, |
142 | 294 BorderExtender.createInstance(BorderExtender.BORDER_COPY)); |
295 blurredImg = JAI.create("Convolve", param, hint); | |
144 | 296 if (blurredImg == null) { |
297 throw new ImageOpException("Unable to scale"); | |
298 } | |
299 img = blurredImg; | |
142 | 300 } |
301 | |
144 | 302 public void scaleBinary(float scale) throws ImageOpException { |
142 | 303 RenderedImage scaledImg; |
466 | 304 // DEBUG |
181 | 305 logger.debug("scaleBinary: " + scale); |
466 | 306 ParameterBlockJAI param = new ParameterBlockJAI("SubsampleBinaryToGray"); |
144 | 307 param.addSource(img); |
142 | 308 param.setParameter("xScale", scale); |
309 param.setParameter("yScale", scale); | |
310 // hint with border extender | |
466 | 311 RenderingHints hint = new RenderingHints(JAI.KEY_BORDER_EXTENDER, |
142 | 312 BorderExtender.createInstance(BorderExtender.BORDER_COPY)); |
313 // scale | |
314 scaledImg = JAI.create("SubsampleBinaryToGray", param, hint); | |
144 | 315 if (scaledImg == null) { |
316 throw new ImageOpException("Unable to scale"); | |
317 } | |
318 img = scaledImg; | |
142 | 319 } |
320 | |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
321 /* crops the current image */ |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
322 public void crop(int x_off, int y_off, int width, int height) |
466 | 323 throws ImageOpException { |
73 | 324 // setup Crop |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
325 ParameterBlock param = new ParameterBlock(); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
326 param.addSource(img); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
327 param.add((float) x_off); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
328 param.add((float) y_off); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
329 param.add((float) width); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
330 param.add((float) height); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
331 RenderedImage croppedImg = JAI.create("crop", param); |
1 | 332 |
466 | 333 logger.debug("CROP: " + x_off + "," + y_off + ", " + width + "," |
334 + height + " ->" + croppedImg.getWidth() + "x" | |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
335 + croppedImg.getHeight()); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
336 img = croppedImg; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
337 } |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
338 |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
339 /* rotates the current image */ |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
340 public void rotate(double angle) throws ImageOpException { |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
341 RenderedImage rotImg; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
342 // convert degrees to radians |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
343 double rangle = Math.toRadians(angle); |
101 | 344 double x = img.getWidth() / 2; |
345 double y = img.getHeight() / 2; | |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
346 |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
347 // optimize rotation by right angles |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
348 TransposeType rotOp = null; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
349 if (Math.abs(angle - 0) < epsilon) { |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
350 // 0 degree |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
351 return; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
352 } else if (Math.abs(angle - 90) < epsilon) { |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
353 // 90 degree |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
354 rotOp = TransposeDescriptor.ROTATE_90; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
355 } else if (Math.abs(angle - 180) < epsilon) { |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
356 // 180 degree |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
357 rotOp = TransposeDescriptor.ROTATE_180; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
358 } else if (Math.abs(angle - 270) < epsilon) { |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
359 // 270 degree |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
360 rotOp = TransposeDescriptor.ROTATE_270; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
361 } else if (Math.abs(angle - 360) < epsilon) { |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
362 // 360 degree |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
363 return; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
364 } |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
365 if (rotOp != null) { |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
366 // use Transpose operation |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
367 ParameterBlock pb = new ParameterBlock(); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
368 pb.addSource(img); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
369 pb.add(rotOp); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
370 rotImg = JAI.create("transpose", pb); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
371 } else { |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
372 // setup "normal" rotation |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
373 ParameterBlock param = new ParameterBlock(); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
374 param.addSource(img); |
101 | 375 param.add((float) x); |
376 param.add((float) y); | |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
377 param.add((float) rangle); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
378 param.add(interpol); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
379 |
101 | 380 rotImg = JAI.create("rotate", param); |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
381 } |
73 | 382 |
466 | 383 logger.debug("ROTATE: " + x + "," + y + ", " + angle + " (" + rangle |
384 + ")" + " ->" + rotImg.getWidth() + "x" + rotImg.getHeight()); | |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
385 img = rotImg; |
73 | 386 } |
1 | 387 |
466 | 388 /* |
389 * 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
|
390 */ |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
391 public void mirror(double angle) throws ImageOpException { |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
392 RenderedImage mirImg; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
393 // only mirroring by right angles |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
394 TransposeType rotOp = null; |
101 | 395 if (Math.abs(angle) < epsilon) { |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
396 // 0 degree |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
397 rotOp = TransposeDescriptor.FLIP_HORIZONTAL; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
398 } else if (Math.abs(angle - 90) < epsilon) { |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
399 // 90 degree |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
400 rotOp = TransposeDescriptor.FLIP_VERTICAL; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
401 } else if (Math.abs(angle - 180) < epsilon) { |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
402 // 180 degree |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
403 rotOp = TransposeDescriptor.FLIP_HORIZONTAL; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
404 } else if (Math.abs(angle - 270) < epsilon) { |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
405 // 270 degree |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
406 rotOp = TransposeDescriptor.FLIP_VERTICAL; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
407 } else if (Math.abs(angle - 360) < epsilon) { |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
408 // 360 degree |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
409 rotOp = TransposeDescriptor.FLIP_HORIZONTAL; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
410 } |
101 | 411 // use Transpose operation |
412 ParameterBlock param = new ParameterBlock(); | |
413 param.addSource(img); | |
414 param.add(rotOp); | |
415 mirImg = JAI.create("transpose", param); | |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
416 |
101 | 417 if (mirImg == null) { |
418 throw new ImageOpException("Unable to flip"); | |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
419 } |
101 | 420 img = mirImg; |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
421 } |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
422 |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
423 /* contrast and brightness enhancement */ |
86 | 424 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
|
425 RenderedImage enhImg; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
426 double[] ma = { mult }; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
427 double[] aa = { add }; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
428 // use Rescale operation |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
429 ParameterBlock param = new ParameterBlock(); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
430 param.addSource(img); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
431 param.add(ma); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
432 param.add(aa); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
433 enhImg = JAI.create("rescale", param); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
434 |
466 | 435 logger.debug("ENHANCE: *" + mult + ", +" + add + " ->" |
436 + enhImg.getWidth() + "x" + enhImg.getHeight()); | |
437 // DEBUG | |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
438 img = enhImg; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
439 } |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
440 |
466 | 441 /* |
442 * (non-Javadoc) | |
443 * | |
90 | 444 * @see digilib.image.DocuImage#enhanceRGB(float[], float[]) |
445 */ | |
466 | 446 public void enhanceRGB(float[] rgbm, float[] rgba) throws ImageOpException { |
101 | 447 RenderedImage enhImg; |
448 int nb = rgbm.length; | |
449 double[] ma = new double[nb]; | |
450 double[] aa = new double[nb]; | |
451 for (int i = 0; i < nb; i++) { | |
452 ma[i] = rgbm[i]; | |
453 aa[i] = rgba[i]; | |
454 } | |
455 // use Rescale operation | |
456 ParameterBlock param = new ParameterBlock(); | |
457 param.addSource(img); | |
458 param.add(ma); | |
459 param.add(aa); | |
460 enhImg = JAI.create("rescale", param); | |
90 | 461 |
466 | 462 logger.debug("ENHANCE_RGB: *" + rgbm + ", +" + rgba + " ->" |
463 + enhImg.getWidth() + "x" + enhImg.getHeight()); | |
101 | 464 img = enhImg; |
90 | 465 } |
466 | |
466 | 467 /* |
468 * (non-Javadoc) | |
469 * | |
208 | 470 * @see digilib.image.DocuImage#dispose() |
471 */ | |
472 public void dispose() { | |
473 img = null; | |
474 } | |
475 | |
1 | 476 } |