[babl/wip/Jehan/babl-cli-tool: 2/3] bin: convert from Babl formats with space.




commit 2c0eeb52cd5b53706a3eca30de0526a11e70ff9d
Author: Jehan <jehan girinstud io>
Date:   Wed Jul 20 22:41:50 2022 +0200

    bin: convert from Babl formats with space.

 bin/babl.c | 113 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 107 insertions(+), 6 deletions(-)
---
diff --git a/bin/babl.c b/bin/babl.c
index 8bd306a3d..a56427997 100644
--- a/bin/babl.c
+++ b/bin/babl.c
@@ -16,6 +16,7 @@
  * <https://www.gnu.org/licenses/>.
  */
 
+#include <errno.h>
 #include <stdint.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -23,19 +24,27 @@
 
 #include <babl/babl.h>
 
+
+static const Babl * babl_cli_get_space (const char *path);
+
+
 int
 main (int    argc,
       char **argv)
 {
   const Babl *from_format;
+  const Babl *from_space       = NULL;
   const Babl *to_format;
+  const Babl *to_space         = NULL;
   const Babl *fish;
-  const char *from     = "R'G'B' u8";
-  const char *to       = "R'G'B' u8";
+  const char *from             = "R'G'B' u8";
+  const char *to               = "R'G'B' u8";
   char       *source;
   char       *dest;
-  int         set_from = 0;
-  int         set_to   = 0;
+  int         set_from         = 0;
+  int         set_to           = 0;
+  int         set_from_profile = 0;
+  int         set_to_profile   = 0;
   int         n_components;
   int         data_index;
   int         c;
@@ -66,6 +75,27 @@ main (int    argc,
               return 1;
             }
         }
+      else if (set_from_profile)
+        {
+          set_from_profile = 0;
+          from_space = babl_cli_get_space (argv[i]);
+
+          if (! from_space)
+            return 6;
+        }
+      else if (set_to_profile)
+        {
+          set_to_profile = 0;
+          if (! babl_format_exists (to))
+            {
+              fprintf (stderr, "babl: unknown format: %s\n", to);
+              return 1;
+            }
+          to_space = babl_cli_get_space (argv[i]);
+
+          if (! to_space)
+            return 6;
+        }
       else if (strcmp (argv[i], "--from") == 0 ||
                strcmp (argv[i], "-f") == 0)
         {
@@ -76,14 +106,24 @@ main (int    argc,
         {
           set_to = 1;
         }
+      else if (strcmp (argv[i], "--input-profile") == 0 ||
+               strcmp (argv[i], "-i") == 0)
+        {
+          set_from_profile = 1;
+        }
+      else if (strcmp (argv[i], "--output-profile") == 0 ||
+               strcmp (argv[i], "-o") == 0)
+        {
+          set_to_profile = 1;
+        }
     }
 
-  from_format  = babl_format (from);
+  from_format  = babl_format_with_space (from, from_space);
   n_components = babl_format_get_n_components (from_format);
   source       = malloc (babl_format_get_bytes_per_pixel (from_format));
   data_index   = 0;
 
-  to_format    = babl_format (to);
+  to_format    = babl_format_with_space (to, to_space);
   dest         = malloc (babl_format_get_bytes_per_pixel (from_format));
 
   /* Re-looping through arguments, to be more flexible with argument orders.
@@ -102,6 +142,16 @@ main (int    argc,
           set_to = 0;
           /* Pass. */
         }
+      else if (set_from_profile)
+        {
+          set_from_profile = 0;
+          /* Pass. */
+        }
+      else if (set_to_profile)
+        {
+          set_to_profile = 0;
+          /* Pass. */
+        }
       else if (strcmp (argv[i], "--from") == 0 ||
                strcmp (argv[i], "-f") == 0)
         {
@@ -112,6 +162,16 @@ main (int    argc,
         {
           set_to = 1;
         }
+      else if (strcmp (argv[i], "--input-profile") == 0 ||
+               strcmp (argv[i], "-i") == 0)
+        {
+          set_from_profile = 1;
+        }
+      else if (strcmp (argv[i], "--output-profile") == 0 ||
+               strcmp (argv[i], "-o") == 0)
+        {
+          set_to_profile = 1;
+        }
       else
         {
           const Babl *arg_type;
@@ -259,3 +319,44 @@ main (int    argc,
 
   return 0;
 }
+
+static const Babl *
+babl_cli_get_space (const char *path)
+{
+  const Babl *space;
+  FILE       *f;
+  char       *icc_data;
+  long        icc_length;
+  const char *error = NULL;
+
+  f = fopen (path, "r");
+
+  if (f == NULL)
+    {
+      fprintf (stderr, "babl: failed to open '%s': %s\n",
+               path, strerror (errno));
+      return NULL;
+    }
+
+  fseek (f, 0, SEEK_END);
+  icc_length = ftell (f);
+  fseek (f, 0, SEEK_SET);
+
+  icc_data = malloc (icc_length);
+  fread (icc_data, icc_length, 1, f);
+
+  fclose (f);
+
+  space = babl_space_from_icc (icc_data, icc_length,
+                               BABL_ICC_INTENT_RELATIVE_COLORIMETRIC,
+                               &error);
+
+  if (space == NULL)
+    {
+      fprintf (stderr, "babl: failed to load space from '%s': %s\n",
+               path, error);
+      return NULL;
+    }
+
+  return space;
+}


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