Mercurial > hg > digilib-old
annotate servlet/src/digilib/image/JAIDocuImage.java @ 140:c878ea574c29
Servlet Version 1.12b2
- fixed small error in XML config parser
- improved high quality scaling with minSubsample parameter
| author | robcast |
|---|---|
| date | Fri, 15 Aug 2003 23:52:20 +0200 |
| parents | 78f52a1876fe |
| children | f52a7664a807 |
| 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.File; | |
| 27 import java.io.IOException; | |
|
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
28 import java.io.OutputStream; |
| 1 | 29 |
|
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
30 import javax.media.jai.*; |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
31 import javax.media.jai.operator.TransposeDescriptor; |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
32 import javax.media.jai.operator.TransposeType; |
| 1 | 33 |
| 73 | 34 import digilib.Utils; |
| 35 import digilib.io.FileOpException; | |
| 1 | 36 |
| 73 | 37 /** A DocuImage implementation using Java Advanced Imaging Library. */ |
| 1 | 38 public class JAIDocuImage extends DocuImageImpl { |
| 39 | |
|
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
40 protected RenderedImage img; |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
41 protected Interpolation interpol = null; |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
42 |
| 73 | 43 /** Default constructor. */ |
| 44 public JAIDocuImage() { | |
| 45 } | |
| 1 | 46 |
| 90 | 47 /** Contructor taking an utils object. |
| 73 | 48 * @param u utils object. |
| 49 */ | |
| 50 public JAIDocuImage(Utils u) { | |
| 51 util = u; | |
| 52 } | |
| 1 | 53 |
|
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
54 /* Load an image file into the Object. */ |
| 73 | 55 public void loadImage(File f) throws FileOpException { |
| 56 System.gc(); | |
| 57 img = JAI.create("fileload", f.getAbsolutePath()); | |
| 58 if (img == null) { | |
| 59 util.dprintln(3, "ERROR(loadImage): unable to load file"); | |
| 60 throw new FileOpException("Unable to load File!"); | |
| 61 } | |
| 62 } | |
| 1 | 63 |
|
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
64 /* Write the current image to an OutputStream. */ |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
65 public void writeImage(String mt, OutputStream ostream) |
| 73 | 66 throws FileOpException { |
| 67 try { | |
| 68 // setup output | |
| 69 ParameterBlock pb3 = new ParameterBlock(); | |
| 70 pb3.addSource(img); | |
|
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
71 pb3.add(ostream); |
| 73 | 72 if (mt == "image/jpeg") { |
| 73 pb3.add("JPEG"); | |
| 74 } else if (mt == "image/png") { | |
| 75 pb3.add("PNG"); | |
| 76 } else { | |
| 77 // unknown mime type | |
| 78 util.dprintln(2, "ERROR(writeImage): Unknown mime type " + mt); | |
| 79 throw new FileOpException("Unknown mime type: " + mt); | |
| 80 } | |
| 81 // render output | |
| 82 JAI.create("encode", pb3); | |
| 1 | 83 |
| 73 | 84 } catch (IOException e) { |
| 85 throw new FileOpException("Error writing image."); | |
| 86 } | |
| 87 } | |
| 1 | 88 |
|
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
89 /* Real setQuality implementation. |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
90 * Creates the correct Interpolation. |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
91 */ |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
92 public void setQuality(int qual) { |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
93 quality = qual; |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
94 // setup interpolation quality |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
95 if (qual > 1) { |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
96 util.dprintln(4, "quality q2"); |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
97 interpol = Interpolation.getInstance(Interpolation.INTERP_BICUBIC); |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
98 } else if (qual == 1) { |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
99 util.dprintln(4, "quality q1"); |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
100 interpol = Interpolation.getInstance(Interpolation.INTERP_BILINEAR); |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
101 } else { |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
102 util.dprintln(4, "quality q0"); |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
103 interpol = Interpolation.getInstance(Interpolation.INTERP_NEAREST); |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
104 } |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
105 } |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
106 |
| 73 | 107 /** The width of the curent image in pixel. |
| 108 * @return Image width in pixels. | |
| 109 */ | |
| 110 public int getWidth() { | |
| 111 if (img != null) { | |
| 112 return img.getWidth(); | |
| 113 } | |
| 114 return 0; | |
| 115 } | |
| 1 | 116 |
| 73 | 117 /** The height of the curent image in pixel. |
| 118 * @return Image height in pixels. | |
| 119 */ | |
| 120 public int getHeight() { | |
| 121 if (img != null) { | |
| 122 return img.getHeight(); | |
| 123 } | |
| 124 return 0; | |
| 125 } | |
| 1 | 126 |
|
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
127 /* scales the current image */ |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
128 public void scale(double scale) throws ImageOpException { |
| 101 | 129 float sf = (float) scale; |
|
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
130 // setup scale |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
131 ParameterBlock param = new ParameterBlock(); |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
132 param.addSource(img); |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
133 param.add(sf); |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
134 param.add(sf); |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
135 param.add(0f); |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
136 param.add(0f); |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
137 param.add(interpol); |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
138 // hint with border extender |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
139 RenderingHints hint = |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
140 new RenderingHints( |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
141 JAI.KEY_BORDER_EXTENDER, |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
142 BorderExtender.createInstance(BorderExtender.BORDER_COPY)); |
|
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 RenderedImage scaledImg = JAI.create("scale", param, hint); |
| 1 | 145 |
|
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
146 //DEBUG |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
147 util.dprintln( |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
148 3, |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
149 "SCALE: " |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
150 + scale |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
151 + " ->" |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
152 + scaledImg.getWidth() |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
153 + "x" |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
154 + scaledImg.getHeight()); |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
155 |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
156 if (scaledImg == null) { |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
157 util.dprintln(2, "ERROR(scale): error in scale"); |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
158 throw new ImageOpException("Unable to scale"); |
| 73 | 159 } |
|
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
160 img = scaledImg; |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
161 } |
| 1 | 162 |
|
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
163 /* crops the current image */ |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
164 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
|
165 throws ImageOpException { |
| 73 | 166 // setup Crop |
|
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
167 ParameterBlock param = new ParameterBlock(); |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
168 param.addSource(img); |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
169 param.add((float) x_off); |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
170 param.add((float) y_off); |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
171 param.add((float) width); |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
172 param.add((float) height); |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
173 RenderedImage croppedImg = JAI.create("crop", param); |
| 1 | 174 |
| 73 | 175 util.dprintln( |
| 176 3, | |
|
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
177 "CROP: " |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
178 + x_off |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
179 + "," |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
180 + y_off |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
181 + ", " |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
182 + width |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
183 + "," |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
184 + height |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
185 + " ->" |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
186 + croppedImg.getWidth() |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
187 + "x" |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
188 + croppedImg.getHeight()); |
| 73 | 189 //DEBUG |
| 190 | |
| 191 if (croppedImg == null) { | |
|
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
192 util.dprintln(2, "ERROR(crop): error in crop"); |
| 73 | 193 throw new ImageOpException("Unable to crop"); |
| 194 } | |
|
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
195 img = croppedImg; |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
196 } |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
197 |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
198 /* rotates the current image */ |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
199 public void rotate(double angle) throws ImageOpException { |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
200 RenderedImage rotImg; |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
201 // convert degrees to radians |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
202 double rangle = Math.toRadians(angle); |
| 101 | 203 double x = img.getWidth() / 2; |
| 204 double y = img.getHeight() / 2; | |
|
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
205 |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
206 // optimize rotation by right angles |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
207 TransposeType rotOp = null; |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
208 if (Math.abs(angle - 0) < epsilon) { |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
209 // 0 degree |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
210 return; |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
211 } else if (Math.abs(angle - 90) < epsilon) { |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
212 // 90 degree |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
213 rotOp = TransposeDescriptor.ROTATE_90; |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
214 } else if (Math.abs(angle - 180) < epsilon) { |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
215 // 180 degree |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
216 rotOp = TransposeDescriptor.ROTATE_180; |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
217 } else if (Math.abs(angle - 270) < epsilon) { |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
218 // 270 degree |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
219 rotOp = TransposeDescriptor.ROTATE_270; |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
220 } else if (Math.abs(angle - 360) < epsilon) { |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
221 // 360 degree |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
222 return; |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
223 } |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
224 if (rotOp != null) { |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
225 // use Transpose operation |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
226 ParameterBlock pb = new ParameterBlock(); |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
227 pb.addSource(img); |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
228 pb.add(rotOp); |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
229 rotImg = JAI.create("transpose", pb); |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
230 } else { |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
231 // setup "normal" rotation |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
232 ParameterBlock param = new ParameterBlock(); |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
233 param.addSource(img); |
| 101 | 234 param.add((float) x); |
| 235 param.add((float) y); | |
|
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
236 param.add((float) rangle); |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
237 param.add(interpol); |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
238 |
| 101 | 239 rotImg = JAI.create("rotate", param); |
|
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
240 } |
| 73 | 241 |
|
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
242 util.dprintln( |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
243 3, |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
244 "ROTATE: " |
| 85 | 245 + x |
|
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
246 + "," |
| 85 | 247 + y |
|
79
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 + angle |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
250 + " (" |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
251 + rangle |
|
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 + " ->" |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
254 + rotImg.getWidth() |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
255 + "x" |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
256 + rotImg.getHeight()); |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
257 //DEBUG |
| 1 | 258 |
|
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
259 if (rotImg == null) { |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
260 util.dprintln(2, "ERROR: error in rotate"); |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
261 throw new ImageOpException("Unable to rotate"); |
| 73 | 262 } |
|
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
263 img = rotImg; |
| 73 | 264 } |
| 1 | 265 |
|
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
266 /* mirrors the current image |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
267 * works only horizontal and vertical |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
268 */ |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
269 public void mirror(double angle) throws ImageOpException { |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
270 RenderedImage mirImg; |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
271 // only mirroring by right angles |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
272 TransposeType rotOp = null; |
| 101 | 273 if (Math.abs(angle) < epsilon) { |
|
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
274 // 0 degree |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
275 rotOp = TransposeDescriptor.FLIP_HORIZONTAL; |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
276 } else if (Math.abs(angle - 90) < epsilon) { |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
277 // 90 degree |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
278 rotOp = TransposeDescriptor.FLIP_VERTICAL; |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
279 } else if (Math.abs(angle - 180) < epsilon) { |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
280 // 180 degree |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
281 rotOp = TransposeDescriptor.FLIP_HORIZONTAL; |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
282 } else if (Math.abs(angle - 270) < epsilon) { |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
283 // 270 degree |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
284 rotOp = TransposeDescriptor.FLIP_VERTICAL; |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
285 } else if (Math.abs(angle - 360) < epsilon) { |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
286 // 360 degree |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
287 rotOp = TransposeDescriptor.FLIP_HORIZONTAL; |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
288 } |
| 101 | 289 // use Transpose operation |
| 290 ParameterBlock param = new ParameterBlock(); | |
| 291 param.addSource(img); | |
| 292 param.add(rotOp); | |
| 293 mirImg = JAI.create("transpose", param); | |
|
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
294 |
| 101 | 295 if (mirImg == null) { |
| 296 util.dprintln(2, "ERROR(mirror): error in flip"); | |
| 297 throw new ImageOpException("Unable to flip"); | |
|
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
298 } |
| 101 | 299 img = mirImg; |
|
79
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 |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
302 /* contrast and brightness enhancement */ |
| 86 | 303 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
|
304 RenderedImage enhImg; |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
305 double[] ma = { mult }; |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
306 double[] aa = { add }; |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
307 // use Rescale operation |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
308 ParameterBlock param = new ParameterBlock(); |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
309 param.addSource(img); |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
310 param.add(ma); |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
311 param.add(aa); |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
312 enhImg = JAI.create("rescale", param); |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
313 |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
314 util.dprintln( |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
315 3, |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
316 "ENHANCE: *" |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
317 + mult |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
318 + ", +" |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
319 + add |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
320 + " ->" |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
321 + enhImg.getWidth() |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
322 + "x" |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
323 + enhImg.getHeight()); |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
324 //DEBUG |
|
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 if (enhImg == null) { |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
327 util.dprintln(2, "ERROR(enhance): error in enhance"); |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
328 throw new ImageOpException("Unable to enhance"); |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
329 } |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
330 img = enhImg; |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
331 } |
|
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
332 |
| 90 | 333 /* (non-Javadoc) |
| 334 * @see digilib.image.DocuImage#enhanceRGB(float[], float[]) | |
| 335 */ | |
| 336 public void enhanceRGB(float[] rgbm, float[] rgba) | |
| 337 throws ImageOpException { | |
| 101 | 338 RenderedImage enhImg; |
| 339 int nb = rgbm.length; | |
| 340 double[] ma = new double[nb]; | |
| 341 double[] aa = new double[nb]; | |
| 342 for (int i = 0; i < nb; i++) { | |
| 343 ma[i] = rgbm[i]; | |
| 344 aa[i] = rgba[i]; | |
| 345 } | |
| 346 // use Rescale operation | |
| 347 ParameterBlock param = new ParameterBlock(); | |
| 348 param.addSource(img); | |
| 349 param.add(ma); | |
| 350 param.add(aa); | |
| 351 enhImg = JAI.create("rescale", param); | |
| 90 | 352 |
| 101 | 353 util.dprintln( |
| 354 3, | |
| 355 "ENHANCE_RGB: *" | |
| 356 + rgbm | |
| 357 + ", +" | |
| 358 + rgba | |
| 359 + " ->" | |
| 360 + enhImg.getWidth() | |
| 361 + "x" | |
| 362 + enhImg.getHeight()); | |
| 363 //DEBUG | |
| 90 | 364 |
| 101 | 365 if (enhImg == null) { |
| 366 util.dprintln(2, "ERROR(enhance): error in enhanceRGB"); | |
| 367 throw new ImageOpException("Unable to enhanceRGB"); | |
| 368 } | |
| 369 img = enhImg; | |
| 90 | 370 } |
| 371 | |
| 1 | 372 } |
