Mercurial > hg > digilib-old
comparison servlet/src/digilib/image/ImageWorker.java @ 557:0885f5ca5b24 digilibPDF
more refactoring and rearranging
pdf and image generation works now
author | robcast |
---|---|
date | Thu, 16 Dec 2010 21:19:11 +0100 |
parents | servlet/src/digilib/servlet/ImageWorker.java@e1094c5ec032 |
children | 4c51d71aef13 |
comparison
equal
deleted
inserted
replaced
556:5cc180bb0a5c | 557:0885f5ca5b24 |
---|---|
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 | |
23 protected static Logger logger = Logger.getLogger(ImageWorker.class); | |
24 private DigilibConfiguration dlConfig; | |
25 private ImageJobDescription jobinfo; | |
26 | |
27 public ImageWorker(DigilibConfiguration dlConfig, ImageJobDescription jobinfo) { | |
28 super(); | |
29 this.dlConfig = dlConfig; | |
30 this.jobinfo = jobinfo; | |
31 } | |
32 | |
33 /** | |
34 * render and return the image | |
35 */ | |
36 @Override | |
37 public DocuImage call() throws FileOpException, IOException, ImageOpException { | |
38 | |
39 logger.debug("image worker starting"); | |
40 long startTime = System.currentTimeMillis(); | |
41 | |
42 /* crop and scale image */ | |
43 | |
44 // new DocuImage instance | |
45 DocuImage docuImage = dlConfig.getDocuImageInstance(); | |
46 if (docuImage == null) { | |
47 throw new ImageOpException("Unable to load DocuImage class!"); | |
48 } | |
49 | |
50 // set interpolation quality | |
51 docuImage.setQuality(jobinfo.get_scaleQual()); | |
52 | |
53 Rectangle loadRect = jobinfo.getOuterUserImgArea().getBounds(); | |
54 float scaleXY = jobinfo.getScaleXY(); | |
55 | |
56 // use subimage loading if possible | |
57 if (docuImage.isSubimageSupported()) { | |
58 logger.debug("Subimage: scale " + scaleXY + " = " + (1 / scaleXY)); | |
59 float subf = 1f; | |
60 float subsamp = 1f; | |
61 if (scaleXY < 1) { | |
62 subf = 1 / scaleXY; | |
63 // for higher quality reduce subsample factor by minSubsample | |
64 if (jobinfo.get_scaleQual() > 0) { | |
65 subsamp = (float) Math.max(Math.floor(subf / dlConfig.getAsFloat("subsample-minimum")), 1d); | |
66 } else { | |
67 subsamp = (float) Math.floor(subf); | |
68 } | |
69 scaleXY = subsamp / subf; | |
70 logger.debug("Using subsampling: " + subsamp + " rest " | |
71 + scaleXY); | |
72 } | |
73 | |
74 docuImage.loadSubimage(jobinfo.getFileToLoad(), loadRect, (int) subsamp); | |
75 | |
76 logger.debug("SUBSAMP: " + subsamp + " -> " + docuImage.getWidth() | |
77 + "x" + docuImage.getHeight()); | |
78 | |
79 docuImage.scale(scaleXY, scaleXY); | |
80 | |
81 } else { | |
82 // else load and crop the whole file | |
83 docuImage.loadImage(jobinfo.getFileToLoad()); | |
84 docuImage.crop((int) loadRect.getX(), (int) loadRect.getY(), | |
85 (int) loadRect.getWidth(), (int) loadRect.getHeight()); | |
86 | |
87 docuImage.scale(scaleXY, scaleXY); | |
88 } | |
89 | |
90 // mirror image | |
91 // operation mode: "hmir": mirror horizontally, "vmir": mirror | |
92 // vertically | |
93 if (jobinfo.hasOption("hmir")) { | |
94 docuImage.mirror(0); | |
95 } | |
96 if (jobinfo.hasOption("vmir")) { | |
97 docuImage.mirror(90); | |
98 } | |
99 | |
100 // rotate image | |
101 if (jobinfo.getAsFloat("rot") != 0d) { | |
102 docuImage.rotate(jobinfo.getAsFloat("rot")); | |
103 /* if (jobinfo.get_wholeRotArea()) { | |
104 // crop to the inner bounding box | |
105 float xcrop = (float) (docuImage.getWidth() - jobinfo.get_innerUserImgArea().getWidth() | |
106 * scaleXY); | |
107 float ycrop = (float) (docuImage.getHeight() - jobinfo.get_innerUserImgArea().getHeight() | |
108 * scaleXY); | |
109 if ((xcrop > 0) || (ycrop > 0)) { | |
110 // only crop smaller | |
111 xcrop = (xcrop > 0) ? xcrop : 0; | |
112 ycrop = (ycrop > 0) ? ycrop : 0; | |
113 // crop image | |
114 docuImage.crop((int) (xcrop / 2), (int) (ycrop / 2), | |
115 (int) (docuImage.getWidth() - xcrop), | |
116 (int) (docuImage.getHeight() - ycrop)); | |
117 } | |
118 } */ | |
119 | |
120 } | |
121 | |
122 // color modification | |
123 float[] paramRGBM = jobinfo.getRGBM(); | |
124 float[] paramRGBA = jobinfo.getRGBA(); | |
125 if ((paramRGBM != null) || (paramRGBA != null)) { | |
126 // make sure we actually have two arrays | |
127 if (paramRGBM == null) { | |
128 paramRGBM = new float[3]; | |
129 } | |
130 if (paramRGBA == null) { | |
131 paramRGBA = new float[3]; | |
132 } | |
133 // calculate "contrast" values (c=2^x) | |
134 float[] mult = new float[3]; | |
135 for (int i = 0; i < 3; i++) { | |
136 mult[i] = (float) Math.pow(2, (float) paramRGBM[i]); | |
137 } | |
138 docuImage.enhanceRGB(mult, paramRGBA); | |
139 } | |
140 | |
141 // contrast and brightness enhancement | |
142 float paramCONT = jobinfo.getAsFloat("cont"); | |
143 float paramBRGT = jobinfo.getAsFloat("brgt"); | |
144 if ((paramCONT != 0f) || (paramBRGT != 0f)) { | |
145 float mult = (float) Math.pow(2, paramCONT); | |
146 docuImage.enhance(mult, paramBRGT); | |
147 } | |
148 | |
149 logger.debug("rendered in " + (System.currentTimeMillis() - startTime) + "ms"); | |
150 | |
151 return docuImage; | |
152 } | |
153 | |
154 } |