Mercurial > hg > digilib-old
annotate servlet/src/digilib/servlet/Scaler.java @ 335:9bce7008a514
Servlet version 1.5.3b
- new getLastModified improves local caching of pages
author | robcast |
---|---|
date | Wed, 03 Nov 2004 14:22:27 +0100 |
parents | 9db80eb0f50b |
children | e1c10653f143 |
rev | line source |
---|---|
181 | 1 /* |
2 * Scaler -- Scaler servlet main class | |
3 * | |
4 * Digital Image Library servlet components | |
5 * | |
303 | 6 * Copyright (C) 200-2004 Robert Casties (robcast@mail.berlios.de) |
181 | 7 * |
8 * This program is free software; you can redistribute it and/or modify it | |
9 * under the terms of the GNU General Public License as published by the Free | |
10 * Software Foundation; either version 2 of the License, or (at your option) | |
11 * any later version. | |
12 * | |
13 * Please read license.txt for the full details. A copy of the GPL may be found | |
14 * at http://www.gnu.org/copyleft/lgpl.html | |
15 * | |
16 * You should have received a copy of the GNU General Public License along with | |
17 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple | |
18 * Place, Suite 330, Boston, MA 02111-1307 USA | |
19 * | |
20 */ | |
1 | 21 |
22 package digilib.servlet; | |
23 | |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
24 import java.awt.geom.AffineTransform; |
100 | 25 import java.awt.geom.NoninvertibleTransformException; |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
26 import java.awt.geom.Rectangle2D; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
27 import java.io.File; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
28 import java.io.IOException; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
29 import java.util.List; |
1 | 30 |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
31 import javax.servlet.ServletConfig; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
32 import javax.servlet.ServletContext; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
33 import javax.servlet.ServletException; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
34 import javax.servlet.http.HttpServlet; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
35 import javax.servlet.http.HttpServletRequest; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
36 import javax.servlet.http.HttpServletResponse; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
37 |
181 | 38 import org.apache.log4j.Logger; |
39 | |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
40 import digilib.auth.AuthOpException; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
41 import digilib.auth.AuthOps; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
42 import digilib.image.ImageOpException; |
284 | 43 import digilib.image.ImageOps; |
149 | 44 import digilib.image.ImageSize; |
86 | 45 import digilib.io.DocuDirCache; |
335 | 46 import digilib.io.DocuDirectory; |
47 import digilib.io.DocuDirent; | |
181 | 48 import digilib.io.FileOpException; |
49 import digilib.io.FileOps; | |
161 | 50 import digilib.io.ImageFile; |
51 import digilib.io.ImageFileset; | |
1 | 52 |
73 | 53 /** |
181 | 54 * @author casties |
55 */ | |
1 | 56 //public class Scaler extends HttpServlet implements SingleThreadModel { |
57 public class Scaler extends HttpServlet { | |
58 | |
263 | 59 private static final long serialVersionUID = -325080527268912852L; |
60 | |
298
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
61 /** digilib servlet version (for all components) */ |
335 | 62 public static final String dlVersion = "1.5.3b"; |
298
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
63 |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
64 /** logger for accounting requests */ |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
65 private static Logger accountlog = Logger.getLogger("account.request"); |
335 | 66 |
298
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
67 /** gengeral logger for this class */ |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
68 private static Logger logger = Logger.getLogger("digilib.servlet"); |
335 | 69 |
298
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
70 /** logger for authentication related */ |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
71 private static Logger authlog = Logger.getLogger("digilib.auth"); |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
72 |
298
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
73 /** general error code */ |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
74 public static final int ERROR_UNKNOWN = 0; |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
75 |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
76 /** error code for authentication error */ |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
77 public static final int ERROR_AUTH = 1; |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
78 |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
79 /** error code for file operation error */ |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
80 public static final int ERROR_FILE = 2; |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
81 |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
82 /** error code for image operation error */ |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
83 public static final int ERROR_IMAGE = 3; |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
84 |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
85 /** DocuDirCache instance */ |
86 | 86 DocuDirCache dirCache; |
161 | 87 |
298
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
88 /** authentication error image file */ |
153 | 89 File denyImgFile; |
298
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
90 |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
91 /** image error image file */ |
153 | 92 File errorImgFile; |
298
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
93 |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
94 /** subsampling before scaling */ |
153 | 95 float minSubsample = 2f; |
298
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
96 |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
97 /** send files as is? */ |
153 | 98 boolean sendFileAllowed = true; |
298
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
99 |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
100 /** default scaling quality */ |
153 | 101 int defaultQuality = 1; |
1 | 102 |
298
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
103 /** DigilibConfiguration instance */ |
73 | 104 DigilibConfiguration dlConfig; |
1 | 105 |
298
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
106 /** use authorization database */ |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
107 boolean useAuthorization = true; |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
108 |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
109 /** AuthOps instance */ |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
110 AuthOps authOp; |
1 | 111 |
100 | 112 // EXPRIMENTAL |
298
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
113 /** try to enlarge cropping area for "oblique" angles */ |
100 | 114 boolean wholeRotArea = false; |
115 | |
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
161
diff
changeset
|
116 /** |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
161
diff
changeset
|
117 * Initialisation on first run. |
73 | 118 * |
119 * @see javax.servlet.Servlet#init(javax.servlet.ServletConfig) | |
120 */ | |
121 public void init(ServletConfig config) throws ServletException { | |
122 super.init(config); | |
1 | 123 |
298
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
124 System.out |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
125 .println("***** Digital Image Library Image Scaler Servlet (version " |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
126 + dlVersion + ") *****"); |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
127 // say hello in the log file |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
128 logger |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
129 .info("***** Digital Image Library Image Scaler Servlet (version " |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
130 + dlVersion + ") *****"); |
1 | 131 |
73 | 132 // get our ServletContext |
133 ServletContext context = config.getServletContext(); | |
134 // see if there is a Configuration instance | |
298
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
135 dlConfig = (DigilibConfiguration) context |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
136 .getAttribute("digilib.servlet.configuration"); |
73 | 137 if (dlConfig == null) { |
298
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
138 // no Configuration |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
139 throw new ServletException("No Configuration!"); |
335 | 140 } |
73 | 141 // set our AuthOps |
298
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
142 useAuthorization = dlConfig.getAsBoolean("use-authorization"); |
153 | 143 authOp = (AuthOps) dlConfig.getValue("servlet.auth.op"); |
181 | 144 |
86 | 145 // DocuDirCache instance |
153 | 146 dirCache = (DocuDirCache) dlConfig.getValue("servlet.dir.cache"); |
298
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
147 denyImgFile = (File) dlConfig.getValue("denied-image"); |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
148 errorImgFile = (File) dlConfig.getValue("error-image"); |
153 | 149 sendFileAllowed = dlConfig.getAsBoolean("sendfile-allowed"); |
150 minSubsample = dlConfig.getAsFloat("subsample-minimum"); | |
151 defaultQuality = dlConfig.getAsInt("default-quality"); | |
73 | 152 } |
1 | 153 |
181 | 154 /** Process the HTTP Get request */ |
73 | 155 public void doGet(HttpServletRequest request, HttpServletResponse response) |
335 | 156 throws ServletException, IOException { |
157 accountlog.info("GET from " + request.getRemoteAddr()); | |
73 | 158 // create new request with defaults |
159 DigilibRequest dlReq = new DigilibRequest(); | |
160 // set with request parameters | |
161 dlReq.setWithRequest(request); | |
162 // add DigilibRequest to ServletRequest | |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
163 request.setAttribute("digilib.servlet.request", dlReq); |
73 | 164 // do the processing |
165 processRequest(request, response); | |
166 } | |
1 | 167 |
181 | 168 /** Process the HTTP Post request */ |
298
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
169 public void doPost(HttpServletRequest request, HttpServletResponse response) |
335 | 170 throws ServletException, IOException { |
171 accountlog.info("POST from " + request.getRemoteAddr()); | |
73 | 172 // create new request with defaults |
173 DigilibRequest dlReq = new DigilibRequest(); | |
174 // set with request parameters | |
175 dlReq.setWithRequest(request); | |
176 // add DigilibRequest to ServletRequest | |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
177 request.setAttribute("digilib.servlet.request", dlReq); |
73 | 178 // do the processing |
179 processRequest(request, response); | |
180 } | |
181 | |
335 | 182 /* |
183 * (non-Javadoc) | |
184 * | |
185 * @see javax.servlet.http.HttpServlet#getLastModified(javax.servlet.http.HttpServletRequest) | |
186 */ | |
187 protected long getLastModified(HttpServletRequest request) { | |
188 accountlog.debug("GetLastModified from " + request.getRemoteAddr()); | |
189 long mtime = -1; | |
190 // create new request with defaults | |
191 DigilibRequest dlReq = new DigilibRequest(); | |
192 // set with request parameters | |
193 dlReq.setWithRequest(request); | |
194 // find the requested file | |
195 DocuDirent f = findFile(dlReq); | |
196 if (f != null) { | |
197 DocuDirectory dd = (DocuDirectory) f.getParent(); | |
198 mtime = dd.getDirMTime() / 1000 * 1000; | |
199 } | |
200 logger.debug("last modified: "+mtime); | |
201 return mtime; | |
202 } | |
203 | |
73 | 204 /** main request handler. */ |
298
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
205 void processRequest(HttpServletRequest request, HttpServletResponse response) |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
206 throws ServletException { |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
207 |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
208 if (dlConfig == null) { |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
209 throw new ServletException("ERROR: No Configuration!"); |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
210 } |
1 | 211 |
335 | 212 accountlog.debug("request: " + request.getQueryString()); |
213 logger.debug("request: " + request.getQueryString()); | |
214 | |
73 | 215 // time for benchmarking |
216 long startTime = System.currentTimeMillis(); | |
335 | 217 // output mime-type |
73 | 218 String mimeType = "image/png"; |
1 | 219 |
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
161
diff
changeset
|
220 /* parameters for a session */ |
1 | 221 |
73 | 222 // scale the image file to fit window size i.e. respect dw,dh |
223 boolean scaleToFit = true; | |
100 | 224 // scale the image by a fixed factor only |
225 boolean absoluteScale = false; | |
122 | 226 // only crop the image to fit |
227 boolean cropToFit = false; | |
149 | 228 // send the file as is |
229 boolean sendFile = false; | |
230 // use low resolution images only | |
231 boolean loresOnly = false; | |
232 // use hires images only | |
122 | 233 boolean hiresOnly = false; |
303 | 234 // send the image always as JPEG |
235 boolean forceJPEG = false; | |
73 | 236 // interpolation to use for scaling |
153 | 237 int scaleQual = defaultQuality; |
73 | 238 // send html error message (or image file) |
239 boolean errorMsgHtml = false; | |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
240 // mirror the image |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
241 boolean doMirror = false; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
242 // angle of mirror axis |
298
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
243 float mirrorAngle = 0; |
130 | 244 // original (hires) image resolution |
298
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
245 float origResX = 0; |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
246 float origResY = 0; |
1 | 247 |
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
161
diff
changeset
|
248 /* request parameters */ |
73 | 249 |
298
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
250 DigilibRequest dlRequest = (DigilibRequest) request |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
251 .getAttribute("digilib.servlet.request"); |
1 | 252 |
73 | 253 // destination image width |
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
161
diff
changeset
|
254 int paramDW = dlRequest.getAsInt("dw"); |
73 | 255 // destination image height |
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
161
diff
changeset
|
256 int paramDH = dlRequest.getAsInt("dh"); |
73 | 257 // relative area x_offset (0..1) |
298
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
258 float paramWX = dlRequest.getAsFloat("wx"); |
73 | 259 // relative area y_offset |
298
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
260 float paramWY = dlRequest.getAsFloat("wy"); |
73 | 261 // relative area width (0..1) |
298
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
262 float paramWW = dlRequest.getAsFloat("ww"); |
73 | 263 // relative area height |
298
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
264 float paramWH = dlRequest.getAsFloat("wh"); |
73 | 265 // scale factor (additional to dw/width, dh/height) |
298
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
266 float paramWS = dlRequest.getAsFloat("ws"); |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
267 // rotation angle |
298
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
268 float paramROT = dlRequest.getAsFloat("rot"); |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
269 // contrast enhancement |
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
161
diff
changeset
|
270 float paramCONT = dlRequest.getAsFloat("cont"); |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
271 // brightness enhancement |
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
161
diff
changeset
|
272 float paramBRGT = dlRequest.getAsFloat("brgt"); |
86 | 273 // color modification |
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
161
diff
changeset
|
274 float[] paramRGBM = null; |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
161
diff
changeset
|
275 Parameter p = dlRequest.get("rgbm"); |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
161
diff
changeset
|
276 if (p.hasValue() && (!p.getAsString().equals("0/0/0"))) { |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
161
diff
changeset
|
277 paramRGBM = p.parseAsFloatArray("/"); |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
161
diff
changeset
|
278 } |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
161
diff
changeset
|
279 float[] paramRGBA = null; |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
161
diff
changeset
|
280 p = dlRequest.get("rgba"); |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
161
diff
changeset
|
281 if (p.hasValue() && (!p.getAsString().equals("0/0/0"))) { |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
161
diff
changeset
|
282 paramRGBA = p.parseAsFloatArray("/"); |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
161
diff
changeset
|
283 } |
130 | 284 // destination resolution (DPI) |
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
161
diff
changeset
|
285 float paramDDPIX = dlRequest.getAsFloat("ddpix"); |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
161
diff
changeset
|
286 float paramDDPIY = dlRequest.getAsFloat("ddpiy"); |
130 | 287 if ((paramDDPIX == 0) || (paramDDPIY == 0)) { |
288 // if X or Y resolution isn't set, use DDPI | |
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
161
diff
changeset
|
289 paramDDPIX = dlRequest.getAsFloat("ddpi"); |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
161
diff
changeset
|
290 paramDDPIY = paramDDPIX; |
130 | 291 } |
1 | 292 |
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
161
diff
changeset
|
293 /* |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
161
diff
changeset
|
294 * operation mode: "fit": always fit to page, "clip": send original |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
161
diff
changeset
|
295 * resolution cropped, "file": send whole file (if allowed) |
73 | 296 */ |
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
161
diff
changeset
|
297 if (dlRequest.hasOption("mo", "clip")) { |
73 | 298 scaleToFit = false; |
100 | 299 absoluteScale = false; |
73 | 300 cropToFit = true; |
149 | 301 sendFile = false; |
302 hiresOnly = true; | |
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
161
diff
changeset
|
303 } else if (dlRequest.hasOption("mo", "fit")) { |
73 | 304 scaleToFit = true; |
100 | 305 absoluteScale = false; |
122 | 306 cropToFit = false; |
149 | 307 sendFile = false; |
308 hiresOnly = false; | |
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
161
diff
changeset
|
309 } else if (dlRequest.hasOption("mo", "osize")) { |
100 | 310 scaleToFit = false; |
311 absoluteScale = true; | |
122 | 312 cropToFit = false; |
149 | 313 sendFile = false; |
122 | 314 hiresOnly = true; |
73 | 315 } |
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
161
diff
changeset
|
316 // operation mode: "lores": try to use scaled image, "hires": use |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
161
diff
changeset
|
317 // unscaled image |
149 | 318 // "autores": try best fitting resolution |
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
161
diff
changeset
|
319 if (dlRequest.hasOption("mo", "lores")) { |
149 | 320 loresOnly = true; |
321 hiresOnly = false; | |
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
161
diff
changeset
|
322 } else if (dlRequest.hasOption("mo", "hires")) { |
149 | 323 loresOnly = false; |
324 hiresOnly = true; | |
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
161
diff
changeset
|
325 } else if (dlRequest.hasOption("mo", "autores")) { |
149 | 326 loresOnly = false; |
327 hiresOnly = false; | |
328 } | |
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
161
diff
changeset
|
329 // operation mode: "errtxt": error message in html, "errimg": error |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
161
diff
changeset
|
330 // image |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
161
diff
changeset
|
331 if (dlRequest.hasOption("mo", "errtxt")) { |
73 | 332 errorMsgHtml = true; |
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
161
diff
changeset
|
333 } else if (dlRequest.hasOption("mo", "errimg")) { |
73 | 334 errorMsgHtml = false; |
335 } | |
336 // operation mode: "q0" - "q2": interpolation quality | |
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
161
diff
changeset
|
337 if (dlRequest.hasOption("mo", "q0")) { |
73 | 338 scaleQual = 0; |
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
161
diff
changeset
|
339 } else if (dlRequest.hasOption("mo", "q1")) { |
73 | 340 scaleQual = 1; |
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
161
diff
changeset
|
341 } else if (dlRequest.hasOption("mo", "q2")) { |
73 | 342 scaleQual = 2; |
343 } | |
303 | 344 // operation mode: "jpg": always use JPEG |
345 if (dlRequest.hasOption("mo", "jpg")) { | |
346 forceJPEG = true; | |
335 | 347 } |
1 | 348 |
185 | 349 // check with the maximum allowed size (if set) |
350 int maxImgSize = dlConfig.getAsInt("max-image-size"); | |
351 if (maxImgSize > 0) { | |
298
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
352 paramDW = (paramDW * paramWS > maxImgSize) ? (int) (maxImgSize / paramWS) |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
353 : paramDW; |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
354 paramDH = (paramDH * paramWS > maxImgSize) ? (int) (maxImgSize / paramWS) |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
355 : paramDH; |
185 | 356 } |
335 | 357 |
73 | 358 //"big" try for all file/image actions |
359 try { | |
360 | |
161 | 361 // ImageFileset of the image to load |
362 ImageFileset fileset = null; | |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
363 |
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
161
diff
changeset
|
364 /* find the file to load/send */ |
1 | 365 |
73 | 366 // get PathInfo |
367 String loadPathName = dlRequest.getFilePath(); | |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
368 |
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
161
diff
changeset
|
369 /* check permissions */ |
298
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
370 if (useAuthorization) { |
73 | 371 // get a list of required roles (empty if no restrictions) |
372 List rolesRequired = authOp.rolesForPath(loadPathName, request); | |
373 if (rolesRequired != null) { | |
298
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
374 authlog.debug("Role required: " + rolesRequired); |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
375 authlog.debug("User: " + request.getRemoteUser()); |
73 | 376 // is the current request/user authorized? |
377 if (!authOp.isRoleAuthorized(rolesRequired, request)) { | |
378 // send deny answer and abort | |
298
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
379 throw new AuthOpException(); |
73 | 380 } |
381 } | |
335 | 382 } |
1 | 383 |
335 | 384 // find the file |
385 fileset = (ImageFileset) findFile(dlRequest); | |
94 | 386 if (fileset == null) { |
298
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
387 throw new FileOpException("File " + loadPathName + "(" |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
388 + dlRequest.getAsInt("pn") + ") not found."); |
94 | 389 } |
181 | 390 |
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
161
diff
changeset
|
391 /* calculate expected source image size */ |
149 | 392 ImageSize expectedSourceSize = new ImageSize(); |
122 | 393 if (scaleToFit) { |
298
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
394 float scale = (1 / Math.min(paramWW, paramWH)) * paramWS; |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
395 expectedSourceSize.setSize((int) (paramDW * scale), |
335 | 396 (int) (paramDH * scale)); |
122 | 397 } else { |
298
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
398 expectedSourceSize.setSize((int) (paramDW * paramWS), |
335 | 399 (int) (paramDH * paramWS)); |
100 | 400 } |
401 | |
335 | 402 ImageFile fileToLoad; |
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
161
diff
changeset
|
403 /* select a resolution */ |
149 | 404 if (hiresOnly) { |
405 // get first element (= highest resolution) | |
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
161
diff
changeset
|
406 fileToLoad = fileset.getBiggest(); |
149 | 407 } else if (loresOnly) { |
408 // enforced lores uses next smaller resolution | |
298
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
409 fileToLoad = fileset.getNextSmaller(expectedSourceSize); |
149 | 410 if (fileToLoad == null) { |
411 // this is the smallest we have | |
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
161
diff
changeset
|
412 fileToLoad = fileset.getSmallest(); |
149 | 413 } |
414 } else { | |
130 | 415 // autores: use next higher resolution |
298
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
416 fileToLoad = fileset.getNextBigger(expectedSourceSize); |
122 | 417 if (fileToLoad == null) { |
130 | 418 // this is the highest we have |
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
161
diff
changeset
|
419 fileToLoad = fileset.getBiggest(); |
122 | 420 } |
86 | 421 } |
298
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
422 logger.info("Planning to load: " + fileToLoad.getFile()); |
73 | 423 |
181 | 424 /* |
425 * send the image if its mo=(raw)file | |
426 */ | |
427 if (dlRequest.hasOption("mo", "file") | |
335 | 428 || dlRequest.hasOption("mo", "rawfile")) { |
181 | 429 if (sendFileAllowed) { |
430 String mt = null; | |
431 if (dlRequest.hasOption("mo", "rawfile")) { | |
432 mt = "application/octet-stream"; | |
433 } | |
263 | 434 logger.debug("Sending RAW File as is."); |
181 | 435 ServletOps.sendFile(fileToLoad.getFile(), mt, response); |
263 | 436 logger.info("Done in " |
298
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
437 + (System.currentTimeMillis() - startTime) + "ms"); |
263 | 438 return; |
181 | 439 } |
440 } | |
441 | |
86 | 442 // check the source image |
122 | 443 if (!fileToLoad.isChecked()) { |
284 | 444 ImageOps.checkFile(fileToLoad); |
122 | 445 } |
86 | 446 // get the source image type |
447 mimeType = fileToLoad.getMimetype(); | |
149 | 448 // get the source image size |
449 ImageSize imgSize = fileToLoad.getSize(); | |
450 | |
130 | 451 // decide if the image can be sent as is |
298
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
452 boolean mimetypeSendable = mimeType.equals("image/jpeg") |
122 | 453 || mimeType.equals("image/png") |
454 || mimeType.equals("image/gif"); | |
298
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
455 boolean imagoOptions = dlRequest.hasOption("mo", "hmir") |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
456 || dlRequest.hasOption("mo", "vmir") || (paramROT != 0) |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
457 || (paramRGBM != null) || (paramRGBA != null) |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
458 || (paramCONT != 0) || (paramBRGT != 0); |
139
11cfe4c89fdc
Servlet version 1.11b1 with improved original-size.
robcast
parents:
130
diff
changeset
|
459 boolean imageSendable = mimetypeSendable && !imagoOptions; |
73 | 460 |
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
161
diff
changeset
|
461 /* |
298
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
462 * if not autoRes and image smaller than requested size then send as |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
463 * is. if autoRes and image has requested size then send as is. if |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
464 * not autoScale and not scaleToFit nor cropToFit then send as is |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
465 * (mo=file) |
73 | 466 */ |
181 | 467 if (imageSendable |
298
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
468 && ((loresOnly && fileToLoad.getSize().isSmallerThan( |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
469 expectedSourceSize)) || (!(loresOnly || hiresOnly) && fileToLoad |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
470 .getSize().fitsIn(expectedSourceSize)))) { |
73 | 471 |
181 | 472 logger.debug("Sending File as is."); |
1 | 473 |
181 | 474 ServletOps.sendFile(fileToLoad.getFile(), null, response); |
475 | |
476 logger.info("Done in " | |
298
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
477 + (System.currentTimeMillis() - startTime) + "ms"); |
73 | 478 return; |
479 } | |
1 | 480 |
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
161
diff
changeset
|
481 // set missing dw or dh from aspect ratio |
298
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
482 float imgAspect = fileToLoad.getAspect(); |
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
161
diff
changeset
|
483 if (paramDW == 0) { |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
161
diff
changeset
|
484 paramDW = (int) Math.round(paramDH * imgAspect); |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
161
diff
changeset
|
485 } else if (paramDH == 0) { |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
161
diff
changeset
|
486 paramDH = (int) Math.round(paramDW / imgAspect); |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
161
diff
changeset
|
487 } |
149 | 488 |
335 | 489 /* |
490 * prepare resolution for original size | |
491 */ | |
492 if (absoluteScale) { | |
493 // get original resolution from metadata | |
494 fileset.checkMeta(); | |
495 origResX = fileset.getResX(); | |
496 origResY = fileset.getResY(); | |
497 if ((origResX == 0) || (origResY == 0)) { | |
498 throw new ImageOpException("Missing image DPI information!"); | |
499 } | |
500 | |
501 if ((paramDDPIX == 0) || (paramDDPIY == 0)) { | |
502 throw new ImageOpException( | |
503 "Missing display DPI information!"); | |
504 } | |
505 } | |
506 | |
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
161
diff
changeset
|
507 /* crop and scale the image */ |
1 | 508 |
298
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
509 logger.debug("IMG: " + imgSize.getWidth() + "x" |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
510 + imgSize.getHeight()); |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
511 logger.debug("time " + (System.currentTimeMillis() - startTime) |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
512 + "ms"); |
1 | 513 |
73 | 514 // coordinates and scaling |
298
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
515 float areaXoff; |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
516 float areaYoff; |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
517 float areaWidth; |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
518 float areaHeight; |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
519 float scaleX; |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
520 float scaleY; |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
521 float scaleXY; |
1 | 522 |
100 | 523 // coordinates using Java2D |
524 // image size in pixels | |
298
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
525 Rectangle2D imgBounds = new Rectangle2D.Float(0, 0, imgSize |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
526 .getWidth(), imgSize.getHeight()); |
100 | 527 // user window area in [0,1] coordinates |
298
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
528 Rectangle2D relUserArea = new Rectangle2D.Float(paramWX, paramWY, |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
529 paramWW, paramWH); |
100 | 530 // transform from relative [0,1] to image coordinates. |
298
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
531 AffineTransform imgTrafo = AffineTransform.getScaleInstance(imgSize |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
532 .getWidth(), imgSize.getHeight()); |
85 | 533 // transform user coordinate area to image coordinate area |
298
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
534 Rectangle2D userImgArea = imgTrafo.createTransformedShape( |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
535 relUserArea).getBounds2D(); |
100 | 536 |
537 // calculate scaling factors based on inner user area | |
73 | 538 if (scaleToFit) { |
298
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
539 areaWidth = (float) userImgArea.getWidth(); |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
540 areaHeight = (float) userImgArea.getHeight(); |
73 | 541 scaleX = paramDW / areaWidth * paramWS; |
542 scaleY = paramDH / areaHeight * paramWS; | |
543 scaleXY = (scaleX > scaleY) ? scaleY : scaleX; | |
100 | 544 } else if (absoluteScale) { |
545 // absolute scale | |
130 | 546 scaleX = paramDDPIX / origResX; |
547 scaleY = paramDDPIY / origResY; | |
548 // currently only same scale :-( | |
549 scaleXY = scaleX; | |
550 areaWidth = paramDW / scaleXY * paramWS; | |
551 areaHeight = paramDH / scaleXY * paramWS; | |
100 | 552 // reset user area size |
298
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
553 userImgArea.setRect(userImgArea.getX(), userImgArea.getY(), |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
554 areaWidth, areaHeight); |
73 | 555 } else { |
556 // crop to fit | |
85 | 557 areaWidth = paramDW * paramWS; |
558 areaHeight = paramDH * paramWS; | |
100 | 559 // reset user area size |
298
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
560 userImgArea.setRect(userImgArea.getX(), userImgArea.getY(), |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
561 areaWidth, areaHeight); |
73 | 562 scaleX = 1f; |
563 scaleY = 1f; | |
564 scaleXY = 1f; | |
100 | 565 } |
85 | 566 |
100 | 567 // enlarge image area for rotations to cover additional pixels |
568 Rectangle2D outerUserImgArea = userImgArea; | |
569 Rectangle2D innerUserImgArea = userImgArea; | |
570 if (wholeRotArea) { | |
571 if (paramROT != 0) { | |
572 try { | |
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
161
diff
changeset
|
573 // rotate user area coordinates around center of user |
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
161
diff
changeset
|
574 // area |
298
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
575 AffineTransform rotTrafo = AffineTransform |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
576 .getRotateInstance(Math.toRadians(paramROT), |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
577 userImgArea.getCenterX(), userImgArea |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
578 .getCenterY()); |
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
161
diff
changeset
|
579 // get bounds from rotated end position |
298
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
580 innerUserImgArea = rotTrafo.createTransformedShape( |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
581 userImgArea).getBounds2D(); |
100 | 582 // get bounds from back-rotated bounds |
298
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
583 outerUserImgArea = rotTrafo.createInverse() |
100 | 584 .createTransformedShape(innerUserImgArea) |
585 .getBounds2D(); | |
586 } catch (NoninvertibleTransformException e1) { | |
587 // this shouldn't happen anyway | |
298
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
588 logger.error(e1); |
100 | 589 } |
590 } | |
73 | 591 } |
1 | 592 |
298
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
593 logger.debug("Scale " + scaleXY + "(" + scaleX + "," + scaleY |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
594 + ") on " + outerUserImgArea); |
1 | 595 |
73 | 596 // clip area at the image border |
100 | 597 outerUserImgArea = outerUserImgArea.createIntersection(imgBounds); |
85 | 598 |
73 | 599 // check image parameters sanity |
298
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
600 if ((outerUserImgArea.getWidth() < 1) |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
601 || (outerUserImgArea.getHeight() < 1) |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
602 || (scaleXY * outerUserImgArea.getWidth() < 2) |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
603 || (scaleXY * outerUserImgArea.getHeight() < 2)) { |
181 | 604 logger.error("ERROR: invalid scale parameter set!"); |
73 | 605 throw new ImageOpException("Invalid scale parameter set!"); |
606 } | |
1 | 607 |
298
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
608 /* |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
609 * submit the image worker job |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
610 */ |
85 | 611 |
298
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
612 DigilibWorker job = new DigilibImageWorker(dlConfig, response, |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
613 mimeType, scaleQual, dlRequest, paramROT, paramCONT, |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
614 paramBRGT, paramRGBM, paramRGBA, fileToLoad, scaleXY, |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
615 outerUserImgArea, innerUserImgArea, minSubsample, |
303 | 616 wholeRotArea, forceJPEG); |
85 | 617 |
298
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
618 job.run(); |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
619 if (job.hasError()) { |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
620 throw new ImageOpException(job.getError().toString()); |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
621 } |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
622 |
298
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
623 logger.debug("servlet done in " |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
624 + (System.currentTimeMillis() - startTime)); |
73 | 625 |
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
161
diff
changeset
|
626 /* error handling */ |
1 | 627 |
73 | 628 } // end of "big" try |
156 | 629 catch (IOException e) { |
298
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
630 logger.error("ERROR: File IO Error: " + e); |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
631 digilibError(errorMsgHtml, ERROR_FILE, |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
632 "ERROR: File IO Error: " + e, response); |
73 | 633 } catch (AuthOpException e) { |
298
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
634 logger.error("ERROR: Authorization error: " + e); |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
635 digilibError(errorMsgHtml, ERROR_AUTH, |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
636 "ERROR: Authorization error: " + e, response); |
73 | 637 } catch (ImageOpException e) { |
298
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
638 logger.error("ERROR: Image Error: " + e); |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
639 digilibError(errorMsgHtml, ERROR_IMAGE, |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
640 "ERROR: Image Operation Error: " + e, response); |
73 | 641 } catch (RuntimeException e) { |
642 // JAI likes to throw RuntimeExceptions ;-( | |
298
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
643 logger.error("ERROR: Other Image Error: " + e); |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
644 digilibError(errorMsgHtml, ERROR_IMAGE, |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
645 "ERROR: Other Image Operation Error: " + e, response); |
73 | 646 } |
647 } | |
1 | 648 |
298
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
649 /** |
335 | 650 * Returns the DocuDirent corresponding to the DigilibRequest. |
651 * | |
652 * @param dlRequest | |
653 * @return | |
654 */ | |
655 public DocuDirent findFile(DigilibRequest dlRequest) { | |
656 // find the file(set) | |
657 DocuDirent f = dirCache.getFile(dlRequest.getFilePath(), dlRequest | |
658 .getAsInt("pn"), FileOps.CLASS_IMAGE); | |
659 return f; | |
660 } | |
661 | |
662 /** | |
298
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
663 * Sends an error to the client as text or image. |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
664 * |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
665 * @param asHTML |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
666 * @param type |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
667 * @param msg |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
668 * @param response |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
669 */ |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
670 public void digilibError(boolean asHTML, int type, String msg, |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
671 HttpServletResponse response) { |
335 | 672 try { |
298
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
673 File img = null; |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
674 if (type == ERROR_AUTH) { |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
675 if (msg == null) { |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
676 msg = "ERROR: Unauthorized access!"; |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
677 } |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
678 img = denyImgFile; |
335 | 679 } else { |
298
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
680 if (msg == null) { |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
681 msg = "ERROR: Other image error!"; |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
682 } |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
683 img = this.errorImgFile; |
335 | 684 } |
298
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
685 if (asHTML && (img != null)) { |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
686 ServletOps.htmlMessage(msg, response); |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
687 } else { |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
688 ServletOps.sendFile(img, null, response); |
335 | 689 } |
298
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
690 } catch (IOException e) { |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
691 logger.error("Error sending error!", e); |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
692 } |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
693 |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
694 } |
1ecaf9c1fd8a
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
288
diff
changeset
|
695 |
73 | 696 } //Scaler class |