[gimp] app, libgimpcolor: refactor GimpColorManaged::get_color_profile()



commit b45d31072bf3dad4acce4e9702a6ab0efd69e3c6
Author: Michael Natterer <mitch gimp org>
Date:   Thu Sep 3 01:36:02 2015 +0200

    app, libgimpcolor: refactor GimpColorManaged::get_color_profile()
    
    to not return a reference that has to be dropped. Also allow NULL to
    be returned if the managed cannot have a profile. If it can have one,
    get_color_profile() still always returns a profile (either the
    assigned one, or a generated built-in one).

 app/core/gimp-edit.c                     |    9 ++++-----
 app/core/gimpimage-color-profile.c       |    9 ++-------
 app/core/gimpimage.c                     |    4 ++--
 app/core/gimplayer-new.c                 |   23 +++++++++--------------
 app/core/gimplayer.c                     |   20 +++++++++-----------
 app/dialogs/color-profile-dialog.c       |    2 --
 app/display/gimpdisplayshell-profile.c   |    3 +++
 app/display/gimpdisplayshell-title.c     |   21 +++++++++++++--------
 app/display/gimpdisplayshell.c           |    3 +--
 app/pdb/image-color-profile-cmds.c       |   16 +++++++++-------
 app/pdb/layer-cmds.c                     |    2 --
 app/pdb/plug-in-compat-cmds.c            |   12 ++++++------
 app/widgets/gimpimageprofileview.c       |    2 --
 libgimp/gimpimagecolorprofile_pdb.c      |    5 ++---
 libgimpcolor/gimpcolormanaged.c          |    3 +--
 modules/display-filter-lcms.c            |   20 ++++++++++++--------
 tools/pdbgen/pdb/image_color_profile.pdb |   19 ++++++++++---------
 tools/pdbgen/pdb/layer.pdb               |    2 --
 tools/pdbgen/pdb/plug_in_compat.pdb      |   12 ++++++------
 19 files changed, 89 insertions(+), 98 deletions(-)
---
diff --git a/app/core/gimp-edit.c b/app/core/gimp-edit.c
index c8704c8..e9230f9 100644
--- a/app/core/gimp-edit.c
+++ b/app/core/gimp-edit.c
@@ -609,14 +609,13 @@ gimp_edit_extract (GimpImage     *image,
                                                  offset_x, offset_y, FALSE);
       g_object_unref (buffer);
 
-      if (GIMP_IS_LAYER (pickable) ||
-          GIMP_IS_IMAGE (pickable))
+      if (GIMP_IS_COLOR_MANAGED (pickable))
         {
           GimpColorProfile *profile =
-            gimp_color_managed_get_color_profile (GIMP_COLOR_MANAGED (image));
+            gimp_color_managed_get_color_profile (GIMP_COLOR_MANAGED (pickable));
 
-          gimp_buffer_set_color_profile (gimp_buffer, profile);
-          g_object_unref (profile);
+          if (profile)
+            gimp_buffer_set_color_profile (gimp_buffer, profile);
         }
 
       return gimp_buffer;
