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