Mercurial > hg > digilib-old
annotate servlet/src/digilib/image/ImageLoaderDocuImage.java @ 144:d87a7e2515af
Sevlet version 1.12b5
- improved scaling also for ImageLoaderDocuImage
author | robcast |
---|---|
date | Tue, 19 Aug 2003 22:31:48 +0200 |
parents | c878ea574c29 |
children | d76006c12e89 |
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 | |
21 package digilib.image; | |
22 | |
85 | 23 import java.awt.Rectangle; |
73 | 24 import java.awt.geom.AffineTransform; |
101 | 25 import java.awt.geom.Rectangle2D; |
73 | 26 import java.awt.image.AffineTransformOp; |
27 import java.awt.image.BufferedImage; | |
144 | 28 import java.awt.image.ConvolveOp; |
29 import java.awt.image.Kernel; | |
85 | 30 import java.awt.image.RescaleOp; |
73 | 31 import java.io.File; |
32 import java.io.IOException; | |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
33 import java.io.OutputStream; |
89 | 34 import java.io.RandomAccessFile; |
85 | 35 import java.util.Iterator; |
1 | 36 |
73 | 37 import javax.imageio.ImageIO; |
85 | 38 import javax.imageio.ImageReadParam; |
39 import javax.imageio.ImageReader; | |
40 import javax.imageio.stream.ImageInputStream; | |
1 | 41 |
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 */ |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
102 public void loadImage(File f) throws FileOpException { |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
103 util.dprintln(10, "loadImage!"); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
104 System.gc(); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
105 try { |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
106 img = ImageIO.read(f); |
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 |
86 | 116 /** Get an ImageReader for the image file. |
117 * | |
118 */ | |
89 | 119 public void preloadImage(File f) throws IOException { |
85 | 120 System.gc(); |
89 | 121 RandomAccessFile rf = new RandomAccessFile(f, "r"); |
122 ImageInputStream istream = ImageIO.createImageInputStream(rf); | |
123 Iterator readers = ImageIO.getImageReaders(istream); | |
124 reader = (ImageReader) readers.next(); | |
140 | 125 /* are there more readers? |
126 System.out.println("this reader: " + reader.getClass()); | |
127 while (readers.hasNext()) { | |
128 System.out.println("next reader: " + readers.next().getClass()); | |
129 } | |
130 */ | |
89 | 131 reader.setInput(istream); |
85 | 132 if (reader == null) { |
133 util.dprintln(3, "ERROR(loadImage): unable to load file"); | |
134 throw new FileOpException("Unable to load File!"); | |
135 } | |
86 | 136 imgFile = f; |
85 | 137 } |
138 | |
139 /* Load an image file into the Object. */ | |
140 public void loadSubimage(File f, Rectangle region, int prescale) | |
141 throws FileOpException { | |
142 System.gc(); | |
143 try { | |
86 | 144 if ((reader == null) || (imgFile != f)) { |
85 | 145 preloadImage(f); |
146 } | |
147 // set up reader parameters | |
148 ImageReadParam readParam = reader.getDefaultReadParam(); | |
149 readParam.setSourceRegion(region); | |
150 readParam.setSourceSubsampling(prescale, prescale, 0, 0); | |
151 // read image | |
152 img = reader.read(0, readParam); | |
153 } catch (IOException e) { | |
154 util.dprintln(3, "ERROR(loadImage): unable to load file"); | |
155 throw new FileOpException("Unable to load File!"); | |
156 } | |
157 if (img == null) { | |
158 util.dprintln(3, "ERROR(loadImage): unable to load file"); | |
159 throw new FileOpException("Unable to load File!"); | |
160 } | |
161 } | |
162 | |
86 | 163 /* write image of type mt to Stream */ |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
164 public void writeImage(String mt, OutputStream ostream) |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
165 throws FileOpException { |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
166 util.dprintln(10, "writeImage!"); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
167 try { |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
168 // setup output |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
169 String type = "png"; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
170 if (mt == "image/jpeg") { |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
171 type = "jpeg"; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
172 } else if (mt == "image/png") { |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
173 type = "png"; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
174 } else { |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
175 // unknown mime type |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
176 util.dprintln(2, "ERROR(writeImage): Unknown mime type " + mt); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
177 throw new FileOpException("Unknown mime type: " + mt); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
178 } |
140 | 179 |
180 /* JPEG doesn't do transparency so we have to convert any RGBA image | |
144 | 181 * to RGB :-( *Java2D BUG* |
140 | 182 */ |
183 if ((type == "jpeg") && (img.getColorModel().hasAlpha())) { | |
184 util.dprintln(2, "BARF: JPEG with transparency!!"); | |
185 int w = img.getWidth(); | |
186 int h = img.getHeight(); | |
187 // BufferedImage.TYPE_INT_RGB seems to be fastest (JDK1.4.1, OSX) | |
144 | 188 int destType = BufferedImage.TYPE_INT_RGB; |
189 BufferedImage img2 = new BufferedImage(w, h, destType); | |
140 | 190 img2.createGraphics().drawImage(img, null, 0, 0); |
191 img = img2; | |
192 } | |
193 | |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
194 // render output |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
195 if (ImageIO.write(img, type, ostream)) { |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
196 // writing was OK |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
197 return; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
198 } else { |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
199 throw new FileOpException("Error writing image: Unknown image format!"); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
200 } |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
201 } catch (IOException e) { |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
202 // e.printStackTrace(); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
203 throw new FileOpException("Error writing image."); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
204 } |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
205 } |
1 | 206 |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
207 public void scale(double scale) throws ImageOpException { |
144 | 208 /* |
209 * for downscaling in high quality the image is blurred first | |
210 */ | |
211 if ((scale <= 0.5) && (quality > 1)) { | |
212 int bl = (int) Math.floor(1 / scale); | |
213 blur(bl); | |
214 } | |
215 /* | |
216 * and scaled | |
217 */ | |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
218 AffineTransformOp scaleOp = |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
219 new AffineTransformOp( |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
220 AffineTransform.getScaleInstance(scale, scale), |
85 | 221 interpol); |
144 | 222 BufferedImage scaledImg = null; |
223 // enforce grey destination image for greyscale *Java2D BUG* | |
224 if ((quality > 0) | |
225 && ((img.getType() == BufferedImage.TYPE_BYTE_GRAY) | |
226 || (img.getType() == BufferedImage.TYPE_BYTE_BINARY) | |
227 || (img.getType() == BufferedImage.TYPE_USHORT_GRAY))) { | |
228 Rectangle2D dstBounds = scaleOp.getBounds2D(img); | |
229 scaledImg = | |
230 new BufferedImage( | |
231 (int) dstBounds.getWidth(), | |
232 (int) dstBounds.getHeight(), | |
233 img.getType()); | |
234 } | |
235 scaledImg = scaleOp.filter(img, scaledImg); | |
236 //DEBUG | |
237 util.dprintln( | |
238 3, | |
239 "SCALE: " | |
240 + scale | |
241 + " ->" | |
242 + scaledImg.getWidth() | |
243 + "x" | |
244 + scaledImg.getHeight()); | |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
245 if (scaledImg == null) { |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
246 util.dprintln(2, "ERROR(cropAndScale): error in scale"); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
247 throw new ImageOpException("Unable to scale"); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
248 } |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
249 img = scaledImg; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
250 } |
1 | 251 |
144 | 252 public void blur(int radius) throws ImageOpException { |
253 BufferedImage blurredImg; | |
254 //DEBUG | |
255 util.dprintln(4, "blur: " + radius); | |
256 // minimum radius is 2 | |
257 int klen = Math.max(radius, 2); | |
258 int ksize = klen * klen; | |
259 // kernel is constant 1/k | |
260 float f = 1f / ksize; | |
261 float[] kern = new float[ksize]; | |
262 for (int i = 0; i < ksize; i++) { | |
263 kern[i] = f; | |
264 } | |
265 Kernel blur = new Kernel(klen, klen, kern); | |
266 // blur with convolve operation | |
267 ConvolveOp blurOp = new ConvolveOp(blur, ConvolveOp.EDGE_NO_OP, null); | |
268 blurredImg = blurOp.filter(img, null); | |
269 if (blurredImg == null) { | |
270 util.dprintln(2, "ERROR(cropAndScale): error in scale"); | |
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); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
280 util.dprintln( |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
281 3, |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
282 "CROP:" + croppedImg.getWidth() + "x" + croppedImg.getHeight()); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
283 //DEBUG |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
284 // util.dprintln(2, " time "+(System.currentTimeMillis()-startTime)+"ms"); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
285 if (croppedImg == null) { |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
286 util.dprintln(2, "ERROR(cropAndScale): error in crop"); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
287 throw new ImageOpException("Unable to crop"); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
288 } |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
289 img = croppedImg; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
290 } |
1 | 291 |
144 | 292 public void enhance(float mult, float add) |
293 throws ImageOpException { /* Only one constant should work regardless of the number of bands | |
294 * according to the JDK spec. | |
295 * Doesn't work on JDK 1.4 for OSX and Linux (at least). | |
296 */ /* RescaleOp scaleOp = | |
297 new RescaleOp( | |
298 (float)mult, (float)add, | |
299 null); | |
300 scaleOp.filter(img, img); | |
301 */ /* The number of constants must match the number of bands in the image. | |
302 */ | |
86 | 303 int ncol = img.getColorModel().getNumColorComponents(); |
304 float[] dm = new float[ncol]; | |
305 float[] da = new float[ncol]; | |
306 for (int i = 0; i < ncol; i++) { | |
307 dm[i] = (float) mult; | |
308 da[i] = (float) add; | |
85 | 309 } |
86 | 310 RescaleOp scaleOp = new RescaleOp(dm, da, null); |
85 | 311 scaleOp.filter(img, img); |
312 } | |
313 | |
86 | 314 public void enhanceRGB(float[] rgbm, float[] rgba) |
144 | 315 throws ImageOpException { /* The number of constants must match the number of bands in the image. |
316 * We do only 3 (RGB) bands. | |
317 */ | |
86 | 318 int ncol = img.getColorModel().getNumColorComponents(); |
319 if ((ncol != 3) || (rgbm.length != 3) || (rgba.length != 3)) { | |
320 util.dprintln( | |
321 2, | |
322 "ERROR(enhance): unknown number of color bands or coefficients (" | |
323 + ncol | |
324 + ")"); | |
325 return; | |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
326 } |
89 | 327 RescaleOp scaleOp = |
328 new RescaleOp(rgbOrdered(rgbm), rgbOrdered(rgba), null); | |
86 | 329 scaleOp.filter(img, img); |
144 | 330 } /** Ensures that the array f is in the right order to map the images RGB components. |
331 */ | |
86 | 332 public float[] rgbOrdered(float[] fa) { |
333 float[] fb = new float[3]; | |
334 int t = img.getType(); | |
335 if ((t == BufferedImage.TYPE_3BYTE_BGR) | |
336 || (t == BufferedImage.TYPE_4BYTE_ABGR) | |
337 || (t == BufferedImage.TYPE_4BYTE_ABGR_PRE)) { | |
338 // BGR Type (actually it looks like RBG...) | |
339 fb[0] = fa[0]; | |
340 fb[1] = fa[2]; | |
341 fb[2] = fa[1]; | |
342 } else { | |
343 fb = fa; | |
344 } | |
345 return fb; | |
85 | 346 } |
347 | |
140 | 348 public void rotate(double angle) throws ImageOpException { |
89 | 349 // setup rotation |
350 double rangle = Math.toRadians(angle); | |
101 | 351 // create offset to make shure the rotated image has no negative coordinates |
352 double w = img.getWidth(); | |
353 double h = img.getHeight(); | |
103 | 354 AffineTransform trafo = new AffineTransform(); |
355 // center of rotation | |
356 double x = (w / 2); | |
357 double y = (h / 2); | |
101 | 358 trafo.rotate(rangle, x, y); |
103 | 359 // try rotation to see how far we're out of bounds |
360 AffineTransformOp rotOp = new AffineTransformOp(trafo, interpol); | |
361 Rectangle2D rotbounds = rotOp.getBounds2D(img); | |
362 double xoff = rotbounds.getX(); | |
363 double yoff = rotbounds.getY(); | |
364 // move image back in line | |
140 | 365 trafo.preConcatenate( |
366 AffineTransform.getTranslateInstance(-xoff, -yoff)); | |
101 | 367 // transform image |
103 | 368 rotOp = new AffineTransformOp(trafo, interpol); |
89 | 369 BufferedImage rotImg = rotOp.filter(img, null); |
101 | 370 // calculate new bounding box |
103 | 371 //Rectangle2D bounds = rotOp.getBounds2D(img); |
89 | 372 if (rotImg == null) { |
373 util.dprintln(2, "ERROR: error in rotate"); | |
374 throw new ImageOpException("Unable to rotate"); | |
375 } | |
103 | 376 img = rotImg; |
101 | 377 // crop new image (with self-made rounding) |
103 | 378 /* img = |
101 | 379 rotImg.getSubimage( |
380 (int) (bounds.getX()+0.5), | |
381 (int) (bounds.getY()+0.5), | |
382 (int) (bounds.getWidth()+0.5), | |
383 (int) (bounds.getHeight()+0.5)); | |
103 | 384 */ |
86 | 385 } |
85 | 386 |
89 | 387 public void mirror(double angle) throws ImageOpException { |
388 // setup mirror | |
389 double mx = 1; | |
390 double my = 1; | |
391 double tx = 0; | |
392 double ty = 0; | |
144 | 393 if (Math.abs(angle - 0) < epsilon) { // 0 degree |
89 | 394 mx = -1; |
395 tx = getWidth(); | |
144 | 396 } else if (Math.abs(angle - 90) < epsilon) { // 90 degree |
89 | 397 my = -1; |
398 ty = getHeight(); | |
144 | 399 } else if (Math.abs(angle - 180) < epsilon) { // 180 degree |
89 | 400 mx = -1; |
401 tx = getWidth(); | |
144 | 402 } else if (Math.abs(angle - 270) < epsilon) { // 270 degree |
89 | 403 my = -1; |
404 ty = getHeight(); | |
144 | 405 } else if (Math.abs(angle - 360) < epsilon) { // 360 degree |
89 | 406 mx = -1; |
407 tx = getWidth(); | |
408 } | |
409 AffineTransformOp mirOp = | |
410 new AffineTransformOp( | |
411 new AffineTransform(mx, 0, 0, my, tx, ty), | |
412 interpol); | |
413 BufferedImage mirImg = mirOp.filter(img, null); | |
414 if (mirImg == null) { | |
415 util.dprintln(2, "ERROR: error in mirror"); | |
416 throw new ImageOpException("Unable to mirror"); | |
417 } | |
418 img = mirImg; | |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
419 } |
86 | 420 |
1 | 421 } |