Changes which allow images to be resized by Mir and saved in the filesystem if an...
[mir.git] / source / mir / media / image / ImageMagickImageProcessor.java
index ac851ad..74db390 100755 (executable)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2001, 2002 The Mir-coders group
+ * Copyright (C) 2005 The Mir-coders group
  *
  * This file is part of Mir.
  *
  * 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
+ * the code of this program with  any library licensed under the Apache Software License.
+ * 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.media.image;
 
 import mir.log.LoggerWrapper;
 import mir.media.MediaExc;
 import mir.media.MediaFailure;
 import mir.util.StreamCopier;
-import mir.util.ExecFunctions;
+import mir.util.ShellRoutines;
 import mir.config.MirPropertiesConfiguration;
 
-import java.io.*;
+import java.io.BufferedOutputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
 import java.util.StringTokenizer;
 
 
@@ -79,6 +83,8 @@ public class ImageMagickImageProcessor implements ImageProcessor {
      */
     int width;
     int height;
+    int fileSize;
+    
     /**
      * Image type as returned by identify %m : "PNG", "GIF", ...
      */
@@ -148,15 +154,30 @@ public class ImageMagickImageProcessor implements ImageProcessor {
      */
     public void readInfo() throws IOException {
       checkFile();
-      String infoString = ExecFunctions.execIntoString
+      String infoString = ShellRoutines.execIntoString
           (getImageMagickPath() +
-              "identify " + "-format \"%w %h %m %n \" " + 
+              "identify " + "-format \"%w %h %m %n %b \" " + 
               file.getAbsolutePath()); // extra space, for multiframe (animations)              
       StringTokenizer st = new StringTokenizer(infoString);
       width = Integer.parseInt(st.nextToken());
       height = Integer.parseInt(st.nextToken());
       type = st.nextToken();
       isAnimation = Integer.parseInt(st.nextToken()) > 1;
+      // yoss: different versions of ImageMagick produce different formatted output file sizes
+      // for example, Version: ImageMagick 6.2.4 (Ubuntu Dapper 6.06) produces a byte value, i.e. 67013
+      // Version: ImageMagick 6.0.6 (Debian Sarge) produces output like 67kb or 500mb.
+      // Trying to do an int.parse in Sarge fails for obvious reasons.
+      /*String sFileSize = st.nextToken();
+      if (sFileSize.endsWith("kb") || sFileSize.endsWith("Kb") || sFileSize.endsWith("KB") || sFileSize.endsWith("kB")){
+         sFileSize = sFileSize.substring(0, sFileSize.length() - 2);
+         fileSize = 1024 * Integer.parseInt(sFileSize);
+      } else if (sFileSize.endsWith("mb") || sFileSize.endsWith("Mb") || sFileSize.endsWith("MB") || sFileSize.endsWith("mB")){
+         sFileSize = sFileSize.substring(0, sFileSize.length() - 2);
+         fileSize = 1048576 * Integer.parseInt(sFileSize);       
+      } else {
+         fileSize = Integer.parseInt(sFileSize);
+      }*/
+      fileSize = (int)file.length();
     }
 
     public ImageFile scale(float aScalingFactor) throws IOException {
@@ -169,7 +190,7 @@ public class ImageMagickImageProcessor implements ImageProcessor {
           Float.toString(aScalingFactor * 100) + "% " +
           result.file.getAbsolutePath();
       logger.debug("ImageFile.scale:command:" + command);
-      ExecFunctions.simpleExec(command);
+      ShellRoutines.simpleExec(command);
       result.readInfo();
       return result;
     }
@@ -305,6 +326,14 @@ public class ImageMagickImageProcessor implements ImageProcessor {
   public int getHeight() {
     return sourceImage.height;
   }
+  
+  public int getSourceFileSize() {
+         return sourceImage.fileSize;
+  }
+  
+  public int getScaledFileSize() {
+         return scaledImage.fileSize;
+  }
 
   public int getScaledWidth() {
     return scaledImage.width;
@@ -332,7 +361,7 @@ public class ImageMagickImageProcessor implements ImageProcessor {
           scaledImage.file.getAbsolutePath() + frame + " " +
           anImageType + ":" + temp.getAbsolutePath();
       logger.debug("writeScaledData command:" + command);
-      ExecFunctions.simpleExec(command);
+      ShellRoutines.simpleExec(command);
       // copy temp file into stream
       StreamCopier.copy(new FileInputStream(temp), aStream);
       temp.delete();
@@ -360,11 +389,15 @@ public class ImageMagickImageProcessor implements ImageProcessor {
           stream.close();
         }
         catch (Throwable t) {
+               logger.debug("Unable to close stream when writing scaled data.");
         }
       }
     }
     catch (FileNotFoundException f) {
       throw new MediaFailure(f);
     }
+    catch (Exception e) {
+       logger.debug("Exception caught while trying to write scaled data: " + e.toString());
+    }
   }
 }