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