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