[gimp] libgimp: fix gimp_drawable_get_format() to honor the drawable's space



commit dfe3e23622343f59d3201ab266cbe02afe813d8d
Author: Michael Natterer <mitch gimp org>
Date:   Tue Jan 1 18:24:35 2019 +0100

    libgimp: fix gimp_drawable_get_format() to honor the drawable's space
    
    The raw PDB wrapper _gimp_drawable_get_format() only transfers the
    format's encoding, so we need to add the space from the image's color
    profile.
    
    Also fix handling of indexed formats: remove our own indexed format
    cache and rely on babl_new_palette_with_space() to return the same
    format for any (encoding, space) combination.
    
    Also update the PDB docs to reflect that most magic is happening in
    the libgimp C wrapper.

 app/pdb/drawable-cmds.c    |  8 +++---
 libgimp/gimpdrawable.c     | 67 ++++++++++++++++++++++++++--------------------
 libgimp/gimpdrawable_pdb.c |  3 +++
 pdb/groups/drawable.pdb    | 14 +++++++---
 4 files changed, 57 insertions(+), 35 deletions(-)
---
diff --git a/app/pdb/drawable-cmds.c b/app/pdb/drawable-cmds.c
index fe3de6d11f..861f3bcbdd 100644
--- a/app/pdb/drawable-cmds.c
+++ b/app/pdb/drawable-cmds.c
@@ -74,8 +74,9 @@ drawable_get_format_invoker (GimpProcedure         *procedure,
       if (gimp->plug_in_manager->current_plug_in)
         gimp_plug_in_enable_precision (gimp->plug_in_manager->current_plug_in);
 
-      /* EEK SPACE: this needs more code on the libgimp side, we currently
-       * lose the space
+      /* this only transfers the encoding, losing the space, see the
+       * code in libgimp/gimpdrawable.c which reconstructs the actual
+       * format in the plug-in process
        */
       format = g_strdup (babl_format_get_encoding (gimp_drawable_get_format (drawable)));
     }
