1
|
1 /* DocuImage -- General image interface class implementation
|
|
2
|
|
3 Digital Image Library servlet components
|
|
4
|
531
|
5 Copyright (C) 2001, 2002, 2003 Robert Casties (robcast@mail.berlios.de)
|
1
|
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
|
531
|
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
1
|
18
|
|
19 */
|
|
20
|
|
21 package digilib.image;
|
|
22
|
531
|
23 import java.awt.Rectangle;
|
|
24 import java.io.File;
|
|
25 import java.io.IOException;
|
|
26 import java.io.RandomAccessFile;
|
|
27 import java.util.Iterator;
|
|
28 import java.util.LinkedList;
|
|
29 import java.util.List;
|
|
30
|
|
31 import org.apache.log4j.Logger;
|
|
32 import org.marcoschmidt.image.ImageInfo;
|
1
|
33
|
531
|
34 import digilib.io.FileOpException;
|
|
35 import digilib.io.ImageFile;
|
1
|
36
|
531
|
37 /** Simple abstract implementation of the <code>DocuImage</code> interface.
|
|
38 *
|
|
39 * This implementation provides basic functionality for the utility methods like
|
|
40 * <code>SetUtils</code>, and <code>getKnownFileTypes</code>. Image methods like
|
|
41 * <code>loadImage</code>, <code>writeImage</code>, <code>getWidth</code>,
|
|
42 * <code>getHeight</code>, <code>crop</code> and <code>scale</code> must be
|
|
43 * implemented by derived classes.
|
|
44 */
|
1
|
45 public abstract class DocuImageImpl implements DocuImage {
|
|
46
|
531
|
47 /** logger */
|
|
48 protected static final Logger logger = Logger.getLogger(DocuImage.class);
|
|
49
|
|
50 /** Interpolation quality. */
|
|
51 protected int quality = 0;
|
|
52
|
|
53 /** epsilon for float comparisons. */
|
|
54 public final double epsilon = 1e-5;
|
|
55
|
|
56 /** image mime-type */
|
|
57 protected String mimeType = null;
|
1
|
58
|
531
|
59 /**
|
|
60 * Returns the quality.
|
|
61 * @return int
|
|
62 */
|
|
63 public int getQuality() {
|
|
64 return quality;
|
|
65 }
|
1
|
66
|
531
|
67 /**
|
|
68 * Sets the quality.
|
|
69 * @param quality The quality to set
|
|
70 */
|
|
71 public void setQuality(int quality) {
|
|
72 this.quality = quality;
|
|
73 }
|
1
|
74
|
531
|
75 /** Check image size and type and store in ImageFile f */
|
|
76 public boolean identify(ImageFile imgf) throws IOException {
|
|
77 // fileset to store the information
|
|
78 File f = imgf.getFile();
|
|
79 if (f == null) {
|
|
80 throw new IOException("File not found!");
|
|
81 }
|
|
82 RandomAccessFile raf = new RandomAccessFile(f, "r");
|
|
83 // set up ImageInfo object
|
|
84 ImageInfo iif = new ImageInfo();
|
|
85 iif.setInput(raf);
|
|
86 iif.setCollectComments(false);
|
|
87 iif.setDetermineImageNumber(false);
|
|
88 logger.debug("identifying (ImageInfo) " + f);
|
|
89 // try with ImageInfo first
|
|
90 if (iif.check()) {
|
|
91 ImageSize d = new ImageSize(iif.getWidth(), iif.getHeight());
|
|
92 imgf.setSize(d);
|
|
93 imgf.setMimetype(iif.getMimeType());
|
|
94 //logger.debug(" format:"+iif.getFormatName());
|
|
95 raf.close();
|
|
96 logger.debug("image size: " + imgf.getSize());
|
|
97 return true;
|
|
98 }
|
|
99 return false;
|
1
|
100 }
|
531
|
101
|
|
102 /** Crop and scale the current image.
|
|
103 *
|
|
104 * The current image is cropped to a rectangle of width, height at position
|
|
105 * x_off, y_off. The resulting image is scaled by the factor scale using the
|
|
106 * interpolation quality qual (0=worst).
|
|
107 *
|
|
108 * @param x_off X offset of the crop rectangle in pixel.
|
|
109 * @param y_off Y offset of the crop rectangle in pixel.
|
|
110 * @param width Width of the crop rectangle in pixel.
|
|
111 * @param height Height of the crop rectangle in pixel.
|
|
112 * @param scale Scaling factor.
|
|
113 * @param qual Interpolation quality (0=worst).
|
|
114 * @throws ImageOpException Exception thrown on any error.
|
|
115 */
|
|
116 public void cropAndScale(
|
|
117 int x_off, int y_off, int width, int height, double scale, int qual)
|
|
118 throws ImageOpException {
|
|
119
|
|
120 setQuality(qual);
|
|
121 crop(x_off, y_off, width, height);
|
|
122 scale(scale, scale);
|
|
123 }
|
|
124
|
|
125 public String getMimetype() {
|
|
126 return mimeType;
|
|
127 }
|
|
128
|
|
129 public void rotate(double angle) throws ImageOpException {
|
|
130 // just a do-nothing implementation
|
|
131 }
|
1
|
132
|
531
|
133 public void mirror(double angle) throws ImageOpException {
|
|
134 // just a do-nothing implementation
|
|
135 }
|
|
136
|
|
137 public void enhance(float mult, float add) throws ImageOpException {
|
|
138 // just a do-nothing implementation
|
|
139 }
|
|
140
|
|
141 public boolean isSubimageSupported() {
|
|
142 // partial loading not supported per default
|
|
143 return false;
|
|
144 }
|
|
145
|
|
146 public void loadSubimage(ImageFile f, Rectangle region, int subsample)
|
|
147 throws FileOpException {
|
|
148 // empty implementation
|
|
149 }
|
|
150
|
|
151 public void enhanceRGB(float[] rgbm, float[] rgba)
|
|
152 throws ImageOpException {
|
|
153 // emtpy implementation
|
|
154 }
|
|
155
|
|
156 public void dispose() {
|
|
157 // emtpy implementation
|
|
158 }
|
|
159
|
|
160 public Iterator<String> getSupportedFormats() {
|
|
161 List<String> empty = new LinkedList<String>();
|
|
162 return empty.iterator();
|
|
163 }
|
|
164
|
|
165
|
1
|
166 }
|