fixed the handling of GIF images now also for the classic image handlers
authorzapata <zapata>
Wed, 30 Apr 2003 00:37:27 +0000 (00:37 +0000)
committerzapata <zapata>
Wed, 30 Apr 2003 00:37:27 +0000 (00:37 +0000)
source/mir/misc/WebdbImage.java [deleted file]
source/mircoders/entity/EntityImages.java
source/mircoders/media/MediaHandlerImages.java

diff --git a/source/mir/misc/WebdbImage.java b/source/mir/misc/WebdbImage.java
deleted file mode 100755 (executable)
index 1864bc8..0000000
+++ /dev/null
@@ -1,155 +0,0 @@
-/*
- * 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  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 mir.misc;
-
-/**
- * Title:
- * Description:
- * Copyright:    Copyright (c) 2002 Mir-coders
- * @author $Author: idfx $
- * @version $Id: WebdbImage.java,v 1.14 2003/04/21 12:42:52 idfx Exp $
- */
-
-import java.io.File;
-import java.io.IOException;
-import java.io.OutputStream;
-import java.util.Random;
-
-import javax.media.jai.InterpolationBilinear;
-import javax.media.jai.JAI;
-import javax.media.jai.ParameterBlockJAI;
-import javax.media.jai.PlanarImage;
-
-import mir.config.MirPropertiesConfiguration;
-
-import com.sun.media.jai.codec.FileSeekableStream;
-
-public class WebdbImage
-{
-
-  // default values for scaling
-  private int maxIconSize;
-  private int maxImageSize;
-
-  private int iconWidth;
-  private int iconHeight;
-
-  Random r = new Random();
-
-  // internal representation of the image
-  private PlanarImage planarImage;
-
-  // type of the image
-  private String _type;
-
-  private WebdbImage() {
-  }
-
-  // constructor
-  // takes a temporary file as a parameter
-  public WebdbImage(File f, String type) throws Exception {
-    // It has to be a FileSeekableStream cause the image conversion
-    // needs to seek backwards.
-    maxImageSize = MirPropertiesConfiguration.instance().getInt("Producer.Image.MaxSize");
-    maxIconSize = MirPropertiesConfiguration.instance().getInt("Producer.Image.MaxIconSize");
-
-    planarImage = JAI.create("stream", new FileSeekableStream(f));
-    _type = type;
-    scaleImage();
-  }
-
-  // acc3ssor-methods
-  // must be run after scaleIcon()
-  public int getIconWidth() throws IOException {
-    return iconWidth;
-  }
-
-  // must be run after scaleIcon()
-  public int getIconHeight() throws IOException {
-    return iconHeight;
-  }
-
-  public int getImageWidth() {
-    return (int) planarImage.getWidth();
-  }
-
-  public int getImageHeight() {
-    return (int) planarImage.getHeight();
-  }
-
-  public void setImage(OutputStream outStream) {
-    JAI.create("encode", planarImage, outStream, _type, null);
-  }
-
-  public void setIcon(OutputStream outStream) throws IOException {
-    scaleIcon(outStream);
-  }
-
-  private void scaleImage() throws java.io.IOException {
-    if (maxImageSize > 0 &&
-        (getImageHeight() > maxImageSize || getImageWidth() > maxImageSize)) {
-      float scale;
-      ParameterBlockJAI params = new ParameterBlockJAI("scale");
-      params.addSource(planarImage);
-      if (getImageHeight() > getImageWidth())
-        scale = (float) maxImageSize / (float) getImageHeight();
-      else
-        scale = (float) maxImageSize / (float) getImageWidth();
-
-      params.setParameter("xScale", scale);
-      params.setParameter("yScale", scale);
-      params.setParameter("xTrans", 0.0F);
-      params.setParameter("yTrans", 0.0F);
-      params.setParameter("interpolation", new InterpolationBilinear());
-      planarImage = JAI.create("scale", params);
-    }
-  }
-
-  private void scaleIcon(OutputStream outStream) throws java.io.IOException {
-    float scale;
-    ParameterBlockJAI params = new ParameterBlockJAI("scale");
-    params.addSource(planarImage);
-    if (getImageHeight() > getImageWidth())
-      scale = (float) maxIconSize / (float) getImageHeight();
-    else
-      scale = (float) maxIconSize / (float) getImageWidth();
-
-    params.setParameter("xScale", scale);
-    params.setParameter("yScale", scale);
-    params.setParameter("xTrans", 0.0F);
-    params.setParameter("yTrans", 0.0F);
-    params.setParameter("interpolation", new InterpolationBilinear());
-    PlanarImage temp = JAI.create("scale", params);
-    JAI.create("encode", temp, outStream, _type, null);
-    iconWidth = temp.getWidth();
-    iconHeight = temp.getHeight();
-  }
-
-}
\ No newline at end of file
index 02ecfcc..0074a08 100755 (executable)
  * 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  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.  
+ * the code of this program with  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.
  */
 
@@ -39,27 +39,32 @@ import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.sql.Statement;
 
+import org.postgresql.largeobject.BlobInputStream;
+import org.postgresql.largeobject.LargeObject;
+import org.postgresql.largeobject.LargeObjectManager;
+
 import mir.config.MirPropertiesConfiguration;
 import mir.log.LoggerWrapper;
 import mir.misc.FileUtil;
