Add GdkColorState

This is just an empty shell for now.
This commit is contained in:
Matthias Clasen 2024-06-08 14:37:51 -04:00
parent 76a80d8f45
commit ae1e2a13d1
11 changed files with 1330 additions and 1 deletions

View File

@ -35,7 +35,11 @@ pacman --noconfirm -S --needed \
mingw-w64-$MSYS2_ARCH-python-gobject \
mingw-w64-$MSYS2_ARCH-shaderc \
mingw-w64-$MSYS2_ARCH-vulkan \
mingw-w64-$MSYS2_ARCH-vulkan-headers
mingw-w64-$MSYS2_ARCH-vulkan-headers \
mingw-w64-$MSYS2_ARCH-libpng \
mingw-w64-$MSYS2_ARCH-libjpeg-turbo \
mingw-w64-$MSYS2_ARCH-libtiff \
mingw-w64-$MSYS2_ARCH-lcms2
mkdir -p _ccache
export CCACHE_BASEDIR="$(pwd)"

View File

@ -30,6 +30,7 @@
#include <gdk/gdkcairo.h>
#include <gdk/gdkcairocontext.h>
#include <gdk/gdkclipboard.h>
#include <gdk/gdkcolorstate.h>
#include <gdk/gdkconfig.h>
#include <gdk/gdkcontentdeserializer.h>
#include <gdk/gdkcontentformats.h>

950
gdk/gdkcolorstate.c Normal file
View File

