Initial revision
[mir.git] / source / mir / misc / WebdbImage.java
diff --git a/source/mir/misc/WebdbImage.java b/source/mir/misc/WebdbImage.java
new file mode 100755 (executable)
index 0000000..f079948
--- /dev/null
@@ -0,0 +1,155 @@
+package mir.misc;
+
+/**
+ * Title:
+ * Description:
+ * Copyright:    Copyright (c) 2001
+ * Company:      Indymedia
+ * @author
+ * @version 1.0
+ */
+
+import java.io.*;
+import javax.media.jai.*;
+import com.sun.media.jai.codec.*;
+import java.awt.image.renderable.ParameterBlock;
+
+public class WebdbImage
+{
+
+       // imageTypes
+       public final static int        WEBDB_JPG=0;
+       public final static int        WEBDB_GIF=1;
+
+       // default values for scaling
+       private int               maxIconSize=120;
+       private int               maxImageSize=640;
+
+       private byte[]            iconData;
+       private byte[]            imageData;
+       private int               imageType;
+       private int               iconWidth;
+       private int               iconHeight;
+
+       // internal representation of the image
+       private PlanarImage       planarImage;
+
+
+       // constructor
+       public WebdbImage(byte[] image, int type)
+               throws IOException
+       {
+               imageType=type;
+               planarImage = JAI.create("stream",new ByteArraySeekableStream(image));
+               scaleImage();
+       }
+
+       public WebdbImage(byte[] image, int type, int iconMax)
+               throws IOException
+       {
+               imageType=type;
+               maxIconSize=iconMax;
+               planarImage = JAI.create("stream",new ByteArraySeekableStream(image));
+               scaleImage();
+       }
+
+       public WebdbImage(byte[] image, int type, int iconMax, int imageMax)
+               throws IOException
+       {
+               imageType=type;
+               maxIconSize=iconMax;
+               maxImageSize=imageMax;
+               planarImage = JAI.create("stream",new ByteArraySeekableStream(image));
+               scaleImage();
+       }
+
+
+       // acc3ssor-methods
+       public int getIconWidth() throws IOException {
+               if (iconData==null) scaleIcon();
+               return iconWidth;
+       }
+
+       public int getIconHeight() throws IOException {
+               if (iconData==null) scaleIcon();
+               return iconHeight;
+       }
+
+       public int getImageWidth() {
+               return (int)planarImage.getWidth();
+       }
+
+       public int getImageHeight() {
+               return (int)planarImage.getHeight();
+       }
+
+       public byte[] getImage() {
+               if (imageData==null) {
+                       ByteArrayOutputStream outStream = new ByteArrayOutputStream();
+
+                       switch (imageType) {
+                               case WEBDB_JPG:
+                                       JAI.create("encode", planarImage, outStream, "JPEG", null);break;
+                               case WEBDB_GIF:
+                                       JAI.create("encode", planarImage, outStream, "JPEG", null);break;
+                               default:
+                                       System.err.println("unknown image format: " + imageType);
+                       }
+
+                       imageData = outStream.toByteArray();
+               }
+               return imageData;
+       }
+
+       public byte[] getIcon()
+               throws IOException
+       {
+               if (iconData == null) scaleIcon();
+               return iconData;
+       }
+
+       private void scaleImage()
+               throws java.io.IOException
+       {
+               if (maxImageSize>0 && ( getImageHeight()> maxImageSize|| getImageWidth() >maxImageSize))
+               {
+                       float scale;
+                       if (getImageHeight() > getImageWidth())
+                               scale = (float)maxImageSize / (float)getImageHeight();
+                       else
+                               scale = (float)maxImageSize / (float)getImageWidth();
+
+                       InterpolationBilinear interp = new InterpolationBilinear();
+                       planarImage = JAI.create("scale", planarImage, scale, scale, 0.0F, 0.0F, interp);
+               }
+       }
+
+       private void scaleIcon()
+               throws java.io.IOException
+       {
+               if (iconData==null) {
+                       float scale;
+                       if (getImageHeight() > getImageWidth())
+                               scale = (float)maxIconSize / (float)getImageHeight();
+                       else
+                               scale = (float)maxIconSize / (float)getImageWidth();
+
+                       InterpolationBilinear interp = new InterpolationBilinear();
+                       PlanarImage temp = JAI.create("scale", planarImage, scale, scale, 0.0F, 0.0F, interp);
+                       ByteArrayOutputStream outStream = new ByteArrayOutputStream();
+                       /** @todo gif */
+                       switch (imageType) {
+                               case WEBDB_JPG:
+                                       JAI.create("encode", temp, outStream, "JPEG", null);break;
+                               case WEBDB_GIF:
+                                       JAI.create("encode", temp, outStream, "JPEG", null);break;
+                               default:
+                                       System.err.println("unknown image format: " + imageType);
+                       }
+                       iconData = outStream.toByteArray();
+                       iconWidth=temp.getWidth();
+                       iconHeight=temp.getHeight();
+               }
+       }
+
+}
\ No newline at end of file