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