comparison common/src/main/java/digilib/io/ImageFile.java @ 903:7779b37d1d05

refactored into maven modules per servlet type. can build servlet-api 2.3 and 3.0 via profile now!
author robcast
date Tue, 26 Apr 2011 20:24:31 +0200
parents servlet/src/main/java/digilib/io/ImageFile.java@ba1eb2d821a2
children
comparison
equal deleted inserted replaced
902:89ba3ffcf552 903:7779b37d1d05
1 /* ImageFile.java -- digilib image file 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 * Created on 25.02.2003
20 */
21
22 package digilib.io;
23
24 import java.io.File;
25 import java.io.IOException;
26 import java.io.RandomAccessFile;
27
28 import javax.imageio.stream.FileImageInputStream;
29 import javax.imageio.stream.ImageInputStream;
30
31 import digilib.servlet.DigilibConfiguration;
32 import digilib.util.ImageSize;
33
34 /**
35 * @author casties
36 */
37 public class ImageFile extends ImageInput {
38
39 // file
40 private File file = null;
41 // file name
42 private String name = null;
43 // parent directory
44 private Directory dir = null;
45
46 /** Constructor with File.
47 *
48 * @param f
49 * @param parent
50 * @param dir
51 */
52 public ImageFile(File f, ImageSet parent, Directory dir) {
53 this.file = f;
54 this.name = f.getName();
55 this.parent = parent;
56 this.dir = dir;
57 }
58
59 /** Constructor with filename (without path).
60 * @param fn
61 * @param parent
62 * @param dir
63 */
64 public ImageFile(String fn, ImageSet parent, Directory dir) {
65 this.name = fn;
66 this.dir = dir;
67 this.file = new File(this.dir.getDir(), fn);
68 this.parent = parent;
69 }
70
71
72 /** Checks the image and sets size and type.
73 *
74 */
75 public void check() {
76 if (pixelSize == null) {
77 try {
78 // use the configured toolkit to identify the image
79 DigilibConfiguration.identifyDocuImage(this);
80 } catch (IOException e) {
81 // nothing much to do...
82 }
83 }
84 }
85
86 /* (non-Javadoc)
87 * @see digilib.io.ImageInput#getSize()
88 */
89 @Override
90 public ImageSize getSize() {
91 check();
92 return pixelSize;
93 }
94
95 /* (non-Javadoc)
96 * @see digilib.io.ImageInput#getMimetype()
97 */
98 @Override
99 public String getMimetype() {
100 check();
101 return mimetype;
102 }
103
104 /* (non-Javadoc)
105 * @see digilib.io.ImageInput#getAspect()
106 */
107 @Override
108 public float getAspect() {
109 check();
110 return (pixelSize != null) ? pixelSize.getAspect() : 0f;
111 }
112
113 /** Returns the file name (without path).
114 *
115 * @return
116 */
117 public String getName() {
118 return name;
119 }
120
121
122 /* (non-Javadoc)
123 * @see digilib.io.ImageInput#hasImageInputStream()
124 */
125 @Override
126 public boolean hasImageInputStream() {
127 return true;
128 }
129
130 /* (non-Javadoc)
131 * @see digilib.io.ImageInput#getImageInputStream()
132 */
133 @Override
134 public ImageInputStream getImageInputStream() {
135 try {
136 RandomAccessFile rf = new RandomAccessFile(file, "r");
137 return new FileImageInputStream(rf);
138 } catch (IOException e) {
139 // what now?
140 }
141 return null;
142 }
143
144 /* (non-Javadoc)
145 * @see digilib.io.ImageInput#hasFile()
146 */
147 @Override
148 public boolean hasFile() {
149 return true;
150 }
151
152 /* (non-Javadoc)
153 * @see digilib.io.ImageInput#getFile()
154 */
155 public File getFile() {
156 return file;
157 }
158
159 /* (non-Javadoc)
160 * @see digilib.io.ImageInput#setSize(digilib.image.ImageSize)
161 */
162 public void setSize(ImageSize imageSize) {
163 this.pixelSize = imageSize;
164 // pass on to parent
165 if (this.parent != null) {
166 this.parent.setAspect(imageSize);
167 }
168 }
169
170 /* (non-Javadoc)
171 * @see java.lang.Object#toString()
172 */
173 @Override
174 public String toString() {
175 // try to use File.toString
176 if (file != null) {
177 return file.toString();
178 }
179 return super.toString();
180 }
181
182
183 }