99ed77f1dbf46437a7e58ea326ea3c76cf7ffa1b
[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
90                         switch (imageType) {
91                                 case WEBDB_JPG:
92                                         JAI.create("encode", planarImage, outStream, "JPEG", null);break;
93                                 case WEBDB_GIF:
94                                         JAI.create("encode", planarImage, outStream, "JPEG", null);break;
95                                 default:
96                                         System.err.println("unknown image format: " + imageType);
97                         }
98
99                         imageData = outStream.toByteArray();
100                 }
101                 return imageData;
102         }
103
104         public byte[] getIcon()
105                 throws IOException
106         {
107                 if (iconData == null) scaleIcon();
108                 return iconData;
109         }
110
111         private void scaleImage()
112                 throws java.io.IOException
113         {
114                 if (maxImageSize>0 && ( getImageHeight()> maxImageSize|| getImageWidth() >maxImageSize))
115                 {
116                         float scale;
117                         if (getImageHeight() > getImageWidth())
118                                 scale = (float)maxImageSize / (float)getImageHeight();
119                         else
120                                 scale = (float)maxImageSize / (float)getImageWidth();
121
122                         InterpolationBilinear interp = new InterpolationBilinear();
123                         planarImage = JAI.create("scale", planarImage, scale, scale, 0.0F, 0.0F, interp);
124                 }
125         }
126
127         private void scaleIcon()
128                 throws java.io.IOException
129         {
130                 if (iconData==null) {
131                         float scale;
132                         if (getImageHeight() > getImageWidth())
133                                 scale = (float)maxIconSize / (float)getImageHeight();
134                         else
135                                 scale = (float)maxIconSize / (float)getImageWidth();
136
137                         InterpolationBilinear interp = new InterpolationBilinear();
138                         PlanarImage temp = JAI.create("scale", planarImage, scale, scale, 0.0F, 0.0F, interp);
139                         ByteArrayOutputStream outStream = new ByteArrayOutputStream();
140                         /** @todo gif */
141                         switch (imageType) {
142                                 case WEBDB_JPG:
143                                         JAI.create("encode", temp, outStream, "JPEG", null);break;
144                                 case WEBDB_GIF:
145                                         JAI.create("encode", temp, outStream, "JPEG", null);break;
146                                 default:
147                                         System.err.println("unknown image format: " + imageType);
148                         }
149                         iconData = outStream.toByteArray();
150                         iconWidth=temp.getWidth();
151                         iconHeight=temp.getHeight();
152                 }
153         }
154
155 }