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