[gtk/heif-demo: 1/3] Add gdk_color_profile_new_from_cicp




commit fb2765cc40b200f554ad38a2b07ba89660e55d47
Author: Matthias Clasen <mclasen redhat com>
Date:   Tue May 10 08:34:17 2022 -0400

    Add gdk_color_profile_new_from_cicp
    
    This is a bit preliminary, and does not handle hdr
    profiles yet.

 gdk/gdkcolorprofile.c        | 223 ++++++++++++++++++++++++++++++++++++++++++-
 gdk/gdkcolorprofile.h        |  14 ++-
 gdk/gdkcolorprofileprivate.h |   2 +
 3 files changed, 236 insertions(+), 3 deletions(-)
---
diff --git a/gdk/gdkcolorprofile.c b/gdk/gdkcolorprofile.c
index 3a200b251c..c6be6af6db 100644
--- a/gdk/gdkcolorprofile.c
+++ b/gdk/gdkcolorprofile.c
@@ -52,6 +52,11 @@ struct _GdkColorProfile
 
   GBytes *icc_profile;
   cmsHPROFILE lcms_profile;
+
+  int color_primaries;
+  int matrix_coefficients;
+  int transfer_characteristics;
+  gboolean full_range;
 };
 
 struct _GdkColorProfileClass
@@ -93,6 +98,12 @@ gdk_color_profile_real_init (GInitable     *initable,
         }
     }
 
+  /* FIXME: can we determine these values from icc */
+  self->color_primaries = 2;
+  self->transfer_characteristics = 2;
+  self->matrix_coefficients = 0;
+  self->full_range = TRUE;
+
   return TRUE;
 }
 
@@ -214,6 +225,11 @@ gdk_color_profile_get_srgb (void)
       new_profile = gdk_color_profile_new_from_lcms_profile (cmsCreate_sRGBProfile (), NULL);
       g_assert (new_profile);
 
+      new_profile->color_primaries = 0;
+      new_profile->transfer_characteristics = 13;
+      new_profile->matrix_coefficients = 0;
+      new_profile->full_range = TRUE;
+
       g_once_init_leave (&srgb_profile, new_profile);
     }
 
@@ -259,6 +275,11 @@ gdk_color_profile_get_srgb_linear (void)
       new_profile = gdk_color_profile_new_from_lcms_profile (lcms_profile, NULL);
       g_assert (new_profile);
 
+      new_profile->color_primaries = 0;
+      new_profile->transfer_characteristics = 8;
+      new_profile->matrix_coefficients = 0;
+      new_profile->full_range = TRUE;
+
       g_once_init_leave (&srgb_linear_profile, new_profile);
     }
 