@ -0,0 +1,950 @@
/* gdkcolorstate.c
*
* Copyright 2024 Matthias Clasen
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include "gdkcolorstateprivate.h"
#include <glib/gi18n-lib.h>
#include "gtk/gtkcolorutilsprivate.h"
/**
* GdkColorState:
*
* A `GdkColorState` object provides the information to interpret
* colors and pixels in a variety of ways.
*
* They are also known as
* [*color spaces*](https://en.wikipedia.org/wiki/Color_space).
*
* Crucially, GTK knows how to convert colors from one color
* state to another.
*
* `GdkColorState objects are immutable and therefore threadsafe.
*
* Since 4.16
*/
static GdkColorStateClass * get_class (GdkColorState *self);
G_DEFINE_BOXED_TYPE (GdkColorState, gdk_color_state,
gdk_color_state_ref, gdk_color_state_unref);
/* {{{ Public API */
GdkColorState *
(gdk_color_state_ref) (GdkColorState *self)
{
return _gdk_color_state_ref (self);
}
void
(gdk_color_state_unref) (GdkColorState *self)
{
_gdk_color_state_unref (self);
}
GdkColorState *
(gdk_color_state_get_srgb) (void)
{
return _gdk_color_state_get_srgb ();
}
GdkColorState *
(gdk_color_state_get_srgb_linear) (void)
{
return _gdk_color_state_get_srgb_linear ();
}
GdkColorState *
(gdk_color_state_get_hsl) (void)
{
return _gdk_color_state_get_hsl ();
}
GdkColorState *
(gdk_color_state_get_hwb) (void)
{
return _gdk_color_state_get_hwb ();
}
GdkColorState *
(gdk_color_state_get_oklab) (void)
{
return _gdk_color_state_get_oklab ();
}
GdkColorState *
(gdk_color_state_get_oklch) (void)
{
return _gdk_color_state_get_oklch ();
}
GdkColorState *
(gdk_color_state_get_display_p3) (void)
{
return _gdk_color_state_get_display_p3 ();
}
GdkColorState *
(gdk_color_state_get_xyz) (void)
{
return _gdk_color_state_get_xyz ();
}
GdkColorState *
(gdk_color_state_get_rec2020) (void)
{
return _gdk_color_state_get_rec2020 ();
}
GdkColorState *
(gdk_color_state_get_rec2100_pq) (void)
{
return _gdk_color_state_get_rec2100_pq ();
}
GdkColorState *
(gdk_color_state_get_rec2100_linear) (void)
{
return _gdk_color_state_get_rec2100_linear ();
}
/**
* gdk_color_state_equal:
* @self: a `GdkColorState`
* @other: another `GdkColorStatee`
*
* Compares two `GdkColorStates` for equality.
*
* Note that this function is not guaranteed to be perfect and two objects
* describing the same color state may compare not equal. However, different
* color states will never compare equal.
*
* Returns: %TRUE if the two color states compare equal
*
* Since: 4.16
*/
gboolean
(gdk_color_state_equal) (GdkColorState *self,
GdkColorState *other)
{
return _gdk_color_state_equal (self, other);
}
/**
* gdk_color_state_is_linear:
* @self: a `GdkColorState`
*
* Returns whether the color state is linear.
*
* Returns: `TRUE` if the color state is linear
* Since: 4.16
*/
gboolean
gdk_color_state_is_linear (GdkColorState *self)
{
return get_class (self)->is_linear (self);
}
/**
* gdk_color_state_save_to_icc_profile:
* @self: a `GdkColorState`
* @error: Return location for an error
*
* Saves the color state to an
* [ICC profile](https://en.wikipedia.org/wiki/ICC_profile).
*
* Some color states cannot be represented as ICC profiles.
* In that case, @error will be set and %NULL will be returned.
*
* Returns: (nullable): A new `GBytes` containing the ICC profile
*
* Since: 4.16
*/
GBytes *
gdk_color_state_save_to_icc_profile (GdkColorState *self,
GError **error)
{
return get_class (self)->save_to_icc_profile (self, error);
}
/**
* gdk_color_state_save_to_cicp_data:
* @self: a `GdkColorState`
* @color_primaries: return location for color primaries
* @transfer_characteristics: return location for transfer characteristics
* @matrix_coefficients: return location for matrix_coefficients
* @full_range: return location for the full range flag
* @error: Return location for an error
*
* Saves the color state as
* [CICP](https://en.wikipedia.org/wiki/Coding-independent_code_points)
* data.
*
* Some color states cannot be represented as CICP data.
* In that case, @error will be set and `FALSE` will be returned.
*
* Returns: (nullable): `TRUE` if the out arguments were set
*
* Since: 4.16
*/
gboolean
gdk_color_state_save_to_cicp_data (GdkColorState *self,
int *color_primaries,
int *transfer_characteristics,
int *matrix_coefficients,
gboolean *full_range,
GError **error)
{
return get_class (self)->save_to_cicp_data (self,
color_primaries,
transfer_characteristics,
matrix_coefficients,
full_range,
error);
}
/* }}} */
/* {{{ ICC implementation */
#define GDK_LCMS_COLOR_STATE(c) ((GdkLcmsColorState *)(c))
typedef struct {
GdkColorState state;
cmsHPROFILE lcms_profile;
} GdkLcmsColorState;
/* {{{ Helpers */
static cmsHPROFILE
gdk_lcms_color_state_get_lcms_profile (GdkColorState *self)
{
g_return_val_if_fail (GDK_IS_LCMS_COLOR_STATE (self), NULL);
return GDK_LCMS_COLOR_STATE (self)->lcms_profile;
}
static cmsHPROFILE
gdk_lcms_get_srgb_profile (void)
{
return cmsCreate_sRGBProfile ();
}
static cmsHPROFILE
gdk_lcms_get_srgb_linear_profile (void)
{
cmsToneCurve *curve;
cmsHPROFILE profile;
curve = cmsBuildGamma (NULL, 1.0);
profile = cmsCreateRGBProfile (&(cmsCIExyY) {
0.3127, 0.3290, 1.0
},
&(cmsCIExyYTRIPLE) {
{ 0.6400, 0.3300, 1.0 },
{ 0.3000, 0.6000, 1.0 },
{ 0.1500, 0.0600, 1.0 }
},
(cmsToneCurve*[3]) { curve, curve, curve });
cmsFreeToneCurve (curve);
return profile;
}
static GBytes *
gdk_lcms_save_profile (cmsHPROFILE profile,
GError **error)
{
cmsUInt32Number size;
guchar *data;
size = 0;
if (!cmsSaveProfileToMem (profile, NULL, &size))
{
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, _("Could not prepare ICC profile"));
return NULL;
}
data = g_malloc (size);
if (!cmsSaveProfileToMem (profile, data, &size))
{
g_free (data);
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, _("Failed to save ICC profile"));
return NULL;
}
return g_bytes_new_take (data, size);
}
/* }}} */
/* {{{ GdkColorState vfuncs */
static void
gdk_lcms_color_state_free (GdkColorState *state)
{
GdkLcmsColorState *self = (GdkLcmsColorState *) state;
cmsCloseProfile (self->lcms_profile);
g_free (self);
}
static gboolean
gdk_lcms_color_state_equal (GdkColorState *state1,
GdkColorState *state2)
{
GBytes *icc1, *icc2;
gboolean res;
icc1 = gdk_color_state_save_to_icc_profile (state1, NULL);
icc2 = gdk_color_state_save_to_icc_profile (state2, NULL);
res = g_bytes_equal (icc1, icc2);
g_bytes_unref (icc1);
g_bytes_unref (icc2);
return res;
}
static gboolean
gdk_lcms_color_state_is_linear (GdkColorState *state)
{
return FALSE; /* FIXME */
}
static GBytes *
gdk_lcms_color_state_save_to_icc_profile (GdkColorState *state,
GError **error)
{
GdkLcmsColorState *self = GDK_LCMS_COLOR_STATE (state);
return gdk_lcms_save_profile (self->lcms_profile, error);
}
static gboolean
gdk_lcms_color_state_save_to_cicp_data (GdkColorState *self,
int *color_primaries,
int *transfer_characteristics,
int *matrix_coefficients,
gboolean *full_range,
GError **error)
{
g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
_("This color state does not support CICP data"));
return FALSE;
}
static const char *
gdk_lcms_color_state_get_name (GdkColorState *self)
{
static char buffer[48];
g_snprintf (buffer, sizeof (buffer), "lcms color state %p", self);
return buffer;
}
static guint
gdk_lcms_color_state_get_min_depth (GdkColorState *self)
{
return GDK_MEMORY_U16; /* ? */
}
static int
gdk_lcms_color_state_get_hue_coord (GdkColorState *self)
{
return -1;
}
static GdkColorStateClass LCMS_COLOR_STATE_CLASS =
{
GDK_TYPE_LCMS_COLOR_STATE,
gdk_lcms_color_state_free,
gdk_lcms_color_state_equal,
gdk_lcms_color_state_is_linear,
gdk_lcms_color_state_save_to_icc_profile,
gdk_lcms_color_state_save_to_cicp_data,
gdk_lcms_color_state_get_name,
gdk_lcms_color_state_get_min_depth,
gdk_lcms_color_state_get_hue_coord,
};
/* }}} */
/* {{{ Private API */
GdkColorState *
gdk_color_state_new_from_lcms_profile (cmsHPROFILE lcms_profile)
{
GdkLcmsColorState *self;
self= g_new0 (GdkLcmsColorState, 1);
self->state.klass = &LCMS_COLOR_STATE_CLASS;
self->state.ref_count = 1;
self->lcms_profile = lcms_profile;
return (GdkColorState *) self;
}
/* }}} */
/* {{{ Public API */
/**
* gdk_color_state_new_from_icc_profile:
* @icc_profile: The ICC profiles given as a `GBytes`
* @error: Return location for an error
*
* Creates a new color state for the given ICC profile data.
*
* if the profile is not valid, %NULL is returned and an error
* is raised.
*
* Returns: a new `GdkColorState` or %NULL on error
*
* Since: 4.16
*/
GdkColorState *
gdk_color_state_new_from_icc_profile (GBytes *icc_profile,
GError **error)
{
cmsHPROFILE lcms_profile;
const guchar *data;
gsize size;
g_return_val_if_fail (icc_profile != NULL, NULL);
g_return_val_if_fail (error == NULL || *error == NULL, NULL);
data = g_bytes_get_data (icc_profile, &size);
lcms_profile = cmsOpenProfileFromMem (data, size);
if (lcms_profile == NULL)
{
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, _("Failed to load ICC profile"));
return NULL;
}
return gdk_color_state_new_from_lcms_profile (lcms_profile);
}
/* }}} */
/* }}} */
/* {{{ Named implementation */
static gboolean
gdk_named_color_state_is_linear (GdkColorState *self)
{
switch (GDK_NAMED_COLOR_STATE_ID (self))
{
case GDK_COLOR_STATE_ID_SRGB_LINEAR:
case GDK_COLOR_STATE_ID_OKLAB:
case GDK_COLOR_STATE_ID_OKLCH:
case GDK_COLOR_STATE_ID_XYZ:
case GDK_COLOR_STATE_ID_REC2100_LINEAR:
return TRUE;
case GDK_COLOR_STATE_ID_SRGB:
case GDK_COLOR_STATE_ID_HSL:
case GDK_COLOR_STATE_ID_HWB:
case GDK_COLOR_STATE_ID_DISPLAY_P3:
case GDK_COLOR_STATE_ID_REC2020:
case GDK_COLOR_STATE_ID_REC2100_PQ:
return FALSE;
default:
g_assert_not_reached ();
}
}
static GBytes *
gdk_named_color_state_save_to_icc_profile (GdkColorState *self,
GError **error)
{
cmsHPROFILE profile;
switch (GDK_NAMED_COLOR_STATE_ID (self))
{
case GDK_COLOR_STATE_ID_SRGB:
profile = gdk_lcms_get_srgb_profile ();
break;
case GDK_COLOR_STATE_ID_SRGB_LINEAR:
profile = gdk_lcms_get_srgb_linear_profile ();
break;
case GDK_COLOR_STATE_ID_OKLAB:
case GDK_COLOR_STATE_ID_OKLCH:
case GDK_COLOR_STATE_ID_HSL:
case GDK_COLOR_STATE_ID_HWB:
case GDK_COLOR_STATE_ID_DISPLAY_P3:
case GDK_COLOR_STATE_ID_XYZ:
case GDK_COLOR_STATE_ID_REC2020:
case GDK_COLOR_STATE_ID_REC2100_PQ:
case GDK_COLOR_STATE_ID_REC2100_LINEAR:
profile = NULL;
break;
default:
g_assert_not_reached ();
}
if (profile)
{
GBytes *bytes;
bytes = gdk_lcms_save_profile (profile, error);
cmsCloseProfile (profile);
return bytes;
}
else
{
g_set_error (error,
G_IO_ERROR, G_IO_ERROR_FAILED,
_("ICC profile not supported for this color state"));
return NULL;
}
}
static gboolean
gdk_named_color_state_save_to_cicp_data (GdkColorState *self,
int *color_primaries,
int *transfer_characteristics,
int *matrix_coefficients,
gboolean *full_range,
GError **error)
{
switch (GDK_NAMED_COLOR_STATE_ID (self))
{
case GDK_COLOR_STATE_ID_SRGB:
*color_primaries = 0;
*transfer_characteristics = 13;
*matrix_coefficients = 0;
*full_range = TRUE;
return TRUE;
case GDK_COLOR_STATE_ID_SRGB_LINEAR:
*color_primaries = 0;
*transfer_characteristics = 8;
*matrix_coefficients = 0;
*full_range = TRUE;
return TRUE;
case GDK_COLOR_STATE_ID_REC2100_PQ:
*color_primaries = 9;
*transfer_characteristics = 16;
*matrix_coefficients = 0;
*full_range = TRUE;
return TRUE;
case GDK_COLOR_STATE_ID_REC2100_LINEAR:
*color_primaries = 9;
*transfer_characteristics = 8;
*matrix_coefficients = 0;
*full_range = TRUE;
return TRUE;
case GDK_COLOR_STATE_ID_DISPLAY_P3:
*color_primaries = 12;
*transfer_characteristics = 13;
*matrix_coefficients = 0;
*full_range = TRUE;
return TRUE;
case GDK_COLOR_STATE_ID_HSL:
case GDK_COLOR_STATE_ID_HWB:
case GDK_COLOR_STATE_ID_OKLAB:
case GDK_COLOR_STATE_ID_OKLCH:
case GDK_COLOR_STATE_ID_XYZ:
case GDK_COLOR_STATE_ID_REC2020:
break;
default:
g_assert_not_reached ();
}
g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
_("This color state does not support CICP data"));
return FALSE;
}
const char *
gdk_color_state_get_name_from_id (GdkColorStateId id)
{
const char *names[] = {
"srgb", "srgb-linear", "hsl", "hwb",
"oklab", "oklch", "display-p3", "xyz",
"rec2020", "rec2100-pq", "rec2100-linear",
};
return names[(id - 1) >> 1];
}
static const char *
gdk_named_color_state_get_name (GdkColorState *self)
{
return gdk_color_state_get_name_from_id (GDK_NAMED_COLOR_STATE_ID (self));
}
static guint
gdk_named_color_state_get_min_depth (GdkColorState *self)
{
switch (GDK_NAMED_COLOR_STATE_ID (self))
{
case GDK_COLOR_STATE_ID_OKLAB:
case GDK_COLOR_STATE_ID_OKLCH:
case GDK_COLOR_STATE_ID_HSL:
case GDK_COLOR_STATE_ID_HWB:
case GDK_COLOR_STATE_ID_DISPLAY_P3:
case GDK_COLOR_STATE_ID_XYZ:
case GDK_COLOR_STATE_ID_REC2020:
case GDK_COLOR_STATE_ID_REC2100_PQ:
case GDK_COLOR_STATE_ID_REC2100_LINEAR:
case GDK_COLOR_STATE_ID_SRGB_LINEAR:
return GDK_MEMORY_FLOAT16;
case GDK_COLOR_STATE_ID_SRGB:
return GDK_MEMORY_U8;
default:
g_assert_not_reached ();
}
}
static int
gdk_named_color_state_get_hue_coord (GdkColorState *in)
{
switch (GDK_NAMED_COLOR_STATE_ID (in))
{
case GDK_COLOR_STATE_ID_SRGB:
case GDK_COLOR_STATE_ID_SRGB_LINEAR:
case GDK_COLOR_STATE_ID_OKLAB:
case GDK_COLOR_STATE_ID_DISPLAY_P3:
case GDK_COLOR_STATE_ID_XYZ:
case GDK_COLOR_STATE_ID_REC2020:
case GDK_COLOR_STATE_ID_REC2100_PQ:
case GDK_COLOR_STATE_ID_REC2100_LINEAR:
return -1;
case GDK_COLOR_STATE_ID_HSL:
case GDK_COLOR_STATE_ID_HWB:
return 0;
case GDK_COLOR_STATE_ID_OKLCH:
return 2;
default:
g_assert_not_reached ();
}
}
static GdkColorStateClass NAMED_COLOR_STATE_CLASS =
{
GDK_TYPE_NAMED_COLOR_STATE,
NULL,
NULL,
gdk_named_color_state_is_linear,
gdk_named_color_state_save_to_icc_profile,
gdk_named_color_state_save_to_cicp_data,
gdk_named_color_state_get_name,
gdk_named_color_state_get_min_depth,
gdk_named_color_state_get_hue_coord,
};
/* }}} */
/* {{{ CICP support */
GdkColorState *
gdk_color_state_new_from_cicp_data (int color_primaries,
int transfer_characteristics,
int matrix_coefficients,
gboolean full_range)
{
/* FIXME We don't support these */
if (!full_range || matrix_coefficients != 0)
return NULL;
/* Look for cases we can handle */
if (color_primaries == 0 && transfer_characteristics == 13)
return gdk_color_state_get_srgb ();
else if (color_primaries == 0 && transfer_characteristics == 8)
return gdk_color_state_get_srgb_linear ();
else if (color_primaries == 9 && transfer_characteristics == 16)
return gdk_color_state_get_rec2100_pq ();
else if (color_primaries == 9 && transfer_characteristics == 8)
return gdk_color_state_get_rec2100_linear ();
else if (color_primaries == 12 && transfer_characteristics == 13)
return gdk_color_state_get_display_p3 ();
return NULL;
}
/* }}} */
/* {{{ Private API */
static GdkColorStateClass *
get_class (GdkColorState *self)
{
if (GDK_IS_NAMED_COLOR_STATE (self))
return &NAMED_COLOR_STATE_CLASS;
return self->klass;
}
GdkMemoryDepth
gdk_color_state_get_min_depth (GdkColorState *self)
{
return (GdkMemoryDepth) get_class (self)->get_min_depth (self);
}
const char *
gdk_color_state_get_name (GdkColorState *self)
{
return get_class (self)->get_name (self);
}
int
gdk_color_state_get_hue_coord (GdkColorState *self)
{
return get_class (self)->get_hue_coord (self);
}
/* }}} */
/* {{{ Conversion */
#define two_step(name, func1, func2) \
static void \
name (float r, float g, float b, \
float *x, float *y, float *z) \
{ \
func1 (r, g, b, x, y, z); \
func2 (*x, *y, *z, x, y, z); \
}
/* A full complement of xyz <> anything step functions */
two_step (srgb_to_xyz, gtk_rgb_to_linear_srgb, gtk_linear_srgb_to_xyz)
two_step (xyz_to_srgb, gtk_xyz_to_linear_srgb, gtk_linear_srgb_to_rgb)
two_step (hsl_to_xyz, gtk_hsl_to_rgb, srgb_to_xyz)
two_step (xyz_to_hsl, xyz_to_srgb, gtk_rgb_to_hsl)
two_step (hwb_to_xyz, gtk_hwb_to_rgb, srgb_to_xyz)
two_step (xyz_to_hwb, xyz_to_srgb, gtk_rgb_to_hwb)
two_step (oklab_to_xyz, gtk_oklab_to_linear_srgb, gtk_linear_srgb_to_xyz)
two_step (xyz_to_oklab, gtk_xyz_to_linear_srgb, gtk_linear_srgb_to_oklab)
two_step (oklch_to_xyz, gtk_oklch_to_oklab, oklab_to_xyz)
two_step (xyz_to_oklch, xyz_to_oklab, gtk_oklab_to_oklch)
two_step (p3_to_xyz, gtk_p3_to_rgb, srgb_to_xyz)
two_step (xyz_to_p3, xyz_to_srgb, gtk_rgb_to_p3)
two_step (rec2100_linear_to_xyz, gtk_rec2100_linear_to_rec2020_linear, gtk_rec2020_linear_to_xyz)
two_step (xyz_to_rec2100_linear, gtk_xyz_to_rec2020_linear, gtk_rec2020_linear_to_rec2100_linear)
two_step (rec2100_pq_to_xyz, gtk_rec2100_pq_to_rec2100_linear, rec2100_linear_to_xyz)
two_step (xyz_to_rec2100_pq, xyz_to_rec2100_linear, gtk_rec2100_linear_to_rec2100_pq)
static struct {
GdkColorStateId n1;
GdkColorStateId n2;
StepFunc func;
} functions[] = {
{ GDK_COLOR_STATE_ID_SRGB, GDK_COLOR_STATE_ID_SRGB_LINEAR, gtk_rgb_to_linear_srgb },
{ GDK_COLOR_STATE_ID_SRGB, GDK_COLOR_STATE_ID_HSL, gtk_rgb_to_hsl },
{ GDK_COLOR_STATE_ID_SRGB, GDK_COLOR_STATE_ID_HWB, gtk_rgb_to_hwb },
{ GDK_COLOR_STATE_ID_SRGB, GDK_COLOR_STATE_ID_OKLAB, gtk_rgb_to_oklab },
{ GDK_COLOR_STATE_ID_SRGB, GDK_COLOR_STATE_ID_DISPLAY_P3, gtk_rgb_to_p3 },
{ GDK_COLOR_STATE_ID_SRGB, GDK_COLOR_STATE_ID_XYZ, srgb_to_xyz },
{ GDK_COLOR_STATE_ID_SRGB_LINEAR, GDK_COLOR_STATE_ID_SRGB, gtk_linear_srgb_to_rgb },
{ GDK_COLOR_STATE_ID_SRGB_LINEAR, GDK_COLOR_STATE_ID_OKLAB, gtk_linear_srgb_to_oklab },
{ GDK_COLOR_STATE_ID_SRGB_LINEAR, GDK_COLOR_STATE_ID_XYZ, gtk_linear_srgb_to_xyz },
{ GDK_COLOR_STATE_ID_HSL, GDK_COLOR_STATE_ID_SRGB, gtk_hsl_to_rgb },
{ GDK_COLOR_STATE_ID_HSL, GDK_COLOR_STATE_ID_XYZ, hsl_to_xyz },
{ GDK_COLOR_STATE_ID_HWB, GDK_COLOR_STATE_ID_SRGB, gtk_hwb_to_rgb },
{ GDK_COLOR_STATE_ID_HWB, GDK_COLOR_STATE_ID_XYZ, hwb_to_xyz },
{ GDK_COLOR_STATE_ID_OKLAB, GDK_COLOR_STATE_ID_SRGB, gtk_oklab_to_rgb },
{ GDK_COLOR_STATE_ID_OKLAB, GDK_COLOR_STATE_ID_SRGB_LINEAR, gtk_oklab_to_linear_srgb },
{ GDK_COLOR_STATE_ID_OKLAB, GDK_COLOR_STATE_ID_OKLCH, gtk_oklab_to_oklch },
{ GDK_COLOR_STATE_ID_OKLAB, GDK_COLOR_STATE_ID_XYZ, oklab_to_xyz },
{ GDK_COLOR_STATE_ID_OKLCH, GDK_COLOR_STATE_ID_OKLAB, gtk_oklch_to_oklab },
{ GDK_COLOR_STATE_ID_OKLCH, GDK_COLOR_STATE_ID_XYZ, oklch_to_xyz },
{ GDK_COLOR_STATE_ID_DISPLAY_P3, GDK_COLOR_STATE_ID_SRGB, gtk_p3_to_rgb },
{ GDK_COLOR_STATE_ID_DISPLAY_P3, GDK_COLOR_STATE_ID_XYZ, p3_to_xyz },
{ GDK_COLOR_STATE_ID_XYZ, GDK_COLOR_STATE_ID_SRGB, xyz_to_srgb },
{ GDK_COLOR_STATE_ID_XYZ, GDK_COLOR_STATE_ID_SRGB_LINEAR, gtk_xyz_to_linear_srgb },
{ GDK_COLOR_STATE_ID_XYZ, GDK_COLOR_STATE_ID_HSL, xyz_to_hsl },
{ GDK_COLOR_STATE_ID_XYZ, GDK_COLOR_STATE_ID_HWB, xyz_to_hwb },
{ GDK_COLOR_STATE_ID_XYZ, GDK_COLOR_STATE_ID_OKLAB, xyz_to_oklab },
{ GDK_COLOR_STATE_ID_XYZ, GDK_COLOR_STATE_ID_OKLCH, xyz_to_oklch },
{ GDK_COLOR_STATE_ID_XYZ, GDK_COLOR_STATE_ID_DISPLAY_P3, xyz_to_p3 },
{ GDK_COLOR_STATE_ID_XYZ, GDK_COLOR_STATE_ID_REC2020, gtk_xyz_to_rec2020 },
{ GDK_COLOR_STATE_ID_XYZ, GDK_COLOR_STATE_ID_REC2100_PQ, xyz_to_rec2100_pq },
{ GDK_COLOR_STATE_ID_XYZ, GDK_COLOR_STATE_ID_REC2100_LINEAR, xyz_to_rec2100_linear },
{ GDK_COLOR_STATE_ID_REC2020, GDK_COLOR_STATE_ID_XYZ, gtk_rec2020_to_xyz },
{ GDK_COLOR_STATE_ID_REC2100_PQ, GDK_COLOR_STATE_ID_XYZ, rec2100_pq_to_xyz },
{ GDK_COLOR_STATE_ID_REC2100_PQ, GDK_COLOR_STATE_ID_REC2100_LINEAR, gtk_rec2100_pq_to_rec2100_linear },
{ GDK_COLOR_STATE_ID_REC2100_LINEAR, GDK_COLOR_STATE_ID_REC2100_PQ, gtk_rec2100_linear_to_rec2100_pq },
{ GDK_COLOR_STATE_ID_REC2100_LINEAR, GDK_COLOR_STATE_ID_XYZ, rec2100_linear_to_xyz },
};
static StepFunc
find_function (GdkColorStateId n1, GdkColorStateId n2)
{
for (int k = 0; k < G_N_ELEMENTS (functions); k++)
{
if (functions[k].n1 == n1 && functions[k].n2 == n2)
return functions[k].func;
}
return NULL;
}
void
gdk_color_state_transform_init (GdkColorStateTransform *tf,
GdkColorState *from,
GdkColorState *to,
gboolean copy_alpha)
{
memset (tf, 0, sizeof (GdkColorStateTransform));
if (GDK_IS_NAMED_COLOR_STATE (from) &&
GDK_IS_NAMED_COLOR_STATE (to))
{
tf->step1 = find_function (GDK_NAMED_COLOR_STATE_ID (from),
GDK_NAMED_COLOR_STATE_ID (to));
if (tf->step1 == NULL && from != to)
{
tf->step1 = find_function (GDK_NAMED_COLOR_STATE_ID (from),
GDK_COLOR_STATE_ID_XYZ);
tf->step2 = find_function (GDK_COLOR_STATE_ID_XYZ,
GDK_NAMED_COLOR_STATE_ID (to));
}
}
else
{
cmsHPROFILE profile1, profile2;
if (GDK_IS_LCMS_COLOR_STATE (from))
profile1 = gdk_lcms_color_state_get_lcms_profile (from);
else
profile1 = NULL;
if (GDK_IS_LCMS_COLOR_STATE (to))
profile2 = gdk_lcms_color_state_get_lcms_profile (to);
else
profile2 = NULL;
if (profile1 && profile2)
{
tf->transform = cmsCreateTransform (profile1,
TYPE_RGBA_FLT,
profile2,
TYPE_RGBA_FLT,
INTENT_PERCEPTUAL,
copy_alpha ? cmsFLAGS_COPY_ALPHA : 0);
}
else if (GDK_IS_NAMED_COLOR_STATE (from) && profile2)
{
tf->step1 = find_function (GDK_NAMED_COLOR_STATE_ID (from),
GDK_COLOR_STATE_ID_XYZ);
profile1 = cmsCreateXYZProfile ();
tf->transform = cmsCreateTransform (profile1,
TYPE_RGBA_FLT,
profile2,
TYPE_RGBA_FLT,
INTENT_PERCEPTUAL,
copy_alpha ? cmsFLAGS_COPY_ALPHA : 0);
tf->cms_first = FALSE;
}
else if (profile1 && GDK_IS_NAMED_COLOR_STATE (to))
{
profile2 = cmsCreateXYZProfile ();
tf->transform = cmsCreateTransform (profile1,
TYPE_RGBA_FLT,
profile2,
TYPE_RGBA_FLT,
INTENT_PERCEPTUAL,
copy_alpha ? cmsFLAGS_COPY_ALPHA : 0);
tf->step1 = find_function (GDK_COLOR_STATE_ID_XYZ,
GDK_NAMED_COLOR_STATE_ID (to));
tf->cms_first = TRUE;
}
else
g_assert_not_reached ();
g_clear_pointer (&profile1, cmsCloseProfile);
g_clear_pointer (&profile2, cmsCloseProfile);
}
tf->copy_alpha = copy_alpha;
}
void
gdk_color_state_transform_finish (GdkColorStateTransform *tf)
{
if (tf->transform)
cmsDeleteTransform (tf->transform);
}
void
gdk_color_state_transform (GdkColorStateTransform *tf,
const float *src,
float *dst,
int width)
{
if (tf->copy_alpha)
memcpy (dst, src, sizeof (float) * 4 * width);
else
{
for (int i = 0; i < width * 4; i += 4)
{
dst[i] = src[i];
dst[i + 1] = src[i + 1];
dst[i + 2] = src[i + 2];
}
}
if (tf->cms_first && tf->transform)
cmsDoTransform (tf->transform, dst, dst, width);
if (tf->step1 && tf->step2)
{
for (int i = 0; i < width * 4; i += 4)
{
tf->step1 (dst[i], dst[i+1], dst[i+2], &dst[i], &dst[i+1], &dst[i+2]);
tf->step2 (dst[i], dst[i+1], dst[i+2], &dst[i], &dst[i+1], &dst[i+2]);
}
}
else if (tf->step1)
{
for (int i = 0; i < width * 4; i += 4)
tf->step1 (dst[i], dst[i+1], dst[i+2], &dst[i], &dst[i+1], &dst[i+2]);
}
if (!tf->cms_first && tf->transform)
cmsDoTransform (tf->transform, dst, dst, width);
}
/* }}} */
/* vim:set foldmethod=marker expandtab: */

