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