organizing imports
[mir.git] / source / mir / misc / WebdbImage.java
index 99ed77f..136acb1 100755 (executable)
-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();
-               }
-       }
-
-}
+/*\r
+ * Copyright (C) 2001, 2002  The Mir-coders group\r
+ *\r
+ * This file is part of Mir.\r
+ *\r
+ * Mir is free software; you can redistribute it and/or modify\r
+ * it under the terms of the GNU General Public License as published by\r
+ * the Free Software Foundation; either version 2 of the License, or\r
+ * (at your option) any later version.\r
+ *\r
+ * Mir is distributed in the hope that it will be useful,\r
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
+ * GNU General Public License for more details.\r
+ *\r
+ * You should have received a copy of the GNU General Public License\r
+ * along with Mir; if not, write to the Free Software\r
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
+ *\r
+ * In addition, as a special exception, The Mir-coders gives permission to link\r
+ * the code of this program with the com.oreilly.servlet library, any library\r
+ * licensed under the Apache Software License, The Sun (tm) Java Advanced\r
+ * Imaging library (JAI), The Sun JIMI library (or with modified versions of\r
+ * the above that use the same license as the above), and distribute linked\r
+ * combinations including the two.  You must obey the GNU General Public\r
+ * License in all respects for all of the code used other than the above\r
+ * mentioned libraries.  If you modify this file, you may extend this exception\r
+ * to your version of the file, but you are not obligated to do so.  If you do\r
+ * not wish to do so, delete this exception statement from your version.\r
+ */\r
+\r
+package mir.misc;\r
+\r
+/**\r
+ * Title:\r
+ * Description:\r
+ * Copyright:    Copyright (c) 2002 Mir-coders\r
+ * @author $Author: idfx $\r
+ * @version $Id: WebdbImage.java,v 1.12 2003/03/05 19:23:14 idfx Exp $\r
+ */\r
+\r
+import java.io.File;\r
+import java.io.IOException;\r
+import java.io.OutputStream;\r
+import java.util.Random;\r
+\r
+import javax.media.jai.InterpolationBilinear;\r
+import javax.media.jai.JAI;\r
+import javax.media.jai.ParameterBlockJAI;\r
+import javax.media.jai.PlanarImage;\r
+\r
+import mir.config.MirPropertiesConfiguration;\r
+\r
+import com.sun.media.jai.codec.FileSeekableStream;\r
+\r
+public class WebdbImage\r
+{\r
+\r
+  // default values for scaling\r
+  private int maxIconSize;\r
+  private int maxImageSize;\r
+\r
+  private int iconWidth;\r
+  private int iconHeight;\r
+\r
+  Random r = new Random();\r
+\r
+  // internal representation of the image\r
+  private PlanarImage planarImage;\r
+\r
+  // type of the image\r
+  private String _type;\r
+\r
+  private WebdbImage() {\r
+  }\r
+\r
+  // constructor\r
+  // takes a temporary file as a parameter\r
+  public WebdbImage(File f, String type) throws Exception {\r
+    // It has to be a FileSeekableStream cause the image conversion\r
+    // needs to seek backwards.\r
+    maxImageSize = MirPropertiesConfiguration.instance().getInt("Producer.Image.MaxSize");\r
+    maxIconSize = MirPropertiesConfiguration.instance().getInt("Producer.Image.MaxIconSize");\r
+\r
+    planarImage = JAI.create("stream", new FileSeekableStream(f));\r
+    _type = type;\r
+    scaleImage();\r
+  }\r
+\r
+  // acc3ssor-methods\r
+  // must be run after scaleIcon()\r
+  public int getIconWidth() throws IOException {\r
+    return iconWidth;\r
+  }\r
+\r
+  // must be run after scaleIcon()\r
+  public int getIconHeight() throws IOException {\r
+    return iconHeight;\r
+  }\r
+\r
+  public int getImageWidth() {\r
+    return (int) planarImage.getWidth();\r
+  }\r
+\r
+  public int getImageHeight() {\r
+    return (int) planarImage.getHeight();\r
+  }\r
+\r
+  public void setImage(OutputStream outStream) {\r
+    JAI.create("encode", planarImage, outStream, _type, null);\r
+  }\r
+\r
+  public void setIcon(OutputStream outStream) throws IOException {\r
+    scaleIcon(outStream);\r
+  }\r
+\r
+  private void scaleImage() throws java.io.IOException {\r
+    if (maxImageSize > 0 &&\r
+        (getImageHeight() > maxImageSize || getImageWidth() > maxImageSize)) {\r
+      float scale;\r
+      ParameterBlockJAI params = new ParameterBlockJAI("scale");\r
+      params.addSource(planarImage);\r
+      if (getImageHeight() > getImageWidth())\r
+        scale = (float) maxImageSize / (float) getImageHeight();\r
+      else\r
+        scale = (float) maxImageSize / (float) getImageWidth();\r
+\r
+      params.setParameter("xScale", scale);\r
+      params.setParameter("yScale", scale);\r
+      params.setParameter("xTrans", 0.0F);\r
+      params.setParameter("yTrans", 0.0F);\r
+      params.setParameter("interpolation", new InterpolationBilinear());\r
+      planarImage = JAI.create("scale", params);\r
+    }\r
+  }\r
+\r
+  private void scaleIcon(OutputStream outStream) throws java.io.IOException {\r
+    float scale;\r
+    ParameterBlockJAI params = new ParameterBlockJAI("scale");\r
+    params.addSource(planarImage);\r
+    if (getImageHeight() > getImageWidth())\r
+      scale = (float) maxIconSize / (float) getImageHeight();\r
+    else\r
+      scale = (float) maxIconSize / (float) getImageWidth();\r
+\r
+    params.setParameter("xScale", scale);\r
+    params.setParameter("yScale", scale);\r
+    params.setParameter("xTrans", 0.0F);\r
+    params.setParameter("yTrans", 0.0F);\r
+    params.setParameter("interpolation", new InterpolationBilinear());\r
+    PlanarImage temp = JAI.create("scale", params);\r
+    JAI.create("encode", temp, outStream, _type, null);\r
+    iconWidth = temp.getWidth();\r
+    iconHeight = temp.getHeight();\r
+  }\r
+\r
+}
\ No newline at end of file