531
|
1 /* ImageFileset -- digilib image file info class.
|
|
2 * Digital Image Library servlet components
|
|
3 * Copyright (C) 2003 Robert Casties (robcast@mail.berlios.de)
|
|
4 *
|
|
5 * This program is free software; you can
|
|
6 * redistribute it and/or modify it under the terms of the GNU General Public
|
|
7 * License as published by the Free Software Foundation; either version 2 of
|
|
8 * the License, or (at your option) any later version.
|
|
9 *
|
|
10 * Please read license.txt for the full details. A copy of the GPL may be
|
|
11 * found at http://www.gnu.org/copyleft/lgpl.html
|
|
12 *
|
|
13 * You should have received a copy of the GNU General Public License along
|
|
14 * with this program; if not, write to the Free Software Foundation, Inc.,
|
|
15 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
16 */
|
|
17
|
|
18 package digilib.io;
|
|
19
|
|
20 import java.io.File;
|
|
21 import java.io.IOException;
|
|
22 import java.util.ArrayList;
|
|
23 import java.util.Arrays;
|
|
24 import java.util.Iterator;
|
|
25 import java.util.List;
|
|
26 import java.util.ListIterator;
|
|
27 import java.util.Map;
|
|
28
|
|
29 import digilib.image.ImageOps;
|
|
30 import digilib.image.ImageSize;
|
|
31
|
|
32 /**
|
|
33 * @author casties
|
|
34 */
|
|
35 public class ImageFileset extends DocuDirent {
|
|
36
|
|
37 /** this is an image file */
|
|
38 protected static int fileClass = FileOps.CLASS_IMAGE;
|
|
39
|
|
40 /** list of files (ImageFile) */
|
|
41 private List<ImageFile> list = null;
|
|
42
|
|
43 /** aspect ratio (width/height) */
|
|
44 private float aspect = 0;
|
|
45
|
|
46 /** resolution of the biggest image (DPI) */
|
|
47 private float resX = 0;
|
|
48
|
|
49 /** resolution of the biggest image (DPI) */
|
|
50 private float resY = 0;
|
|
51
|
|
52 /**
|
|
53 * Creator for empty fileset.
|
|
54 *
|
|
55 *
|
|
56 * @param initialCapacity
|
|
57 */
|
|
58 public ImageFileset() {
|
|
59 list = new ArrayList<ImageFile>();
|
|
60 }
|
|
61
|
|
62 /**
|
|
63 * Constructor with a file and hints.
|
|
64 *
|
|
65 * The hints are expected to contain 'basedirs' and 'scaledfilext' keys.
|
|
66 *
|
|
67 * @param file
|
|
68 * @param hints
|
|
69 */
|
|
70 public ImageFileset(File file, Map<Integer,Object> hints) {
|
|
71 Directory[] dirs = (Directory[]) hints.get(FileOps.HINT_BASEDIRS);
|
|
72 int nb = dirs.length;
|
|
73 list = new ArrayList<ImageFile>(nb);
|
|
74 parent = dirs[0];
|
|
75 fill(dirs, file, hints);
|
|
76 }
|
|
77
|
|
78 /**
|
|
79 * Adds an ImageFile to this Fileset.
|
|
80 *
|
|
81 * The files should be added in the order of higher to lower resolutions.
|
|
82 * The first file is considered the hires "original".
|
|
83 *
|
|
84 *
|
|
85 * @param f
|
|
86 * file to add
|
|
87 * @return true (always)
|
|
88 */
|
|
89 public boolean add(ImageFile f) {
|
|
90 f.setParent(this);
|
|
91 return list.add(f);
|
|
92 }
|
|
93
|
|
94 /**
|
|
95 * The number of image files in this Fileset.
|
|
96 *
|
|
97 *
|
|
98 * @return number of image files
|
|
99 */
|
|
100 public int size() {
|
|
101 return (list != null) ? list.size() : 0;
|
|
102 }
|
|
103
|
|
104 /**
|
|
105 * Gets the default File.
|
|
106 *
|
|
107 */
|
|
108 public File getFile() {
|
|
109 return (list != null) ? list.get(0).getFile() : null;
|
|
110 }
|
|
111
|
|
112 /**
|
|
113 * Get the ImageFile at the index.
|
|
114 *
|
|
115 *
|
|
116 * @param index
|
|
117 * @return
|
|
118 */
|
|
119 public ImageFile get(int index) {
|
|
120 return list.get(index);
|
|
121 }
|
|
122
|
|
123 /**
|
|
124 * Get the next smaller ImageFile than the given size.
|
|
125 *
|
|
126 * Returns the ImageFile from the set that has a width and height smaller or
|
|
127 * equal the given size. Returns null if there isn't any smaller image.
|
|
128 * Needs DocuInfo instance to checkFile().
|
|
129 *
|
|
130 *
|
|
131 * @param size
|
|
132 * @param info
|
|
133 * @return
|
|
134 */
|
|
135 public ImageFile getNextSmaller(ImageSize size) {
|
|
136 for (Iterator<ImageFile> i = getHiresIterator(); i.hasNext();) {
|
|
137 ImageFile f = i.next();
|
|
138 try {
|
|
139 if (!f.isChecked()) {
|
|
140 ImageOps.checkFile(f);
|
|
141 }
|
|
142 if (f.getSize().isTotallySmallerThan(size)) {
|
|
143 return f;
|
|
144 }
|
|
145 } catch (IOException e) {
|
|
146 }
|
|
147 }
|
|
148 return null;
|
|
149 }
|
|
150
|
|
151 /**
|
|
152 * Get the next bigger ImageFile than the given size.
|
|
153 *
|
|
154 * Returns the ImageFile from the set that has a width or height bigger or
|
|
155 * equal the given size. Returns null if there isn't any bigger image. Needs
|
|
156 * DocuInfo instance to checkFile().
|
|
157 *
|
|
158 *
|
|
159 * @param size
|
|
160 * @param info
|
|
161 * @return
|
|
162 */
|
|
163 public ImageFile getNextBigger(ImageSize size) {
|
|
164 for (ListIterator<ImageFile> i = getLoresIterator(); i.hasPrevious();) {
|
|
165 ImageFile f = i.previous();
|
|
166 try {
|
|
167 if (!f.isChecked()) {
|
|
168 ImageOps.checkFile(f);
|
|
169 }
|
|
170 if (f.getSize().isBiggerThan(size)) {
|
|
171 return f;
|
|
172 }
|
|
173 } catch (IOException e) {
|
|
174 }
|
|
175 }
|
|
176 return null;
|
|
177 }
|
|
178
|
|
179 /**
|
|
180 * Returns the biggest ImageFile in the set.
|
|
181 *
|
|
182 *
|
|
183 * @return
|
|
184 */
|
|
185 public ImageFile getBiggest() {
|
|
186 return this.get(0);
|
|
187 }
|
|
188
|
|
189 /**
|
|
190 * Returns the biggest ImageFile in the set.
|
|
191 *
|
|
192 *
|
|
193 * @return
|
|
194 */
|
|
195 public ImageFile getSmallest() {
|
|
196 return this.get(this.size() - 1);
|
|
197 }
|
|
198
|
|
199 /**
|
|
200 * Get an Iterator for this Fileset starting at the highest resolution
|
|
201 * images.
|
|
202 *
|
|
203 *
|
|
204 * @return
|
|
205 */
|
|
206 public ListIterator<ImageFile> getHiresIterator() {
|
|
207 return list.listIterator();
|
|
208 }
|
|
209
|
|
210 /**
|
|
211 * Get an Iterator for this Fileset starting at the lowest resolution
|
|
212 * images.
|
|
213 *
|
|
214 * The Iterator starts at the last element, so you have to use it backwards
|
|
215 * with hasPrevious() and previous().
|
|
216 *
|
|
217 *
|
|
218 * @return
|
|
219 */
|
|
220 public ListIterator<ImageFile> getLoresIterator() {
|
|
221 return list.listIterator(list.size());
|
|
222 }
|
|
223
|
|
224 /**
|
|
225 * Fill the ImageFileset with files from different base directories.
|
|
226 *
|
|
227 *
|
|
228 * @param dirs
|
|
229 * list of base directories
|
|
230 * @param fl
|
|
231 * file (from first base dir)
|
|
232 * @param hints
|
|
233 *
|
|
234 */
|
|
235 void fill(Directory[] dirs, File fl, Map<Integer,Object> hints) {
|
|
236 int nb = dirs.length;
|
|
237 String fn = fl.getName();
|
|
238 String baseFn = FileOps.basename(fn);
|
|
239 // add the first ImageFile to the ImageFileset
|
|
240 add(new ImageFile(fn, this, parent));
|
|
241 // iterate the remaining base directories
|
|
242 for (int dirIdx = 1; dirIdx < nb; dirIdx++) {
|
|
243 if (dirs[dirIdx] == null) {
|
|
244 continue;
|
|
245 }
|
|
246 // read the directory
|
|
247 if (dirs[dirIdx].getFilenames() == null) {
|
|
248 dirs[dirIdx].readDir();
|
|
249 }
|
|
250 String[] dirFiles = dirs[dirIdx].getFilenames();
|
|
251 // try the same filename as the original
|
|
252 int fileIdx = Arrays.binarySearch(dirFiles, fn);
|
|
253 if (fileIdx < 0) {
|
|
254 // try closest matches without extension
|
|
255 fileIdx = -fileIdx - 1;
|
|
256 // try idx
|
|
257 if ((fileIdx < dirFiles.length)
|
|
258 && (FileOps.basename(dirFiles[fileIdx]).equals(baseFn))) {
|
|
259 // idx ok
|
|
260 } else if ((fileIdx > 0)
|
|
261 && (FileOps.basename(dirFiles[fileIdx - 1])
|
|
262 .equals(baseFn))) {
|
|
263 // idx-1 ok
|
|
264 fileIdx = fileIdx - 1;
|
|
265 } else if ((fileIdx+1 < dirFiles.length)
|
|
266 && (FileOps.basename(dirFiles[fileIdx + 1])
|
|
267 .equals(baseFn))) {
|
|
268 // idx+1 ok
|
|
269 fileIdx = fileIdx + 1;
|
|
270 } else {
|
|
271 // basename doesn't match
|
|
272 continue;
|
|
273 }
|
|
274 }
|
|
275 if (FileOps.classForFilename(dirFiles[fileIdx]) == FileOps.CLASS_IMAGE) {
|
|
276 /* logger.debug("adding file " + dirFiles[fileIdx]
|
|
277 + " to Fileset " + this.getName()); */
|
|
278 add(new ImageFile(dirFiles[fileIdx], this, dirs[dirIdx]));
|
|
279 }
|
|
280 }
|
|
281 }
|
|
282
|
|
283 /**
|
|
284 * Checks metadata and sets resolution in resX and resY.
|
|
285 *
|
|
286 */
|
|
287 public void checkMeta() {
|
|
288 if (metaChecked) {
|
|
289 return;
|
|
290 }
|
|
291 if (fileMeta == null) {
|
|
292 // try to read metadata file
|
|
293 readMeta();
|
|
294 if (fileMeta == null) {
|
|
295 // try directory metadata
|
|
296 ((DocuDirectory) parent).checkMeta();
|
|
297 if (((DocuDirectory) parent).getDirMeta() != null) {
|
|
298 fileMeta = ((DocuDirectory) parent).getDirMeta();
|
|
299 } else {
|
|
300 // try parent directory metadata
|
|
301 DocuDirectory gp = (DocuDirectory) parent.getParent();
|
|
302 if (gp != null) {
|
|
303 gp.checkMeta();
|
|
304 if (gp.getDirMeta() != null) {
|
|
305 fileMeta = gp.getDirMeta();
|
|
306 }
|
|
307 }
|
|
308 }
|
|
309 }
|
|
310 }
|
|
311 if (fileMeta == null) {
|
|
312 // no metadata available
|
|
313 metaChecked = true;
|
|
314 return;
|
|
315 }
|
|
316 metaChecked = true;
|
|
317 float dpi = 0;
|
|
318 float dpix = 0;
|
|
319 float dpiy = 0;
|
|
320 float sizex = 0;
|
|
321 float sizey = 0;
|
|
322 float pixx = 0;
|
|
323 float pixy = 0;
|
|
324 // DPI is valid for X and Y
|
|
325 if (fileMeta.containsKey("original-dpi")) {
|
|
326 try {
|
|
327 dpi = Float.parseFloat((String) fileMeta.get("original-dpi"));
|
|
328 } catch (NumberFormatException e) {
|
|
329 }
|
|
330 if (dpi != 0) {
|
|
331 resX = dpi;
|
|
332 resY = dpi;
|
|
333 return;
|
|
334 }
|
|
335 }
|
|
336 // DPI-X and DPI-Y
|
|
337 if (fileMeta.containsKey("original-dpi-x")
|
|
338 && fileMeta.containsKey("original-dpi-y")) {
|
|
339 try {
|
|
340 dpix = Float.parseFloat((String) fileMeta
|
|
341 .get("original-dpi-x"));
|
|
342 dpiy = Float.parseFloat((String) fileMeta
|
|
343 .get("original-dpi-y"));
|
|
344 } catch (NumberFormatException e) {
|
|
345 }
|
|
346 if ((dpix != 0) && (dpiy != 0)) {
|
|
347 resX = dpix;
|
|
348 resY = dpiy;
|
|
349 return;
|
|
350 }
|
|
351 }
|
|
352 // SIZE-X and SIZE-Y and PIXEL-X and PIXEL-Y
|
|
353 if (fileMeta.containsKey("original-size-x")
|
|
354 && fileMeta.containsKey("original-size-y")
|
|
355 && fileMeta.containsKey("original-pixel-x")
|
|
356 && fileMeta.containsKey("original-pixel-y")) {
|
|
357 try {
|
|
358 sizex = Float.parseFloat((String) fileMeta
|
|
359 .get("original-size-x"));
|
|
360 sizey = Float.parseFloat((String) fileMeta
|
|
361 .get("original-size-y"));
|
|
362 pixx = Float.parseFloat((String) fileMeta
|
|
363 .get("original-pixel-x"));
|
|
364 pixy = Float.parseFloat((String) fileMeta
|
|
365 .get("original-pixel-y"));
|
|
366 } catch (NumberFormatException e) {
|
|
367 }
|
|
368 if ((sizex != 0) && (sizey != 0) && (pixx != 0) && (pixy != 0)) {
|
|
369 resX = pixx / (sizex * 100 / 2.54f);
|
|
370 resY = pixy / (sizey * 100 / 2.54f);
|
|
371 return;
|
|
372 }
|
|
373 }
|
|
374 }
|
|
375
|
|
376 /**
|
|
377 * @return
|
|
378 */
|
|
379 public float getResX() {
|
|
380 return resX;
|
|
381 }
|
|
382
|
|
383 /**
|
|
384 * @return
|
|
385 */
|
|
386 public float getResY() {
|
|
387 return resY;
|
|
388 }
|
|
389
|
|
390 /**
|
|
391 * Sets the aspect ratio from an ImageSize.
|
|
392 *
|
|
393 *
|
|
394 * @param f
|
|
395 */
|
|
396 public void setAspect(ImageSize s) {
|
|
397 aspect = s.getAspect();
|
|
398 }
|
|
399
|
|
400 /**
|
|
401 * Returns the aspect ratio.
|
|
402 *
|
|
403 * Aspect ratio is (width/height). So it's <1 for portrait and >1 for
|
|
404 * landscape.
|
|
405 *
|
|
406 *
|
|
407 * @return
|
|
408 */
|
|
409 public float getAspect() {
|
|
410 return aspect;
|
|
411 }
|
|
412
|
|
413 } |