-
Notifications
You must be signed in to change notification settings - Fork 329
/
Copy pathimprocfun.cc
7159 lines (6010 loc) · 296 KB
/
improcfun.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 <cmath>
#include <glib.h>
#include <glibmm.h>
#include "rtengine.h"
#include "improcfun.h"
#include "curves.h"
#include "colorclip.h"
#include "gauss.h"
#include "bilateral2.h"
#include "mytime.h"
#include "iccstore.h"
#include "impulse_denoise.h"
#include "imagesource.h"
#include "rtthumbnail.h"
#include "utils.h"
#include "iccmatrices.h"
#include "color.h"
#include "calc_distort.h"
#include "rt_math.h"
#include "EdgePreservingDecomposition.h"
#include "improccoordinator.h"
#include "clutstore.h"
#include "ciecam02.h"
#ifdef _OPENMP
#include <omp.h>
#endif
#undef CLIPD
#define CLIPD(a) ((a)>0.0f?((a)<1.0f?(a):1.0f):0.0f)
namespace rtengine
{
using namespace procparams;
#undef ABS
#undef CLIPS
#undef CLIPC
#define ABS(a) ((a)<0?-(a):(a))
#define CLIPS(a) ((a)>-32768?((a)<32767?(a):32767):-32768)
#define CLIPC(a) ((a)>-32000?((a)<32000?(a):32000):-32000)
#define CLIP2(a) ((a)<MAXVAL ? a : MAXVAL )
#define FCLIP(a) ((a)>0.0?((a)<65535.5?(a):65535.5):0.0)
extern const Settings* settings;
LUTf ImProcFunctions::cachef;
LUTf ImProcFunctions::gamma2curve;
void ImProcFunctions::initCache ()
{
const int maxindex = 65536;
cachef(maxindex, 0/*LUT_CLIP_BELOW*/);
gamma2curve(maxindex, 0);
for (int i = 0; i < maxindex; i++) {
if (i > Color::eps_max) {
cachef[i] = 327.68 * ( exp(1.0 / 3.0 * log((double)i / MAXVALD) ));
} else {
cachef[i] = 327.68 * ((Color::kappa * i / MAXVALD + 16.0) / 116.0);
}
}
for (int i = 0; i < maxindex; i++) {
gamma2curve[i] = (CurveFactory::gamma2(i / 65535.0) * 65535.0);
}
}
void ImProcFunctions::cleanupCache ()
{
}
ImProcFunctions::~ImProcFunctions ()
{
if (monitorTransform != NULL) {
cmsDeleteTransform (monitorTransform);
}
if (output2monitorTransform != NULL) {
cmsDeleteTransform (output2monitorTransform);
}
if (lab2outputTransform != NULL) {
cmsDeleteTransform (lab2outputTransform);
}
}
void ImProcFunctions::setScale (double iscale)
{
scale = iscale;
}
// Called from several threads
void ImProcFunctions::firstAnalysisThread (Imagefloat* original, Glib::ustring wprofile, unsigned int* histogram, int row_from, int row_to)
{
TMatrix wprof = iccStore->workingSpaceMatrix (wprofile);
lumimul[0] = wprof[1][0];
lumimul[1] = wprof[1][1];
lumimul[2] = wprof[1][2];
int W = original->width;
for (int i = row_from; i < row_to; i++) {
for (int j = 0; j < W; j++) {
int r = original->r(i, j);
int g = original->g(i, j);
int b = original->b(i, j);
int y = CLIP((int)(lumimul[0] * r + lumimul[1] * g + lumimul[2] * b)) ;
if (histogram) {
histogram[y]++;
}
}
}
}
/*
void ImProcFunctions::CAT02 (Imagefloat* baseImg, const ProcParams* params)
{
const double toxyz[3][3] = {{0.7976749, 0.1351917, 0.0313534},
{0.2880402, 0.7118741, 0.0000857},
{0.0000000, 0.0000000, 0.8252100}};
const double xyzto[3][3] = {{1.3459433, -0.2556075, -0.0511118},
{-0.5445989, 1.5081673, 0.0205351},
{0.0000000, 0.0000000, 1.2118128}};
int fw = baseImg->width;
int fh = baseImg->height;
double CAM02BB00,CAM02BB01,CAM02BB02,CAM02BB10,CAM02BB11,CAM02BB12,CAM02BB20,CAM02BB21,CAM02BB22;
double Xxx,Yyy,Zzz;
// Xxx=1.09844;
// Yyy=1.0;
// Zzz=0.355961;
//params.wb.temperature, params.wb.green, params.wb.method
double Xxyz, Zxyz;
// ColorTemp::temp2mulxyz (params->wb.temperature, params->wb.green, params->wb.method, Xxyz, Zxyz);
ColorTemp::temp2mulxyz (5000.0, 1.0, "Camera", Xxyz, Zxyz);
ColorTemp::cieCAT02(Xxx, Yyy, Zzz, CAM02BB00,CAM02BB01,CAM02BB02,CAM02BB10,CAM02BB11,CAM02BB12,CAM02BB20,CAM02BB21,CAM02BB22);
printf("00=%f 01=%f 11=%f 20=%f 22=%f\n", CAM02BB00,CAM02BB01,CAM02BB11,CAM02BB20,CAM02BB22);
for (int i=0; i<fh; i++) {
for (int j=0; j<fw; j++) {
float r = baseImg->r(i,j);
float g = baseImg->g(i,j);
float b = baseImg->b(i,j);
float x = toxyz[0][0] * r + toxyz[0][1] * g + toxyz[0][2] * b;
float y = toxyz[1][0] * r + toxyz[1][1] * g + toxyz[1][2] * b;
float z = toxyz[2][0] * r + toxyz[2][1] * g + toxyz[2][2] * b;
float Xcam=CAM02BB00* x +CAM02BB01* y + CAM02BB02* z ;
float Ycam=CAM02BB10* x +CAM02BB11* y + CAM02BB12* z ;
float Zcam=CAM02BB20* x +CAM02BB21* y + CAM02BB22* z ;
baseImg->r(i,j) = xyzto[0][0] * Xcam + xyzto[0][1] * Ycam + xyzto[0][2] * Zcam;
baseImg->g(i,j) = xyzto[1][0] * Xcam + xyzto[1][1] * Ycam + xyzto[1][2] * Zcam;
baseImg->b(i,j) = xyzto[2][0] * Xcam + xyzto[2][1] * Ycam + xyzto[2][2] * Zcam;
}
}
}
*/
void ImProcFunctions::firstAnalysis (Imagefloat* original, const ProcParams* params, LUTu & histogram)
{
// set up monitor transform
Glib::ustring wprofile = params->icm.working;
if (monitorTransform != NULL) {
cmsDeleteTransform (monitorTransform);
}
if (output2monitorTransform != NULL) {
cmsDeleteTransform (output2monitorTransform);
}
if (lab2outputTransform != NULL) {
cmsDeleteTransform (lab2outputTransform);
}
monitorTransform = NULL;
output2monitorTransform = NULL;
lab2outputTransform = NULL;
#if !defined(__APPLE__) // No support for monitor profiles on OS X, all data is sRGB
Glib::ustring monitorProfile = settings->monitorProfile;
#if defined(WIN32)
if (settings->autoMonitorProfile) {
monitorProfile = iccStore->defaultMonitorProfile;
}
#endif
cmsHPROFILE monitor = iccStore->getProfile ("file:" + monitorProfile);
if (monitor) {
lcmsMutex->lock ();
cmsHPROFILE iprof = cmsCreateLab4Profile(NULL);
monitorTransform = cmsCreateTransform (iprof, TYPE_Lab_FLT, monitor, TYPE_RGB_8, INTENT_RELATIVE_COLORIMETRIC,
cmsFLAGS_NOOPTIMIZE | cmsFLAGS_NOCACHE ); // NOCACHE is for thread safety, NOOPTIMIZE for precision
Glib::ustring outputProfile;
if (params->icm.output != "" && params->icm.output != ColorManagementParams::NoICMString) {
outputProfile = params->icm.output;
cmsHPROFILE jprof = iccStore->getProfile(outputProfile);
if (jprof) {
lab2outputTransform = cmsCreateTransform (iprof, TYPE_Lab_FLT, jprof, TYPE_RGB_FLT, INTENT_RELATIVE_COLORIMETRIC, cmsFLAGS_NOOPTIMIZE | cmsFLAGS_NOCACHE );
if (monitor) {
output2monitorTransform = cmsCreateTransform (jprof, TYPE_RGB_FLT, monitor, TYPE_RGB_8, INTENT_RELATIVE_COLORIMETRIC, cmsFLAGS_NOOPTIMIZE | cmsFLAGS_NOCACHE );
}
}
}
cmsCloseProfile(iprof);
lcmsMutex->unlock ();
}
#endif
// calculate histogram of the y channel needed for contrast curve calculation in exposure adjustments
int T = 1;
#ifdef _OPENMP
if(multiThread) {
T = omp_get_max_threads();
}
#endif
unsigned int** hist = new unsigned int* [T];
for (int i = 0; i < T; i++) {
hist[i] = new unsigned int[65536];
memset (hist[i], 0, 65536 * sizeof(int));
}
#ifdef _OPENMP
#pragma omp parallel if (multiThread)
{
int H = original->height;
int tid = omp_get_thread_num();
int nthreads = omp_get_num_threads();
int blk = H / nthreads;
if (tid < nthreads - 1) {
firstAnalysisThread (original, wprofile, hist[tid], tid * blk, (tid + 1)*blk);
} else {
firstAnalysisThread (original, wprofile, hist[tid], tid * blk, H);
}
}
#else
firstAnalysisThread (original, wprofile, hist[0], 0, original->height);
#endif
histogram.clear();
for (int j = 0; j < T; j++)
for (int i = 0; i < 65536; i++) {
histogram[i] += hist[j][i];
}
for (int i = 0; i < T; i++) {
delete [] hist[i];
}
delete [] hist;
}
// Copyright (c) 2012 Jacques Desmis <jdesmis@gmail.com>
void ImProcFunctions::ciecam_02 (CieImage* ncie, double adap, int begh, int endh, int pW, int pwb, LabImage* lab, const ProcParams* params ,
const ColorAppearance & customColCurve1, const ColorAppearance & customColCurve2, const ColorAppearance & customColCurve3,
LUTu & histLCAM, LUTu & histCCAM, LUTf & CAMBrightCurveJ, LUTf & CAMBrightCurveQ, float &mean, int Iterates, int scale, bool execsharp, double &d, int scalecd, int rtt)
{
if(params->colorappearance.enabled) {
//int lastskip;
//if(rtt==1) {lastskip=scalecd;} //not for Rtthumbnail
#ifdef _DEBUG
MyTime t1e, t2e;
t1e.set();
#endif
LUTf dLcurve;
LUTu hist16JCAM;
bool jp = false;
float val;
//preparate for histograms CIECAM
if(pW != 1) { //only with improccoordinator
dLcurve(65536, 0);
dLcurve.clear();
hist16JCAM(65536, 0);
hist16JCAM.clear();
for (int i = 0; i < 32768; i++) { //# 32768*1.414 approximation maxi for chroma
val = (double)i / 32767.0;
dLcurve[i] = CLIPD(val);
}
}
LUTf dCcurve;
LUTu hist16_CCAM;
bool chropC = false;
float valc;
if(pW != 1) { //only with improccoordinator
dCcurve(65536, 0);
hist16_CCAM(65536);
hist16_CCAM.clear();
for (int i = 0; i < 48000; i++) { //# 32768*1.414 approximation maxi for chroma
valc = (double)i / 47999.0;
dCcurve[i] = CLIPD(valc);
}
}
//end preparate histogram
int width = lab->W, height = lab->H;
float minQ = 10000.f;
float minM = 10000.f;
float maxQ = -1000.f;
float maxM = -1000.f;
float w_h;
float a_w;
float c_;
float f_l;
double Yw;
Yw = 1.0;
double Xw, Zw;
double f, c, nc, yb, la, xw, yw, zw, f2, c2, nc2, yb2, la2;
double fl, n, nbb, ncb, aw;
double xwd, ywd, zwd;
int alg = 0;
bool algepd = false;
float sum = 0.f;
bool ciedata = params->colorappearance.datacie;
ColorTemp::temp2mulxyz (params->wb.temperature, params->wb.green, params->wb.method, Xw, Zw); //compute white Xw Yw Zw : white current WB
//viewing condition for surround
if(params->colorappearance.surround == "Average") {
f = 1.00;
c = 0.69;
nc = 1.00;
f2 = 1.0, c2 = 0.69, nc2 = 1.0;
} else if(params->colorappearance.surround == "Dim") {
f2 = 0.9;
c2 = 0.59;
nc2 = 0.9;
f = 1.0, c = 0.69, nc = 1.0;
} else if(params->colorappearance.surround == "Dark") {
f2 = 0.8;
c2 = 0.525;
nc2 = 0.8;
f = 1.0, c = 0.69, nc = 1.0;
} else if(params->colorappearance.surround == "ExtremelyDark") {
f2 = 0.8;
c2 = 0.41;
nc2 = 0.8;
f = 1.0, c = 0.69, nc = 1.0;
}
//scene condition for surround
if(params->colorappearance.surrsource == true) {
f = 0.85; // if user => source image has surround very dark
c = 0.55;
nc = 0.85;
}
//with which algorithme
if (params->colorappearance.algo == "JC") {
alg = 0;
} else if(params->colorappearance.algo == "JS") {
alg = 1;
} else if(params->colorappearance.algo == "QM") {
alg = 2;
algepd = true;
} else if(params->colorappearance.algo == "ALL") {
alg = 3;
algepd = true;
}
bool needJ = (alg == 0 || alg == 1 || alg == 3);
bool needQ = (alg == 2 || alg == 3);
//settings white point of output device - or illuminant viewing
if(settings->viewingdevice == 0) {
xwd = 96.42; //5000K
ywd = 100.0;
zwd = 82.52;
} else if(settings->viewingdevice == 1) {
xwd = 95.68; //5500
ywd = 100.0;
zwd = 92.15;
} else if(settings->viewingdevice == 2) {
xwd = 95.24; //6000
ywd = 100.0;
zwd = 100.81;
} else if(settings->viewingdevice == 3) {
xwd = 95.04; //6500
ywd = 100.0;
zwd = 108.88;
} else if(settings->viewingdevice == 4) {
xwd = 109.85; //tungsten
ywd = 100.0;
zwd = 35.58;
} else if(settings->viewingdevice == 5) {
xwd = 99.18; //fluo F2
ywd = 100.0;
zwd = 67.39;
} else if(settings->viewingdevice == 6) {
xwd = 95.04; //fluo F7
ywd = 100.0;
zwd = 108.75;
} else if(settings->viewingdevice == 7) {
xwd = 100.96; //fluo F11
ywd = 100.0;
zwd = 64.35;
}
//settings mean Luminance Y of output device or viewing
if(settings->viewingdevicegrey == 0) {
yb2 = 5.0;
} else if(settings->viewingdevicegrey == 1) {
yb2 = 10.0;
} else if(settings->viewingdevicegrey == 2) {
yb2 = 15.0;
} else if(settings->viewingdevicegrey == 3) {
yb2 = 18.0;
} else if(settings->viewingdevicegrey == 4) {
yb2 = 23.0;
} else if(settings->viewingdevicegrey == 5) {
yb2 = 30.0;
} else if(settings->viewingdevicegrey == 6) {
yb2 = 40.0;
}
//La and la2 = ambiant luminosity scene and viewing
la = double(params->colorappearance.adapscen);
if(pwb == 2) {
if(params->colorappearance.autoadapscen) {
la = adap;
}
}
la2 = double(params->colorappearance.adaplum);
// level of adaptation
double deg = (params->colorappearance.degree) / 100.0;
double pilot = params->colorappearance.autodegree ? 2.0 : deg;
//algoritm's params
float jli = params->colorappearance.jlight;
float chr = params->colorappearance.chroma;
float contra = params->colorappearance.contrast;
float qbri = params->colorappearance.qbright;
float schr = params->colorappearance.schroma;
float mchr = params->colorappearance.mchroma;
float qcontra = params->colorappearance.qcontrast;
float hue = params->colorappearance.colorh;
double rstprotection = 100. - params->colorappearance.rstprotection;
if(schr > 0.0) {
schr = schr / 2.0f; //divide sensibility for saturation
}
// extracting datas from 'params' to avoid cache flush (to be confirmed)
ColorAppearanceParams::eTCModeId curveMode = params->colorappearance.curveMode;
ColorAppearanceParams::eTCModeId curveMode2 = params->colorappearance.curveMode2;
bool hasColCurve1 = bool(customColCurve1);
bool hasColCurve2 = bool(customColCurve2);
ColorAppearanceParams::eCTCModeId curveMode3 = params->colorappearance.curveMode3;
bool hasColCurve3 = bool(customColCurve3);
if(CAMBrightCurveJ.dirty || CAMBrightCurveQ.dirty) {
LUTu hist16J;
LUTu hist16Q;
if (needJ) {
hist16J (65536);
hist16J.clear();
}
if (needQ) {
hist16Q (65536);
hist16Q.clear();
}
float koef = 1.0f; //rough correspondence between L and J
for (int i = 0; i < height; i++)
// for (int i=begh; i<endh; i++)
for (int j = 0; j < width; j++) { //rough correspondence between L and J
float currL = lab->L[i][j] / 327.68f;
if (currL > 95.) {
koef = 1.f;
} else if(currL > 85.) {
koef = 0.97f;
} else if(currL > 80.) {
koef = 0.93f;
} else if(currL > 70.) {
koef = 0.87f;
} else if(currL > 60.) {
koef = 0.85f;
} else if(currL > 50.) {
koef = 0.8f;
} else if(currL > 40.) {
koef = 0.75f;
} else if(currL > 30.) {
koef = 0.7f;
} else if(currL > 20.) {
koef = 0.7f;
} else if(currL > 10.) {
koef = 0.9f;
} else if(currL > 0.) {
koef = 1.0f;
}
if (needJ) {
hist16J[CLIP((int)((koef * lab->L[i][j])))]++; //evaluate histogram luminance L # J
}
if (needQ) {
hist16Q[CLIP((int) (32768.f * sqrt((koef * (lab->L[i][j])) / 32768.f)))]++; //for brightness Q : approximation for Q=wh*sqrt(J/100) J not equal L
}
sum += koef * lab->L[i][j]; //evaluate mean J to calcualte Yb
}
//mean=(sum/((endh-begh)*width))/327.68f;//for Yb for all image...if one day "pipette" we can adapt Yb for each zone
mean = (sum / ((height) * width)) / 327.68f; //for Yb for all image...if one day "pipette" we can adapt Yb for each zone
//evaluate lightness, contrast
if (needJ) {
if (!CAMBrightCurveJ) {
CAMBrightCurveJ(65536, 0);
CAMBrightCurveJ.dirty = false;
}
Ciecam02::curveJ (jli, contra, 1, CAMBrightCurveJ, hist16J);//lightness and contrast J
}
if (needQ) {
if (!CAMBrightCurveQ) {
CAMBrightCurveQ(65536, 0);
CAMBrightCurveQ.dirty = false;
}
Ciecam02::curveJ (qbri, qcontra, 1, CAMBrightCurveQ, hist16Q);//brightness and contrast Q
}
}
if(settings->viewinggreySc == 0) { //auto
if (mean < 15.f) {
yb = 3.0;
} else if(mean < 30.f) {
yb = 5.0;
} else if(mean < 40.f) {
yb = 10.0;
} else if(mean < 45.f) {
yb = 15.0;
} else if(mean < 50.f) {
yb = 18.0;
} else if(mean < 55.f) {
yb = 23.0;
} else if(mean < 60.f) {
yb = 30.0;
} else if(mean < 70.f) {
yb = 40.0;
} else if(mean < 80.f) {
yb = 60.0;
} else if(mean < 90.f) {
yb = 80.0;
} else {
yb = 90.0;
}
}
if(settings->viewinggreySc == 1) {
yb = 18.0;
}
int gamu = 0;
bool highlight = params->toneCurve.hrenabled; //Get the value if "highlight reconstruction" is activated
if(params->colorappearance.gamut == true) {
gamu = 1; //enabled gamut control
}
xw = 100.0 * Xw;
yw = 100.0 * Yw;
zw = 100.0 * Zw;
double xw1, yw1, zw1, xw2, yw2, zw2;
// settings of WB: scene and viewing
if(params->colorappearance.wbmodel == "RawT") {
xw1 = 96.46; //use RT WB; CAT 02 is used for output device (see prefreneces)
yw1 = 100.0;
zw1 = 82.445;
xw2 = xwd;
yw2 = ywd;
zw2 = zwd;
} else if(params->colorappearance.wbmodel == "RawTCAT02") {
xw1 = xw; // Settings RT WB are used for CAT02 => mix , CAT02 is use for output device (screen: D50 D65, projector: lamp, LED) see preferences
yw1 = yw;
zw1 = zw;
xw2 = xwd;
yw2 = ywd;
zw2 = zwd;
}
double cz, wh, pfl;
Ciecam02::initcam1(gamu, yb, pilot, f, la, xw, yw, zw, n, d, nbb, ncb, cz, aw, wh, pfl, fl, c);
double nj, dj, nbbj, ncbj, czj, awj, flj;
Ciecam02::initcam2(gamu, yb2, f2, la2, xw2, yw2, zw2, nj, dj, nbbj, ncbj, czj, awj, flj);
#ifndef _DEBUG
#pragma omp parallel default(shared) firstprivate(lab,xw1,xw2,yw1,yw2,zw1,zw2,pilot,jli,chr,yb,la,yb2,la2,fl,nc,f,c, height,width,begh, endh,nc2,f2,c2, alg,algepd, gamu, highlight, rstprotection, pW, scalecd)
#endif
{
//matrix for current working space
TMatrix wiprof = iccStore->workingSpaceInverseMatrix (params->icm.working);
double wip[3][3] = {
{wiprof[0][0], wiprof[0][1], wiprof[0][2]},
{wiprof[1][0], wiprof[1][1], wiprof[1][2]},
{wiprof[2][0], wiprof[2][1], wiprof[2][2]}
};
#ifndef _DEBUG
#pragma omp for schedule(dynamic, 10)
#endif
for (int i = 0; i < height; i++)
// for (int i=begh; i<endh; i++)
for (int j = 0; j < width; j++) {
float L = lab->L[i][j];
float a = lab->a[i][j];
float b = lab->b[i][j];
float x1, y1, z1;
double x, y, z;
double epsil = 0.0001;
//convert Lab => XYZ
Color::Lab2XYZ(L, a, b, x1, y1, z1);
// double J, C, h, Q, M, s, aw, fl, wh;
double J, C, h, Q, M, s;
double Jpro, Cpro, hpro, Qpro, Mpro, spro;
bool t1L = false;
bool t1B = false;
bool t2B = false;
int c1s = 0;
int c1co = 0;
//double n,nbb,ncb,pfl,cz,d;
x = (double)x1 / 655.35;
y = (double)y1 / 655.35;
z = (double)z1 / 655.35;
//process source==> normal
Ciecam02::xyz2jchqms_ciecam02( J, C, h,
Q, M, s, aw, fl, wh,
x, y, z,
xw1, yw1, zw1,
yb, la,
f, c, nc, pilot, gamu , n, nbb, ncb, pfl, cz, d );
Jpro = J;
Cpro = C;
hpro = h;
Qpro = Q;
Mpro = M;
spro = s;
w_h = wh + epsil;
a_w = aw;
c_ = c;
f_l = fl;
// we cannot have all algoritms with all chroma curves
if(alg == 1) {
// Lightness saturation
if(Jpro > 99.9f) {
Jpro = 99.9f;
}
Jpro = (CAMBrightCurveJ[(float)(Jpro * 327.68)]) / 327.68; //ligthness CIECAM02 + contrast
double sres;
double Sp = spro / 100.0;
double parsat = 1.5;
parsat = 1.5; //parsat=1.5 =>saturation ; 1.8 => chroma ; 2.5 => colorfullness (personal evaluation)
if(schr == -100.0) {
schr = -99.8;
}
Ciecam02::curvecolor(schr, Sp , sres, parsat);
double coe = pow(fl, 0.25);
float dred = 100.f; // in C mode
float protect_red = 80.0f; // in C mode
dred = 100.0 * sqrt((dred * coe) / Qpro);
protect_red = 100.0 * sqrt((protect_red * coe) / Qpro);
int sk = 0;
float ko = 100.f;
Color::skinred(Jpro, hpro, sres, Sp, dred, protect_red, sk, rstprotection, ko, spro);
Qpro = ( 4.0 / c ) * sqrt( Jpro / 100.0 ) * ( aw + 4.0 ) ;
Cpro = (spro * spro * Qpro) / (10000.0);
} else if(alg == 3 || alg == 0 || alg == 2) {
double coef = 32760. / wh;
if(alg == 3 || alg == 2) {
if(Qpro * coef > 32767.0f) {
Qpro = (CAMBrightCurveQ[(float)32767.0f]) / coef; //brightness and contrast
} else {
Qpro = (CAMBrightCurveQ[(float)(Qpro * coef)]) / coef; //brightness and contrast
}
}
double Mp, sres;
double coe = pow(fl, 0.25);
Mp = Mpro / 100.0;
double parsat = 2.5;
if(mchr == -100.0) {
mchr = -99.8 ;
}
if(mchr == 100.0) {
mchr = 99.9;
}
if(alg == 3 || alg == 2) {
Ciecam02::curvecolor(mchr, Mp , sres, parsat);
} else {
Ciecam02::curvecolor(0.0, Mp , sres, parsat); //colorfullness
}
float dred = 100.f; //in C mode
float protect_red = 80.0f; // in C mode
dred *= coe; //in M mode
protect_red *= coe; //M mode
int sk = 0;
float ko = 100.f;
Color::skinred(Jpro, hpro, sres, Mp, dred, protect_red, sk, rstprotection, ko, Mpro);
Jpro = (100.0 * Qpro * Qpro) / (wh * wh);
Cpro = Mpro / coe;
spro = 100.0 * sqrt( Mpro / Qpro );
if(alg != 2) {
if(Jpro > 99.9f) {
Jpro = 99.9f;
}
Jpro = (CAMBrightCurveJ[(float)(Jpro * 327.68f)]) / 327.68f; //ligthness CIECAM02 + contrast
}
double Cp;
double Sp = spro / 100.0;
parsat = 1.5;
if(schr == -100.0) {
schr = -99.;
}
if(schr == 100.0) {
schr = 98.;
}
if(alg == 3) {
Ciecam02::curvecolor(schr, Sp , sres, parsat);
} else {
Ciecam02::curvecolor(0.0, Sp , sres, parsat); //saturation
}
dred = 100.f; // in C mode
protect_red = 80.0f; // in C mode
dred = 100.0 * sqrt((dred * coe) / Q);
protect_red = 100.0 * sqrt((protect_red * coe) / Q);
sk = 0;
Color::skinred(Jpro, hpro, sres, Sp, dred, protect_red, sk, rstprotection, ko, spro);
//double Q1;
Qpro = ( 4.0 / c ) * sqrt( Jpro / 100.0 ) * ( aw + 4.0 ) ;
Cpro = (spro * spro * Qpro) / (10000.0);
Cp = Cpro / 100.0;
parsat = 1.8; //parsat=1.5 =>saturation ; 1.8 => chroma ; 2.5 => colorfullness (personal evaluation : for not)
if(chr == -100.0) {
chr = -99.8;
}
if(alg != 2) {
Ciecam02::curvecolor(chr, Cp , sres, parsat);
} else {
Ciecam02::curvecolor(0.0, Cp , sres, parsat); //chroma
}
dred = 55.f;
protect_red = 30.0f;
sk = 1;
Color::skinred(Jpro, hpro, sres, Cp, dred, protect_red, sk, rstprotection, ko, Cpro);
if(Jpro < 1. && Cpro > 12.) {
Cpro = 12.; //reduce artifacts by "pseudo gamut control CIECAM"
} else if(Jpro < 2. && Cpro > 15.) {
Cpro = 15.;
} else if(Jpro < 4. && Cpro > 30.) {
Cpro = 30.;
} else if(Jpro < 7. && Cpro > 50.) {
Cpro = 50.;
}
hpro = hpro + hue;
if( hpro < 0.0 ) {
hpro += 360.0; //hue
}
}
if (hasColCurve1) {//curve 1 with Lightness and Brightness
if (curveMode == ColorAppearanceParams::TC_MODE_LIGHT) {
/* float Jj=(float) Jpro*327.68;
float Jold=Jj;
const Lightcurve& userColCurve = static_cast<const Lightcurve&>(customColCurve1);
userColCurve.Apply(Jj);
Jj=0.7f*(Jj-Jold)+Jold;//divide sensibility
*/
float Jj = (float) Jpro * 327.68f;
float Jold = Jj;
float Jold100 = (float) Jpro;
float redu = 25.f;
float reduc = 1.f;
const Lightcurve& userColCurveJ1 = static_cast<const Lightcurve&>(customColCurve1);
userColCurveJ1.Apply(Jj);
if(Jj > Jold) {
if(Jj < 65535.f) {
if(Jold < 327.68f * redu) {
Jj = 0.3f * (Jj - Jold) + Jold; //divide sensibility
} else {
reduc = LIM((100.f - Jold100) / (100.f - redu), 0.f, 1.f);
Jj = 0.3f * reduc * (Jj - Jold) + Jold; //reduct sensibility in highlights
}
}
} else if(Jj > 10.f) {
Jj = 0.8f * (Jj - Jold) + Jold;
} else if (Jj >= 0.f) {
Jj = 0.90f * (Jj - Jold) + Jold; // not zero ==>artifacts
}
Jpro = (double)(Jj / 327.68f);
if(Jpro < 1.) {
Jpro = 1.;
}
t1L = true;
} else if (curveMode == ColorAppearanceParams::TC_MODE_BRIGHT) {
//attention! Brightness curves are open - unlike Lightness or Lab or RGB==> rendering and algoritms will be different
float coef = ((aw + 4.f) * (4.f / c)) / 100.f;
float Qq = (float) Qpro * 327.68f * (1.f / coef);
float Qold100 = (float) Qpro / coef;
float Qold = Qq;
float redu = 20.f;
float reduc = 1.f;
const Brightcurve& userColCurveB1 = static_cast<const Brightcurve&>(customColCurve1);
userColCurveB1.Apply(Qq);
if(Qq > Qold) {
if(Qq < 65535.f) {
if(Qold < 327.68f * redu) {
Qq = 0.25f * (Qq - Qold) + Qold; //divide sensibility
} else {
reduc = LIM((100.f - Qold100) / (100.f - redu), 0.f, 1.f);
Qq = 0.25f * reduc * (Qq - Qold) + Qold; //reduct sensibility in highlights
}
}
} else if(Qq > 10.f) {
Qq = 0.5f * (Qq - Qold) + Qold;
} else if (Qq >= 0.f) {
Qq = 0.7f * (Qq - Qold) + Qold; // not zero ==>artifacts
}
Qpro = (double)(Qq * (coef) / 327.68f);
Jpro = 100.*(Qpro * Qpro) / ((4.0 / c) * (4.0 / c) * (aw + 4.0) * (aw + 4.0));
t1B = true;
if(Jpro < 1.) {
Jpro = 1.;
}
}
}
if (hasColCurve2) {//curve 2 with Lightness and Brightness
if (curveMode2 == ColorAppearanceParams::TC_MODE_LIGHT) {
float Jj = (float) Jpro * 327.68;
float Jold = Jj;
/*
const Lightcurve& userColCurve = static_cast<const Lightcurve&>(customColCurve2);
userColCurve.Apply(Jj);
Jj=0.7f*(Jj-Jold)+Jold;//divide sensibility
*/
float Jold100 = (float) Jpro;
float redu = 25.f;
float reduc = 1.f;
const Lightcurve& userColCurveJ2 = static_cast<const Lightcurve&>(customColCurve2);
userColCurveJ2.Apply(Jj);
if(Jj > Jold) {
if(Jj < 65535.f) {
if(Jold < 327.68f * redu) {
Jj = 0.3f * (Jj - Jold) + Jold; //divide sensibility
} else {
reduc = LIM((100.f - Jold100) / (100.f - redu), 0.f, 1.f);
Jj = 0.3f * reduc * (Jj - Jold) + Jold; //reduct sensibility in highlights
}
}
} else if(Jj > 10.f) {
if(!t1L) {
Jj = 0.8f * (Jj - Jold) + Jold;
} else {
Jj = 0.4f * (Jj - Jold) + Jold;
}
} else if (Jj >= 0.f) {
if(!t1L) {
Jj = 0.90f * (Jj - Jold) + Jold; // not zero ==>artifacts
} else {
Jj = 0.5f * (Jj - Jold) + Jold;
}
}
Jpro = (double)(Jj / 327.68f);
if(Jpro < 1.) {
Jpro = 1.;
}
} else if (curveMode2 == ColorAppearanceParams::TC_MODE_BRIGHT) { //
float coef = ((aw + 4.f) * (4.f / c)) / 100.f;
float Qq = (float) Qpro * 327.68f * (1.f / coef);
float Qold100 = (float) Qpro / coef;
float Qold = Qq;
float redu = 20.f;
float reduc = 1.f;
const Brightcurve& userColCurveB2 = static_cast<const Brightcurve&>(customColCurve2);
userColCurveB2.Apply(Qq);
if(Qq > Qold) {
if(Qq < 65535.f) {
if(Qold < 327.68f * redu) {
Qq = 0.25f * (Qq - Qold) + Qold; //divide sensibility
} else {
reduc = LIM((100.f - Qold100) / (100.f - redu), 0.f, 1.f);
Qq = 0.25f * reduc * (Qq - Qold) + Qold; //reduct sensibility in highlights
}
}
} else if(Qq > 10.f) {
Qq = 0.5f * (Qq - Qold) + Qold;
} else if (Qq >= 0.f) {
Qq = 0.7f * (Qq - Qold) + Qold; // not zero ==>artifacts