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