comparison servlet/src/digilib/image/ImageLoaderDocuImage.java @ 1:0ff3ede32060

Initial revision
author robcast
date Thu, 17 Jan 2002 15:25:46 +0100
parents
children 3b8797fc3e90 5d0c0da080ec 6d2032b6121d 9cedd170b581
comparison
equal deleted inserted replaced
0:ffd2df307e81 1:0ff3ede32060
1 /* ImageLoaderDocuImage -- Image class implementation using JDK 1.4 ImageLoader
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 java.awt.*;
29 import java.awt.image.*;
30 import java.awt.geom.*;
31 import java.awt.image.renderable.*;
32
33 import javax.imageio.*;
34
35 import digilib.*;
36 import digilib.io.*;
37
38 public class ImageLoaderDocuImage extends DocuImageImpl {
39
40 private BufferedImage img;
41
42 public ImageLoaderDocuImage() {
43 }
44
45 public ImageLoaderDocuImage(Utils u) {
46 util = u;
47 }
48
49 /**
50 * load image file
51 */
52 public void loadImage(File f) throws FileOpException {
53 util.dprintln(10, "loadImage!");
54 System.gc();
55 try {
56 for (int i = 0; i < ImageIO.getReaderFormatNames().length; i++) {
57 System.out.println("ImageLoader reader:"+ImageIO.getReaderFormatNames()[i]);
58 }
59 for (int i = 0; i < ImageIO.getWriterFormatNames().length; i++) {
60 System.out.println("ImageLoader writer:"+ImageIO.getWriterFormatNames()[i]);
61 }
62 img = ImageIO.read(f);
63 if (img == null) {
64 util.dprintln(3, "ERROR(loadImage): unable to load file");
65 throw new FileOpException("Unable to load File!");
66 }
67 }
68 catch (IOException e) {
69 throw new FileOpException("Error reading image.");
70 }
71 }
72
73 /**
74 * write image of type mt to Stream
75 */
76 public void writeImage(String mt, ServletResponse res)
77 throws FileOpException {
78 util.dprintln(10, "writeImage!");
79 try {
80 // setup output
81 String type = "png";
82 if (mt == "image/jpeg") {
83 type = "jpeg";
84 } else if (mt == "image/png") {
85 type = "png";
86 } else {
87 // unknown mime type
88 util.dprintln(2, "ERROR(writeImage): Unknown mime type "+mt);
89 throw new FileOpException("Unknown mime type: "+mt);
90 }
91 res.setContentType(mt);
92 // render output
93 if (ImageIO.write(img, type, res.getOutputStream())) {
94 // writing was OK
95 return;
96 } else {
97 throw new FileOpException("Error writing image: Unknown image format!");
98 }
99 } catch (IOException e) {
100 // e.printStackTrace();
101 throw new FileOpException("Error writing image.");
102 }
103 }
104
105 public int getWidth() {
106 if (img != null) {
107 return img.getWidth();
108 }
109 return 0;
110 }
111
112 public int getHeight() {
113 if (img != null) {
114 return img.getHeight();
115 }
116 return 0;
117 }
118
119 /**
120 * crop and scale image
121 * take rectangle width,height at position x_off,y_off
122 * and scale by scale
123 */
124 public void cropAndScale(int x_off, int y_off, int width, int height,
125 float scale, int qual) throws ImageOpException {
126 util.dprintln(10, "cropAndScale!");
127
128 int scaleInt = 0;
129 // setup interpolation quality
130 if (qual > 0) {
131 util.dprintln(4, "quality q1");
132 scaleInt = AffineTransformOp.TYPE_BILINEAR;
133 } else {
134 util.dprintln(4, "quality q0");
135 scaleInt = AffineTransformOp.TYPE_NEAREST_NEIGHBOR;
136 }
137
138 // setup Crop
139 BufferedImage croppedImg = img.getSubimage(x_off, y_off, width, height);
140
141 img = null; // free img
142 util.dprintln(3, "CROP:"+croppedImg.getWidth()+"x"+croppedImg.getHeight()); //DEBUG
143 // util.dprintln(2, " time "+(System.currentTimeMillis()-startTime)+"ms");
144
145 if (croppedImg == null) {
146 util.dprintln(2, "ERROR(cropAndScale): error in crop");
147 throw new ImageOpException("Unable to crop");
148 }
149
150 // setup scale
151 AffineTransformOp scaleOp = new AffineTransformOp(
152 AffineTransform.getScaleInstance(scale, scale),
153 scaleInt);
154 BufferedImage scaledImg = scaleOp.filter(croppedImg, null);
155 croppedImg = null; // free opCrop
156
157 if (scaledImg == null) {
158 util.dprintln(2, "ERROR(cropAndScale): error in scale");
159 throw new ImageOpException("Unable to scale");
160 }
161 img = scaledImg;
162 }
163
164 }