Mercurial > hg > digilib-old
comparison servlet/src/digilib/io/ImageFileSet.java @ 576:dad720e9b12b stream
try: DocuDirent as interface, ImageFile inherits from ImageInput and implements DocuDirent
author | robcast |
---|---|
date | Wed, 22 Dec 2010 20:19:06 +0100 |
parents | |
children | 50bd3d9759f3 |
comparison
equal
deleted
inserted
replaced
574:790cbfb58b52 | 576:dad720e9b12b |
---|---|
1 /** | |
2 * | |
3 */ | |
4 package digilib.io; | |
5 | |
6 import java.io.File; | |
7 import java.util.ArrayList; | |
8 import java.util.Arrays; | |
9 import java.util.Map; | |
10 | |
11 import digilib.io.FileOps.FileClass; | |
12 | |
13 /** | |
14 * @author casties | |
15 * | |
16 */ | |
17 public class ImageFileSet extends ImageSet implements DocuDirent { | |
18 | |
19 /** this is an image file */ | |
20 protected static FileClass fileClass = FileClass.IMAGE; | |
21 private Directory parent; | |
22 private boolean metaChecked; | |
23 private Map fileMeta; | |
24 | |
25 /** | |
26 * Constructor with a file and hints. | |
27 * | |
28 * The hints are expected to contain 'basedirs' and 'scaledfilext' keys. | |
29 * | |
30 * @param file | |
31 * @param hints | |
32 */ | |
33 public ImageFileSet(File file, Map<Integer,Object> hints) { | |
34 Directory[] dirs = (Directory[]) hints.get(FileOps.HINT_BASEDIRS); | |
35 int nb = dirs.length; | |
36 list = new ArrayList<ImageInput>(nb); | |
37 parent = dirs[0]; | |
38 fill(dirs, file, hints); | |
39 } | |
40 | |
41 /* (non-Javadoc) | |
42 * @see digilib.io.DocuDirent#getName() | |
43 */ | |
44 @Override | |
45 public String getName() { | |
46 // TODO Auto-generated method stub | |
47 return null; | |
48 } | |
49 | |
50 /* (non-Javadoc) | |
51 * @see digilib.io.DocuDirent#getParent() | |
52 */ | |
53 @Override | |
54 public Directory getParent() { | |
55 // TODO Auto-generated method stub | |
56 return null; | |
57 } | |
58 | |
59 /* (non-Javadoc) | |
60 * @see digilib.io.DocuDirent#setParent(digilib.io.Directory) | |
61 */ | |
62 @Override | |
63 public void setParent(Directory parent) { | |
64 // TODO Auto-generated method stub | |
65 | |
66 } | |
67 | |
68 /* (non-Javadoc) | |
69 * @see digilib.io.DocuDirent#getFileMeta() | |
70 */ | |
71 @Override | |
72 public MetadataMap getFileMeta() { | |
73 // TODO Auto-generated method stub | |
74 return null; | |
75 } | |
76 | |
77 /* (non-Javadoc) | |
78 * @see digilib.io.DocuDirent#setFileMeta(digilib.io.MetadataMap) | |
79 */ | |
80 @Override | |
81 public void setFileMeta(MetadataMap fileMeta) { | |
82 // TODO Auto-generated method stub | |
83 | |
84 } | |
85 | |
86 /* (non-Javadoc) | |
87 * @see digilib.io.DocuDirent#isMetaChecked() | |
88 */ | |
89 @Override | |
90 public boolean isMetaChecked() { | |
91 // TODO Auto-generated method stub | |
92 return false; | |
93 } | |
94 | |
95 /* (non-Javadoc) | |
96 * @see digilib.io.DocuDirent#compareTo(java.lang.Object) | |
97 */ | |
98 @Override | |
99 public int compareTo(Object arg0) { | |
100 // TODO Auto-generated method stub | |
101 return 0; | |
102 } | |
103 | |
104 @Override | |
105 public File getInput() { | |
106 // TODO Auto-generated method stub | |
107 return null; | |
108 } | |
109 | |
110 /** | |
111 * Adds an ImageFile to this Fileset. | |
112 * | |
113 * The files should be added in the order of higher to lower resolutions. | |
114 * The first file is considered the hires "original". | |
115 * | |
116 * | |
117 * @param f | |
118 * file to add | |
119 * @return true (always) | |
120 */ | |
121 public boolean add(ImageFile f) { | |
122 f.setParent(this); | |
123 return list.add(f); | |
124 } | |
125 | |
126 /** | |
127 * Fill the ImageSet with files from different base directories. | |
128 * | |
129 * | |
130 * @param dirs | |
131 * list of base directories | |
132 * @param fl | |
133 * file (from first base dir) | |
134 * @param hints | |
135 * | |
136 */ | |
137 void fill(Directory[] dirs, File fl, Map<Integer,Object> hints) { | |
138 int nb = dirs.length; | |
139 String fn = fl.getName(); | |
140 String baseFn = FileOps.basename(fn); | |
141 // add the first ImageFile to the ImageSet | |
142 add(new ImageFile(fn, this, parent)); | |
143 // iterate the remaining base directories | |
144 for (int dirIdx = 1; dirIdx < nb; dirIdx++) { | |
145 if (dirs[dirIdx] == null) { | |
146 continue; | |
147 } | |
148 // read the directory | |
149 if (dirs[dirIdx].getFilenames() == null) { | |
150 dirs[dirIdx].readDir(); | |
151 } | |
152 String[] dirFiles = dirs[dirIdx].getFilenames(); | |
153 // try the same filename as the original | |
154 int fileIdx = Arrays.binarySearch(dirFiles, fn); | |
155 if (fileIdx < 0) { | |
156 // try closest matches without extension | |
157 fileIdx = -fileIdx - 1; | |
158 // try idx | |
159 if ((fileIdx < dirFiles.length) | |
160 && (FileOps.basename(dirFiles[fileIdx]).equals(baseFn))) { | |
161 // idx ok | |
162 } else if ((fileIdx > 0) | |
163 && (FileOps.basename(dirFiles[fileIdx - 1]) | |
164 .equals(baseFn))) { | |
165 // idx-1 ok | |
166 fileIdx = fileIdx - 1; | |
167 } else if ((fileIdx+1 < dirFiles.length) | |
168 && (FileOps.basename(dirFiles[fileIdx + 1]) | |
169 .equals(baseFn))) { | |
170 // idx+1 ok | |
171 fileIdx = fileIdx + 1; | |
172 } else { | |
173 // basename doesn't match | |
174 continue; | |
175 } | |
176 } | |
177 if (FileOps.classForFilename(dirFiles[fileIdx]) == fileClass) { | |
178 /* logger.debug("adding file " + dirFiles[fileIdx] | |
179 + " to Fileset " + this.getName()); */ | |
180 add(new ImageFile(dirFiles[fileIdx], this, dirs[dirIdx])); | |
181 } | |
182 } | |
183 } | |
184 | |
185 /** | |
186 * Checks metadata and sets resolution in resX and resY. | |
187 * | |
188 */ | |
189 public void checkMeta() { | |
190 if (metaChecked) { | |
191 return; | |
192 } | |
193 if (fileMeta == null) { | |
194 // try to read metadata file | |
195 readMeta(); | |
196 if (fileMeta == null) { | |
197 // try directory metadata | |
198 ((DocuDirectory) parent).checkMeta(); | |
199 if (((DocuDirectory) parent).getDirMeta() != null) { | |
200 fileMeta = ((DocuDirectory) parent).getDirMeta(); | |
201 } else { | |
202 // try parent directory metadata | |
203 DocuDirectory gp = (DocuDirectory) parent.getParent(); | |
204 if (gp != null) { | |
205 gp.checkMeta(); | |
206 if (gp.getDirMeta() != null) { | |
207 fileMeta = gp.getDirMeta(); | |
208 } | |
209 } | |
210 } | |
211 } | |
212 } | |
213 if (fileMeta == null) { | |
214 // no metadata available | |
215 metaChecked = true; | |
216 return; | |
217 } | |
218 metaChecked = true; | |
219 float dpi = 0; | |
220 float dpix = 0; | |
221 float dpiy = 0; | |
222 float sizex = 0; | |
223 float sizey = 0; | |
224 float pixx = 0; | |
225 float pixy = 0; | |
226 // DPI is valid for X and Y | |
227 if (fileMeta.containsKey("original-dpi")) { | |
228 try { | |
229 dpi = Float.parseFloat((String) fileMeta.get("original-dpi")); | |
230 } catch (NumberFormatException e) { | |
231 } | |
232 if (dpi != 0) { | |
233 resX = dpi; | |
234 resY = dpi; | |
235 return; | |
236 } | |
237 } | |
238 // DPI-X and DPI-Y | |
239 if (fileMeta.containsKey("original-dpi-x") | |
240 && fileMeta.containsKey("original-dpi-y")) { | |
241 try { | |
242 dpix = Float.parseFloat((String) fileMeta | |
243 .get("original-dpi-x")); | |
244 dpiy = Float.parseFloat((String) fileMeta | |
245 .get("original-dpi-y")); | |
246 } catch (NumberFormatException e) { | |
247 } | |
248 if ((dpix != 0) && (dpiy != 0)) { | |
249 resX = dpix; | |
250 resY = dpiy; | |
251 return; | |
252 } | |
253 } | |
254 // SIZE-X and SIZE-Y and PIXEL-X and PIXEL-Y | |
255 if (fileMeta.containsKey("original-size-x") | |
256 && fileMeta.containsKey("original-size-y") | |
257 && fileMeta.containsKey("original-pixel-x") | |
258 && fileMeta.containsKey("original-pixel-y")) { | |
259 try { | |
260 sizex = Float.parseFloat((String) fileMeta | |
261 .get("original-size-x")); | |
262 sizey = Float.parseFloat((String) fileMeta | |
263 .get("original-size-y")); | |
264 pixx = Float.parseFloat((String) fileMeta | |
265 .get("original-pixel-x")); | |
266 pixy = Float.parseFloat((String) fileMeta | |
267 .get("original-pixel-y")); | |
268 } catch (NumberFormatException e) { | |
269 } | |
270 if ((sizex != 0) && (sizey != 0) && (pixx != 0) && (pixy != 0)) { | |
271 resX = pixx / (sizex * 100 / 2.54f); | |
272 resY = pixy / (sizey * 100 / 2.54f); | |
273 return; | |
274 } | |
275 } | |
276 } | |
277 | |
278 public void readMeta() { | |
279 // TODO Auto-generated method stub | |
280 | |
281 } | |
282 | |
283 } |