Some small things like, reorganizing imports, accessing static variables
[mir.git] / source / mir / misc / WebdbImage.java
1 /*\r
2  * Copyright (C) 2001, 2002  The Mir-coders group\r
3  *\r
4  * This file is part of Mir.\r
5  *\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
10  *\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
15  *\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
19  *\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
30  */\r
31 \r
32 package mir.misc;\r
33 \r
34 /**\r
35  * Title:\r
36  * Description:\r
37  * Copyright:    Copyright (c) 2002 Mir-coders\r
38  * @author $Author: idfx $\r
39  * @version $Id: WebdbImage.java,v 1.11 2003/02/28 18:27:07 idfx Exp $\r
40  */\r
41 \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
46 \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
51 \r
52 import com.sun.media.jai.codec.FileSeekableStream;\r
53 \r
54 import mir.config.MirPropertiesConfiguration;\r
55 \r
56 public class WebdbImage\r
57 {\r
58 \r
59   // default values for scaling\r
60   private int maxIconSize;\r
61   private int maxImageSize;\r
62 \r
63   private int iconWidth;\r
64   private int iconHeight;\r
65 \r
66   Random r = new Random();\r
67 \r
68   // internal representation of the image\r
69   private PlanarImage planarImage;\r
70 \r
71   // type of the image\r
72   private String _type;\r
73 \r
74   private WebdbImage() {\r
75   }\r
76 \r
77   // constructor\r
78   // takes a temporary file as a parameter\r
79   public WebdbImage(File f, String type) throws Exception {\r
80     // It has to be a FileSeekableStream cause the image conversion\r
81     // needs to seek backwards.\r
82     maxImageSize = MirPropertiesConfiguration.instance().getInt("Producer.Image.MaxSize");\r
83     maxIconSize = MirPropertiesConfiguration.instance().getInt("Producer.Image.MaxIconSize");\r
84 \r
85     planarImage = JAI.create("stream", new FileSeekableStream(f));\r
86     _type = type;\r
87     scaleImage();\r
88   }\r
89 \r
90   // acc3ssor-methods\r
91   // must be run after scaleIcon()\r
92   public int getIconWidth() throws IOException {\r
93     return iconWidth;\r
94   }\r
95 \r
96   // must be run after scaleIcon()\r
97   public int getIconHeight() throws IOException {\r
98     return iconHeight;\r
99   }\r
100 \r
101   public int getImageWidth() {\r
102     return (int) planarImage.getWidth();\r
103   }\r
104 \r
105   public int getImageHeight() {\r
106     return (int) planarImage.getHeight();\r
107   }\r
108 \r
109   public void setImage(OutputStream outStream) {\r
110     JAI.create("encode", planarImage, outStream, _type, null);\r
111   }\r
112 \r
113   public void setIcon(OutputStream outStream) throws IOException {\r
114     scaleIcon(outStream);\r
115   }\r
116 \r
117   private void scaleImage() throws java.io.IOException {\r
118     if (maxImageSize > 0 &&\r
119         (getImageHeight() > maxImageSize || getImageWidth() > maxImageSize)) {\r
120       float scale;\r
121       ParameterBlockJAI params = new ParameterBlockJAI("scale");\r
122       params.addSource(planarImage);\r
123       if (getImageHeight() > getImageWidth())\r
124         scale = (float) maxImageSize / (float) getImageHeight();\r
125       else\r
126         scale = (float) maxImageSize / (float) getImageWidth();\r
127 \r
128       params.setParameter("xScale", scale);\r
129       params.setParameter("yScale", scale);\r
130       params.setParameter("xTrans", 0.0F);\r
131       params.setParameter("yTrans", 0.0F);\r
132       params.setParameter("interpolation", new InterpolationBilinear());\r
133       planarImage = JAI.create("scale", params);\r
134     }\r
135   }\r
136 \r
137   private void scaleIcon(OutputStream outStream) throws java.io.IOException {\r
138     float scale;\r
139     ParameterBlockJAI params = new ParameterBlockJAI("scale");\r
140     params.addSource(planarImage);\r
141     if (getImageHeight() > getImageWidth())\r
142       scale = (float) maxIconSize / (float) getImageHeight();\r
143     else\r
144       scale = (float) maxIconSize / (float) getImageWidth();\r
145 \r
146     params.setParameter("xScale", scale);\r
147     params.setParameter("yScale", scale);\r
148     params.setParameter("xTrans", 0.0F);\r
149     params.setParameter("yTrans", 0.0F);\r
150     params.setParameter("interpolation", new InterpolationBilinear());\r
151     PlanarImage temp = JAI.create("scale", params);\r
152     JAI.create("encode", temp, outStream, _type, null);\r
153     iconWidth = temp.getWidth();\r
154     iconHeight = temp.getHeight();\r
155   }\r
156 \r
157 }