From bf513a362a52acc56b5805c0020a5d5185799d00 Mon Sep 17 00:00:00 2001 From: Pekka Paalanen Date: Thu, 26 Feb 2026 15:07:54 +0200 Subject: [PATCH] libweston: add weston_color_gamut_from_protocol() Reduce 4 copies of a piece of code into one. Usable for both client and server side. Signed-off-by: Pekka Paalanen --- include/libweston/colorimetry.h | 7 +++++++ libweston/color-management.c | 18 ++--------------- libweston/color.c | 30 ++++++++++++++++++++++++++++ tests/harness/color-manager-client.c | 19 +++--------------- 4 files changed, 42 insertions(+), 32 deletions(-) diff --git a/include/libweston/colorimetry.h b/include/libweston/colorimetry.h index caaa90420..5bc5ccc32 100644 --- a/include/libweston/colorimetry.h +++ b/include/libweston/colorimetry.h @@ -142,6 +142,13 @@ struct weston_color_gamut { struct weston_CIExy white_point; }; +void +weston_color_gamut_from_protocol(struct weston_color_gamut *dst, + int32_t r_x, int32_t r_y, + int32_t g_x, int32_t g_y, + int32_t b_x, int32_t b_y, + int32_t w_x, int32_t w_y); + enum weston_npm_direction { WESTON_NPM_FORWARD, WESTON_NPM_INVERSE diff --git a/libweston/color-management.c b/libweston/color-management.c index 2fea1f55d..10cf7a305 100644 --- a/libweston/color-management.c +++ b/libweston/color-management.c @@ -1342,14 +1342,7 @@ cm_creator_params_set_primaries(struct wl_client *client, struct wl_resource *re wl_resource_get_user_data(resource); struct weston_color_gamut primaries; - primaries.primary[0].x = r_x / 1000000.0f; - primaries.primary[0].y = r_y / 1000000.0f; - primaries.primary[1].x = g_x / 1000000.0f; - primaries.primary[1].y = g_y / 1000000.0f; - primaries.primary[2].x = b_x / 1000000.0f; - primaries.primary[2].y = b_y / 1000000.0f; - primaries.white_point.x = w_x / 1000000.0f; - primaries.white_point.y = w_y / 1000000.0f; + weston_color_gamut_from_protocol(&primaries, r_x, r_y, g_x, g_y, b_x, b_y, w_x, w_y); if (!weston_color_profile_param_builder_set_primaries(cm_creator_params->builder, &primaries)) @@ -1433,14 +1426,7 @@ cm_creator_params_set_mastering_display_primaries(struct wl_client *client, wl_resource_get_user_data(resource); struct weston_color_gamut primaries; - primaries.primary[0].x = r_x / 1000000.0f; - primaries.primary[0].y = r_y / 1000000.0f; - primaries.primary[1].x = g_x / 1000000.0f; - primaries.primary[1].y = g_y / 1000000.0f; - primaries.primary[2].x = b_x / 1000000.0f; - primaries.primary[2].y = b_y / 1000000.0f; - primaries.white_point.x = w_x / 1000000.0f; - primaries.white_point.y = w_y / 1000000.0f; + weston_color_gamut_from_protocol(&primaries, r_x, r_y, g_x, g_y, b_x, b_y, w_x, w_y); if (!weston_color_profile_param_builder_set_target_primaries(cm_creator_params->builder, &primaries)) diff --git a/libweston/color.c b/libweston/color.c index b0e03abac..ae68ee293 100644 --- a/libweston/color.c +++ b/libweston/color.c @@ -145,6 +145,36 @@ weston_color_profile_init(struct weston_color_profile *cprof, cprof->id = weston_idalloc_get_id(cm->compositor->color_profile_id_generator); } +/** + * Initialize gamut from protocol integer-encoded values + * + * The color_management_v1 Wayland protocol encodes colorimetric coordinates + * into integers by multiplying them with one million. This function does the + * decoding. + * + * \param[out] dst Gamut record to initialize. + * \param r_x,r_y Red primary + * \param g_x,g_y Green primary + * \param b_x,b_y Blue primary + * \param w_x,w_y White point + */ +WL_EXPORT void +weston_color_gamut_from_protocol(struct weston_color_gamut *dst, + int32_t r_x, int32_t r_y, + int32_t g_x, int32_t g_y, + int32_t b_x, int32_t b_y, + int32_t w_x, int32_t w_y) +{ + dst->primary[0].x = r_x / 1000000.0f; + dst->primary[0].y = r_y / 1000000.0f; + dst->primary[1].x = g_x / 1000000.0f; + dst->primary[1].y = g_y / 1000000.0f; + dst->primary[2].x = b_x / 1000000.0f; + dst->primary[2].y = b_y / 1000000.0f; + dst->white_point.x = w_x / 1000000.0f; + dst->white_point.y = w_y / 1000000.0f; +} + static void weston_color_gamut_fprint(FILE *fp, const char *indent, diff --git a/tests/harness/color-manager-client.c b/tests/harness/color-manager-client.c index 8ce2d9923..8eff70258 100644 --- a/tests/harness/color-manager-client.c +++ b/tests/harness/color-manager-client.c @@ -319,14 +319,7 @@ image_descr_info_primaries(void *data, image_descr_info_received(info, IMAGE_DESCR_INFO_EVENT_PRIMARIES); - info->primaries.primary[0].x = r_x / 1000000.0f; - info->primaries.primary[0].y = r_y / 1000000.0f; - info->primaries.primary[1].x = g_x / 1000000.0f; - info->primaries.primary[1].y = g_y / 1000000.0f; - info->primaries.primary[2].x = b_x / 1000000.0f; - info->primaries.primary[2].y = b_y / 1000000.0f; - info->primaries.white_point.x = w_x / 1000000.0f; - info->primaries.white_point.y = w_y / 1000000.0f; + weston_color_gamut_from_protocol(&info->primaries, r_x, r_y, g_x, g_y, b_x, b_y, w_x, w_y); } static void @@ -386,14 +379,8 @@ image_descr_info_target_primaries(void *data, image_descr_info_received(info, IMAGE_DESCR_INFO_EVENT_TARGET_PRIMARIES); - info->target_primaries.primary[0].x = r_x / 1000000.0f; - info->target_primaries.primary[0].y = r_y / 1000000.0f; - info->target_primaries.primary[1].x = g_x / 1000000.0f; - info->target_primaries.primary[1].y = g_y / 1000000.0f; - info->target_primaries.primary[2].x = b_x / 1000000.0f; - info->target_primaries.primary[2].y = b_y / 1000000.0f; - info->target_primaries.white_point.x = w_x / 1000000.0f; - info->target_primaries.white_point.y = w_y / 1000000.0f; + weston_color_gamut_from_protocol(&info->target_primaries, + r_x, r_y, g_x, g_y, b_x, b_y, w_x, w_y); } static void