55#define DT_CONVERSION_MAGIC_LIVE 0xC0117E51u
56#define DT_CONVERSION_MAGIC_DEAD 0xDEADC017u
106 cmsUInt32Number
size;
107 cmsHPROFILE quantised = NULL;
109 if(profile && cmsSaveProfileToMem(profile, NULL, &
size))
114 if(cmsSaveProfileToMem(profile,
data, &
size)) quantised = cmsOpenProfileFromMem(
data,
size);
155 const char *filename =
"";
180 const cmsColorSpaceSignature space = cmsGetColorSpace(profile);
184 return TYPE_RGBA_FLT;
190 (
char)(space >> 24), (
char)(space >> 16), (
char)(space >> 8), (
char)(space));
192 return TYPE_RGBA_FLT;
198 for(
int c = 0; c < 3; c++)
209 for(
int c = 0; c < 3; c++)
242 conversion->
matrix[0][0] = NAN;
265 gboolean source_supported =
TRUE, target_supported =
TRUE;
266 cmsUInt32Number source_format = TYPE_RGBA_FLT, target_format = TYPE_RGBA_FLT;
267 gboolean proofing =
FALSE;
268 gboolean matrix_allowed =
FALSE;
273 source_format =
_format_for(from_profile, &source_supported);
274 target_format =
_format_for(to_profile, &target_supported);
306 const gboolean have_source
310 const gboolean have_target
315 if(have_source && have_target)
334 if(source_ok && target_ok)
374 conversion->
matrix[0][0] = NAN;
382 cmsUInt32Number transform_flags = cmsFLAGS_NOCACHE;
385 transform_flags |= cmsFLAGS_SOFTPROOFING | cmsFLAGS_BLACKPOINTCOMPENSATION;
391 conversion->
xform = cmsCreateTransform(from_profile, source_format, clip_profile, TYPE_RGBA_FLT,
392 intent, transform_flags);
393 conversion->
clip_xform = cmsCreateTransform(clip_profile, TYPE_RGBA_FLT, to_profile, target_format,
394 intent, transform_flags);
407 conversion->
xform = cmsCreateProofingTransform(from_profile, source_format, to_profile, target_format,
408 proof_profile, intent, INTENT_RELATIVE_COLORIMETRIC,
432 identity =
dt_hash(identity, (
const char *)&intent,
sizeof(intent));
441 identity =
dt_hash(identity, (
const char *)conversion->
matrix,
sizeof(conversion->
matrix));
452 for(
int c = 0; c < 3; c++)
456 identity =
dt_hash(identity, (
const char *)&has_source,
sizeof(has_source));
457 identity =
dt_hash(identity, (
const char *)&has_target,
sizeof(has_target));
496 caller, (
void *)conversion);
499 " (tag 0x%08x) -- refusing\n", caller, (
void *)conversion, conversion->
magic);
525 if(!
IS_NULL_PTR(c->xform)) cmsDeleteTransform(c->xform);
526 if(!
IS_NULL_PTR(c->clip_xform)) cmsDeleteTransform(c->clip_xform);
536static inline __attribute__((always_inline)) dt_aligned_pixel_simd_t _clamp_unit(dt_aligned_pixel_simd_t
v)
538 v[0] = CLAMP(
v[0], 0.0f, 1.0f);
539 v[1] = CLAMP(
v[1], 0.0f, 1.0f);
540 v[2] = CLAMP(
v[2], 0.0f, 1.0f);
547 const size_t npixels)
549 const float *
const restrict lut0 = c->lut_target[0];
550 const float *
const restrict lut1 = c->lut_target[1];
551 const float *
const restrict lut2 = c->lut_target[2];
552 const int run_lut0 = lut0[0] >= 0.0f;
553 const int run_lut1 = lut1[0] >= 0.0f;
554 const int run_lut2 = lut2[0] >= 0.0f;
555 if(!(run_lut0 || run_lut1 || run_lut2))
return;
557 const float *
const coeff0 = c->coeffs_target[0];
558 const float *
const coeff1 = c->coeffs_target[1];
559 const float *
const coeff2 = c->coeffs_target[2];
561 if(run_lut0 && run_lut1 && run_lut2)
564 for(
size_t k = 0;
k < npixels;
k++)
566 const size_t idx = 4 *
k;
575 for(
size_t k = 0;
k < npixels;
k++)
577 const size_t idx = 4 *
k;
594 float *
const restrict
out,
const size_t npixels,
600 const dt_aligned_pixel_simd_t m0 = dt_colormatrix_row_to_simd(
m, 0);
601 const dt_aligned_pixel_simd_t m1 = dt_colormatrix_row_to_simd(
m, 1);
602 const dt_aligned_pixel_simd_t m2 = dt_colormatrix_row_to_simd(
m, 2);
603 const dt_aligned_pixel_simd_t c0 = dt_colormatrix_row_to_simd(cm, 0);
604 const dt_aligned_pixel_simd_t
c1 = dt_colormatrix_row_to_simd(cm, 1);
605 const dt_aligned_pixel_simd_t
c2 = dt_colormatrix_row_to_simd(cm, 2);
610 const gboolean decode = !
IS_NULL_PTR(c->lut_source[0]) && c->nonlinear_source > 0;
611 const gboolean encode = !
IS_NULL_PTR(c->lut_target[0]) && c->nonlinear_target > 0;
612 const gboolean clipping = c->has_clipping;
621 for(
size_t k = 0;
k < npixels;
k++)
623 const size_t idx = 4 *
k;
624 dt_aligned_pixel_simd_t
v = dt_mat3x4_mul_vec4(dt_load_simd_aligned(
in + idx), m0, m1, m2);
625 if(clipping)
v = dt_mat3x4_mul_vec4(_clamp_unit(
v), c0,
c1,
c2);
632 for(
size_t k = 0;
k < npixels;
k++)
634 const size_t idx = 4 *
k;
635 dt_aligned_pixel_simd_t
v = dt_mat3x4_mul_vec4(dt_load_simd_aligned(
in + idx), m0, m1, m2);
636 if(clipping)
v = dt_mat3x4_mul_vec4(_clamp_unit(
v), c0,
c1,
c2);
637 dt_store_simd_nontemporal(
out + idx,
v);
644 const float *
const lut_r = decode ? c->lut_source[0] : NULL;
645 const float *
const lut_g = decode ? c->lut_source[1] : NULL;
646 const float *
const lut_b = decode ? c->lut_source[2] : NULL;
649 for(
size_t k = 0;
k < npixels;
k++)
651 const float *
const in_pixel =
in + 4 *
k;
652 float *
const out_pixel =
out + 4 *
k;
657 staged[0] = (decode && lut_r[0] >= 0.0f)
660 staged[1] = (decode && lut_g[0] >= 0.0f)
663 staged[2] = (decode && lut_b[0] >= 0.0f)
670 dt_aligned_pixel_simd_t
v = dt_mat3x4_mul_vec4(dt_load_simd_aligned(staged), m0, m1, m2);
671 if(clipping)
v = dt_mat3x4_mul_vec4(_clamp_unit(
v), c0,
c1,
c2);
676 dt_store_simd_nontemporal(out_pixel,
v);
690 const cmsHTRANSFORM xform = c->xform;
691 const cmsHTRANSFORM clip_xform = c->clip_xform;
692 const gboolean clipping = c->has_clipping;
693 const gboolean gamutcheck = c->gamutcheck;
698 const float *
const restrict source =
in + 4 *
row *
width;
699 float *
const restrict target =
out + 4 *
row *
width;
706 for(
size_t j = 0; j <
width; j++)
708 hook(source + 4 * j, target + 4 * j);
709 target[4 * j + 3] = 0.0f;
720 float *
const restrict clipped = target;
722 for(
size_t j = 0; j <
width; j++)
724 for(
int ch = 0;
ch < 3;
ch++) clipped[4 * j +
ch] = CLAMP(clipped[4 * j +
ch], 0.0f, 1.0f);
731 for(
size_t j = 0; j <
width; j++)
733 if(target[4 * j + 0] < 0.0f || target[4 * j + 1] < 0.0f || target[4 * j + 2] < 0.0f)
735 target[4 * j + 0] = 0.0f;
736 target[4 * j + 1] = 1.0f;
737 target[4 * j + 2] = 1.0f;
745 const float *
const in,
float *
const out,
const size_t width,
807 if(!
_conversion_valid(conversion,
"conversion_source_curve") || channel < 0 || channel > 2)
return NULL;
814 if(!
_conversion_valid(conversion,
"conversion_target_curve") || channel < 0 || channel > 2)
return NULL;
int dt_ioppr_init_unbounded_coeffs(float *const lutr, float *const lutg, float *const lutb, float *const unbounded_coeffsr, float *const unbounded_coeffsg, float *const unbounded_coeffsb, const int lutsize)
Fit the power-law continuation of three tone curves past white.
The colour-profile struct and the maths over it: the derived matrix/LUT engine.
void dt_colorspaces_unlock_profile(const dt_colorspaces_color_profile_t *const profile)
const dt_colorspaces_color_profile_t * dt_colorspaces_get_profile(dt_colorspaces_color_profile_type_t type, const char *filename, dt_colorspaces_profile_role_t role)
Resolve a profile identity to its registered entry.
int dt_colorspaces_get_matrix_from_output_profile(cmsHPROFILE prof, dt_colormatrix_t matrix, float *lutr, float *lutg, float *lutb, const int lutsize)
Extract the XYZ->profile matrix and the inverse tone curves from an OUTPUT profile.
int dt_colorspaces_get_matrix_from_input_profile(cmsHPROFILE prof, dt_colormatrix_t matrix, float *lutr, float *lutg, float *lutb, const int lutsize)
Extract the profile->XYZ matrix and the per-channel tone curves from an INPUT profile.
void dt_colorspaces_cleanup_profile(cmsHPROFILE p)
Close a profile created by any of the dt_colorspaces_create_* / dt_colorspaces_get_rgb_profile_from_m...
void dt_colorspaces_lock_profile(const dt_colorspaces_color_profile_t *const profile)
void dt_colorspaces_transform_rgba_float_row(const cmsHTRANSFORM transform, const float *in, float *out, const int width)
Run a caller-owned LCMS transform over one row of RGBA float pixels.
void dt_colorprofiles_get_settings(dt_colorprofiles_settings_t *const out)
Copy the current settings into caller-provided storage, under one lock.
The colour-profile module's API: which profiles exist, and how to apply one.
#define TYPE_XYZA_FLT
lcms2 pixel format for float XYZ + one extra channel.
const dt_colormatrix_t dt_aligned_pixel_t out
dt_store_simd_aligned(out, dt_mat3x4_mul_vec4(vin, dt_colormatrix_row_to_simd(matrix, 0), dt_colormatrix_row_to_simd(matrix, 1), dt_colormatrix_row_to_simd(matrix, 2)))
const dt_colormatrix_t matrix
static cmsHPROFILE _resolve_endpoint(const dt_colorspaces_endpoint_t *const endpoint, const dt_colorspaces_color_profile_t **entry)
const float * dt_colorspaces_conversion_source_coeffs(const dt_colorspaces_conversion_t *const conversion)
The 3x3 power-law fits extrapolating the source curves past white, as one flat array of 9 floats in c...
static gboolean _allocate_curves(float *lut[3])
void dt_colorspaces_apply_conversion_hooked(const dt_colorspaces_conversion_t *const conversion, const float *const in, float *const out, const size_t width, const size_t height, const dt_colorspaces_conversion_hook_t hook)
dt_colorspaces_apply_conversion() with a per-pixel hook. See dt_colorspaces_conversion_hook_t for why...
gboolean dt_colorspaces_conversion_has_clipping(const dt_colorspaces_conversion_t *const conversion)
Whether the conversion has a gamut-clipping stage, i.e. whether a clip endpoint was given AND survive...
gboolean dt_colorspaces_conversion_matrix(const dt_colorspaces_conversion_t *const conversion, dt_colormatrix_t matrix)
The composed source-to-target matrix, row-major.
static uint64_t _hash_endpoint(uint64_t hash, const dt_colorspaces_endpoint_t *const endpoint, const dt_colorspaces_color_profile_t *const entry)
const float * dt_colorspaces_conversion_target_curve(const dt_colorspaces_conversion_t *const conversion, const int channel)
One channel of the target encoding curves. Same contract as dt_colorspaces_conversion_source_curve().
static __DT_CLONE_TARGETS__ void _apply_target_curves(const dt_colorspaces_conversion_t *const c, float *const restrict out, const size_t npixels)
gboolean dt_colorspaces_conversion_is_matrix(const dt_colorspaces_conversion_t *const conversion)
Whether the conversion reduced to matrices and curves, and can therefore be run by a device kernel at...
const float * dt_colorspaces_conversion_target_coeffs(const dt_colorspaces_conversion_t *const conversion)
The same fits for the target curves. Same contract.
static __DT_CLONE_TARGETS__ void _apply_matrix(const dt_colorspaces_conversion_t *const c, const float *const restrict in, float *const restrict out, const size_t npixels, const dt_colorspaces_conversion_hook_t hook)
static __DT_CLONE_TARGETS__ void _apply_lcms2(const dt_colorspaces_conversion_t *const c, const float *const in, float *const out, const size_t width, const size_t height, const dt_colorspaces_conversion_hook_t hook)
static cmsUInt32Number _format_for(cmsHPROFILE profile, gboolean *supported)
#define DT_CONVERSION_MAGIC_LIVE
uint64_t dt_colorspaces_conversion_identity(const dt_colorspaces_conversion_t *const conversion)
What this conversion IS, as a number: equal for two conversions that render the same pixels,...
gboolean dt_colorspaces_conversion_source_matrix(const dt_colorspaces_conversion_t *const conversion, dt_colormatrix_t matrix)
The SOURCE profile's own RGB -> XYZ (D50) matrix, before composition.
static void _free_curves(float *lut[3])
static gboolean _conversion_valid(const dt_colorspaces_conversion_t *const conversion, const char *caller)
Is this pointer a live conversion?
const float * dt_colorspaces_conversion_source_curve(const dt_colorspaces_conversion_t *const conversion, const int channel)
One channel of the source decoding curves, DT_CONVERSION_LUT_SAMPLES entries.
static cmsHPROFILE _quantise_profile(cmsHPROFILE profile)
void dt_colorspaces_free_conversion(dt_colorspaces_conversion_t **conversion)
Release a conversion and NULL the caller's pointer.
gboolean dt_colorspaces_conversion_clip_matrix(const dt_colorspaces_conversion_t *const conversion, dt_colormatrix_t matrix)
The clip-to-target matrix, the second leg of a clipping conversion.
dt_colorspaces_conversion_t * dt_colorspaces_prepare_conversion(const dt_colorspaces_endpoint_t *const from, const dt_colorspaces_endpoint_t *const to, const dt_colorspaces_endpoint_t *const clip, const dt_colorspaces_endpoint_t *const proof, const dt_iop_color_intent_t intent, const dt_colorspaces_conversion_flags_t flags)
Build a conversion from from to to. The expensive call; do it once.
void dt_colorspaces_apply_conversion(const dt_colorspaces_conversion_t *const conversion, const float *const in, float *const out, const size_t width, const size_t height)
Convert a 4-channel float image through a prepared conversion.
#define DT_CONVERSION_MAGIC_DEAD
A PREPARED CONVERSION: everything needed to turn pixels in one colour space into pixels in another,...
dt_colorspaces_conversion_flags_t
What the caller wants, and what the caller can consume.
@ DT_CONVERSION_FORCE_LCMS2
Never take the matrix path, even when both profiles reduce to matrices. Backs the plugins/lighttable/...
@ DT_CONVERSION_SOURCE_CURVES
The caller can apply the SOURCE profile's decoding curves before the matrix. Without this,...
@ DT_CONVERSION_GAMUTCHECK
Mark out-of-gamut pixels rather than merely proofing them. Requires a soft-proof endpoint,...
@ DT_CONVERSION_TARGET_CURVES
The caller can apply the TARGET profile's encoding curves after the matrix. Without this,...
void(* dt_colorspaces_conversion_hook_t)(const float *const in, float *const out)
A per-pixel hook run between the source curves and the colour conversion proper.
#define DT_CONVERSION_LUT_SAMPLES
Entries per tone curve. Callers that upload the curves to a device need this to size the buffer; the ...
int supported(struct dt_imageio_module_storage_t *storage, struct dt_imageio_module_format_t *format)
memcpy(roundtrip, origin, sizeof(roundtrip))
static uint64_t dt_hash(uint64_t hash, const char *str, size_t size)
_lib_location_type_t type
void dt_print(dt_debug_thread_t thread, const char *msg,...) __attribute__((format(printf
Print to stdout when thread is enabled, prefixed with seconds since startup.
float *const restrict const size_t k
float *const restrict const size_t const size_t ch
#define IS_NULL_PTR(p)
C is way too permissive with !=, == and if(var) checks, which can mean too many things depending on w...
float DT_ALIGNED_ARRAY dt_colormatrix_t[4][4]
static void transpose_3xSSE(const dt_colormatrix_t input, dt_colormatrix_t output)
static void dt_colormatrix_mul(dt_colormatrix_t dst, const dt_colormatrix_t m1, const dt_colormatrix_t m2)
#define dt_free_align(ptr)
Release memory from dt_alloc_align() and set ptr to NULL.
static void * dt_calloc_align(size_t size)
dt_alloc_align() followed by a zero fill.
static float * dt_alloc_align_float(size_t pixels)
Allocate pixels floats, cacheline-aligned and marked as such.
#define dt_free(ptr)
g_free() ptr and set it to NULL, skipping both if it is already NULL.
dt_mipmap_buffer_dsc_flags flags
#define __OMP_SIMD__(...)
#define dt_omploop_sfence()
#define __OMP_PARALLEL_FOR__(...)
#define __OMP_PARALLEL_FOR_SIMD__(...)
dt_iop_color_intent_t
ICC rendering intent, as stored in iop params and in conf.
dt_colorspaces_color_profile_type_t
@ DT_COLORSPACE_NONE
No profile / "take it from the image settings". Never matches a list entry.
DT_ALIGNED_PIXEL float dt_aligned_pixel_t[4]
float dt_aligned_pixel_simd_t __attribute__((vector_size(16), aligned(16)))
Apply one channel's tone curve to each of the three colour channels, or pass the channel through unto...
unsigned __int64 uint64_t
Consistent snapshot of the display and soft-proofing settings.
One registered profile: its identity, its LCMS handle, and where it sits in each combo box.
cmsHPROFILE profile
the actual profile; NULL for the three category entries
char filename[DT_IOP_COLOR_ICC_LEN]
icc file name (absolute; compare with dt_colorspaces_is_profile_equal())
dt_colorspaces_color_profile_type_t type
filename is only used for type DT_COLORSPACE_FILE
dt_colormatrix_t clip_matrix
clip -> target; meaningless unless has_clipping
dt_colormatrix_t source_matrix
gboolean have_source_matrix
dt_colorspaces_color_profile_type_t from_type
float coeffs_target[3][3]
dt_colormatrix_t matrix
source -> target, or source -> clip when clipping
cmsHTRANSFORM clip_xform
clip -> target; NULL unless has_clipping
float coeffs_source[3][3]
9 contiguous floats: what the kernels upload verbatim
cmsHTRANSFORM xform
source -> target, or source -> clip when clipping
float * lut_source[3]
NULL when this conversion has no source curve stage.
dt_colorspaces_color_profile_type_t to_type
One end of a conversion: a profile, named either by identity or by handing over one this module alrea...
dt_colorspaces_profile_role_t role
Which role the identity is looked up under. Load-bearing, never a formality: DT_COLORSPACE_SRGB is re...
const struct dt_colorspaces_color_profile_t * resolved
An already-resolved, image-owned profile, or NULL to resolve type / filename from the registered list...
dt_colorspaces_color_profile_type_t type
Profile identity. Ignored when resolved is set, except for tracing.
const char * filename
ICC file name, or "" / NULL for a built-in. Ignored when resolved is set.
#define __DT_CLONE_TARGETS__