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