comparison servlet/src/digilib/image/JAIImageLoaderDocuImage.java @ 64:5ea1999befd4

New JAI ImageLoader plugin. Currently uses first beta version of the plugin. Needs Java 1.4. digilib/io/FileOps.java digilib/servlet/Scaler.java Modified to use JAIImageLoaderDocuImage. digilib/image/JAIImageLoaderDocuImage.java New class JAIImageLoaderDocuImage.
author robcast
date Tue, 07 Jan 2003 18:26:06 +0100
parents
children 3b8797fc3e90
comparison
equal deleted inserted replaced
63:c2ac0efbce8d 64:5ea1999befd4
1 /* JAIImageLoaderDocuImage -- Image class implementation using JAI's ImageLoader Plugin
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.image.renderable.*;
31 import javax.media.jai.*;
32
33 import digilib.*;
34 import digilib.io.*;
35
36
37 public class JAIImageLoaderDocuImage extends DocuImageImpl {
38
39 private RenderedImage img;
40
41 public JAIImageLoaderDocuImage() {
42 }
43
44 public JAIImageLoaderDocuImage(Utils u) {
45 util = u;
46 }
47
48 /**
49 * load image file
50 */
51 public void loadImage(File f) throws FileOpException {
52 System.gc();
53 img = JAI.create("ImageRead", f.getAbsolutePath());
54 if (img == null) {
55 util.dprintln(3, "ERROR(loadImage): unable to load file");
56 throw new FileOpException("Unable to load File!");
57 }
58 }
59
60 /**
61 * write image of type mt to Stream
62 */
63 public void writeImage(String mt, ServletResponse res)
64 throws FileOpException {
65 try {
66 // setup output
67 ParameterBlock pb3 = new ParameterBlock();
68 pb3.addSource(img);
69 pb3.add(res.getOutputStream());
70 if (mt == "image/jpeg") {
71 pb3.add("JPEG");
72 } else if (mt == "image/png") {
73 pb3.add("PNG");
74 } else {
75 // unknown mime type
76 util.dprintln(2, "ERROR(writeImage): Unknown mime type "+mt);
77 throw new FileOpException("Unknown mime type: "+mt);
78 }
79 res.setContentType(mt);
80 // render output
81 JAI.create("ImageWrite", pb3);
82
83 } catch (IOException e) {
84 throw new FileOpException("Error writing image.");
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 Interpolation scaleInt = null;
112 // setup interpolation quality
113 if (qual > 1) {
114 util.dprintln(4, "quality q2");
115 scaleInt = Interpolation.getInstance(Interpolation.INTERP_BICUBIC);
116 } else if (qual == 1) {
117 util.dprintln(4, "quality q1");
118 scaleInt = Interpolation.getInstance(Interpolation.INTERP_BILINEAR);
119 } else {
120 util.dprintln(4, "quality q0");
121 scaleInt = Interpolation.getInstance(Interpolation.INTERP_NEAREST);
122 }
123
124 // setup Crop
125 ParameterBlock pb1 = new ParameterBlock();
126 pb1.addSource(img);
127 pb1.add((float)x_off);
128 pb1.add((float)y_off);
129 pb1.add((float)width);
130 pb1.add((float)height);
131 RenderedImage croppedImg = JAI.create("crop", pb1);
132 img = null; // free img
133
134 util.dprintln(3, "CROP:"+croppedImg.getWidth()+"x"+croppedImg.getHeight()); //DEBUG
135
136 if (croppedImg == null) {
137 util.dprintln(2, "ERROR(cropAndScale): error in crop");
138 throw new ImageOpException("Unable to crop");
139 }
140
141 // setup scale
142 ParameterBlock pb2 = new ParameterBlock();
143 pb2.addSource(croppedImg);
144 pb2.add(scale);
145 pb2.add(scale);
146 pb2.add(0f);
147 pb2.add(0f);
148 pb2.add(scaleInt);
149 // the following is nice but way too slow...
150 //if (opCrop.getColorModel().getPixelSize() < 8) {
151 // change color model if necessary
152 // util.dprintln("converting color model...");
153 // BufferedImage bi = new BufferedImage(1, 1, BufferedImage.TYPE_BYTE_GRAY);
154 // ImageLayout lay = new ImageLayout(bi);
155 // rh = new RenderingHints(JAI.KEY_IMAGE_LAYOUT, lay);
156 //}
157 RenderedImage scaledImg = JAI.create("scale", pb2);
158 croppedImg = null; // free opCrop
159
160 if (scaledImg == null) {
161 util.dprintln(2, "ERROR(cropAndScale): error in scale");
162 throw new ImageOpException("Unable to scale");
163 }
164
165 img = scaledImg;
166 }
167
168 }