render/color: replace COLOR_TRANSFORM_LUT_3D with COLOR_TRANSFORM_LCMS2

Converting the LCMS2 transform to a 3D LUT early causes issues:

- It's a lossy process, the consumer will not be able to pick a
  3D LUT size on their own.
- It requires unnecessary conversions and allocations: an intermediate
  3D LUT is allocated, but the renderer already allocates one.
- It makes it harder to support arbitrary color transforms in the
  renderer, because each type needs to be handled differently.

Instead, expose a function to evaluate a color transform, and use
that to build the 3D LUT in the renderer.
This commit is contained in:
Simon Ser 2025-05-24 13:04:45 +02:00 committed by Kenny Levinsen
parent 9b97e2607d
commit 3665b53e29
6 changed files with 109 additions and 111 deletions

View file

@ -1,9 +1,17 @@
#include <assert.h>
#include <lcms2.h>
#include <stdlib.h>
#include <wlr/util/log.h>
#include <wlr/render/color.h>
#include "render/color.h"
struct wlr_color_transform_lcms2 {
struct wlr_color_transform base;
cmsContext ctx;
cmsHTRANSFORM lcms;
};
static const cmsCIExyY srgb_whitepoint = { 0.3127, 0.3291, 1 };
static const cmsCIExyYTRIPLE srgb_primaries = {
@ -18,7 +26,7 @@ static void handle_lcms_error(cmsContext ctx, cmsUInt32Number code, const char *
struct wlr_color_transform *wlr_color_transform_init_linear_to_icc(
const void *data, size_t size) {
struct wlr_color_transform_lut3d *tx = NULL;
struct wlr_color_transform_lcms2 *tx = NULL;
cmsContext ctx = cmsCreateContext(NULL, NULL);
if (ctx == NULL) {
@ -31,18 +39,18 @@ struct wlr_color_transform *wlr_color_transform_init_linear_to_icc(
cmsHPROFILE icc_profile = cmsOpenProfileFromMemTHR(ctx, data, size);
if (icc_profile == NULL) {
wlr_log(WLR_ERROR, "cmsOpenProfileFromMemTHR failed");
goto out_ctx;
goto error_ctx;
}
if (cmsGetDeviceClass(icc_profile) != cmsSigDisplayClass) {
wlr_log(WLR_ERROR, "ICC profile must have the Display device class");
goto out_icc_profile;
goto error_icc_profile;
}
cmsToneCurve *linear_tone_curve = cmsBuildGamma(ctx, 1);
if (linear_tone_curve == NULL) {
wlr_log(WLR_ERROR, "cmsBuildGamma failed");
goto out_icc_profile;
goto error_icc_profile;
}
cmsToneCurve *linear_tf[] = {
@ -52,66 +60,54 @@ struct wlr_color_transform *wlr_color_transform_init_linear_to_icc(
};
cmsHPROFILE srgb_profile = cmsCreateRGBProfileTHR(ctx, &srgb_whitepoint,
&srgb_primaries, linear_tf);
cmsFreeToneCurve(linear_tone_curve);
if (srgb_profile == NULL) {
wlr_log(WLR_ERROR, "cmsCreateRGBProfileTHR failed");
goto out_linear_tone_curve;
goto error_icc_profile;
}
cmsHTRANSFORM lcms_tr = cmsCreateTransformTHR(ctx,
srgb_profile, TYPE_RGB_FLT, icc_profile, TYPE_RGB_FLT,
INTENT_RELATIVE_COLORIMETRIC, 0);
cmsCloseProfile(srgb_profile);
cmsCloseProfile(icc_profile);
if (lcms_tr == NULL) {
wlr_log(WLR_ERROR, "cmsCreateTransformTHR failed");
goto out_srgb_profile;
}
size_t dim_len = 33;
float *lut_3d = calloc(3 * dim_len * dim_len * dim_len, sizeof(float));
if (lut_3d == NULL) {
wlr_log_errno(WLR_ERROR, "Allocation failed");
goto out_lcms_tr;
}
float factor = 1.0f / (dim_len - 1);
for (size_t b_index = 0; b_index < dim_len; b_index++) {
for (size_t g_index = 0; g_index < dim_len; g_index++) {
for (size_t r_index = 0; r_index < dim_len; r_index++) {
float rgb_in[3] = {
r_index * factor,
g_index * factor,
b_index * factor,
};
float rgb_out[3];
// TODO: use a single call to cmsDoTransform for the entire calculation?
// this does require allocating an extra temp buffer
cmsDoTransform(lcms_tr, rgb_in, rgb_out, 1);
size_t offset = 3 * (r_index + dim_len * g_index + dim_len * dim_len * b_index);
// TODO: maybe clamp values to [0.0, 1.0] here?
lut_3d[offset] = rgb_out[0];
lut_3d[offset + 1] = rgb_out[1];
lut_3d[offset + 2] = rgb_out[2];
}
}
goto error_ctx;
}
tx = calloc(1, sizeof(*tx));
if (!tx) {
goto out_lcms_tr;
cmsDeleteTransform(lcms_tr);
goto error_ctx;
}
wlr_color_transform_init(&tx->base, COLOR_TRANSFORM_LUT_3D);
tx->dim_len = dim_len;
tx->lut_3d = lut_3d;
wlr_color_transform_init(&tx->base, COLOR_TRANSFORM_LCMS2);
tx->ctx = ctx;
tx->lcms = lcms_tr;
out_lcms_tr:
cmsDeleteTransform(lcms_tr);
out_linear_tone_curve:
cmsFreeToneCurve(linear_tone_curve);
out_srgb_profile:
cmsCloseProfile(srgb_profile);
out_icc_profile:
cmsCloseProfile(icc_profile);
out_ctx:
cmsDeleteContext(ctx);
return &tx->base;
error_icc_profile:
cmsCloseProfile(icc_profile);
error_ctx:
cmsDeleteContext(ctx);
return NULL;
}
void color_transform_lcms2_finish(struct wlr_color_transform_lcms2 *tr) {
cmsDeleteTransform(tr->lcms);
cmsDeleteContext(tr->ctx);
}
struct wlr_color_transform_lcms2 *color_transform_lcms2_from_base(
struct wlr_color_transform *tr) {
assert(tr->type == COLOR_TRANSFORM_LCMS2);
struct wlr_color_transform_lcms2 *lcms2 = wl_container_of(tr, lcms2, base);
return lcms2;
}
void color_transform_lcms2_eval(struct wlr_color_transform_lcms2 *tr,
float out[static 3], const float in[static 3]) {
cmsDoTransform(tr->lcms, in, out, 1);
}