2 * Copyright (C) 2001, 2002 The Mir-coders group
4 * This file is part of Mir.
6 * Mir is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * Mir is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with Mir; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * In addition, as a special exception, The Mir-coders gives permission to link
21 * the code of this program with the com.oreilly.servlet library, any library
22 * licensed under the Apache Software License, The Sun (tm) Java Advanced
23 * Imaging library (JAI), The Sun JIMI library (or with modified versions of
24 * the above that use the same license as the above), and distribute linked
25 * combinations including the two. You must obey the GNU General Public
26 * License in all respects for all of the code used other than the above
27 * mentioned libraries. If you modify this file, you may extend this exception
28 * to your version of the file, but you are not obligated to do so. If you do
29 * not wish to do so, delete this exception statement from your version.
37 * Copyright: Copyright (c) 2002 Mir-coders
38 * @author $Author: idfx $
39 * @version $Id: WebdbImage.java,v 1.9 2003/01/25 17:45:18 idfx Exp $
43 import java.io.IOException;
44 import java.io.OutputStream;
45 import java.util.Random;
47 import javax.media.jai.InterpolationBilinear;
48 import javax.media.jai.JAI;
49 import javax.media.jai.ParameterBlockJAI;
50 import javax.media.jai.PlanarImage;
52 import com.sun.media.jai.codec.FileSeekableStream;
54 public class WebdbImage
57 // default values for scaling
58 private int maxIconSize=120;
59 private int maxImageSize=640;
61 private int iconWidth;
62 private int iconHeight;
64 Random r = new Random();
66 // internal representation of the image
67 private PlanarImage planarImage;
74 // takes a temporary file as a parameter
75 public WebdbImage(File f, String type)
78 // It has to be a FileSeekableStream cause the image conversion
79 // needs to seek backwards.
80 planarImage = JAI.create("stream",new FileSeekableStream(f));
86 // must be run after scaleIcon()
87 public int getIconWidth() throws IOException {
91 // must be run after scaleIcon()
92 public int getIconHeight() throws IOException {
96 public int getImageWidth() {
97 return (int)planarImage.getWidth();
100 public int getImageHeight() {
101 return (int)planarImage.getHeight();
104 public void setImage(OutputStream outStream) {
105 JAI.create("encode", planarImage, outStream, _type, null);
108 public void setIcon(OutputStream outStream)
111 scaleIcon(outStream);
114 private void scaleImage()
115 throws java.io.IOException
117 if (maxImageSize>0 && ( getImageHeight()> maxImageSize|| getImageWidth() >maxImageSize))
120 ParameterBlockJAI params = new ParameterBlockJAI("scale");
121 params.addSource(planarImage);
122 if (getImageHeight() > getImageWidth())
123 scale = (float)maxImageSize / (float)getImageHeight();
125 scale = (float)maxImageSize / (float)getImageWidth();
127 params.setParameter("xScale", scale);
128 params.setParameter("yScale", scale);
129 params.setParameter("xTrans",0.0F);
130 params.setParameter("yTrans",0.0F);
131 params.setParameter("interpolation",new InterpolationBilinear());
132 planarImage = JAI.create("scale", params);
136 private void scaleIcon(OutputStream outStream)
137 throws java.io.IOException
140 ParameterBlockJAI params = new ParameterBlockJAI("scale");
141 params.addSource(planarImage);
142 if (getImageHeight() > getImageWidth())
143 scale = (float)maxIconSize / (float)getImageHeight();
145 scale = (float)maxIconSize / (float)getImageWidth();
147 params.setParameter("xScale", scale);
148 params.setParameter("yScale", scale);
149 params.setParameter("xTrans",0.0F);
150 params.setParameter("yTrans",0.0F);
151 params.setParameter("interpolation",new InterpolationBilinear());
152 PlanarImage temp = JAI.create("scale", params);
153 JAI.create("encode", temp, outStream, _type, null);
154 iconWidth=temp.getWidth();
155 iconHeight=temp.getHeight();