Update of /var/cvs/src/org/mmbase/util/images
In directory james.mmbase.org:/tmp/cvs-serv13316

Modified Files:
      Tag: MMBase-1_8
        ImageMagickImageConverter.java 
Log Message:
  MMB-1725


See also: http://cvs.mmbase.org/viewcvs/src/org/mmbase/util/images
See also: http://www.mmbase.org/jira/browse/MMB-1725


Index: ImageMagickImageConverter.java
===================================================================
RCS file: 
/var/cvs/src/org/mmbase/util/images/Attic/ImageMagickImageConverter.java,v
retrieving revision 1.4.2.4
retrieving revision 1.4.2.5
diff -u -b -r1.4.2.4 -r1.4.2.5
--- ImageMagickImageConverter.java      14 Jul 2008 12:30:14 -0000      1.4.2.4
+++ ImageMagickImageConverter.java      29 Sep 2008 15:27:30 -0000      1.4.2.5
@@ -27,12 +27,13 @@
  * @author Michiel Meeuwissen
  * @author Nico Klasens
  * @author Jaco de Groot
- * @version $Id: ImageMagickImageConverter.java,v 1.4.2.4 2008/07/14 12:30:14 
nklasens Exp $
+ * @version $Id: ImageMagickImageConverter.java,v 1.4.2.5 2008/09/29 15:27:30 
michiel Exp $
  */
 public class ImageMagickImageConverter implements ImageConverter {
     private static final Logger log = 
Logging.getLoggerInstance(ImageMagickImageConverter.class);
 
     private static final Pattern IM_VERSION_PATTERN = 
Pattern.compile("(?is).*?\\s(\\d+)\\.(\\d+)\\.(\\d+)\\s.*");
+    private static final Pattern IM_FORMAT_PATTERN  = 
Pattern.compile("(?is)\\s*([A-Z0-9]+)\\*?\\s+[A-Z0-9]+\\s*[r\\-]w[\\+\\-]\\s+.*");
 
     private int imVersionMajor = 5;
     private int imVersionMinor = 5;
@@ -52,6 +53,10 @@
 
     protected List excludeFormats = new ArrayList();
 
+    private static final Set validFormats = new TreeSet();
+
+
+
     /**
      * This function initializes this class
      * @param params a <code>Map</code> of <code>String</code>s containing 
information, this should contain the key's
@@ -120,6 +125,7 @@
         // TODO: research how we tell convert, that is should use the 
System.getProperty(); with respective the value's 'java.io.tmpdir', 'user.dir'
         //       this, since convert writes at this moment inside the 
'user.dir'(working dir), which isnt writeable all the time.
 
+        {
         ByteArrayOutputStream errorStream = new ByteArrayOutputStream();
         ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
         try {
@@ -147,6 +153,34 @@
             log.info("Supposing ImageMagick version " + imVersionMajor + "." + 
imVersionMinor + "." + imVersionPatch);
 
         }
+        }
+        {
+            ByteArrayOutputStream errorStream = new ByteArrayOutputStream();
+            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
+            try {
+                CommandLauncher launcher = new CommandLauncher("ConvertImage");
+                log.debug("Starting convert");
+                List cmd = new ArrayList();
+                cmd.add("-list");
+                cmd.add("format");
+                launcher.execute(converterPath, (String[]) cmd.toArray(new 
String[] {}));
+                launcher.waitAndRead(outputStream, errorStream);
+            } catch (ProcessException e) {
+                log.error("Convert -list format failed. " + converterPath + " 
(" + e.toString() + ") conv.root='" + converterRoot
+                          + "' conv.command='" + converterCommand + "'", e);
+            }
+
+            String[] imOutput = outputStream.toString().split("\n");
+            for (int i = 0; i < imOutput.length; i++) {
+                String imLine = imOutput[i];
+                Matcher m = IM_FORMAT_PATTERN.matcher(imLine);
+                if (m.matches()) {
+                    String format = m.group(1);
+                    validFormats.add(format.toUpperCase());
+                }
+            }
+            log.info("Found ImageMagick supported formats " + validFormats);
+        }
 
         // Cant do more checking then this, i think....
         tmp = (String) params.get("ImageConvert.ColorizeHexScale");
@@ -491,6 +525,10 @@
         return modCmd;
     }
 
+
+
+
+
     /**
      * Does the actual conversion.
      *
@@ -505,7 +543,11 @@
         if (pict != null && pict.length > 0) {
             cmd.add(0, "-");
             cmd.add(0, converterPath);
-            cmd.add(format+ ":-");
+            if (! validFormats.contains(format.toUpperCase())) {
+                log.warn("format " + format + "' is not supported (" + 
validFormats + ") falling back to " + Factory.getDefaultImageFormat());
+                format = Factory.getDefaultImageFormat();
+            }
+            cmd.add(format + ":-");
 
             String command = cmd.toString(); // only for debugging.
             log.debug("Converting image(#" + pict.length + " bytes)  to '" + 
format + "' ('" + command + "')");
_______________________________________________
Cvs mailing list
[email protected]
http://lists.mmbase.org/mailman/listinfo/cvs

Reply via email to