diff --git a/app/core/gimpimage-color-profile.c b/app/core/gimpimage-color-profile.c
index d9fc273..2119880 100644
--- a/app/core/gimpimage-color-profile.c
+++ b/app/core/gimpimage-color-profile.c
@@ -358,11 +358,8 @@ gimp_image_convert_color_profile (GimpImage                *image,
 
   src_profile = gimp_color_managed_get_color_profile (GIMP_COLOR_MANAGED (image));
 
-  if (gimp_color_profile_is_equal (src_profile, dest_profile))
-    {
-      g_object_unref (src_profile);
-      return TRUE;
-    }
+  if (! src_profile || gimp_color_profile_is_equal (src_profile, dest_profile))
+    return TRUE;
 
   if (progress)
     gimp_progress_start (progress, FALSE,
@@ -402,8 +399,6 @@ gimp_image_convert_color_profile (GimpImage                *image,
   if (progress)
     gimp_progress_end (progress);
 
-  g_object_unref (src_profile);
-
   return TRUE;
 }
 
diff --git a/app/core/gimpimage.c b/app/core/gimpimage.c
index d46de3d..a467e23 100644
--- a/app/core/gimpimage.c
+++ b/app/core/gimpimage.c
@@ -1390,10 +1390,10 @@ gimp_image_color_managed_get_color_profile (GimpColorManaged *managed)
 
   profile = gimp_image_get_color_profile (image);
 
-  if (! profile)
+  if (! profile && gimp_image_get_base_type (image) != GIMP_INDEXED)
     profile = gimp_image_get_builtin_color_profile (image);
 
-  return g_object_ref (profile);
+  return profile;
 }
 
 static void
diff --git a/app/core/gimplayer-new.c b/app/core/gimplayer-new.c
index 208bf57..99ea81b 100644
--- a/app/core/gimplayer-new.c
+++ b/app/core/gimplayer-new.c
@@ -234,28 +234,23 @@ gimp_layer_new_convert_buffer (GimpLayer         *layer,
   GeglBuffer       *dest_buffer = gimp_drawable_get_buffer (drawable);
   GimpColorProfile *dest_profile;
 
-  if (! src_profile)
-    {
-      gegl_buffer_copy (src_buffer, NULL, GEGL_ABYSS_NONE, dest_buffer, NULL);
-      return;
-    }
+  dest_profile =
+    gimp_color_managed_get_color_profile (GIMP_COLOR_MANAGED (layer));
 
-  /*  FIXME: this is the wrong check, need something like file import
-   *  conversion config
-   */
-  if (config->mode == GIMP_COLOR_MANAGEMENT_OFF)
+  if (! src_profile  ||
+      ! dest_profile ||
+
+      /*  FIXME: this is the wrong check, need something like file import
+       *  conversion config
+       */
+      config->mode == GIMP_COLOR_MANAGEMENT_OFF)
     {
       gegl_buffer_copy (src_buffer, NULL, GEGL_ABYSS_NONE, dest_buffer, NULL);
       return;
     }
 
-  dest_profile =
-    gimp_color_managed_get_color_profile (GIMP_COLOR_MANAGED (image));
-
   gimp_gegl_convert_color_profile (src_buffer,  NULL, src_profile,
                                    dest_buffer, NULL, dest_profile,
                                    GIMP_COLOR_RENDERING_INTENT_PERCEPTUAL,
                                    TRUE);
-
-  g_object_unref (dest_profile);
 }
diff --git a/app/core/gimplayer.c b/app/core/gimplayer.c
index aa30741..ab728fe 100644
--- a/app/core/gimplayer.c
+++ b/app/core/gimplayer.c
@@ -1052,9 +1052,11 @@ gimp_layer_convert_type (GimpDrawable      *drawable,
                          gboolean           convert_profile,
                          gboolean           push_undo)
 {
-  GimpLayer  *layer = GIMP_LAYER (drawable);
-  GeglBuffer *src_buffer;
-  GeglBuffer *dest_buffer;
+  GimpLayer        *layer = GIMP_LAYER (drawable);
+  GeglBuffer       *src_buffer;
+  GeglBuffer       *dest_buffer;
+  GimpColorProfile *src_profile  = NULL;
+  GimpColorProfile *dest_profile = NULL;
 
   if (layer_dither_type == 0)
     {
@@ -1086,23 +1088,19 @@ gimp_layer_convert_type (GimpDrawable      *drawable,
 
   if (convert_profile)
     {
-      GimpImage        *src_image = gimp_item_get_image (GIMP_ITEM (layer));
-      GimpColorProfile *src_profile;
-      GimpColorProfile *dest_profile;
-
       src_profile =
-        gimp_color_managed_get_color_profile (GIMP_COLOR_MANAGED (src_image));
+        gimp_color_managed_get_color_profile (GIMP_COLOR_MANAGED (layer));
 
       dest_profile =
         gimp_color_managed_get_color_profile (GIMP_COLOR_MANAGED (dest_image));
+    }
 
+  if (src_profile && dest_profile)
+    {
       gimp_gegl_convert_color_profile (src_buffer,  NULL, src_profile,
                                        dest_buffer, NULL, dest_profile,
                                        GIMP_COLOR_RENDERING_INTENT_PERCEPTUAL,
                                        TRUE);
-
-      g_object_unref (src_profile);
-      g_object_unref (dest_profile);
     }
   else
     {
diff --git a/app/dialogs/color-profile-dialog.c b/app/dialogs/color-profile-dialog.c
index 748ffdf..9588fbc 100644
--- a/app/dialogs/color-profile-dialog.c
+++ b/app/dialogs/color-profile-dialog.c
@@ -221,8 +221,6 @@ color_profile_dialog_new (GimpImage    *image,
   gtk_container_add (GTK_CONTAINER (frame), label);
   gtk_widget_show (label);
 
-  g_object_unref (src_profile);
-
   frame = gimp_frame_new (convert ? _("Convert to") : _("Assign"));
   gtk_box_pack_start (GTK_BOX (dialog->main_vbox), frame, FALSE, FALSE, 0);
   gtk_widget_show (frame);
diff --git a/app/display/gimpdisplayshell-profile.c b/app/display/gimpdisplayshell-profile.c
index cf12f9f..cda711b 100644
--- a/app/display/gimpdisplayshell-profile.c
+++ b/app/display/gimpdisplayshell-profile.c
@@ -88,6 +88,9 @@ gimp_display_shell_profile_update (GimpDisplayShell *shell)
 
   src_profile = gimp_color_managed_get_color_profile (GIMP_COLOR_MANAGED (shell));
 
+  if (! src_profile)
+    return;
+
   src_format = gimp_babl_format (GIMP_RGB,
                                  gimp_image_get_precision (image),
                                  TRUE);
diff --git a/app/display/gimpdisplayshell-title.c b/app/display/gimpdisplayshell-title.c
index 8f6e149..6073a93 100644
--- a/app/display/gimpdisplayshell-title.c
+++ b/app/display/gimpdisplayshell-title.c
@@ -366,6 +366,7 @@ gimp_display_shell_format_title (GimpDisplayShell *shell,
                   break;
                 }
               /* else fallthru */
+
             case 'w': /* width in pixels */
               i += print (title, title_len, i, "%d",
                           gimp_image_get_width (image));
@@ -388,6 +389,7 @@ gimp_display_shell_format_title (GimpDisplayShell *shell,
                   break;
                 }
               /* else fallthru */
+
             case 'h': /* height in pixels */
               i += print (title, title_len, i, "%d",
                           gimp_image_get_height (image));
@@ -421,6 +423,7 @@ gimp_display_shell_format_title (GimpDisplayShell *shell,
                   break;
                 }
               /* else fallthru */
+
             case 'x': /* drawable width in pixels */
               if (drawable)
                 i += print (title, title_len, i, "%d",
@@ -445,6 +448,7 @@ gimp_display_shell_format_title (GimpDisplayShell *shell,
                   break;
                 }
               /* else fallthru */
+
             case 'y': /* drawable height in pixels */
               if (drawable)
                 i += print (title, title_len, i, "%d",
@@ -458,28 +462,29 @@ gimp_display_shell_format_title (GimpDisplayShell *shell,
 
                 profile = gimp_color_managed_get_color_profile (managed);
 
-                i += print (title, title_len, i, "%s",
-                            gimp_color_profile_get_label (profile));
-
-                g_object_unref (profile);
+                if (profile)
+                  i += print (title, title_len, i, "%s",
+                              gimp_color_profile_get_label (profile));
               }
               break;
 
             case 'e': /* display's offsets in pixels */
               {
-                gdouble scale = gimp_zoom_model_get_factor (shell->zoom);
+                gdouble scale    = gimp_zoom_model_get_factor (shell->zoom);
                 gdouble offset_x = shell->offset_x / scale;
                 gdouble offset_y = shell->offset_y / scale;
+
                 i += print (title, title_len, i,
-                            scale >= 0.15 ? "%.0fx%.0f" : "%.2fx%.2f", offset_x, offset_y);
+                            scale >= 0.15 ? "%.0fx%.0f" : "%.2fx%.2f",
+                            offset_x, offset_y);
               }
-            break;
+              break;
 
             case 'r': /* view rotation angle in degrees */
               {
                 i += print (title, title_len, i, "%.1f", shell->rotate_angle);
               }
-            break;
+              break;
 
             case '\xc3': /* utf-8 extended char */
               {
diff --git a/app/display/gimpdisplayshell.c b/app/display/gimpdisplayshell.c
index 8706f51..ac39f7f 100644
--- a/app/display/gimpdisplayshell.c
+++ b/app/display/gimpdisplayshell.c
@@ -1147,8 +1147,7 @@ gimp_display_shell_get_color_profile (GimpColorManaged *managed)
   if (image)
     return gimp_color_managed_get_color_profile (GIMP_COLOR_MANAGED (image));
 
-  /* never return a NULL profile */
-  return gimp_color_profile_new_srgb ();
+  return NULL;
 }
 
 static void
diff --git a/app/pdb/image-color-profile-cmds.c b/app/pdb/image-color-profile-cmds.c
index 53e5b2e..15ecfd2 100644
--- a/app/pdb/image-color-profile-cmds.c
+++ b/app/pdb/image-color-profile-cmds.c
@@ -107,17 +107,19 @@ image_get_effective_color_profile_invoker (GimpProcedure         *procedure,
   if (success)
     {
       GimpColorProfile *profile;
-      const guint8     *data;
-      gsize             length;
 
       profile = gimp_color_managed_get_color_profile (GIMP_COLOR_MANAGED (image));
 
-      data = gimp_color_profile_get_icc_profile (profile, &length);
+      if (profile)
+        {
+          const guint8     *data;
+          gsize             length;
 
-      profile_data = g_memdup (data, length);
-      num_bytes = length;
+          data = gimp_color_profile_get_icc_profile (profile, &length);
 
-      g_object_unref (profile);
+          profile_data = g_memdup (data, length);
+          num_bytes = length;
+        }
     }
 
   return_vals = gimp_procedure_get_return_values (procedure, success,
@@ -366,7 +368,7 @@ register_image_color_profile_procs (GimpPDB *pdb)
   gimp_procedure_set_static_strings (procedure,
                                      "gimp-image-get-effective-color-profile",
                                      "Returns the color profile that is used for the image",
-                                     "This procedure returns the color profile that is actually used for 
this image, which is the profile returned by 'gimp-image-get-color-profile' if the image has a profile 
assigned, or the default RGB profile from preferences if no profile is assigned to the image. If there is no 
default RGB profile configured in preferences either, a generated default RGB profile is returned.",
+                                     "This procedure returns the color profile that is actually used for 
this image, which is the profile returned by 'gimp-image-get-color-profile' if the image has a profile 
assigned, or a generated default RGB profile. If the image is not RGB or INDEXED, NULL is returned.",
                                      "Michael Natterer <mitch gimp org>",
                                      "Michael Natterer",
                                      "2015",
diff --git a/app/pdb/layer-cmds.c b/app/pdb/layer-cmds.c
index f28ff83..5bc5727 100644
--- a/app/pdb/layer-cmds.c
+++ b/app/pdb/layer-cmds.c
@@ -183,8 +183,6 @@ layer_new_from_visible_invoker (GimpProcedure         *procedure,
                                                GIMP_OPACITY_OPAQUE,
                                                GIMP_NORMAL_MODE,
                                                profile);
-
-      g_object_unref (profile);
     }
 
   return_vals = gimp_procedure_get_return_values (procedure, success,
diff --git a/app/pdb/plug-in-compat-cmds.c b/app/pdb/plug-in-compat-cmds.c
index 662e1a4..3557d1f 100644
--- a/app/pdb/plug-in-compat-cmds.c
+++ b/app/pdb/plug-in-compat-cmds.c
@@ -1888,12 +1888,12 @@ plug_in_icc_profile_info_invoker (GimpProcedure         *procedure,
 
       profile = gimp_color_managed_get_color_profile (GIMP_COLOR_MANAGED (image));
 
-      profile_name = g_strdup (gimp_color_profile_get_model (profile));
-      profile_desc = g_strdup (gimp_color_profile_get_description (profile));
-      profile_info = g_strdup (gimp_color_profile_get_summary (profile));
-
-      g_object_unref (profile);
-
+      if (profile)
+        {
+          profile_name = g_strdup (gimp_color_profile_get_model (profile));
+          profile_desc = g_strdup (gimp_color_profile_get_description (profile));
+          profile_info = g_strdup (gimp_color_profile_get_summary (profile));
+        }
     }
 
   return_vals = gimp_procedure_get_return_values (procedure, success,
diff --git a/app/widgets/gimpimageprofileview.c b/app/widgets/gimpimageprofileview.c
index cee3e1d..5f08494 100644
--- a/app/widgets/gimpimageprofileview.c
+++ b/app/widgets/gimpimageprofileview.c
@@ -111,6 +111,4 @@ gimp_image_profile_view_update (GimpImageParasiteView *view)
   profile = gimp_color_managed_get_color_profile (managed);
 
   gimp_color_profile_view_set_profile (profile_view->profile_view, profile);
-
-  g_object_unref (profile);
 }
diff --git a/libgimp/gimpimagecolorprofile_pdb.c b/libgimp/gimpimagecolorprofile_pdb.c
index 6bd5dfe..9984477 100644
--- a/libgimp/gimpimagecolorprofile_pdb.c
+++ b/libgimp/gimpimagecolorprofile_pdb.c
@@ -90,9 +90,8 @@ _gimp_image_get_color_profile (gint32  image_ID,
  * This procedure returns the color profile that is actually used for
  * this image, which is the profile returned by
  * gimp_image_get_color_profile() if the image has a profile assigned,
- * or the default RGB profile from preferences if no profile is
- * assigned to the image. If there is no default RGB profile configured
- * in preferences either, a generated default RGB profile is returned.
+ * or a generated default RGB profile. If the image is not RGB or
+ * INDEXED, NULL is returned.
  *
  * Returns: The image's serialized color profile. The returned value
  * must be freed with g_free().
diff --git a/libgimpcolor/gimpcolormanaged.c b/libgimpcolor/gimpcolormanaged.c
index f52308f..6c85932 100644
--- a/libgimpcolor/gimpcolormanaged.c
+++ b/libgimpcolor/gimpcolormanaged.c
@@ -153,8 +153,7 @@ gimp_color_managed_get_color_profile (GimpColorManaged *managed)
   if (iface->get_color_profile)
     return iface->get_color_profile (managed);
 
-  /* never return a NULL profile */
-  return gimp_color_profile_new_srgb ();
+  return NULL;
 }
 
 /**
diff --git a/modules/display-filter-lcms.c b/modules/display-filter-lcms.c
index 0ce53d1..c30615a 100644
--- a/modules/display-filter-lcms.c
+++ b/modules/display-filter-lcms.c
@@ -278,15 +278,16 @@ cdisplay_lcms_changed (GimpColorDisplay *display)
 
   src_profile = gimp_color_managed_get_color_profile (managed);
 
-  lcms->src_format  = babl_format ("R'G'B'A float");
-  lcms->dest_format = babl_format ("R'G'B'A float");
-
-  lcms->transform = gimp_widget_get_color_transform (widget,
-                                                     config, src_profile,
-                                                     &lcms->src_format,
-                                                     &lcms->dest_format);
+  if (src_profile)
+    {
+      lcms->src_format  = babl_format ("R'G'B'A float");
+      lcms->dest_format = babl_format ("R'G'B'A float");
 
-  g_object_unref (src_profile);
+      lcms->transform = gimp_widget_get_color_transform (widget,
+                                                         config, src_profile,
+                                                         &lcms->src_format,
+                                                         &lcms->dest_format);
+    }
 }
 
 static GimpColorProfile *
@@ -362,6 +363,9 @@ cdisplay_lcms_update_profile_label (CdisplayLcms *lcms,
   if (strcmp (name, "rgb-profile") == 0)
     {
       profile = gimp_color_managed_get_color_profile (managed);
+
+      if (profile)
+        g_object_ref (profile);
     }
   else if (g_str_has_prefix (name, "display-profile"))
     {
diff --git a/tools/pdbgen/pdb/image_color_profile.pdb b/tools/pdbgen/pdb/image_color_profile.pdb
index a489b04..09fb21c 100644
--- a/tools/pdbgen/pdb/image_color_profile.pdb
+++ b/tools/pdbgen/pdb/image_color_profile.pdb
@@ -67,9 +67,8 @@ sub image_get_effective_color_profile {
 This procedure returns the color profile that is actually used for
 this image, which is the profile returned by
 gimp_image_get_color_profile() if the image has a profile assigned, or
-the default RGB profile from preferences if no profile is assigned to
-the image. If there is no default RGB profile configured in
-preferences either, a generated default RGB profile is returned.
+a generated default RGB profile. If the image is not RGB or INDEXED,
+NULL is returned.
 HELP
 
     &mitch_pdb_misc('2015', '2.10');
@@ -90,17 +89,19 @@ HELP
         code => <<'CODE'
 {
   GimpColorProfile *profile;
-  const guint8     *data;
-  gsize             length;
 
   profile = gimp_color_managed_get_color_profile (GIMP_COLOR_MANAGED (image));
 
-  data = gimp_color_profile_get_icc_profile (profile, &length);
+  if (profile)
+    {
+      const guint8     *data;
+      gsize             length;
 
-  profile_data = g_memdup (data, length);
-  num_bytes = length;
+      data = gimp_color_profile_get_icc_profile (profile, &length);
 
-  g_object_unref (profile);
+      profile_data = g_memdup (data, length);
+      num_bytes = length;
+    }
 }
 CODE
     );
diff --git a/tools/pdbgen/pdb/layer.pdb b/tools/pdbgen/pdb/layer.pdb
index 550f917..277f499 100644
--- a/tools/pdbgen/pdb/layer.pdb
+++ b/tools/pdbgen/pdb/layer.pdb
@@ -157,8 +157,6 @@ HELP
                                            GIMP_OPACITY_OPAQUE,
                                            GIMP_NORMAL_MODE,
                                            profile);
-
-  g_object_unref (profile);
 }
 CODE
     );
diff --git a/tools/pdbgen/pdb/plug_in_compat.pdb b/tools/pdbgen/pdb/plug_in_compat.pdb
index e2afcad..d31b767 100644
--- a/tools/pdbgen/pdb/plug_in_compat.pdb
+++ b/tools/pdbgen/pdb/plug_in_compat.pdb
@@ -1784,12 +1784,12 @@ HELP
 
   profile = gimp_color_managed_get_color_profile (GIMP_COLOR_MANAGED (image));
 
-  profile_name = g_strdup (gimp_color_profile_get_model (profile));
-  profile_desc = g_strdup (gimp_color_profile_get_description (profile));
-  profile_info = g_strdup (gimp_color_profile_get_summary (profile));
-
-  g_object_unref (profile);
-
+  if (profile)
+    {
+      profile_name = g_strdup (gimp_color_profile_get_model (profile));
+      profile_desc = g_strdup (gimp_color_profile_get_description (profile));
+      profile_info = g_strdup (gimp_color_profile_get_summary (profile));
+    }
 }
 CODE
     );


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