-
Notifications
You must be signed in to change notification settings - Fork 304
/
color.cc
6841 lines (5743 loc) · 222 KB
/
color.cc
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 RawTherapee.
*
* Copyright (c) 2004-2010 Gabor Horvath <hgabor@rawtherapee.com>
*
* RawTherapee 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.
*
* RawTherapee 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 RawTherapee. If not, see <http://www.gnu.org/licenses/>.
*/
#include "rtengine.h"
#include "color.h"
#include "iccmatrices.h"
#include "mytime.h"
#include "sleef.c"
#include "opthelper.h"
#include "iccstore.h"
#define pow_F(a,b) (xexpf(b*xlogf(a)))
using namespace std;
namespace rtengine
{
extern const Settings* settings;
cmsToneCurve* Color::linearGammaTRC;
LUTf Color::cachef;
LUTf Color::gamma2curve;
LUTf Color::gammatab;
LUTuc Color::gammatabThumb;
LUTf Color::igammatab_srgb;
LUTf Color::igammatab_srgb1;
LUTf Color::gammatab_srgb;
LUTf Color::gammatab_srgb1;
LUTf Color::denoiseGammaTab;
LUTf Color::denoiseIGammaTab;
LUTf Color::igammatab_24_17;
LUTf Color::gammatab_24_17a;
LUTf Color::gammatab_13_2;
LUTf Color::igammatab_13_2;
LUTf Color::gammatab_115_2;
LUTf Color::igammatab_115_2;
LUTf Color::gammatab_145_3;
LUTf Color::igammatab_145_3;
// Wikipedia sRGB: Unlike most other RGB color spaces, the sRGB gamma cannot be expressed as a single numerical value.
// The overall gamma is approximately 2.2, consisting of a linear (gamma 1.0) section near black, and a non-linear section elsewhere involving a 2.4 exponent
// and a gamma (slope of log output versus log input) changing from 1.0 through about 2.3.
const double Color::sRGBGamma = 2.2;
const double Color::sRGBGammaCurve = 2.4;
const double Color::eps_max = 580.40756; //(MAXVALF* 216.0f/24389.0);
const double Color::eps = 216.0f / 24389.0; //0.008856
const double Color::kappa = 24389.0 / 27.0; //903.29630;
const float Color::D50x = 0.9642f; //0.96422;
const float Color::D50z = 0.8249f; //0.82521;
const double Color::u0 = 4.0 * D50x / (D50x + 15 + 3 * D50z);
const double Color::v0 = 9.0 / (D50x + 15 + 3 * D50z);
const double Color::epskap = 8.0;
/*
* Munsell Lch correction
* Copyright (c) 2011 Jacques Desmis <jdesmis@gmail.com>
*/
// Munsell Lch LUTf : 195 LUT
// about 70% data are corrected with significative corrections
// almost all data are taken for BG, YR, G excepted a few extreme values with a slight correction
// No LUTf for BG and Y : low corrections
// Only between 5B and 5PB for L > 40 : under very low corrections for L < 40
//give hue in function of L and C : Munsell correction
LUTf Color::_4P10, Color::_4P20, Color::_4P30, Color::_4P40, Color::_4P50, Color::_4P60;
LUTf Color::_1P10, Color::_1P20, Color::_1P30, Color::_1P40, Color::_1P50, Color::_1P60;
LUTf Color::_10PB10, Color::_10PB20, Color::_10PB30, Color::_10PB40, Color::_10PB50, Color::_10PB60;
LUTf Color::_9PB10, Color::_9PB20, Color::_9PB30, Color::_9PB40, Color::_9PB50, Color::_9PB60, Color::_9PB70, Color::_9PB80;
LUTf Color::_75PB10, Color::_75PB20, Color::_75PB30, Color::_75PB40, Color::_75PB50, Color::_75PB60, Color::_75PB70, Color::_75PB80;
LUTf Color::_6PB10, Color::_6PB20, Color::_6PB30, Color::_6PB40, Color::_6PB50, Color::_6PB60, Color::_6PB70, Color::_6PB80;
LUTf Color::_45PB10, Color::_45PB20, Color::_45PB30, Color::_45PB40, Color::_45PB50, Color::_45PB60, Color::_45PB70, Color::_45PB80;
LUTf Color::_3PB10, Color::_3PB20, Color::_3PB30, Color::_3PB40, Color::_3PB50, Color::_3PB60, Color::_3PB70, Color::_3PB80;
LUTf Color::_15PB10, Color::_15PB20, Color::_15PB30, Color::_15PB40, Color::_15PB50, Color::_15PB60, Color::_15PB70, Color::_15PB80;
LUTf Color::_05PB40, Color::_05PB50, Color::_05PB60, Color::_05PB70, Color::_05PB80;
LUTf Color::_10B40, Color::_10B50, Color::_10B60, Color::_10B70, Color::_10B80;
LUTf Color::_9B40, Color::_9B50, Color::_9B60, Color::_9B70, Color::_9B80;
LUTf Color::_7B40, Color::_7B50, Color::_7B60, Color::_7B70, Color::_7B80;
LUTf Color::_5B40, Color::_5B50, Color::_5B60, Color::_5B70, Color::_5B80;
LUTf Color::_10YR20, Color::_10YR30, Color::_10YR40, Color::_10YR50, Color::_10YR60, Color::_10YR70, Color::_10YR80, Color::_10YR90;
LUTf Color::_85YR20, Color::_85YR30, Color::_85YR40, Color::_85YR50, Color::_85YR60, Color::_85YR70, Color::_85YR80, Color::_85YR90;
LUTf Color::_7YR30, Color::_7YR40, Color::_7YR50, Color::_7YR60, Color::_7YR70, Color::_7YR80;
LUTf Color::_55YR30, Color::_55YR40, Color::_55YR50, Color::_55YR60, Color::_55YR70, Color::_55YR80, Color::_55YR90;
LUTf Color::_4YR30, Color::_4YR40, Color::_4YR50, Color::_4YR60, Color::_4YR70, Color::_4YR80;
LUTf Color::_25YR30, Color::_25YR40, Color::_25YR50, Color::_25YR60, Color::_25YR70;
LUTf Color::_10R30, Color::_10R40, Color::_10R50, Color::_10R60, Color::_10R70;
LUTf Color::_9R30, Color::_9R40, Color::_9R50, Color::_9R60, Color::_9R70;
LUTf Color::_7R30, Color::_7R40, Color::_7R50, Color::_7R60, Color::_7R70;
LUTf Color::_5R10, Color::_5R20, Color::_5R30;
LUTf Color::_25R10, Color::_25R20, Color::_25R30;
LUTf Color::_10RP10, Color::_10RP20, Color::_10RP30;
LUTf Color::_7G30, Color::_7G40, Color::_7G50, Color::_7G60, Color::_7G70, Color::_7G80;
LUTf Color::_5G30, Color::_5G40, Color::_5G50, Color::_5G60, Color::_5G70, Color::_5G80;
LUTf Color::_25G30, Color::_25G40, Color::_25G50, Color::_25G60, Color::_25G70, Color::_25G80;
LUTf Color::_1G30, Color::_1G40, Color::_1G50, Color::_1G60, Color::_1G70, Color::_1G80;
LUTf Color::_10GY30, Color::_10GY40, Color::_10GY50, Color::_10GY60, Color::_10GY70, Color::_10GY80;
LUTf Color::_75GY30, Color::_75GY40, Color::_75GY50, Color::_75GY60, Color::_75GY70, Color::_75GY80;
LUTf Color::_5GY30, Color::_5GY40, Color::_5GY50, Color::_5GY60, Color::_5GY70, Color::_5GY80;
#ifdef _DEBUG
MunsellDebugInfo::MunsellDebugInfo()
{
reinitValues();
}
void MunsellDebugInfo::reinitValues()
{
maxdhue[0] = maxdhue[1] = maxdhue[2] = maxdhue[3] = 0.0f;
maxdhuelum[0] = maxdhuelum[1] = maxdhuelum[2] = maxdhuelum[3] = 0.0f;
depass = depassLum = 0;
}
#endif
void Color::init ()
{
/*******************************************/
constexpr auto maxindex = 65536;
cachef(maxindex, LUT_CLIP_BELOW);
gammatab(maxindex, 0);
gammatabThumb(maxindex, 0);
igammatab_srgb(maxindex, 0);
igammatab_srgb1(maxindex, 0);
gammatab_srgb(maxindex, 0);
gammatab_srgb1(maxindex, 0);
denoiseGammaTab(maxindex, 0);
denoiseIGammaTab(maxindex, 0);
igammatab_24_17(maxindex, 0);
gammatab_24_17a(maxindex, LUT_CLIP_ABOVE | LUT_CLIP_BELOW);
gammatab_13_2(maxindex, 0);
igammatab_13_2(maxindex, 0);
gammatab_115_2(maxindex, 0);
igammatab_115_2(maxindex, 0);
gammatab_145_3(maxindex, 0);
igammatab_145_3(maxindex, 0);
#ifdef _OPENMP
#pragma omp parallel sections
#endif
{
#ifdef _OPENMP
#pragma omp section
#endif
{
int i = 0;
int epsmaxint = eps_max;
for (; i <= epsmaxint; i++)
{
cachef[i] = 327.68 * ((kappa * i / MAXVALF + 16.0) / 116.0);
}
for(; i < maxindex; i++)
{
cachef[i] = 327.68 * std::cbrt((double)i / MAXVALF);
}
}
#ifdef _OPENMP
#pragma omp section
#endif
{
for (int i = 0; i < maxindex; i++)
{
gammatab_srgb[i] = gammatab_srgb1[i] = gamma2(i / 65535.0);
}
gammatab_srgb *= 65535.f;
gamma2curve.share(gammatab_srgb, LUT_CLIP_BELOW | LUT_CLIP_ABOVE); // shares the buffer with gammatab_srgb but has different clip flags
}
#ifdef _OPENMP
#pragma omp section
#endif
{
for (int i = 0; i < maxindex; i++)
{
igammatab_srgb[i] = igammatab_srgb1[i] = igamma2 (i / 65535.0);
}
igammatab_srgb *= 65535.f;
}
#ifdef _OPENMP
#pragma omp section
#endif
{
double rsRGBGamma = 1.0 / sRGBGamma;
for (int i = 0; i < maxindex; i++)
{
double val = pow (i / 65535.0, rsRGBGamma);
gammatab[i] = 65535.0 * val;
gammatabThumb[i] = (unsigned char)(255.0 * val);
}
}
#ifdef _OPENMP
#pragma omp section
#endif
// modify arbitrary data for Lab..I have test : nothing, gamma 2.6 11 - gamma 4 5 - gamma 5.5 10
// we can put other as gamma g=2.6 slope=11, etc.
// but noting to do with real gamma !!!: it's only for data Lab # data RGB
// finally I opted for gamma55 and with options we can change
switch(settings->denoiselabgamma) {
case 0:
for (int i = 0; i < maxindex; i++) {
denoiseGammaTab[i] = 65535.0 * gamma26_11 (i / 65535.0);
}
break;
case 1:
for (int i = 0; i < maxindex; i++) {
denoiseGammaTab[i] = 65535.0 * gamma4 (i / 65535.0);
}
break;
default:
for (int i = 0; i < maxindex; i++) {
denoiseGammaTab[i] = 65535.0 * gamma55 (i / 65535.0);
}
break;
}
#ifdef _OPENMP
#pragma omp section
#endif
// modify arbitrary data for Lab..I have test : nothing, gamma 2.6 11 - gamma 4 5 - gamma 5.5 10
// we can put other as gamma g=2.6 slope=11, etc.
// but noting to do with real gamma !!!: it's only for data Lab # data RGB
// finally I opted for gamma55 and with options we can change
switch(settings->denoiselabgamma) {
case 0:
for (int i = 0; i < maxindex; i++) {
denoiseIGammaTab[i] = 65535.0 * igamma26_11 (i / 65535.0);
}
break;
case 1:
for (int i = 0; i < maxindex; i++) {
denoiseIGammaTab[i] = 65535.0 * igamma4 (i / 65535.0);
}
break;
default:
for (int i = 0; i < maxindex; i++) {
denoiseIGammaTab[i] = 65535.0 * igamma55 (i / 65535.0);
}
break;
}
#ifdef _OPENMP
#pragma omp section
#endif
for (int i = 0; i < maxindex; i++) {
gammatab_13_2[i] = 65535.0 * gamma13_2 (i / 65535.0);
}
#ifdef _OPENMP
#pragma omp section
#endif
for (int i = 0; i < maxindex; i++) {
igammatab_13_2[i] = 65535.0 * igamma13_2 (i / 65535.0);
}
#ifdef _OPENMP
#pragma omp section
#endif
for (int i = 0; i < maxindex; i++) {
gammatab_115_2[i] = 65535.0 * gamma115_2 (i / 65535.0);
}
#ifdef _OPENMP
#pragma omp section
#endif
for (int i = 0; i < maxindex; i++) {
igammatab_115_2[i] = 65535.0 * igamma115_2 (i / 65535.0);
}
#ifdef _OPENMP
#pragma omp section
#endif
for (int i = 0; i < maxindex; i++) {
gammatab_145_3[i] = 65535.0 * gamma145_3 (i / 65535.0);
}
#ifdef _OPENMP
#pragma omp section
#endif
for (int i = 0; i < maxindex; i++) {
igammatab_145_3[i] = 65535.0 * igamma145_3 (i / 65535.0);
}
#ifdef _OPENMP
#pragma omp section
#endif
for (int i = 0; i < maxindex; i++) {
gammatab_24_17a[i] = gamma24_17(i / 65535.0);
}
#ifdef _OPENMP
#pragma omp section
#endif
for (int i = 0; i < maxindex; i++) {
igammatab_24_17[i] = 65535.0 * igamma24_17 (i / 65535.0);
}
#ifdef _OPENMP
#pragma omp section
#endif
initMunsell();
#ifdef _OPENMP
#pragma omp section
#endif
linearGammaTRC = cmsBuildGamma(nullptr, 1.0);
}
}
void Color::cleanup ()
{
if (linearGammaTRC) {
cmsFreeToneCurve(linearGammaTRC);
}
}
void Color::rgb2lab (Glib::ustring profile, Glib::ustring profileW, int r, int g, int b, float &LAB_l, float &LAB_a, float &LAB_b, bool workingSpace)
{
double xyz_rgb[3][3];
const double ep = 216.0 / 24389.0;
const double ka = 24389.0 / 27.0;
double var_R = r / 65535.0;
double var_G = g / 65535.0;
double var_B = b / 65535.0;
Glib::ustring profileCalc;
profileCalc = "sRGB"; //default
if (workingSpace) {
profileCalc = profileW; //display working
}
else {// if you want display = output space
if (profile == "RT_sRGB" || profile == "RT_sRGB_gBT709" || profile == "RT_sRGB_g10") {
profileCalc = "sRGB";
}
if (profile == "ProPhoto" || profile == "RT_Large_gBT709" || profile == "RT_Large_g10" || profile == "RT_Large_gsRGB") {
profileCalc = "ProPhoto";
}
if (profile == "AdobeRGB1998" || profile == "RT_Medium_gsRGB") {
profileCalc = "Adobe RGB";
}
if (profile == "WideGamutRGB") {
profileCalc = "WideGamut";
}
}
if (workingSpace) {//display working
if (profileW == "sRGB") { //apply sRGB inverse gamma
if ( var_R > 0.04045 ) {
var_R = pow ( ( ( var_R + 0.055 ) / 1.055 ), rtengine::Color::sRGBGammaCurve);
} else {
var_R = var_R / 12.92;
}
if ( var_G > 0.04045 ) {
var_G = pow ( ( ( var_G + 0.055 ) / 1.055 ), rtengine::Color::sRGBGammaCurve);
} else {
var_G = var_G / 12.92;
}
if ( var_B > 0.04045 ) {
var_B = pow ( ( ( var_B + 0.055 ) / 1.055 ), rtengine::Color::sRGBGammaCurve);
} else {
var_B = var_B / 12.92;
}
} else if (profileW == "ProPhoto") { // apply inverse gamma 1.8
var_R = pow ( var_R, 1.8);
var_G = pow ( var_G, 1.8);
var_B = pow ( var_B, 1.8);
} else { // apply inverse gamma 2.2
var_R = pow ( var_R, 2.2);
var_G = pow ( var_G, 2.2);
var_B = pow ( var_B, 2.2);
}
} else { //display outout profile
if (profile == "RT_sRGB" || profile == "RT_Large_gsRGB" || profile == "RT_Medium_gsRGB") { //apply sRGB inverse gamma
if ( var_R > 0.04045 ) {
var_R = pow ( ( ( var_R + 0.055 ) / 1.055 ), rtengine::Color::sRGBGammaCurve);
} else {
var_R = var_R / 12.92;
}
if ( var_G > 0.04045 ) {
var_G = pow ( ( ( var_G + 0.055 ) / 1.055 ), rtengine::Color::sRGBGammaCurve);
} else {
var_G = var_G / 12.92;
}
if ( var_B > 0.04045 ) {
var_B = pow ( ( ( var_B + 0.055 ) / 1.055 ), rtengine::Color::sRGBGammaCurve);
} else {
var_B = var_B / 12.92;
}
}
else if (profile == "RT_sRGB_gBT709" || profile == "RT_Large_gBT709") { //
if ( var_R > 0.0795 ) {
var_R = pow ( ( ( var_R + 0.0954 ) / 1.0954 ), 2.2);
} else {
var_R = var_R / 4.5;
}
if ( var_G > 0.0795 ) {
var_G = pow ( ( ( var_G + 0.0954 ) / 1.0954 ), 2.2);
} else {
var_G = var_G / 4.5;
}
if ( var_B > 0.0795 ) {
var_B = pow ( ( ( var_B + 0.0954 ) / 1.0954 ), 2.2);
} else {
var_B = var_B / 4.5;
}
} else if (profile == "ProPhoto") { // apply inverse gamma 1.8
var_R = pow ( var_R, 1.8);
var_G = pow ( var_G, 1.8);
var_B = pow ( var_B, 1.8);
} else if (profile == "RT_sRGB_g10" || profile == "RT_Large_g10") { // apply inverse gamma 1.8
var_R = pow ( var_R, 1.);
var_G = pow ( var_G, 1.);
var_B = pow ( var_B, 1.);
}
else {// apply inverse gamma 2.2
var_R = pow ( var_R, 2.2);
var_G = pow ( var_G, 2.2);
var_B = pow ( var_B, 2.2);
}
}
// TMatrix wprof = rtengine::ICCStore::getInstance()->workingSpaceMatrix (profileW);
TMatrix wprof = rtengine::ICCStore::getInstance()->workingSpaceMatrix (profileCalc);
for (int m = 0; m < 3; m++)
for (int n = 0; n < 3; n++) {
xyz_rgb[m][n] = wprof[m][n];
}
double varxx, varyy, varzz;
double var_X = ( xyz_rgb[0][0] * var_R + xyz_rgb[0][1] * var_G + xyz_rgb[0][2] * var_B ) / Color::D50x;
double var_Y = ( xyz_rgb[1][0] * var_R + xyz_rgb[1][1] * var_G + xyz_rgb[1][2] * var_B ) ;
double var_Z = ( xyz_rgb[2][0] * var_R + xyz_rgb[2][1] * var_G + xyz_rgb[2][2] * var_B ) / Color::D50z;
varxx = var_X > ep ? cbrt(var_X) : ( ka * var_X + 16.0) / 116.0 ;
varyy = var_Y > ep ? cbrt(var_Y) : ( ka * var_Y + 16.0) / 116.0 ;
varzz = var_Z > ep ? cbrt(var_Z) : ( ka * var_Z + 16.0) / 116.0 ;
LAB_l = ( 116 * varyy ) - 16;
LAB_a = 500 * ( varxx - varyy );
LAB_b = 200 * ( varyy - varzz );
}
void Color::rgb2hsl(float r, float g, float b, float &h, float &s, float &l)
{
double var_R = double(r) / 65535.0;
double var_G = double(g) / 65535.0;
double var_B = double(b) / 65535.0;
double m = min(var_R, var_G, var_B);
double M = max(var_R, var_G, var_B);
double C = M - m;
double l_ = (M + m) / 2.;
l = float(l_);
if (C < 0.00001 && C > -0.00001) { // no fabs, slow!
h = 0.f;
s = 0.f;
} else {
double h_;
if (l_ <= 0.5) {
s = float( (M - m) / (M + m) );
} else {
s = float( (M - m) / (2.0 - M - m) );
}
if ( var_R == M ) {
h_ = (var_G - var_B) / C;
} else if ( var_G == M ) {
h_ = 2. + (var_B - var_R) / C;
} else {
h_ = 4. + (var_R - var_G) / C;
}
h = float(h_ / 6.0);
if ( h < 0.f ) {
h += 1.f;
}
if ( h > 1.f ) {
h -= 1.f;
}
}
}
void Color::rgb2hslfloat(float r, float g, float b, float &h, float &s, float &l)
{
float m = min(r, g, b);
float M = max(r, g, b);
float C = M - m;
l = (M + m) * 7.6295109e-6f; // (0.5f / 65535.f)
if (fabsf(C) < 0.65535f) { // 0.00001f * 65535.f
h = 0.f;
s = 0.f;
} else {
if (l <= 0.5f) {
s = (M - m) / (M + m);
} else {
s = (M - m) / (131070.f - M - m); // 131070.f = 2.f * 65535.f
}
if ( r == M ) {
h = (g - b);
} else if ( g == M ) {
h = (2.f * C) + (b - r);
} else {
h = (4.f * C) + (r - g);
}
h /= (6.f * C);
if ( h < 0.f ) {
h += 1.f;
} else if ( h > 1.f ) {
h -= 1.f;
}
}
}
#ifdef __SSE2__
void Color::rgb2hsl(vfloat r, vfloat g, vfloat b, vfloat &h, vfloat &s, vfloat &l)
{
vfloat maxv = _mm_max_ps(r, _mm_max_ps(g, b));
vfloat minv = _mm_min_ps(r, _mm_min_ps(g, b));
vfloat C = maxv - minv;
vfloat tempv = maxv + minv;
l = (tempv) * F2V(7.6295109e-6f);
s = (maxv - minv);
s /= vself(vmaskf_gt(l, F2V(0.5f)), F2V(131070.f) - tempv, tempv);
h = F2V(4.f) * C + r - g;
h = vself(vmaskf_eq(g, maxv), F2V(2.f) * C + b - r, h);
h = vself(vmaskf_eq(r, maxv), g - b, h);
h /= (F2V(6.f) * C);
vfloat onev = F2V(1.f);
h = vself(vmaskf_lt(h, ZEROV), h + onev, h);
h = vself(vmaskf_gt(h, onev), h - onev, h);
vmask zeromask = vmaskf_lt(vabsf(C), F2V(0.65535f));
h = vself(zeromask, ZEROV, h);
s = vself(zeromask, ZEROV, s);
}
#endif
double Color::hue2rgb(double p, double q, double t)
{
if (t < 0.) {
t += 6.;
} else if( t > 6.) {
t -= 6.;
}
if (t < 1.) {
return p + (q - p) * t;
} else if (t < 3.) {
return q;
} else if (t < 4.) {
return p + (q - p) * (4. - t);
} else {
return p;
}
}
float Color::hue2rgbfloat(float p, float q, float t)
{
if (t < 0.f) {
t += 6.f;
} else if( t > 6.f) {
t -= 6.f;
}
if (t < 1.f) {
return p + (q - p) * t;
} else if (t < 3.f) {
return q;
} else if (t < 4.f) {
return p + (q - p) * (4.f - t);
} else {
return p;
}
}
#ifdef __SSE2__
vfloat Color::hue2rgb(vfloat p, vfloat q, vfloat t)
{
vfloat fourv = F2V(4.f);
vfloat threev = F2V(3.f);
vfloat sixv = threev + threev;
t = vself(vmaskf_lt(t, ZEROV), t + sixv, t);
t = vself(vmaskf_gt(t, sixv), t - sixv, t);
vfloat temp1 = p + (q - p) * t;
vfloat temp2 = p + (q - p) * (fourv - t);
vfloat result = vself(vmaskf_lt(t, fourv), temp2, p);
result = vself(vmaskf_lt(t, threev), q, result);
return vself(vmaskf_lt(t, fourv - threev), temp1, result);
}
#endif
void Color::hsl2rgb (float h, float s, float l, float &r, float &g, float &b)
{
if (s == 0) {
r = g = b = 65535.0f * l; // achromatic
} else {
double m2;
double h_ = double(h);
double s_ = double(s);
double l_ = double(l);
if (l <= 0.5f) {
m2 = l_ * (1.0 + s_);
} else {
m2 = l_ + s_ - l_ * s_;
}
double m1 = 2.0 * l_ - m2;
r = float(65535.0 * hue2rgb (m1, m2, h_ * 6.0 + 2.0));
g = float(65535.0 * hue2rgb (m1, m2, h_ * 6.0));
b = float(65535.0 * hue2rgb (m1, m2, h_ * 6.0 - 2.0));
}
}
void Color::hsl2rgbfloat (float h, float s, float l, float &r, float &g, float &b)
{
if (s == 0.f) {
r = g = b = 65535.f * l; // achromatic
} else {
float m2;
if (l <= 0.5f) {
m2 = l * (1.f + s);
} else {
m2 = l + s - l * s;
}
float m1 = 2.f * l - m2;
r = 65535.f * hue2rgbfloat (m1, m2, h * 6.f + 2.f);
g = 65535.f * hue2rgbfloat (m1, m2, h * 6.f);
b = 65535.f * hue2rgbfloat (m1, m2, h * 6.f - 2.f);
}
}
#ifdef __SSE2__
void Color::hsl2rgb (vfloat h, vfloat s, vfloat l, vfloat &r, vfloat &g, vfloat &b)
{
vfloat m2 = s * l;
m2 = vself(vmaskf_gt(l, F2V(0.5f)), s - m2, m2);
m2 += l;
vfloat twov = F2V(2.f);
vfloat c65535v = F2V(65535.f);
vfloat m1 = l + l - m2;
h *= F2V(6.f);
r = c65535v * hue2rgb (m1, m2, h + twov);
g = c65535v * hue2rgb (m1, m2, h);
b = c65535v * hue2rgb (m1, m2, h - twov);
vmask selectsMask = vmaskf_eq(ZEROV, s);
vfloat lc65535v = c65535v * l;
r = vself(selectsMask, lc65535v, r);
g = vself(selectsMask, lc65535v, g);
b = vself(selectsMask, lc65535v, b);
}
#endif
void Color::hsl2rgb01 (float h, float s, float l, float &r, float &g, float &b)
{
if (s == 0) {
r = g = b = l; // achromatic
} else {
double m2;
double h_ = double(h);
double s_ = double(s);
double l_ = double(l);
if (l <= 0.5f) {
m2 = l_ * (1.0 + s_);
} else {
m2 = l_ + s_ - l_ * s_;
}
double m1 = 2.0 * l_ - m2;
r = float(hue2rgb (m1, m2, h_ * 6.0 + 2.0));
g = float(hue2rgb (m1, m2, h_ * 6.0));
b = float(hue2rgb (m1, m2, h_ * 6.0 - 2.0));
}
}
void Color::rgb2hsv(float r, float g, float b, float &h, float &s, float &v)
{
double var_R = r / 65535.0;
double var_G = g / 65535.0;
double var_B = b / 65535.0;
double var_Min = min(var_R, var_G, var_B);
double var_Max = max(var_R, var_G, var_B);
double del_Max = var_Max - var_Min;
v = var_Max;
if (del_Max < 0.00001 && del_Max > -0.00001) { // no fabs, slow!
h = 0;
s = 0;
} else {
s = del_Max / var_Max;
if ( var_R == var_Max ) {
h = (var_G - var_B) / del_Max;
} else if ( var_G == var_Max ) {
h = 2.0 + (var_B - var_R) / del_Max;
} else if ( var_B == var_Max ) {
h = 4.0 + (var_R - var_G) / del_Max;
}
h /= 6.0;
if ( h < 0 ) {
h += 1;
}
if ( h > 1 ) {
h -= 1;
}
}
}
void Color::hsv2rgb (float h, float s, float v, float &r, float &g, float &b)
{
float h1 = h * 6.f; // sector 0 to 5
int i = (int)h1; // floor() is very slow, and h1 is always >0
float f = h1 - i; // fractional part of h
float p = v * ( 1.f - s );
float q = v * ( 1.f - s * f );
float t = v * ( 1.f - s * ( 1.f - f ) );
float r1, g1, b1;
if (i == 1) {
r1 = q;
g1 = v;
b1 = p;
} else if (i == 2) {
r1 = p;
g1 = v;
b1 = t;
} else if (i == 3) {
r1 = p;
g1 = q;
b1 = v;
} else if (i == 4) {
r1 = t;
g1 = p;
b1 = v;
} else if (i == 5) {
r1 = v;
g1 = p;
b1 = q;
} else { /*i==(0|6)*/
r1 = v;
g1 = t;
b1 = p;
}
r = ((r1) * 65535.0f);
g = ((g1) * 65535.0f);
b = ((b1) * 65535.0f);
}
// Function copied for speed concerns
// Not exactly the same as above ; this one return a result in the [0.0 ; 1.0] range
void Color::hsv2rgb01 (float h, float s, float v, float &r, float &g, float &b)
{
float h1 = h * 6; // sector 0 to 5
int i = int(h1);
float f = h1 - i; // fractional part of h
float p = v * ( 1 - s );
float q = v * ( 1 - s * f );
float t = v * ( 1 - s * ( 1 - f ) );
if (i == 1) {
r = q;
g = v;
b = p;
} else if (i == 2) {
r = p;
g = v;
b = t;
} else if (i == 3) {
r = p;
g = q;
b = v;
} else if (i == 4) {
r = t;
g = p;
b = v;
} else if (i == 5) {
r = v;
g = p;
b = q;
} else { /*(i==0|6)*/
r = v;
g = t;
b = p;
}
}
void Color::hsv2rgb (float h, float s, float v, int &r, int &g, int &b)
{
float h1 = h * 6; // sector 0 to 5
int i = floor( h1 );
float f = h1 - i; // fractional part of h
float p = v * ( 1 - s );
float q = v * ( 1 - s * f );
float t = v * ( 1 - s * ( 1 - f ) );
float r1, g1, b1;
if (i == 0) {
r1 = v;
g1 = t;
b1 = p;
} else if (i == 1) {
r1 = q;
g1 = v;
b1 = p;
} else if (i == 2) {
r1 = p;
g1 = v;
b1 = t;
} else if (i == 3) {
r1 = p;
g1 = q;
b1 = v;
} else if (i == 4) {
r1 = t;
g1 = p;
b1 = v;
} else /*if (i == 5)*/ {
r1 = v;
g1 = p;
b1 = q;
}
r = (int)( r1 * 65535);
g = (int)( g1 * 65535);
b = (int)( b1 * 65535);
}
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
void Color::xyz2srgb (float x, float y, float z, float &r, float &g, float &b)
{
//Transform to output color. Standard sRGB is D65, but internal representation is D50
//Note that it is only at this point that we should have need of clipping color data
/*float x65 = d65_d50[0][0]*x + d65_d50[0][1]*y + d65_d50[0][2]*z ;
float y65 = d65_d50[1][0]*x + d65_d50[1][1]*y + d65_d50[1][2]*z ;
float z65 = d65_d50[2][0]*x + d65_d50[2][1]*y + d65_d50[2][2]*z ;
r = sRGB_xyz[0][0]*x65 + sRGB_xyz[0][1]*y65 + sRGB_xyz[0][2]*z65;
g = sRGB_xyz[1][0]*x65 + sRGB_xyz[1][1]*y65 + sRGB_xyz[1][2]*z65;
b = sRGB_xyz[2][0]*x65 + sRGB_xyz[2][1]*y65 + sRGB_xyz[2][2]*z65;*/
/*r = sRGBd65_xyz[0][0]*x + sRGBd65_xyz[0][1]*y + sRGBd65_xyz[0][2]*z ;
g = sRGBd65_xyz[1][0]*x + sRGBd65_xyz[1][1]*y + sRGBd65_xyz[1][2]*z ;
b = sRGBd65_xyz[2][0]*x + sRGBd65_xyz[2][1]*y + sRGBd65_xyz[2][2]*z ;*/
r = ((sRGB_xyz[0][0] * x + sRGB_xyz[0][1] * y + sRGB_xyz[0][2] * z)) ;
g = ((sRGB_xyz[1][0] * x + sRGB_xyz[1][1] * y + sRGB_xyz[1][2] * z)) ;
b = ((sRGB_xyz[2][0] * x + sRGB_xyz[2][1] * y + sRGB_xyz[2][2] * z)) ;
}
void Color::xyz2Prophoto (float x, float y, float z, float &r, float &g, float &b)
{
r = ((prophoto_xyz[0][0] * x + prophoto_xyz[0][1] * y + prophoto_xyz[0][2] * z)) ;
g = ((prophoto_xyz[1][0] * x + prophoto_xyz[1][1] * y + prophoto_xyz[1][2] * z)) ;
b = ((prophoto_xyz[2][0] * x + prophoto_xyz[2][1] * y + prophoto_xyz[2][2] * z)) ;
}
void Color::Prophotoxyz (float r, float g, float b, float &x, float &y, float &z)
{
x = ((xyz_prophoto[0][0] * r + xyz_prophoto[0][1] * g + xyz_prophoto[0][2] * b)) ;
y = ((xyz_prophoto[1][0] * r + xyz_prophoto[1][1] * g + xyz_prophoto[1][2] * b)) ;
z = ((xyz_prophoto[2][0] * r + xyz_prophoto[2][1] * g + xyz_prophoto[2][2] * b)) ;
}
void Color::rgbxyz (float r, float g, float b, float &x, float &y, float &z, const double xyz_rgb[3][3])
{
x = ((xyz_rgb[0][0] * r + xyz_rgb[0][1] * g + xyz_rgb[0][2] * b)) ;
y = ((xyz_rgb[1][0] * r + xyz_rgb[1][1] * g + xyz_rgb[1][2] * b)) ;
z = ((xyz_rgb[2][0] * r + xyz_rgb[2][1] * g + xyz_rgb[2][2] * b)) ;
}
void Color::rgbxyz (float r, float g, float b, float &x, float &y, float &z, const float xyz_rgb[3][3])
{
x = ((xyz_rgb[0][0] * r + xyz_rgb[0][1] * g + xyz_rgb[0][2] * b)) ;
y = ((xyz_rgb[1][0] * r + xyz_rgb[1][1] * g + xyz_rgb[1][2] * b)) ;
z = ((xyz_rgb[2][0] * r + xyz_rgb[2][1] * g + xyz_rgb[2][2] * b)) ;
}
#ifdef __SSE2__
void Color::rgbxyz (vfloat r, vfloat g, vfloat b, vfloat &x, vfloat &y, vfloat &z, const vfloat xyz_rgb[3][3])
{
x = ((xyz_rgb[0][0] * r + xyz_rgb[0][1] * g + xyz_rgb[0][2] * b)) ;
y = ((xyz_rgb[1][0] * r + xyz_rgb[1][1] * g + xyz_rgb[1][2] * b)) ;
z = ((xyz_rgb[2][0] * r + xyz_rgb[2][1] * g + xyz_rgb[2][2] * b)) ;
}
#endif
void Color::xyz2rgb (float x, float y, float z, float &r, float &g, float &b, const double rgb_xyz[3][3])
{