Skip to content

Commit

Permalink
Some maintenance in colorout and colorspaces
Browse files Browse the repository at this point in the history
1. Introduce cmsHPROFILE dt_colorspaces_make_temporary_profile(cmsHPROFILE profile)
   in colorspaces.c for more usecases and make colorout use that instead of internal _make_clipping_profile()
2. Some int to gboolean conversions where appropriate
3. Some static functions renamed for leading underscore
  • Loading branch information
jenshannoschwalm committed Apr 25, 2024
1 parent bc85de3 commit d8d8765
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 59 deletions.
85 changes: 52 additions & 33 deletions src/common/colorspaces.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,13 @@ static const dt_colorspaces_color_profile_t *_get_profile
const char *filename,
dt_colorspaces_profile_direction_t direction);

static int dt_colorspaces_get_matrix_from_profile(cmsHPROFILE prof,
dt_colormatrix_t matrix,
float *lutr,
float *lutg,
float *lutb,
const int lutsize,
const int input)
static int _colorspaces_get_matrix_from_profile(cmsHPROFILE prof,
dt_colormatrix_t matrix,
float *lutr,
float *lutg,
float *lutb,
const int lutsize,
const int input)
{
// create an OpenCL processable matrix + tone curves from an cmsHPROFILE:
// NOTE: may be invoked with matrix and LUT pointers set to null to find
Expand Down Expand Up @@ -235,7 +235,7 @@ int dt_colorspaces_get_matrix_from_input_profile(cmsHPROFILE prof,
float *lutb,
const int lutsize)
{
return dt_colorspaces_get_matrix_from_profile(prof, matrix, lutr, lutg, lutb, lutsize, 1);
return _colorspaces_get_matrix_from_profile(prof, matrix, lutr, lutg, lutb, lutsize, 1);
}

int dt_colorspaces_get_matrix_from_output_profile(cmsHPROFILE prof,
Expand All @@ -245,10 +245,10 @@ int dt_colorspaces_get_matrix_from_output_profile(cmsHPROFILE prof,
float *lutb,
const int lutsize)
{
return dt_colorspaces_get_matrix_from_profile(prof, matrix, lutr, lutg, lutb, lutsize, 0);
return _colorspaces_get_matrix_from_profile(prof, matrix, lutr, lutg, lutb, lutsize, 0);
}

static cmsHPROFILE dt_colorspaces_create_lab_profile()
static cmsHPROFILE _colorspaces_create_lab_profile()
{
return cmsCreateLab4Profile(cmsD50_xyY());
}
Expand Down Expand Up @@ -431,7 +431,7 @@ static cmsHPROFILE dt_colorspaces_create_srgb_profile_v4()
return _colorspaces_create_srgb_profile(FALSE);
}

static cmsHPROFILE dt_colorspaces_create_brg_profile()
static cmsHPROFILE _colorspaces_create_brg_profile()
{
cmsFloat64Number srgb_parameters[5] =
{ 2.4, 1.0 / 1.055, 0.055 / 1.055, 1.0 / 12.92, 0.04045 };
Expand All @@ -449,7 +449,7 @@ static cmsHPROFILE dt_colorspaces_create_brg_profile()
return profile;
}

static cmsHPROFILE dt_colorspaces_create_gamma_rec709_rgb_profile(void)
static cmsHPROFILE _colorspaces_create_gamma_rec709_rgb_profile(void)
{
cmsFloat64Number srgb_parameters[5] =
{ 1/0.45, 1.0 / 1.099, 0.099 / 1.099, 1.0 / 4.5, 0.081 };
Expand Down Expand Up @@ -631,7 +631,7 @@ cmsHPROFILE dt_colorspaces_create_darktable_profile(const char *makermodel)
return hp;
}

static cmsHPROFILE dt_colorspaces_create_xyz_profile(void)
static cmsHPROFILE _colorspaces_create_xyz_profile(void)
{
cmsHPROFILE hXYZ = cmsCreateXYZProfile();
cmsSetPCS(hXYZ, cmsSigXYZData);
Expand Down Expand Up @@ -670,7 +670,7 @@ static cmsHPROFILE dt_colorspaces_create_linear_rec709_rgb_profile(void)
return profile;
}

static cmsHPROFILE dt_colorspaces_create_linear_rec2020_rgb_profile(void)
static cmsHPROFILE _colorspaces_create_linear_rec2020_rgb_profile(void)
{
cmsToneCurve *transferFunction = cmsBuildGamma(NULL, 1.0);

Expand All @@ -683,7 +683,7 @@ static cmsHPROFILE dt_colorspaces_create_linear_rec2020_rgb_profile(void)
return profile;
}

static cmsHPROFILE dt_colorspaces_create_pq_rec2020_rgb_profile(void)
static cmsHPROFILE _colorspaces_create_pq_rec2020_rgb_profile(void)
{
cmsToneCurve *transferFunction = _colorspaces_create_transfer(4096, _PQ_fct);

Expand All @@ -696,7 +696,7 @@ static cmsHPROFILE dt_colorspaces_create_pq_rec2020_rgb_profile(void)
return profile;
}

static cmsHPROFILE dt_colorspaces_create_hlg_rec2020_rgb_profile(void)
static cmsHPROFILE _colorspaces_create_hlg_rec2020_rgb_profile(void)
{
cmsToneCurve *transferFunction = _colorspaces_create_transfer(4096, _HLG_fct);

Expand All @@ -709,7 +709,7 @@ static cmsHPROFILE dt_colorspaces_create_hlg_rec2020_rgb_profile(void)
return profile;
}

static cmsHPROFILE dt_colorspaces_create_pq_p3_rgb_profile(void)
static cmsHPROFILE _colorspaces_create_pq_p3_rgb_profile(void)
{
cmsToneCurve *transferFunction = _colorspaces_create_transfer(4096, _PQ_fct);

Expand All @@ -722,7 +722,7 @@ static cmsHPROFILE dt_colorspaces_create_pq_p3_rgb_profile(void)
return profile;
}

static cmsHPROFILE dt_colorspaces_create_hlg_p3_rgb_profile(void)
static cmsHPROFILE _colorspaces_create_hlg_p3_rgb_profile(void)
{
cmsToneCurve *transferFunction = _colorspaces_create_transfer(4096, _HLG_fct);

Expand All @@ -735,7 +735,7 @@ static cmsHPROFILE dt_colorspaces_create_hlg_p3_rgb_profile(void)
return profile;
}

static cmsHPROFILE dt_colorspaces_create_display_p3_rgb_profile(void)
static cmsHPROFILE _colorspaces_create_display_p3_rgb_profile(void)
{
cmsFloat64Number srgb_parameters[5] =
{ 2.4, 1.0 / 1.055, 0.055 / 1.055, 1.0 / 12.92, 0.04045 };
Expand All @@ -750,7 +750,7 @@ static cmsHPROFILE dt_colorspaces_create_display_p3_rgb_profile(void)
return profile;
}

static cmsHPROFILE dt_colorspaces_create_linear_prophoto_rgb_profile(void)
static cmsHPROFILE _colorspaces_create_linear_prophoto_rgb_profile(void)
{
cmsToneCurve *transferFunction = cmsBuildGamma(NULL, 1.0);

Expand All @@ -763,7 +763,7 @@ static cmsHPROFILE dt_colorspaces_create_linear_prophoto_rgb_profile(void)
return profile;
}

static cmsHPROFILE dt_colorspaces_create_linear_infrared_profile(void)
static cmsHPROFILE _colorspaces_create_linear_infrared_profile(void)
{
cmsToneCurve *transferFunction = cmsBuildGamma(NULL, 1.0);

Expand Down Expand Up @@ -1046,6 +1046,25 @@ void dt_colorspaces_cleanup_profile(cmsHPROFILE p)
cmsCloseProfile(p);
}

cmsHPROFILE dt_colorspaces_make_temporary_profile(cmsHPROFILE profile)
{
cmsUInt32Number size;
cmsHPROFILE old_profile = profile;
profile = NULL;

if(old_profile && cmsSaveProfileToMem(old_profile, NULL, &size))
{
char *data = malloc(size);

if(cmsSaveProfileToMem(old_profile, data, &size))
profile = cmsOpenProfileFromMem(data, size);

free(data);
}

return profile;
}

void dt_colorspaces_get_profile_name(cmsHPROFILE p,
const char *language,
const char *country,
Expand Down Expand Up @@ -1446,85 +1465,85 @@ dt_colorspaces_t *dt_colorspaces_init()

res->profiles = g_list_append
(res->profiles,
_create_profile(DT_COLORSPACE_REC709, dt_colorspaces_create_gamma_rec709_rgb_profile(),
_create_profile(DT_COLORSPACE_REC709, _colorspaces_create_gamma_rec709_rgb_profile(),
_("Rec709 RGB"), ++in_pos, ++out_pos, -1, -1,
++work_pos, -1));

res->profiles = g_list_append
(res->profiles,
_create_profile(DT_COLORSPACE_LIN_REC2020,
dt_colorspaces_create_linear_rec2020_rgb_profile(),
_colorspaces_create_linear_rec2020_rgb_profile(),
_("linear Rec2020 RGB"), ++in_pos, ++out_pos,
++display_pos, ++category_pos,
++work_pos, ++display2_pos));

res->profiles = g_list_append
(res->profiles,
_create_profile(DT_COLORSPACE_PQ_REC2020,
dt_colorspaces_create_pq_rec2020_rgb_profile(),
_colorspaces_create_pq_rec2020_rgb_profile(),
_("PQ Rec2020 RGB"), ++in_pos, ++out_pos,
++display_pos, ++category_pos,
++work_pos, ++display2_pos));

res->profiles = g_list_append
(res->profiles,
_create_profile(DT_COLORSPACE_HLG_REC2020,
dt_colorspaces_create_hlg_rec2020_rgb_profile(),
_colorspaces_create_hlg_rec2020_rgb_profile(),
_("HLG Rec2020 RGB"), ++in_pos, ++out_pos,
++display_pos, ++category_pos,
++work_pos, ++display2_pos));

res->profiles = g_list_append
(res->profiles,
_create_profile(DT_COLORSPACE_PQ_P3, dt_colorspaces_create_pq_p3_rgb_profile(),
_create_profile(DT_COLORSPACE_PQ_P3, _colorspaces_create_pq_p3_rgb_profile(),
_("PQ P3 RGB"), ++in_pos, ++out_pos, ++display_pos, ++category_pos,
++work_pos, ++display2_pos));

res->profiles = g_list_append
(res->profiles,
_create_profile(DT_COLORSPACE_HLG_P3, dt_colorspaces_create_hlg_p3_rgb_profile(),
_create_profile(DT_COLORSPACE_HLG_P3, _colorspaces_create_hlg_p3_rgb_profile(),
_("HLG P3 RGB"), ++in_pos, ++out_pos, ++display_pos, ++category_pos,
++work_pos, ++display2_pos));

res->profiles = g_list_append
(res->profiles,
_create_profile(DT_COLORSPACE_DISPLAY_P3,
dt_colorspaces_create_display_p3_rgb_profile(),
_colorspaces_create_display_p3_rgb_profile(),
_("Display P3 RGB"), ++in_pos, ++out_pos,
++display_pos, ++category_pos,
++work_pos, ++display2_pos));

res->profiles = g_list_append
(res->profiles,
_create_profile(DT_COLORSPACE_PROPHOTO_RGB,
dt_colorspaces_create_linear_prophoto_rgb_profile(),
_colorspaces_create_linear_prophoto_rgb_profile(),
_("linear ProPhoto RGB"), ++in_pos, ++out_pos,
++display_pos, ++category_pos,
++work_pos, ++display2_pos));

res->profiles = g_list_append
(res->profiles,
_create_profile(DT_COLORSPACE_XYZ,
dt_colorspaces_create_xyz_profile(), _("linear XYZ"), ++in_pos,
_colorspaces_create_xyz_profile(), _("linear XYZ"), ++in_pos,
dt_conf_get_bool("allow_lab_output") ? ++out_pos : -1,
-1, -1, -1, -1));

res->profiles = g_list_append
(res->profiles,
_create_profile(DT_COLORSPACE_LAB,
dt_colorspaces_create_lab_profile(), _("Lab"), ++in_pos,
_colorspaces_create_lab_profile(), _("Lab"), ++in_pos,
dt_conf_get_bool("allow_lab_output") ? ++out_pos : -1,
-1, -1, -1, -1));

res->profiles = g_list_append
(res->profiles,
_create_profile(DT_COLORSPACE_INFRARED,
dt_colorspaces_create_linear_infrared_profile(),
_colorspaces_create_linear_infrared_profile(),
_("linear infrared BGR"), ++in_pos, -1, -1, -1, -1, -1));

res->profiles = g_list_append
(res->profiles,
_create_profile(DT_COLORSPACE_BRG, dt_colorspaces_create_brg_profile(),
_create_profile(DT_COLORSPACE_BRG, _colorspaces_create_brg_profile(),
_("BRG (for testing)"), ++in_pos, ++out_pos, ++display_pos,
-1, -1, ++display2_pos));

Expand Down
5 changes: 4 additions & 1 deletion src/common/colorspaces.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
This file is part of darktable,
Copyright (C) 2010-2023 darktable developers.
Copyright (C) 2010-2024 darktable developers.
darktable is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -267,6 +267,9 @@ int dt_colorspaces_get_matrix_from_output_profile(cmsHPROFILE prof,
float *lutb,
const int lutsize);

/** create a temporary profile to be removed by dt_colorspaces_cleanup_profile */
cmsHPROFILE dt_colorspaces_make_temporary_profile(cmsHPROFILE profile);

/** wrapper to get the name from a color profile. this tries to handle character encodings. */
void dt_colorspaces_get_profile_name(cmsHPROFILE p,
const char *language,
Expand Down
31 changes: 6 additions & 25 deletions src/iop/colorout.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
This file is part of darktable,
Copyright (C) 2009-2023 darktable developers.
Copyright (C) 2009-2024 darktable developers.
darktable is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -573,25 +573,6 @@ void process(struct dt_iop_module_t *self, dt_dev_pixelpipe_iop_t *piece, const
}
}

static cmsHPROFILE _make_clipping_profile(cmsHPROFILE profile)
{
cmsUInt32Number size;
cmsHPROFILE old_profile = profile;
profile = NULL;

if(old_profile && cmsSaveProfileToMem(old_profile, NULL, &size))
{
char *data = malloc(size);

if(cmsSaveProfileToMem(old_profile, data, &size))
profile = cmsOpenProfileFromMem(data, size);

free(data);
}

return profile;
}

void commit_params(struct dt_iop_module_t *self, dt_iop_params_t *p1, dt_dev_pixelpipe_t *pipe,
dt_dev_pixelpipe_iop_t *piece)
{
Expand All @@ -600,7 +581,7 @@ void commit_params(struct dt_iop_module_t *self, dt_iop_params_t *p1, dt_dev_pix

d->type = p->type;

const int force_lcms2 = dt_conf_get_bool("plugins/lighttable/export/force_lcms2");
const gboolean force_lcms2 = dt_conf_get_bool("plugins/lighttable/export/force_lcms2");

dt_colorspaces_color_profile_type_t out_type = DT_COLORSPACE_SRGB;
gchar *out_filename = NULL;
Expand Down Expand Up @@ -724,7 +705,7 @@ void commit_params(struct dt_iop_module_t *self, dt_iop_params_t *p1, dt_dev_pix
// taking a roundtrip through those profiles during softproofing has no effect. as a workaround we have to
// make lcms quantisize those gamma tables to get the desired effect.
// in case that fails we don't enable softproofing.
softproof = _make_clipping_profile(softproof);
softproof = dt_colorspaces_make_temporary_profile(softproof);
if(softproof)
{
/* TODO: the use of bpc should be userconfigurable either from module or preference pane */
Expand Down Expand Up @@ -798,7 +779,7 @@ void commit_params(struct dt_iop_module_t *self, dt_iop_params_t *p1, dt_dev_pix
d->unbounded_coeffs[k][0] = -1.0f;
}

// softproof is never the original but always a copy that went through _make_clipping_profile()
// softproof is never the original but always a copy that went through dt_colorspaces_make_temporary_profile()
dt_colorspaces_cleanup_profile(softproof);

dt_ioppr_set_pipe_output_profile_info(self->dev, piece->pipe, d->type, out_filename, p->intent);
Expand Down Expand Up @@ -861,7 +842,7 @@ static void _preference_changed(gpointer instance, gpointer user_data)
dt_iop_module_t *self = (dt_iop_module_t *)user_data;
dt_iop_colorout_gui_data_t *g = (dt_iop_colorout_gui_data_t *)self->gui_data;

const int force_lcms2 = dt_conf_get_bool("plugins/lighttable/export/force_lcms2");
const gboolean force_lcms2 = dt_conf_get_bool("plugins/lighttable/export/force_lcms2");
if(force_lcms2)
{
gtk_widget_set_no_show_all(g->output_intent, FALSE);
Expand All @@ -876,7 +857,7 @@ static void _preference_changed(gpointer instance, gpointer user_data)

void gui_init(struct dt_iop_module_t *self)
{
const int force_lcms2 = dt_conf_get_bool("plugins/lighttable/export/force_lcms2");
const gboolean force_lcms2 = dt_conf_get_bool("plugins/lighttable/export/force_lcms2");

dt_iop_colorout_gui_data_t *g = IOP_GUI_ALLOC(colorout);

Expand Down

0 comments on commit d8d8765

Please sign in to comment.