From d4b4a3e57e23e97386c7aa91e62ad368a76d5337 Mon Sep 17 00:00:00 2001 From: Alexander Orzechowski Date: Sat, 24 Aug 2024 18:52:17 -0400 Subject: [PATCH] render/color: Add wlr_color_transform_create_from_gamma_lut --- include/wlr/render/color.h | 7 +++++++ render/color.c | 30 ++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/include/wlr/render/color.h b/include/wlr/render/color.h index 43d9154be..619e07acf 100644 --- a/include/wlr/render/color.h +++ b/include/wlr/render/color.h @@ -10,6 +10,7 @@ #define WLR_RENDER_COLOR_H #include +#include #include /** @@ -52,4 +53,10 @@ struct wlr_color_transform *wlr_color_transform_ref(struct wlr_color_transform * */ void wlr_color_transform_unref(struct wlr_color_transform *tr); +/** + * Creates a color transform based on a gamma ramp. + */ +struct wlr_color_transform *wlr_color_transform_create_from_gamma_lut( + size_t ramp_size, const uint16_t *r, const uint16_t *g, const uint16_t *b); + #endif diff --git a/render/color.c b/render/color.c index 403a6532b..d7b4d7691 100644 --- a/render/color.c +++ b/render/color.c @@ -1,5 +1,6 @@ #include #include +#include #include #include "render/color.h" @@ -49,6 +50,35 @@ void wlr_color_transform_unref(struct wlr_color_transform *tr) { } } +struct wlr_color_transform *wlr_color_transform_create_from_gamma_lut( + size_t ramp_size, const uint16_t *r, const uint16_t *g, const uint16_t *b) { + uint16_t *data = malloc(3 * ramp_size * sizeof(uint16_t)); + if (!data) { + return NULL; + } + + struct wlr_color_transform_lut3x1d *tx = calloc(1, sizeof(*tx)); + if (!tx) { + free(data); + return NULL; + } + + tx->base.type = COLOR_TRANSFORM_LUT_3x1D; + tx->base.ref_count = 1; + wlr_addon_set_init(&tx->base.addons); + + tx->r = data; + tx->g = data + ramp_size; + tx->b = data + ramp_size * 2; + tx->ramp_size = ramp_size; + + memcpy(tx->r, r, ramp_size * sizeof(uint16_t)); + memcpy(tx->g, g, ramp_size * sizeof(uint16_t)); + memcpy(tx->b, b, ramp_size * sizeof(uint16_t)); + + return &tx->base; +} + struct wlr_color_transform_lut3d *wlr_color_transform_lut3d_from_base( struct wlr_color_transform *tr) { assert(tr->type == COLOR_TRANSFORM_LUT_3D);