Mercurial > hg > digilib-old
annotate common/src/main/java/digilib/image/ImageLoaderDocuImage.java @ 925:66f1ba72d07b
added timeout-parameter and timeout-handler to AsyncServletWorker.
added stopNow abort method to ImageWorker.
| author | robcast |
|---|---|
| date | Mon, 19 Dec 2011 21:39:17 +0100 |
| parents | cbfc94584d3b |
| children | 776347921bec |
| rev | line source |
|---|---|
| 1 | 1 /* ImageLoaderDocuImage -- Image class implementation using JDK 1.4 ImageLoader |
| 2 | |
| 279 | 3 Digital Image Library servlet components |
| 1 | 4 |
|
819
a23c4c15a6a8
clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents:
801
diff
changeset
|
5 Copyright (C) 2002 - 2011 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; |
| 829 | 25 import java.awt.color.ColorSpace; |
| 73 | 26 import java.awt.geom.AffineTransform; |
| 101 | 27 import java.awt.geom.Rectangle2D; |
| 73 | 28 import java.awt.image.AffineTransformOp; |
| 860 | 29 import java.awt.image.BandCombineOp; |
| 73 | 30 import java.awt.image.BufferedImage; |
|
834
c07c21ac78f9
color invert operation. doesn't work with all image types yet...
robcast
parents:
831
diff
changeset
|
31 import java.awt.image.ByteLookupTable; |
| 829 | 32 import java.awt.image.ColorConvertOp; |
| 838 | 33 import java.awt.image.ColorModel; |
| 144 | 34 import java.awt.image.ConvolveOp; |
| 864 | 35 import java.awt.image.IndexColorModel; |
| 144 | 36 import java.awt.image.Kernel; |
|
834
c07c21ac78f9
color invert operation. doesn't work with all image types yet...
robcast
parents:
831
diff
changeset
|
37 import java.awt.image.LookupOp; |
|
c07c21ac78f9
color invert operation. doesn't work with all image types yet...
robcast
parents:
831
diff
changeset
|
38 import java.awt.image.LookupTable; |
| 85 | 39 import java.awt.image.RescaleOp; |
| 73 | 40 import java.io.IOException; |
|
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
41 import java.io.OutputStream; |
| 89 | 42 import java.io.RandomAccessFile; |
| 464 | 43 import java.util.Arrays; |
| 85 | 44 import java.util.Iterator; |
| 1 | 45 |
| 352 | 46 import javax.imageio.IIOImage; |
| 73 | 47 import javax.imageio.ImageIO; |
| 85 | 48 import javax.imageio.ImageReadParam; |
| 49 import javax.imageio.ImageReader; | |
| 352 | 50 import javax.imageio.ImageWriteParam; |
| 51 import javax.imageio.ImageWriter; | |
| 220 | 52 import javax.imageio.stream.FileImageInputStream; |
| 85 | 53 import javax.imageio.stream.ImageInputStream; |
| 352 | 54 import javax.imageio.stream.ImageOutputStream; |
| 1 | 55 |
| 207 | 56 import digilib.io.FileOpException; |
| 464 | 57 import digilib.io.FileOps; |
|
579
efd7a223f819
try: ImageInput as interface, ImageFile inherits from Dirent and implements ImageInput
robcast
parents:
574
diff
changeset
|
58 import digilib.io.ImageInput; |
| 596 | 59 import digilib.util.ImageSize; |
| 1 | 60 |
| 73 | 61 /** Implementation of DocuImage using the ImageLoader API of Java 1.4 and Java2D. */ |
| 564 | 62 public class ImageLoaderDocuImage extends ImageInfoDocuImage { |
| 590 | 63 |
| 86 | 64 /** image object */ |
| 65 protected BufferedImage img; | |
| 570 | 66 |
| 906 | 67 /** the reader object */ |
| 68 protected ImageReader reader = null; | |
| 69 | |
| 70 /** try to reuse reader object */ | |
| 71 public boolean reuseReader = false; | |
| 72 | |
| 86 | 73 /** interpolation type */ |
| 838 | 74 protected RenderingHints renderHint = null; |
| 279 | 75 |
|
834
c07c21ac78f9
color invert operation. doesn't work with all image types yet...
robcast
parents:
831
diff
changeset
|
76 /** convolution kernels for blur() */ |
| 590 | 77 protected static Kernel[] convolutionKernels = { |
| 78 null, | |
| 79 new Kernel(1, 1, new float[] {1f}), | |
| 80 new Kernel(2, 2, new float[] {0.25f, 0.25f, 0.25f, 0.25f}), | |
| 81 new Kernel(3, 3, new float[] {1f/9f, 1f/9f, 1f/9f, 1f/9f, 1f/9f, 1f/9f, 1f/9f, 1f/9f, 1f/9f}) | |
| 82 }; | |
| 279 | 83 |
| 854 | 84 /* lookup tables for inverting images (byte) */ |
|
841
3519cfa11a0b
organised special juju for invert on linux jdk 1.6.
robcast
parents:
839
diff
changeset
|
85 protected static LookupTable invertSingleByteTable; |
|
3519cfa11a0b
organised special juju for invert on linux jdk 1.6.
robcast
parents:
839
diff
changeset
|
86 protected static LookupTable invertRgbaByteTable; |
| 854 | 87 protected static boolean needsInvertRgba = false; |
| 860 | 88 /* RescaleOp for contrast/brightness operation */ |
| 854 | 89 protected static boolean needsRescaleRgba = false; |
| 865 | 90 /* lookup table for false-color */ |
| 860 | 91 protected static LookupTable mapBgrByteTable; |
| 865 | 92 protected static boolean needsMapBgr = false; |
| 854 | 93 |
|
834
c07c21ac78f9
color invert operation. doesn't work with all image types yet...
robcast
parents:
831
diff
changeset
|
94 static { |
| 864 | 95 /* |
| 96 * create static lookup tables | |
| 97 */ | |
|
834
c07c21ac78f9
color invert operation. doesn't work with all image types yet...
robcast
parents:
831
diff
changeset
|
98 byte[] invertByte = new byte[256]; |
|
c07c21ac78f9
color invert operation. doesn't work with all image types yet...
robcast
parents:
831
diff
changeset
|
99 byte[] orderedByte = new byte[256]; |
|
841
3519cfa11a0b
organised special juju for invert on linux jdk 1.6.
robcast
parents:
839
diff
changeset
|
100 byte[] nullByte = new byte[256]; |
| 860 | 101 byte[] mapR = new byte[256]; |
| 102 byte[] mapG = new byte[256]; | |
| 103 byte[] mapB = new byte[256]; | |
|
834
c07c21ac78f9
color invert operation. doesn't work with all image types yet...
robcast
parents:
831
diff
changeset
|
104 for (int i = 0; i < 256; ++i) { |
| 860 | 105 // counting down |
|
834
c07c21ac78f9
color invert operation. doesn't work with all image types yet...
robcast
parents:
831
diff
changeset
|
106 invertByte[i] = (byte) (256 - i); |
| 860 | 107 // counting up |
|
834
c07c21ac78f9
color invert operation. doesn't work with all image types yet...
robcast
parents:
831
diff
changeset
|
108 orderedByte[i] = (byte) i; |
| 860 | 109 // constant 0 |
|
841
3519cfa11a0b
organised special juju for invert on linux jdk 1.6.
robcast
parents:
839
diff
changeset
|
110 nullByte[i] = 0; |
| 860 | 111 // three overlapping slopes |
| 112 if (i < 64) { | |
| 113 mapR[i] = 0; | |
| 114 mapG[i] = (byte) (4 * i); | |
| 115 mapB[i] = (byte) 255; | |
| 116 } else if (i >= 64 && i < 192) { | |
| 117 mapR[i] = (byte) (2 * (i - 64)); | |
| 118 mapG[i] = (byte) 255; | |
| 119 mapB[i] = (byte) (255 - 2 * (i - 64)); | |
| 120 } else { | |
| 121 mapR[i] = (byte) 255; | |
| 861 | 122 mapG[i] = (byte) (255 - (4 * (i - 192))); |
| 860 | 123 mapB[i] = 0; |
| 124 } | |
|
834
c07c21ac78f9
color invert operation. doesn't work with all image types yet...
robcast
parents:
831
diff
changeset
|
125 } |
|
841
3519cfa11a0b
organised special juju for invert on linux jdk 1.6.
robcast
parents:
839
diff
changeset
|
126 // should(!) work for all color models |
|
3519cfa11a0b
organised special juju for invert on linux jdk 1.6.
robcast
parents:
839
diff
changeset
|
127 invertSingleByteTable = new ByteLookupTable(0, invertByte); |
|
3519cfa11a0b
organised special juju for invert on linux jdk 1.6.
robcast
parents:
839
diff
changeset
|
128 // but doesn't work with alpha channel on all platforms |
|
3519cfa11a0b
organised special juju for invert on linux jdk 1.6.
robcast
parents:
839
diff
changeset
|
129 String ver = System.getProperty("java.version"); |
|
3519cfa11a0b
organised special juju for invert on linux jdk 1.6.
robcast
parents:
839
diff
changeset
|
130 String os = System.getProperty("os.name"); |
|
3519cfa11a0b
organised special juju for invert on linux jdk 1.6.
robcast
parents:
839
diff
changeset
|
131 logger.debug("os="+os+" ver="+ver); |
|
3519cfa11a0b
organised special juju for invert on linux jdk 1.6.
robcast
parents:
839
diff
changeset
|
132 if (os.startsWith("Linux") && ver.startsWith("1.6")) { |
|
3519cfa11a0b
organised special juju for invert on linux jdk 1.6.
robcast
parents:
839
diff
changeset
|
133 // GRAB(WTF?) works in Linux JDK1.6 with transparency |
|
3519cfa11a0b
organised special juju for invert on linux jdk 1.6.
robcast
parents:
839
diff
changeset
|
134 invertRgbaByteTable = new ByteLookupTable(0, new byte[][] { |
|
3519cfa11a0b
organised special juju for invert on linux jdk 1.6.
robcast
parents:
839
diff
changeset
|
135 invertByte, invertByte, orderedByte, invertByte}); |
| 854 | 136 needsInvertRgba = true; |
| 137 needsRescaleRgba = true; | |
| 865 | 138 needsMapBgr = true; |
|
841
3519cfa11a0b
organised special juju for invert on linux jdk 1.6.
robcast
parents:
839
diff
changeset
|
139 } else { |
|
3519cfa11a0b
organised special juju for invert on linux jdk 1.6.
robcast
parents:
839
diff
changeset
|
140 invertRgbaByteTable = invertSingleByteTable; |
|
3519cfa11a0b
organised special juju for invert on linux jdk 1.6.
robcast
parents:
839
diff
changeset
|
141 } |
| 860 | 142 // this hopefully works for all |
| 865 | 143 mapBgrByteTable = new ByteLookupTable(0, new byte[][] { |
| 860 | 144 mapR, mapG, mapB}); |
|
834
c07c21ac78f9
color invert operation. doesn't work with all image types yet...
robcast
parents:
831
diff
changeset
|
145 } |
|
c07c21ac78f9
color invert operation. doesn't work with all image types yet...
robcast
parents:
831
diff
changeset
|
146 |
|
819
a23c4c15a6a8
clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents:
801
diff
changeset
|
147 /** the size of the current image */ |
|
a23c4c15a6a8
clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents:
801
diff
changeset
|
148 protected ImageSize imageSize; |
| 590 | 149 |
| 150 | |
| 85 | 151 /* loadSubimage is supported. */ |
| 152 public boolean isSubimageSupported() { | |
| 153 return true; | |
|
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
154 } |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
155 |
| 85 | 156 public void setQuality(int qual) { |
| 157 quality = qual; | |
| 207 | 158 renderHint = new RenderingHints(null); |
| 352 | 159 // hint.put(RenderingHints.KEY_ANTIALIASING, |
| 279 | 160 // RenderingHints.VALUE_ANTIALIAS_OFF); |
| 85 | 161 // setup interpolation quality |
| 162 if (qual > 0) { | |
| 181 | 163 logger.debug("quality q1"); |
| 279 | 164 renderHint.put(RenderingHints.KEY_INTERPOLATION, |
| 165 RenderingHints.VALUE_INTERPOLATION_BICUBIC); | |
| 85 | 166 } else { |
| 181 | 167 logger.debug("quality q0"); |
| 279 | 168 renderHint.put(RenderingHints.KEY_INTERPOLATION, |
| 169 RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR); | |
| 85 | 170 } |
| 171 } | |
| 86 | 172 |
| 570 | 173 /* returns the size of the current image */ |
| 174 public ImageSize getSize() { | |
|
819
a23c4c15a6a8
clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents:
801
diff
changeset
|
175 if (imageSize == null) { |
|
a23c4c15a6a8
clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents:
801
diff
changeset
|
176 int h = 0; |
|
a23c4c15a6a8
clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents:
801
diff
changeset
|
177 int w = 0; |
|
a23c4c15a6a8
clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents:
801
diff
changeset
|
178 try { |
|
a23c4c15a6a8
clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents:
801
diff
changeset
|
179 if (img == null) { |
| 906 | 180 reader = getReader(input); |
|
819
a23c4c15a6a8
clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents:
801
diff
changeset
|
181 // get size from ImageReader |
|
a23c4c15a6a8
clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents:
801
diff
changeset
|
182 h = reader.getHeight(0); |
|
a23c4c15a6a8
clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents:
801
diff
changeset
|
183 w = reader.getWidth(0); |
|
a23c4c15a6a8
clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents:
801
diff
changeset
|
184 } else { |
|
a23c4c15a6a8
clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents:
801
diff
changeset
|
185 // get size from image |
|
a23c4c15a6a8
clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents:
801
diff
changeset
|
186 h = img.getHeight(); |
|
a23c4c15a6a8
clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents:
801
diff
changeset
|
187 w = img.getWidth(); |
|
a23c4c15a6a8
clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents:
801
diff
changeset
|
188 } |
|
a23c4c15a6a8
clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents:
801
diff
changeset
|
189 imageSize = new ImageSize(w, h); |
|
a23c4c15a6a8
clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents:
801
diff
changeset
|
190 } catch (IOException e) { |
|
a23c4c15a6a8
clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents:
801
diff
changeset
|
191 logger.debug("error in getSize:", e); |
| 570 | 192 } |
| 193 } | |
|
819
a23c4c15a6a8
clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents:
801
diff
changeset
|
194 return imageSize; |
| 570 | 195 } |
| 1 | 196 |
| 464 | 197 /* returns a list of supported image formats */ |
| 531 | 198 public Iterator<String> getSupportedFormats() { |
| 464 | 199 String[] formats = ImageIO.getReaderFormatNames(); |
| 200 return Arrays.asList(formats).iterator(); | |
| 201 } | |
| 202 | |
| 588 | 203 /* Check image size and type and store in ImageInput */ |
| 204 public ImageInput identify(ImageInput input) throws IOException { | |
| 906 | 205 ImageInput ii = null; |
| 206 if (!reuseReader) { | |
| 207 // try parent method first | |
| 208 ii = super.identify(input); | |
| 209 if (ii != null) { | |
| 210 return ii; | |
| 211 } | |
|
462
03ff7238c9d4
second try for flashpix support (doesn't work currently...)
robcast
parents:
402
diff
changeset
|
212 } |
| 588 | 213 logger.debug("identifying (ImageIO) " + input); |
| 590 | 214 try { |
|
819
a23c4c15a6a8
clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents:
801
diff
changeset
|
215 /* |
|
a23c4c15a6a8
clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents:
801
diff
changeset
|
216 * try ImageReader |
|
a23c4c15a6a8
clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents:
801
diff
changeset
|
217 */ |
| 590 | 218 reader = getReader(input); |
|
819
a23c4c15a6a8
clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents:
801
diff
changeset
|
219 // set size |
|
a23c4c15a6a8
clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents:
801
diff
changeset
|
220 ImageSize d = new ImageSize(reader.getWidth(0), reader.getHeight(0)); |
|
a23c4c15a6a8
clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents:
801
diff
changeset
|
221 input.setSize(d); |
|
a23c4c15a6a8
clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents:
801
diff
changeset
|
222 // set mime type |
|
a23c4c15a6a8
clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents:
801
diff
changeset
|
223 if (input.getMimetype() == null) { |
|
a23c4c15a6a8
clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents:
801
diff
changeset
|
224 if (input.hasFile()) { |
|
a23c4c15a6a8
clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents:
801
diff
changeset
|
225 String t = FileOps.mimeForFile(input.getFile()); |
|
a23c4c15a6a8
clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents:
801
diff
changeset
|
226 input.setMimetype(t); |
|
a23c4c15a6a8
clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents:
801
diff
changeset
|
227 } else { |
|
a23c4c15a6a8
clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents:
801
diff
changeset
|
228 // FIXME: is format name a mime type??? |
|
a23c4c15a6a8
clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents:
801
diff
changeset
|
229 String t = reader.getFormatName(); |
|
a23c4c15a6a8
clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents:
801
diff
changeset
|
230 input.setMimetype(t); |
|
a23c4c15a6a8
clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents:
801
diff
changeset
|
231 } |
|
a23c4c15a6a8
clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents:
801
diff
changeset
|
232 } |
|
a23c4c15a6a8
clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents:
801
diff
changeset
|
233 return input; |
| 590 | 234 } catch (FileOpException e) { |
| 235 // maybe just our class doesn't know what to do | |
|
819
a23c4c15a6a8
clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents:
801
diff
changeset
|
236 logger.error("ImageLoaderDocuimage unable to identify:", e); |
| 590 | 237 return null; |
|
819
a23c4c15a6a8
clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents:
801
diff
changeset
|
238 } finally { |
| 906 | 239 if (!reuseReader && reader != null) { |
|
819
a23c4c15a6a8
clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents:
801
diff
changeset
|
240 reader.dispose(); |
| 588 | 241 } |
|
462
03ff7238c9d4
second try for flashpix support (doesn't work currently...)
robcast
parents:
402
diff
changeset
|
242 } |
|
03ff7238c9d4
second try for flashpix support (doesn't work currently...)
robcast
parents:
402
diff
changeset
|
243 } |
|
03ff7238c9d4
second try for flashpix support (doesn't work currently...)
robcast
parents:
402
diff
changeset
|
244 |
|
03ff7238c9d4
second try for flashpix support (doesn't work currently...)
robcast
parents:
402
diff
changeset
|
245 /* load image file */ |
| 588 | 246 public void loadImage(ImageInput ii) throws FileOpException { |
| 247 logger.debug("loadImage: " + ii); | |
| 248 this.input = ii; | |
|
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
249 try { |
| 588 | 250 if (ii.hasImageInputStream()) { |
| 251 img = ImageIO.read(ii.getImageInputStream()); | |
| 252 } else if (ii.hasFile()) { | |
| 253 img = ImageIO.read(ii.getFile()); | |
| 254 } | |
|
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
255 } catch (IOException e) { |
| 906 | 256 throw new FileOpException("Error reading image!", e); |
| 257 } | |
| 258 if (img == null) { | |
| 259 throw new FileOpException("Unable to read image!"); | |
|
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
260 } |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
261 } |
| 1 | 262 |
| 279 | 263 /** |
| 264 * Get an ImageReader for the image file. | |
| 265 * | |
| 266 * @return | |
| 267 */ | |
| 587 | 268 public ImageReader getReader(ImageInput input) throws IOException { |
| 269 logger.debug("get ImageReader for " + input); | |
| 906 | 270 if (reuseReader && reader != null) { |
| 271 logger.debug("reuseing ImageReader"); | |
| 272 return reader; | |
| 273 } | |
| 587 | 274 ImageInputStream istream = null; |
| 275 if (input.hasImageInputStream()) { | |
| 906 | 276 // ImageInputStream input |
| 587 | 277 istream = input.getImageInputStream(); |
| 278 } else if (input.hasFile()) { | |
| 279 // file only input | |
| 280 RandomAccessFile rf = new RandomAccessFile(input.getFile(), "r"); | |
| 281 istream = new FileImageInputStream(rf); | |
| 282 } else { | |
| 283 throw new FileOpException("Unable to get data from ImageInput"); | |
| 148 | 284 } |
| 565 | 285 Iterator<ImageReader> readers; |
|
819
a23c4c15a6a8
clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents:
801
diff
changeset
|
286 String mt = null; |
|
a23c4c15a6a8
clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents:
801
diff
changeset
|
287 if (input.hasMimetype()) { |
|
a23c4c15a6a8
clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents:
801
diff
changeset
|
288 // check hasMimetype first or we might get into a loop |
|
a23c4c15a6a8
clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents:
801
diff
changeset
|
289 mt = input.getMimetype(); |
| 821 | 290 } else { |
| 291 // try file extension | |
| 292 mt = FileOps.mimeForFile(input.getFile()); | |
|
819
a23c4c15a6a8
clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents:
801
diff
changeset
|
293 } |
| 565 | 294 if (mt == null) { |
| 295 logger.debug("No mime-type. Trying automagic."); | |
| 296 readers = ImageIO.getImageReaders(istream); | |
| 297 } else { | |
| 298 logger.debug("File type:" + mt); | |
| 299 readers = ImageIO.getImageReadersByMIMEType(mt); | |
| 300 } | |
| 279 | 301 if (!readers.hasNext()) { |
| 565 | 302 throw new FileOpException("Can't find Reader to load File!"); |
| 279 | 303 } |
|
819
a23c4c15a6a8
clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents:
801
diff
changeset
|
304 ImageReader reader = readers.next(); |
| 149 | 305 /* are there more readers? */ |
| 207 | 306 logger.debug("ImageIO: this reader: " + reader.getClass()); |
| 564 | 307 /* while (readers.hasNext()) { |
| 207 | 308 logger.debug("ImageIO: next reader: " + readers.next().getClass()); |
| 564 | 309 } */ |
| 89 | 310 reader.setInput(istream); |
| 279 | 311 return reader; |
| 85 | 312 } |
| 313 | |
| 314 /* Load an image file into the Object. */ | |
| 588 | 315 public void loadSubimage(ImageInput ii, Rectangle region, int prescale) |
| 279 | 316 throws FileOpException { |
| 207 | 317 logger.debug("loadSubimage"); |
|
819
a23c4c15a6a8
clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents:
801
diff
changeset
|
318 this.input = ii; |
| 906 | 319 //ImageReader reader = null; |
| 85 | 320 try { |
| 906 | 321 reader = getReader(ii); |
| 85 | 322 // set up reader parameters |
| 323 ImageReadParam readParam = reader.getDefaultReadParam(); | |
| 324 readParam.setSourceRegion(region); | |
| 220 | 325 if (prescale > 1) { |
| 326 readParam.setSourceSubsampling(prescale, prescale, 0, 0); | |
| 327 } | |
| 85 | 328 // read image |
| 207 | 329 logger.debug("loading.."); |
| 85 | 330 img = reader.read(0, readParam); |
| 207 | 331 logger.debug("loaded"); |
| 912 | 332 // invalidate image size |
| 333 imageSize = null; | |
| 865 | 334 /* downconversion of highcolor images seems not to work |
| 335 if (img.getColorModel().getComponentSize(0) > 8) { | |
| 336 logger.debug("converting to 8bit"); | |
| 337 BufferedImage dest = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_3BYTE_BGR); | |
| 338 dest.createGraphics().drawImage(img, null, 0, 0); | |
| 339 img = dest; | |
| 340 } */ | |
| 85 | 341 } catch (IOException e) { |
| 906 | 342 throw new FileOpException("Unable to load File!", e); |
|
819
a23c4c15a6a8
clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents:
801
diff
changeset
|
343 } finally { |
| 906 | 344 if (!reuseReader && reader != null) { |
|
819
a23c4c15a6a8
clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents:
801
diff
changeset
|
345 reader.dispose(); |
|
a23c4c15a6a8
clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents:
801
diff
changeset
|
346 } |
| 85 | 347 } |
| 348 } | |
|
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
349 |
| 86 | 350 /* write image of type mt to Stream */ |
|
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
351 public void writeImage(String mt, OutputStream ostream) |
| 906 | 352 throws ImageOpException, FileOpException { |
| 207 | 353 logger.debug("writeImage"); |
| 353 | 354 // setup output |
| 355 ImageWriter writer = null; | |
| 356 ImageOutputStream imgout = null; | |
|
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
357 try { |
| 353 | 358 imgout = ImageIO.createImageOutputStream(ostream); |
|
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
359 if (mt == "image/jpeg") { |
| 352 | 360 /* |
| 361 * JPEG doesn't do transparency so we have to convert any RGBA | |
| 864 | 362 * image to RGB or we the client will think its CMYK :-( *Java2D BUG* |
| 352 | 363 */ |
| 364 if (img.getColorModel().hasAlpha()) { | |
| 365 logger.debug("BARF: JPEG with transparency!!"); | |
| 864 | 366 BufferedImage rgbImg = new BufferedImage(img.getWidth(), |
| 367 img.getHeight(), BufferedImage.TYPE_INT_RGB); | |
| 368 rgbImg.createGraphics().drawImage(img, null, 0, 0); | |
| 369 img = rgbImg; | |
| 352 | 370 } |
| 864 | 371 writer = ImageIO.getImageWritersByFormatName("jpeg").next(); |
| 352 | 372 if (writer == null) { |
| 570 | 373 throw new ImageOpException("Unable to get JPEG writer"); |
| 352 | 374 } |
| 375 ImageWriteParam param = writer.getDefaultWriteParam(); | |
| 376 if (quality > 1) { | |
| 377 // change JPEG compression quality | |
| 353 | 378 param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); |
| 352 | 379 param.setCompressionQuality(0.9f); |
| 380 } | |
| 381 writer.setOutput(imgout); | |
| 382 // render output | |
| 864 | 383 logger.debug("writing JPEG"); |
| 352 | 384 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
|
385 } else if (mt == "image/png") { |
| 352 | 386 // render output |
| 864 | 387 writer = ImageIO.getImageWritersByFormatName("png").next(); |
| 352 | 388 if (writer == null) { |
| 570 | 389 throw new ImageOpException("Unable to get PNG writer"); |
| 352 | 390 } |
| 391 writer.setOutput(imgout); | |
| 864 | 392 logger.debug("writing PNG"); |
| 352 | 393 writer.write(img); |
|
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
394 } else { |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
395 // unknown mime type |
| 570 | 396 throw new ImageOpException("Unknown mime type: " + mt); |
|
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
397 } |
| 140 | 398 |
|
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
399 } catch (IOException e) { |
| 570 | 400 logger.error("Error writing image:", e); |
| 906 | 401 throw new FileOpException("Error writing image!", e); |
|
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
402 } |
|
801
72662bb585ba
remove all ServletOutputStream.flush(). (stupid me ;-)
robcast
parents:
596
diff
changeset
|
403 // TODO: should we: finally { writer.dispose(); } |
|
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
404 } |
| 1 | 405 |
| 864 | 406 public void scale(double scaleX, double scaleY) throws ImageOpException { |
| 407 logger.debug("scale: " + scaleX); | |
| 408 /* for downscaling in high quality the image is blurred first */ | |
| 409 if ((scaleX <= 0.5) && (quality > 1)) { | |
| 410 int bl = (int) Math.floor(1 / scaleX); | |
| 411 blur(bl); | |
| 412 } | |
| 413 /* then scaled */ | |
| 414 AffineTransformOp scaleOp = new AffineTransformOp( | |
| 415 AffineTransform.getScaleInstance(scaleX, scaleY), renderHint); | |
| 416 img = scaleOp.filter(img, null); | |
| 417 logger.debug("scaled to " + img.getWidth() + "x" + img.getHeight() | |
| 418 + " img=" + img); | |
| 912 | 419 // invalidate image size |
| 420 imageSize = null; | |
| 864 | 421 } |
| 1 | 422 |
| 144 | 423 public void blur(int radius) throws ImageOpException { |
| 181 | 424 logger.debug("blur: " + radius); |
| 144 | 425 // minimum radius is 2 |
| 426 int klen = Math.max(radius, 2); | |
| 590 | 427 Kernel blur = null; |
| 428 if (klen < convolutionKernels.length) { | |
| 863 | 429 // use precalculated Kernel |
| 590 | 430 blur = convolutionKernels[klen]; |
| 431 } else { | |
| 432 // calculate our own kernel | |
| 433 int ksize = klen * klen; | |
| 434 // kernel is constant 1/k | |
| 435 float f = 1f / ksize; | |
| 436 float[] kern = new float[ksize]; | |
| 437 for (int i = 0; i < ksize; ++i) { | |
| 438 kern[i] = f; | |
| 439 } | |
| 440 blur = new Kernel(klen, klen, kern); | |
| 144 | 441 } |
| 442 // blur with convolve operation | |
| 279 | 443 ConvolveOp blurOp = new ConvolveOp(blur, ConvolveOp.EDGE_NO_OP, |
| 444 renderHint); | |
| 863 | 445 BufferedImage dest = null; |
| 864 | 446 // blur needs explicit destination image type for 3BYTE_BGR *Java2D BUG* |
| 145 | 447 if (img.getType() == BufferedImage.TYPE_3BYTE_BGR) { |
| 864 | 448 logger.debug("blur: fixing destination image type"); |
| 449 dest = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_3BYTE_BGR); | |
| 863 | 450 } |
| 451 img = blurOp.filter(img, dest); | |
| 452 logger.debug("blurred: "+img); | |
| 144 | 453 } |
| 454 | |
|
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
455 public void crop(int x_off, int y_off, int width, int height) |
| 279 | 456 throws ImageOpException { |
|
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
457 // setup Crop |
| 857 | 458 img = img.getSubimage(x_off, y_off, width, height); |
| 459 logger.debug("CROP:" + img.getWidth() + "x" | |
| 460 + img.getHeight()); | |
| 912 | 461 // invalidate image size |
| 462 imageSize = null; | |
|
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
463 } |
| 1 | 464 |
| 864 | 465 public void rotate(double angle) throws ImageOpException { |
| 466 logger.debug("rotate: " + angle); | |
| 467 // setup rotation | |
| 468 double rangle = Math.toRadians(angle); | |
| 469 // center of rotation is center of image | |
| 470 double w = img.getWidth(); | |
| 471 double h = img.getHeight(); | |
| 472 double x = (w / 2); | |
| 473 double y = (h / 2); | |
| 474 AffineTransform trafo = AffineTransform.getRotateInstance(rangle, x, y); | |
| 475 AffineTransformOp rotOp = new AffineTransformOp(trafo, renderHint); | |
| 476 // rotate bounds to see how much of the image would be off screen | |
| 477 Rectangle2D rotbounds = rotOp.getBounds2D(img); | |
| 478 double xoff = rotbounds.getX(); | |
| 479 double yoff = rotbounds.getY(); | |
| 480 if (Math.abs(xoff) > epsilon || Math.abs(yoff) > epsilon) { | |
| 481 // move image back on screen | |
| 482 logger.debug("move rotation: xoff="+xoff+" yoff="+yoff); | |
| 483 trafo.preConcatenate(AffineTransform.getTranslateInstance(-xoff, -yoff)); | |
| 484 rotOp = new AffineTransformOp(trafo, renderHint); | |
| 485 } | |
| 486 // transform image | |
| 487 img = rotOp.filter(img, null); | |
| 488 logger.debug("rotated: "+img); | |
| 912 | 489 // invalidate image size |
| 490 imageSize = null; | |
| 864 | 491 } |
| 492 | |
| 493 public void mirror(double angle) throws ImageOpException { | |
| 494 logger.debug("mirror: " + angle); | |
| 495 // setup mirror | |
| 496 double mx = 1; | |
| 497 double my = 1; | |
| 498 double tx = 0; | |
| 499 double ty = 0; | |
| 500 if (Math.abs(angle - 0) < epsilon) { // 0 degree | |
| 501 mx = -1; | |
| 502 tx = img.getWidth(); | |
| 503 } else if (Math.abs(angle - 90) < epsilon) { // 90 degree | |
| 504 my = -1; | |
| 505 ty = img.getHeight(); | |
| 506 } else if (Math.abs(angle - 180) < epsilon) { // 180 degree | |
| 507 mx = -1; | |
| 508 tx = img.getWidth(); | |
| 509 } else if (Math.abs(angle - 270) < epsilon) { // 270 degree | |
| 510 my = -1; | |
| 511 ty = img.getHeight(); | |
| 512 } else if (Math.abs(angle - 360) < epsilon) { // 360 degree | |
| 513 mx = -1; | |
| 514 tx = img.getWidth(); | |
| 515 } else { | |
| 516 logger.error("invalid mirror angle "+angle); | |
| 517 return; | |
| 518 } | |
| 519 AffineTransformOp mirOp = new AffineTransformOp(new AffineTransform(mx, | |
| 520 0, 0, my, tx, ty), renderHint); | |
| 521 img = mirOp.filter(img, null); | |
| 912 | 522 // invalidate image size |
| 523 imageSize = null; | |
| 864 | 524 } |
| 525 | |
| 526 public void enhance(float mult, float add) throws ImageOpException { | |
| 527 RescaleOp op = null; | |
| 528 logger.debug("enhance: img=" + img); | |
| 529 if (needsRescaleRgba) { | |
| 857 | 530 /* |
| 531 * Only one constant should work regardless of the number of bands | |
| 532 * according to the JDK spec. Doesn't work on JDK 1.4 for OSX and | |
| 533 * Linux (at least). | |
| 534 * | |
| 864 | 535 * The number of constants must match the number of bands in the |
| 536 * image. | |
| 537 */ | |
| 538 int ncol = img.getColorModel().getNumComponents(); | |
| 539 float[] dm = new float[ncol]; | |
| 540 float[] da = new float[ncol]; | |
| 541 for (int i = 0; i < ncol; i++) { | |
| 542 dm[i] = (float) mult; | |
| 543 da[i] = (float) add; | |
| 544 } | |
| 545 op = new RescaleOp(dm, da, null); | |
| 546 } else { | |
| 547 op = new RescaleOp(mult, add, renderHint); | |
| 548 } | |
| 549 op.filter(img, img); | |
| 550 } | |
|
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
551 |
| 864 | 552 public void enhanceRGB(float[] rgbm, float[] rgba) throws ImageOpException { |
| 553 logger.debug("enhanceRGB: rgbm="+rgbm+" rgba="+rgba); | |
| 554 /* | |
| 555 * The number of constants must match the number of bands in the image. | |
| 556 * We do only 3 (RGB) bands. | |
| 557 */ | |
| 558 int ncol = img.getColorModel().getNumColorComponents(); | |
| 559 if ((ncol != 3) || (rgbm.length != 3) || (rgba.length != 3)) { | |
| 560 logger.error("enhanceRGB: unknown number of color bands or coefficients (" | |
| 561 + ncol + ")"); | |
| 562 return; | |
| 563 } | |
| 564 if (img.getColorModel().hasAlpha()) { | |
| 565 // add constant for alpha | |
| 566 rgbm = new float[] {rgbm[0], rgbm[1], rgbm[2], 1}; | |
| 567 rgba = new float[] {rgba[0], rgba[1], rgba[2], 0}; | |
| 568 } | |
| 569 RescaleOp scaleOp = new RescaleOp(rgbm, rgba, renderHint); | |
| 570 scaleOp.filter(img, img); | |
| 571 } | |
| 85 | 572 |
| 860 | 573 /* |
| 574 * (non-Javadoc) | |
| 575 * | |
| 576 * @see | |
| 577 * digilib.image.DocuImageImpl#colorOp(digilib.image.DocuImage.ColorOps) | |
| 578 */ | |
| 579 public void colorOp(ColorOp colop) throws ImageOpException { | |
| 580 if (colop == ColorOp.GRAYSCALE) { | |
| 581 /* | |
| 582 * convert image to grayscale | |
| 583 */ | |
| 584 logger.debug("Color op: grayscaling"); | |
| 585 ColorModel cm = img.getColorModel(); | |
| 864 | 586 if (cm.getNumColorComponents() < 3) { |
| 860 | 587 // grayscale already |
| 864 | 588 logger.debug("Color op: not grayscaling"); |
| 860 | 589 return; |
| 590 } | |
| 591 ColorConvertOp op = new ColorConvertOp( | |
| 592 ColorSpace.getInstance(ColorSpace.CS_GRAY), renderHint); | |
| 593 // let filter create new image | |
| 594 img = op.filter(img, null); | |
| 595 } else if (colop == ColorOp.NTSC_GRAY) { | |
| 596 /* | |
| 597 * convert image to grayscale NTSC-style: luminance = 0.2989*red + | |
| 598 * 0.5870*green + 0.1140*blue | |
| 599 */ | |
| 600 logger.debug("Color op: NTSC gray"); | |
| 864 | 601 logger.debug("img="+img); |
| 860 | 602 ColorModel cm = img.getColorModel(); |
| 864 | 603 if (cm.getNumColorComponents() < 3 || cm instanceof IndexColorModel) { |
| 604 // grayscale already or not possible | |
| 605 logger.debug("Color op: unable to NTSC gray"); | |
| 860 | 606 return; |
| 607 } | |
| 608 float[][] combineFn = new float[1][4]; | |
| 864 | 609 combineFn[0] = new float[] { 0.299f, 0.587f, 0.114f, 0f }; |
| 860 | 610 BandCombineOp op = new BandCombineOp(combineFn, renderHint); |
| 863 | 611 // BandCombineOp only works on Rasters so we create a |
| 860 | 612 // new image and use its Raster |
| 613 BufferedImage dest = new BufferedImage(img.getWidth(), | |
| 614 img.getHeight(), BufferedImage.TYPE_BYTE_GRAY); | |
| 615 op.filter(img.getRaster(), dest.getRaster()); | |
| 616 img = dest; | |
| 617 } else if (colop == ColorOp.INVERT) { | |
| 618 /* | |
| 619 * invert colors i.e. invert every channel | |
| 620 */ | |
| 621 logger.debug("Color op: inverting"); | |
| 622 LookupTable invtbl = null; | |
| 623 ColorModel cm = img.getColorModel(); | |
| 864 | 624 if (cm instanceof IndexColorModel) { |
| 625 // invert not possible | |
| 626 // TODO: should we convert? | |
| 627 logger.debug("Color op: unable to invert"); | |
| 628 return; | |
| 629 } | |
| 860 | 630 if (needsInvertRgba && cm.hasAlpha()) { |
| 864 | 631 // fix for some cases |
| 860 | 632 invtbl = invertRgbaByteTable; |
| 633 } else { | |
| 634 invtbl = invertSingleByteTable; | |
| 635 } | |
| 636 LookupOp op = new LookupOp(invtbl, renderHint); | |
| 865 | 637 logger.debug("colop: image=" + img); |
| 860 | 638 op.filter(img, img); |
| 639 } else if (colop == ColorOp.MAP_GRAY_BGR) { | |
| 640 /* | |
| 641 * false color image from grayscale (0: blue, 128: green, 255: red) | |
| 642 */ | |
| 643 logger.debug("Color op: map_gray_bgr"); | |
| 644 // convert to grayscale | |
| 645 ColorConvertOp grayOp = new ColorConvertOp( | |
| 646 ColorSpace.getInstance(ColorSpace.CS_GRAY), renderHint); | |
| 864 | 647 // create new 3-channel image |
| 865 | 648 int destType = BufferedImage.TYPE_INT_RGB; |
| 649 if (needsMapBgr) { | |
| 650 // special case for broken Java2Ds | |
| 651 destType = BufferedImage.TYPE_3BYTE_BGR; | |
| 652 } | |
| 860 | 653 BufferedImage dest = new BufferedImage(img.getWidth(), |
| 865 | 654 img.getHeight(), destType); |
| 860 | 655 img = grayOp.filter(img, dest); |
| 656 logger.debug("map_gray: image=" + img); | |
| 657 // convert to false color | |
| 658 LookupOp mapOp = new LookupOp(mapBgrByteTable, renderHint); | |
| 659 mapOp.filter(img, img); | |
| 660 logger.debug("mapped image=" + img); | |
| 661 } | |
| 662 } | |
| 829 | 663 |
| 207 | 664 public void dispose() { |
| 906 | 665 if (reader != null) { |
| 666 reader.dispose(); | |
| 667 } | |
|
819
a23c4c15a6a8
clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents:
801
diff
changeset
|
668 // is this necessary? |
| 148 | 669 img = null; |
| 670 } | |
| 671 | |
| 533 | 672 public Image getAwtImage(){ |
| 496 | 673 return (Image) img; |
| 674 } | |
| 675 | |
| 676 | |
| 1 | 677 } |
