1
|
1 /* JAIDocuImage -- Image class implementation using JIMI toolkit
|
|
2
|
|
3 Digital Image Library servlet components
|
|
4
|
|
5 Copyright (C) 2001, 2002 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
|
|
21 package digilib.image;
|
|
22
|
|
23 import javax.servlet.*;
|
|
24 import javax.servlet.http.*;
|
|
25 import java.io.*;
|
|
26 import java.util.*;
|
|
27
|
|
28 import com.sun.jimi.core.*;
|
|
29 import com.sun.jimi.core.raster.*;
|
|
30 import com.sun.jimi.core.filters.*;
|
|
31
|
|
32 import java.awt.*;
|
|
33 import java.awt.image.*;
|
|
34
|
|
35 import digilib.*;
|
|
36 import digilib.io.*;
|
|
37
|
|
38
|
73
|
39 /** Implementation of DocuImage using the JIMI image Library. */
|
1
|
40 public class JIMIDocuImage extends DocuImageImpl {
|
|
41
|
|
42 private JimiRasterImage img;
|
|
43 private ImageProducer imgp;
|
|
44
|
|
45 public JIMIDocuImage() {
|
|
46 }
|
|
47
|
|
48 public JIMIDocuImage(Utils u) {
|
|
49 util = u;
|
|
50 }
|
|
51
|
|
52 /**
|
|
53 * load image file
|
|
54 */
|
|
55 public void loadImage(File f) throws FileOpException {
|
|
56 System.gc();
|
|
57 try {
|
|
58 img = Jimi.getRasterImage(f.toURL());
|
|
59 } catch (java.net.MalformedURLException e) {
|
|
60 util.dprintln(3, "ERROR(loadImage): MalformedURLException");
|
|
61 } catch (JimiException e) {
|
|
62 util.dprintln(3, "ERROR(loadImage): JIMIException");
|
|
63 throw new FileOpException("Unable to load File!"+e);
|
|
64 }
|
|
65 if (img == null) {
|
|
66 util.dprintln(3, "ERROR(loadImage): unable to load file");
|
|
67 throw new FileOpException("Unable to load File!");
|
|
68 }
|
|
69 }
|
|
70
|
|
71 /**
|
|
72 * write image of type mt to Stream
|
|
73 */
|
|
74 public void writeImage(String mt, ServletResponse res)
|
|
75 throws FileOpException {
|
|
76 try {
|
|
77 // setup output
|
|
78 res.setContentType(mt);
|
|
79 // render output
|
|
80 Jimi.putImage(mt, imgp, res.getOutputStream());
|
|
81
|
|
82 } catch (JimiException e) {
|
|
83 throw new FileOpException("Error writing image!"+e);
|
|
84 } catch (IOException e) {
|
|
85 throw new FileOpException("Error writing image."+e);
|
|
86 }
|
|
87 }
|
|
88
|
|
89 public int getWidth() {
|
|
90 if (img != null) {
|
|
91 return img.getWidth();
|
|
92 }
|
|
93 return 0;
|
|
94 }
|
|
95
|
|
96 public int getHeight() {
|
|
97 if (img != null) {
|
|
98 return img.getHeight();
|
|
99 }
|
|
100 return 0;
|
|
101 }
|
|
102
|
|
103
|
|
104 /**
|
|
105 * crop and scale image
|
|
106 * take rectangle width,height at position x_off,y_off
|
|
107 * and scale by scale
|
|
108 */
|
|
109 public void cropAndScale(int x_off, int y_off, int width, int height,
|
|
110 float scale, int qual) throws ImageOpException {
|
|
111
|
|
112 ImageFilter scaleFilter;
|
|
113 int destWidth = (int)(scale * (float)width);
|
|
114 int destHeight = (int)(scale * (float)height);
|
|
115
|
|
116 // setup Crop
|
|
117 ImageProducer croppedImg = img.getCroppedImageProducer(x_off, y_off, width, height);
|
|
118 //util.dprintln(3, "CROP:"+croppedImg.getWidth()+"x"+croppedImg.getHeight()); //DEBUG
|
|
119
|
|
120 if (croppedImg == null) {
|
|
121 util.dprintln(2, "ERROR(cropAndScale): error in crop");
|
|
122 throw new ImageOpException("Unable to crop");
|
|
123 }
|
|
124
|
|
125 // setup scale and interpolation quality
|
|
126 if (qual > 0) {
|
|
127 util.dprintln(4, "quality q1");
|
|
128 scaleFilter = new AreaAverageScaleFilter(destWidth, destHeight);
|
|
129 } else {
|
|
130 util.dprintln(4, "quality q0");
|
|
131 scaleFilter = new ReplicatingScaleFilter(destWidth, destHeight);
|
|
132 }
|
|
133
|
|
134 ImageProducer scaledImg = new FilteredImageSource(croppedImg, scaleFilter);
|
|
135
|
|
136 if (scaledImg == null) {
|
|
137 util.dprintln(2, "ERROR(cropAndScale): error in scale");
|
|
138 throw new ImageOpException("Unable to scale");
|
|
139 }
|
|
140
|
|
141 imgp = scaledImg;
|
|
142 }
|
|
143
|
|
144 }
|