Mercurial > hg > digilib-old
annotate servlet/src/digilib/image/ImageJobDescription.java @ 741:ee620bcf4ab0 jquery
image-drag now with full background image :-)
if your browser supports background-size.
and some cleanups.
author | robcast |
---|---|
date | Wed, 02 Feb 2011 20:03:36 +0100 |
parents | c1306f6d6a79 |
children | 5d3f74f378dd 4b686a0d44f7 |
rev | line source |
---|---|
557 | 1 package digilib.image; |
500 | 2 |
3 import java.awt.geom.AffineTransform; | |
4 import java.awt.geom.Rectangle2D; | |
5 import java.io.IOException; | |
6 | |
7 import org.apache.log4j.Logger; | |
8 | |
9 import digilib.io.DocuDirCache; | |
541 | 10 import digilib.io.DocuDirectory; |
500 | 11 import digilib.io.FileOpException; |
12 import digilib.io.FileOps; | |
563 | 13 import digilib.io.FileOps.FileClass; |
500 | 14 import digilib.io.ImageFile; |
15 import digilib.io.ImageFileset; | |
557 | 16 import digilib.servlet.DigilibConfiguration; |
17 import digilib.util.OptionsSet; | |
18 import digilib.util.Parameter; | |
19 import digilib.util.ParameterMap; | |
500 | 20 |
21 | |
22 /** | |
23 * A container class for storing a set of instructional parameters | |
24 * used for content generating classes like MakePDF. | |
25 * | |
511 | 26 * This contains the functionality formerly found in Scaler, processRequest, only factorized. |
27 * | |
28 * TODO clean up... | |
500 | 29 * |
547 | 30 * @author cmielack, casties |
500 | 31 * |
32 */ | |
33 | |
547 | 34 public class ImageJobDescription extends ParameterMap { |
500 | 35 |
36 DigilibConfiguration dlConfig = null; | |
37 protected static Logger logger = Logger.getLogger("digilib.servlet"); | |
38 | |
39 ImageFile fileToLoad = null; | |
547 | 40 ImageFileset fileset = null; |
541 | 41 DocuDirectory fileDir = null; |
42 String filePath = null; | |
500 | 43 ImageSize expectedSourceSize = null; |
44 Float scaleXY = null; | |
45 Rectangle2D userImgArea = null; | |
46 Rectangle2D outerUserImgArea= null; | |
502 | 47 Boolean imageSendable = null; |
547 | 48 String mimeType; |
49 Integer paramDW; | |
50 Integer paramDH; | |
51 | |
52 /** create empty ImageJobDescription. | |
53 * @param dlcfg | |
54 */ | |
55 public ImageJobDescription(DigilibConfiguration dlcfg) { | |
500 | 56 super(30); |
547 | 57 dlConfig = dlcfg; |
552 | 58 } |
59 | |
60 | |
61 /** set up Parameters | |
557 | 62 * @see digilib.util.ParameterMap#initParams() |
552 | 63 */ |
64 @Override | |
65 protected void initParams() { | |
500 | 66 // url of the page/document (second part) |
67 newParameter("fn", "", null, 's'); | |
68 // page number | |
69 newParameter("pn", new Integer(1), null, 's'); | |
70 // width of client in pixels | |
71 newParameter("dw", new Integer(0), null, 's'); | |
72 // height of client in pixels | |
73 newParameter("dh", new Integer(0), null, 's'); | |
74 // left edge of image (float from 0 to 1) | |
75 newParameter("wx", new Float(0), null, 's'); | |
76 // top edge in image (float from 0 to 1) | |
77 newParameter("wy", new Float(0), null, 's'); | |
78 // width of image (float from 0 to 1) | |
79 newParameter("ww", new Float(1), null, 's'); | |
80 // height of image (float from 0 to 1) | |
81 newParameter("wh", new Float(1), null, 's'); | |
82 // scale factor | |
83 newParameter("ws", new Float(1), null, 's'); | |
84 // special options like 'fit' for gifs | |
547 | 85 newParameter("mo", this.options, null, 's'); |
500 | 86 // rotation angle (degree) |
87 newParameter("rot", new Float(0), null, 's'); | |
88 // contrast enhancement factor | |
89 newParameter("cont", new Float(0), null, 's'); | |
90 // brightness enhancement factor | |
91 newParameter("brgt", new Float(0), null, 's'); | |
92 // color multiplicative factors | |
93 newParameter("rgbm", "0/0/0", null, 's'); | |
94 // color additive factors | |
95 newParameter("rgba", "0/0/0", null, 's'); | |
96 // display dpi resolution (total) | |
97 newParameter("ddpi", new Float(0), null, 's'); | |
98 // display dpi X resolution | |
99 newParameter("ddpix", new Float(0), null, 's'); | |
100 // display dpi Y resolution | |
101 newParameter("ddpiy", new Float(0), null, 's'); | |
102 // scale factor for mo=ascale | |
103 newParameter("scale", new Float(1), null, 's'); | |
104 } | |
105 | |
503
fdb824bd57ab
first functional version of PDFCache after restructuring the code
cmielack
parents:
502
diff
changeset
|
106 |
552 | 107 /* (non-Javadoc) |
108 * @see digilib.servlet.ParameterMap#initOptions() | |
547 | 109 */ |
552 | 110 @Override |
111 protected void initOptions() { | |
565 | 112 String s = this.getAsString("mo"); |
113 options = new OptionsSet(s); | |
500 | 114 } |
552 | 115 |
116 | |
117 /** Creates new ImageJobDescription by merging Parameters from another ParameterMap. | |
118 * @param pm | |
119 * @param dlcfg | |
120 * @return | |
121 */ | |
557 | 122 public static ImageJobDescription getInstance(ParameterMap pm, DigilibConfiguration dlcfg) { |
552 | 123 ImageJobDescription newMap = new ImageJobDescription(dlcfg); |
124 // add all params to this map | |
557 | 125 newMap.params.putAll(pm.getParams()); |
552 | 126 newMap.initOptions(); |
127 return newMap; | |
128 } | |
129 | |
500 | 130 |
547 | 131 public String getMimeType() throws IOException { |
132 if (mimeType == null) { | |
133 fileToLoad = getFileToLoad(); | |
134 if(! fileToLoad.isChecked()){ | |
563 | 135 DigilibConfiguration.docuImageIdentify(fileToLoad); |
502 | 136 } |
547 | 137 mimeType = fileToLoad.getMimetype(); |
502 | 138 } |
500 | 139 return mimeType; |
140 } | |
141 | |
547 | 142 public ImageFile getFileToLoad() throws IOException { |
500 | 143 |
144 if(fileToLoad == null){ | |
547 | 145 fileset = getFileset(); |
500 | 146 |
147 /* select a resolution */ | |
547 | 148 if (getHiresOnly()) { |
500 | 149 // get first element (= highest resolution) |
150 fileToLoad = fileset.getBiggest(); | |
547 | 151 } else if (getLoresOnly()) { |
500 | 152 // enforced lores uses next smaller resolution |
547 | 153 fileToLoad = fileset.getNextSmaller(getExpectedSourceSize()); |
500 | 154 if (fileToLoad == null) { |
155 // this is the smallest we have | |
156 fileToLoad = fileset.getSmallest(); | |
157 } | |
158 } else { | |
159 // autores: use next higher resolution | |
547 | 160 fileToLoad = fileset.getNextBigger(getExpectedSourceSize()); |
500 | 161 if (fileToLoad == null) { |
162 // this is the highest we have | |
163 fileToLoad = fileset.getBiggest(); | |
164 } | |
165 } | |
166 logger.info("Planning to load: " + fileToLoad.getFile()); | |
167 } | |
168 | |
169 return fileToLoad; | |
170 | |
171 } | |
172 | |
557 | 173 public DocuDirectory getFileDirectory() throws FileOpException { |
555 | 174 if(fileDir == null){ |
500 | 175 DocuDirCache dirCache = (DocuDirCache) dlConfig.getValue("servlet.dir.cache"); |
555 | 176 String fp = getFilePath(); |
177 fileDir = dirCache.getDirectory(fp); | |
541 | 178 if (fileDir == null) { |
179 throw new FileOpException("Directory " + getFilePath() + " not found."); | |
500 | 180 } |
181 } | |
541 | 182 return fileDir; |
500 | 183 } |
184 | |
557 | 185 public ImageFileset getFileset() throws FileOpException { |
541 | 186 if(fileset==null){ |
187 DocuDirCache dirCache = (DocuDirCache) dlConfig.getValue("servlet.dir.cache"); | |
188 | |
563 | 189 fileset = (ImageFileset) dirCache.getFile(getFilePath(), getAsInt("pn"), FileClass.IMAGE); |
541 | 190 if (fileset == null) { |
191 throw new FileOpException("File " + getFilePath() + "(" | |
192 + getAsInt("pn") + ") not found."); | |
193 } | |
194 } | |
195 return fileset; | |
196 } | |
197 | |
500 | 198 public String getFilePath() { |
541 | 199 if(filePath == null){ |
500 | 200 String s = this.getAsString("request.path"); |
201 s += this.getAsString("fn"); | |
541 | 202 filePath = FileOps.normalName(s); |
500 | 203 } |
541 | 204 return filePath; |
500 | 205 } |
206 | |
547 | 207 public boolean getHiresOnly(){ |
208 return hasOption("clip") || hasOption("hires"); | |
500 | 209 } |
210 | |
547 | 211 public boolean getLoresOnly(){ |
212 return hasOption("lores"); | |
500 | 213 } |
214 | |
547 | 215 public boolean getScaleToFit() { |
216 return !(hasOption("clip") || hasOption("osize") || hasOption("ascale")); | |
500 | 217 } |
218 | |
547 | 219 public boolean getAbsoluteScale(){ |
220 return hasOption("osize") || hasOption("ascale"); | |
500 | 221 } |
222 | |
223 | |
547 | 224 public ImageSize getExpectedSourceSize() throws IOException { |
500 | 225 if (expectedSourceSize == null){ |
226 expectedSourceSize = new ImageSize(); | |
547 | 227 if (getScaleToFit()) { |
500 | 228 // scale to fit -- calculate minimum source size |
229 float scale = (1 / Math.min(getAsFloat("ww"), getAsFloat("wh"))) * getAsFloat("ws"); | |
547 | 230 expectedSourceSize.setSize((int) (getDw() * scale), |
231 (int) (getDh() * scale)); | |
232 } else if (getAbsoluteScale() && hasOption("ascale")) { | |
500 | 233 // absolute scale -- apply scale to hires size |
547 | 234 expectedSourceSize = getHiresSize().getScaled(getAsFloat("scale")); |
500 | 235 } else { |
236 // clip to fit -- source = destination size | |
547 | 237 expectedSourceSize.setSize((int) (getDw() * getAsFloat("ws")), |
238 (int) (getDh() * getAsFloat("ws"))); | |
500 | 239 } |
240 } | |
241 return expectedSourceSize; | |
242 } | |
243 | |
547 | 244 public ImageSize getHiresSize() throws IOException { |
500 | 245 logger.debug("get_hiresSize()"); |
246 | |
247 ImageSize hiresSize = null; | |
547 | 248 ImageFileset fileset = getFileset(); |
249 if (getAbsoluteScale()) { | |
500 | 250 ImageFile hiresFile = fileset.getBiggest(); |
251 if (!hiresFile.isChecked()) { | |
563 | 252 DigilibConfiguration.docuImageIdentify(hiresFile); |
500 | 253 } |
254 hiresSize = hiresFile.getSize(); | |
255 } | |
256 return hiresSize; | |
257 } | |
258 | |
547 | 259 /** Returns image scaling factor. |
260 * Uses image size and user parameters. | |
261 * Modifies scaleXY, userImgArea. | |
262 * @return | |
263 * @throws IOException | |
264 * @throws ImageOpException | |
265 */ | |
266 public float getScaleXY() throws IOException, ImageOpException { | |
500 | 267 //logger.debug("get_scaleXY()"); |
268 if(scaleXY == null){ | |
269 // coordinates and scaling | |
270 float areaWidth; | |
271 float areaHeight; | |
547 | 272 float ws = getAsFloat("ws"); |
273 ImageSize imgSize = getFileToLoad().getSize(); | |
500 | 274 // user window area in [0,1] coordinates |
275 Rectangle2D relUserArea = new Rectangle2D.Float(getAsFloat("wx"), getAsFloat("wy"), | |
276 getAsFloat("ww"), getAsFloat("wh")); | |
277 // transform from relative [0,1] to image coordinates. | |
278 AffineTransform imgTrafo = AffineTransform.getScaleInstance(imgSize | |
279 .getWidth(), imgSize.getHeight()); | |
280 // transform user coordinate area to image coordinate area | |
547 | 281 userImgArea = imgTrafo.createTransformedShape( |
500 | 282 relUserArea).getBounds2D(); |
283 | |
547 | 284 if (getScaleToFit()) { |
285 // calculate scaling factors based on inner user area | |
500 | 286 areaWidth = (float) userImgArea.getWidth(); |
287 areaHeight = (float) userImgArea.getHeight(); | |
547 | 288 float scaleX = getDw() / areaWidth * ws; |
289 float scaleY = getDh() / areaHeight * ws; | |
500 | 290 scaleXY = (scaleX > scaleY) ? scaleY : scaleX; |
547 | 291 } else if (getAbsoluteScale()) { |
292 // absolute scaling factor | |
293 if (hasOption("osize")) { | |
294 // get original resolution from metadata | |
295 fileset.checkMeta(); | |
296 float origResX = fileset.getResX(); | |
297 float origResY = fileset.getResY(); | |
298 if ((origResX == 0) || (origResY == 0)) { | |
299 throw new ImageOpException("Missing image DPI information!"); | |
300 } | |
301 if ((getAsFloat("ddpix") == 0) || (getAsFloat("ddpiy") == 0)) { | |
302 throw new ImageOpException("Missing display DPI information!"); | |
303 } | |
304 // calculate absolute scale factor | |
305 float sx = getAsFloat("ddpix") / origResX; | |
306 float sy = getAsFloat("ddpiy") / origResY; | |
307 // currently only same scale -- mean value | |
308 scaleXY = (sx + sy) / 2f; | |
309 } else { | |
310 scaleXY = getAsFloat("scale"); | |
311 } | |
500 | 312 // we need to correct the factor if we use a pre-scaled image |
547 | 313 ImageSize hiresSize = getHiresSize(); |
500 | 314 if (imgSize.getWidth() != hiresSize.getWidth()) { |
315 scaleXY *= (float)hiresSize.getWidth() / (float)imgSize.getWidth(); | |
316 } | |
547 | 317 areaWidth = getDw() / scaleXY * ws; |
318 areaHeight = getDh() / scaleXY * ws; | |
500 | 319 // reset user area size |
320 userImgArea.setRect(userImgArea.getX(), userImgArea.getY(), | |
321 areaWidth, areaHeight); | |
322 } else { | |
547 | 323 // crop to fit -- don't scale |
324 areaWidth = getDw() * ws; | |
325 areaHeight = getDh() * ws; | |
500 | 326 // reset user area size |
327 userImgArea.setRect(userImgArea.getX(), userImgArea.getY(), | |
328 areaWidth, areaHeight); | |
329 scaleXY = 1f; | |
330 } | |
331 } | |
332 return (float) scaleXY; | |
333 } | |
334 | |
547 | 335 public int getDw() throws IOException { |
500 | 336 logger.debug("get_paramDW()"); |
547 | 337 if (paramDW == null) { |
500 | 338 |
547 | 339 paramDW = getAsInt("dw"); |
340 paramDH = getAsInt("dh"); | |
341 | |
342 float imgAspect = getFileToLoad().getAspect(); | |
500 | 343 if (paramDW == 0) { |
547 | 344 // calculate dw |
345 paramDW = Math.round(paramDH * imgAspect); | |
500 | 346 setValue("dw", paramDW); |
347 } else if (paramDH == 0) { | |
547 | 348 // calculate dh |
349 paramDH = Math.round(paramDW / imgAspect); | |
350 setValue("dh", paramDH); | |
500 | 351 } |
352 } | |
353 return paramDW; | |
354 } | |
355 | |
547 | 356 public int getDh() throws IOException { |
500 | 357 logger.debug("get_paramDH()"); |
547 | 358 if (paramDH == null) { |
359 | |
360 paramDW = getAsInt("dw"); | |
361 paramDH = getAsInt("dh"); | |
500 | 362 |
547 | 363 float imgAspect = getFileToLoad().getAspect(); |
500 | 364 if (paramDW == 0) { |
547 | 365 // calculate dw |
366 paramDW = Math.round(paramDH * imgAspect); | |
367 setValue("dw", paramDW); | |
500 | 368 } else if (paramDH == 0) { |
547 | 369 // calculate dh |
370 paramDH = Math.round(paramDW / imgAspect); | |
371 setValue("dh", paramDH); | |
500 | 372 } |
373 } | |
374 return paramDH; | |
375 } | |
547 | 376 |
565 | 377 public Integer getScaleQual(){ |
500 | 378 logger.debug("get_scaleQual()"); |
547 | 379 Integer qual = dlConfig.getAsInt("default-quality"); |
380 if(hasOption("q0")) | |
500 | 381 qual = 0; |
547 | 382 else if(hasOption("q1")) |
500 | 383 qual = 1; |
547 | 384 else if(hasOption("q2")) |
500 | 385 qual = 2; |
386 return qual; | |
387 } | |
388 | |
389 | |
547 | 390 public Rectangle2D getUserImgArea() throws IOException, ImageOpException{ |
391 if(userImgArea == null) { | |
392 // getScaleXY sets userImgArea | |
393 getScaleXY(); | |
500 | 394 } |
395 return userImgArea; | |
396 | |
397 } | |
398 | |
547 | 399 public Rectangle2D getOuterUserImgArea() throws IOException, ImageOpException { |
500 | 400 if(outerUserImgArea == null){ |
547 | 401 outerUserImgArea = getUserImgArea(); |
500 | 402 |
403 // image size in pixels | |
547 | 404 ImageSize imgSize = getFileToLoad().getSize(); |
405 Rectangle2D imgBounds = new Rectangle2D.Float(0, 0, imgSize.getWidth(), | |
406 imgSize.getHeight()); | |
500 | 407 |
408 // clip area at the image border | |
409 outerUserImgArea = outerUserImgArea.createIntersection(imgBounds); | |
410 | |
411 // check image parameters sanity | |
547 | 412 scaleXY = getScaleXY(); |
500 | 413 logger.debug("outerUserImgArea.getWidth()=" + outerUserImgArea.getWidth()); |
547 | 414 logger.debug("get_scaleXY() * outerUserImgArea.getWidth() = " + (scaleXY * outerUserImgArea.getWidth())); |
500 | 415 |
416 if ((outerUserImgArea.getWidth() < 1) | |
417 || (outerUserImgArea.getHeight() < 1) | |
547 | 418 || (scaleXY * outerUserImgArea.getWidth() < 2) |
419 || (scaleXY * outerUserImgArea.getHeight() < 2)) { | |
500 | 420 logger.error("ERROR: invalid scale parameter set!"); |
421 throw new ImageOpException("Invalid scale parameter set!"); | |
422 } | |
423 } | |
424 return outerUserImgArea; | |
425 } | |
426 | |
427 | |
547 | 428 public float[] getRGBM(){ |
502 | 429 float[] paramRGBM = null;//{0f,0f,0f}; |
547 | 430 Parameter p = params.get("rgbm"); |
500 | 431 if (p.hasValue() && (!p.getAsString().equals("0/0/0"))) { |
642 | 432 paramRGBM = p.parseAsFloatArray("/"); |
433 if ((paramRGBM == null) || (paramRGBM.length != 3)) { | |
434 return null; | |
435 } | |
500 | 436 } |
437 return paramRGBM; | |
438 } | |
439 | |
547 | 440 public float[] getRGBA(){ |
502 | 441 float[] paramRGBA = null;//{0f,0f,0f}; |
547 | 442 Parameter p = params.get("rgba"); |
500 | 443 if (p.hasValue() && (!p.getAsString().equals("0/0/0"))) { |
444 paramRGBA = p.parseAsFloatArray("/"); | |
642 | 445 if ((paramRGBA == null) || (paramRGBA.length != 3)) { |
446 return null; | |
447 } | |
500 | 448 } |
449 return paramRGBA; | |
450 } | |
451 | |
547 | 452 /** Has send-as-file been requested? |
453 * @return | |
454 */ | |
455 public boolean getSendAsFile(){ | |
456 return hasOption("file") | |
457 || hasOption("rawfile"); | |
500 | 458 } |
502 | 459 |
547 | 460 /** Could the image be sent without processing? |
461 * Takes image type and additional image operations into account. | |
462 * Does not check requested size transformation. | |
463 * @return | |
464 * @throws IOException | |
465 */ | |
466 public boolean isImageSendable() throws IOException { | |
467 // cached result? | |
468 if (imageSendable == null) { | |
469 String mimeType = getMimeType(); | |
502 | 470 imageSendable = ( (mimeType.equals("image/jpeg") |
471 || mimeType.equals("image/png") | |
472 || mimeType.equals("image/gif") ) | |
473 && | |
547 | 474 !(hasOption("hmir") |
475 || hasOption("vmir") | |
476 || (getAsFloat("rot") != 0.0) | |
477 || (getRGBM() != null) | |
478 || (getRGBA() != null) | |
479 || (getAsFloat("cont") != 0.0) | |
480 || (getAsFloat("brgt") != 0.0))); | |
502 | 481 } |
482 | |
483 return imageSendable; | |
484 } | |
485 | |
486 | |
547 | 487 public boolean isTransformRequired() throws IOException { |
488 ImageSize is = getFileToLoad().getSize(); | |
489 ImageSize ess = getExpectedSourceSize(); | |
490 // nt = no transform required | |
491 boolean nt = isImageSendable() && ( | |
492 // lores: send if smaller | |
493 (getLoresOnly() && is.isSmallerThan(ess)) | |
494 // else send if it fits | |
495 || (!(getLoresOnly() || getHiresOnly()) && is.fitsIn(ess))); | |
496 return ! nt; | |
502 | 497 } |
500 | 498 } |