@@ -369,8 +390,8 @@ gdk_color_profile_is_linear (GdkColorProfile *self)
 {
   g_return_val_if_fail (GDK_IS_COLOR_PROFILE (self), FALSE);
 
-  /* FIXME: Make this useful */
-  if (self == gdk_color_profile_get_srgb_linear ())
+  /* FIXME: Make this useful for lcms profiles */
+  if (self->transfer_characteristics == 8)
     return TRUE;
 
   return FALSE;
@@ -516,4 +537,202 @@ gdk_color_profile_lookup_transform (GdkColorProfile *source,
   return transform;
 }
 
+/**
+ * gdk_color_profile_new_from_cicp:
+ * @color_primaries: the color primaries to use
+ * @transfer_characteristics: the transfer function to use
+ * @matrix_coefficients: the matrix coefficients
+ * @full_range: whether the data is full-range or not
+ *
+ * Creates a new color profile from CICP parameters.
+ *
+ * This function only supports a subset of possible combinations
+ * of @color_primaries and @transfer_characteristics.
+ *
+ * @matrix_coefficients and @full_range must have the values
+ * 0 and `TRUE`, since only full-range RGB profiles are
+ * supported.
+ *
+ * Returns: a new `GdkColorProfile` or %NULL on error
+ *
+ * Since: 4.8
+ */
+GdkColorProfile *
+gdk_color_profile_new_from_cicp (int        color_primaries,
+                                 int        transfer_characteristics,
+                                 int        matrix_coefficients,
+                                 gboolean   full_range,
+                                 GError   **error)
+{
+  cmsHPROFILE profile = NULL;
+  cmsCIExyY whitepoint;
+  cmsCIExyYTRIPLE primaries;
+  cmsToneCurve *curve[3];
+  cmsCIExyY whiteD65 = (cmsCIExyY) { 0.3127, 0.3290, 1.0 };
+  cmsCIExyY whiteC = (cmsCIExyY) { 0.310, 0.316, 1.0 };
+  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 };
+
+  /* We only support full-range RGB profiles */
+  g_assert (matrix_coefficients == 0);
+  g_assert (full_range);
+
+  if (color_primaries == 0 /* ITU_R_BT_709_5 */)
+    {
+      if (transfer_characteristics == 13 /* IEC_61966_2_1 */)
+        return g_object_ref (gdk_color_profile_get_srgb ());
+      else if (transfer_characteristics == 8 /* linear */)
+        return g_object_ref (gdk_color_profile_get_srgb_linear());
+    }
 
+  switch (color_primaries)
+    {
+    case 1:
+      primaries.Green = (cmsCIExyY) { 0.300, 0.600, 1.0 };
+      primaries.Blue  = (cmsCIExyY) { 0.150, 0.060, 1.0 };
+      primaries.Red   = (cmsCIExyY) { 0.640, 0.330, 1.0 };
+      whitepoint = whiteD65;
+      break;
+    case 4:
+      primaries.Green = (cmsCIExyY) { 0.21, 0.71, 1.0 };
+      primaries.Blue  = (cmsCIExyY) { 0.14, 0.08, 1.0 };
+      primaries.Red   = (cmsCIExyY) { 0.67, 0.33, 1.0 };
+      whitepoint = whiteC;
+      break;
+    case 5:
+      primaries.Green = (cmsCIExyY) { 0.29, 0.60, 1.0 };
+      primaries.Blue  = (cmsCIExyY) { 0.15, 0.06, 1.0 };
+      primaries.Red   = (cmsCIExyY) { 0.64, 0.33, 1.0 };
+      whitepoint = whiteD65;
+      break;
+    case 6:
+    case 7:
+      primaries.Green = (cmsCIExyY) { 0.310, 0.595, 1.0 };
+      primaries.Blue  = (cmsCIExyY) { 0.155, 0.070, 1.0 };
+      primaries.Red   = (cmsCIExyY) { 0.630, 0.340, 1.0 };
+      whitepoint = whiteD65;
+      break;
+    case 8:
+      primaries.Green = (cmsCIExyY) { 0.243, 0.692, 1.0 };
+      primaries.Blue  = (cmsCIExyY) { 0.145, 0.049, 1.0 };
+      primaries.Red   = (cmsCIExyY) { 0.681, 0.319, 1.0 };
+      whitepoint = whiteC;
+      break;
+    case 9:
+      primaries.Green = (cmsCIExyY) { 0.170, 0.797, 1.0 };
+      primaries.Blue  = (cmsCIExyY) { 0.131, 0.046, 1.0 };
+      primaries.Red   = (cmsCIExyY) { 0.708, 0.292, 1.0 };
+      whitepoint = whiteD65;
+      break;
+    case 10:
+      primaries.Green = (cmsCIExyY) { 0.0, 1.0, 1.0 };
+      primaries.Blue  = (cmsCIExyY) { 0.0, 0.0, 1.0 };
+      primaries.Red   = (cmsCIExyY) { 1.0, 0.0, 1.0 };
+      whitepoint = (cmsCIExyY) { 0.333333, 0.333333, 1.0 };
+      break;
+    case 11:
+      primaries.Green = (cmsCIExyY) { 0.265, 0.690, 1.0 };
+      primaries.Blue  = (cmsCIExyY) { 0.150, 0.060, 1.0 };
+      primaries.Red   = (cmsCIExyY) { 0.680, 0.320, 1.0 };
+      whitepoint = (cmsCIExyY) { 0.314, 0.351, 1.0 };
+      break;
+    case 12:
+      primaries.Green = (cmsCIExyY) { 0.265, 0.690, 1.0 };
+      primaries.Blue  = (cmsCIExyY) { 0.150, 0.060, 1.0 };
+      primaries.Red   = (cmsCIExyY) { 0.680, 0.320, 1.0 };
+      whitepoint = whiteD65;
+      break;
+    case 22:
+      primaries.Green = (cmsCIExyY) { 0.295, 0.605, 1.0 };
+      primaries.Blue  = (cmsCIExyY) { 0.155, 0.077, 1.0 };
+      primaries.Red   = (cmsCIExyY) { 0.630, 0.340, 1.0 };
+      whitepoint = whiteD65;
+      break;
+    default:
+      g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "Unsupported color primaries (%d)", 
color_primaries);
+      return NULL;
+    }
+
+  switch (transfer_characteristics)
+    {
+    case 1: /* 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 4: /* 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 5: /* 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 8: /* linear */
+      curve[0] = curve[1] = curve[2] = cmsBuildGamma (NULL, 1.0f);
+      profile = cmsCreateRGBProfile (&whitepoint, &primaries, curve);
+      cmsFreeToneCurve (curve[0]);
+      break;
+    case 13: /* IEC_61966_2_1 */
+    default: /* FIXME */
+      curve[0] = curve[1] = curve[2] = cmsBuildParametricToneCurve (NULL, 4,
+                                       srgb_parameters);
+      profile = cmsCreateRGBProfile (&whitepoint, &primaries, curve);
+      cmsFreeToneCurve (curve[0]);
+      break;
+    }
+
+  if (profile)
+    {
+      GdkColorProfile *color_profile;
+
+      color_profile = gdk_color_profile_new_from_lcms_profile (profile, error);
+
+      color_profile->color_primaries = color_primaries;
+      color_profile->transfer_characteristics = transfer_characteristics;
+      color_profile->matrix_coefficients = matrix_coefficients;
+      color_profile->full_range = full_range;
+
+      return color_profile;
+    }
+
+  return NULL;
+}
+
+/**
+ * gdk_color_profile_get_cicp_data:
+ * @self: a `GdkColorProfile`
+ * @color_primaries: (out): return location for color primaries
+ * @transfer_characteristics: (out): return location for transfer characteristics
+ * @matrix_coefficients: (out): return location for matrix coefficients
+ * @full_range: (out): return location for the full range flag
+ *
+ * Gets the CICP parameters of a color profile.
+ *
+ * This is mainly useful for color profiles created via
+ * [ctor@Gdk.ColorProfile.new_for_cicp].
+ *
+ * Note that @color_primaries and @transfer_characteristics will be set to
+ * 2 (unspecified) if the color profile does not contain CICP information.
+ *
+ * Since: 4.8
+ */
+void
+gdk_color_profile_get_cicp_data (GdkColorProfile *self,
+                                 int             *color_primaries,
+                                 int             *transfer_characteristics,
+                                 int             *matrix_coefficients,
+                                 gboolean        *full_range)
+{
+  g_return_if_fail (GDK_IS_COLOR_PROFILE (self));
+
+  *color_primaries = self->color_primaries;
+  *transfer_characteristics = self->transfer_characteristics;
+  *matrix_coefficients = self->matrix_coefficients;
+  *full_range = self->full_range;
+}
diff --git a/gdk/gdkcolorprofile.h b/gdk/gdkcolorprofile.h
index a7b166e31f..9c7afb6ace 100644
--- a/gdk/gdkcolorprofile.h
+++ b/gdk/gdkcolorprofile.h
@@ -50,10 +50,22 @@ GdkColorProfile *            gdk_color_profile_get_srgb                   (void)
 GDK_AVAILABLE_IN_4_8
 GdkColorProfile *            gdk_color_profile_new_from_icc_bytes         (GBytes               *bytes,
                                                                            GError              **error);
