Mercurial > hg > digilib-old
annotate servlet/src/digilib/image/ImageLoaderDocuImage.java @ 573:beeedf90cb81 stream
Merge from HEAD
fd2ef7e461198ef4a9d9a02459be2057038d600d
author | robcast |
---|---|
date | Wed, 22 Dec 2010 09:54:34 +0100 |
parents | 50f291d808b1 fd2ef7e46119 |
children | 790cbfb58b52 |
rev | line source |
---|---|
1 | 1 /* ImageLoaderDocuImage -- Image class implementation using JDK 1.4 ImageLoader |
2 | |
279 | 3 Digital Image Library servlet components |
1 | 4 |
279 | 5 Copyright (C) 2002, 2003 Robert Casties (robcast@mail.berlios.de) |
1 | 6 |
279 | 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 |
279 | 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 | |
18 */ | |
1 | 19 |
20 package digilib.image; | |
21 | |
496 | 22 import java.awt.Image; |
85 | 23 import java.awt.Rectangle; |
207 | 24 import java.awt.RenderingHints; |
73 | 25 import java.awt.geom.AffineTransform; |
101 | 26 import java.awt.geom.Rectangle2D; |
73 | 27 import java.awt.image.AffineTransformOp; |
28 import java.awt.image.BufferedImage; | |
144 | 29 import java.awt.image.ConvolveOp; |
30 import java.awt.image.Kernel; | |
85 | 31 import java.awt.image.RescaleOp; |
73 | 32 import java.io.File; |
33 import java.io.IOException; | |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
34 import java.io.OutputStream; |
89 | 35 import java.io.RandomAccessFile; |
464 | 36 import java.util.Arrays; |
85 | 37 import java.util.Iterator; |
1 | 38 |
352 | 39 import javax.imageio.IIOImage; |
73 | 40 import javax.imageio.ImageIO; |
85 | 41 import javax.imageio.ImageReadParam; |
42 import javax.imageio.ImageReader; | |
352 | 43 import javax.imageio.ImageWriteParam; |
44 import javax.imageio.ImageWriter; | |
220 | 45 import javax.imageio.stream.FileImageInputStream; |
85 | 46 import javax.imageio.stream.ImageInputStream; |
352 | 47 import javax.imageio.stream.ImageOutputStream; |
570 | 48 import javax.servlet.ServletException; |
1 | 49 |
207 | 50 import digilib.io.FileOpException; |
464 | 51 import digilib.io.FileOps; |
159 | 52 import digilib.io.ImageFile; |
464 | 53 import digilib.io.ImageFileset; |
566 | 54 import digilib.io.ImageInput; |
1 | 55 |
73 | 56 /** Implementation of DocuImage using the ImageLoader API of Java 1.4 and Java2D. */ |
564 | 57 public class ImageLoaderDocuImage extends ImageInfoDocuImage { |
279 | 58 |
86 | 59 /** image object */ |
60 protected BufferedImage img; | |
570 | 61 |
86 | 62 /** interpolation type */ |
207 | 63 protected RenderingHints renderHint; |
279 | 64 |
86 | 65 /** ImageIO image reader */ |
66 protected ImageReader reader; | |
279 | 67 |
86 | 68 /** File that was read */ |
69 protected File imgFile; | |
85 | 70 |
71 /* loadSubimage is supported. */ | |
72 public boolean isSubimageSupported() { | |
73 return true; | |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
74 } |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
75 |
85 | 76 public void setQuality(int qual) { |
77 quality = qual; | |
207 | 78 renderHint = new RenderingHints(null); |
352 | 79 // hint.put(RenderingHints.KEY_ANTIALIASING, |
279 | 80 // RenderingHints.VALUE_ANTIALIAS_OFF); |
85 | 81 // setup interpolation quality |
82 if (qual > 0) { | |
181 | 83 logger.debug("quality q1"); |
279 | 84 renderHint.put(RenderingHints.KEY_INTERPOLATION, |
85 RenderingHints.VALUE_INTERPOLATION_BICUBIC); | |
85 | 86 } else { |
181 | 87 logger.debug("quality q0"); |
279 | 88 renderHint.put(RenderingHints.KEY_INTERPOLATION, |
89 RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR); | |
85 | 90 } |
91 } | |
86 | 92 |
570 | 93 /* returns the size of the current image */ |
94 public ImageSize getSize() { | |
95 ImageSize is = null; | |
96 // TODO: do we want to cache imageSize? | |
97 int h = 0; | |
98 int w = 0; | |
99 try { | |
100 if (img == null) { | |
101 // get size from ImageReader | |
102 h = reader.getHeight(0); | |
103 w = reader.getWidth(0); | |
104 } else { | |
105 // get size from image | |
106 h = img.getHeight(); | |
107 w = img.getWidth(); | |
108 } | |
109 is = new ImageSize(w, h); | |
110 } catch (IOException e) { | |
111 logger.debug("error in getSize:", e); | |
112 } | |
113 return is; | |
114 } | |
1 | 115 |
464 | 116 /* returns a list of supported image formats */ |
531 | 117 public Iterator<String> getSupportedFormats() { |
464 | 118 String[] formats = ImageIO.getReaderFormatNames(); |
119 return Arrays.asList(formats).iterator(); | |
120 } | |
121 | |
462
03ff7238c9d4
second try for flashpix support (doesn't work currently...)
robcast
parents:
402
diff
changeset
|
122 /** Check image size and type and store in ImageFile f */ |
566 | 123 public ImageInput identify(ImageFile imageFile) throws IOException { |
462
03ff7238c9d4
second try for flashpix support (doesn't work currently...)
robcast
parents:
402
diff
changeset
|
124 // try parent method first |
566 | 125 ImageInput imf = super.identify(imageFile); |
563 | 126 if (imf != null) { |
127 return imf; | |
462
03ff7238c9d4
second try for flashpix support (doesn't work currently...)
robcast
parents:
402
diff
changeset
|
128 } |
03ff7238c9d4
second try for flashpix support (doesn't work currently...)
robcast
parents:
402
diff
changeset
|
129 // fileset to store the information |
563 | 130 ImageFileset imgfs = imageFile.getParent(); |
131 File f = imageFile.getFile(); | |
462
03ff7238c9d4
second try for flashpix support (doesn't work currently...)
robcast
parents:
402
diff
changeset
|
132 if (f == null) { |
03ff7238c9d4
second try for flashpix support (doesn't work currently...)
robcast
parents:
402
diff
changeset
|
133 throw new IOException("File not found!"); |
03ff7238c9d4
second try for flashpix support (doesn't work currently...)
robcast
parents:
402
diff
changeset
|
134 } |
03ff7238c9d4
second try for flashpix support (doesn't work currently...)
robcast
parents:
402
diff
changeset
|
135 logger.debug("identifying (ImageIO) " + f); |
03ff7238c9d4
second try for flashpix support (doesn't work currently...)
robcast
parents:
402
diff
changeset
|
136 /* |
03ff7238c9d4
second try for flashpix support (doesn't work currently...)
robcast
parents:
402
diff
changeset
|
137 * try ImageReader |
03ff7238c9d4
second try for flashpix support (doesn't work currently...)
robcast
parents:
402
diff
changeset
|
138 */ |
564 | 139 if ((reader == null) || (imgFile != imageFile.getFile())) { |
140 getReader(imageFile); | |
462
03ff7238c9d4
second try for flashpix support (doesn't work currently...)
robcast
parents:
402
diff
changeset
|
141 } |
564 | 142 ImageSize d = new ImageSize(reader.getWidth(0), reader.getHeight(0)); |
143 imageFile.setSize(d); | |
144 // String t = reader.getFormatName(); | |
145 String t = FileOps.mimeForFile(f); | |
146 imageFile.setMimetype(t); | |
147 // logger.debug(" format:"+t); | |
148 if (imgfs != null) { | |
149 imgfs.setAspect(d); | |
150 } | |
151 return imageFile; | |
462
03ff7238c9d4
second try for flashpix support (doesn't work currently...)
robcast
parents:
402
diff
changeset
|
152 } |
03ff7238c9d4
second try for flashpix support (doesn't work currently...)
robcast
parents:
402
diff
changeset
|
153 |
03ff7238c9d4
second try for flashpix support (doesn't work currently...)
robcast
parents:
402
diff
changeset
|
154 /* load image file */ |
159 | 155 public void loadImage(ImageFile f) throws FileOpException { |
279 | 156 logger.debug("loadImage " + f.getFile()); |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
157 try { |
149 | 158 img = ImageIO.read(f.getFile()); |
543
919e008ab1fb
more steps towards more standard java.util.concurrent design
robcast
parents:
536
diff
changeset
|
159 mimeType = f.getMimetype(); |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
160 } catch (IOException e) { |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
161 throw new FileOpException("Error reading image."); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
162 } |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
163 } |
1 | 164 |
279 | 165 /** |
166 * Get an ImageReader for the image file. | |
167 * | |
168 * @return | |
169 */ | |
170 public ImageReader getReader(ImageFile f) throws IOException { | |
171 logger.debug("preloadImage " + f.getFile()); | |
148 | 172 if (reader != null) { |
207 | 173 logger.debug("Reader was not null!"); |
148 | 174 // clean up old reader |
207 | 175 dispose(); |
148 | 176 } |
149 | 177 RandomAccessFile rf = new RandomAccessFile(f.getFile(), "r"); |
220 | 178 ImageInputStream istream = new FileImageInputStream(rf); |
565 | 179 Iterator<ImageReader> readers; |
279 | 180 String mt = f.getMimetype(); |
565 | 181 if (mt == null) { |
182 logger.debug("No mime-type. Trying automagic."); | |
183 readers = ImageIO.getImageReaders(istream); | |
184 } else { | |
185 logger.debug("File type:" + mt); | |
186 readers = ImageIO.getImageReadersByMIMEType(mt); | |
187 } | |
279 | 188 if (!readers.hasNext()) { |
564 | 189 rf.close(); |
565 | 190 throw new FileOpException("Can't find Reader to load File!"); |
279 | 191 } |
531 | 192 reader = readers.next(); |
149 | 193 /* are there more readers? */ |
207 | 194 logger.debug("ImageIO: this reader: " + reader.getClass()); |
564 | 195 /* while (readers.hasNext()) { |
207 | 196 logger.debug("ImageIO: next reader: " + readers.next().getClass()); |
564 | 197 } */ |
89 | 198 reader.setInput(istream); |
149 | 199 imgFile = f.getFile(); |
279 | 200 return reader; |
85 | 201 } |
202 | |
203 /* Load an image file into the Object. */ | |
159 | 204 public void loadSubimage(ImageFile f, Rectangle region, int prescale) |
279 | 205 throws FileOpException { |
207 | 206 logger.debug("loadSubimage"); |
85 | 207 try { |
149 | 208 if ((reader == null) || (imgFile != f.getFile())) { |
279 | 209 getReader(f); |
85 | 210 } |
211 // set up reader parameters | |
212 ImageReadParam readParam = reader.getDefaultReadParam(); | |
213 readParam.setSourceRegion(region); | |
220 | 214 if (prescale > 1) { |
215 readParam.setSourceSubsampling(prescale, prescale, 0, 0); | |
216 } | |
85 | 217 // read image |
207 | 218 logger.debug("loading.."); |
85 | 219 img = reader.read(0, readParam); |
543
919e008ab1fb
more steps towards more standard java.util.concurrent design
robcast
parents:
536
diff
changeset
|
220 mimeType = f.getMimetype(); |
207 | 221 logger.debug("loaded"); |
85 | 222 } catch (IOException e) { |
223 throw new FileOpException("Unable to load File!"); | |
224 } | |
225 } | |
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
226 |
86 | 227 /* write image of type mt to Stream */ |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
228 public void writeImage(String mt, OutputStream ostream) |
570 | 229 throws ImageOpException, ServletException { |
207 | 230 logger.debug("writeImage"); |
353 | 231 // setup output |
232 ImageWriter writer = null; | |
233 ImageOutputStream imgout = null; | |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
234 try { |
353 | 235 imgout = ImageIO.createImageOutputStream(ostream); |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
236 if (mt == "image/jpeg") { |
352 | 237 /* |
238 * JPEG doesn't do transparency so we have to convert any RGBA | |
239 * image to RGB :-( *Java2D BUG* | |
240 */ | |
241 if (img.getColorModel().hasAlpha()) { | |
242 logger.debug("BARF: JPEG with transparency!!"); | |
243 int w = img.getWidth(); | |
244 int h = img.getHeight(); | |
245 // BufferedImage.TYPE_INT_RGB seems to be fastest (JDK1.4.1, | |
246 // OSX) | |
247 int destType = BufferedImage.TYPE_INT_RGB; | |
248 BufferedImage img2 = new BufferedImage(w, h, destType); | |
249 img2.createGraphics().drawImage(img, null, 0, 0); | |
250 img = img2; | |
251 } | |
252 writer = (ImageWriter) ImageIO.getImageWritersByFormatName( | |
253 "jpeg").next(); | |
254 if (writer == null) { | |
570 | 255 throw new ImageOpException("Unable to get JPEG writer"); |
352 | 256 } |
257 ImageWriteParam param = writer.getDefaultWriteParam(); | |
258 if (quality > 1) { | |
259 // change JPEG compression quality | |
353 | 260 param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); |
469
11e11fe4d680
Improved performance of JAIDocuImage for large images
robcast
parents:
464
diff
changeset
|
261 //logger.debug("JPEG qual before: " |
11e11fe4d680
Improved performance of JAIDocuImage for large images
robcast
parents:
464
diff
changeset
|
262 // + Float.toString(param.getCompressionQuality())); |
352 | 263 param.setCompressionQuality(0.9f); |
469
11e11fe4d680
Improved performance of JAIDocuImage for large images
robcast
parents:
464
diff
changeset
|
264 //logger.debug("JPEG qual now: " |
11e11fe4d680
Improved performance of JAIDocuImage for large images
robcast
parents:
464
diff
changeset
|
265 // + Float.toString(param.getCompressionQuality())); |
352 | 266 } |
267 writer.setOutput(imgout); | |
268 // render output | |
269 logger.debug("writing"); | |
270 writer.write(null, new IIOImage(img, null, null), param); | |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
271 } else if (mt == "image/png") { |
352 | 272 // render output |
273 writer = (ImageWriter) ImageIO.getImageWritersByFormatName( | |
274 "png").next(); | |
275 if (writer == null) { | |
570 | 276 throw new ImageOpException("Unable to get PNG writer"); |
352 | 277 } |
278 writer.setOutput(imgout); | |
279 logger.debug("writing"); | |
280 writer.write(img); | |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
281 } else { |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
282 // unknown mime type |
570 | 283 throw new ImageOpException("Unknown mime type: " + mt); |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
284 } |
140 | 285 |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
286 } catch (IOException e) { |
570 | 287 logger.error("Error writing image:", e); |
288 throw new ServletException("Error writing image:", e); | |
353 | 289 } finally { |
290 // clean up | |
291 if (writer != null) { | |
292 writer.dispose(); | |
293 } | |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
294 } |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
295 } |
1 | 296 |
149 | 297 public void scale(double scale, double scaleY) throws ImageOpException { |
207 | 298 logger.debug("scale"); |
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
299 /* for downscaling in high quality the image is blurred first */ |
144 | 300 if ((scale <= 0.5) && (quality > 1)) { |
301 int bl = (int) Math.floor(1 / scale); | |
302 blur(bl); | |
303 } | |
207 | 304 /* then scaled */ |
279 | 305 AffineTransformOp scaleOp = new AffineTransformOp(AffineTransform |
306 .getScaleInstance(scale, scale), renderHint); | |
144 | 307 BufferedImage scaledImg = null; |
565 | 308 /* enforce destination image type (*Java2D BUG*) |
220 | 309 int type = img.getType(); |
279 | 310 if ((quality > 0) && (type != 0)) { |
207 | 311 logger.debug("creating destination image"); |
144 | 312 Rectangle2D dstBounds = scaleOp.getBounds2D(img); |
279 | 313 scaledImg = new BufferedImage((int) dstBounds.getWidth(), |
314 (int) dstBounds.getHeight(), type); | |
565 | 315 } */ |
207 | 316 logger.debug("scaling..."); |
144 | 317 scaledImg = scaleOp.filter(img, scaledImg); |
207 | 318 if (scaledImg == null) { |
319 throw new ImageOpException("Unable to scale"); | |
320 } | |
352 | 321 // DEBUG |
469
11e11fe4d680
Improved performance of JAIDocuImage for large images
robcast
parents:
464
diff
changeset
|
322 logger.debug("destination image type " + scaledImg.getType()); |
279 | 323 logger.debug("SCALE: " + scale + " ->" + scaledImg.getWidth() + "x" |
144 | 324 + scaledImg.getHeight()); |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
325 img = scaledImg; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
326 } |
1 | 327 |
144 | 328 public void blur(int radius) throws ImageOpException { |
352 | 329 // DEBUG |
181 | 330 logger.debug("blur: " + radius); |
144 | 331 // minimum radius is 2 |
332 int klen = Math.max(radius, 2); | |
220 | 333 // FIXME: use constant kernels for most common sizes |
144 | 334 int ksize = klen * klen; |
335 // kernel is constant 1/k | |
336 float f = 1f / ksize; | |
337 float[] kern = new float[ksize]; | |
338 for (int i = 0; i < ksize; i++) { | |
339 kern[i] = f; | |
340 } | |
341 Kernel blur = new Kernel(klen, klen, kern); | |
342 // blur with convolve operation | |
279 | 343 ConvolveOp blurOp = new ConvolveOp(blur, ConvolveOp.EDGE_NO_OP, |
344 renderHint); | |
565 | 345 BufferedImage blurredImg = null; |
145 | 346 // blur needs explicit destination image type for color *Java2D BUG* |
347 if (img.getType() == BufferedImage.TYPE_3BYTE_BGR) { | |
565 | 348 logger.debug("blur: fixing destination image type"); |
279 | 349 blurredImg = new BufferedImage(img.getWidth(), img.getHeight(), img |
350 .getType()); | |
145 | 351 } |
352 blurredImg = blurOp.filter(img, blurredImg); | |
144 | 353 img = blurredImg; |
354 } | |
355 | |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
356 public void crop(int x_off, int y_off, int width, int height) |
279 | 357 throws ImageOpException { |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
358 // setup Crop |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
359 BufferedImage croppedImg = img.getSubimage(x_off, y_off, width, height); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
360 if (croppedImg == null) { |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
361 throw new ImageOpException("Unable to crop"); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
362 } |
469
11e11fe4d680
Improved performance of JAIDocuImage for large images
robcast
parents:
464
diff
changeset
|
363 logger.debug("CROP:" + croppedImg.getWidth() + "x" |
11e11fe4d680
Improved performance of JAIDocuImage for large images
robcast
parents:
464
diff
changeset
|
364 + croppedImg.getHeight()); |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
365 img = croppedImg; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
366 } |
1 | 367 |
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
368 public void enhance(float mult, float add) throws ImageOpException { |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
369 /* |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
370 * Only one constant should work regardless of the number of bands |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
371 * according to the JDK spec. Doesn't work on JDK 1.4 for OSX and Linux |
279 | 372 * (at least). RescaleOp scaleOp = new RescaleOp( (float)mult, |
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
373 * (float)add, null); scaleOp.filter(img, img); |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
374 */ |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
375 |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
376 /* The number of constants must match the number of bands in the image. */ |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
377 int ncol = img.getColorModel().getNumComponents(); |
86 | 378 float[] dm = new float[ncol]; |
379 float[] da = new float[ncol]; | |
380 for (int i = 0; i < ncol; i++) { | |
381 dm[i] = (float) mult; | |
382 da[i] = (float) add; | |
85 | 383 } |
86 | 384 RescaleOp scaleOp = new RescaleOp(dm, da, null); |
85 | 385 scaleOp.filter(img, img); |
386 } | |
387 | |
279 | 388 public void enhanceRGB(float[] rgbm, float[] rgba) throws ImageOpException { |
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
389 /* |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
390 * The number of constants must match the number of bands in the image. |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
391 * We do only 3 (RGB) bands. |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
392 */ |
86 | 393 int ncol = img.getColorModel().getNumColorComponents(); |
394 if ((ncol != 3) || (rgbm.length != 3) || (rgba.length != 3)) { | |
279 | 395 logger |
396 .debug("ERROR(enhance): unknown number of color bands or coefficients (" | |
397 + ncol + ")"); | |
86 | 398 return; |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
399 } |
279 | 400 RescaleOp scaleOp = new RescaleOp(rgbOrdered(rgbm), rgbOrdered(rgba), |
401 null); | |
86 | 402 scaleOp.filter(img, img); |
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
403 } |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
404 |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
405 /** |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
406 * Ensures that the array f is in the right order to map the images RGB |
279 | 407 * components. (not shure what happens |
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
408 */ |
86 | 409 public float[] rgbOrdered(float[] fa) { |
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
410 /* |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
411 * TODO: this is UGLY, UGLY!! |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
412 */ |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
413 float[] fb; |
86 | 414 int t = img.getType(); |
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
415 if (img.getColorModel().hasAlpha()) { |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
416 fb = new float[4]; |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
417 if ((t == BufferedImage.TYPE_INT_ARGB) |
279 | 418 || (t == BufferedImage.TYPE_INT_ARGB_PRE)) { |
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
419 // RGB Type |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
420 fb[0] = fa[0]; |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
421 fb[1] = fa[1]; |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
422 fb[2] = fa[2]; |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
423 fb[3] = 1f; |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
424 } else { |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
425 // this isn't tested :-( |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
426 fb[0] = 1f; |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
427 fb[1] = fa[0]; |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
428 fb[2] = fa[1]; |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
429 fb[3] = fa[2]; |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
430 } |
86 | 431 } else { |
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
432 fb = new float[3]; |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
433 if (t == BufferedImage.TYPE_3BYTE_BGR) { |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
434 // BGR Type (actually it looks like RBG...) |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
435 fb[0] = fa[0]; |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
436 fb[1] = fa[2]; |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
437 fb[2] = fa[1]; |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
438 } else { |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
439 fb[0] = fa[0]; |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
440 fb[1] = fa[1]; |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
441 fb[2] = fa[2]; |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
442 } |
86 | 443 } |
444 return fb; | |
85 | 445 } |
446 | |
140 | 447 public void rotate(double angle) throws ImageOpException { |
89 | 448 // setup rotation |
449 double rangle = Math.toRadians(angle); | |
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
450 // create offset to make shure the rotated image has no negative |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
451 // coordinates |
101 | 452 double w = img.getWidth(); |
453 double h = img.getHeight(); | |
103 | 454 AffineTransform trafo = new AffineTransform(); |
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
455 // center of rotation |
103 | 456 double x = (w / 2); |
457 double y = (h / 2); | |
101 | 458 trafo.rotate(rangle, x, y); |
103 | 459 // try rotation to see how far we're out of bounds |
207 | 460 AffineTransformOp rotOp = new AffineTransformOp(trafo, renderHint); |
103 | 461 Rectangle2D rotbounds = rotOp.getBounds2D(img); |
462 double xoff = rotbounds.getX(); | |
463 double yoff = rotbounds.getY(); | |
464 // move image back in line | |
565 | 465 trafo.preConcatenate(AffineTransform.getTranslateInstance(-xoff, -yoff)); |
101 | 466 // transform image |
207 | 467 rotOp = new AffineTransformOp(trafo, renderHint); |
89 | 468 BufferedImage rotImg = rotOp.filter(img, null); |
101 | 469 // calculate new bounding box |
352 | 470 // Rectangle2D bounds = rotOp.getBounds2D(img); |
103 | 471 img = rotImg; |
101 | 472 // crop new image (with self-made rounding) |
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
473 /* |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
474 * img = rotImg.getSubimage( (int) (bounds.getX()+0.5), (int) |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
475 * (bounds.getY()+0.5), (int) (bounds.getWidth()+0.5), (int) |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
476 * (bounds.getHeight()+0.5)); |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
477 */ |
86 | 478 } |
85 | 479 |
89 | 480 public void mirror(double angle) throws ImageOpException { |
481 // setup mirror | |
482 double mx = 1; | |
483 double my = 1; | |
484 double tx = 0; | |
485 double ty = 0; | |
144 | 486 if (Math.abs(angle - 0) < epsilon) { // 0 degree |
89 | 487 mx = -1; |
488 tx = getWidth(); | |
144 | 489 } else if (Math.abs(angle - 90) < epsilon) { // 90 degree |
89 | 490 my = -1; |
491 ty = getHeight(); | |
144 | 492 } else if (Math.abs(angle - 180) < epsilon) { // 180 degree |
89 | 493 mx = -1; |
494 tx = getWidth(); | |
144 | 495 } else if (Math.abs(angle - 270) < epsilon) { // 270 degree |
89 | 496 my = -1; |
497 ty = getHeight(); | |
144 | 498 } else if (Math.abs(angle - 360) < epsilon) { // 360 degree |
89 | 499 mx = -1; |
500 tx = getWidth(); | |
501 } | |
279 | 502 AffineTransformOp mirOp = new AffineTransformOp(new AffineTransform(mx, |
503 0, 0, my, tx, ty), renderHint); | |
89 | 504 BufferedImage mirImg = mirOp.filter(img, null); |
505 img = mirImg; | |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
506 } |
86 | 507 |
279 | 508 /* |
509 * (non-Javadoc) | |
510 * | |
511 * @see java.lang.Object#finalize() | |
512 */ | |
148 | 513 protected void finalize() throws Throwable { |
207 | 514 dispose(); |
515 super.finalize(); | |
516 } | |
517 | |
518 public void dispose() { | |
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
519 // we must dispose the ImageReader because it keeps the filehandle |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
520 // open! |
256 | 521 if (reader != null) { |
522 reader.dispose(); | |
523 reader = null; | |
524 } | |
148 | 525 img = null; |
526 } | |
527 | |
533 | 528 public Image getAwtImage(){ |
496 | 529 return (Image) img; |
530 } | |
531 | |
532 | |
1 | 533 } |