@@ -971,7 +972,8 @@ register_drawable_procs (GimpPDB *pdb)
   gimp_procedure_set_static_strings (procedure,
                                      "gimp-drawable-get-format",
                                      "Returns the drawable's Babl format",
-                                     "This procedure returns the drawable's Babl format.",
+                                     "This procedure returns the drawable's Babl format.\n"
+                                     "Note that the actual PDB procedure only transfers the format's 
encoding. In order to get to the real format, the libbgimp C wrapper must be used.",
                                      "Michael Natterer <mitch gimp org>",
                                      "Michael Natterer",
                                      "2012",
diff --git a/libgimp/gimpdrawable.c b/libgimp/gimpdrawable.c
index 88fffd47b9..57557a0b95 100644
--- a/libgimp/gimpdrawable.c
+++ b/libgimp/gimpdrawable.c
@@ -418,59 +418,68 @@ gimp_drawable_get_shadow_buffer (gint32 drawable_ID)
 const Babl *
 gimp_drawable_get_format (gint32 drawable_ID)
 {
-  static GHashTable *palette_formats = NULL;
   const Babl *format     = NULL;
   gchar      *format_str = _gimp_drawable_get_format (drawable_ID);
 
-  /* EEK SPACE _gimp_drawable_get_format() only returns the encoding,
-   * needs to create the actual space from the image's profile
+  /* _gimp_drawable_get_format() only returns the encoding, so we
+   * create the actual space from the image's profile
    */
 
   if (format_str)
     {
+      gint32            image_ID = gimp_item_get_image (drawable_ID);
+      GimpColorProfile *profile;
+      const Babl       *space = NULL;
+
+      profile = gimp_image_get_color_profile (image_ID);
+
+      if (profile)
+        {
+          GError *error = NULL;
+
+          space =
+            gimp_color_profile_get_space (profile,
+                                          GIMP_COLOR_RENDERING_INTENT_RELATIVE_COLORIMETRIC,
+                                          &error);
+          if (! space)
+            {
+              g_printerr ("%s: failed to create Babl space from profile: %s\n",
+                          G_STRFUNC, error->message);
+              g_clear_error (&error);
+            }
+
+          g_object_unref (profile);
+        }
+
       if (gimp_drawable_is_indexed (drawable_ID))
         {
-          gint32      image_ID = gimp_item_get_image (drawable_ID);
+          const Babl *palette;
+          const Babl *palette_alpha;
           guchar     *colormap;
           gint        n_colors;
 
-          colormap = gimp_image_get_colormap (image_ID, &n_colors);
-
-          if (!palette_formats)
-            palette_formats = g_hash_table_new (g_str_hash, g_str_equal);
+          babl_new_palette_with_space (format_str, space,
+                                       &palette, &palette_alpha);
 
-          format = g_hash_table_lookup (palette_formats, format_str);
+          if (gimp_drawable_has_alpha (drawable_ID))
+            format = palette_alpha;
+          else
+            format = palette;
 
-          if (!format)
-            {
-              const Babl *palette;
-              const Babl *palette_alpha;
-
-              babl_new_palette (format_str, &palette, &palette_alpha);
-              g_hash_table_insert (palette_formats,
-                                   (gpointer) babl_get_name (palette),
-                                   (gpointer) palette);
-              g_hash_table_insert (palette_formats,
-                                   (gpointer) babl_get_name (palette_alpha),
-                                   (gpointer) palette_alpha);
-
-              if (gimp_drawable_has_alpha (drawable_ID))
-                format = palette_alpha;
-              else
-                format = palette;
-            }
+          colormap = gimp_image_get_colormap (image_ID, &n_colors);
 
           if (colormap)
             {
               babl_palette_set_palette (format,
-                                        babl_format ("R'G'B' u8"),
+                                        babl_format_with_space ("R'G'B' u8",
+                                                                space),
                                         colormap, n_colors);
               g_free (colormap);
             }
         }
       else
         {
-          format = babl_format (format_str);
+          format = babl_format_with_space (format_str, space);
         }
 
       g_free (format_str);
diff --git a/libgimp/gimpdrawable_pdb.c b/libgimp/gimpdrawable_pdb.c
index 818a27e90d..4ebba8f7e7 100644
--- a/libgimp/gimpdrawable_pdb.c
+++ b/libgimp/gimpdrawable_pdb.c
@@ -43,6 +43,9 @@
  * Returns the drawable's Babl format
  *
  * This procedure returns the drawable's Babl format.
+ * Note that the actual PDB procedure only transfers the format's
+ * encoding. In order to get to the real format, the libbgimp C wrapper
+ * must be used.
  *
  * Returns: The drawable's Babl format.
  *
diff --git a/pdb/groups/drawable.pdb b/pdb/groups/drawable.pdb
index 860d4b7f86..a16d943c87 100644
--- a/pdb/groups/drawable.pdb
+++ b/pdb/groups/drawable.pdb
@@ -270,7 +270,14 @@ CODE
 
 sub drawable_get_format {
     $blurb = "Returns the drawable's Babl format";
-    $help  = "This procedure returns the drawable's Babl format.";
+
+    $help = <<'HELP';
+This procedure returns the drawable's Babl format.
+
+Note that the actual PDB procedure only transfers the format's
+encoding. In order to get to the real format, the libbgimp C wrapper
+must be used.
+HELP
 
     &mitch_pdb_misc('2012', '2.10');
 
@@ -290,8 +297,9 @@ sub drawable_get_format {
   if (gimp->plug_in_manager->current_plug_in)
     gimp_plug_in_enable_precision (gimp->plug_in_manager->current_plug_in);
 
-  /* EEK SPACE: this needs more code on the libgimp side, we currently
-   * lose the space
+  /* this only transfers the encoding, losing the space, see the
+   * code in libgimp/gimpdrawable.c which reconstructs the actual
+   * format in the plug-in process
    */
   format = g_strdup (babl_format_get_encoding (gimp_drawable_get_format (drawable)));
 }


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