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 c62753c..74db390 100755 (executable)
@@ -33,7 +33,15 @@ import mir.util.StreamCopier;
 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;
 
 
@@ -75,6 +83,8 @@ public class ImageMagickImageProcessor implements ImageProcessor {
      */
     int width;
     int height;
+    int fileSize;
+    
     /**
      * Image type as returned by identify %m : "PNG", "GIF", ...
      */
@@ -146,13 +156,28 @@ public class ImageMagickImageProcessor implements ImageProcessor {
       checkFile();
       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 {
@@ -301,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;
@@ -356,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());
+    }
   }
 }