Image handler that stores imagedata outside the database.
authorzapata <zapata>
Fri, 17 Jan 2003 17:34:52 +0000 (17:34 +0000)
committerzapata <zapata>
Fri, 17 Jan 2003 17:34:52 +0000 (17:34 +0000)
This handler is only temporary: a new media handler system will replace this.

source/mircoders/media/ImageProcessor.java [new file with mode: 0755]
source/mircoders/media/MediaHandlerGeneric.java
source/mircoders/media/MediaHandlerImagesExtern.java [new file with mode: 0755]
source/mircoders/producer/ProducerMedia.java

diff --git a/source/mircoders/media/ImageProcessor.java b/source/mircoders/media/ImageProcessor.java
new file mode 100755 (executable)
index 0000000..cc1d498
--- /dev/null
@@ -0,0 +1,153 @@
+/*
+ * Copyright (C) 2001, 2002  The Mir-coders group
+ *
+ * This file is part of Mir.
+ *
+ * Mir is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Mir is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Mir; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ * In addition, as a special exception, The Mir-coders gives permission to link
+ * the code of this program with the com.oreilly.servlet library, any library
+ * licensed under the Apache Software License, The Sun (tm) Java Advanced
+ * Imaging library (JAI), The Sun JIMI library (or with modified versions of
+ * the above that use the same license as the above), and distribute linked
+ * combinations including the two.  You must obey the GNU General Public
+ * License in all respects for all of the code used other than the above
+ * mentioned libraries.  If you modify this file, you may extend this exception
+ * to your version of the file, but you are not obligated to do so.  If you do
+ * not wish to do so, delete this exception statement from your version.
+ */
+
+package mircoders.media;
+
+import java.io.*;
+import java.awt.image.renderable.ParameterBlock;
+import javax.media.jai.*;
+import com.sun.media.jai.codec.*;
+
+import mir.log.*;
+
+/**
+ *
+ * <p>Title: Image processor</p>
+ * <p>Description: Temporary image processor class. (Made for the immediate needs of CMI brasil.
+ *                 Will become obsolete when mh's media handler rewrite is finished. </p>
+ * @author Zapata
+ * @version 1.0
+ */
+
+public class ImageProcessor {
+  static final LoggerWrapper logger = new LoggerWrapper("media");
+
+  private static final int DEFAULT_MAX_ICON_SIZE=120;
+  private static final int DEFAULT_MAX_IMAGE_SIZE=640;
+
+  private PlanarImage image;
+  private PlanarImage scaledImage;
+
+  private byte[] iconData;
+  private byte[] imageData;
+  private int iconWidth;
+  private int iconHeight;
+  private String imageType;
+
+//  public ImageScaler(
+
+  public ImageProcessor(SeekableStream anImageStream, String anImageType) throws IOException {
+    image = JAI.create("stream", anImageStream);
+    scaledImage = image;
+    imageType = anImageType;
+  }
+
+  public ImageProcessor(File aFile, String anImageType) throws IOException {
+    this(new FileSeekableStream(aFile), anImageType);
+  }
+
+  public ImageProcessor(byte[] anImageData, String anImageType) throws IOException {
+    this(new ByteArraySeekableStream(anImageData), anImageType);
+  }
+
+  public void descaleImage(int aMaxSize) throws java.io.IOException {
+    descaleImage(aMaxSize, 0);
+  }
+
+  public void descaleImage(int aMaxSize, float aMinDescale) throws java.io.IOException {
+    descaleImage(aMaxSize, aMaxSize, aMinDescale);
+  }
+
+  public void descaleImage(int aMaxWidth, int aMaxHeight, float aMinDescale) throws java.io.IOException {
+    float scale;
+    scaledImage = image;
+
+    if ((aMaxWidth>0 && image.getWidth()>aMaxWidth) || (aMaxHeight>0 && image.getHeight()>aMaxHeight))
+    {
+      logger.info("Scaling image");
+
+      scale=1;
+
+      if (aMaxWidth>0 && image.getWidth()>aMaxWidth) {
+        scale = Math.min(scale, (float) aMaxWidth / (float) image.getWidth());
+      }
+      if (aMaxHeight>0 && image.getHeight()>aMaxHeight) {
+        scale = Math.min(scale, (float) aMaxHeight / (float) image.getHeight());
+      }
+
+      if (1-scale>aMinDescale) {
+        scaleImage(scale);
+      }
+    }
+  }
+
+  public void scaleImage(float aScalingFactor) throws java.io.IOException {
+    ParameterBlockJAI params = new ParameterBlockJAI("scale");
+    params.addSource(image);
+
+    params.setParameter("xScale", aScalingFactor);
+    params.setParameter("yScale", aScalingFactor);
+    params.setParameter("xTrans", 0.0F);
+    params.setParameter("yTrans", 0.0F);
+    params.setParameter("interpolation", new InterpolationBilinear());
+    scaledImage = JAI.create("scale", params);
+  }
+
+  public int getWidth() {
+    return image.getWidth();
+  }
+
+  public int getHeight() {
+    return image.getWidth();
+  }
+
+  public int getScaledWidth() {
+    return scaledImage.getWidth();
+  }
+
+  public int getScaledHeight() {
+    return scaledImage.getWidth();
+  }
+
+  public void writeScaledData(OutputStream aStream) {
+    JAI.create("encode", scaledImage, aStream, imageType, null);
+  }
+
+  public byte[] getScaledData() {
+    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
+    writeScaledData(outputStream);
+    return outputStream.toByteArray();
+  }
+
+  public void writeScaledData(File aFile) throws IOException {
+    writeScaledData(new FileOutputStream(aFile));
+  }
+}
index 5f7b553..0c35158 100755 (executable)
@@ -59,7 +59,7 @@ import mir.storage.*;
  *
  * @see mir.media.MirMedia
  * @author mh <mh@nadir.org>
