first cut of merge of STABLE-pre1_0 into HEAD. I won't even guarantee that it
[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         // default values for scaling
21         private int               maxIconSize=120;
22         private int               maxImageSize=640;
23
24         private byte[]            iconData;
25         private byte[]            imageData;
26         private int               iconWidth;
27         private int               iconHeight;
28
29         // internal representation of the image
30         private PlanarImage       planarImage;
31
32
33         // constructor
34         public WebdbImage(byte[] image)
35                 throws IOException
36         {
37                 planarImage = JAI.create("stream",new ByteArraySeekableStream(image));
38                 scaleImage();
39         }
40
41         public WebdbImage(byte[] image, int iconMax)
42                 throws IOException
43         {
44                 maxIconSize=iconMax;
45                 planarImage = JAI.create("stream",new ByteArraySeekableStream(image));
46                 scaleImage();
47         }
48
49         public WebdbImage(byte[] image, int iconMax, int imageMax)
50                 throws IOException
51         {
52                 maxIconSize=iconMax;
53                 maxImageSize=imageMax;
54                 planarImage = JAI.create("stream",new ByteArraySeekableStream(image));
55                 scaleImage();
56         }
57
58
59         // acc3ssor-methods
60         public int getIconWidth() throws IOException {
61                 if (iconData==null) scaleIcon();
62                 return iconWidth;
63         }
64
65         public int getIconHeight() throws IOException {
66                 if (iconData==null) scaleIcon();
67                 return iconHeight;
68         }
69
70         public int getImageWidth() {
71                 return (int)planarImage.getWidth();
72         }
73
74         public int getImageHeight() {
75                 return (int)planarImage.getHeight();
76         }
77
78         public byte[] getImage() {
79                 if (imageData==null) {
80                         ByteArrayOutputStream outStream = new ByteArrayOutputStream();
81             // @todo the choice of PNG or JPEG should be made configurable
82             JAI.create("encode", planarImage, outStream, "PNG", null);
83                         imageData = outStream.toByteArray();
84                 }
85                 return imageData;
86         }
87
88         public byte[] getIcon()
89                 throws IOException
90         {
91                 if (iconData == null) scaleIcon();
92                 return iconData;
93         }
94
95         private void scaleImage()
96                 throws java.io.IOException
97         {
98                 if (maxImageSize>0 && ( getImageHeight()> maxImageSize|| getImageWidth() >maxImageSize))
99                 {
100                         float scale;
101                         if (getImageHeight() > getImageWidth())
102                                 scale = (float)maxImageSize / (float)getImageHeight();
103                         else
104                                 scale = (float)maxImageSize / (float)getImageWidth();
105
106                         InterpolationBilinear interp = new InterpolationBilinear();
107                         planarImage = JAI.create("scale", planarImage, scale, scale, 0.0F, 0.0F, interp);
108                 }
109         }
110
111         private void scaleIcon()
112                 throws java.io.IOException
113         {
114                 if (iconData==null) {
115                         float scale;
116                         if (getImageHeight() > getImageWidth())
117                                 scale = (float)maxIconSize / (float)getImageHeight();
118                         else
119                                 scale = (float)maxIconSize / (float)getImageWidth();
120
121                         InterpolationBilinear interp = new InterpolationBilinear();
122                         PlanarImage temp = JAI.create("scale", planarImage, scale, scale, 0.0F, 0.0F, interp);
123                         ByteArrayOutputStream outStream = new ByteArrayOutputStream();
124             // @todo the choice of PNG or JPEG should be made configurable
125             JAI.create("encode", temp, outStream, "PNG", null);
126                         iconData = outStream.toByteArray();
127                         iconWidth=temp.getWidth();
128                         iconHeight=temp.getHeight();
129                 }
130         }
131
132 }