we now produce PNG's instead of JPEGs as they are even freer and support
[mir.git] / source / mir / misc / WebdbImage.java
1 package mir.misc;
2
3 /**
4  * Title:
5  * Description:
6  * Copyright:    Copyright (c) 2001
7  * Company:      Indymedia
8  * @author
9  * @version 1.0
10  */
11
12 import java.io.*;
13 import javax.media.jai.*;
14 import com.sun.media.jai.codec.*;
15 import java.awt.image.renderable.ParameterBlock;
16
17 public class WebdbImage
18 {
19
20         // imageTypes
21         public final static int        WEBDB_JPG=0;
22         public final static int        WEBDB_GIF=1;
23
24         // default values for scaling
25         private int               maxIconSize=120;
26         private int               maxImageSize=640;
27
28         private byte[]            iconData;
29         private byte[]            imageData;
30         private int               imageType;
31         private int               iconWidth;
32         private int               iconHeight;
33
34         // internal representation of the image
35         private PlanarImage       planarImage;
36
37
38         // constructor
39         public WebdbImage(byte[] image, int type)
40                 throws IOException
41         {
42                 imageType=type;
43                 planarImage = JAI.create("stream",new ByteArraySeekableStream(image));
44                 scaleImage();
45         }
46
47         public WebdbImage(byte[] image, int type, int iconMax)
48                 throws IOException
49         {
50                 imageType=type;
51                 maxIconSize=iconMax;
52                 planarImage = JAI.create("stream",new ByteArraySeekableStream(image));
53                 scaleImage();
54         }
55
56         public WebdbImage(byte[] image, int type, int iconMax, int imageMax)
57                 throws IOException
58         {
59                 imageType=type;
60                 maxIconSize=iconMax;
61                 maxImageSize=imageMax;
62                 planarImage = JAI.create("stream",new ByteArraySeekableStream(image));
63                 scaleImage();
64         }
65
66
67         // acc3ssor-methods
68         public int getIconWidth() throws IOException {
69                 if (iconData==null) scaleIcon();
70                 return iconWidth;
71         }
72
73         public int getIconHeight() throws IOException {
74                 if (iconData==null) scaleIcon();
75                 return iconHeight;
76         }
77
78         public int getImageWidth() {
79                 return (int)planarImage.getWidth();
80         }
81
82         public int getImageHeight() {
83                 return (int)planarImage.getHeight();
84         }
85
86         public byte[] getImage() {
87                 if (imageData==null) {
88                         ByteArrayOutputStream outStream = new ByteArrayOutputStream();
89             // @todo the choice of PNG or JPEG should be made configurable
90             JAI.create("encode", planarImage, outStream, "PNG", null);
91                         imageData = outStream.toByteArray();
92                 }
93                 return imageData;
94         }
95
96         public byte[] getIcon()
97                 throws IOException
98         {
99                 if (iconData == null) scaleIcon();
100                 return iconData;
101         }
102
103         private void scaleImage()
104                 throws java.io.IOException
105         {
106                 if (maxImageSize>0 && ( getImageHeight()> maxImageSize|| getImageWidth() >maxImageSize))
107                 {
108                         float scale;
109                         if (getImageHeight() > getImageWidth())
110                                 scale = (float)maxImageSize / (float)getImageHeight();
111                         else
112                                 scale = (float)maxImageSize / (float)getImageWidth();
113
114                         InterpolationBilinear interp = new InterpolationBilinear();
115                         planarImage = JAI.create("scale", planarImage, scale, scale, 0.0F, 0.0F, interp);
116                 }
117         }
118
119         private void scaleIcon()
120                 throws java.io.IOException
121         {
122                 if (iconData==null) {
123                         float scale;
124                         if (getImageHeight() > getImageWidth())
125                                 scale = (float)maxIconSize / (float)getImageHeight();
126                         else
127                                 scale = (float)maxIconSize / (float)getImageWidth();
128
129                         InterpolationBilinear interp = new InterpolationBilinear();
130                         PlanarImage temp = JAI.create("scale", planarImage, scale, scale, 0.0F, 0.0F, interp);
131                         ByteArrayOutputStream outStream = new ByteArrayOutputStream();
132             // @todo the choice of PNG or JPEG should be made configurable
133             JAI.create("encode", temp, outStream, "PNG", null);
134                         iconData = outStream.toByteArray();
135                         iconWidth=temp.getWidth();
136                         iconHeight=temp.getHeight();
137                 }
138         }
139
140 }