[gimp] plug-ins: implement our profile export policy for HEIF.



commit 5d6baf5f85fc62cf1f577a505beaad27a11b6c19
Author: Jehan <jehan girinstud io>
Date:   Mon Jul 1 15:36:59 2019 +0200

    plug-ins: implement our profile export policy for HEIF.
    
    Same as WebP or jpeg: follow explicitly assigned profile TRC; convert
    default (non-assigned) profile to sRGB, except if work format is 8-bit
    linear.

 plug-ins/common/file-heif.c | 77 +++++++++++++++++++++++++++++++++------------
 1 file changed, 57 insertions(+), 20 deletions(-)
---
diff --git a/plug-ins/common/file-heif.c b/plug-ins/common/file-heif.c
index bf10f2b9a8..3975b07554 100644
--- a/plug-ins/common/file-heif.c
+++ b/plug-ins/common/file-heif.c
@@ -674,7 +674,11 @@ save_image (GFile             *file,
   struct heif_image_handle *handle;
   struct heif_writer        writer;
   struct heif_error         err;
+#ifdef HAVE_LIBHEIF_1_4_0
   GimpColorProfile         *profile = NULL;
+  const guint8             *icc_data;
+  gsize                     icc_length;
+#endif
   GOutputStream            *output;
   GeglBuffer               *buffer;
   const Babl               *format;
@@ -684,11 +688,11 @@ save_image (GFile             *file,
   gint                      width;
   gint                      height;
   gboolean                  has_alpha;
+  gboolean                  out_linear = FALSE;
 
   gimp_progress_init_printf (_("Exporting '%s'"),
                              g_file_get_parse_name (file));
 
-  profile = gimp_image_get_effective_color_profile (image_ID);
   width   = gimp_drawable_width  (drawable_ID);
   height  = gimp_drawable_height (drawable_ID);
 
@@ -701,28 +705,50 @@ save_image (GFile             *file,
                            heif_chroma_interleaved_RGB,
                            &image);
 
-  if (profile)
-    {
 #ifdef HAVE_LIBHEIF_1_4_0
-      const guint8 *icc_data;
-      gsize         icc_length;
-
-      icc_data = gimp_color_profile_get_icc_profile (profile, &icc_length);
-      heif_image_set_raw_color_profile (image, "prof", icc_data, icc_length);
-      space = gimp_color_profile_get_space (profile,
-                                            GIMP_COLOR_RENDERING_INTENT_RELATIVE_COLORIMETRIC,
-                                            error);
-      if (error && *error)
+  profile = gimp_image_get_color_profile (image_ID);
+  if (profile && gimp_color_profile_is_linear (profile))
+    out_linear = TRUE;
+
+  if (! profile)
+    {
+      profile = gimp_image_get_effective_color_profile (image_ID);
+
+      if (gimp_color_profile_is_linear (profile))
         {
-          /* Don't make this a hard failure yet output the error. */
-          g_printerr ("%s: error getting the profile space: %s",
-                      G_STRFUNC, (*error)->message);
-          g_clear_error (error);
+          if (gimp_image_get_precision (image_ID) != GIMP_PRECISION_U8_LINEAR)
+            {
+              /* If stored data was linear, let's convert the profile. */
+              GimpColorProfile *saved_profile;
+
+              saved_profile = gimp_color_profile_new_srgb_trc_from_color_profile (profile);
+              g_clear_object (&profile);
+              profile = saved_profile;
+            }
+          else
+            {
+              /* Keep linear profile as-is for 8-bit linear image. */
+              out_linear = TRUE;
+            }
         }
+    }
 
-#endif /* HAVE_LIBHEIF_1_4_0 */
-      g_object_unref (profile);
+  icc_data = gimp_color_profile_get_icc_profile (profile, &icc_length);
+  heif_image_set_raw_color_profile (image, "prof", icc_data, icc_length);
+  space = gimp_color_profile_get_space (profile,
+                                        GIMP_COLOR_RENDERING_INTENT_RELATIVE_COLORIMETRIC,
+                                        error);
+  if (error && *error)
+    {
+      /* Don't make this a hard failure yet output the error. */
+      g_printerr ("%s: error getting the profile space: %s",
+                  G_STRFUNC, (*error)->message);
+      g_clear_error (error);
     }
+
+  g_object_unref (profile);
+#endif /* HAVE_LIBHEIF_1_4_0 */
+
   if (! space)
     space = gimp_drawable_get_format (drawable_ID);
 
@@ -734,9 +760,20 @@ save_image (GFile             *file,
   buffer = gimp_drawable_get_buffer (drawable_ID);
 
   if (has_alpha)
-    format = babl_format_with_space ("R'G'B'A u8", space);
+    {
+      if (out_linear)
+        format = babl_format ("RGBA u8");
+      else
+        format = babl_format ("R'G'B'A u8");
+    }
   else
-    format = babl_format_with_space ("R'G'B' u8", space);
+    {
+      if (out_linear)
+        format = babl_format ("RGB u8");
+      else
+        format = babl_format ("R'G'B' u8");
+    }
+  format = babl_format_with_space (babl_format_get_encoding (format), space);
 
   gegl_buffer_get (buffer,
                    GEGL_RECTANGLE (0, 0, width, height),


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