Mercurial > hg > digilib-old
annotate servlet/src/digilib/image/JAIDocuImage.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 | e743b853efca |
children | afe7ff98bb71 |
rev | line source |
---|---|
1 | 1 /* JAIDocuImage -- Image class implementation using JAI (Java Advanced Imaging) |
2 | |
3 Digital Image Library servlet components | |
4 | |
85 | 5 Copyright (C) 2001, 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 | |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
23 import java.awt.RenderingHints; |
73 | 24 import java.awt.image.RenderedImage; |
25 import java.awt.image.renderable.ParameterBlock; | |
26 import java.io.IOException; | |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
27 import java.io.OutputStream; |
1 | 28 |
142 | 29 import javax.media.jai.BorderExtender; |
30 import javax.media.jai.Interpolation; | |
31 import javax.media.jai.JAI; | |
32 import javax.media.jai.KernelJAI; | |
33 import javax.media.jai.ParameterBlockJAI; | |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
34 import javax.media.jai.operator.TransposeDescriptor; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
35 import javax.media.jai.operator.TransposeType; |
1 | 36 |
73 | 37 import digilib.Utils; |
159 | 38 import digilib.io.ImageFile; |
73 | 39 import digilib.io.FileOpException; |
1 | 40 |
73 | 41 /** A DocuImage implementation using Java Advanced Imaging Library. */ |
1 | 42 public class JAIDocuImage extends DocuImageImpl { |
43 | |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
44 protected RenderedImage img; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
45 protected Interpolation interpol = null; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
46 |
73 | 47 /** Default constructor. */ |
48 public JAIDocuImage() { | |
49 } | |
1 | 50 |
90 | 51 /** Contructor taking an utils object. |
73 | 52 * @param u utils object. |
53 */ | |
54 public JAIDocuImage(Utils u) { | |
55 util = u; | |
56 } | |
1 | 57 |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
58 /* Load an image file into the Object. */ |
159 | 59 public void loadImage(ImageFile f) throws FileOpException { |
73 | 60 System.gc(); |
149 | 61 img = JAI.create("fileload", f.getFile().getAbsolutePath()); |
73 | 62 if (img == null) { |
63 util.dprintln(3, "ERROR(loadImage): unable to load file"); | |
64 throw new FileOpException("Unable to load File!"); | |
65 } | |
66 } | |
1 | 67 |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
68 /* Write the current image to an OutputStream. */ |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
69 public void writeImage(String mt, OutputStream ostream) |
73 | 70 throws FileOpException { |
71 try { | |
72 // setup output | |
73 ParameterBlock pb3 = new ParameterBlock(); | |
74 pb3.addSource(img); | |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
75 pb3.add(ostream); |
73 | 76 if (mt == "image/jpeg") { |
77 pb3.add("JPEG"); | |
78 } else if (mt == "image/png") { | |
79 pb3.add("PNG"); | |
80 } else { | |
81 // unknown mime type | |
82 util.dprintln(2, "ERROR(writeImage): Unknown mime type " + mt); | |
83 throw new FileOpException("Unknown mime type: " + mt); | |
84 } | |
85 // render output | |
86 JAI.create("encode", pb3); | |
1 | 87 |
73 | 88 } catch (IOException e) { |
89 throw new FileOpException("Error writing image."); | |
90 } | |
91 } | |
1 | 92 |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
93 /* Real setQuality implementation. |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
94 * Creates the correct Interpolation. |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
95 */ |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
96 public void setQuality(int qual) { |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
97 quality = qual; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
98 // setup interpolation quality |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
99 if (qual > 1) { |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
100 util.dprintln(4, "quality q2"); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
101 interpol = Interpolation.getInstance(Interpolation.INTERP_BICUBIC); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
102 } else if (qual == 1) { |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
103 util.dprintln(4, "quality q1"); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
104 interpol = Interpolation.getInstance(Interpolation.INTERP_BILINEAR); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
105 } else { |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
106 util.dprintln(4, "quality q0"); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
107 interpol = Interpolation.getInstance(Interpolation.INTERP_NEAREST); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
108 } |
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 |
73 | 111 /** The width of the curent image in pixel. |
112 * @return Image width in pixels. | |
113 */ | |
114 public int getWidth() { | |
115 if (img != null) { | |
116 return img.getWidth(); | |
117 } | |
118 return 0; | |
119 } | |
1 | 120 |
73 | 121 /** The height of the curent image in pixel. |
122 * @return Image height in pixels. | |
123 */ | |
124 public int getHeight() { | |
125 if (img != null) { | |
126 return img.getHeight(); | |
127 } | |
128 return 0; | |
129 } | |
1 | 130 |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
131 /* scales the current image */ |
149 | 132 public void scale(double scale, double scaleY) throws ImageOpException { |
142 | 133 if ((scale < 1) |
134 && (img.getColorModel().getPixelSize() == 1) | |
135 && (quality > 0)) { | |
136 /* | |
137 * "SubsampleBinaryToGray" for downscaling BW | |
138 */ | |
144 | 139 scaleBinary((float) scale); |
142 | 140 } else if ((scale <= 0.5) && (quality > 1)) { |
141 /* | |
142 * blur and "Scale" for downscaling color images | |
143 */ | |
144 int subsample = (int) Math.floor(1 / scale); | |
144 | 145 blur(subsample); |
146 scaleAll((float) scale); | |
142 | 147 } else { |
148 /* | |
149 * "Scale" for the rest | |
150 */ | |
144 | 151 scaleAll((float) scale); |
142 | 152 } |
1 | 153 |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
154 //DEBUG |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
155 util.dprintln( |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
156 3, |
144 | 157 "SCALE: " + scale + " ->" + img.getWidth() + "x" + img.getHeight()); |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
158 |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
159 } |
1 | 160 |
144 | 161 public void scaleAll(float scale) throws ImageOpException { |
142 | 162 RenderedImage scaledImg; |
163 //DEBUG | |
164 util.dprintln(4, "scaleAll: " + scale); | |
165 ParameterBlockJAI param = new ParameterBlockJAI("Scale"); | |
144 | 166 param.addSource(img); |
142 | 167 param.setParameter("xScale", scale); |
168 param.setParameter("yScale", scale); | |
169 param.setParameter("interpolation", interpol); | |
170 // hint with border extender | |
171 RenderingHints hint = | |
172 new RenderingHints( | |
173 JAI.KEY_BORDER_EXTENDER, | |
174 BorderExtender.createInstance(BorderExtender.BORDER_COPY)); | |
175 // scale | |
176 scaledImg = JAI.create("Scale", param, hint); | |
144 | 177 |
178 if (scaledImg == null) { | |
179 util.dprintln(2, "ERROR(scale): error in scale"); | |
180 throw new ImageOpException("Unable to scale"); | |
181 } | |
182 img = scaledImg; | |
142 | 183 } |
184 | |
144 | 185 public void blur(int radius) throws ImageOpException { |
142 | 186 RenderedImage blurredImg; |
187 //DEBUG | |
188 util.dprintln(4, "blur: " + radius); | |
189 int klen = Math.max(radius, 2); | |
190 int ksize = klen * klen; | |
191 float f = 1f / ksize; | |
192 float[] kern = new float[ksize]; | |
193 for (int i = 0; i < ksize; i++) { | |
194 kern[i] = f; | |
195 } | |
196 KernelJAI blur = new KernelJAI(klen, klen, kern); | |
197 ParameterBlockJAI param = new ParameterBlockJAI("Convolve"); | |
144 | 198 param.addSource(img); |
142 | 199 param.setParameter("kernel", blur); |
200 // hint with border extender | |
201 RenderingHints hint = | |
202 new RenderingHints( | |
203 JAI.KEY_BORDER_EXTENDER, | |
204 BorderExtender.createInstance(BorderExtender.BORDER_COPY)); | |
205 blurredImg = JAI.create("Convolve", param, hint); | |
144 | 206 if (blurredImg == null) { |
207 util.dprintln(2, "ERROR(scale): error in scale"); | |
208 throw new ImageOpException("Unable to scale"); | |
209 } | |
210 img = blurredImg; | |
142 | 211 } |
212 | |
144 | 213 public void scaleBinary(float scale) throws ImageOpException { |
142 | 214 RenderedImage scaledImg; |
215 //DEBUG | |
216 util.dprintln(4, "scaleBinary: " + scale); | |
217 ParameterBlockJAI param = | |
218 new ParameterBlockJAI("SubsampleBinaryToGray"); | |
144 | 219 param.addSource(img); |
142 | 220 param.setParameter("xScale", scale); |
221 param.setParameter("yScale", scale); | |
222 // hint with border extender | |
223 RenderingHints hint = | |
224 new RenderingHints( | |
225 JAI.KEY_BORDER_EXTENDER, | |
226 BorderExtender.createInstance(BorderExtender.BORDER_COPY)); | |
227 // scale | |
228 scaledImg = JAI.create("SubsampleBinaryToGray", param, hint); | |
144 | 229 if (scaledImg == null) { |
230 util.dprintln(2, "ERROR(scale): error in scale"); | |
231 throw new ImageOpException("Unable to scale"); | |
232 } | |
233 img = scaledImg; | |
142 | 234 } |
235 | |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
236 /* crops the current image */ |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
237 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
|
238 throws ImageOpException { |
73 | 239 // setup Crop |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
240 ParameterBlock param = new ParameterBlock(); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
241 param.addSource(img); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
242 param.add((float) x_off); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
243 param.add((float) y_off); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
244 param.add((float) width); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
245 param.add((float) height); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
246 RenderedImage croppedImg = JAI.create("crop", param); |
1 | 247 |
73 | 248 util.dprintln( |
249 3, | |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
250 "CROP: " |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
251 + x_off |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
252 + "," |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
253 + y_off |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
254 + ", " |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
255 + width |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
256 + "," |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
257 + height |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
258 + " ->" |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
259 + croppedImg.getWidth() |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
260 + "x" |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
261 + croppedImg.getHeight()); |
73 | 262 //DEBUG |
263 | |
264 if (croppedImg == null) { | |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
265 util.dprintln(2, "ERROR(crop): error in crop"); |
73 | 266 throw new ImageOpException("Unable to crop"); |
267 } | |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
268 img = croppedImg; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
269 } |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
270 |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
271 /* rotates the current image */ |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
272 public void rotate(double angle) throws ImageOpException { |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
273 RenderedImage rotImg; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
274 // convert degrees to radians |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
275 double rangle = Math.toRadians(angle); |
101 | 276 double x = img.getWidth() / 2; |
277 double y = img.getHeight() / 2; | |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
278 |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
279 // optimize rotation by right angles |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
280 TransposeType rotOp = null; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
281 if (Math.abs(angle - 0) < epsilon) { |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
282 // 0 degree |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
283 return; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
284 } else if (Math.abs(angle - 90) < epsilon) { |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
285 // 90 degree |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
286 rotOp = TransposeDescriptor.ROTATE_90; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
287 } else if (Math.abs(angle - 180) < epsilon) { |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
288 // 180 degree |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
289 rotOp = TransposeDescriptor.ROTATE_180; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
290 } else if (Math.abs(angle - 270) < epsilon) { |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
291 // 270 degree |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
292 rotOp = TransposeDescriptor.ROTATE_270; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
293 } else if (Math.abs(angle - 360) < epsilon) { |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
294 // 360 degree |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
295 return; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
296 } |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
297 if (rotOp != null) { |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
298 // use Transpose operation |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
299 ParameterBlock pb = new ParameterBlock(); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
300 pb.addSource(img); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
301 pb.add(rotOp); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
302 rotImg = JAI.create("transpose", pb); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
303 } else { |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
304 // setup "normal" rotation |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
305 ParameterBlock param = new ParameterBlock(); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
306 param.addSource(img); |
101 | 307 param.add((float) x); |
308 param.add((float) y); | |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
309 param.add((float) rangle); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
310 param.add(interpol); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
311 |
101 | 312 rotImg = JAI.create("rotate", param); |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
313 } |
73 | 314 |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
315 util.dprintln( |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
316 3, |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
317 "ROTATE: " |
85 | 318 + x |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
319 + "," |
85 | 320 + y |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
321 + ", " |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
322 + angle |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
323 + " (" |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
324 + rangle |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
325 + ")" |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
326 + " ->" |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
327 + rotImg.getWidth() |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
328 + "x" |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
329 + rotImg.getHeight()); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
330 //DEBUG |
1 | 331 |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
332 if (rotImg == null) { |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
333 util.dprintln(2, "ERROR: error in rotate"); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
334 throw new ImageOpException("Unable to rotate"); |
73 | 335 } |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
336 img = rotImg; |
73 | 337 } |
1 | 338 |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
339 /* mirrors the current image |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
340 * works only horizontal and vertical |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
341 */ |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
342 public void mirror(double angle) throws ImageOpException { |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
343 RenderedImage mirImg; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
344 // only mirroring by right angles |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
345 TransposeType rotOp = null; |
101 | 346 if (Math.abs(angle) < epsilon) { |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
347 // 0 degree |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
348 rotOp = TransposeDescriptor.FLIP_HORIZONTAL; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
349 } else if (Math.abs(angle - 90) < epsilon) { |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
350 // 90 degree |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
351 rotOp = TransposeDescriptor.FLIP_VERTICAL; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
352 } else if (Math.abs(angle - 180) < epsilon) { |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
353 // 180 degree |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
354 rotOp = TransposeDescriptor.FLIP_HORIZONTAL; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
355 } else if (Math.abs(angle - 270) < epsilon) { |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
356 // 270 degree |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
357 rotOp = TransposeDescriptor.FLIP_VERTICAL; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
358 } else if (Math.abs(angle - 360) < epsilon) { |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
359 // 360 degree |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
360 rotOp = TransposeDescriptor.FLIP_HORIZONTAL; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
361 } |
101 | 362 // use Transpose operation |
363 ParameterBlock param = new ParameterBlock(); | |
364 param.addSource(img); | |
365 param.add(rotOp); | |
366 mirImg = JAI.create("transpose", param); | |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
367 |
101 | 368 if (mirImg == null) { |
369 util.dprintln(2, "ERROR(mirror): error in flip"); | |
370 throw new ImageOpException("Unable to flip"); | |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
371 } |
101 | 372 img = mirImg; |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
373 } |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
374 |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
375 /* contrast and brightness enhancement */ |
86 | 376 public void enhance(float mult, float add) throws ImageOpException { |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
377 RenderedImage enhImg; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
378 double[] ma = { mult }; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
379 double[] aa = { add }; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
380 // use Rescale operation |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
381 ParameterBlock param = new ParameterBlock(); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
382 param.addSource(img); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
383 param.add(ma); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
384 param.add(aa); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
385 enhImg = JAI.create("rescale", param); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
386 |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
387 util.dprintln( |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
388 3, |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
389 "ENHANCE: *" |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
390 + mult |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
391 + ", +" |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
392 + add |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
393 + " ->" |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
394 + enhImg.getWidth() |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
395 + "x" |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
396 + enhImg.getHeight()); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
397 //DEBUG |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
398 |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
399 if (enhImg == null) { |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
400 util.dprintln(2, "ERROR(enhance): error in enhance"); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
401 throw new ImageOpException("Unable to enhance"); |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
402 } |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
403 img = enhImg; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
404 } |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
405 |
90 | 406 /* (non-Javadoc) |
407 * @see digilib.image.DocuImage#enhanceRGB(float[], float[]) | |
408 */ | |
409 public void enhanceRGB(float[] rgbm, float[] rgba) | |
410 throws ImageOpException { | |
101 | 411 RenderedImage enhImg; |
412 int nb = rgbm.length; | |
413 double[] ma = new double[nb]; | |
414 double[] aa = new double[nb]; | |
415 for (int i = 0; i < nb; i++) { | |
416 ma[i] = rgbm[i]; | |
417 aa[i] = rgba[i]; | |
418 } | |
419 // use Rescale operation | |
420 ParameterBlock param = new ParameterBlock(); | |
421 param.addSource(img); | |
422 param.add(ma); | |
423 param.add(aa); | |
424 enhImg = JAI.create("rescale", param); | |
90 | 425 |
101 | 426 util.dprintln( |
427 3, | |
428 "ENHANCE_RGB: *" | |
429 + rgbm | |
430 + ", +" | |
431 + rgba | |
432 + " ->" | |
433 + enhImg.getWidth() | |
434 + "x" | |
435 + enhImg.getHeight()); | |
436 //DEBUG | |
90 | 437 |
101 | 438 if (enhImg == null) { |
439 util.dprintln(2, "ERROR(enhance): error in enhanceRGB"); | |
440 throw new ImageOpException("Unable to enhanceRGB"); | |
441 } | |
442 img = enhImg; | |
90 | 443 } |
444 | |
1 | 445 } |