/
colorspaces.c
1909 lines (1638 loc) · 69.9 KB
/
colorspaces.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
This file is part of darktable,
copyright (c) 2009--2010 johannes hanika.
copyright (c) 2011--2017 tobias ellinghaus.
darktable is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
darktable is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with darktable. If not, see <http://www.gnu.org/licenses/>.
*/
#include "common/colorspaces.h"
#include "common/colormatrices.c"
#include "common/darktable.h"
#include "common/debug.h"
#include "common/srgb_tone_curve_values.h"
#include "control/conf.h"
#include "control/control.h"
#include "develop/imageop.h"
#include "external/adobe_coeff.c"
#ifdef USE_COLORDGTK
#include "colord-gtk.h"
#endif
#if 0
#include <ApplicationServices/ApplicationServices.h>
#include <Carbon/Carbon.h>
#include <CoreServices/CoreServices.h>
#endif
static const cmsCIEXYZ d65 = {0.95045471, 1.00000000, 1.08905029};
static const cmsCIEXYZTRIPLE rec709_primaries_pre_quantized = {
{0.43603516, 0.22248840, 0.01391602},
{0.38511658, 0.71690369, 0.09706116},
{0.14305115, 0.06060791, 0.71392822}
};
static const cmsCIEXYZTRIPLE rec2020_primaries_prequantized = {
{0.67349243, 0.27903748, -0.00193787},
{0.16566467, 0.67535400, 0.02998352},
{0.12504578, 0.04560852, 0.79685974}
};
static const cmsCIEXYZTRIPLE adobe_primaries_prequantized = {
{0.60974121, 0.31111145, 0.01947021},
{0.20527649, 0.62567139, 0.06086731},
{0.14918518, 0.06321716, 0.74456787}
};
#define generate_mat3inv_body(c_type, A, B) \
int mat3inv_##c_type(c_type *const dst, const c_type *const src) \
{ \
\
const c_type det = A(1, 1) * (A(3, 3) * A(2, 2) - A(3, 2) * A(2, 3)) \
- A(2, 1) * (A(3, 3) * A(1, 2) - A(3, 2) * A(1, 3)) \
+ A(3, 1) * (A(2, 3) * A(1, 2) - A(2, 2) * A(1, 3)); \
\
const c_type epsilon = 1e-7f; \
if(fabs(det) < epsilon) return 1; \
\
const c_type invDet = 1.0 / det; \
\
B(1, 1) = invDet * (A(3, 3) * A(2, 2) - A(3, 2) * A(2, 3)); \
B(1, 2) = -invDet * (A(3, 3) * A(1, 2) - A(3, 2) * A(1, 3)); \
B(1, 3) = invDet * (A(2, 3) * A(1, 2) - A(2, 2) * A(1, 3)); \
\
B(2, 1) = -invDet * (A(3, 3) * A(2, 1) - A(3, 1) * A(2, 3)); \
B(2, 2) = invDet * (A(3, 3) * A(1, 1) - A(3, 1) * A(1, 3)); \
B(2, 3) = -invDet * (A(2, 3) * A(1, 1) - A(2, 1) * A(1, 3)); \
\
B(3, 1) = invDet * (A(3, 2) * A(2, 1) - A(3, 1) * A(2, 2)); \
B(3, 2) = -invDet * (A(3, 2) * A(1, 1) - A(3, 1) * A(1, 2)); \
B(3, 3) = invDet * (A(2, 2) * A(1, 1) - A(2, 1) * A(1, 2)); \
return 0; \
}
#define A(y, x) src[(y - 1) * 3 + (x - 1)]
#define B(y, x) dst[(y - 1) * 3 + (x - 1)]
/** inverts the given 3x3 matrix */
generate_mat3inv_body(float, A, B)
int mat3inv(float *const dst, const float *const src)
{
return mat3inv_float(dst, src);
}
generate_mat3inv_body(double, A, B)
#undef B
#undef A
#undef generate_mat3inv_body
static void mat3mulv(float *dst, const float *const mat, const float *const v)
{
for(int k = 0; k < 3; k++)
{
float x = 0.0f;
for(int i = 0; i < 3; i++) x += mat[3 * k + i] * v[i];
dst[k] = x;
}
}
static void mat3mul(float *dst, const float *const m1, const float *const m2)
{
for(int k = 0; k < 3; k++)
{
for(int i = 0; i < 3; i++)
{
float x = 0.0f;
for(int j = 0; j < 3; j++) x += m1[3 * k + j] * m2[3 * j + i];
dst[3 * k + i] = x;
}
}
}
static const dt_colorspaces_color_profile_t *_get_profile(dt_colorspaces_t *self,
dt_colorspaces_color_profile_type_t type,
const char *filename,
dt_colorspaces_profile_direction_t direction);
static int dt_colorspaces_get_matrix_from_profile(cmsHPROFILE prof, float *matrix, float *lutr, float *lutg,
float *lutb, const int lutsize, const int input,
const int intent)
{
// create an OpenCL processable matrix + tone curves from an cmsHPROFILE:
// check this first:
if(!prof || !cmsIsMatrixShaper(prof)) return 1;
// if this profile contains LUT, it might also contain swapped matrix,
// so the only right way to handle it is to let LCMS apply it.
const int UsedDirection = input ? LCMS_USED_AS_INPUT : LCMS_USED_AS_OUTPUT;
if(cmsIsCLUT(prof, intent, UsedDirection)) return 1;
cmsToneCurve *red_curve = cmsReadTag(prof, cmsSigRedTRCTag);
cmsToneCurve *green_curve = cmsReadTag(prof, cmsSigGreenTRCTag);
cmsToneCurve *blue_curve = cmsReadTag(prof, cmsSigBlueTRCTag);
cmsCIEXYZ *red_color = cmsReadTag(prof, cmsSigRedColorantTag);
cmsCIEXYZ *green_color = cmsReadTag(prof, cmsSigGreenColorantTag);
cmsCIEXYZ *blue_color = cmsReadTag(prof, cmsSigBlueColorantTag);
if(!red_curve || !green_curve || !blue_curve || !red_color || !green_color || !blue_color) return 2;
matrix[0] = red_color->X;
matrix[1] = green_color->X;
matrix[2] = blue_color->X;
matrix[3] = red_color->Y;
matrix[4] = green_color->Y;
matrix[5] = blue_color->Y;
matrix[6] = red_color->Z;
matrix[7] = green_color->Z;
matrix[8] = blue_color->Z;
// some camera ICC profiles claim to have color locations for red, green and blue base colors defined,
// but in fact these are all set to zero. we catch this case here.
float sum = 0.0f;
for(int k = 0; k < 9; k++) sum += matrix[k];
if(sum == 0.0f) return 3;
if(input)
{
// mark as linear, if they are:
if(cmsIsToneCurveLinear(red_curve))
lutr[0] = -1.0f;
else
for(int k = 0; k < lutsize; k++) lutr[k] = cmsEvalToneCurveFloat(red_curve, k / (lutsize - 1.0f));
if(cmsIsToneCurveLinear(green_curve))
lutg[0] = -1.0f;
else
for(int k = 0; k < lutsize; k++) lutg[k] = cmsEvalToneCurveFloat(green_curve, k / (lutsize - 1.0f));
if(cmsIsToneCurveLinear(blue_curve))
lutb[0] = -1.0f;
else
for(int k = 0; k < lutsize; k++) lutb[k] = cmsEvalToneCurveFloat(blue_curve, k / (lutsize - 1.0f));
}
else
{
// invert profile->XYZ matrix for output profiles
float tmp[9];
memcpy(tmp, matrix, sizeof(float) * 9);
if(mat3inv(matrix, tmp)) return 3;
// also need to reverse gamma, to apply reverse before matrix multiplication:
cmsToneCurve *rev_red = cmsReverseToneCurveEx(0x8000, red_curve);
cmsToneCurve *rev_green = cmsReverseToneCurveEx(0x8000, green_curve);
cmsToneCurve *rev_blue = cmsReverseToneCurveEx(0x8000, blue_curve);
if(!rev_red || !rev_green || !rev_blue)
{
cmsFreeToneCurve(rev_red);
cmsFreeToneCurve(rev_green);
cmsFreeToneCurve(rev_blue);
return 4;
}
// pass on tonecurves, in case lutsize > 0:
if(cmsIsToneCurveLinear(red_curve))
lutr[0] = -1.0f;
else
for(int k = 0; k < lutsize; k++) lutr[k] = cmsEvalToneCurveFloat(rev_red, k / (lutsize - 1.0f));
if(cmsIsToneCurveLinear(green_curve))
lutg[0] = -1.0f;
else
for(int k = 0; k < lutsize; k++) lutg[k] = cmsEvalToneCurveFloat(rev_green, k / (lutsize - 1.0f));
if(cmsIsToneCurveLinear(blue_curve))
lutb[0] = -1.0f;
else
for(int k = 0; k < lutsize; k++) lutb[k] = cmsEvalToneCurveFloat(rev_blue, k / (lutsize - 1.0f));
cmsFreeToneCurve(rev_red);
cmsFreeToneCurve(rev_green);
cmsFreeToneCurve(rev_blue);
}
return 0;
}
int dt_colorspaces_get_matrix_from_input_profile(cmsHPROFILE prof, float *matrix, float *lutr, float *lutg,
float *lutb, const int lutsize, const int intent)
{
return dt_colorspaces_get_matrix_from_profile(prof, matrix, lutr, lutg, lutb, lutsize, 1, intent);
}
int dt_colorspaces_get_matrix_from_output_profile(cmsHPROFILE prof, float *matrix, float *lutr, float *lutg,
float *lutb, const int lutsize, const int intent)
{
return dt_colorspaces_get_matrix_from_profile(prof, matrix, lutr, lutg, lutb, lutsize, 0, intent);
}
static cmsHPROFILE dt_colorspaces_create_lab_profile()
{
return cmsCreateLab4Profile(cmsD50_xyY());
}
static cmsHPROFILE _create_lcms_profile(const char *desc, const char *dmdd,
const cmsCIEXYZTRIPLE *primaries, const cmsToneCurve *trc, gboolean v2)
{
cmsCIEXYZ black = { 0, 0, 0 };
cmsHPROFILE profile = cmsCreateProfilePlaceholder(0);
if(!profile) return NULL;
if(v2)
cmsSetProfileVersion(profile, 2.1);
cmsMLU *mlu0 = cmsMLUalloc(NULL, 1);
cmsMLUsetASCII(mlu0, "en", "US", "Public Domain");
cmsMLU *mlu1 = cmsMLUalloc(NULL, 1);
cmsMLUsetASCII(mlu1, "en", "US", desc);
cmsMLU *mlu2 = cmsMLUalloc(NULL, 1);
cmsMLUsetASCII(mlu2, "en", "US", "Darktable");
cmsMLU *mlu3 = cmsMLUalloc(NULL, 1);
cmsMLUsetASCII(mlu3, "en", "US", dmdd);
// this will only be displayed when the embedded profile is read by for example GIMP
cmsWriteTag(profile, cmsSigCopyrightTag, mlu0);
cmsWriteTag(profile, cmsSigProfileDescriptionTag, mlu1);
cmsWriteTag(profile, cmsSigDeviceMfgDescTag, mlu2);
cmsWriteTag(profile, cmsSigDeviceModelDescTag, mlu3);
cmsMLUfree(mlu0);
cmsMLUfree(mlu1);
cmsMLUfree(mlu2);
cmsMLUfree(mlu3);
cmsSetDeviceClass(profile, cmsSigDisplayClass);
cmsSetColorSpace(profile, cmsSigRgbData);
cmsSetPCS(profile, cmsSigXYZData);
cmsWriteTag(profile, cmsSigMediaWhitePointTag, &d65);
cmsWriteTag(profile, cmsSigMediaBlackPointTag, &black);
cmsWriteTag(profile, cmsSigRedColorantTag, (void *)&primaries->Red);
cmsWriteTag(profile, cmsSigGreenColorantTag, (void *)&primaries->Green);
cmsWriteTag(profile, cmsSigBlueColorantTag, (void *)&primaries->Blue);
cmsWriteTag(profile, cmsSigRedTRCTag, (void *)trc);
cmsLinkTag(profile, cmsSigGreenTRCTag, cmsSigRedTRCTag);
cmsLinkTag(profile, cmsSigBlueTRCTag, cmsSigRedTRCTag);
return profile;
}
static cmsHPROFILE _colorspaces_create_srgb_profile(gboolean v2)
{
cmsFloat64Number srgb_parameters[5] = { 2.4, 1.0 / 1.055, 0.055 / 1.055, 1.0 / 12.92, 0.04045 };
cmsToneCurve *transferFunction = cmsBuildParametricToneCurve(NULL, 4, srgb_parameters);
cmsHPROFILE profile = _create_lcms_profile("sRGB", "sRGB",
&rec709_primaries_pre_quantized, transferFunction, v2);
cmsFreeToneCurve(transferFunction);
return profile;
}
static cmsHPROFILE dt_colorspaces_create_srgb_profile()
{
return _colorspaces_create_srgb_profile(TRUE);
}
static cmsHPROFILE dt_colorspaces_create_srgb_profile_v4()
{
return _colorspaces_create_srgb_profile(FALSE);
}
static cmsHPROFILE dt_colorspaces_create_brg_profile()
{
cmsCIEXYZTRIPLE primaries_pre_quantized = {
rec709_primaries_pre_quantized.Blue,
rec709_primaries_pre_quantized.Red,
rec709_primaries_pre_quantized.Green,
};
cmsFloat64Number srgb_parameters[5] = { 2.4, 1.0 / 1.055, 0.055 / 1.055, 1.0 / 12.92, 0.04045 };
cmsToneCurve *transferFunction = cmsBuildParametricToneCurve(NULL, 4, srgb_parameters);
cmsHPROFILE profile = _create_lcms_profile("BRG", "BRG",
&primaries_pre_quantized, transferFunction, TRUE);
cmsFreeToneCurve(transferFunction);
return profile;
}
// Create the ICC virtual profile for adobe rgb space
static cmsHPROFILE dt_colorspaces_create_adobergb_profile(void)
{
// AdobeRGB's "2.2" gamma is technically defined as 2 + 51/256
cmsToneCurve *transferFunction = cmsBuildGamma(NULL, 2.19921875);
cmsHPROFILE profile = _create_lcms_profile("Adobe RGB (compatible)", "Adobe RGB",
&adobe_primaries_prequantized, transferFunction, TRUE);
cmsFreeToneCurve(transferFunction);
return profile;
}
static cmsToneCurve *build_linear_gamma(void)
{
double Parameters[2];
Parameters[0] = 1.0;
Parameters[1] = 0;
return cmsBuildParametricToneCurve(0, 1, Parameters);
}
int dt_colorspaces_get_darktable_matrix(const char *makermodel, float *matrix)
{
dt_profiled_colormatrix_t *preset = NULL;
for(int k = 0; k < dt_profiled_colormatrix_cnt; k++)
{
if(!strcasecmp(makermodel, dt_profiled_colormatrices[k].makermodel))
{
preset = dt_profiled_colormatrices + k;
break;
}
}
if(!preset) return -1;
const float wxyz = preset->white[0] + preset->white[1] + preset->white[2];
const float rxyz = preset->rXYZ[0] + preset->rXYZ[1] + preset->rXYZ[2];
const float gxyz = preset->gXYZ[0] + preset->gXYZ[1] + preset->gXYZ[2];
const float bxyz = preset->bXYZ[0] + preset->bXYZ[1] + preset->bXYZ[2];
const float xn = preset->white[0] / wxyz;
const float yn = preset->white[1] / wxyz;
const float xr = preset->rXYZ[0] / rxyz;
const float yr = preset->rXYZ[1] / rxyz;
const float xg = preset->gXYZ[0] / gxyz;
const float yg = preset->gXYZ[1] / gxyz;
const float xb = preset->bXYZ[0] / bxyz;
const float yb = preset->bXYZ[1] / bxyz;
const float primaries[9] = { xr, xg, xb, yr, yg, yb, 1.0f - xr - yr, 1.0f - xg - yg, 1.0f - xb - yb };
float result[9];
if(mat3inv(result, primaries)) return -1;
const float whitepoint[3] = { xn / yn, 1.0f, (1.0f - xn - yn) / yn };
float coeff[3];
// get inverse primary whitepoint
mat3mulv(coeff, result, whitepoint);
float tmp[9] = { coeff[0] * xr, coeff[1] * xg, coeff[2] * xb, coeff[0] * yr, coeff[1] * yg, coeff[2] * yb,
coeff[0] * (1.0f - xr - yr), coeff[1] * (1.0f - xg - yg), coeff[2] * (1.0f - xb - yb) };
// input whitepoint[] in XYZ with Y normalized to 1.0f
const float dn[3]
= { preset->white[0] / (float)preset->white[1], 1.0f, preset->white[2] / (float)preset->white[1] };
const float lam_rigg[9] = { 0.8951, 0.2664, -0.1614, -0.7502, 1.7135, 0.0367, 0.0389, -0.0685, 1.0296 };
const float d50[3] = { 0.9642, 1.0, 0.8249 };
// adapt to d50
float chad_inv[9];
if(mat3inv(chad_inv, lam_rigg)) return -1;
float cone_src_rgb[3], cone_dst_rgb[3];
mat3mulv(cone_src_rgb, lam_rigg, dn);
mat3mulv(cone_dst_rgb, lam_rigg, d50);
const float cone[9]
= { cone_dst_rgb[0] / cone_src_rgb[0], 0.0f, 0.0f, 0.0f, cone_dst_rgb[1] / cone_src_rgb[1], 0.0f, 0.0f,
0.0f, cone_dst_rgb[2] / cone_src_rgb[2] };
float tmp2[9];
float bradford[9];
mat3mul(tmp2, cone, lam_rigg);
mat3mul(bradford, chad_inv, tmp2);
mat3mul(matrix, bradford, tmp);
return 0;
}
cmsHPROFILE dt_colorspaces_create_alternate_profile(const char *makermodel)
{
dt_profiled_colormatrix_t *preset = NULL;
for(int k = 0; k < dt_alternate_colormatrix_cnt; k++)
{
if(!strcmp(makermodel, dt_alternate_colormatrices[k].makermodel))
{
preset = dt_alternate_colormatrices + k;
break;
}
}
if(!preset) return NULL;
const float wxyz = preset->white[0] + preset->white[1] + preset->white[2];
const float rxyz = preset->rXYZ[0] + preset->rXYZ[1] + preset->rXYZ[2];
const float gxyz = preset->gXYZ[0] + preset->gXYZ[1] + preset->gXYZ[2];
const float bxyz = preset->bXYZ[0] + preset->bXYZ[1] + preset->bXYZ[2];
cmsCIExyY WP = { preset->white[0] / wxyz, preset->white[1] / wxyz, 1.0 };
cmsCIExyYTRIPLE XYZPrimaries = { { preset->rXYZ[0] / rxyz, preset->rXYZ[1] / rxyz, 1.0 },
{ preset->gXYZ[0] / gxyz, preset->gXYZ[1] / gxyz, 1.0 },
{ preset->bXYZ[0] / bxyz, preset->bXYZ[1] / bxyz, 1.0 } };
cmsToneCurve *Gamma[3];
cmsHPROFILE hp;
Gamma[0] = Gamma[1] = Gamma[2] = build_linear_gamma();
hp = cmsCreateRGBProfile(&WP, &XYZPrimaries, Gamma);
cmsFreeToneCurve(Gamma[0]);
if(hp == NULL) return NULL;
char name[512];
snprintf(name, sizeof(name), "darktable alternate %s", makermodel);
cmsSetProfileVersion(hp, 2.1);
cmsMLU *mlu0 = cmsMLUalloc(NULL, 1);
cmsMLUsetASCII(mlu0, "en", "US", "(dt internal)");
cmsMLU *mlu1 = cmsMLUalloc(NULL, 1);
cmsMLUsetASCII(mlu1, "en", "US", name);
cmsMLU *mlu2 = cmsMLUalloc(NULL, 1);
cmsMLUsetASCII(mlu2, "en", "US", name);
cmsWriteTag(hp, cmsSigDeviceMfgDescTag, mlu0);
cmsWriteTag(hp, cmsSigDeviceModelDescTag, mlu1);
// this will only be displayed when the embedded profile is read by for example GIMP
cmsWriteTag(hp, cmsSigProfileDescriptionTag, mlu2);
cmsMLUfree(mlu0);
cmsMLUfree(mlu1);
cmsMLUfree(mlu2);
return hp;
}
cmsHPROFILE dt_colorspaces_create_vendor_profile(const char *makermodel)
{
dt_profiled_colormatrix_t *preset = NULL;
for(int k = 0; k < dt_vendor_colormatrix_cnt; k++)
{
if(!strcmp(makermodel, dt_vendor_colormatrices[k].makermodel))
{
preset = dt_vendor_colormatrices + k;
break;
}
}
if(!preset) return NULL;
const float wxyz = preset->white[0] + preset->white[1] + preset->white[2];
const float rxyz = preset->rXYZ[0] + preset->rXYZ[1] + preset->rXYZ[2];
const float gxyz = preset->gXYZ[0] + preset->gXYZ[1] + preset->gXYZ[2];
const float bxyz = preset->bXYZ[0] + preset->bXYZ[1] + preset->bXYZ[2];
cmsCIExyY WP = { preset->white[0] / wxyz, preset->white[1] / wxyz, 1.0 };
cmsCIExyYTRIPLE XYZPrimaries = { { preset->rXYZ[0] / rxyz, preset->rXYZ[1] / rxyz, 1.0 },
{ preset->gXYZ[0] / gxyz, preset->gXYZ[1] / gxyz, 1.0 },
{ preset->bXYZ[0] / bxyz, preset->bXYZ[1] / bxyz, 1.0 } };
cmsToneCurve *Gamma[3];
cmsHPROFILE hp;
Gamma[0] = Gamma[1] = Gamma[2] = build_linear_gamma();
hp = cmsCreateRGBProfile(&WP, &XYZPrimaries, Gamma);
cmsFreeToneCurve(Gamma[0]);
if(hp == NULL) return NULL;
char name[512];
snprintf(name, sizeof(name), "darktable vendor %s", makermodel);
cmsSetProfileVersion(hp, 2.1);
cmsMLU *mlu0 = cmsMLUalloc(NULL, 1);
cmsMLUsetASCII(mlu0, "en", "US", "(dt internal)");
cmsMLU *mlu1 = cmsMLUalloc(NULL, 1);
cmsMLUsetASCII(mlu1, "en", "US", name);
cmsMLU *mlu2 = cmsMLUalloc(NULL, 1);
cmsMLUsetASCII(mlu2, "en", "US", name);
cmsWriteTag(hp, cmsSigDeviceMfgDescTag, mlu0);
cmsWriteTag(hp, cmsSigDeviceModelDescTag, mlu1);
// this will only be displayed when the embedded profile is read by for example GIMP
cmsWriteTag(hp, cmsSigProfileDescriptionTag, mlu2);
cmsMLUfree(mlu0);
cmsMLUfree(mlu1);
cmsMLUfree(mlu2);
return hp;
}
cmsHPROFILE dt_colorspaces_create_darktable_profile(const char *makermodel)
{
dt_profiled_colormatrix_t *preset = NULL;
for(int k = 0; k < dt_profiled_colormatrix_cnt; k++)
{
if(!strcasecmp(makermodel, dt_profiled_colormatrices[k].makermodel))
{
preset = dt_profiled_colormatrices + k;
break;
}
}
if(!preset) return NULL;
const float wxyz = preset->white[0] + preset->white[1] + preset->white[2];
const float rxyz = preset->rXYZ[0] + preset->rXYZ[1] + preset->rXYZ[2];
const float gxyz = preset->gXYZ[0] + preset->gXYZ[1] + preset->gXYZ[2];
const float bxyz = preset->bXYZ[0] + preset->bXYZ[1] + preset->bXYZ[2];
cmsCIExyY WP = { preset->white[0] / wxyz, preset->white[1] / wxyz, 1.0 };
cmsCIExyYTRIPLE XYZPrimaries = { { preset->rXYZ[0] / rxyz, preset->rXYZ[1] / rxyz, 1.0 },
{ preset->gXYZ[0] / gxyz, preset->gXYZ[1] / gxyz, 1.0 },
{ preset->bXYZ[0] / bxyz, preset->bXYZ[1] / bxyz, 1.0 } };
cmsToneCurve *Gamma[3];
cmsHPROFILE hp;
Gamma[0] = Gamma[1] = Gamma[2] = build_linear_gamma();
hp = cmsCreateRGBProfile(&WP, &XYZPrimaries, Gamma);
cmsFreeToneCurve(Gamma[0]);
if(hp == NULL) return NULL;
char name[512];
snprintf(name, sizeof(name), "Darktable profiled %s", makermodel);
cmsSetProfileVersion(hp, 2.1);
cmsMLU *mlu0 = cmsMLUalloc(NULL, 1);
cmsMLUsetASCII(mlu0, "en", "US", "(dt internal)");
cmsMLU *mlu1 = cmsMLUalloc(NULL, 1);
cmsMLUsetASCII(mlu1, "en", "US", name);
cmsMLU *mlu2 = cmsMLUalloc(NULL, 1);
cmsMLUsetASCII(mlu2, "en", "US", name);
cmsWriteTag(hp, cmsSigDeviceMfgDescTag, mlu0);
cmsWriteTag(hp, cmsSigDeviceModelDescTag, mlu1);
// this will only be displayed when the embedded profile is read by for example GIMP
cmsWriteTag(hp, cmsSigProfileDescriptionTag, mlu2);
cmsMLUfree(mlu0);
cmsMLUfree(mlu1);
cmsMLUfree(mlu2);
return hp;
}
static cmsHPROFILE dt_colorspaces_create_xyz_profile(void)
{
cmsHPROFILE hXYZ = cmsCreateXYZProfile();
cmsSetPCS(hXYZ, cmsSigXYZData);
cmsSetHeaderRenderingIntent(hXYZ, INTENT_PERCEPTUAL);
if(hXYZ == NULL) return NULL;
cmsSetProfileVersion(hXYZ, 2.1);
cmsMLU *mlu0 = cmsMLUalloc(NULL, 1);
cmsMLUsetASCII(mlu0, "en", "US", "(dt internal)");
cmsMLU *mlu1 = cmsMLUalloc(NULL, 1);
cmsMLUsetASCII(mlu1, "en", "US", "linear XYZ");
cmsMLU *mlu2 = cmsMLUalloc(NULL, 1);
cmsMLUsetASCII(mlu2, "en", "US", "Darktable linear XYZ");
cmsWriteTag(hXYZ, cmsSigDeviceMfgDescTag, mlu0);
cmsWriteTag(hXYZ, cmsSigDeviceModelDescTag, mlu1);
// this will only be displayed when the embedded profile is read by for example GIMP
cmsWriteTag(hXYZ, cmsSigProfileDescriptionTag, mlu2);
cmsMLUfree(mlu0);
cmsMLUfree(mlu1);
cmsMLUfree(mlu2);
return hXYZ;
}
static cmsHPROFILE dt_colorspaces_create_linear_rec709_rgb_profile(void)
{
cmsToneCurve *transferFunction = build_linear_gamma();
cmsHPROFILE profile = _create_lcms_profile("Linear Rec709 RGB", "Linear Rec709 RGB",
&rec709_primaries_pre_quantized, transferFunction, TRUE);
cmsFreeToneCurve(transferFunction);
return profile;
}
static cmsHPROFILE dt_colorspaces_create_linear_rec2020_rgb_profile(void)
{
cmsToneCurve *transferFunction = build_linear_gamma();
cmsHPROFILE profile = _create_lcms_profile("Linear Rec2020 RGB", "Linear Rec2020 RGB",
&rec2020_primaries_prequantized, transferFunction, TRUE);
cmsFreeToneCurve(transferFunction);
return profile;
}
static cmsHPROFILE dt_colorspaces_create_linear_infrared_profile(void)
{
// linear rgb with r and b swapped:
cmsCIEXYZTRIPLE primaries_pre_quantized = {
rec709_primaries_pre_quantized.Blue,
rec709_primaries_pre_quantized.Green,
rec709_primaries_pre_quantized.Red,
};
cmsToneCurve *transferFunction = build_linear_gamma();
cmsHPROFILE profile = _create_lcms_profile("linear infrared bgr", "Darktable Linear Infrared BGR",
&primaries_pre_quantized, transferFunction, FALSE);
cmsFreeToneCurve(transferFunction);
return profile;
}
const dt_colorspaces_color_profile_t *dt_colorspaces_get_output_profile(const int imgid,
dt_colorspaces_color_profile_type_t over_type,
const char *over_filename)
{
// find the colorout module -- the pointer stays valid until darktable shuts down
static dt_iop_module_so_t *colorout = NULL;
if(colorout == NULL)
{
GList *modules = g_list_first(darktable.iop);
while(modules)
{
dt_iop_module_so_t *module = (dt_iop_module_so_t *)(modules->data);
if(!strcmp(module->op, "colorout"))
{
colorout = module;
break;
}
modules = g_list_next(modules);
}
}
const dt_colorspaces_color_profile_t *p = NULL;
if(over_type != DT_COLORSPACE_NONE)
{
// return the profile specified in export.
// we have that in here to get rid of the if() check in all places calling this function.
p = dt_colorspaces_get_profile(over_type, over_filename, DT_PROFILE_DIRECTION_OUT | DT_PROFILE_DIRECTION_DISPLAY);
}
else if(colorout && colorout->get_p)
{
// get the profile assigned from colorout
// FIXME: does this work when using JPEG thumbs and the image was never opened?
sqlite3_stmt *stmt;
DT_DEBUG_SQLITE3_PREPARE_V2(
dt_database_get(darktable.db),
"SELECT op_params FROM main.history WHERE imgid=?1 AND operation='colorout' ORDER BY num DESC LIMIT 1", -1,
&stmt, NULL);
DT_DEBUG_SQLITE3_BIND_INT(stmt, 1, imgid);
if(sqlite3_step(stmt) == SQLITE_ROW)
{
// use introspection to get the profile name from the binary params blob
const void *params = sqlite3_column_blob(stmt, 0);
dt_colorspaces_color_profile_type_t *type = colorout->get_p(params, "type");
char *filename = colorout->get_p(params, "filename");
if(type && filename) p = dt_colorspaces_get_profile(*type, filename,
DT_PROFILE_DIRECTION_OUT | DT_PROFILE_DIRECTION_DISPLAY);
}
sqlite3_finalize(stmt);
}
// if all else fails -> fall back to sRGB
if(!p) p = dt_colorspaces_get_profile(DT_COLORSPACE_SRGB, "", DT_PROFILE_DIRECTION_OUT);
return p;
}
#if 0
static void dt_colorspaces_create_cmatrix(float cmatrix[4][3], float mat[3][3])
{
// sRGB D65, the linear part:
const float rgb_to_xyz[3][3] = { { 0.4124564, 0.3575761, 0.1804375 },
{ 0.2126729, 0.7151522, 0.0721750 },
{ 0.0193339, 0.1191920, 0.9503041 } };
for(int c = 0; c < 3; c++)
{
for(int j = 0; j < 3; j++)
{
mat[c][j] = 0.0f;
for(int k = 0; k < 3; k++)
{
mat[c][j] += rgb_to_xyz[k][j] * cmatrix[c][k];
}
}
}
}
#endif
static cmsHPROFILE dt_colorspaces_create_xyzmatrix_profile(float mat[3][3])
{
// mat: cam -> xyz
float x[3], y[3];
for(int k = 0; k < 3; k++)
{
const float norm = mat[0][k] + mat[1][k] + mat[2][k];
x[k] = mat[0][k] / norm;
y[k] = mat[1][k] / norm;
}
cmsCIExyYTRIPLE CameraPrimaries = { { x[0], y[0], 1.0 }, { x[1], y[1], 1.0 }, { x[2], y[2], 1.0 } };
cmsHPROFILE profile;
cmsCIExyY D65;
cmsXYZ2xyY(&D65, &d65);
cmsToneCurve *Gamma[3];
Gamma[0] = Gamma[1] = Gamma[2] = build_linear_gamma();
profile = cmsCreateRGBProfile(&D65, &CameraPrimaries, Gamma);
cmsFreeToneCurve(Gamma[0]);
if(profile == NULL) return NULL;
cmsSetProfileVersion(profile, 2.1);
cmsMLU *mlu0 = cmsMLUalloc(NULL, 1);
cmsMLUsetASCII(mlu0, "en", "US", "(dt internal)");
cmsMLU *mlu1 = cmsMLUalloc(NULL, 1);
cmsMLUsetASCII(mlu1, "en", "US", "color matrix built-in");
cmsMLU *mlu2 = cmsMLUalloc(NULL, 1);
cmsMLUsetASCII(mlu2, "en", "US", "color matrix built-in");
cmsWriteTag(profile, cmsSigDeviceMfgDescTag, mlu0);
cmsWriteTag(profile, cmsSigDeviceModelDescTag, mlu1);
// this will only be displayed when the embedded profile is read by for example GIMP
cmsWriteTag(profile, cmsSigProfileDescriptionTag, mlu2);
cmsMLUfree(mlu0);
cmsMLUfree(mlu1);
cmsMLUfree(mlu2);
return profile;
}
cmsHPROFILE dt_colorspaces_create_xyzimatrix_profile(float mat[3][3])
{
// mat: xyz -> cam
float imat[3][3];
mat3inv((float *)imat, (float *)mat);
return dt_colorspaces_create_xyzmatrix_profile(imat);
}
static cmsHPROFILE _ensure_rgb_profile(cmsHPROFILE profile)
{
if(profile && cmsGetColorSpace(profile) == cmsSigGrayData)
{
cmsToneCurve *trc = cmsReadTag(profile, cmsSigGrayTRCTag);
cmsCIEXYZ *wtpt = cmsReadTag(profile, cmsSigMediaWhitePointTag);
cmsCIEXYZ *bkpt = cmsReadTag(profile, cmsSigMediaBlackPointTag);
cmsCIEXYZ *chad = cmsReadTag(profile, cmsSigChromaticAdaptationTag);
cmsMLU *cprt = cmsReadTag(profile, cmsSigCopyrightTag);
cmsMLU *desc = cmsReadTag(profile, cmsSigProfileDescriptionTag);
cmsMLU *dmnd = cmsReadTag(profile, cmsSigDeviceMfgDescTag);
cmsMLU *dmdd = cmsReadTag(profile, cmsSigDeviceModelDescTag);
cmsHPROFILE rgb_profile = cmsCreateProfilePlaceholder(0);
cmsSetDeviceClass(rgb_profile, cmsSigDisplayClass);
cmsSetColorSpace(rgb_profile, cmsSigRgbData);
cmsSetPCS(rgb_profile, cmsSigXYZData);
cmsWriteTag(rgb_profile, cmsSigCopyrightTag, cprt);
cmsWriteTag(rgb_profile, cmsSigProfileDescriptionTag, desc);
cmsWriteTag(rgb_profile, cmsSigDeviceMfgDescTag, dmnd);
cmsWriteTag(rgb_profile, cmsSigDeviceModelDescTag, dmdd);
cmsWriteTag(rgb_profile, cmsSigMediaBlackPointTag, bkpt);
cmsWriteTag(rgb_profile, cmsSigMediaWhitePointTag, wtpt);
cmsWriteTag(rgb_profile, cmsSigChromaticAdaptationTag, chad);
cmsSetColorSpace(rgb_profile, cmsSigRgbData);
cmsSetPCS(rgb_profile, cmsSigXYZData);
cmsWriteTag(rgb_profile, cmsSigRedColorantTag, (void *)&rec709_primaries_pre_quantized.Red);
cmsWriteTag(rgb_profile, cmsSigGreenColorantTag, (void *)&rec709_primaries_pre_quantized.Green);
cmsWriteTag(rgb_profile, cmsSigBlueColorantTag, (void *)&rec709_primaries_pre_quantized.Blue);
cmsWriteTag(rgb_profile, cmsSigRedTRCTag, (void *)trc);
cmsLinkTag(rgb_profile, cmsSigGreenTRCTag, cmsSigRedTRCTag);
cmsLinkTag(rgb_profile, cmsSigBlueTRCTag, cmsSigRedTRCTag);
cmsCloseProfile(profile);
profile = rgb_profile;
}
return profile;
}
cmsHPROFILE dt_colorspaces_get_rgb_profile_from_mem(uint8_t *data, uint32_t size)
{
cmsHPROFILE profile = _ensure_rgb_profile(cmsOpenProfileFromMem(data, size));
return profile;
}
void dt_colorspaces_cleanup_profile(cmsHPROFILE p)
{
if(!p) return;
cmsCloseProfile(p);
}
void dt_colorspaces_get_profile_name(cmsHPROFILE p, const char *language, const char *country, char *name,
size_t len)
{
cmsUInt32Number size;
gchar *buf = NULL;
wchar_t *wbuf = NULL;
gchar *utf8 = NULL;
size = cmsGetProfileInfoASCII(p, cmsInfoDescription, language, country, NULL, 0);
if(size == 0) goto error;
buf = (char *)calloc(size + 1, sizeof(char));
size = cmsGetProfileInfoASCII(p, cmsInfoDescription, language, country, buf, size);
if(size == 0) goto error;
// most unix like systems should work with this, but at least Windows doesn't
if(sizeof(wchar_t) != 4 || g_utf8_validate(buf, -1, NULL))
g_strlcpy(name, buf, len); // better a little weird than totally borked
else
{
wbuf = (wchar_t *)calloc(size + 1, sizeof(wchar_t));
size = cmsGetProfileInfo(p, cmsInfoDescription, language, country, wbuf, sizeof(wchar_t) * size);
if(size == 0) goto error;
utf8 = g_ucs4_to_utf8((gunichar *)wbuf, -1, NULL, NULL, NULL);
if(!utf8) goto error;
g_strlcpy(name, utf8, len);
}
free(buf);
free(wbuf);
g_free(utf8);
return;
error:
if(buf)
g_strlcpy(name, buf, len); // better a little weird than totally borked
else
*name = '\0'; // nothing to do here
free(buf);
free(wbuf);
g_free(utf8);
}
void rgb2hsl(const float rgb[3], float *h, float *s, float *l)
{
const float r = rgb[0], g = rgb[1], b = rgb[2];
float pmax = fmax(r, fmax(g, b));
float pmin = fmin(r, fmin(g, b));
float delta = (pmax - pmin);
float hv = 0, sv = 0, lv = (pmin + pmax) / 2.0;
if(pmax != pmin)
{
sv = lv < 0.5 ? delta / (pmax + pmin) : delta / (2.0 - pmax - pmin);
if(pmax == r)
hv = (g - b) / delta;
else if(pmax == g)
hv = 2.0 + (b - r) / delta;
else if(pmax == b)
hv = 4.0 + (r - g) / delta;
hv /= 6.0;
if(hv < 0.0)
hv += 1.0;
else if(hv > 1.0)
hv -= 1.0;
}
*h = hv;
*s = sv;
*l = lv;
}
static inline float hue2rgb(float m1, float m2, float hue)
{
if(hue < 0.0)
hue += 1.0;
else if(hue > 1.0)
hue -= 1.0;
if(hue < 1.0 / 6.0)
return (m1 + (m2 - m1) * hue * 6.0);
else if(hue < 1.0 / 2.0)
return m2;
else if(hue < 2.0 / 3.0)
return (m1 + (m2 - m1) * ((2.0 / 3.0) - hue) * 6.0);
else
return m1;
}
void hsl2rgb(float rgb[3], float h, float s, float l)
{
float m1, m2;
if(s == 0)
{
rgb[0] = rgb[1] = rgb[2] = l;
return;
}
m2 = l < 0.5 ? l * (1.0 + s) : l + s - l * s;
m1 = (2.0 * l - m2);
rgb[0] = hue2rgb(m1, m2, h + (1.0 / 3.0));
rgb[1] = hue2rgb(m1, m2, h);
rgb[2] = hue2rgb(m1, m2, h - (1.0 / 3.0));
}
static const char *_profile_names[]
= { "", // 0th entry is a dummy for DT_COLORSPACE_FILE and not used
N_("sRGB"), // this is only used in error messages, no need for the (...) description
N_("Adobe RGB (compatible)"),
N_("linear Rec709 RGB"),
N_("linear Rec2020 RGB"),
N_("linear XYZ"),
N_("Lab"),
N_("linear infrared BGR"),
N_("system display profile"),
N_("embedded ICC profile"),
N_("embedded matrix"),
N_("standard color matrix"),
N_("enhanced color matrix"),
N_("vendor color matrix"),
N_("alternate color matrix"),
N_("BRG (experimental)"),
N_("export profile"),
N_("softproof profile"),
N_("work profile"),
N_("system display profile") };
static dt_colorspaces_color_profile_t *_create_profile(dt_colorspaces_color_profile_type_t type,
cmsHPROFILE profile, const char *name, int in_pos,
int out_pos, int display_pos, int category_pos,
int work_pos, int display2_pos)
{
dt_colorspaces_color_profile_t *prof;
prof = (dt_colorspaces_color_profile_t *)calloc(1, sizeof(dt_colorspaces_color_profile_t));
prof->type = type;
g_strlcpy(prof->name, name, sizeof(prof->name));
prof->profile = profile;
prof->in_pos = in_pos;
prof->out_pos = out_pos;
prof->display_pos = display_pos;
prof->category_pos = category_pos;
prof->work_pos = work_pos;
prof->display2_pos = display2_pos;
return prof;
}
// this function is basically thread safe, at least when not called on the global darktable.color_profiles
static void _update_display_transforms(dt_colorspaces_t *self)
{
if(self->transform_srgb_to_display) cmsDeleteTransform(self->transform_srgb_to_display);
self->transform_srgb_to_display = NULL;
if(self->transform_adobe_rgb_to_display) cmsDeleteTransform(self->transform_adobe_rgb_to_display);
self->transform_adobe_rgb_to_display = NULL;
const dt_colorspaces_color_profile_t *display_dt_profile = _get_profile(self, self->display_type,
self->display_filename,
DT_PROFILE_DIRECTION_DISPLAY);
if(!display_dt_profile) return;
cmsHPROFILE display_profile = display_dt_profile->profile;
if(!display_profile) return;
self->transform_srgb_to_display = cmsCreateTransform(_get_profile(self, DT_COLORSPACE_SRGB, "",
DT_PROFILE_DIRECTION_DISPLAY)->profile,
TYPE_RGBA_8,
display_profile,
TYPE_BGRA_8,
self->display_intent,
0);
self->transform_adobe_rgb_to_display = cmsCreateTransform(_get_profile(self, DT_COLORSPACE_ADOBERGB, "",
DT_PROFILE_DIRECTION_DISPLAY)->profile,
TYPE_RGBA_8,
display_profile,