comparison servlet/src/digilib/image/JIMIDocuImage.java @ 79:63c8186455c1

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