comparison servlet/src/digilib/image/DocuImageImpl.java @ 531:9cedd170b581 digilibPDF

* PDF generation works now even with subdirectories * genericsification and clean up
author robcast
date Fri, 05 Feb 2010 20:58:38 +0100
parents 0ff3ede32060
children 87cb3dc2aa12
comparison
equal deleted inserted replaced
530:bd6569a95a3c 531:9cedd170b581
1 /* DocuImage -- General image interface class implementation 1 /* DocuImage -- General image interface class implementation
2 2
3 Digital Image Library servlet components 3 Digital Image Library servlet components
4 4
5 Copyright (C) 2001, 2002 Robert Casties (robcast@mail.berlios.de) 5 Copyright (C) 2001, 2002, 2003 Robert Casties (robcast@mail.berlios.de)
6 6
7 This program is free software; you can redistribute it and/or modify it 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 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 9 Free Software Foundation; either version 2 of the License, or (at your
10 option) any later version. 10 option) any later version.
12 Please read license.txt for the full details. A copy of the GPL 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 13 may be found at http://www.gnu.org/copyleft/lgpl.html
14 14
15 You should have received a copy of the GNU General Public License 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 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 17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 18
19 */ 19 */
20 20
21 package digilib.image; 21 package digilib.image;
22 22
23 import java.io.*; 23 import java.awt.Rectangle;
24 import javax.servlet.ServletResponse; 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;
25 30
26 import digilib.*; 31 import org.apache.log4j.Logger;
27 import digilib.io.*; 32 import org.marcoschmidt.image.ImageInfo;
28 33
34 import digilib.io.FileOpException;
35 import digilib.io.ImageFile;
36
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 */
29 public abstract class DocuImageImpl implements DocuImage { 45 public abstract class DocuImageImpl implements DocuImage {
30 46
31 protected Utils util = null; 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;
32 58
33 public DocuImageImpl() { 59 /**
34 util = new Utils(); 60 * Returns the quality.
35 } 61 * @return int
62 */
63 public int getQuality() {
64 return quality;
65 }
36 66
37 public DocuImageImpl(Utils u) { 67 /**
38 util = u; 68 * Sets the quality.
39 } 69 * @param quality The quality to set
70 */
71 public void setQuality(int quality) {
72 this.quality = quality;
73 }
40 74
41 public void setUtils(Utils u) { 75 /** Check image size and type and store in ImageFile f */
42 util = u; 76 public boolean identify(ImageFile imgf) throws IOException {
43 } 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;
100 }
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 {
44 119
45 protected String[] knownFileTypes = {"jpg", "png", "gif", "tiff"}; 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 }
46 128
47 public String[] getKnownFileTypes() { 129 public void rotate(double angle) throws ImageOpException {
48 return knownFileTypes; 130 // just a do-nothing implementation
49 } 131 }
50 132
51 /** 133 public void mirror(double angle) throws ImageOpException {
52 * send an image file as-is 134 // just a do-nothing implementation
53 */ 135 }
54 public void sendFile(File f, ServletResponse response) throws FileOpException {
55 util.dprintln(4, "sendFile("+f+")");
56 String mimeType = FileOps.mimeForFile(f);
57 if (mimeType == null) {
58 util.dprintln(2, "ERROR(sendFile): unknown file Type");
59 throw new FileOpException("Unknown file type.");
60 }
61 response.setContentType(mimeType);
62 // open file
63 try {
64 FileInputStream inFile = new FileInputStream(f);
65 OutputStream outStream = response.getOutputStream();
66 byte dataBuffer[] = new byte[1024];
67 int len;
68 while ((len = inFile.read(dataBuffer)) != -1) {
69 // copy out file
70 outStream.write(dataBuffer, 0, len);
71 }
72 inFile.close();
73 } catch (IOException e) {
74 util.dprintln(2, "ERROR(sendFile): unable to send file");
75 throw new FileOpException("Unable to send file.");
76 }
77 }
78 136
79 public abstract void loadImage(File f) throws FileOpException; 137 public void enhance(float mult, float add) throws ImageOpException {
80 public abstract void writeImage(String mt, ServletResponse res) throws FileOpException; 138 // just a do-nothing implementation
81 public abstract int getWidth(); 139 }
82 public abstract int getHeight(); 140
83 public abstract void cropAndScale(int x_off, int y_off, int width, int height, float scale, int qual) throws ImageOpException; 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
84 } 166 }