Mercurial > hg > digilib-old
annotate servlet/src/digilib/image/ImageLoaderDocuImage.java @ 206:ffbe705d1c3f
Servlet version 1.18b9 -- cleanup and bugfixes
- fixed bug with slow color JPEGs
- better pathname handling
- better filehandle cleanup (hopefully)
author | robcast |
---|---|
date | Fri, 12 Mar 2004 19:52:06 +0100 |
parents | afe7ff98bb71 |
children | 1a65d8e43620 |
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 { |
181 | 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) { | |
181 | 65 logger.debug("quality q1"); |
85 | 66 interpol = AffineTransformOp.TYPE_BILINEAR; |
67 } else { | |
181 | 68 logger.debug("quality q0"); |
85 | 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) { | |
181 | 82 logger.debug("error in getHeight", e); |
85 | 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) { | |
181 | 96 logger.debug("error in getHeight", e); |
85 | 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 { |
181 | 103 logger.debug("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 throw new FileOpException("Unable to load File!"); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
109 } |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
110 } catch (IOException e) { |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
111 throw new FileOpException("Error reading image."); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
112 } |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
113 } |
1 | 114 |
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
115 /** Get an ImageReader for the image file. */ |
159 | 116 public void preloadImage(ImageFile f) throws IOException { |
148 | 117 if (reader != null) { |
118 // clean up old reader | |
119 reader.dispose(); | |
120 reader = null; | |
121 } | |
159 | 122 //System.gc(); |
149 | 123 RandomAccessFile rf = new RandomAccessFile(f.getFile(), "r"); |
89 | 124 ImageInputStream istream = ImageIO.createImageInputStream(rf); |
149 | 125 //Iterator readers = ImageIO.getImageReaders(istream); |
126 //String ext = f.getName().substring(f.getName().lastIndexOf('.')+1); | |
127 //Iterator readers = ImageIO.getImageReadersBySuffix(ext); | |
128 Iterator readers = ImageIO.getImageReadersByMIMEType(f.getMimetype()); | |
89 | 129 reader = (ImageReader) readers.next(); |
149 | 130 /* are there more readers? */ |
181 | 131 logger.debug("this reader: " + reader.getClass()); |
140 | 132 while (readers.hasNext()) { |
181 | 133 logger.debug("next reader: " + readers.next().getClass()); |
140 | 134 } |
149 | 135 //*/ |
89 | 136 reader.setInput(istream); |
85 | 137 if (reader == null) { |
138 throw new FileOpException("Unable to load File!"); | |
139 } | |
149 | 140 imgFile = f.getFile(); |
85 | 141 } |
142 | |
143 /* Load an image file into the Object. */ | |
159 | 144 public void loadSubimage(ImageFile f, Rectangle region, int prescale) |
85 | 145 throws FileOpException { |
159 | 146 //System.gc(); |
85 | 147 try { |
149 | 148 if ((reader == null) || (imgFile != f.getFile())) { |
85 | 149 preloadImage(f); |
150 } | |
151 // set up reader parameters | |
152 ImageReadParam readParam = reader.getDefaultReadParam(); | |
153 readParam.setSourceRegion(region); | |
154 readParam.setSourceSubsampling(prescale, prescale, 0, 0); | |
155 // read image | |
156 img = reader.read(0, readParam); | |
157 } catch (IOException e) { | |
158 throw new FileOpException("Unable to load File!"); | |
159 } | |
160 if (img == null) { | |
161 throw new FileOpException("Unable to load File!"); | |
162 } | |
163 } | |
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
164 |
86 | 165 /* write image of type mt to Stream */ |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
166 public void writeImage(String mt, OutputStream ostream) |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
167 throws FileOpException { |
181 | 168 logger.debug("writeImage!"); |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
169 try { |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
170 // setup output |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
171 String type = "png"; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
172 if (mt == "image/jpeg") { |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
173 type = "jpeg"; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
174 } else if (mt == "image/png") { |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
175 type = "png"; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
176 } else { |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
177 // unknown mime type |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
178 throw new FileOpException("Unknown mime type: " + mt); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
179 } |
140 | 180 |
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
181 /* |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
182 * 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
|
183 * image to RGB :-( *Java2D BUG* |
140 | 184 */ |
185 if ((type == "jpeg") && (img.getColorModel().hasAlpha())) { | |
181 | 186 logger.debug("BARF: JPEG with transparency!!"); |
140 | 187 int w = img.getWidth(); |
188 int h = img.getHeight(); | |
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
189 // 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
|
190 // OSX) |
144 | 191 int destType = BufferedImage.TYPE_INT_RGB; |
192 BufferedImage img2 = new BufferedImage(w, h, destType); | |
140 | 193 img2.createGraphics().drawImage(img, null, 0, 0); |
194 img = img2; | |
195 } | |
196 | |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
197 // render output |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
198 if (ImageIO.write(img, type, ostream)) { |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
199 // writing was OK |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
200 return; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
201 } else { |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
202 throw new FileOpException("Error writing image: Unknown image format!"); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
203 } |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
204 } catch (IOException e) { |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
205 throw new FileOpException("Error writing image."); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
206 } |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
207 } |
1 | 208 |
149 | 209 public void scale(double scale, double scaleY) throws ImageOpException { |
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
210 /* for downscaling in high quality the image is blurred first */ |
144 | 211 if ((scale <= 0.5) && (quality > 1)) { |
212 int bl = (int) Math.floor(1 / scale); | |
213 blur(bl); | |
214 } | |
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
215 /* and scaled */ |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
216 AffineTransformOp scaleOp = |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
217 new AffineTransformOp( |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
218 AffineTransform.getScaleInstance(scale, scale), |
85 | 219 interpol); |
144 | 220 BufferedImage scaledImg = null; |
221 // enforce grey destination image for greyscale *Java2D BUG* | |
222 if ((quality > 0) | |
145 | 223 && (img.getColorModel().getNumColorComponents() == 1)) { |
144 | 224 Rectangle2D dstBounds = scaleOp.getBounds2D(img); |
225 scaledImg = | |
226 new BufferedImage( | |
227 (int) dstBounds.getWidth(), | |
228 (int) dstBounds.getHeight(), | |
229 img.getType()); | |
230 } | |
231 scaledImg = scaleOp.filter(img, scaledImg); | |
232 //DEBUG | |
181 | 233 logger.debug("SCALE: " |
144 | 234 + scale |
235 + " ->" | |
236 + scaledImg.getWidth() | |
237 + "x" | |
238 + scaledImg.getHeight()); | |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
239 if (scaledImg == null) { |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
240 throw new ImageOpException("Unable to scale"); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
241 } |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
242 img = scaledImg; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
243 } |
1 | 244 |
144 | 245 public void blur(int radius) throws ImageOpException { |
246 //DEBUG | |
181 | 247 logger.debug("blur: " + radius); |
144 | 248 // minimum radius is 2 |
249 int klen = Math.max(radius, 2); | |
250 int ksize = klen * klen; | |
251 // kernel is constant 1/k | |
252 float f = 1f / ksize; | |
253 float[] kern = new float[ksize]; | |
254 for (int i = 0; i < ksize; i++) { | |
255 kern[i] = f; | |
256 } | |
257 Kernel blur = new Kernel(klen, klen, kern); | |
258 // blur with convolve operation | |
259 ConvolveOp blurOp = new ConvolveOp(blur, ConvolveOp.EDGE_NO_OP, null); | |
145 | 260 // blur needs explicit destination image type for color *Java2D BUG* |
261 BufferedImage blurredImg = null; | |
262 if (img.getType() == BufferedImage.TYPE_3BYTE_BGR) { | |
263 blurredImg = | |
264 new BufferedImage( | |
265 img.getWidth(), | |
266 img.getHeight(), | |
267 img.getType()); | |
268 } | |
269 blurredImg = blurOp.filter(img, blurredImg); | |
144 | 270 if (blurredImg == null) { |
271 throw new ImageOpException("Unable to scale"); | |
272 } | |
273 img = blurredImg; | |
274 } | |
275 | |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
276 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
|
277 throws ImageOpException { |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
278 // setup Crop |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
279 BufferedImage croppedImg = img.getSubimage(x_off, y_off, width, height); |
181 | 280 logger.debug("CROP:" + croppedImg.getWidth() + "x" + croppedImg.getHeight()); |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
281 //DEBUG |
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
282 // util.dprintln(2, " time |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
283 // "+(System.currentTimeMillis()-startTime)+"ms"); |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
284 if (croppedImg == null) { |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
285 throw new ImageOpException("Unable to crop"); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
286 } |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
287 img = croppedImg; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
288 } |
1 | 289 |
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
290 public void enhance(float mult, float add) throws ImageOpException { |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
291 /* |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
292 * 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
|
293 * 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
|
294 * (at least). RescaleOp scaleOp = new RescaleOp( (float)mult, |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
295 * (float)add, null); scaleOp.filter(img, img); |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
296 */ |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
297 |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
298 /* 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
|
299 int ncol = img.getColorModel().getNumComponents(); |
86 | 300 float[] dm = new float[ncol]; |
301 float[] da = new float[ncol]; | |
302 for (int i = 0; i < ncol; i++) { | |
303 dm[i] = (float) mult; | |
304 da[i] = (float) add; | |
85 | 305 } |
86 | 306 RescaleOp scaleOp = new RescaleOp(dm, da, null); |
85 | 307 scaleOp.filter(img, img); |
308 } | |
309 | |
86 | 310 public void enhanceRGB(float[] rgbm, float[] rgba) |
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
311 throws ImageOpException { |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
312 |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
313 /* |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
314 * 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
|
315 * We do only 3 (RGB) bands. |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
316 */ |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
317 |
86 | 318 int ncol = img.getColorModel().getNumColorComponents(); |
319 if ((ncol != 3) || (rgbm.length != 3) || (rgba.length != 3)) { | |
181 | 320 logger.debug("ERROR(enhance): unknown number of color bands or coefficients (" |
86 | 321 + ncol |
322 + ")"); | |
323 return; | |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
324 } |
89 | 325 RescaleOp scaleOp = |
326 new RescaleOp(rgbOrdered(rgbm), rgbOrdered(rgba), null); | |
86 | 327 scaleOp.filter(img, img); |
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
328 } |
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 /** |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
331 * 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
|
332 * components. (not shure what happens |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
333 */ |
86 | 334 public float[] rgbOrdered(float[] fa) { |
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
335 /* |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
336 * TODO: this is UGLY, UGLY!! |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
337 */ |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
338 float[] fb; |
86 | 339 int t = img.getType(); |
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
340 if (img.getColorModel().hasAlpha()) { |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
341 fb = new float[4]; |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
342 if ((t == BufferedImage.TYPE_INT_ARGB) |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
343 || (t == BufferedImage.TYPE_INT_ARGB_PRE)) { |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
344 // RGB Type |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
345 fb[0] = fa[0]; |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
346 fb[1] = fa[1]; |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
347 fb[2] = fa[2]; |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
348 fb[3] = 1f; |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
349 } else { |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
350 // this isn't tested :-( |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
351 fb[0] = 1f; |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
352 fb[1] = fa[0]; |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
353 fb[2] = fa[1]; |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
354 fb[3] = fa[2]; |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
355 } |
86 | 356 } else { |
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
357 fb = new float[3]; |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
358 if (t == BufferedImage.TYPE_3BYTE_BGR) { |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
359 // BGR Type (actually it looks like RBG...) |
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[2]; |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
362 fb[2] = fa[1]; |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
363 } else { |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
364 fb[0] = fa[0]; |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
365 fb[1] = fa[1]; |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
366 fb[2] = fa[2]; |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
367 } |
86 | 368 } |
369 return fb; | |
85 | 370 } |
371 | |
140 | 372 public void rotate(double angle) throws ImageOpException { |
89 | 373 // setup rotation |
374 double rangle = Math.toRadians(angle); | |
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
375 // 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
|
376 // coordinates |
101 | 377 double w = img.getWidth(); |
378 double h = img.getHeight(); | |
103 | 379 AffineTransform trafo = new AffineTransform(); |
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
380 // center of rotation |
103 | 381 double x = (w / 2); |
382 double y = (h / 2); | |
101 | 383 trafo.rotate(rangle, x, y); |
103 | 384 // try rotation to see how far we're out of bounds |
385 AffineTransformOp rotOp = new AffineTransformOp(trafo, interpol); | |
386 Rectangle2D rotbounds = rotOp.getBounds2D(img); | |
387 double xoff = rotbounds.getX(); | |
388 double yoff = rotbounds.getY(); | |
389 // move image back in line | |
140 | 390 trafo.preConcatenate( |
391 AffineTransform.getTranslateInstance(-xoff, -yoff)); | |
101 | 392 // transform image |
103 | 393 rotOp = new AffineTransformOp(trafo, interpol); |
89 | 394 BufferedImage rotImg = rotOp.filter(img, null); |
101 | 395 // calculate new bounding box |
103 | 396 //Rectangle2D bounds = rotOp.getBounds2D(img); |
89 | 397 if (rotImg == null) { |
398 throw new ImageOpException("Unable to rotate"); | |
399 } | |
103 | 400 img = rotImg; |
101 | 401 // crop new image (with self-made rounding) |
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
402 /* |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
403 * img = rotImg.getSubimage( (int) (bounds.getX()+0.5), (int) |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
404 * (bounds.getY()+0.5), (int) (bounds.getWidth()+0.5), (int) |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
405 * (bounds.getHeight()+0.5)); |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
406 */ |
86 | 407 } |
85 | 408 |
89 | 409 public void mirror(double angle) throws ImageOpException { |
410 // setup mirror | |
411 double mx = 1; | |
412 double my = 1; | |
413 double tx = 0; | |
414 double ty = 0; | |
144 | 415 if (Math.abs(angle - 0) < epsilon) { // 0 degree |
89 | 416 mx = -1; |
417 tx = getWidth(); | |
144 | 418 } else if (Math.abs(angle - 90) < epsilon) { // 90 degree |
89 | 419 my = -1; |
420 ty = getHeight(); | |
144 | 421 } else if (Math.abs(angle - 180) < epsilon) { // 180 degree |
89 | 422 mx = -1; |
423 tx = getWidth(); | |
144 | 424 } else if (Math.abs(angle - 270) < epsilon) { // 270 degree |
89 | 425 my = -1; |
426 ty = getHeight(); | |
144 | 427 } else if (Math.abs(angle - 360) < epsilon) { // 360 degree |
89 | 428 mx = -1; |
429 tx = getWidth(); | |
430 } | |
431 AffineTransformOp mirOp = | |
432 new AffineTransformOp( | |
433 new AffineTransform(mx, 0, 0, my, tx, ty), | |
434 interpol); | |
435 BufferedImage mirImg = mirOp.filter(img, null); | |
436 if (mirImg == null) { | |
437 throw new ImageOpException("Unable to mirror"); | |
438 } | |
439 img = mirImg; | |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
440 } |
86 | 441 |
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
442 /* (non-Javadoc) @see java.lang.Object#finalize() */ |
148 | 443 protected void finalize() throws Throwable { |
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
444 // we must dispose the ImageReader because it keeps the filehandle |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
159
diff
changeset
|
445 // open! |
148 | 446 reader.dispose(); |
447 reader = null; | |
448 img = null; | |
449 super.finalize(); | |
450 } | |
451 | |
1 | 452 } |