339
|
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.Iterator;
|
|
24 import java.util.List;
|
|
25 import java.util.ListIterator;
|
|
26 import java.util.Map;
|
|
27
|
|
28 import digilib.image.ImageOps;
|
|
29 import digilib.image.ImageSize;
|
|
30
|
|
31 /**
|
|
32 * @author casties
|
|
33 */
|
|
34 public class ImageFileset extends DigiDirent {
|
|
35
|
|
36 /** this is an image file */
|
|
37 protected static int FILE_CLASS = FileOps.CLASS_IMAGE;
|
|
38
|
|
39 /** list of files (ImageFile) */
|
|
40 private List files;
|
|
41
|
|
42 /** resolution of the biggest image (DPI) */
|
|
43 private float resX = 0;
|
|
44
|
|
45 /** resolution of the biggest image (DPI) */
|
|
46 private float resY = 0;
|
|
47
|
|
48 /**
|
|
49 * Constructor with a file and hints.
|
|
50 *
|
|
51 * The hints are expected to contain 'basedirs' and 'scaledfilext' keys.
|
|
52 *
|
|
53 * @param file
|
|
54 * @param hints
|
|
55 */
|
|
56 public ImageFileset(File file, DigiDirectory parent, Map hints) {
|
|
57 super(file.getName(), parent);
|
|
58 files = new ArrayList(FileOps.getBaseDirs().length);
|
|
59 fill(file, hints);
|
|
60 }
|
|
61
|
|
62 /**
|
|
63 * Gets the default File.
|
|
64 *
|
|
65 */
|
|
66 public ImageFile getFile() {
|
|
67 return (files != null) ? (ImageFile) files.get(0) : null;
|
|
68 }
|
|
69
|
|
70 /**
|
|
71 * Get the ImageFile at the index.
|
|
72 *
|
|
73 *
|
|
74 * @param index
|
|
75 * @return
|
|
76 */
|
|
77 public ImageFile get(int index) {
|
|
78 return (files != null) ? (ImageFile) files.get(index) : null;
|
|
79 }
|
|
80
|
|
81 /**
|
|
82 * Get the next smaller ImageFile than the given size.
|
|
83 *
|
|
84 * Returns the ImageFile from the set that has a width and height smaller or
|
|
85 * equal the given size. Returns null if there isn't any smaller image.
|
|
86 * Needs DocuInfo instance to checkFile().
|
|
87 *
|
|
88 *
|
|
89 * @param size
|
|
90 * @param info
|
|
91 * @return
|
|
92 */
|
|
93 public ImageFile getNextSmaller(ImageSize size) {
|
|
94 for (Iterator i = getHiresIterator(); i.hasNext();) {
|
|
95 ImageFile f = (ImageFile) i.next();
|
|
96 try {
|
|
97 ImageOps.checkFile(f);
|
|
98 if (f.getSize().isTotallySmallerThan(size)) {
|
|
99 return f;
|
|
100 }
|
|
101 } catch (IOException e) {
|
|
102 }
|
|
103 }
|
|
104 return null;
|
|
105 }
|
|
106
|
|
107 /**
|
|
108 * Get the next bigger ImageFile than the given size.
|
|
109 *
|
|
110 * Returns the ImageFile from the set that has a width or height bigger or
|
|
111 * equal the given size. Returns null if there isn't any bigger image. Needs
|
|
112 * DocuInfo instance to checkFile().
|
|
113 *
|
|
114 *
|
|
115 * @param size
|
|
116 * @param info
|
|
117 * @return
|
|
118 */
|
|
119 public ImageFile getNextBigger(ImageSize size) {
|
|
120 for (ListIterator i = getLoresIterator(); i.hasPrevious();) {
|
|
121 ImageFile f = (ImageFile) i.previous();
|
|
122 try {
|
|
123 ImageOps.checkFile(f);
|
|
124 if (f.getSize().isBiggerThan(size)) {
|
|
125 return f;
|
|
126 }
|
|
127 } catch (IOException e) {
|
|
128 }
|
|
129 }
|
|
130 return null;
|
|
131 }
|
|
132
|
|
133 /**
|
|
134 * Returns the biggest ImageFile in the set.
|
|
135 *
|
|
136 *
|
|
137 * @return
|
|
138 */
|
|
139 public ImageFile getBiggest() {
|
|
140 return (ImageFile) files.get(0);
|
|
141 }
|
|
142
|
|
143 /**
|
|
144 * Returns the biggest ImageFile in the set.
|
|
145 *
|
|
146 *
|
|
147 * @return
|
|
148 */
|
|
149 public ImageFile getSmallest() {
|
|
150 return (ImageFile) files.get(files.size() - 1);
|
|
151 }
|
|
152
|
|
153 /**
|
|
154 * Get an Iterator for this Fileset starting at the highest resolution
|
|
155 * images.
|
|
156 *
|
|
157 *
|
|
158 * @return
|
|
159 */
|
|
160 public ListIterator getHiresIterator() {
|
|
161 return files.listIterator();
|
|
162 }
|
|
163
|
|
164 /**
|
|
165 * Get an Iterator for this Fileset starting at the lowest resolution
|
|
166 * images.
|
|
167 *
|
|
168 * The Iterator starts at the last element, so you have to use it backwards
|
|
169 * with hasPrevious() and previous().
|
|
170 *
|
|
171 *
|
|
172 * @return
|
|
173 */
|
|
174 public ListIterator getLoresIterator() {
|
|
175 return files.listIterator(files.size());
|
|
176 }
|
|
177
|
|
178 /**
|
|
179 * Fill the ImageFileset with files from different base directories.
|
|
180 *
|
|
181 *
|
|
182 * @param dirs
|
|
183 * list of base directories
|
|
184 * @param imf
|
|
185 * file (from first base dir)
|
|
186 * @param hints
|
|
187 *
|
|
188 */
|
|
189 void fill(File imf, Map hints) {
|
|
190 File[][] scaledirs = (File[][]) hints.get(FileOps.HINT_BASEDIRS);
|
|
191 File[] bd = FileOps.getBaseDirs();
|
|
192 int nb = bd.length;
|
|
193 if (scaledirs == null) {
|
|
194 // read all scaled directories
|
|
195 scaledirs = new File[nb][];
|
|
196 for (int i = 1; i < nb; i++) {
|
|
197 // check basedir + digilib path
|
|
198 File d = FileOps.getRealFile(bd[i], parent.getDLPath());
|
|
199 scaledirs[i] = d.listFiles();
|
|
200 }
|
|
201 hints.put(FileOps.HINT_BASEDIRS, scaledirs);
|
|
202 }
|
|
203 // add the first ImageFile to the ImageFileset
|
|
204 files.add(new ImageFile(imf));
|
|
205 // iterate the remaining base directories
|
|
206 for (int dirIdx = 1; dirIdx < nb; dirIdx++) {
|
|
207 if (scaledirs[dirIdx] == null) {
|
|
208 continue;
|
|
209 }
|
|
210 // find the file in the directory
|
|
211 File fn = FileOps.findFile(imf, scaledirs[dirIdx]);
|
|
212 if (fn == null) {
|
|
213 continue;
|
|
214 }
|
|
215 if (FileOps.classForFile(fn) == FileOps.CLASS_IMAGE) {
|
|
216 // add to the fileset
|
|
217 files.add(new ImageFile(fn));
|
|
218 }
|
|
219 }
|
|
220 }
|
|
221
|
|
222 /**
|
|
223 * Reads metadata and sets resolution in resX and resY.
|
|
224 *
|
|
225 */
|
|
226 public void readMeta() {
|
|
227 if (isMetaRead) {
|
|
228 return;
|
|
229 }
|
|
230 // read the metadata file
|
|
231 super.readMeta();
|
|
232 if (meta == null) {
|
|
233 // try directory metadata
|
|
234 meta = parent.getMeta();
|
|
235 if (meta == null) {
|
|
236 // no metadata available
|
|
237 isMetaRead = true;
|
|
238 return;
|
|
239 }
|
|
240 }
|
|
241 isMetaRead = true;
|
|
242 String s;
|
|
243 float dpi = 0;
|
|
244 float dpix = 0;
|
|
245 float dpiy = 0;
|
|
246 float sizex = 0;
|
|
247 float sizey = 0;
|
|
248 float pixx = 0;
|
|
249 float pixy = 0;
|
|
250 // DPI is valid for X and Y
|
|
251 if (meta.containsKey("original-dpi")) {
|
|
252 try {
|
|
253 dpi = Float.parseFloat((String) meta.get("original-dpi"));
|
|
254 } catch (NumberFormatException e) {
|
|
255 }
|
|
256 if (dpi != 0) {
|
|
257 resX = dpi;
|
|
258 resY = dpi;
|
|
259 return;
|
|
260 }
|
|
261 }
|
|
262 // DPI-X and DPI-Y
|
|
263 if (meta.containsKey("original-dpi-x")
|
|
264 && meta.containsKey("original-dpi-y")) {
|
|
265 try {
|
|
266 dpix = Float.parseFloat((String) meta.get("original-dpi-x"));
|
|
267 dpiy = Float.parseFloat((String) meta.get("original-dpi-y"));
|
|
268 } catch (NumberFormatException e) {
|
|
269 }
|
|
270 if ((dpix != 0) && (dpiy != 0)) {
|
|
271 resX = dpix;
|
|
272 resY = dpiy;
|
|
273 return;
|
|
274 }
|
|
275 }
|
|
276 // SIZE-X and SIZE-Y and PIXEL-X and PIXEL-Y
|
|
277 if (meta.containsKey("original-size-x")
|
|
278 && meta.containsKey("original-size-y")
|
|
279 && meta.containsKey("original-pixel-x")
|
|
280 && meta.containsKey("original-pixel-y")) {
|
|
281 try {
|
|
282 sizex = Float.parseFloat((String) meta.get("original-size-x"));
|
|
283 sizey = Float.parseFloat((String) meta.get("original-size-y"));
|
|
284 pixx = Float.parseFloat((String) meta.get("original-pixel-x"));
|
|
285 pixy = Float.parseFloat((String) meta.get("original-pixel-y"));
|
|
286 } catch (NumberFormatException e) {
|
|
287 }
|
|
288 if ((sizex != 0) && (sizey != 0) && (pixx != 0) && (pixy != 0)) {
|
|
289 resX = pixx / (sizex * 100 / 2.54f);
|
|
290 resY = pixy / (sizey * 100 / 2.54f);
|
|
291 return;
|
|
292 }
|
|
293 }
|
|
294 }
|
|
295
|
|
296 /**
|
|
297 * Returns the aspect ratio of the images.
|
|
298 *
|
|
299 * @return
|
|
300 */
|
|
301 public float getAspect() {
|
|
302 for (Iterator i = files.iterator(); i.hasNext();) {
|
|
303 ImageFile f = (ImageFile) i.next();
|
|
304 ImageSize s = f.getSize();
|
|
305 if (s != null) {
|
|
306 return s.getAspect();
|
|
307 }
|
|
308 }
|
|
309 return 0f;
|
|
310 }
|
|
311
|
|
312 /**
|
|
313 * @return
|
|
314 */
|
|
315 public float getResX() {
|
|
316 return resX;
|
|
317 }
|
|
318
|
|
319 /**
|
|
320 * @return
|
|
321 */
|
|
322 public float getResY() {
|
|
323 return resY;
|
|
324 }
|
|
325
|
|
326 } |