ff035b906df873aca0b9c3f00897b74dfdc42b1d
[mir.git] / source / mir / misc / WebdbImage.java
1 /*
2  * Copyright (C) 2001, 2002  The Mir-coders group
3  *
4  * This file is part of Mir.
5  *
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.
10  *
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.
15  *
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
19  *
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.
30  */
31
32 package mir.misc;
33
34 /**
35  * Title:
36  * Description:
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 $
40  */
41
42 import java.io.File;
43 import java.io.IOException;
44 import java.io.OutputStream;
45 import java.util.Random;
46
47 import javax.media.jai.InterpolationBilinear;
48 import javax.media.jai.JAI;
49 import javax.media.jai.ParameterBlockJAI;
50 import javax.media.jai.PlanarImage;
51
52 import com.sun.media.jai.codec.FileSeekableStream;
53
54 public class WebdbImage
55 {
56
57         // default values for scaling
58         private int               maxIconSize=120;
59         private int               maxImageSize=640;
60
61         private int               iconWidth;
62         private int               iconHeight;
63
64   Random r = new Random();
65
66         // internal representation of the image
67         private PlanarImage       planarImage;
68
69   // type of the image
70   private String _type;
71
72
73         // constructor
74   // takes a temporary file as a parameter
75         public WebdbImage(File f, String type)
76                 throws IOException
77         {
78     // It has to be a FileSeekableStream cause the image conversion
79     // needs to seek backwards.
80                 planarImage = JAI.create("stream",new FileSeekableStream(f));
81     _type = type;
82                 scaleImage();
83         }
84
85         // acc3ssor-methods
86   // must be run after scaleIcon()
87         public int getIconWidth() throws IOException {
88                 return iconWidth;
89         }
90
91   // must be run after scaleIcon()
92         public int getIconHeight() throws IOException {
93                 return iconHeight;
94         }
95
96         public int getImageWidth() {
97                 return (int)planarImage.getWidth();
98         }
99
100         public int getImageHeight() {
101                 return (int)planarImage.getHeight();
102         }
103
104         public void setImage(OutputStream outStream) {
105     JAI.create("encode", planarImage, outStream, _type, null);
106         }
107
108         public void setIcon(OutputStream outStream)
109                 throws IOException
110         {
111                 scaleIcon(outStream);
112         }
113
114         private void scaleImage()
115                 throws java.io.IOException
116         {
117                 if (maxImageSize>0 && ( getImageHeight()> maxImageSize|| getImageWidth() >maxImageSize))
118                 {
119                         float scale;
120       ParameterBlockJAI params = new ParameterBlockJAI("scale");
121       params.addSource(planarImage);
122                         if (getImageHeight() > getImageWidth())
123         scale = (float)maxImageSize / (float)getImageHeight();
124       else
125         scale = (float)maxImageSize / (float)getImageWidth();
126
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);
133                 }
134         }
135
136         private void scaleIcon(OutputStream outStream)
137                 throws java.io.IOException
138         {
139     float scale;
140     ParameterBlockJAI params = new ParameterBlockJAI("scale");
141     params.addSource(planarImage);
142     if (getImageHeight() > getImageWidth())
143       scale = (float)maxIconSize / (float)getImageHeight();
144     else
145       scale = (float)maxIconSize / (float)getImageWidth();
146
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();
156         }
157
158 }