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
20 // default values for scaling
21 private int maxIconSize=120;
22 private int maxImageSize=640;
24 private byte[] iconData;
25 private byte[] imageData;
26 private int iconWidth;
27 private int iconHeight;
29 // internal representation of the image
30 private PlanarImage planarImage;
34 public WebdbImage(byte[] image)
37 planarImage = JAI.create("stream",new ByteArraySeekableStream(image));
41 public WebdbImage(byte[] image, int iconMax)
45 planarImage = JAI.create("stream",new ByteArraySeekableStream(image));
49 public WebdbImage(byte[] image, int iconMax, int imageMax)
53 maxImageSize=imageMax;
54 planarImage = JAI.create("stream",new ByteArraySeekableStream(image));
60 public int getIconWidth() throws IOException {
61 if (iconData==null) scaleIcon();
65 public int getIconHeight() throws IOException {
66 if (iconData==null) scaleIcon();
70 public int getImageWidth() {
71 return (int)planarImage.getWidth();
74 public int getImageHeight() {
75 return (int)planarImage.getHeight();
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();
88 public byte[] getIcon()
91 if (iconData == null) scaleIcon();
95 private void scaleImage()
96 throws java.io.IOException
98 if (maxImageSize>0 && ( getImageHeight()> maxImageSize|| getImageWidth() >maxImageSize))
101 if (getImageHeight() > getImageWidth())
102 scale = (float)maxImageSize / (float)getImageHeight();
104 scale = (float)maxImageSize / (float)getImageWidth();
106 InterpolationBilinear interp = new InterpolationBilinear();
107 planarImage = JAI.create("scale", planarImage, scale, scale, 0.0F, 0.0F, interp);
111 private void scaleIcon()
112 throws java.io.IOException
114 if (iconData==null) {
116 if (getImageHeight() > getImageWidth())
117 scale = (float)maxIconSize / (float)getImageHeight();
119 scale = (float)maxIconSize / (float)getImageWidth();
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();