fixed comment/article search + configurable icon sizes
[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: zapata $\r
39  * @version $Id: WebdbImage.java,v 1.10 2003/02/21 05:37:59 zapata 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 import mir.config.MirPropertiesConfiguration.PropertiesConfigExc;\r
56 \r
57 public class WebdbImage\r
58 {\r
59 \r
60   // default values for scaling\r
61   private int maxIconSize;\r
62   private int maxImageSize;\r
63 \r
64   private int iconWidth;\r
65   private int iconHeight;\r
66 \r
67   Random r = new Random();\r
68 \r
69   // internal representation of the image\r
70   private PlanarImage planarImage;\r
71 \r
72   // type of the image\r
73   private String _type;\r
74 \r
75   private WebdbImage() {\r
76   }\r
77 \r
78   // constructor\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
85 \r
86     planarImage = JAI.create("stream", new FileSeekableStream(f));\r
87     _type = type;\r
88     scaleImage();\r
89   }\r
90 \r
91   // acc3ssor-methods\r
92   // must be run after scaleIcon()\r
93   public int getIconWidth() throws IOException {\r
94     return iconWidth;\r
95   }\r
96 \r
97   // must be run after scaleIcon()\r
98   public int getIconHeight() throws IOException {\r
99     return iconHeight;\r
100   }\r
101 \r
102   public int getImageWidth() {\r
103     return (int) planarImage.getWidth();\r
104   }\r
105 \r
106   public int getImageHeight() {\r
107     return (int) planarImage.getHeight();\r
108   }\r
109 \r
110   public void setImage(OutputStream outStream) {\r
111     JAI.create("encode", planarImage, outStream, _type, null);\r
112   }\r
113 \r
114   public void setIcon(OutputStream outStream) throws IOException {\r
115     scaleIcon(outStream);\r
116   }\r
117 \r
118   private void scaleImage() throws java.io.IOException {\r
119     if (maxImageSize > 0 &&\r
120         (getImageHeight() > maxImageSize || getImageWidth() > maxImageSize)) {\r
121       float scale;\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
126       else\r
127         scale = (float) maxImageSize / (float) getImageWidth();\r
128 \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
135     }\r
136   }\r
137 \r
138   private void scaleIcon(OutputStream outStream) throws java.io.IOException {\r
139     float scale;\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
144     else\r
145       scale = (float) maxIconSize / (float) getImageWidth();\r
146 \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
156   }\r
157 \r
158 }