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();
92 JAI.create("encode", planarImage, outStream, "JPEG", null);break;
94 JAI.create("encode", planarImage, outStream, "JPEG", null);break;
96 System.err.println("unknown image format: " + imageType);
99 imageData = outStream.toByteArray();
104 public byte[] getIcon()
107 if (iconData == null) scaleIcon();
111 private void scaleImage()
112 throws java.io.IOException
114 if (maxImageSize>0 && ( getImageHeight()> maxImageSize|| getImageWidth() >maxImageSize))
117 if (getImageHeight() > getImageWidth())
118 scale = (float)maxImageSize / (float)getImageHeight();
120 scale = (float)maxImageSize / (float)getImageWidth();
122 InterpolationBilinear interp = new InterpolationBilinear();
123 planarImage = JAI.create("scale", planarImage, scale, scale, 0.0F, 0.0F, interp);
127 private void scaleIcon()
128 throws java.io.IOException
130 if (iconData==null) {
132 if (getImageHeight() > getImageWidth())
133 scale = (float)maxIconSize / (float)getImageHeight();
135 scale = (float)maxIconSize / (float)getImageWidth();
137 InterpolationBilinear interp = new InterpolationBilinear();
138 PlanarImage temp = JAI.create("scale", planarImage, scale, scale, 0.0F, 0.0F, interp);
139 ByteArrayOutputStream outStream = new ByteArrayOutputStream();
143 JAI.create("encode", temp, outStream, "JPEG", null);break;
145 JAI.create("encode", temp, outStream, "JPEG", null);break;
147 System.err.println("unknown image format: " + imageType);
149 iconData = outStream.toByteArray();
150 iconWidth=temp.getWidth();
151 iconHeight=temp.getHeight();