-
 GDK_AVAILABLE_IN_4_8
 GBytes *                     gdk_color_profile_get_icc_profile            (GdkColorProfile      *self);
 
+GDK_AVAILABLE_IN_4_8
+GdkColorProfile *            gdk_color_profile_new_from_cicp              (int                   
color_primaries,
+                                                                           int                   
transfer_characteristics,
+                                                                           int                   
matrix_coefficients,
+                                                                           gboolean              full_range,
+                                                                           GError              **error);
+GDK_AVAILABLE_IN_4_8
+void                         gdk_color_profile_get_cicp_data              (GdkColorProfile      *self,
+                                                                           int                  
*color_primaries,
+                                                                           int                  
*transfer_characteristics,
+                                                                           int                  
*matrix_coefficients,
+                                                                           gboolean             *full_range);
+
 GDK_AVAILABLE_IN_4_8
 gboolean                     gdk_color_profile_is_linear                  (GdkColorProfile      *self) 
G_GNUC_PURE;
 
diff --git a/gdk/gdkcolorprofileprivate.h b/gdk/gdkcolorprofileprivate.h
index ab87ccde82..4c58f763a4 100644
--- a/gdk/gdkcolorprofileprivate.h
+++ b/gdk/gdkcolorprofileprivate.h
@@ -8,8 +8,10 @@
 
 G_BEGIN_DECLS
 
+GDK_AVAILABLE_IN_ALL
 GdkColorProfile *            gdk_color_profile_get_srgb_linear            (void) G_GNUC_CONST;
 
+GDK_AVAILABLE_IN_ALL
 GdkColorProfile *            gdk_color_profile_new_from_lcms_profile      (cmsHPROFILE           
lcms_profile,
                                                                            GError              **error);
 


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