104
gdk/gdkcolorstate.h Normal file
View File

@ -0,0 +1,104 @@
/* gdkcolorstate.h
*
* Copyright 2024 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#if !defined (__GDK_H_INSIDE__) && !defined (GTK_COMPILATION)
#error "Only <gdk/gdk.h> can be included directly."
#endif
#include <gdk/gdktypes.h>
G_BEGIN_DECLS
#define GDK_TYPE_COLOR_STATE (gdk_color_state_get_type ())
GDK_AVAILABLE_IN_4_16
GType gdk_color_state_get_type (void) G_GNUC_CONST;
GDK_AVAILABLE_IN_4_16
GdkColorState * gdk_color_state_ref (GdkColorState *self);
GDK_AVAILABLE_IN_4_16
void gdk_color_state_unref (GdkColorState *self);
GDK_AVAILABLE_IN_4_16
GdkColorState * gdk_color_state_get_srgb (void);
GDK_AVAILABLE_IN_4_16
GdkColorState * gdk_color_state_get_srgb_linear (void);
GDK_AVAILABLE_IN_4_16
GdkColorState * gdk_color_state_get_hsl (void);
GDK_AVAILABLE_IN_4_16
GdkColorState * gdk_color_state_get_hwb (void);
GDK_AVAILABLE_IN_4_16
GdkColorState * gdk_color_state_get_oklab (void);
GDK_AVAILABLE_IN_4_16
GdkColorState * gdk_color_state_get_oklch (void);
GDK_AVAILABLE_IN_4_16
GdkColorState * gdk_color_state_get_display_p3 (void);
GDK_AVAILABLE_IN_4_16
GdkColorState * gdk_color_state_get_xyz (void);
GDK_AVAILABLE_IN_4_16
GdkColorState * gdk_color_state_get_rec2020 (void);
GDK_AVAILABLE_IN_4_16
GdkColorState * gdk_color_state_get_rec2100_pq (void);
GDK_AVAILABLE_IN_4_16
GdkColorState * gdk_color_state_get_rec2100_linear (void);
GDK_AVAILABLE_IN_4_16
gboolean gdk_color_state_equal (GdkColorState *self,
GdkColorState *other);
GDK_AVAILABLE_IN_4_16
gboolean gdk_color_state_is_linear (GdkColorState *self);
GDK_AVAILABLE_IN_4_16
GdkColorState * gdk_color_state_new_from_icc_profile (GBytes *icc_profile,
GError **error);
GDK_AVAILABLE_IN_4_16
GdkColorState * gdk_color_state_new_from_cicp_data (int color_primaries,
int transfer_characteristics,
int matrix_coefficients,
gboolean full_range);
GDK_AVAILABLE_IN_4_16
GBytes * gdk_color_state_save_to_icc_profile (GdkColorState *self,
GError **error);
GDK_AVAILABLE_IN_4_16
gboolean gdk_color_state_save_to_cicp_data (GdkColorState *self,
int *color_primaries,
int *transfer_characteristic,
int *matrix_coefficients,
gboolean *full_range,
GError **error);
#include "gdkcolorstateimpl.h"
G_END_DECLS

204
gdk/gdkcolorstateimpl.h Normal file
View File

@ -0,0 +1,204 @@
/* gdkcolorstateimpl.h
*
* Copyright 2024 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
typedef enum
{
GDK_COLOR_STATE_ID_SRGB = 1,
GDK_COLOR_STATE_ID_SRGB_LINEAR = 3,
GDK_COLOR_STATE_ID_HSL = 5,
GDK_COLOR_STATE_ID_HWB = 7,
GDK_COLOR_STATE_ID_OKLAB = 9,
GDK_COLOR_STATE_ID_OKLCH = 11,
GDK_COLOR_STATE_ID_DISPLAY_P3 = 13,
GDK_COLOR_STATE_ID_XYZ = 15,
GDK_COLOR_STATE_ID_REC2020 = 17,
GDK_COLOR_STATE_ID_REC2100_PQ = 19,
GDK_COLOR_STATE_ID_REC2100_LINEAR = 21,
} GdkColorStateId;
enum
{
GDK_TYPE_NAMED_COLOR_STATE,
GDK_TYPE_LCMS_COLOR_STATE,
};
typedef struct _GdkColorStateClass GdkColorStateClass;
struct _GdkColorState
{
GdkColorStateClass *klass;
int ref_count;
};
struct _GdkColorStateClass
{
int type;
void (* free) (GdkColorState *self);
gboolean (* equal) (GdkColorState *self,
GdkColorState *other);
gboolean (* is_linear) (GdkColorState *self);
GBytes * (* save_to_icc_profile) (GdkColorState *self,
GError **error);
gboolean (* save_to_cicp_data) (GdkColorState *self,
int *color_primaries,
int *transfer_characteristics,
int *matrix_coefficients,
gboolean *full_range,
GError **error);
const char * (* get_name) (GdkColorState *self);
guint (* get_min_depth) (GdkColorState *self);
int (* get_hue_coord) (GdkColorState *self);
};
#define GDK_NAMED_COLOR_STATE_ID(c) ((GdkColorStateId) (GPOINTER_TO_INT (c)))
#define GDK_IS_NAMED_COLOR_STATE(c) ((GPOINTER_TO_INT(c)) & 1)
#define GDK_IS_LCMS_COLOR_STATE(c) ((c)->klass->type == GDK_TYPE_LCMS_COLOR_STATE)
#define gdk_color_state_ref(self) _gdk_color_state_ref (self)
static inline GdkColorState *
_gdk_color_state_ref (GdkColorState *self)
{
if (GDK_IS_NAMED_COLOR_STATE (self))
return self;
self->ref_count++;
return self;
}
#define gdk_color_state_unref(self) _gdk_color_state_unref (self)
static inline void
_gdk_color_state_unref (GdkColorState *self)
{
if (GDK_IS_NAMED_COLOR_STATE (self))
return;
self->ref_count--;
if (self->ref_count == 0)
self->klass->free (self);
}
#define GDK_COLOR_STATE_SRGB ((GdkColorState *) GINT_TO_POINTER (GDK_COLOR_STATE_ID_SRGB))
#define GDK_COLOR_STATE_SRGB_LINEAR ((GdkColorState *) GINT_TO_POINTER (GDK_COLOR_STATE_ID_SRGB_LINEAR))
#define GDK_COLOR_STATE_HSL ((GdkColorState *) GINT_TO_POINTER (GDK_COLOR_STATE_ID_HSL))
#define GDK_COLOR_STATE_HWB ((GdkColorState *) GINT_TO_POINTER (GDK_COLOR_STATE_ID_HWB))
#define GDK_COLOR_STATE_OKLAB ((GdkColorState *) GINT_TO_POINTER (GDK_COLOR_STATE_ID_OKLAB))
#define GDK_COLOR_STATE_OKLCH ((GdkColorState *) GINT_TO_POINTER (GDK_COLOR_STATE_ID_OKLCH))
#define GDK_COLOR_STATE_DISPLAY_P3 ((GdkColorState *) GINT_TO_POINTER (GDK_COLOR_STATE_ID_DISPLAY_P3))
#define GDK_COLOR_STATE_XYZ ((GdkColorState *) GINT_TO_POINTER (GDK_COLOR_STATE_ID_XYZ))
#define GDK_COLOR_STATE_REC2020 ((GdkColorState *) GINT_TO_POINTER (GDK_COLOR_STATE_ID_REC2020))
#define GDK_COLOR_STATE_REC2100_PQ ((GdkColorState *) GINT_TO_POINTER (GDK_COLOR_STATE_ID_REC2100_PQ))
#define GDK_COLOR_STATE_REC2100_LINEAR ((GdkColorState *) GINT_TO_POINTER (GDK_COLOR_STATE_ID_REC2100_LINEAR))
#define gdk_color_state_get_srgb() _gdk_color_state_get_srgb ()
static inline GdkColorState *
_gdk_color_state_get_srgb (void)
{
return GDK_COLOR_STATE_SRGB;
}
#define gdk_color_state_get_srgb_linear() _gdk_color_state_get_srgb_linear ()
static inline GdkColorState *
_gdk_color_state_get_srgb_linear (void)
{
return GDK_COLOR_STATE_SRGB_LINEAR;
}
#define gdk_color_state_get_hsl() _gdk_color_state_get_hsl ()
static inline GdkColorState *
_gdk_color_state_get_hsl (void)
{
return GDK_COLOR_STATE_HSL;
}
#define gdk_color_state_get_hwb() _gdk_color_state_get_hwb ()
static inline GdkColorState *
_gdk_color_state_get_hwb (void)
{
return GDK_COLOR_STATE_HWB;
}
#define gdk_color_state_get_oklab() _gdk_color_state_get_oklab ()
static inline GdkColorState *
_gdk_color_state_get_oklab (void)
{
return GDK_COLOR_STATE_OKLAB;
}
#define gdk_color_state_get_oklch() _gdk_color_state_get_oklch ()
static inline GdkColorState *
_gdk_color_state_get_oklch (void)
{
return GDK_COLOR_STATE_OKLCH;
}
#define gdk_color_state_get_display_p3() _gdk_color_state_get_display_p3 ()
static inline GdkColorState *
_gdk_color_state_get_display_p3 (void)
{
return GDK_COLOR_STATE_DISPLAY_P3;
}
#define gdk_color_state_get_xyz() _gdk_color_state_get_xyz ()
static inline GdkColorState *
_gdk_color_state_get_xyz (void)
{
return GDK_COLOR_STATE_XYZ;
}
#define gdk_color_state_get_rec2020() _gdk_color_state_get_rec2020 ()
static inline GdkColorState *
_gdk_color_state_get_rec2020 (void)
{
return GDK_COLOR_STATE_REC2020;
}
#define gdk_color_state_get_rec2100_pq() _gdk_color_state_get_rec2100_pq ()
static inline GdkColorState *
_gdk_color_state_get_rec2100_pq (void)
{
return GDK_COLOR_STATE_REC2100_PQ;
}
#define gdk_color_state_get_rec2100_linear() _gdk_color_state_get_rec2100_linear ()
static inline GdkColorState *
_gdk_color_state_get_rec2100_linear (void)
{
return GDK_COLOR_STATE_REC2100_LINEAR;
}
#define gdk_color_state_equal(a,b) _gdk_color_state_equal ((a), (b))
static inline gboolean
_gdk_color_state_equal (GdkColorState *self,
GdkColorState *other)
{
if (self == other)
return TRUE;
if (GDK_IS_NAMED_COLOR_STATE (self) || GDK_IS_NAMED_COLOR_STATE (other))
return FALSE;
if (self->klass != other->klass)
return FALSE;
return self->klass->equal (self, other);
}

View File

@ -0,0 +1,39 @@
#pragma once
#include "gdkcolorstate.h"
#include "gdkmemoryformatprivate.h"
#include <lcms2.h>
const char * gdk_color_state_get_name (GdkColorState *color_state);
GdkMemoryDepth gdk_color_state_get_min_depth (GdkColorState *color_state);
int gdk_color_state_get_hue_coord (GdkColorState *color_state);
typedef void (* StepFunc) (float s0, float s1, float s2,
float *d0, float *d1, float *d2);
typedef struct _GdkColorStateTransform GdkColorStateTransform;
struct _GdkColorStateTransform
{
gpointer transform;
StepFunc step1;
StepFunc step2;
gboolean cms_first;
gboolean copy_alpha;
};
void gdk_color_state_transform_init (GdkColorStateTransform *tf,
GdkColorState *from,
GdkColorState *to,
gboolean copy_alpha);
void gdk_color_state_transform_finish (GdkColorStateTransform *tf);
void gdk_color_state_transform (GdkColorStateTransform *tf,
const float *src,
float *dst,
int width);
GdkColorState *gdk_color_state_new_from_lcms_profile (cmsHPROFILE profile);
const char *gdk_color_state_get_name_from_id (GdkColorStateId id);

View File

@ -74,6 +74,7 @@ typedef cairo_rectangle_int_t GdkRectangle;
/* Forward declarations of commonly used types */
typedef struct _GdkRGBA GdkRGBA;
typedef struct _GdkColorState GdkColorState;
typedef struct _GdkContentFormats GdkContentFormats;
typedef struct _GdkContentProvider GdkContentProvider;
typedef struct _GdkCursor GdkCursor;

