86
|
1 /* DocuFileset -- 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
|
122
|
22 import java.awt.Dimension;
|
130
|
23 import java.io.File;
|
91
|
24 import java.util.ArrayList;
|
|
25 import java.util.HashMap;
|
122
|
26 import java.util.Iterator;
|
91
|
27 import java.util.ListIterator;
|
86
|
28
|
122
|
29 import digilib.image.DocuInfo;
|
|
30
|
86
|
31 /**
|
|
32 * @author casties
|
|
33 */
|
91
|
34 public class DocuFileset {
|
86
|
35
|
130
|
36 // list of files (DocuFile)
|
91
|
37 private ArrayList list = null;
|
86
|
38 // metadata
|
91
|
39 private HashMap fileMeta = null;
|
130
|
40 // metadata has been checked
|
|
41 private boolean metaChecked = false;
|
|
42 // resolution (DPI)
|
|
43 private double resX = 0;
|
|
44 private double resY = 0;
|
86
|
45 // parent directory
|
|
46 private DocuDirectory parent = null;
|
|
47
|
130
|
48
|
91
|
49 /*
|
|
50 * constructors
|
|
51 */
|
86
|
52
|
|
53 public DocuFileset(int initialCapacity) {
|
91
|
54 list = new ArrayList(initialCapacity);
|
86
|
55 }
|
|
56
|
91
|
57 /*
|
|
58 * other stuff
|
|
59 */
|
|
60
|
|
61 /** Adds a DocuFile to this Fileset.
|
|
62 *
|
122
|
63 * The files should be added in the order of higher to lower resolutions.
|
|
64 * The first file is considered the hires "original".
|
91
|
65 *
|
|
66 * @param f file to add
|
|
67 * @return true (always)
|
|
68 */
|
|
69 public boolean add(DocuFile f) {
|
|
70 f.setParent(this);
|
|
71 return list.add(f);
|
86
|
72 }
|
122
|
73
|
91
|
74 /** The number of image files in this Fileset.
|
|
75 *
|
|
76 * @return number of image files
|
|
77 */
|
|
78 public int size() {
|
|
79 return (list != null) ? list.size() : 0;
|
|
80 }
|
122
|
81
|
91
|
82 /** Get the DocuFile at the index.
|
|
83 *
|
|
84 * @param index
|
|
85 * @return
|
86
|
86 */
|
91
|
87 public DocuFile get(int index) {
|
|
88 return (DocuFile) list.get(index);
|
|
89 }
|
122
|
90
|
|
91 /** Get the next smaller DocuFile than the given size.
|
|
92 *
|
|
93 * Returns the DocuFile from the set that has a width and height
|
|
94 * smaller or equal the given size.
|
|
95 * Returns null if there isn't any smaller image.
|
|
96 * Needs DocuInfo instance to checkFile().
|
|
97 *
|
|
98 * @param size
|
|
99 * @param info
|
|
100 * @return
|
|
101 */
|
|
102 public DocuFile getNextSmaller(Dimension size, DocuInfo info) {
|
|
103 for (Iterator i = getHiresIterator(); i.hasNext();) {
|
|
104 DocuFile f = (DocuFile) i.next();
|
|
105 if (!f.isChecked()) {
|
|
106 f.check(info);
|
|
107 }
|
|
108 if ((f.getSize().getHeight() <= size.getHeight())
|
|
109 && (f.getSize().getWidth() <= size.getWidth())) {
|
|
110 return f;
|
|
111 }
|
|
112 }
|
|
113 return null;
|
|
114 }
|
|
115
|
|
116 /** Get the next bigger DocuFile than the given size.
|
|
117 *
|
|
118 * Returns the DocuFile from the set that has a width and height
|
|
119 * bigger or equal the given size.
|
|
120 * Returns null if there isn't any bigger image.
|
|
121 * Needs DocuInfo instance to checkFile().
|
|
122 *
|
|
123 * @param size
|
|
124 * @param info
|
|
125 * @return
|
|
126 */
|
|
127 public DocuFile getNextBigger(Dimension size, DocuInfo info) {
|
|
128 for (ListIterator i = getLoresIterator(); i.hasPrevious();) {
|
|
129 DocuFile f = (DocuFile) i.previous();
|
|
130 if (!f.isChecked()) {
|
|
131 f.check(info);
|
|
132 }
|
|
133 if ((f.getSize().getHeight() >= size.getHeight())
|
|
134 && (f.getSize().getWidth() >= size.getWidth())) {
|
|
135 return f;
|
|
136 }
|
|
137 }
|
|
138 return null;
|
|
139 }
|
|
140
|
91
|
141 /** Get an Iterator for this Fileset starting at the highest resolution
|
|
142 * images.
|
|
143 *
|
|
144 * @return
|
|
145 */
|
|
146 public ListIterator getHiresIterator() {
|
|
147 return list.listIterator();
|
86
|
148 }
|
122
|
149
|
91
|
150 /** Get an Iterator for this Fileset starting at the lowest resolution
|
|
151 * images.
|
|
152 *
|
|
153 * The Iterator starts at the last element, so you have to use it backwards
|
|
154 * with hasPrevious() and previous().
|
|
155 *
|
|
156 * @return
|
|
157 */
|
|
158 public ListIterator getLoresIterator() {
|
|
159 return list.listIterator(list.size());
|
|
160 }
|
122
|
161
|
91
|
162 /** Reads meta-data for this Fileset if there is any.
|
130
|
163 *
|
|
164 */
|
|
165 public void readMeta() {
|
|
166 if ((fileMeta != null) || list.isEmpty()) {
|
|
167 // there is already metadata or there's no file
|
|
168 return;
|
|
169 }
|
|
170 // metadata is in the file {filename}.meta
|
|
171 String fn = ((DocuFile) list.get(0)).getFile().getAbsolutePath();
|
|
172 File mf = new File(fn + ".meta");
|
|
173 if (mf.canRead()) {
|
|
174 XMLMetaLoader ml = new XMLMetaLoader();
|
|
175 try {
|
|
176 // read meta file
|
|
177 HashMap meta = ml.loadURL(mf.getAbsolutePath());
|
|
178 if (meta == null) {
|
|
179 return;
|
|
180 }
|
|
181 fileMeta = (HashMap) meta.get(getName());
|
|
182 } catch (Exception e) {
|
|
183 // TODO Auto-generated catch block
|
|
184 e.printStackTrace();
|
|
185 }
|
|
186 }
|
|
187 }
|
|
188
|
|
189 /** Checks metadata and sets resolution in resX and resY.
|
|
190 *
|
91
|
191 */
|
|
192 public void checkMeta() {
|
130
|
193 if (metaChecked) {
|
|
194 return;
|
|
195 }
|
|
196 if (fileMeta == null) {
|
|
197 // try to read meta-data file
|
|
198 readMeta();
|
|
199 if (fileMeta == null) {
|
|
200 // there is no meta data
|
|
201 metaChecked = true;
|
|
202 return;
|
|
203 }
|
|
204 }
|
|
205 metaChecked = true;
|
|
206 double dpi = 0;
|
|
207 double dpix = 0;
|
|
208 double dpiy = 0;
|
|
209 double sizex = 0;
|
|
210 double sizey = 0;
|
|
211 double pixx = 0;
|
|
212 double pixy = 0;
|
|
213 // DPI is valid for X and Y
|
|
214 try {
|
|
215 dpi = Double.parseDouble((String) fileMeta.get("dpi"));
|
|
216 } catch (NumberFormatException e) {
|
|
217 }
|
|
218 if (dpi != 0) {
|
|
219 resX = dpi;
|
|
220 resY = dpi;
|
|
221 return;
|
|
222 }
|
|
223 // DPI-X and DPI-Y
|
|
224 try {
|
|
225 dpix = Double.parseDouble((String) fileMeta.get("dpi-x"));
|
|
226 dpiy = Double.parseDouble((String) fileMeta.get("dpi-y"));
|
|
227 } catch (NumberFormatException e) {
|
|
228 }
|
|
229 if ((dpix != 0) && (dpiy != 0)) {
|
|
230 resX = dpix;
|
|
231 resY = dpiy;
|
|
232 return;
|
|
233 }
|
|
234 // SIZE-X and SIZE-Y and PIXEL-X and PIXEL-Y
|
|
235 try {
|
|
236 sizex =
|
|
237 Double.parseDouble((String) fileMeta.get("original-size-x"));
|
|
238 sizey =
|
|
239 Double.parseDouble((String) fileMeta.get("original-size-y"));
|
|
240 pixx =
|
|
241 Double.parseDouble((String) fileMeta.get("original-pixel-x"));
|
|
242 pixy =
|
|
243 Double.parseDouble((String) fileMeta.get("original-pixel-y"));
|
|
244 } catch (NumberFormatException e) {
|
|
245 }
|
|
246 if ((sizex != 0) && (sizey != 0) && (pixx != 0) && (pixy != 0)) {
|
|
247 resX = pixx / (sizex * 100 / 2.54);
|
|
248 resY = pixy / (sizey * 100 / 2.54);
|
|
249 return;
|
|
250 }
|
86
|
251 }
|
|
252
|
130
|
253 /** The name of the (hires) image file.
|
91
|
254 *
|
|
255 * @return
|
|
256 */
|
86
|
257 public String getName() {
|
91
|
258 if (!list.isEmpty()) {
|
|
259 return ((DocuFile) list.get(0)).getName();
|
86
|
260 }
|
|
261 return null;
|
|
262 }
|
122
|
263
|
91
|
264 /** Returns the parent DocuDirectory.
|
130
|
265 *
|
86
|
266 * @return DocuDirectory
|
|
267 */
|
|
268 public DocuDirectory getParent() {
|
|
269 return parent;
|
|
270 }
|
|
271
|
|
272 /**
|
|
273 * Sets the parent.
|
|
274 * @param parent The parent to set
|
|
275 */
|
|
276 public void setParent(DocuDirectory parent) {
|
|
277 this.parent = parent;
|
|
278 }
|
|
279
|
91
|
280 /** Returns the meta-data for this fileset.
|
|
281 *
|
|
282 * @return HashMap
|
|
283 */
|
|
284 public HashMap getFileMeta() {
|
|
285 return fileMeta;
|
|
286 }
|
|
287
|
|
288 /**
|
|
289 * Sets the fileMeta.
|
|
290 * @param fileMeta The fileMeta to set
|
|
291 */
|
|
292 public void setFileMeta(HashMap fileMeta) {
|
|
293 this.fileMeta = fileMeta;
|
|
294 }
|
|
295
|
130
|
296 /**
|
|
297 * @return
|
|
298 */
|
|
299 public boolean isMetaChecked() {
|
|
300 return metaChecked;
|
|
301 }
|
|
302
|
|
303 /**
|
|
304 * @return
|
|
305 */
|
|
306 public double getResX() {
|
|
307 return resX;
|
|
308 }
|
|
309
|
|
310 /**
|
|
311 * @return
|
|
312 */
|
|
313 public double getResY() {
|
|
314 return resY;
|
|
315 }
|
|
316
|
86
|
317 }
|