[gtk/heif-demo: 53/53] testheif: Use gdk_color_profile_new_for_cicp




commit 436d2f349e1457903a3b80e6c70b33dd60e276e4
Author: Matthias Clasen <mclasen redhat com>
Date:   Wed May 11 00:13:26 2022 -0400

    testheif: Use gdk_color_profile_new_for_cicp

 tests/testheif.c | 157 ++++++++++++++++++++++++-------------------------------
 1 file changed, 68 insertions(+), 89 deletions(-)
---
diff --git a/tests/testheif.c b/tests/testheif.c
index 26d68da176..8db100fee0 100644
--- a/tests/testheif.c
+++ b/tests/testheif.c
@@ -6,99 +6,62 @@ extern GdkColorProfile *gdk_color_profile_new_from_lcms_profile (cmsHPROFILE   l
                                                                  GError      **error);
 extern GdkColorProfile *gdk_color_profile_get_srgb_linear (void);
 
-
-static GdkColorProfile *
-nclx_to_color_profile (const struct heif_color_profile_nclx *nclx)
+static void
+describe_nclx_color_profile (const struct heif_color_profile_nclx *nclx,
+                             GString                              *s)
 {
-  cmsHPROFILE profile = NULL;
-  cmsCIExyY whitepoint;
-  cmsCIExyYTRIPLE primaries;
-  cmsToneCurve *curve[3];
-
-  cmsFloat64Number srgb_parameters[5] =
-  { 2.4, 1.0 / 1.055,  0.055 / 1.055, 1.0 / 12.92, 0.04045 };
-
-  cmsFloat64Number rec709_parameters[5] =
-  { 2.2, 1.0 / 1.099,  0.099 / 1.099, 1.0 / 4.5, 0.081 };
-
-  if (nclx == NULL)
-    {
-      return NULL;
-    }
-
   if (nclx->color_primaries == heif_color_primaries_unspecified)
-    {
-      return NULL;
-    }
+    return;
 
   if (nclx->color_primaries == heif_color_primaries_ITU_R_BT_709_5)
     {
-      if (nclx->transfer_characteristics == heif_transfer_characteristic_IEC_61966_2_1)
-        return gdk_color_profile_get_srgb ();
-      else if (nclx->transfer_characteristics == heif_transfer_characteristic_linear)
-        return gdk_color_profile_get_srgb_linear();
-    }
-
-  whitepoint.x = nclx->color_primary_white_x;
-  whitepoint.y = nclx->color_primary_white_y;
-  whitepoint.Y = 1.0f;
-
-  primaries.Red.x = nclx->color_primary_red_x;
-  primaries.Red.y = nclx->color_primary_red_y;
-  primaries.Red.Y = 1.0f;
-
-  primaries.Green.x = nclx->color_primary_green_x;
-  primaries.Green.y = nclx->color_primary_green_y;
-  primaries.Green.Y = 1.0f;
-
-  primaries.Blue.x = nclx->color_primary_blue_x;
-  primaries.Blue.y = nclx->color_primary_blue_y;
-  primaries.Blue.Y = 1.0f;
+      if (nclx->transfer_characteristics == heif_transfer_characteristic_IEC_61966_2_1 &&
+          (nclx->matrix_coefficients == heif_matrix_coefficients_ITU_R_BT_470_6_System_B_G ||
+           nclx->matrix_coefficients == heif_matrix_coefficients_ITU_R_BT_601_6))
+        {
+          g_string_append (s, "sRGB");
+          return;
+        }
 
-  switch (nclx->transfer_characteristics)
-    {
-    case heif_transfer_characteristic_ITU_R_BT_709_5:
-      curve[0] = curve[1] = curve[2] = cmsBuildParametricToneCurve (NULL, 4,
-                                       rec709_parameters);
-      profile = cmsCreateRGBProfile (&whitepoint, &primaries, curve);
-      cmsFreeToneCurve (curve[0]);
-      break;
-    case heif_transfer_characteristic_ITU_R_BT_470_6_System_M:
-      curve[0] = curve[1] = curve[2] = cmsBuildGamma (NULL, 2.2f);
-      profile = cmsCreateRGBProfile (&whitepoint, &primaries, curve);
-      cmsFreeToneCurve (curve[0]);
-      break;
-    case heif_transfer_characteristic_ITU_R_BT_470_6_System_B_G:
-      curve[0] = curve[1] = curve[2] = cmsBuildGamma (NULL, 2.8f);
-      profile = cmsCreateRGBProfile (&whitepoint, &primaries, curve);
-      cmsFreeToneCurve (curve[0]);
-      break;
-    case heif_transfer_characteristic_linear:
-      curve[0] = curve[1] = curve[2] = cmsBuildGamma (NULL, 1.0f);
-      profile = cmsCreateRGBProfile (&whitepoint, &primaries, curve);
-      cmsFreeToneCurve (curve[0]);
-      break;
-    case heif_transfer_characteristic_IEC_61966_2_1:
-      /* same as default */
-    default:
-      curve[0] = curve[1] = curve[2] = cmsBuildParametricToneCurve (NULL, 4,
-                                       srgb_parameters);
-      profile = cmsCreateRGBProfile (&whitepoint, &primaries, curve);
-      cmsFreeToneCurve (curve[0]);
-      break;
+      if (nclx->transfer_characteristics == heif_transfer_characteristic_linear &&
+          (nclx->matrix_coefficients == heif_matrix_coefficients_ITU_R_BT_470_6_System_B_G ||
+           nclx->matrix_coefficients == heif_matrix_coefficients_ITU_R_BT_601_6))
+        {
+          g_string_append (s, "sRGB linear");
+          return;
+        }
     }
-  if (profile)
-    {
-      GdkColorProfile *new_profile;
 
-      new_profile = gdk_color_profile_new_from_lcms_profile (profile, NULL);
+  if (nclx->color_primaries == heif_color_primaries_ITU_R_BT_2020_2_and_2100_0)
+    {
+      if (nclx->transfer_characteristics == heif_transfer_characteristic_ITU_R_BT_2100_0_PQ &&
+          nclx->matrix_coefficients == heif_matrix_coefficients_ITU_R_BT_2020_2_non_constant_luminance)
+        {
+          g_string_append (s, "BT.2020 PQ");
+          return;
+        }
 
-      cmsCloseProfile (profile);
+      if (nclx->transfer_characteristics == heif_transfer_characteristic_ITU_R_BT_2100_0_HLG &&
+          nclx->matrix_coefficients == heif_matrix_coefficients_ITU_R_BT_2020_2_non_constant_luminance)
+        {
+          g_string_append (s, "BT.2020 HLG");
+          return;
+        }
+    }
 
-      return new_profile;
+  if (nclx->color_primaries == heif_color_primaries_SMPTE_EG_432_1)
+    {
+      if (nclx->transfer_characteristics == heif_transfer_characteristic_ITU_R_BT_2100_0_PQ)
+        {
+          g_string_append (s, "P3 PQ");
+          return;
+        }
     }
 
-  return NULL;
+  g_string_append_printf (s, "%d/%d/%d",
+                          nclx->color_primaries,
+                          nclx->matrix_coefficients,
+                          nclx->transfer_characteristics);
 }
 
 static GdkTexture *
@@ -118,7 +81,7 @@ load_heif_image (const char  *filename,
   GBytes *bytes;
   GdkTexture *texture = NULL;
   GdkColorProfile *profile = NULL;
-  const char *profile_type = "";
+  char *profile_type = NULL;
 
   ctx = heif_context_alloc ();
 
@@ -147,7 +110,7 @@ load_heif_image (const char  *filename,
         guchar *icc_data;
         gsize icc_size;
 
-        profile_type = ", icc color profile";
+        profile_type = g_strdup ("icc color profile");
         icc_size = heif_image_handle_get_raw_color_profile_size (handle);
         icc_data = g_new0 (guchar, icc_size);
         err = heif_image_handle_get_raw_color_profile (handle, icc_data);
@@ -167,8 +130,16 @@ load_heif_image (const char  *filename,
         err = heif_image_handle_get_nclx_color_profile (handle, &nclx);
         if (err.code == heif_error_Ok)
           {
-            profile_type = ", nclx color profile";
-            profile = nclx_to_color_profile (nclx);
+            GString *s;
+
+            s = g_string_new ("nclx color profile ");
+            describe_nclx_color_profile (nclx, s);
+            profile_type = g_string_free (s, FALSE);
+            profile = gdk_color_profile_new_from_cicp (nclx->color_primaries,
+                                                       nclx->transfer_characteristics,
+                                                       nclx->matrix_coefficients,
+                                                       nclx->full_range_flag,
+                                                       NULL);
             heif_nclx_color_profile_free (nclx);
           }
       }
@@ -177,16 +148,21 @@ load_heif_image (const char  *filename,
       g_assert_not_reached ();
     }
 
-  g_string_append_printf (details, "%d × %d pixels\n%d bits of luma, %d bits of chroma%s%s\n",
+  g_string_append_printf (details, "%d × %d pixels\n%d bits of luma, %d bits of chroma%s\n",
                           heif_image_handle_get_width (handle),
                           heif_image_handle_get_height (handle),
                           heif_image_handle_get_luma_bits_per_pixel (handle),
                           heif_image_handle_get_chroma_bits_per_pixel (handle),
-                          heif_image_handle_has_alpha_channel (handle) ? ", with alpha" : "",
-                          profile ? profile_type : "");
+                          heif_image_handle_has_alpha_channel (handle) ? ", with alpha" : "");
+
+  if (profile_type)
+    {
+      g_string_append_printf (details, "%s\n", profile_type);
+      g_free (profile_type);
+    }
 
   if (profile == NULL)
-    profile = gdk_color_profile_get_srgb ();
+    profile = g_object_ref (gdk_color_profile_get_srgb ());
 
   if (heif_image_handle_has_alpha_channel (handle))
     {
@@ -236,6 +212,8 @@ load_heif_image (const char  *filename,
 
   g_string_append_printf (details, "texture format %s", format_name);
 
+  g_print ("%s\n", details->str);
+
   if (format == GDK_MEMORY_R16G16B16A16 || format == GDK_MEMORY_R16G16B16)
     {
       /* Shift image data to full 16bit range */
@@ -266,6 +244,7 @@ load_heif_image (const char  *filename,
                                                        format, profile,
                                                        bytes, stride);
   g_bytes_unref (bytes);
+  g_object_unref (profile);
 
 out:
   heif_context_free (ctx);


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