6 * Copyright: Copyright (c) 2001
13 import javax.media.jai.*;
14 import com.sun.media.jai.codec.*;
15 import java.awt.image.renderable.ParameterBlock;
17 public class WebdbImage
21 public final static int WEBDB_JPG=0;
22 public final static int WEBDB_GIF=1;
24 // default values for scaling
25 private int maxIconSize=120;
26 private int maxImageSize=640;
28 private byte[] iconData;
29 private byte[] imageData;
30 private int imageType;
31 private int iconWidth;
32 private int iconHeight;
34 // internal representation of the image
35 private PlanarImage planarImage;
39 public WebdbImage(byte[] image, int type)
43 planarImage = JAI.create("stream",new ByteArraySeekableStream(image));
47 public WebdbImage(byte[] image, int type, int iconMax)
52 planarImage = JAI.create("stream",new ByteArraySeekableStream(image));
56 public WebdbImage(byte[] image, int type, int iconMax, int imageMax)
61 maxImageSize=imageMax;
62 planarImage = JAI.create("stream",new ByteArraySeekableStream(image));
68 public int getIconWidth() throws IOException {
69 if (iconData==null) scaleIcon();
73 public int getIconHeight() throws IOException {
74 if (iconData==null) scaleIcon();
78 public int getImageWidth() {
79 return (int)planarImage.getWidth();
82 public int getImageHeight() {
83 return (int)planarImage.getHeight();
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();
96 public byte[] getIcon()
99 if (iconData == null) scaleIcon();
103 private void scaleImage()
104 throws java.io.IOException
106 if (maxImageSize>0 && ( getImageHeight()> maxImageSize|| getImageWidth() >maxImageSize))
109 if (getImageHeight() > getImageWidth())
110 scale = (float)maxImageSize / (float)getImageHeight();
112 scale = (float)maxImageSize / (float)getImageWidth();
114 InterpolationBilinear interp = new InterpolationBilinear();
115 planarImage = JAI.create("scale", planarImage, scale, scale, 0.0F, 0.0F, interp);
119 private void scaleIcon()
120 throws java.io.IOException
122 if (iconData==null) {
124 if (getImageHeight() > getImageWidth())
125 scale = (float)maxIconSize / (float)getImageHeight();
127 scale = (float)maxIconSize / (float)getImageWidth();
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();