comparison common/src/main/java/digilib/image/ImageWorker.java @ 903:7779b37d1d05

refactored into maven modules per servlet type. can build servlet-api 2.3 and 3.0 via profile now!
author robcast
date Tue, 26 Apr 2011 20:24:31 +0200
parents servlet/src/main/java/digilib/image/ImageWorker.java@ba1eb2d821a2
children 28d007673346
comparison
equal deleted inserted replaced
902:89ba3ffcf552 903:7779b37d1d05
1 /** Worker (Callable) that renders an image.
2 *
3 */
4 package digilib.image;
5
6 import java.awt.Rectangle;
7 import java.io.IOException;
8 import java.util.concurrent.Callable;
9
10 import org.apache.log4j.Logger;
11
12 import digilib.io.FileOpException;
13 import digilib.servlet.DigilibConfiguration;
14
15 /** Worker that renders an image.
16 *
17 * @author casties
18 *
19 */
20 public class ImageWorker implements Callable<DocuImage> {
21
22 protected static Logger logger = Logger.getLogger(ImageWorker.class);
23 private DigilibConfiguration dlConfig;
24 private ImageJobDescription jobinfo;
25
26 public ImageWorker(DigilibConfiguration dlConfig, ImageJobDescription jobinfo) {
27 super();
28 this.dlConfig = dlConfig;
29 this.jobinfo = jobinfo;
30 }
31
32 /**
33 * render and return the image
34 */
35 public DocuImage call() throws FileOpException, IOException, ImageOpException {
36
37 logger.debug("image worker starting");
38 long startTime = System.currentTimeMillis();
39
40 /* crop and scale image */
41
42 // new DocuImage instance
43 DocuImage docuImage = DigilibConfiguration.getDocuImageInstance();
44 if (docuImage == null) {
45 throw new ImageOpException("Unable to load DocuImage class!");
46 }
47
48 // set interpolation quality
49 docuImage.setQuality(jobinfo.getScaleQual());
50
51 Rectangle loadRect = jobinfo.getOuterUserImgArea().getBounds();
52 float scaleXY = jobinfo.getScaleXY();
53
54 // use subimage loading if possible
55 if (docuImage.isSubimageSupported()) {
56 logger.debug("Subimage: scale " + scaleXY + " = " + (1 / scaleXY));
57 float subf = 1f;
58 float subsamp = 1f;
59 if (scaleXY < 1) {
60 subf = 1 / scaleXY;
61 // for higher quality reduce subsample factor by minSubsample
62 if (jobinfo.getScaleQual() > 0) {
63 subsamp = (float) Math.max(Math.floor(subf / dlConfig.getAsFloat("subsample-minimum")), 1d);
64 } else {
65 subsamp = (float) Math.floor(subf);
66 }
67 scaleXY = subsamp / subf;
68 logger.debug("Using subsampling: " + subsamp + " rest "
69 + scaleXY);
70 }
71 docuImage.loadSubimage(jobinfo.getInput(), loadRect, (int) subsamp);
72 logger.debug("SUBSAMP: " + subsamp + " -> " + docuImage.getSize());
73 docuImage.scale(scaleXY, scaleXY);
74 } else {
75 // else load and crop the whole file
76 docuImage.loadImage(jobinfo.getInput());
77 docuImage.crop((int) loadRect.getX(), (int) loadRect.getY(),
78 (int) loadRect.getWidth(), (int) loadRect.getHeight());
79 docuImage.scale(scaleXY, scaleXY);
80 }
81
82 // mirror image
83 // operation mode: "hmir": mirror horizontally, "vmir": mirror
84 // vertically
85 if (jobinfo.hasOption("hmir")) {
86 docuImage.mirror(0);
87 }
88 if (jobinfo.hasOption("vmir")) {
89 docuImage.mirror(90);
90 }
91
92 // rotate image
93 if (jobinfo.getAsFloat("rot") != 0d) {
94 docuImage.rotate(jobinfo.getAsFloat("rot"));
95 /* if (jobinfo.get_wholeRotArea()) {
96 // crop to the inner bounding box
97 float xcrop = (float) (docuImage.getWidth() - jobinfo.get_innerUserImgArea().getWidth()
98 * scaleXY);
99 float ycrop = (float) (docuImage.getHeight() - jobinfo.get_innerUserImgArea().getHeight()
100 * scaleXY);
101 if ((xcrop > 0) || (ycrop > 0)) {
102 // only crop smaller
103 xcrop = (xcrop > 0) ? xcrop : 0;
104 ycrop = (ycrop > 0) ? ycrop : 0;
105 // crop image
106 docuImage.crop((int) (xcrop / 2), (int) (ycrop / 2),
107 (int) (docuImage.getWidth() - xcrop),
108 (int) (docuImage.getHeight() - ycrop));
109 }
110 } */
111
112 }
113
114 // color modification
115 float[] paramRGBM = jobinfo.getRGBM();
116 float[] paramRGBA = jobinfo.getRGBA();
117 if ((paramRGBM != null) || (paramRGBA != null)) {
118 // make sure we actually have two arrays
119 if (paramRGBM == null) {
120 paramRGBM = new float[3];
121 }
122 if (paramRGBA == null) {
123 paramRGBA = new float[3];
124 }
125 // calculate "contrast" values (c=2^x)
126 float[] mult = new float[3];
127 for (int i = 0; i < 3; i++) {
128 mult[i] = (float) Math.pow(2, (float) paramRGBM[i]);
129 }
130 docuImage.enhanceRGB(mult, paramRGBA);
131 }
132
133 // contrast and brightness enhancement
134 float paramCONT = jobinfo.getAsFloat("cont");
135 float paramBRGT = jobinfo.getAsFloat("brgt");
136 if ((paramCONT != 0f) || (paramBRGT != 0f)) {
137 float mult = (float) Math.pow(2, paramCONT);
138 docuImage.enhance(mult, paramBRGT);
139 }
140
141 // color operation
142 DocuImage.ColorOp colop = jobinfo.getColOp();
143 if (colop != null) {
144 docuImage.colorOp(colop);
145 }
146
147 logger.debug("rendered in " + (System.currentTimeMillis() - startTime) + "ms");
148
149 return docuImage;
150 }
151
152 }