Mercurial > hg > digilib-old
annotate servlet/src/digilib/image/ImageLoaderDocuImage.java @ 176:67ff8c7fecb9
Servlet version 1.17b2
- new mapping file for "virtual directories"
- direct file URLs now work without extension (even with wrong ones)
author | robcast |
---|---|
date | Mon, 10 Nov 2003 20:59:00 +0100 |
parents | d40922628e4a |
children | afe7ff98bb71 |
rev | line source |
---|---|
1 | 1 /* ImageLoaderDocuImage -- Image class implementation using JDK 1.4 ImageLoader |
2 | |
3 Digital Image Library servlet components | |
4 | |
85 | 5 Copyright (C) 2002, 2003 Robert Casties (robcast@mail.berlios.de) |
1 | 6 |
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 | |
14 | |
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 */ | |
19 | |
20 package digilib.image; | |
21 | |
85 | 22 import java.awt.Rectangle; |
73 | 23 import java.awt.geom.AffineTransform; |
101 | 24 import java.awt.geom.Rectangle2D; |
73 | 25 import java.awt.image.AffineTransformOp; |
26 import java.awt.image.BufferedImage; | |
144 | 27 import java.awt.image.ConvolveOp; |
28 import java.awt.image.Kernel; | |
85 | 29 import java.awt.image.RescaleOp; |
73 | 30 import java.io.File; |
31 import java.io.IOException; | |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
32 import java.io.OutputStream; |
89 | 33 import java.io.RandomAccessFile; |
85 | 34 import java.util.Iterator; |
1 | 35 |
73 | 36 import javax.imageio.ImageIO; |
85 | 37 import javax.imageio.ImageReadParam; |
38 import javax.imageio.ImageReader; | |
39 import javax.imageio.stream.ImageInputStream; | |
1 | 40 |
159 | 41 import digilib.io.ImageFile; |
73 | 42 import digilib.io.FileOpException; |
1 | 43 |
73 | 44 /** Implementation of DocuImage using the ImageLoader API of Java 1.4 and Java2D. */ |
1 | 45 public class ImageLoaderDocuImage extends DocuImageImpl { |
46 | |
86 | 47 /** image object */ |
48 protected BufferedImage img; | |
49 /** interpolation type */ | |
50 protected int interpol; | |
51 /** ImageIO image reader */ | |
52 protected ImageReader reader; | |
53 /** File that was read */ | |
54 protected File imgFile; | |
85 | 55 |
56 /* loadSubimage is supported. */ | |
57 public boolean isSubimageSupported() { | |
58 return true; | |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
59 } |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
60 |
85 | 61 public void setQuality(int qual) { |
62 quality = qual; | |
63 // setup interpolation quality | |
64 if (qual > 0) { | |
65 util.dprintln(4, "quality q1"); | |
66 interpol = AffineTransformOp.TYPE_BILINEAR; | |
67 } else { | |
68 util.dprintln(4, "quality q0"); | |
69 interpol = AffineTransformOp.TYPE_NEAREST_NEIGHBOR; | |
70 } | |
71 } | |
86 | 72 |
85 | 73 public int getHeight() { |
74 int h = 0; | |
75 try { | |
76 if (img == null) { | |
77 h = reader.getHeight(0); | |
78 } else { | |
79 h = img.getHeight(); | |
80 } | |
81 } catch (IOException e) { | |
82 e.printStackTrace(); | |
83 } | |
84 return h; | |
85 } | |
86 | |
87 public int getWidth() { | |
88 int w = 0; | |
89 try { | |
90 if (img == null) { | |
91 w = reader.getWidth(0); | |
92 } else { | |
93 w = img.getWidth(); | |
94 } | |
95 } catch (IOException e) { | |
96 e.printStackTrace(); | |
97 } | |
98 return w; | |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
99 } |
1 | 100 |
86 | 101 /* load image file */ |
159 | 102 public void loadImage(ImageFile f) throws FileOpException { |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
103 util.dprintln(10, "loadImage!"); |
159 | 104 //System.gc(); |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
105 try { |
149 | 106 img = ImageIO.read(f.getFile()); |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
107 if (img == null) { |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
108 util.dprintln(3, "ERROR(loadImage): unable to load file"); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
109 throw new FileOpException("Unable to load File!"); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
110 } |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
111 } catch (IOException e) { |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
112 throw new FileOpException("Error reading image."); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
113 } |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
114 } |
1 | 115 |
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
116 /** Get an ImageReader for the image file. */ |
159 | 117 public void preloadImage(ImageFile f) throws IOException { |
148 | 118 if (reader != null) { |
119 // clean up old reader | |
120 reader.dispose(); | |
121 reader = null; | |
122 } | |
159 | 123 //System.gc(); |
149 | 124 RandomAccessFile rf = new RandomAccessFile(f.getFile(), "r"); |
89 | 125 ImageInputStream istream = ImageIO.createImageInputStream(rf); |
149 | 126 //Iterator readers = ImageIO.getImageReaders(istream); |
127 //String ext = f.getName().substring(f.getName().lastIndexOf('.')+1); | |
128 //Iterator readers = ImageIO.getImageReadersBySuffix(ext); | |
129 Iterator readers = ImageIO.getImageReadersByMIMEType(f.getMimetype()); | |
89 | 130 reader = (ImageReader) readers.next(); |
149 | 131 /* are there more readers? */ |
140 | 132 System.out.println("this reader: " + reader.getClass()); |
133 while (readers.hasNext()) { | |
134 System.out.println("next reader: " + readers.next().getClass()); | |
135 } | |
149 | 136 //*/ |
89 | 137 reader.setInput(istream); |
85 | 138 if (reader == null) { |
139 util.dprintln(3, "ERROR(loadImage): unable to load file"); | |
140 throw new FileOpException("Unable to load File!"); | |
141 } | |
149 | 142 imgFile = f.getFile(); |
85 | 143 } |
144 | |
145 /* Load an image file into the Object. */ | |
159 | 146 public void loadSubimage(ImageFile f, Rectangle region, int prescale) |
85 | 147 throws FileOpException { |
159 | 148 //System.gc(); |
85 | 149 try { |
149 | 150 if ((reader == null) || (imgFile != f.getFile())) { |
85 | 151 preloadImage(f); |
152 } | |
153 // set up reader parameters | |
154 ImageReadParam readParam = reader.getDefaultReadParam(); | |
155 readParam.setSourceRegion(region); | |
156 readParam.setSourceSubsampling(prescale, prescale, 0, 0); | |
157 // read image | |
158 img = reader.read(0, readParam); | |
159 } catch (IOException e) { | |
160 util.dprintln(3, "ERROR(loadImage): unable to load file"); | |
161 throw new FileOpException("Unable to load File!"); | |
162 } | |
163 if (img == null) { | |
164 util.dprintln(3, "ERROR(loadImage): unable to load file"); | |
165 throw new FileOpException("Unable to load File!"); | |
166 } | |
167 } | |
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
168 |
86 | 169 /* write image of type mt to Stream */ |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
170 public void writeImage(String mt, OutputStream ostream) |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
171 throws FileOpException { |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
172 util.dprintln(10, "writeImage!"); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
173 try { |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
174 // setup output |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
175 String type = "png"; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
176 if (mt == "image/jpeg") { |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
177 type = "jpeg"; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
178 } else if (mt == "image/png") { |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
179 type = "png"; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
180 } else { |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
181 // unknown mime type |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
182 util.dprintln(2, "ERROR(writeImage): Unknown mime type " + mt); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
183 throw new FileOpException("Unknown mime type: " + mt); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
184 } |
140 | 185 |
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
186 /* |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
187 * JPEG doesn't do transparency so we have to convert any RGBA |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
188 * image to RGB :-( *Java2D BUG* |
140 | 189 */ |
190 if ((type == "jpeg") && (img.getColorModel().hasAlpha())) { | |
191 util.dprintln(2, "BARF: JPEG with transparency!!"); | |
192 int w = img.getWidth(); | |
193 int h = img.getHeight(); | |
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
194 // BufferedImage.TYPE_INT_RGB seems to be fastest (JDK1.4.1, |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
195 // OSX) |
144 | 196 int destType = BufferedImage.TYPE_INT_RGB; |
197 BufferedImage img2 = new BufferedImage(w, h, destType); | |
140 | 198 img2.createGraphics().drawImage(img, null, 0, 0); |
199 img = img2; | |
200 } | |
201 | |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
202 // render output |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
203 if (ImageIO.write(img, type, ostream)) { |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
204 // writing was OK |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
205 return; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
206 } else { |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
207 throw new FileOpException("Error writing image: Unknown image format!"); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
208 } |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
209 } catch (IOException e) { |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
210 // e.printStackTrace(); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
211 throw new FileOpException("Error writing image."); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
212 } |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
213 } |
1 | 214 |
149 | 215 public void scale(double scale, double scaleY) throws ImageOpException { |
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
216 /* for downscaling in high quality the image is blurred first */ |
144 | 217 if ((scale <= 0.5) && (quality > 1)) { |
218 int bl = (int) Math.floor(1 / scale); | |
219 blur(bl); | |
220 } | |
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
221 /* and scaled */ |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
222 AffineTransformOp scaleOp = |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
223 new AffineTransformOp( |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
224 AffineTransform.getScaleInstance(scale, scale), |
85 | 225 interpol); |
144 | 226 BufferedImage scaledImg = null; |
227 // enforce grey destination image for greyscale *Java2D BUG* | |
228 if ((quality > 0) | |
145 | 229 && (img.getColorModel().getNumColorComponents() == 1)) { |
144 | 230 Rectangle2D dstBounds = scaleOp.getBounds2D(img); |
231 scaledImg = | |
232 new BufferedImage( | |
233 (int) dstBounds.getWidth(), | |
234 (int) dstBounds.getHeight(), | |
235 img.getType()); | |
236 } | |
237 scaledImg = scaleOp.filter(img, scaledImg); | |
238 //DEBUG | |
239 util.dprintln( | |
240 3, | |
241 "SCALE: " | |
242 + scale | |
243 + " ->" | |
244 + scaledImg.getWidth() | |
245 + "x" | |
246 + scaledImg.getHeight()); | |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
247 if (scaledImg == null) { |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
248 util.dprintln(2, "ERROR(cropAndScale): error in scale"); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
249 throw new ImageOpException("Unable to scale"); |
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 img = scaledImg; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
252 } |
1 | 253 |
144 | 254 public void blur(int radius) throws ImageOpException { |
255 //DEBUG | |
256 util.dprintln(4, "blur: " + radius); | |
257 // minimum radius is 2 | |
258 int klen = Math.max(radius, 2); | |
259 int ksize = klen * klen; | |
260 // kernel is constant 1/k | |
261 float f = 1f / ksize; | |
262 float[] kern = new float[ksize]; | |
263 for (int i = 0; i < ksize; i++) { | |
264 kern[i] = f; | |
265 } | |
266 Kernel blur = new Kernel(klen, klen, kern); | |
267 // blur with convolve operation | |
268 ConvolveOp blurOp = new ConvolveOp(blur, ConvolveOp.EDGE_NO_OP, null); | |
145 | 269 // blur needs explicit destination image type for color *Java2D BUG* |
270 BufferedImage blurredImg = null; | |
271 if (img.getType() == BufferedImage.TYPE_3BYTE_BGR) { | |
272 blurredImg = | |
273 new BufferedImage( | |
274 img.getWidth(), | |
275 img.getHeight(), | |
276 img.getType()); | |
277 } | |
278 blurredImg = blurOp.filter(img, blurredImg); | |
144 | 279 if (blurredImg == null) { |
280 util.dprintln(2, "ERROR(cropAndScale): error in scale"); | |
281 throw new ImageOpException("Unable to scale"); | |
282 } | |
283 img = blurredImg; | |
284 } | |
285 | |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
286 public void crop(int x_off, int y_off, int width, int height) |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
287 throws ImageOpException { |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
288 // setup Crop |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
289 BufferedImage croppedImg = img.getSubimage(x_off, y_off, width, height); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
290 util.dprintln( |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
291 3, |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
292 "CROP:" + croppedImg.getWidth() + "x" + croppedImg.getHeight()); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
293 //DEBUG |
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
294 // util.dprintln(2, " time |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
295 // "+(System.currentTimeMillis()-startTime)+"ms"); |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
296 if (croppedImg == null) { |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
297 util.dprintln(2, "ERROR(cropAndScale): error in crop"); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
298 throw new ImageOpException("Unable to crop"); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
299 } |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
300 img = croppedImg; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
301 } |
1 | 302 |
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
303 public void enhance(float mult, float add) throws ImageOpException { |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
304 /* |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
305 * Only one constant should work regardless of the number of bands |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
306 * according to the JDK spec. Doesn't work on JDK 1.4 for OSX and Linux |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
307 * (at least). RescaleOp scaleOp = new RescaleOp( (float)mult, |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
308 * (float)add, null); scaleOp.filter(img, img); |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
309 */ |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
310 |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
311 /* The number of constants must match the number of bands in the image. */ |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
312 int ncol = img.getColorModel().getNumComponents(); |
86 | 313 float[] dm = new float[ncol]; |
314 float[] da = new float[ncol]; | |
315 for (int i = 0; i < ncol; i++) { | |
316 dm[i] = (float) mult; | |
317 da[i] = (float) add; | |
85 | 318 } |
86 | 319 RescaleOp scaleOp = new RescaleOp(dm, da, null); |
85 | 320 scaleOp.filter(img, img); |
321 } | |
322 | |
86 | 323 public void enhanceRGB(float[] rgbm, float[] rgba) |
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
324 throws ImageOpException { |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
325 |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
326 /* |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
327 * The number of constants must match the number of bands in the image. |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
328 * We do only 3 (RGB) bands. |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
329 */ |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
330 |
86 | 331 int ncol = img.getColorModel().getNumColorComponents(); |
332 if ((ncol != 3) || (rgbm.length != 3) || (rgba.length != 3)) { | |
333 util.dprintln( | |
334 2, | |
335 "ERROR(enhance): unknown number of color bands or coefficients (" | |
336 + ncol | |
337 + ")"); | |
338 return; | |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
339 } |
89 | 340 RescaleOp scaleOp = |
341 new RescaleOp(rgbOrdered(rgbm), rgbOrdered(rgba), null); | |
86 | 342 scaleOp.filter(img, img); |
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
343 } |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
344 |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
345 /** |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
346 * Ensures that the array f is in the right order to map the images RGB |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
347 * components. (not shure what happens |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
348 */ |
86 | 349 public float[] rgbOrdered(float[] fa) { |
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
350 /* |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
351 * TODO: this is UGLY, UGLY!! |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
352 */ |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
353 float[] fb; |
86 | 354 int t = img.getType(); |
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
355 if (img.getColorModel().hasAlpha()) { |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
356 fb = new float[4]; |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
357 if ((t == BufferedImage.TYPE_INT_ARGB) |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
358 || (t == BufferedImage.TYPE_INT_ARGB_PRE)) { |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
359 // RGB Type |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
360 fb[0] = fa[0]; |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
361 fb[1] = fa[1]; |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
362 fb[2] = fa[2]; |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
363 fb[3] = 1f; |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
364 } else { |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
365 // this isn't tested :-( |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
366 fb[0] = 1f; |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
367 fb[1] = fa[0]; |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
368 fb[2] = fa[1]; |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
369 fb[3] = fa[2]; |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
370 } |
86 | 371 } else { |
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
372 fb = new float[3]; |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
373 if (t == BufferedImage.TYPE_3BYTE_BGR) { |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
374 // BGR Type (actually it looks like RBG...) |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
375 fb[0] = fa[0]; |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
376 fb[1] = fa[2]; |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
377 fb[2] = fa[1]; |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
378 } else { |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
379 fb[0] = fa[0]; |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
380 fb[1] = fa[1]; |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
381 fb[2] = fa[2]; |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
382 } |
86 | 383 } |
384 return fb; | |
85 | 385 } |
386 | |
140 | 387 public void rotate(double angle) throws ImageOpException { |
89 | 388 // setup rotation |
389 double rangle = Math.toRadians(angle); | |
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
390 // create offset to make shure the rotated image has no negative |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
391 // coordinates |
101 | 392 double w = img.getWidth(); |
393 double h = img.getHeight(); | |
103 | 394 AffineTransform trafo = new AffineTransform(); |
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
395 // center of rotation |
103 | 396 double x = (w / 2); |
397 double y = (h / 2); | |
101 | 398 trafo.rotate(rangle, x, y); |
103 | 399 // try rotation to see how far we're out of bounds |
400 AffineTransformOp rotOp = new AffineTransformOp(trafo, interpol); | |
401 Rectangle2D rotbounds = rotOp.getBounds2D(img); | |
402 double xoff = rotbounds.getX(); | |
403 double yoff = rotbounds.getY(); | |
404 // move image back in line | |
140 | 405 trafo.preConcatenate( |
406 AffineTransform.getTranslateInstance(-xoff, -yoff)); | |
101 | 407 // transform image |
103 | 408 rotOp = new AffineTransformOp(trafo, interpol); |
89 | 409 BufferedImage rotImg = rotOp.filter(img, null); |
101 | 410 // calculate new bounding box |
103 | 411 //Rectangle2D bounds = rotOp.getBounds2D(img); |
89 | 412 if (rotImg == null) { |
413 util.dprintln(2, "ERROR: error in rotate"); | |
414 throw new ImageOpException("Unable to rotate"); | |
415 } | |
103 | 416 img = rotImg; |
101 | 417 // crop new image (with self-made rounding) |
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
418 /* |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
419 * img = rotImg.getSubimage( (int) (bounds.getX()+0.5), (int) |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
420 * (bounds.getY()+0.5), (int) (bounds.getWidth()+0.5), (int) |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
421 * (bounds.getHeight()+0.5)); |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
422 */ |
86 | 423 } |
85 | 424 |
89 | 425 public void mirror(double angle) throws ImageOpException { |
426 // setup mirror | |
427 double mx = 1; | |
428 double my = 1; | |
429 double tx = 0; | |
430 double ty = 0; | |
144 | 431 if (Math.abs(angle - 0) < epsilon) { // 0 degree |
89 | 432 mx = -1; |
433 tx = getWidth(); | |
144 | 434 } else if (Math.abs(angle - 90) < epsilon) { // 90 degree |
89 | 435 my = -1; |
436 ty = getHeight(); | |
144 | 437 } else if (Math.abs(angle - 180) < epsilon) { // 180 degree |
89 | 438 mx = -1; |
439 tx = getWidth(); | |
144 | 440 } else if (Math.abs(angle - 270) < epsilon) { // 270 degree |
89 | 441 my = -1; |
442 ty = getHeight(); | |
144 | 443 } else if (Math.abs(angle - 360) < epsilon) { // 360 degree |
89 | 444 mx = -1; |
445 tx = getWidth(); | |
446 } | |
447 AffineTransformOp mirOp = | |
448 new AffineTransformOp( | |
449 new AffineTransform(mx, 0, 0, my, tx, ty), | |
450 interpol); | |
451 BufferedImage mirImg = mirOp.filter(img, null); | |
452 if (mirImg == null) { | |
453 util.dprintln(2, "ERROR: error in mirror"); | |
454 throw new ImageOpException("Unable to mirror"); | |
455 } | |
456 img = mirImg; | |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
457 } |
86 | 458 |
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
459 /* (non-Javadoc) @see java.lang.Object#finalize() */ |
148 | 460 protected void finalize() throws Throwable { |
461 //System.out.println("FIN de ImageLoaderDocuImage!"); | |
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
462 // we must dispose the ImageReader because it keeps the filehandle |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
463 // open! |
148 | 464 reader.dispose(); |
465 reader = null; | |
466 img = null; | |
467 super.finalize(); | |
468 } | |
469 | |
1 | 470 } |