-import mir.misc.WebdbImage;
 import mir.storage.StorageObject;
 import mir.storage.StorageObjectFailure;
-
-import org.postgresql.largeobject.BlobInputStream;
-import org.postgresql.largeobject.LargeObject;
-import org.postgresql.largeobject.LargeObjectManager;
+import mircoders.media.ImageProcessor;
 
 /**
  * Diese Klasse enth?lt die Daten eines MetaObjekts
  *
  * @author RK, mh, mir-coders
- * @version $Id: EntityImages.java,v 1.20 2003/04/21 12:42:53 idfx Exp $
+ * @version $Id: EntityImages.java,v 1.21 2003/04/30 00:37:27 zapata Exp $
  */
 
 
 public class EntityImages extends EntityUploadedMedia
 {
+  private int maxImageSize = configuration.getInt("Producer.Image.MaxSize");
+  private int maxIconSize = configuration.getInt("Producer.Image.MaxIconSize");
+  private float minDescaleRatio = configuration.getFloat("Producer.Image.MinDescalePercentage")/100;
+  private int minDescaleReduction = configuration.getInt("Producer.Image.MinDescaleReduction");
+
 
   public EntityImages()
   {
@@ -127,53 +132,54 @@ public class EntityImages extends EntityUploadedMedia
     return img_in;
   }
 
-  public void setImage(InputStream in, String type)
-        throws StorageObjectFailure {
+  public void setImage(InputStream in, String type) throws StorageObjectFailure {
 
     if (in != null) {
+
       Connection con = null;
       PreparedStatement pstmt = null;
       File f = null;
       try {
         logger.debug("EntityImages.settimage :: making internal representation of image");
 
-        File tempDir = new File(MirPropertiesConfiguration.instance().getString("TempDir"));
-        f = File.createTempFile("mir", ".tmp", tempDir);
+        f = File.createTempFile("mir", ".tmp",
+                new File(MirPropertiesConfiguration.instance().getString("TempDir")));
         FileUtil.write(f, in);
-        WebdbImage webdbImage= new WebdbImage(f, type);
-        logger.debug("EntityImages.settimage :: made internal representation of image");
+        ImageProcessor processor = new ImageProcessor(f);
 
         con = theStorageObject.getPooledCon();
         con.setAutoCommit(false);
-        logger.debug("EntityImages.settimage :: trying to insert image");
-
-        // setting values
         LargeObjectManager lom;
-        java.sql.Connection jCon;
-        jCon = ((com.codestudio.sql.PoolManConnectionHandle)con)
-             .getNativeConnection();
+        java.sql.Connection connection;
+        connection = ((com.codestudio.sql.PoolManConnectionHandle)con).getNativeConnection();
 
-        lom = ((org.postgresql.Connection) jCon).getLargeObjectAPI();
+        lom = ((org.postgresql.Connection) connection).getLargeObjectAPI();
 
         int oidImage = lom.create();
-        int oidIcon = lom.create();
         LargeObject lobImage = lom.open(oidImage);
-        LargeObject lobIcon = lom.open(oidIcon);
-        webdbImage.setImage(lobImage.getOutputStream());
-        webdbImage.setIcon(lobIcon.getOutputStream());
+        processor.descaleImage(maxImageSize, minDescaleRatio, minDescaleReduction);
+        processor.writeScaledData(lobImage.getOutputStream(), type);
         lobImage.close();
+        setValueForProperty("img_height", new Integer(processor.getScaledHeight()).toString());
+        setValueForProperty("img_width", new Integer(processor.getScaledWidth()).toString());
+
+        int oidIcon = lom.create();
+        LargeObject lobIcon = lom.open(oidIcon);
+        processor.descaleImage(maxIconSize, minDescaleRatio, minDescaleReduction);
+        processor.writeScaledData(lobIcon.getOutputStream(), type);
         lobIcon.close();
 
-        setValueForProperty("img_height", new Integer(webdbImage.getImageHeight()).toString());
-        setValueForProperty("img_width", new Integer(webdbImage.getImageWidth()).toString());
-        setValueForProperty("icon_height", new Integer(webdbImage.getIconHeight()).toString());
-        setValueForProperty("icon_width", new Integer(webdbImage.getIconWidth()).toString());
+        setValueForProperty("icon_height", new Integer(processor.getScaledHeight()).toString());
+        setValueForProperty("icon_width", new Integer(processor.getScaledWidth()).toString());
+
         setValueForProperty("image_data", new Integer(oidImage).toString());
         setValueForProperty("icon_data", new Integer(oidIcon).toString());
         update();
-      } catch (Exception e) {
+      }
+      catch (Exception e) {
         throwStorageObjectFailure(e, "settimage :: setImage gescheitert: ");
-      } finally {
+      }
+      finally {
         try {
           if (con!=null)
             con.setAutoCommit(true);
index 3b0c3be..210ad37 100755 (executable)
  * 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  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.  
+ * the code of this program with  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;
@@ -58,7 +58,7 @@ import freemarker.template.SimpleList;
  *
  * @see mir.media.MirMedia
  * @author mh
- * @version $Id: MediaHandlerImages.java,v 1.22 2003/04/21 12:42:48 idfx Exp $
+ * @version $Id: MediaHandlerImages.java,v 1.23 2003/04/30 00:37:27 zapata Exp $
  */