View File

@ -6,6 +6,7 @@ gdk_public_sources = files([
'gdkcairo.c',
'gdkcairocontext.c',
'gdkclipboard.c',
'gdkcolorstate.c',
'gdkcontentdeserializer.c',
'gdkcontentformats.c',
'gdkcontentprovider.c',
@ -75,6 +76,7 @@ gdk_public_headers = files([
'gdkcairo.h',
'gdkcairocontext.h',
'gdkclipboard.h',
'gdkcolorstate.h',
'gdkcontentdeserializer.h',
'gdkcontentformats.h',
'gdkcontentprovider.h',
@ -224,6 +226,7 @@ gdk_deps = [
fontconfig_dep,
platform_gio_dep,
pangocairo_dep,
lcms2_dep,
vulkan_dep,
libdrm_dep,
png_dep,

View File

@ -18,6 +18,7 @@ harfbuzz_req = '>= 2.6.0'
fribidi_req = '>= 1.0.6'
cairo_req = '>= 1.18.0'
gdk_pixbuf_req = '>= 2.30.0'
lcms2_req = '>= 2.8'
wayland_proto_req = '>= 1.36'
wayland_req = '>= 1.21.0'
graphene_req = '>= 1.10.0'
@ -400,6 +401,8 @@ fribidi_dep = dependency('fribidi', version: fribidi_req,
harfbuzz_dep = dependency('harfbuzz', version: harfbuzz_req,
default_options: ['coretext=enabled'])
hb_subset_dep = dependency('harfbuzz-subset', version: harfbuzz_req)
lcms2_dep = dependency('lcms2', version: lcms2_req,
fallback: ['lcms2', 'liblcms2_dep'])
# Require PangoFT2 if on X11 or wayland
pangoft_dep = dependency('pangoft2', version: pango_req,

6
subprojects/lcms2.wrap Normal file
View File

@ -0,0 +1,6 @@
[wrap-git]
directory=lcms2
url=https://github.com/mm2/Little-CMS.git
revision=master
patch_directory=lcms2
depth=1

View File

@ -0,0 +1,14 @@
project('lcms2', 'c',
version : '2.12',
meson_version : '>=0.56.0',
)
mod = import('unstable_external_project')
p = mod.add_project('configure',
configure_options : ['--prefix=@PREFIX@',
'--libdir=@PREFIX@/@LIBDIR@',
],
)
liblcms2_dep = p.dependency('lcms2'