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