159
|
1 /* ImageFileset -- digilib image file info class.
|
|
2
|
|
3 Digital Image Library servlet components
|
|
4
|
|
5 Copyright (C) 2003 Robert Casties (robcast@mail.berlios.de)
|
|
6
|
|
7 This program is free software; you can redistribute it and/or modify it
|
|
8 under the terms of the GNU General Public License as published by the
|
|
9 Free Software Foundation; either version 2 of the License, or (at your
|
|
10 option) any later version.
|
|
11
|
|
12 Please read license.txt for the full details. A copy of the GPL
|
|
13 may be found at http://www.gnu.org/copyleft/lgpl.html
|
|
14
|
|
15 You should have received a copy of the GNU General Public License
|
|
16 along with this program; if not, write to the Free Software
|
|
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
18
|
|
19 */
|
|
20 package digilib.io;
|
|
21
|
|
22 import java.io.File;
|
|
23 import java.io.IOException;
|
|
24 import java.util.ArrayList;
|
|
25 import java.util.Iterator;
|
|
26 import java.util.ListIterator;
|
|
27
|
|
28 import digilib.image.DocuInfo;
|
|
29 import digilib.image.ImageSize;
|
|
30
|
|
31 /**
|
|
32 * @author casties
|
|
33 */
|
|
34 public class ImageFileset extends DocuDirent {
|
|
35
|
|
36 // this is an image file
|
|
37 protected static int fileClass = FileOps.CLASS_IMAGE;
|
|
38
|
|
39 // list of files (ImageFile)
|
|
40 private ArrayList list = null;
|
|
41 // resolution (DPI)
|
|
42 private double resX = 0;
|
|
43 private double resY = 0;
|
|
44
|
|
45 /** Creator for empty fileset with size for file list.
|
|
46 *
|
|
47 * @param initialCapacity
|
|
48 */
|
|
49 public ImageFileset(int initialCapacity) {
|
|
50 list = new ArrayList(initialCapacity);
|
|
51 }
|
|
52
|
|
53 /** Creator with a file and base directories.
|
|
54 *
|
|
55 * Reads the file and fills the
|
|
56 * fileset with corresponding files from the other base directories.
|
|
57 * First entry in dirs is the parent of this fileset.
|
|
58 *
|
|
59 * @see fill
|
|
60 *
|
|
61 * @param dirs array of base directories
|
|
62 * @param file first file to read
|
|
63 * @param scalext extension for scaled images
|
|
64 */
|
|
65 public ImageFileset(Directory[] dirs, File file, String scalext) {
|
|
66 int nb = dirs.length;
|
|
67 list = new ArrayList(nb);
|
|
68 parent = dirs[0];
|
|
69 fill(dirs, file, scalext);
|
|
70 }
|
|
71
|
|
72 /** Adds a ImageFile to this Fileset.
|
|
73 *
|
|
74 * The files should be added in the order of higher to lower resolutions.
|
|
75 * The first file is considered the hires "original".
|
|
76 *
|
|
77 * @param f file to add
|
|
78 * @return true (always)
|
|
79 */
|
|
80 public boolean add(ImageFile f) {
|
|
81 f.setParent(this);
|
|
82 return list.add(f);
|
|
83 }
|
|
84
|
|
85 /** The number of image files in this Fileset.
|
|
86 *
|
|
87 * @return number of image files
|
|
88 */
|
|
89 public int size() {
|
|
90 return (list != null) ? list.size() : 0;
|
|
91 }
|
|
92
|
|
93 /** Gets the default File.
|
|
94 *
|
|
95 */
|
|
96 public File getFile() {
|
|
97 return (list != null) ? ((ImageFile) list.get(0)).getFile() : null;
|
|
98 }
|
|
99
|
|
100 /** Get the ImageFile at the index.
|
|
101 *
|
|
102 * @param index
|
|
103 * @return
|
|
104 */
|
|
105 public ImageFile get(int index) {
|
|
106 return (ImageFile) list.get(index);
|
|
107 }
|
|
108
|
|
109 /** Get the next smaller ImageFile than the given size.
|
|
110 *
|
|
111 * Returns the ImageFile from the set that has a width and height
|
|
112 * smaller or equal the given size.
|
|
113 * Returns null if there isn't any smaller image.
|
|
114 * Needs DocuInfo instance to checkFile().
|
|
115 *
|
|
116 * @param size
|
|
117 * @param info
|
|
118 * @return
|
|
119 */
|
|
120 public ImageFile getNextSmaller(ImageSize size, DocuInfo info) {
|
|
121 for (Iterator i = getHiresIterator(); i.hasNext();) {
|
|
122 ImageFile f = (ImageFile) i.next();
|
|
123 try {
|
|
124 if (!f.isChecked()) {
|
|
125 info.checkFile(f);
|
|
126 }
|
|
127 if (f.getSize().isTotallySmallerThan(size)) {
|
|
128 return f;
|
|
129 }
|
|
130 } catch (IOException e) {
|
|
131 // TODO Auto-generated catch block
|
|
132 e.printStackTrace();
|
|
133 }
|
|
134 }
|
|
135 return null;
|
|
136 }
|
|
137
|
|
138 /** Get the next bigger ImageFile than the given size.
|
|
139 *
|
|
140 * Returns the ImageFile from the set that has a width or height
|
|
141 * bigger or equal the given size.
|
|
142 * Returns null if there isn't any bigger image.
|
|
143 * Needs DocuInfo instance to checkFile().
|
|
144 *
|
|
145 * @param size
|
|
146 * @param info
|
|
147 * @return
|
|
148 */
|
|
149 public ImageFile getNextBigger(ImageSize size, DocuInfo info) {
|
|
150 for (ListIterator i = getLoresIterator(); i.hasPrevious();) {
|
|
151 ImageFile f = (ImageFile) i.previous();
|
|
152 try {
|
|
153 if (!f.isChecked()) {
|
|
154 info.checkFile(f);
|
|
155 }
|
|
156 if (f.getSize().isBiggerThan(size)) {
|
|
157 return f;
|
|
158 }
|
|
159 } catch (IOException e) {
|
|
160 // TODO Auto-generated catch block
|
|
161 e.printStackTrace();
|
|
162 }
|
|
163 }
|
|
164 return null;
|
|
165 }
|
|
166
|
|
167 /** Get an Iterator for this Fileset starting at the highest resolution
|
|
168 * images.
|
|
169 *
|
|
170 * @return
|
|
171 */
|
|
172 public ListIterator getHiresIterator() {
|
|
173 return list.listIterator();
|
|
174 }
|
|
175
|
|
176 /** Get an Iterator for this Fileset starting at the lowest resolution
|
|
177 * images.
|
|
178 *
|
|
179 * The Iterator starts at the last element, so you have to use it backwards
|
|
180 * with hasPrevious() and previous().
|
|
181 *
|
|
182 * @return
|
|
183 */
|
|
184 public ListIterator getLoresIterator() {
|
|
185 return list.listIterator(list.size());
|
|
186 }
|
|
187
|
|
188 /** Fill the ImageFileset with files from different base directories.
|
|
189 *
|
|
190 * @param dirs list of base directories
|
|
191 * @param fl file (from first base dir)
|
|
192 * @param scalext first extension to try in other base dirs
|
|
193 */
|
|
194 void fill(Directory[] dirs, File fl, String scalext) {
|
|
195 int nb = dirs.length;
|
|
196 String fn = fl.getName();
|
|
197 String fnx = fn.substring(0, fn.lastIndexOf('.') + 1);
|
|
198 // add the first ImageFile to the ImageFileset
|
|
199 add(new ImageFile(fn, this, parent));
|
|
200 // iterate the remaining base directories
|
|
201 for (int j = 1; j < nb; j++) {
|
|
202 if (dirs[j] == null) {
|
|
203 continue;
|
|
204 }
|
|
205 File f;
|
|
206 if (scalext != null) {
|
|
207 // use the last extension
|
|
208 f = new File(dirs[j].getDir(), fnx + scalext);
|
|
209 } else {
|
|
210 // try the same filename as the original
|
|
211 f = new File(dirs[j].getDir(), fn);
|
|
212 }
|
|
213 // if the file exists, add to the ImageFileset
|
|
214 if (f.canRead()) {
|
|
215 add(new ImageFile(f.getName(), this, dirs[j]));
|
|
216 } else {
|
|
217 // try other file extensions
|
|
218 Iterator exts = FileOps.getImageExtensionIterator();
|
|
219 while (exts.hasNext()) {
|
|
220 String s = (String) exts.next();
|
|
221 f = new File(dirs[j].getDir(), fnx + s);
|
|
222 // if the file exists, add to the ImageFileset
|
|
223 if (f.canRead()) {
|
|
224 add(new ImageFile(f.getName(), this, dirs[j]));
|
|
225 scalext = s;
|
|
226 break;
|
|
227 }
|
|
228 }
|
|
229 }
|
|
230 }
|
|
231 }
|
|
232
|
|
233 /** Checks metadata and sets resolution in resX and resY.
|
|
234 *
|
|
235 */
|
|
236 public void checkMeta() {
|
|
237 if (metaChecked) {
|
|
238 return;
|
|
239 }
|
|
240 if (fileMeta == null) {
|
|
241 // try to read metadata file
|
|
242 readMeta();
|
|
243 if (fileMeta == null) {
|
|
244 // try directory metadata
|
|
245 if (((DocuDirectory) parent).getDirMeta() != null) {
|
|
246 fileMeta = ((DocuDirectory) parent).getDirMeta();
|
|
247 } else {
|
|
248 // no metadata available
|
|
249 metaChecked = true;
|
|
250 return;
|
|
251 }
|
|
252 }
|
|
253 }
|
|
254 metaChecked = true;
|
|
255 String s;
|
|
256 double dpi = 0;
|
|
257 double dpix = 0;
|
|
258 double dpiy = 0;
|
|
259 double sizex = 0;
|
|
260 double sizey = 0;
|
|
261 double pixx = 0;
|
|
262 double pixy = 0;
|
|
263 // DPI is valid for X and Y
|
|
264 if (fileMeta.containsKey("original-dpi")) {
|
|
265 try {
|
|
266 dpi = Double.parseDouble((String) fileMeta.get("original-dpi"));
|
|
267 } catch (NumberFormatException e) {
|
|
268 }
|
|
269 if (dpi != 0) {
|
|
270 resX = dpi;
|
|
271 resY = dpi;
|
|
272 return;
|
|
273 }
|
|
274 }
|
|
275 // DPI-X and DPI-Y
|
|
276 if (fileMeta.containsKey("original-dpi-x")
|
|
277 && fileMeta.containsKey("original-dpi-y")) {
|
|
278 try {
|
|
279 dpix =
|
|
280 Double.parseDouble((String) fileMeta.get("original-dpi-x"));
|
|
281 dpiy =
|
|
282 Double.parseDouble((String) fileMeta.get("original-dpi-y"));
|
|
283 } catch (NumberFormatException e) {
|
|
284 }
|
|
285 if ((dpix != 0) && (dpiy != 0)) {
|
|
286 resX = dpix;
|
|
287 resY = dpiy;
|
|
288 return;
|
|
289 }
|
|
290 }
|
|
291 // SIZE-X and SIZE-Y and PIXEL-X and PIXEL-Y
|
|
292 if (fileMeta.containsKey("original-size-x")
|
|
293 && fileMeta.containsKey("original-size-y")
|
|
294 && fileMeta.containsKey("original-pixel-x")
|
|
295 && fileMeta.containsKey("original-pixel-y")) {
|
|
296 try {
|
|
297 sizex =
|
|
298 Double.parseDouble(
|
|
299 (String) fileMeta.get("original-size-x"));
|
|
300 sizey =
|
|
301 Double.parseDouble(
|
|
302 (String) fileMeta.get("original-size-y"));
|
|
303 pixx =
|
|
304 Double.parseDouble(
|
|
305 (String) fileMeta.get("original-pixel-x"));
|
|
306 pixy =
|
|
307 Double.parseDouble(
|
|
308 (String) fileMeta.get("original-pixel-y"));
|
|
309 } catch (NumberFormatException e) {
|
|
310 }
|
|
311 if ((sizex != 0) && (sizey != 0) && (pixx != 0) && (pixy != 0)) {
|
|
312 resX = pixx / (sizex * 100 / 2.54);
|
|
313 resY = pixy / (sizey * 100 / 2.54);
|
|
314 return;
|
|
315 }
|
|
316 }
|
|
317 }
|
|
318
|
|
319 /**
|
|
320 * @return
|
|
321 */
|
|
322 public double getResX() {
|
|
323 return resX;
|
|
324 }
|
|
325
|
|
326 /**
|
|
327 * @return
|
|
328 */
|
|
329 public double getResY() {
|
|
330 return resY;
|
|
331 }
|
|
332
|
|
333 }
|