[gimp] app: validate 'icc-profile' parasites before attaching them



commit 4f10ff3ad43bca7efd9e2e51f6e58901220bdc03
Author: Michael Natterer <mitch gimp org>
Date:   Sun Mar 23 19:46:09 2014 +0100

    app: validate 'icc-profile' parasites before attaching them
    
    The must be persistent and undoable and contain an RGB ICC profile.

 app/core/gimpimage-profile.c |   70 ++++++++++++++++++++++++++++++++++++++---
 app/core/gimpimage-profile.h |   17 ++++++----
 app/core/gimpimage.c         |    9 +++++
 3 files changed, 83 insertions(+), 13 deletions(-)
---
diff --git a/app/core/gimpimage-profile.c b/app/core/gimpimage-profile.c
index 883fec8..32e274b 100644
--- a/app/core/gimpimage-profile.c
+++ b/app/core/gimpimage-profile.c
@@ -35,7 +35,7 @@
 
 #include "config/gimpcoreconfig.h"
 
-#include "core/gimp.h"
+#include "gimp.h"
 #include "gimperror.h"
 #include "gimpimage.h"
 #include "gimpimage-profile.h"
@@ -45,6 +45,67 @@
 
 /* public functions */
 
+gboolean
+gimp_image_validate_icc_profile (GimpImage           *image,
+                                 const GimpParasite  *icc_profile,
+                                 GError             **error)
+{
+  GimpColorProfile *profile;
+
+  g_return_val_if_fail (GIMP_IS_IMAGE (image), FALSE);
+  g_return_val_if_fail (icc_profile != NULL, FALSE);
+  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
+
+  if (strcmp (gimp_parasite_name (icc_profile),
+              GIMP_ICC_PROFILE_PARASITE_NAME) != 0)
+    {
+      g_set_error_literal (error, GIMP_ERROR, GIMP_FAILED,
+                           _("ICC profile validation failed: "
+                             "Parasite's name is not 'icc-profile'"));
+      return FALSE;
+    }
+
+  if (gimp_parasite_flags (icc_profile) != (GIMP_PARASITE_PERSISTENT |
+                                            GIMP_PARASITE_UNDOABLE))
+    {
+      g_set_error_literal (error, GIMP_ERROR, GIMP_FAILED,
+                           _("ICC profile validation failed: "
+                             "Parasite's flags are not (PERSISTENT | UNDOABLE)"));
+      return FALSE;
+    }
+
+  if (gimp_image_get_base_type (image) == GIMP_GRAY)
+    {
+      g_set_error_literal (error, GIMP_ERROR, GIMP_FAILED,
+                           _("ICC profile validation failed: "
+                             "Cannot attach a color profile to a GRAY image"));
+      return FALSE;
+    }
+
+  profile = gimp_lcms_profile_open_from_data (gimp_parasite_data (icc_profile),
+                                              gimp_parasite_data_size (icc_profile),
+                                              NULL, error);
+
+  if (! profile)
+    {
+      g_prefix_error (error, _("ICC profile validation failed: "));
+      return FALSE;
+    }
+
+  if (! gimp_lcms_profile_is_rgb (profile))
+    {
+      g_set_error_literal (error, GIMP_ERROR, GIMP_FAILED,
+                           _("ICC profile validation failed: "
+                             "Color profile is not for RGB color space"));
+      cmsCloseProfile (profile);
+      return FALSE;
+    }
+
+  cmsCloseProfile (profile);
+
+  return TRUE;
+}
+
 const GimpParasite *
 gimp_image_get_icc_profile (GimpImage *image)
 {
@@ -61,11 +122,8 @@ gimp_image_set_icc_profile (GimpImage          *image,
 
   if (icc_profile)
     {
-      g_return_if_fail (strcmp (gimp_parasite_name (icc_profile),
-                                GIMP_ICC_PROFILE_PARASITE_NAME) == 0);
-      g_return_if_fail (gimp_parasite_flags (icc_profile) ==
-                        (GIMP_PARASITE_PERSISTENT |
-                         GIMP_PARASITE_UNDOABLE));
+      g_return_if_fail (gimp_image_validate_icc_profile (image, icc_profile,
+                                                         NULL) == TRUE);
 
       gimp_image_parasite_attach (image, icc_profile);
     }
diff --git a/app/core/gimpimage-profile.h b/app/core/gimpimage-profile.h
index 8d653e6..9974826 100644
--- a/app/core/gimpimage-profile.h
+++ b/app/core/gimpimage-profile.h
@@ -22,13 +22,16 @@
 #define GIMP_ICC_PROFILE_PARASITE_NAME "icc-profile"
 
 
-const GimpParasite * gimp_image_get_icc_profile (GimpImage          *image);
-void                 gimp_image_set_icc_profile (GimpImage          *image,
-                                                 const GimpParasite *icc_profile);
-
-GimpColorProfile     gimp_image_get_profile     (GimpImage          *image,
-                                                 guint8             *md5_digest,
-                                                 GError            **error);
+gboolean             gimp_image_validate_icc_profile (GimpImage           *image,
+                                                      const GimpParasite  *icc_profile,
+                                                      GError             **error);
+const GimpParasite * gimp_image_get_icc_profile      (GimpImage           *image);
+void                 gimp_image_set_icc_profile      (GimpImage           *image,
+                                                      const GimpParasite  *icc_profile);
+
+GimpColorProfile     gimp_image_get_profile          (GimpImage           *image,
+                                                      guint8              *md5_digest,
+                                                      GError             **error);
 
 
 #endif /* __GIMP_IMAGE_PROFILE_H__ */
diff --git a/app/core/gimpimage.c b/app/core/gimpimage.c
index eebef2f..6ef1b04 100644
--- a/app/core/gimpimage.c
+++ b/app/core/gimpimage.c
@@ -3055,10 +3055,19 @@ gimp_image_parasite_validate (GimpImage           *image,
                               const GimpParasite  *parasite,
                               GError             **error)
 {
+  const gchar *name;
+
   g_return_val_if_fail (GIMP_IS_IMAGE (image), FALSE);
   g_return_val_if_fail (parasite != NULL, FALSE);
   g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
 
+  name = gimp_parasite_name (parasite);
+
+  if (strcmp (name, GIMP_ICC_PROFILE_PARASITE_NAME) == 0)
+    {
+      return gimp_image_validate_icc_profile (image, parasite, error);
+    }
+
   return TRUE;
 }
 


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