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




commit e51dc6b00fae809569df94b7bb4ce917036279c8
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        | 132 +++++++++++++++++++++++++++++++++++++++++++
 gdk/gdkcolorprofile.h        |   8 ++-
 gdk/gdkcolorprofileprivate.h |   2 +
 3 files changed, 141 insertions(+), 1 deletion(-)
---
diff --git a/gdk/gdkcolorprofile.c b/gdk/gdkcolorprofile.c
index 3a200b251c..0191d84e8e 100644
--- a/gdk/gdkcolorprofile.c
+++ b/gdk/gdkcolorprofile.c
@@ -516,4 +516,136 @@ gdk_color_profile_lookup_transform (GdkColorProfile *source,
   return transform;
 }
 
+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 };
+
+  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)
+    return gdk_color_profile_new_from_lcms_profile (profile, error);
+
+  return NULL;
+}
diff --git a/gdk/gdkcolorprofile.h b/gdk/gdkcolorprofile.h
index a7b166e31f..57ef79ced7 100644
--- a/gdk/gdkcolorprofile.h
+++ b/gdk/gdkcolorprofile.h
@@ -50,10 +50,16 @@ 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
 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]