Mercurial > hg > digilib-old
annotate servlet/src/digilib/servlet/Scaler.java @ 64:5ea1999befd4
New JAI ImageLoader plugin. Currently uses first beta version of the plugin.
Needs Java 1.4.
digilib/io/FileOps.java digilib/servlet/Scaler.java
Modified to use JAIImageLoaderDocuImage.
digilib/image/JAIImageLoaderDocuImage.java
New class JAIImageLoaderDocuImage.
| author | robcast |
|---|---|
| date | Tue, 07 Jan 2003 18:26:06 +0100 |
| parents | 1efc2a994f48 |
| children | 4ef2a86fc1bc |
| rev | line source |
|---|---|
| 1 | 1 /* Scaler -- Scaler servlet main class |
| 2 | |
| 3 Digital Image Library servlet components | |
| 4 | |
| 5 Copyright (C) 2001, 2002 Robert Casties (robcast@mail.berlios.de) | |
| 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.servlet; | |
| 22 | |
| 23 import javax.servlet.*; | |
| 24 import javax.servlet.http.*; | |
| 25 import java.io.*; | |
| 26 import java.util.*; | |
| 27 | |
| 28 import digilib.*; | |
| 29 import digilib.io.*; | |
| 30 import digilib.image.*; | |
| 31 import digilib.auth.*; | |
| 32 | |
|
64
5ea1999befd4
New JAI ImageLoader plugin. Currently uses first beta version of the plugin.
robcast
parents:
62
diff
changeset
|
33 //import tilecachetool.*; |
| 1 | 34 |
| 35 //public class Scaler extends HttpServlet implements SingleThreadModel { | |
| 36 public class Scaler extends HttpServlet { | |
| 37 | |
| 38 // Utils instance with debuglevel | |
| 39 Utils util; | |
| 40 // ServletOpss instance | |
| 41 ServletOps servletOp; | |
| 42 // FileOps instance | |
| 43 FileOps fileOp; | |
| 44 // AuthOps instance | |
| 45 AuthOps authOp; | |
| 46 // global DocuImage instance (don't reuse inside a request!) | |
| 47 DocuImage globalImage; | |
| 48 | |
| 49 // use authorization database | |
| 50 boolean useAuthentication = true; | |
| 51 // image file to send in case of error | |
| 52 File errorImgFile = new File("/docuserver/images/icons/scalerror.gif"); | |
| 53 // image file to send if access is denied | |
| 54 File denyImgFile = new File("/docuserver/images/icons/denied.gif"); | |
| 55 // base directories in order of preference (prescaled versions first) | |
| 56 String[] baseDirs = {"/docuserver/scaled/small", "/docuserver/images", "/docuserver/scans/quellen"}; | |
| 57 | |
| 58 | |
| 59 /********************************************************* | |
| 60 * Initialize global variables | |
| 61 *********************************************************/ | |
| 62 public void init(ServletConfig config) throws ServletException { | |
| 63 super.init(config); | |
| 64 | |
|
64
5ea1999befd4
New JAI ImageLoader plugin. Currently uses first beta version of the plugin.
robcast
parents:
62
diff
changeset
|
65 // Debuggin! |
|
5ea1999befd4
New JAI ImageLoader plugin. Currently uses first beta version of the plugin.
robcast
parents:
62
diff
changeset
|
66 //TCTool tctool = new TCTool(); |
|
5ea1999befd4
New JAI ImageLoader plugin. Currently uses first beta version of the plugin.
robcast
parents:
62
diff
changeset
|
67 |
| 1 | 68 // first we need an Utils to setup ServletOps UGLY!! |
| 69 util = new Utils(5); | |
| 70 // servletOps takes a ServletConfig to get the config file name | |
| 71 servletOp = new ServletOps(util, config); | |
| 72 // then we can start reading parameters UGLY!! | |
| 73 | |
|
59
8d9a0abf3626
Utils now has a setter for debugLevel. DocumentBean now properly sets
robcast
parents:
56
diff
changeset
|
74 // Utils with new debuglevel |
| 1 | 75 int debugLevel = servletOp.tryToGetInitParam("debug-level", 10); |
|
59
8d9a0abf3626
Utils now has a setter for debugLevel. DocumentBean now properly sets
robcast
parents:
56
diff
changeset
|
76 util.setDebugLevel(debugLevel); |
| 1 | 77 // image file to send in case of error |
| 78 String errorImgFileName = servletOp.tryToGetInitParam("error-image", "/docuserver/images/icons/scalerror.gif"); | |
| 79 errorImgFile = new File(errorImgFileName); | |
| 80 // image file to send if access is denied | |
| 81 String denyImgFileName = servletOp.tryToGetInitParam("denied-image", "/docuserver/images/icons/denied.gif"); | |
| 82 denyImgFile = new File(denyImgFileName); | |
| 83 // base directories in order of preference (prescaled versions first) | |
| 84 String baseDirList = servletOp.tryToGetInitParam("basedir-list", "/docuserver/scaled/small:/docuserver/images:/docuserver/scans/quellen"); | |
| 85 // split list into directories | |
|
56
2ea78a56ecf8
Use system specific pathSeparator for documents paths (; on Win).
robcast
parents:
1
diff
changeset
|
86 baseDirs = servletOp.tryToGetPathArray(baseDirList, baseDirs); |
| 1 | 87 // use authentication information |
| 88 String useAuth = servletOp.tryToGetInitParam("use-authorization", "true"); | |
| 62 | 89 if ((useAuth.indexOf("false") > -1)||(useAuth.indexOf("FALSE") > -1)) { |
| 1 | 90 useAuthentication = false; |
| 91 } else { | |
| 92 useAuthentication = true; | |
| 93 try { | |
| 94 // DB version | |
| 95 //authOp = new DBAuthOpsImpl(util); | |
| 96 // XML version | |
| 97 String cnfPath = servletOp.tryToGetInitParam("auth-file", "/docuserver/www/digitallibrary/WEB-INF/digilib-auth.xml"); | |
| 98 authOp = new XMLAuthOps(util, cnfPath); | |
| 99 } catch (AuthOpException e) { | |
| 100 throw new ServletException(e); | |
| 101 } | |
| 102 } | |
| 103 // FileOps instance | |
| 104 fileOp = new FileOps(util); | |
| 105 // global DocuImage instance (don't reuse inside a request!) | |
|
64
5ea1999befd4
New JAI ImageLoader plugin. Currently uses first beta version of the plugin.
robcast
parents:
62
diff
changeset
|
106 //globalImage = new JAIDocuImage(util); |
|
5ea1999befd4
New JAI ImageLoader plugin. Currently uses first beta version of the plugin.
robcast
parents:
62
diff
changeset
|
107 // globalImage = new JIMIDocuImage(util); |
| 1 | 108 //globalImage = new ImageLoaderDocuImage(util); |
|
64
5ea1999befd4
New JAI ImageLoader plugin. Currently uses first beta version of the plugin.
robcast
parents:
62
diff
changeset
|
109 globalImage = new JAIImageLoaderDocuImage(util); |
| 1 | 110 } |
| 111 | |
| 112 /**Process the HTTP Get request*/ | |
| 113 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { | |
| 114 util.dprintln(1, "The servlet has received a GET!"); | |
| 115 processRequest(request, response); | |
| 116 } | |
| 117 | |
| 118 /**Process the HTTP Post request*/ | |
| 119 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { | |
| 120 util.dprintln(1, "The servlet has received a POST!"); | |
| 121 processRequest(request, response); | |
| 122 } | |
| 123 | |
| 124 /**Clean up resources*/ | |
| 125 public void destroy() { | |
| 126 } | |
| 127 | |
| 128 /********************************************************************** | |
| 129 * main request handler | |
| 130 **********************************************************************/ | |
| 131 | |
| 132 void processRequest(HttpServletRequest request, HttpServletResponse response) | |
| 133 throws ServletException, IOException { | |
| 134 | |
| 135 // time for benchmarking | |
| 136 long startTime = System.currentTimeMillis(); | |
| 137 // output mime/type | |
| 138 String mimeType = "image/png"; | |
| 139 | |
| 140 /** | |
| 141 * parameters for a session | |
| 142 */ | |
| 143 | |
| 144 // scale the image file to fit window size | |
| 145 boolean scaleToFit = true; | |
| 146 // use heuristics (GIF?) to scale or not | |
| 147 boolean forcedScale = false; | |
| 148 // try prescaled images first | |
| 149 boolean preScaledFirst = true; | |
| 150 // interpolation to use for scaling | |
| 151 int scaleQual = 0; | |
| 152 // send html error message (or image file) | |
| 153 boolean errorMsgHtml = false; | |
| 154 | |
| 155 /** | |
| 156 * request parameter | |
| 157 */ | |
| 158 | |
| 159 // file/dir to load | |
| 160 String param_fn = servletOp.tryToGetParam("fn", "", request); | |
| 161 // page number | |
| 162 int param_pn = servletOp.tryToGetParam("pn", 1, request); | |
| 163 // destination image width | |
| 164 int param_dw = servletOp.tryToGetParam("dw", 300, request); | |
| 165 // destination image height | |
| 166 int param_dh = servletOp.tryToGetParam("dh", 400, request); | |
| 167 // relative area x_offset (0..1) | |
| 168 float param_wx = servletOp.tryToGetParam("wx", 0f, request); | |
| 169 // relative area y_offset | |
| 170 float param_wy = servletOp.tryToGetParam("wy", 0f, request); | |
| 171 // relative area width (0..1) | |
| 172 float param_ww = servletOp.tryToGetParam("ww", 1f, request); | |
| 173 // relative area height | |
| 174 float param_wh = servletOp.tryToGetParam("wh", 1f, request); | |
| 175 // scale factor (additional to dw/width, dh/height) | |
| 176 float param_ws = servletOp.tryToGetParam("ws", 1f, request); | |
| 177 // operation mode: flags separated by "+" | |
| 178 String param_mo = servletOp.tryToGetParam("mo", "", request); | |
| 179 // operation mode: "fit": always fit to page, "file": send as-is | |
| 180 if (param_mo.indexOf("fit") >= 0) { | |
| 181 scaleToFit = true; | |
| 182 forcedScale = true; | |
| 183 } else if (param_mo.indexOf("file") >= 0) { | |
| 184 scaleToFit = false; | |
| 185 forcedScale = true; | |
| 186 } | |
| 187 // operation mode: "errtxt": error message in html, "errimg": error image | |
| 188 if (param_mo.indexOf("errtxt") >= 0) { | |
| 189 errorMsgHtml = true; | |
| 190 } else if (param_mo.indexOf("errimg") >= 0) { | |
| 191 errorMsgHtml = false; | |
| 192 } | |
| 193 // operation mode: "q0" - "q2": interpolation quality | |
| 194 if (param_mo.indexOf("q0") >= 0) { | |
| 195 scaleQual = 0; | |
| 196 } else if (param_mo.indexOf("q1") >= 0) { | |
| 197 scaleQual = 1; | |
| 198 } else if (param_mo.indexOf("q2") >= 0) { | |
| 199 scaleQual = 2; | |
| 200 } | |
| 201 // operation mode: "lores": try to use scaled image, "hires": unscaled image | |
| 202 if (param_mo.indexOf("lores") >= 0) { | |
| 203 preScaledFirst = true; | |
| 204 } else if (param_mo.indexOf("hires") >= 0) { | |
| 205 preScaledFirst = false; | |
| 206 } | |
| 207 | |
| 208 Utils.dprintln(1, "Parameter values: fn:"+param_fn+" pn:"+param_pn+" dw:"+param_dw+" dh:"+param_dh+" wx:"+param_wx+" wy:"+param_wy+" ww:"+param_ww+" wh:"+param_wh+" ws:"+param_ws+" mo:"+param_mo); | |
| 209 | |
| 210 //"big" try for all file/image actions | |
| 211 try { | |
| 212 | |
| 213 // DocuImage instance | |
|
64
5ea1999befd4
New JAI ImageLoader plugin. Currently uses first beta version of the plugin.
robcast
parents:
62
diff
changeset
|
214 //DocuImage docuImage = new JAIDocuImage(util); |
|
5ea1999befd4
New JAI ImageLoader plugin. Currently uses first beta version of the plugin.
robcast
parents:
62
diff
changeset
|
215 //DocuImage docuImage = new JIMIDocuImage(util); |
| 1 | 216 //DocuImage docuImage = new ImageLoaderDocuImage(util); |
|
64
5ea1999befd4
New JAI ImageLoader plugin. Currently uses first beta version of the plugin.
robcast
parents:
62
diff
changeset
|
217 DocuImage docuImage = new JAIImageLoaderDocuImage(util); |
| 1 | 218 |
| 219 /** | |
| 220 * find the file to load/send | |
| 221 */ | |
| 222 | |
| 223 String loadPathName = ""; | |
| 224 // if there's PathInfo, append | |
| 225 if (request.getPathInfo() != null) { | |
| 226 loadPathName += request.getPathInfo(); | |
| 227 } | |
| 228 // append fn parameter | |
| 229 loadPathName += param_fn; | |
| 230 // if it's zoomed, try hires version (to be optimized...) | |
| 231 if ((param_ww < 1f) || (param_wh < 1f)) { | |
| 232 preScaledFirst = false; | |
| 233 } | |
| 234 | |
| 235 if (useAuthentication) { | |
| 236 // check permissions | |
| 237 List rolesRequired = authOp.rolesForPath(loadPathName, request); | |
| 238 if (rolesRequired != null) { | |
| 239 Utils.dprintln(1, "Role required: "+rolesRequired); | |
| 240 Utils.dprintln(2, "User: "+request.getRemoteUser()); | |
| 241 if (! authOp.isRoleAuthorized(rolesRequired, request)) { | |
| 242 Utils.dprintln(1, "ERROR: access denied!"); | |
| 243 if (errorMsgHtml) { | |
| 244 servletOp.htmlMessage("ERROR: Unauthorized access!", response); | |
| 245 } else { | |
| 246 docuImage.sendFile(denyImgFile, response); | |
| 247 } | |
| 248 return; | |
| 249 } | |
| 250 } | |
| 251 } | |
| 252 | |
| 253 // find the file | |
| 254 File fileToLoad = fileOp.getFileVariant(baseDirs, loadPathName, param_pn, preScaledFirst); | |
| 255 | |
| 256 Utils.dprintln(1, "Loading: "+fileToLoad); | |
| 257 | |
| 258 // get the source image type (if it's known) | |
| 259 mimeType = fileOp.mimeForFile(fileToLoad); | |
| 260 | |
| 261 // if not forced and source is GIF/PNG then send-as-is if not zoomed | |
| 262 if((!forcedScale && (mimeType == "image/gif" || mimeType == "image/png") | |
| 263 && (param_ww == 1f) && (param_wh == 1f)) || (forcedScale && !scaleToFit)) { | |
| 264 | |
| 265 Utils.dprintln(1, "Sending File as is."); | |
| 266 | |
| 267 docuImage.sendFile(fileToLoad, response); | |
| 268 | |
| 269 Utils.dprintln(1, "Done in "+(System.currentTimeMillis()-startTime)+"ms"); | |
| 270 return; | |
| 271 } | |
| 272 | |
| 273 // load file | |
| 274 docuImage.loadImage(fileToLoad); | |
| 275 | |
| 276 /** | |
| 277 * crop and scale the image | |
| 278 */ | |
| 279 | |
| 280 // get size | |
| 281 int imgWidth = docuImage.getWidth(); | |
| 282 int imgHeight = docuImage.getHeight(); | |
| 283 | |
| 284 util.dprintln(2, "IMG: "+imgWidth+"x"+imgHeight); | |
| 285 util.dprintln(2, "time "+(System.currentTimeMillis()-startTime)+"ms"); | |
| 286 | |
| 287 // calculate absolute from relative coordinates | |
| 288 float areaXoff = param_wx * imgWidth; | |
| 289 float areaYoff = param_wy * imgHeight; | |
| 290 float areaWidth = param_ww * imgWidth; | |
| 291 float areaHeight = param_wh * imgHeight; | |
| 292 // calculate scaling factors | |
| 293 float scaleX = param_dw / areaWidth * param_ws; | |
| 294 float scaleY = param_dh / areaHeight * param_ws; | |
| 295 float scaleXY = (scaleX > scaleY) ? scaleY : scaleX; | |
| 296 | |
| 297 util.dprintln(1, "Scale "+scaleXY+"("+scaleX+","+scaleY+") on "+areaXoff+","+areaYoff+" "+areaWidth+"x"+areaHeight); | |
| 298 | |
| 299 // fit area to image | |
| 300 areaWidth = (areaXoff + areaWidth > imgWidth) ? imgWidth - areaXoff : areaWidth; | |
| 301 areaHeight = (areaYoff + areaHeight > imgHeight) ? imgHeight - areaYoff : areaHeight; | |
| 302 | |
| 303 util.dprintln(2, "cropped: "+areaXoff+","+areaYoff+" "+areaWidth+"x"+areaHeight); | |
| 304 | |
| 305 // check image parameters | |
| 306 if ((areaWidth < 1)||(areaHeight < 1) | |
| 307 ||(scaleXY * areaWidth < 2)||(scaleXY * areaHeight < 2)) { | |
| 308 Utils.dprintln(1, "ERROR: invalid scale parameter set!"); | |
| 309 throw new ImageOpException("Invalid scale parameter set!"); | |
| 310 } | |
| 311 | |
| 312 // crop and scale image | |
| 313 docuImage.cropAndScale((int)areaXoff, (int)areaYoff, (int)areaWidth, (int)areaHeight, | |
| 314 scaleXY, scaleQual); | |
| 315 | |
| 316 util.dprintln(2, "time "+(System.currentTimeMillis()-startTime)+"ms"); | |
| 317 | |
| 318 /** | |
| 319 * write the resulting image | |
| 320 */ | |
| 321 | |
| 322 // setup output -- if source is JPG then dest will be JPG else it's PNG | |
| 323 if (mimeType != "image/jpeg") { | |
| 324 mimeType="image/png"; | |
| 325 } | |
| 326 | |
| 327 // write the image | |
| 328 docuImage.writeImage(mimeType, response); | |
| 329 | |
| 330 util.dprintln(1, "Done in "+(System.currentTimeMillis()-startTime)+"ms"); | |
| 331 | |
| 332 /** | |
| 333 * error handling | |
| 334 */ | |
| 335 | |
| 336 }//"big" try | |
| 337 catch (FileOpException e) { | |
| 338 util.dprintln(1, "ERROR: File IO Error: "+e); | |
| 339 try { | |
| 340 if (errorMsgHtml) { | |
| 341 servletOp.htmlMessage("ERROR: File IO Error: "+e, response); | |
| 342 } else { | |
| 343 globalImage.sendFile(errorImgFile, response); | |
| 344 } | |
| 345 } catch (FileOpException ex) {} // so we don't get a loop | |
| 346 return; | |
| 347 } | |
| 348 catch (AuthOpException e) { | |
| 349 Utils.dprintln(1, "ERROR: Authorization error: "+e); | |
| 350 try { | |
| 351 if (errorMsgHtml) { | |
| 352 servletOp.htmlMessage("ERROR: Authorization error: "+e, response); | |
| 353 } else { | |
| 354 globalImage.sendFile(errorImgFile, response); | |
| 355 } | |
| 356 } catch (FileOpException ex) {} // so we don't get a loop | |
| 357 return; | |
| 358 } | |
| 359 catch (ImageOpException e) { | |
| 360 Utils.dprintln(1, "ERROR: Image Error: "+e); | |
| 361 try { | |
| 362 if (errorMsgHtml) { | |
| 363 servletOp.htmlMessage("ERROR: Image Operation Error: "+e, response); | |
| 364 } else { | |
| 365 globalImage.sendFile(errorImgFile, response); | |
| 366 } | |
| 367 } catch (FileOpException ex) {} // so we don't get a loop | |
| 368 return; | |
| 369 } | |
| 370 | |
| 371 } | |
| 372 | |
| 373 }//Scaler class |
