[gegl] bin: Lookup ouput file type dynamically through extension



commit 7b6723def82f34b7c25af81173f41086ca3d4be2
Author: Martin Nordholts <martinn src gnome org>
Date:   Wed May 12 21:10:44 2010 +0200

    bin: Lookup ouput file type dynamically through extension
    
    Look up handler for a given extension based on registrations instead
    of having it hardcoded. I stole a function from GIMP called
    file_utils_get_ext_start() (which I wrote myself...) to help with
    this.

 bin/gegl-options.c |   32 +++-----------------------------
 bin/gegl-options.h |    3 +--
 bin/gegl.c         |   52 ++++++++++++++++++++++++++++++++++++++--------------
 3 files changed, 42 insertions(+), 45 deletions(-)
---
diff --git a/bin/gegl-options.c b/bin/gegl-options.c
index 93b617b..8cc954d 100644
--- a/bin/gegl-options.c
+++ b/bin/gegl-options.c
@@ -59,9 +59,8 @@ usage (char *application_name)
 "\n"
 "     --dot       output a graphviz graph description\n"
 ""
-"     --output    output generated image to named file\n"
-"     -o          (file is saved in PNG or PPM format\n"
-"                  depending on the file ending.)\n"
+"     --output    output generated image to named file, type based on\n"
+"     -o          extension.\n"
 "\n"
 "     -p          (increment frame counters of various elements when\n"
 "                  processing is done.)\n"
@@ -233,33 +232,8 @@ parse_args (int    argc,
 
         else if (match ("--output") ||
                  match ("-o")) {
-            const char *trigramstart;
-            int   length;
-
             get_string_forced (o->output);
-
-            length       = strlen (o->output);
-            trigramstart = o->output;
-
-            /* position the pointer at the start of the trigram or
-             * if the string is not long enough, at the end of the string
-             */
-            trigramstart += (length > 3 ? length - 3 : length);
-
-            if (strcmp (trigramstart, "png") == 0 ||
-                strcmp (trigramstart, "PNG") == 0 ) {
-
-                o->mode = GEGL_RUN_MODE_PNG;
-            } else if (strcmp (trigramstart, "ppm") == 0 ||
-                       strcmp (trigramstart, "PPM") == 0) {
-
-                o->mode = GEGL_RUN_MODE_PPM;
-            } else {
-                fprintf (stderr,
-                         "Output file extension not found (%s), using png\n",
-                         o->output);
-                o->mode = GEGL_RUN_MODE_PNG;
-            }
+            o->mode = GEGL_RUN_MODE_OUTPUT;
         }
 
         else if (match ("-X")) {
diff --git a/bin/gegl-options.h b/bin/gegl-options.h
index 91c9267..0c572d4 100644
--- a/bin/gegl-options.h
+++ b/bin/gegl-options.h
@@ -26,8 +26,7 @@ typedef enum
   GEGL_RUN_MODE_HELP,
   GEGL_RUN_MODE_DOT,
   GEGL_RUN_MODE_EDITOR,
-  GEGL_RUN_MODE_PNG,
-  GEGL_RUN_MODE_PPM,
+  GEGL_RUN_MODE_OUTPUT,
   GEGL_RUN_MODE_XML
 } GeglRunMode;
 
diff --git a/bin/gegl.c b/bin/gegl.c
index 532ed78..5b8841e 100644
--- a/bin/gegl.c
+++ b/bin/gegl.c
@@ -33,6 +33,7 @@
 #include "gegl-path-spiro.h"
 #endif
 #include "gegl-path-smooth.h"
+#include "operation/gegl-extension-handler.h"
 
 #ifdef G_OS_WIN32
 #include <direct.h>
@@ -75,6 +76,38 @@ static gboolean file_is_gegl_xml (const gchar *path)
   return FALSE;
 }
 
+/**
+ * file_utils_get_ext_start:
+ * @uri:
+ *
+ * Returns the position of the extension (including .) for an URI. If
+ * there is no extension the returned position is right after the
+ * string, at the terminating NULL character.
+ *
+ * Returns:
+ **/
+static const gchar *
+file_utils_get_ext_start (const gchar *uri)
+{
+  const gchar *ext = NULL;
+  int uri_len = strlen (uri);
+  int search_len = 0;
+
+  if (g_strrstr (uri, ".gz"))
+    search_len = uri_len - 3;
+  else if (g_strrstr (uri, ".bz2"))
+    search_len = uri_len - 4;
+  else
+    search_len = uri_len;
+
+  ext = g_strrstr_len (uri, search_len, ".");
+
+  if (! ext)
+    ext = uri + uri_len;
+
+  return ext;
+}
+
 gint
 main (gint    argc,
       gchar **argv)
@@ -241,28 +274,19 @@ main (gint    argc,
 	return 0;
         break;
 #endif
-      case GEGL_RUN_MODE_PNG:
+      case GEGL_RUN_MODE_OUTPUT:
         {
+          const gchar *ext = file_utils_get_ext_start (o->output);
+          const gchar *handler = gegl_extension_handler_get_saver (ext);
           GeglNode *output = gegl_node_new_child (gegl,
-						  "operation", "gegl:png-save",
+						  "operation", handler,
 						  "path", o->output,
 						  NULL);
           gegl_node_connect_from (output, "input", gegl_node_get_output_proxy (gegl, "output"), "output");
           gegl_node_process (output);
           g_object_unref (output);
         }
-        break;
-      case GEGL_RUN_MODE_PPM:
-        {
-          GeglNode *output = gegl_node_new_child (gegl,
-						  "operation", "gegl:ppm-save",
-						  "path", o->output,
-						  NULL);
-          gegl_node_connect_from (output, "input", gegl_node_get_output_proxy (gegl, "output"), "output");
-          gegl_node_process (output);
-          g_object_unref (output);
-        }
-        break;
+        break;  
       case GEGL_RUN_MODE_HELP:
         break;
       default:



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]