annotate servlet/src/digilib/image/DocuImageImpl.java @ 86:997ba69afb81

New version 1.8b1. With directory and file information cache. With enhanceRGB method for color correction.
author robcast
date Sun, 09 Mar 2003 21:37:27 +0100
parents 4e6757e8ccd4
children a32e8c80e2f2
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
1 /* DocuImage -- General image interface class implementation
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
2
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
3 Digital Image Library servlet components
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
4
85
4e6757e8ccd4 New enhanced ImageLoader stuff.
robcast
parents: 79
diff changeset
5 Copyright (C) 2001, 2002, 2003 Robert Casties (robcast@mail.berlios.de)
1
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
6
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
7 This program is free software; you can redistribute it and/or modify it
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
8 under the terms of the GNU General Public License as published by the
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
9 Free Software Foundation; either version 2 of the License, or (at your
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
10 option) any later version.
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
11
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
12 Please read license.txt for the full details. A copy of the GPL
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
13 may be found at http://www.gnu.org/copyleft/lgpl.html
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
14
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
15 You should have received a copy of the GNU General Public License
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
16 along with this program; if not, write to the Free Software
73
3b8797fc3e90 New servlet version 1.5b.
robcast
parents: 1
diff changeset
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
18
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
19 */
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
20
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
21 package digilib.image;
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
22
86
997ba69afb81 New version 1.8b1.
robcast
parents: 85
diff changeset
23 import java.awt.Dimension;
85
4e6757e8ccd4 New enhanced ImageLoader stuff.
robcast
parents: 79
diff changeset
24 import java.awt.Rectangle;
4e6757e8ccd4 New enhanced ImageLoader stuff.
robcast
parents: 79
diff changeset
25 import java.io.File;
86
997ba69afb81 New version 1.8b1.
robcast
parents: 85
diff changeset
26 import java.io.IOException;
85
4e6757e8ccd4 New enhanced ImageLoader stuff.
robcast
parents: 79
diff changeset
27
79
63c8186455c1 Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents: 73
diff changeset
28 import digilib.Utils;
86
997ba69afb81 New version 1.8b1.
robcast
parents: 85
diff changeset
29 import digilib.io.DocuFile;
85
4e6757e8ccd4 New enhanced ImageLoader stuff.
robcast
parents: 79
diff changeset
30 import digilib.io.FileOpException;
86
997ba69afb81 New version 1.8b1.
robcast
parents: 85
diff changeset
31 import digilib.io.FileOps;
1
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
32
73
3b8797fc3e90 New servlet version 1.5b.
robcast
parents: 1
diff changeset
33 /** Simple abstract implementation of the <code>DocuImage</code> interface.
3b8797fc3e90 New servlet version 1.5b.
robcast
parents: 1
diff changeset
34 *
3b8797fc3e90 New servlet version 1.5b.
robcast
parents: 1
diff changeset
35 * This implementation provides basic functionality for the utility methods like
3b8797fc3e90 New servlet version 1.5b.
robcast
parents: 1
diff changeset
36 * <code>SetUtils</code>, and <code>getKnownFileTypes</code>. Image methods like
3b8797fc3e90 New servlet version 1.5b.
robcast
parents: 1
diff changeset
37 * <code>loadImage</code>, <code>writeImage</code>, <code>getWidth</code>,
79
63c8186455c1 Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents: 73
diff changeset
38 * <code>getHeight</code>, <code>crop</code> and <code>scale</code> must be
63c8186455c1 Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents: 73
diff changeset
39 * implemented by derived classes.
73
3b8797fc3e90 New servlet version 1.5b.
robcast
parents: 1
diff changeset
40 */
1
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
41 public abstract class DocuImageImpl implements DocuImage {
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
42
79
63c8186455c1 Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents: 73
diff changeset
43 /** Internal utils object. */
63c8186455c1 Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents: 73
diff changeset
44 protected Utils util = null;
63c8186455c1 Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents: 73
diff changeset
45
63c8186455c1 Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents: 73
diff changeset
46 /** Interpolation quality. */
63c8186455c1 Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents: 73
diff changeset
47 protected int quality = 0;
85
4e6757e8ccd4 New enhanced ImageLoader stuff.
robcast
parents: 79
diff changeset
48
86
997ba69afb81 New version 1.8b1.
robcast
parents: 85
diff changeset
49 /** epsilon for float comparisons. */
85
4e6757e8ccd4 New enhanced ImageLoader stuff.
robcast
parents: 79
diff changeset
50 public final double epsilon = 1e-5;
86
997ba69afb81 New version 1.8b1.
robcast
parents: 85
diff changeset
51
997ba69afb81 New version 1.8b1.
robcast
parents: 85
diff changeset
52 /** image mime-type */
997ba69afb81 New version 1.8b1.
robcast
parents: 85
diff changeset
53 protected String mimeType = null;
1
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
54
79
63c8186455c1 Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents: 73
diff changeset
55 /** Default constructor. */
63c8186455c1 Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents: 73
diff changeset
56 public DocuImageImpl() {
63c8186455c1 Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents: 73
diff changeset
57 util = new Utils();
63c8186455c1 Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents: 73
diff changeset
58 }
63c8186455c1 Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents: 73
diff changeset
59
63c8186455c1 Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents: 73
diff changeset
60 /** Contructor taking an utils object.
63c8186455c1 Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents: 73
diff changeset
61 *
63c8186455c1 Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents: 73
diff changeset
62 * @param u Utils object.
63c8186455c1 Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents: 73
diff changeset
63 */
63c8186455c1 Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents: 73
diff changeset
64 public DocuImageImpl(Utils u) {
63c8186455c1 Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents: 73
diff changeset
65 util = u;
63c8186455c1 Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents: 73
diff changeset
66 }
1
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
67
79
63c8186455c1 Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents: 73
diff changeset
68 /** Set local Utils object.
63c8186455c1 Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents: 73
diff changeset
69 *
63c8186455c1 Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents: 73
diff changeset
70 * @param u Utils object.
63c8186455c1 Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents: 73
diff changeset
71 */
63c8186455c1 Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents: 73
diff changeset
72 public void setUtils(Utils u) {
63c8186455c1 Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents: 73
diff changeset
73 util = u;
63c8186455c1 Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents: 73
diff changeset
74 }
63c8186455c1 Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents: 73
diff changeset
75
63c8186455c1 Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents: 73
diff changeset
76 /** Internal knownFileTypes. */
63c8186455c1 Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents: 73
diff changeset
77 protected String[] knownFileTypes = { "jpg", "png", "gif", "tiff" };
63c8186455c1 Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents: 73
diff changeset
78
63c8186455c1 Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents: 73
diff changeset
79 /** Returns the list of image file types known to the DocuImage implementation.
63c8186455c1 Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents: 73
diff changeset
80 *
63c8186455c1 Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents: 73
diff changeset
81 * @return List of image file types. Strings are standard file extensions.
63c8186455c1 Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents: 73
diff changeset
82 */
63c8186455c1 Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents: 73
diff changeset
83 public String[] getKnownFileTypes() {
63c8186455c1 Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents: 73
diff changeset
84 return knownFileTypes;
63c8186455c1 Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents: 73
diff changeset
85 }
1
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
86
79
63c8186455c1 Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents: 73
diff changeset
87 /**
63c8186455c1 Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents: 73
diff changeset
88 * Returns the quality.
63c8186455c1 Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents: 73
diff changeset
89 * @return int
63c8186455c1 Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents: 73
diff changeset
90 */
63c8186455c1 Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents: 73
diff changeset
91 public int getQuality() {
63c8186455c1 Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents: 73
diff changeset
92 return quality;
63c8186455c1 Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents: 73
diff changeset
93 }
1
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
94
79
63c8186455c1 Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents: 73
diff changeset
95 /**
63c8186455c1 Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents: 73
diff changeset
96 * Sets the quality.
63c8186455c1 Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents: 73
diff changeset
97 * @param quality The quality to set
63c8186455c1 Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents: 73
diff changeset
98 */
63c8186455c1 Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents: 73
diff changeset
99 public void setQuality(int quality) {
63c8186455c1 Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents: 73
diff changeset
100 this.quality = quality;
63c8186455c1 Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents: 73
diff changeset
101 }
1
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
102
79
63c8186455c1 Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents: 73
diff changeset
103 /** Crop and scale the current image.
63c8186455c1 Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents: 73
diff changeset
104 *
63c8186455c1 Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents: 73
diff changeset
105 * The current image is cropped to a rectangle of width, height at position
63c8186455c1 Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents: 73
diff changeset
106 * x_off, y_off. The resulting image is scaled by the factor scale using the
63c8186455c1 Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents: 73
diff changeset
107 * interpolation quality qual (0=worst).
63c8186455c1 Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents: 73
diff changeset
108 *
63c8186455c1 Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents: 73
diff changeset
109 * @param x_off X offset of the crop rectangle in pixel.
63c8186455c1 Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents: 73
diff changeset
110 * @param y_off Y offset of the crop rectangle in pixel.
63c8186455c1 Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents: 73
diff changeset
111 * @param width Width of the crop rectangle in pixel.
63c8186455c1 Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents: 73
diff changeset
112 * @param height Height of the crop rectangle in pixel.
63c8186455c1 Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents: 73
diff changeset
113 * @param scale Scaling factor.
63c8186455c1 Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents: 73
diff changeset
114 * @param qual Interpolation quality (0=worst).
63c8186455c1 Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents: 73
diff changeset
115 * @throws ImageOpException Exception thrown on any error.
63c8186455c1 Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents: 73
diff changeset
116 */
63c8186455c1 Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents: 73
diff changeset
117 public void cropAndScale(
63c8186455c1 Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents: 73
diff changeset
118 int x_off, int y_off, int width, int height, double scale, int qual)
63c8186455c1 Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents: 73
diff changeset
119 throws ImageOpException {
1
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
120
79
63c8186455c1 Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents: 73
diff changeset
121 setQuality(qual);
63c8186455c1 Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents: 73
diff changeset
122 crop(x_off, y_off, width, height);
63c8186455c1 Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents: 73
diff changeset
123 scale(scale);
63c8186455c1 Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents: 73
diff changeset
124 }
63c8186455c1 Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents: 73
diff changeset
125
86
997ba69afb81 New version 1.8b1.
robcast
parents: 85
diff changeset
126 /* this is a rather stupid implementation, eventually loading the whole file. */
997ba69afb81 New version 1.8b1.
robcast
parents: 85
diff changeset
127 public boolean checkFile(DocuFile f) throws IOException {
997ba69afb81 New version 1.8b1.
robcast
parents: 85
diff changeset
128 loadImage(f.getFile());
997ba69afb81 New version 1.8b1.
robcast
parents: 85
diff changeset
129 int w = getWidth();
997ba69afb81 New version 1.8b1.
robcast
parents: 85
diff changeset
130 int h = getHeight();
997ba69afb81 New version 1.8b1.
robcast
parents: 85
diff changeset
131 Dimension s = new Dimension(w, h);
997ba69afb81 New version 1.8b1.
robcast
parents: 85
diff changeset
132 f.setSize(s);
997ba69afb81 New version 1.8b1.
robcast
parents: 85
diff changeset
133 String m = FileOps.mimeForFile(f.getFile());
997ba69afb81 New version 1.8b1.
robcast
parents: 85
diff changeset
134 mimeType = m;
997ba69afb81 New version 1.8b1.
robcast
parents: 85
diff changeset
135 f.setMimetype(m);
997ba69afb81 New version 1.8b1.
robcast
parents: 85
diff changeset
136 return true;
997ba69afb81 New version 1.8b1.
robcast
parents: 85
diff changeset
137 }
997ba69afb81 New version 1.8b1.
robcast
parents: 85
diff changeset
138
997ba69afb81 New version 1.8b1.
robcast
parents: 85
diff changeset
139 public String getMimetype() {
997ba69afb81 New version 1.8b1.
robcast
parents: 85
diff changeset
140 return mimeType;
997ba69afb81 New version 1.8b1.
robcast
parents: 85
diff changeset
141 }
997ba69afb81 New version 1.8b1.
robcast
parents: 85
diff changeset
142
79
63c8186455c1 Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents: 73
diff changeset
143 public void rotate(double angle) throws ImageOpException {
63c8186455c1 Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents: 73
diff changeset
144 // just a do-nothing implementation
63c8186455c1 Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents: 73
diff changeset
145 }
1
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
146
79
63c8186455c1 Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents: 73
diff changeset
147 public void mirror(double angle) throws ImageOpException {
63c8186455c1 Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents: 73
diff changeset
148 // just a do-nothing implementation
63c8186455c1 Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents: 73
diff changeset
149 }
63c8186455c1 Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents: 73
diff changeset
150
86
997ba69afb81 New version 1.8b1.
robcast
parents: 85
diff changeset
151 public void enhance(float mult, float add) throws ImageOpException {
85
4e6757e8ccd4 New enhanced ImageLoader stuff.
robcast
parents: 79
diff changeset
152 // just a do-nothing implementation
4e6757e8ccd4 New enhanced ImageLoader stuff.
robcast
parents: 79
diff changeset
153 }
4e6757e8ccd4 New enhanced ImageLoader stuff.
robcast
parents: 79
diff changeset
154
4e6757e8ccd4 New enhanced ImageLoader stuff.
robcast
parents: 79
diff changeset
155 public boolean isSubimageSupported() {
86
997ba69afb81 New version 1.8b1.
robcast
parents: 85
diff changeset
156 // partial loading not supported per default
85
4e6757e8ccd4 New enhanced ImageLoader stuff.
robcast
parents: 79
diff changeset
157 return false;
4e6757e8ccd4 New enhanced ImageLoader stuff.
robcast
parents: 79
diff changeset
158 }
4e6757e8ccd4 New enhanced ImageLoader stuff.
robcast
parents: 79
diff changeset
159
4e6757e8ccd4 New enhanced ImageLoader stuff.
robcast
parents: 79
diff changeset
160 public void loadSubimage(File f, Rectangle region, int subsample)
4e6757e8ccd4 New enhanced ImageLoader stuff.
robcast
parents: 79
diff changeset
161 throws FileOpException {
4e6757e8ccd4 New enhanced ImageLoader stuff.
robcast
parents: 79
diff changeset
162 // empty implementation
4e6757e8ccd4 New enhanced ImageLoader stuff.
robcast
parents: 79
diff changeset
163 }
4e6757e8ccd4 New enhanced ImageLoader stuff.
robcast
parents: 79
diff changeset
164
86
997ba69afb81 New version 1.8b1.
robcast
parents: 85
diff changeset
165 public void enhanceRGB(float[] rgbm, float[] rgba)
997ba69afb81 New version 1.8b1.
robcast
parents: 85
diff changeset
166 throws ImageOpException {
997ba69afb81 New version 1.8b1.
robcast
parents: 85
diff changeset
167 // emtpy implementation
997ba69afb81 New version 1.8b1.
robcast
parents: 85
diff changeset
168 }
997ba69afb81 New version 1.8b1.
robcast
parents: 85
diff changeset
169
1
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
170 }