- * @version $Id: MediaHandlerGeneric.java,v 1.11 2002/11/27 08:57:32 mh Exp $
+ * @version $Id: MediaHandlerGeneric.java,v 1.12 2003/01/17 17:34:52 zapata Exp $
  */
 
 public class MediaHandlerGeneric implements MirMedia
@@ -122,7 +122,7 @@ public class MediaHandlerGeneric implements MirMedia
       return in;
     }
 
-    public InputStream getIcon (Entity ent) {
+    public InputStream getIcon (Entity ent) throws MirMediaException {
         return null;
     }
 
diff --git a/source/mircoders/media/MediaHandlerImagesExtern.java b/source/mircoders/media/MediaHandlerImagesExtern.java
new file mode 100755 (executable)
index 0000000..2ecdc89
--- /dev/null
@@ -0,0 +1,173 @@
+/*\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 mircoders.media;\r
+\r
+\r
+import java.lang.*;\r
+import java.io.*;\r
+import java.util.*;\r
+import java.lang.reflect.*;\r
+\r
+import freemarker.template.SimpleList;\r
+\r
+import mir.media.*;\r
+import mir.misc.*;\r
+import mir.entity.*;\r
+import mir.storage.*;\r
+\r
+import mircoders.entity.*;\r
+import mircoders.storage.*;\r
+\r
+\r
+/**\r
+ * Image handler that stores images outside of the database. Will be replaced by the new\r
+ *   media handling system.\r
+ * @author Zapata\r
+ * @version 1.0\r
+ */\r
+\r
+public class MediaHandlerImagesExtern extends MediaHandlerGeneric\r
+{\r
+  public void produce(Entity anImageEntity, Entity mediaTypeEnt) throws MirMediaException\r
+  {\r
+    try {\r
+      String date = anImageEntity.getValue("date");\r
+      String datePath = StringUtil.webdbDate2path(date);\r
+      String ext = "." + mediaTypeEnt.getValue("name");\r
+      String filePath = datePath + anImageEntity.getId() + ext;\r
+      String iconFilePath = MirConfig.getProp("Producer.StorageRoot") + getIconStoragePath() + filePath;\r
+      String imageFilePath = getStoragePath() + File.separator + filePath;\r
+\r
+      File imageFile = new File(imageFilePath);\r
+      File iconFile = new File(iconFilePath);\r
+\r
+      if (!imageFile.exists()) {\r
+        throw new MirMediaException("error in MediaHandlerImagesExtern.produce(): " + filePath + " does not exist!");\r
+      }\r
+      else {\r
+        ImageProcessor processor = new ImageProcessor(imageFile, "JPEG");\r
+\r
+        processor.descaleImage(50, 0.8F);\r
+        File dir = new File(iconFile.getParent());\r
+          if (dir!=null && !dir.exists()){\r
+            dir.mkdirs();\r
+        }\r
+        processor.writeScaledData(iconFile);\r
+\r
+        anImageEntity.setValueForProperty("img_height", new Integer(processor.getHeight()).toString());\r
+        anImageEntity.setValueForProperty("img_width", new Integer(processor.getWidth()).toString());\r
+\r
+        anImageEntity.setValueForProperty("icon_height", new Integer(processor.getScaledHeight()).toString());\r
+        anImageEntity.setValueForProperty("icon_width", new Integer(processor.getScaledWidth()).toString());\r
+\r
+        anImageEntity.setValueForProperty("icon_path", getIconStoragePath()+filePath);\r
+        anImageEntity.setValueForProperty("publish_path", filePath);\r
+\r
+        anImageEntity.update();\r
+      }\r
+    }\r
+    catch(Throwable t) {\r
+      t.printStackTrace(System.out);\r
+      throw new MirMediaException(t.getMessage());\r
+    }\r
+  }\r
+\r
+\r
+  public InputStream getIcon(Entity anImageEntity) throws MirMediaException\r
+  {\r
+    try {\r
+      Entity mediaType = DatabaseUploadedMedia.getInstance().getMediaType(\r
+          anImageEntity);\r
+\r
+      String date = anImageEntity.getValue("date");\r
+      String datePath = StringUtil.webdbDate2path(date);\r
+      String ext = "." + mediaType.getValue("name");\r
+      String filePath = MirConfig.getProp("Producer.StorageRoot") +\r
+          getIconStoragePath() + datePath + anImageEntity.getId() + ext;\r
+\r
+      return new FileInputStream(new File(filePath));\r
+    }\r
+    catch (Throwable t) {\r
+      throw new MirMediaException(t.getMessage());\r
+    }\r
+  }\r
+\r
+  public String getStoragePath()\r
+  {\r
+    return MirConfig.getProp("Producer.Image.Path");\r
+  }\r
+\r
+  public String getIconStoragePath()\r
+  {\r
+    return MirConfig.getProp("Producer.Image.IconPath");\r
+  }\r
+\r
+  public String getPublishHost()\r
+  {\r
+    return StringUtil.removeSlash(MirConfig.getProp("Producer.Image.Host"));\r
+  }\r
+\r
+  public String getTinyIconName()\r
+  {\r
+    return MirConfig.getProp("Producer.Icon.TinyImage");\r
+  }\r
+\r
+  public String getBigIconName()\r
+  {\r
+    return MirConfig.getProp("Producer.Icon.BigImage");\r
+  }\r
+\r
+  public String getIconAltName()\r
+  {\r
+    return "Image";\r
+  }\r
+\r
+  public boolean isVideo()\r
+  {\r
+    return false;\r
+  }\r
+\r
+  public boolean isAudio()\r
+  {\r
+    return false;\r
+  }\r
+\r
+  public boolean isImage ()\r
+  {\r
+    return true;\r
+  }\r
+\r
+  public String getDescr(Entity mediaType)\r
+  {\r
+     return "image/jpeg";\r
+  }\r
+}\r
index 7f77cc4..be0c1b3 100755 (executable)
@@ -107,7 +107,7 @@ abstract public class ProducerMedia extends Producer {
                             e.toString());
           logHTML(htmlout, "problem with media id: "+currentMedia.getId()+
                   " <font color=\"Red\"> failed!</font>: "+e.toString());
-          e.printStackTrace(htmlout);
+          e.printStackTrace(System.out);
         }
       }