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