Mercurial > hg > digilib-old
annotate servlet/src/digilib/image/JAIDocuImage.java @ 445:6154dfc67615
pageWidth (not yet working in IE)
| author | hertzhaft |
|---|---|
| date | Fri, 13 Jan 2006 12:31:00 +0100 |
| parents | 460cd1f18b96 |
| children | 03ff7238c9d4 |
| 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 { |
| 208 | 119 logger.debug("scale"); |
| 142 | 120 if ((scale < 1) |
| 121 && (img.getColorModel().getPixelSize() == 1) | |
| 122 && (quality > 0)) { | |
| 123 /* | |
| 124 * "SubsampleBinaryToGray" for downscaling BW | |
| 125 */ | |
| 144 | 126 scaleBinary((float) scale); |
| 142 | 127 } else if ((scale <= 0.5) && (quality > 1)) { |
| 128 /* | |
| 129 * blur and "Scale" for downscaling color images | |
| 130 */ | |
| 131 int subsample = (int) Math.floor(1 / scale); | |
| 144 | 132 blur(subsample); |
| 133 scaleAll((float) scale); | |
| 142 | 134 } else { |
| 135 /* | |
| 136 * "Scale" for the rest | |
| 137 */ | |
| 144 | 138 scaleAll((float) scale); |
| 142 | 139 } |
| 1 | 140 |
|
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
141 //DEBUG |
| 181 | 142 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
|
143 |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
144 } |
| 1 | 145 |
| 144 | 146 public void scaleAll(float scale) throws ImageOpException { |
| 142 | 147 RenderedImage scaledImg; |
| 148 //DEBUG | |
| 181 | 149 logger.debug("scaleAll: " + scale); |
| 142 | 150 ParameterBlockJAI param = new ParameterBlockJAI("Scale"); |
| 144 | 151 param.addSource(img); |
| 142 | 152 param.setParameter("xScale", scale); |
| 153 param.setParameter("yScale", scale); | |
| 154 param.setParameter("interpolation", interpol); | |
| 155 // hint with border extender | |
| 156 RenderingHints hint = | |
| 157 new RenderingHints( | |
| 158 JAI.KEY_BORDER_EXTENDER, | |
| 159 BorderExtender.createInstance(BorderExtender.BORDER_COPY)); | |
| 160 // scale | |
| 161 scaledImg = JAI.create("Scale", param, hint); | |
| 144 | 162 |
| 163 if (scaledImg == null) { | |
| 164 throw new ImageOpException("Unable to scale"); | |
| 165 } | |
| 166 img = scaledImg; | |
| 142 | 167 } |
| 168 | |
| 144 | 169 public void blur(int radius) throws ImageOpException { |
| 142 | 170 RenderedImage blurredImg; |
| 171 //DEBUG | |
| 181 | 172 logger.debug("blur: " + radius); |
| 142 | 173 int klen = Math.max(radius, 2); |
| 174 int ksize = klen * klen; | |
| 175 float f = 1f / ksize; | |
| 176 float[] kern = new float[ksize]; | |
| 177 for (int i = 0; i < ksize; i++) { | |
| 178 kern[i] = f; | |
| 179 } | |
| 180 KernelJAI blur = new KernelJAI(klen, klen, kern); | |
| 181 ParameterBlockJAI param = new ParameterBlockJAI("Convolve"); | |
| 144 | 182 param.addSource(img); |
| 142 | 183 param.setParameter("kernel", blur); |
| 184 // hint with border extender | |
| 185 RenderingHints hint = | |
| 186 new RenderingHints( | |
| 187 JAI.KEY_BORDER_EXTENDER, | |
| 188 BorderExtender.createInstance(BorderExtender.BORDER_COPY)); | |
| 189 blurredImg = JAI.create("Convolve", param, hint); | |
| 144 | 190 if (blurredImg == null) { |
| 191 throw new ImageOpException("Unable to scale"); | |
| 192 } | |
| 193 img = blurredImg; | |
| 142 | 194 } |
| 195 | |
| 144 | 196 public void scaleBinary(float scale) throws ImageOpException { |
| 142 | 197 RenderedImage scaledImg; |
| 198 //DEBUG | |
| 181 | 199 logger.debug("scaleBinary: " + scale); |
| 142 | 200 ParameterBlockJAI param = |
| 201 new ParameterBlockJAI("SubsampleBinaryToGray"); | |
| 144 | 202 param.addSource(img); |
| 142 | 203 param.setParameter("xScale", scale); |
| 204 param.setParameter("yScale", scale); | |
| 205 // hint with border extender | |
| 206 RenderingHints hint = | |
| 207 new RenderingHints( | |
| 208 JAI.KEY_BORDER_EXTENDER, | |
| 209 BorderExtender.createInstance(BorderExtender.BORDER_COPY)); | |
| 210 // scale | |
| 211 scaledImg = JAI.create("SubsampleBinaryToGray", param, hint); | |
| 144 | 212 if (scaledImg == null) { |
| 213 throw new ImageOpException("Unable to scale"); | |
| 214 } | |
| 215 img = scaledImg; | |
| 142 | 216 } |
| 217 | |
|
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
218 /* crops the current image */ |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
219 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
|
220 throws ImageOpException { |
| 73 | 221 // setup Crop |
|
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
222 ParameterBlock param = new ParameterBlock(); |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
223 param.addSource(img); |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
224 param.add((float) x_off); |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
225 param.add((float) y_off); |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
226 param.add((float) width); |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
227 param.add((float) height); |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
228 RenderedImage croppedImg = JAI.create("crop", param); |
| 1 | 229 |
| 181 | 230 logger.debug("CROP: " |
|
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
231 + x_off |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
232 + "," |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
233 + y_off |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
234 + ", " |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
235 + width |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
236 + "," |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
237 + height |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
238 + " ->" |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
239 + croppedImg.getWidth() |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
240 + "x" |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
241 + croppedImg.getHeight()); |
| 73 | 242 //DEBUG |
| 243 | |
| 244 if (croppedImg == null) { | |
| 245 throw new ImageOpException("Unable to crop"); | |
| 246 } | |
|
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
247 img = croppedImg; |
|
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 |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
250 /* rotates the current image */ |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
251 public void rotate(double angle) throws ImageOpException { |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
252 RenderedImage rotImg; |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
253 // convert degrees to radians |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
254 double rangle = Math.toRadians(angle); |
| 101 | 255 double x = img.getWidth() / 2; |
| 256 double y = img.getHeight() / 2; | |
|
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
257 |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
258 // optimize rotation by right angles |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
259 TransposeType rotOp = null; |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
260 if (Math.abs(angle - 0) < epsilon) { |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
261 // 0 degree |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
262 return; |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
263 } else if (Math.abs(angle - 90) < epsilon) { |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
264 // 90 degree |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
265 rotOp = TransposeDescriptor.ROTATE_90; |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
266 } else if (Math.abs(angle - 180) < epsilon) { |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
267 // 180 degree |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
268 rotOp = TransposeDescriptor.ROTATE_180; |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
269 } else if (Math.abs(angle - 270) < epsilon) { |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
270 // 270 degree |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
271 rotOp = TransposeDescriptor.ROTATE_270; |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
272 } else if (Math.abs(angle - 360) < epsilon) { |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
273 // 360 degree |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
274 return; |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
275 } |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
276 if (rotOp != null) { |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
277 // use Transpose operation |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
278 ParameterBlock pb = new ParameterBlock(); |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
279 pb.addSource(img); |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
280 pb.add(rotOp); |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
281 rotImg = JAI.create("transpose", pb); |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
282 } else { |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
283 // setup "normal" rotation |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
284 ParameterBlock param = new ParameterBlock(); |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
285 param.addSource(img); |
| 101 | 286 param.add((float) x); |
| 287 param.add((float) y); | |
|
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
288 param.add((float) rangle); |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
289 param.add(interpol); |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
290 |
| 101 | 291 rotImg = JAI.create("rotate", param); |
|
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
292 } |
| 73 | 293 |
| 181 | 294 logger.debug("ROTATE: " |
| 85 | 295 + x |
|
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
296 + "," |
| 85 | 297 + y |
|
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
298 + ", " |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
299 + angle |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
300 + " (" |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
301 + rangle |
|
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 + " ->" |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
304 + rotImg.getWidth() |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
305 + "x" |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
306 + rotImg.getHeight()); |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
307 //DEBUG |
| 1 | 308 |
|
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
309 if (rotImg == null) { |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
310 throw new ImageOpException("Unable to rotate"); |
| 73 | 311 } |
|
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
312 img = rotImg; |
| 73 | 313 } |
| 1 | 314 |
|
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
315 /* mirrors the current image |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
316 * works only horizontal and vertical |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
317 */ |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
318 public void mirror(double angle) throws ImageOpException { |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
319 RenderedImage mirImg; |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
320 // only mirroring by right angles |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
321 TransposeType rotOp = null; |
| 101 | 322 if (Math.abs(angle) < epsilon) { |
|
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
323 // 0 degree |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
324 rotOp = TransposeDescriptor.FLIP_HORIZONTAL; |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
325 } else if (Math.abs(angle - 90) < epsilon) { |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
326 // 90 degree |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
327 rotOp = TransposeDescriptor.FLIP_VERTICAL; |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
328 } else if (Math.abs(angle - 180) < epsilon) { |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
329 // 180 degree |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
330 rotOp = TransposeDescriptor.FLIP_HORIZONTAL; |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
331 } else if (Math.abs(angle - 270) < epsilon) { |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
332 // 270 degree |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
333 rotOp = TransposeDescriptor.FLIP_VERTICAL; |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
334 } else if (Math.abs(angle - 360) < epsilon) { |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
335 // 360 degree |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
336 rotOp = TransposeDescriptor.FLIP_HORIZONTAL; |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
337 } |
| 101 | 338 // use Transpose operation |
| 339 ParameterBlock param = new ParameterBlock(); | |
| 340 param.addSource(img); | |
| 341 param.add(rotOp); | |
| 342 mirImg = JAI.create("transpose", param); | |
|
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
343 |
| 101 | 344 if (mirImg == null) { |
| 345 throw new ImageOpException("Unable to flip"); | |
|
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
346 } |
| 101 | 347 img = mirImg; |
|
79
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 |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
350 /* contrast and brightness enhancement */ |
| 86 | 351 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
|
352 RenderedImage enhImg; |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
353 double[] ma = { mult }; |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
354 double[] aa = { add }; |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
355 // use Rescale operation |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
356 ParameterBlock param = new ParameterBlock(); |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
357 param.addSource(img); |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
358 param.add(ma); |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
359 param.add(aa); |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
360 enhImg = JAI.create("rescale", param); |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
361 |
| 181 | 362 logger.debug("ENHANCE: *" |
|
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
363 + mult |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
364 + ", +" |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
365 + add |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
366 + " ->" |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
367 + enhImg.getWidth() |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
368 + "x" |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
369 + enhImg.getHeight()); |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
370 //DEBUG |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
371 |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
372 if (enhImg == null) { |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
373 throw new ImageOpException("Unable to enhance"); |
|
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 img = enhImg; |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
376 } |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
377 |
| 90 | 378 /* (non-Javadoc) |
| 379 * @see digilib.image.DocuImage#enhanceRGB(float[], float[]) | |
| 380 */ | |
| 381 public void enhanceRGB(float[] rgbm, float[] rgba) | |
| 382 throws ImageOpException { | |
| 101 | 383 RenderedImage enhImg; |
| 384 int nb = rgbm.length; | |
| 385 double[] ma = new double[nb]; | |
| 386 double[] aa = new double[nb]; | |
| 387 for (int i = 0; i < nb; i++) { | |
| 388 ma[i] = rgbm[i]; | |
| 389 aa[i] = rgba[i]; | |
| 390 } | |
| 391 // use Rescale operation | |
| 392 ParameterBlock param = new ParameterBlock(); | |
| 393 param.addSource(img); | |
| 394 param.add(ma); | |
| 395 param.add(aa); | |
| 396 enhImg = JAI.create("rescale", param); | |
| 90 | 397 |
| 181 | 398 logger.debug("ENHANCE_RGB: *" |
| 101 | 399 + rgbm |
| 400 + ", +" | |
| 401 + rgba | |
| 402 + " ->" | |
| 403 + enhImg.getWidth() | |
| 404 + "x" | |
| 405 + enhImg.getHeight()); | |
| 406 //DEBUG | |
| 90 | 407 |
| 101 | 408 if (enhImg == null) { |
| 409 throw new ImageOpException("Unable to enhanceRGB"); | |
| 410 } | |
| 411 img = enhImg; | |
| 90 | 412 } |
| 413 | |
| 208 | 414 /* (non-Javadoc) |
| 415 * @see digilib.image.DocuImage#dispose() | |
| 416 */ | |
| 417 public void dispose() { | |
| 418 img = null; | |
| 419 } | |
| 420 | |
| 1 | 421 } |
