-
Notifications
You must be signed in to change notification settings - Fork 304
/
curves.h
1156 lines (965 loc) · 32.3 KB
/
curves.h
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/>.
*/
#ifndef __CURVES_H__
#define __CURVES_H__
#include <glibmm.h>
#include <map>
#include <string>
#include "rt_math.h"
#include "../rtgui/mycurve.h"
#include "../rtgui/myflatcurve.h"
#include "../rtgui/mydiagonalcurve.h"
#include "color.h"
#include "procparams.h"
#include "pipettebuffer.h"
#include "LUT.h"
#define CURVES_MIN_POLY_POINTS 1000
#include "rt_math.h"
#define CLIPI(a) ((a)>0?((a)<65534?(a):65534):0)
using namespace std;
namespace rtengine
{
class ToneCurve;
class ColorAppearance;
class CurveFactory
{
friend class Curve;
protected:
// functions calculating the parameters of the contrast curve based on the desired slope at the center
static double solve_upper (double m, double c, double deriv);
static double solve_lower (double m, double c, double deriv);
static double dupper (const double b, const double m, const double c);
static double dlower (const double b, const double m, const double c);
// basic convex function between (0,0) and (1,1). m1 and m2 controls the slope at the start and end point
static inline double basel (double x, double m1, double m2)
{
if (x == 0.0) {
return 0.0;
}
double k = sqrt ((m1 - 1.0) * (m1 - m2) * 0.5) / (1.0 - m2);
double l = (m1 - m2) / (1.0 - m2) + k;
double lx = xlog(x);
return m2 * x + (1.0 - m2) * (2.0 - xexp(k * lx)) * xexp(l * lx);
}
// basic concave function between (0,0) and (1,1). m1 and m2 controls the slope at the start and end point
static inline double baseu (double x, double m1, double m2)
{
return 1.0 - basel(1.0 - x, m1, m2);
}
// convex curve between (0,0) and (1,1) with slope m at (0,0). hr controls the highlight recovery
static inline double cupper (double x, double m, double hr)
{
if (hr > 1.0) {
return baseu (x, m, 2.0 * (hr - 1.0) / m);
}
double x1 = (1.0 - hr) / m;
double x2 = x1 + hr;
if (x >= x2) {
return 1.0;
}
if (x < x1) {
return x * m;
}
return 1.0 - hr + hr * baseu((x - x1) / hr, m, 0);
}
// concave curve between (0,0) and (1,1) with slope m at (1,1). sr controls the shadow recovery
static inline double clower (double x, double m, double sr)
{
return 1.0 - cupper(1.0 - x, m, sr);
}
// convex curve between (0,0) and (1,1) with slope m at (0,0). hr controls the highlight recovery
static inline double cupper2 (double x, double m, double hr)
{
double x1 = (1.0 - hr) / m;
double x2 = x1 + hr;
if (x >= x2) {
return 1.0;
}
if (x < x1) {
return x * m;
}
return 1.0 - hr + hr * baseu((x - x1) / hr, m, 0.3 * hr);
}
static inline double clower2 (double x, double m, double sr)
{
//curve for b<0; starts with positive slope and then rolls over toward straight line to x=y=1
double x1 = sr / 1.5 + 0.00001;
if (x > x1 || sr < 0.001) {
return 1 - (1 - x) * m;
} else {
double y1 = 1 - (1 - x1) * m;
return y1 + m * (x - x1) - (1 - m) * SQR(SQR(1 - x / x1));
}
}
// tone curve base. a: slope (from exp.comp.), b: black point normalized by 65535,
// D: max. x value (can be>1), hr,sr: highlight,shadow recovery
static inline double basecurve (double x, double a, double b, double D, double hr, double sr)
{
if (b < 0) {
double m = 0.5;//midpoint
double slope = 1.0 + b; //slope of straight line between (0,-b) and (1,1)
double y = -b + m * slope; //value at midpoint
if (x > m) {
return y + (x - m) * slope; //value on straight line between (m,y) and (1,1)
} else {
return y * clower2(x / m, slope * m / y, 2.0 - sr);
}
} else {
double slope = a / (1.0 - b);
double m = a * D > 1.0 ? b / a + (0.25) / slope : b + (1 - b) / 4;
double y = a * D > 1.0 ? 0.25 : (m - b / a) * slope;
if (x <= m) {
return b == 0 ? x * slope : clower (x / m, slope * m / y, sr) * y;
} else if (a * D > 1.0) {
return y + (1.0 - y) * cupper2((x - m) / (D - m), slope * (D - m) / (1.0 - y), hr);
} else {
return y + (x - m) * slope;
}
}
}
static inline double simplebasecurve (double x, double b, double sr)
{
// a = 1, D = 1, hr = 0 (unused for a = D = 1)
if (b == 0.0) {
return x;
} else if (b < 0) {
double m = 0.5;//midpoint
double slope = 1.0 + b; //slope of straight line between (0,-b) and (1,1)
double y = -b + m * slope; //value at midpoint
if (x > m) {
return y + (x - m) * slope; //value on straight line between (m,y) and (1,1)
} else {
return y * clower2(x / m, slope * m / y, 2.0 - sr);
}
} else {
double slope = 1.0 / (1.0 - b);
double m = b + (1 - b) * 0.25;
double y = (m - b) * slope;
if (x <= m) {
return clower (x / m, slope * m / y, sr) * y;
} else {
return y + (x - m) * slope;
}
}
}
public:
const static double sRGBGamma; // standard average gamma
const static double sRGBGammaCurve; // 2.4 in the curve
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
// accurately determine value from integer array with float as index
//linearly interpolate from ends of range if arg is out of bounds
static inline float interp(int *array, float f)
{
int index = CLIPI(floor(f));
float part = (float)((f) - index) * (float)(array[index + 1] - array[index]);
return (float)array[index] + part;
}
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
// accurately determine value from float array with float as index
//linearly interpolate from ends of range if arg is out of bounds
static inline float flinterp(float *array, float f)
{
int index = CLIPI(floor(f));
float part = ((f) - (float)index) * (array[index + 1] - array[index]);
return array[index] + part;
}
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
static inline double centercontrast (double x, double b, double m);
// standard srgb gamma and its inverse
static inline double gamma2 (double x)
{
return x <= 0.00304 ? x * 12.92 : 1.055 * exp(log(x) / sRGBGammaCurve) - 0.055;
}
static inline double igamma2 (double x)
{
return x <= 0.03928 ? x / 12.92 : exp(log((x + 0.055) / 1.055) * sRGBGammaCurve);
}
static inline float gamma2 (float x)
{
return x <= 0.00304 ? x * 12.92 : 1.055 * expf(logf(x) / sRGBGammaCurve) - 0.055;
}
static inline float igamma2 (float x)
{
return x <= 0.03928 ? x / 12.92 : expf(logf((x + 0.055) / 1.055) * sRGBGammaCurve);
}
// gamma function with adjustable parameters
static inline double gamma (double x, double gamma, double start, double slope, double mul, double add)
{
return (x <= start ? x*slope : exp(log(x) / gamma) * mul - add);
}
static inline double igamma (double x, double gamma, double start, double slope, double mul, double add)
{
return (x <= start * slope ? x / slope : exp(log((x + add) / mul) * gamma) );
}
static inline float gamma (float x, float gamma, float start, float slope, float mul, float add)
{
return (x <= start ? x*slope : xexpf(xlogf(x) / gamma) * mul - add);
}
static inline float igamma (float x, float gamma, float start, float slope, float mul, float add)
{
return (x <= start * slope ? x / slope : xexpf(xlogf((x + add) / mul) * gamma) );
}
#ifdef __SSE2__
static inline vfloat igamma (vfloat x, vfloat gamma, vfloat start, vfloat slope, vfloat mul, vfloat add)
{
#if !defined(__clang__)
return (x <= start * slope ? x / slope : xexpf(xlogf((x + add) / mul) * gamma) );
#else
return vself(vmaskf_le(x, start * slope), x / slope, xexpf(xlogf((x + add) / mul) * gamma));
#endif
}
#endif
static inline float hlcurve (const float exp_scale, const float comp, const float hlrange, float level)
{
if (comp > 0.0) {
float val = level + (hlrange - 65536.0);
if(val == 0.0f) { // to avoid division by zero
val = 0.000001f;
}
float Y = val * exp_scale / hlrange;
Y *= comp;
if(Y <= -1.0) { // to avoid log(<=0)
Y = -.999999f;
}
float R = hlrange / (val * comp);
return log1p(Y) * R;
} else {
return exp_scale;
}
}
public:
static void complexCurve (double ecomp, double black, double hlcompr, double hlcomprthresh, double shcompr, double br, double contr,
procparams::ToneCurveParams::eTCModeId curveMode, const std::vector<double>& curvePoints, procparams::ToneCurveParams::eTCModeId curveMode2, const std::vector<double>& curvePoints2,
LUTu & histogram, LUTf & hlCurve, LUTf & shCurve, LUTf & outCurve, LUTu & outBeforeCCurveHistogram, ToneCurve & outToneCurve, ToneCurve & outToneCurve2,
int skip = 1);
static void curveBW (const std::vector<double>& curvePointsbw, const std::vector<double>& curvePointsbw2, const LUTu & histogrambw, LUTu & outBeforeCCurveHistogrambw,
ToneCurve & customToneCurvebw1, ToneCurve & customToneCurvebw2, int skip);
static void curveCL ( bool & clcutili, const std::vector<double>& clcurvePoints, LUTf & clCurve, int skip);
static void curveWavContL ( bool & wavcontlutili, const std::vector<double>& wavclcurvePoints, LUTf & wavclCurve,/* LUTu & histogramwavcl, LUTu & outBeforeWavCLurveHistogram,*/int skip);
static void curveDehaContL ( bool & dehacontlutili, const std::vector<double>& dehaclcurvePoints, LUTf & dehaclCurve, int skip, const LUTu & histogram, LUTu & outBeforeCurveHistogram);
static void mapcurve ( bool & mapcontlutili, const std::vector<double>& mapcurvePoints, LUTf & mapcurve, int skip, const LUTu & histogram, LUTu & outBeforeCurveHistogram);
static void curveToning ( const std::vector<double>& curvePoints, LUTf & ToningCurve, int skip);
static void complexsgnCurve ( bool & autili, bool & butili, bool & ccutili, bool & clcutili, const std::vector<double>& acurvePoints,
const std::vector<double>& bcurvePoints, const std::vector<double>& cccurvePoints, const std::vector<double>& lccurvePoints, LUTf & aoutCurve, LUTf & boutCurve, LUTf & satCurve, LUTf & lhskCurve,
int skip = 1);
static void complexLCurve (double br, double contr, const std::vector<double>& curvePoints, const LUTu & histogram, LUTf & outCurve, LUTu & outBeforeCCurveHistogram, int skip, bool & utili);
static void curveLightBrightColor (
const std::vector<double>& curvePoints,
const std::vector<double>& curvePoints2,
const std::vector<double>& curvePoints3,
const LUTu & histogram, LUTu & outBeforeCCurveHistogram,
const LUTu & histogramC, LUTu & outBeforeCCurveHistogramC,
ColorAppearance & outColCurve1,
ColorAppearance & outColCurve2,
ColorAppearance & outColCurve3,
int skip = 1);
static void RGBCurve (const std::vector<double>& curvePoints, LUTf & outCurve, int skip);
};
class Curve
{
class HashEntry
{
public:
unsigned short smallerValue;
unsigned short higherValue;
};
protected:
int N;
int ppn; // targeted polyline point number
double* x;
double* y;
// begin of variables used in Parametric curves only
double mc;
double mfc;
double msc;
double mhc;
// end of variables used in Parametric curves only
std::vector<double> poly_x; // X points of the faceted curve
std::vector<double> poly_y; // Y points of the faceted curve
std::vector<double> dyByDx;
std::vector<HashEntry> hash;
unsigned short hashSize; // hash table's size, between [10, 100, 1000]
double* ypp;
// Fields for the elementary curve polygonisation
double x1, y1, x2, y2, x3, y3;
bool firstPointIncluded;
double increment;
int nbr_points;
static inline double p00 (double x, double prot)
{
return CurveFactory::clower (x, 2.0, prot);
}
static inline double p11 (double x, double prot)
{
return CurveFactory::cupper (x, 2.0, prot);
}
static inline double p01 (double x, double prot)
{
return x <= 0.5 ? CurveFactory::clower (x * 2, 2.0, prot) * 0.5 : 0.5 + CurveFactory::cupper ((x - 0.5) * 2, 2.0, prot) * 0.5;
}
static inline double p10 (double x, double prot)
{
return x <= 0.5 ? CurveFactory::cupper (x * 2, 2.0, prot) * 0.5 : 0.5 + CurveFactory::clower ((x - 0.5) * 2, 2.0, prot) * 0.5;
}
static inline double pfull (double x, double prot, double sh, double hl)
{
return (1 - sh) * (1 - hl) * p00(x, prot) + sh * hl * p11(x, prot) + (1 - sh) * hl * p01(x, prot) + sh * (1 - hl) * p10(x, prot);
}
void fillHash();
void fillDyByDx();
public:
Curve ();
virtual ~Curve () {};
void AddPolygons ();
int getSize () const; // return the number of control points
void getControlPoint(int cpNum, double &x, double &y) const;
virtual double getVal (double t) const = 0;
virtual void getVal (const std::vector<double>& t, std::vector<double>& res) const = 0;
virtual bool isIdentity () const = 0;
};
class DiagonalCurve : public Curve
{
protected:
DiagonalCurveType kind;
void spline_cubic_set ();
void NURBS_set ();
public:
DiagonalCurve (const std::vector<double>& points, int ppn = CURVES_MIN_POLY_POINTS);
virtual ~DiagonalCurve ();
double getVal (double t) const;
void getVal (const std::vector<double>& t, std::vector<double>& res) const;
bool isIdentity () const
{
return kind == DCT_Empty;
};
};
class FlatCurve : public Curve
{
private:
FlatCurveType kind;
double* leftTangent;
double* rightTangent;
double identityValue;
bool periodic;
void CtrlPoints_set ();
public:
FlatCurve (const std::vector<double>& points, bool isPeriodic = true, int ppn = CURVES_MIN_POLY_POINTS);
virtual ~FlatCurve ();
double getVal (double t) const;
void getVal (const std::vector<double>& t, std::vector<double>& res) const;
bool setIdentityValue (double iVal);
bool isIdentity () const
{
return kind == FCT_Empty;
};
};
class RetinextransmissionCurve
{
private:
LUTf luttransmission; // 0xffff range
void Set(const Curve &pCurve);
public:
virtual ~RetinextransmissionCurve() {};
RetinextransmissionCurve();
void Reset();
void Set(const Curve *pCurve);
void Set(const std::vector<double> &curvePoints);
float operator[](float index) const
{
return luttransmission[index];
}
operator bool (void) const
{
return luttransmission;
}
};
class RetinexgaintransmissionCurve
{
private:
LUTf lutgaintransmission; // 0xffff range
void Set(const Curve &pCurve);
public:
virtual ~RetinexgaintransmissionCurve() {};
RetinexgaintransmissionCurve();
void Reset();
void Set(const Curve *pCurve);
void Set(const std::vector<double> &curvePoints);
float operator[](float index) const
{
return lutgaintransmission[index];
}
operator bool (void) const
{
return lutgaintransmission;
}
};
class ToneCurve
{
public:
LUTf lutToneCurve; // 0xffff range
virtual ~ToneCurve() {};
void Reset();
void Set(const Curve &pCurve, float gamma = 0);
operator bool (void) const
{
return lutToneCurve;
}
};
class OpacityCurve
{
public:
LUTf lutOpacityCurve; // 0xffff range
virtual ~OpacityCurve() {};
void Reset();
void Set(const Curve *pCurve);
void Set(const std::vector<double> &curvePoints, bool &opautili);
// TODO: transfer this method to the Color class...
float blend (float x, float lower, float upper) const
{
return (upper - lower) * lutOpacityCurve[x * 500.f] + lower;
}
void blend3f (float x, float lower1, float upper1, float &result1, float lower2, float upper2, float &result2, float lower3, float upper3, float &result3) const
{
float opacity = lutOpacityCurve[x * 500.f];
result1 = (upper1 - lower1) * opacity + lower1;
result2 = (upper2 - lower2) * opacity + lower2;
result3 = (upper3 - lower3) * opacity + lower3;
}
operator bool (void) const
{
return lutOpacityCurve;
}
};
class WavCurve
{
private:
LUTf lutWavCurve; // 0xffff range
void Set(const Curve &pCurve);
public:
float sum;
virtual ~WavCurve() {};
WavCurve();
void Reset();
void Set(const std::vector<double> &curvePoints);
float getSum() const
{
return sum;
}
float operator[](float index) const
{
return lutWavCurve[index];
}
operator bool (void) const
{
return lutWavCurve;
}
};
class WavOpacityCurveRG
{
private:
LUTf lutOpacityCurveRG; // 0xffff range
void Set(const Curve &pCurve);
public:
virtual ~WavOpacityCurveRG() {};
WavOpacityCurveRG();
void Reset();
// void Set(const std::vector<double> &curvePoints, bool &opautili);
void Set(const std::vector<double> &curvePoints);
float operator[](float index) const
{
return lutOpacityCurveRG[index];
}
operator bool (void) const
{
return lutOpacityCurveRG;
}
};
class WavOpacityCurveBY
{
private:
LUTf lutOpacityCurveBY; // 0xffff range
void Set(const Curve &pCurve);
public:
virtual ~WavOpacityCurveBY() {};
WavOpacityCurveBY();
void Reset();
void Set(const Curve *pCurve);
void Set(const std::vector<double> &curvePoints);
float operator[](float index) const
{
return lutOpacityCurveBY[index];
}
operator bool (void) const
{
return lutOpacityCurveBY;
}
};
class WavOpacityCurveW
{
private:
LUTf lutOpacityCurveW; // 0xffff range
void Set(const Curve &pCurve);
public:
virtual ~WavOpacityCurveW() {};
WavOpacityCurveW();
void Reset();
void Set(const Curve *pCurve);
void Set(const std::vector<double> &curvePoints);
float operator[](float index) const
{
return lutOpacityCurveW[index];
}
operator bool (void) const
{
return lutOpacityCurveW;
}
};
class WavOpacityCurveWL
{
private:
LUTf lutOpacityCurveWL; // 0xffff range
void Set(const Curve &pCurve);
public:
virtual ~WavOpacityCurveWL() {};
WavOpacityCurveWL();
void Reset();
void Set(const Curve *pCurve);
void Set(const std::vector<double> &curvePoints);
float operator[](float index) const
{
return lutOpacityCurveWL[index];
}
operator bool (void) const
{
return lutOpacityCurveWL;
}
};
class NoiseCurve
{
private:
LUTf lutNoiseCurve; // 0xffff range
float sum;
void Set(const Curve &pCurve);
public:
virtual ~NoiseCurve() {};
NoiseCurve();
void Reset();
void Set(const std::vector<double> &curvePoints);
float getSum() const
{
return sum;
}
float operator[](float index) const
{
return lutNoiseCurve[index];
}
operator bool (void) const
{
return lutNoiseCurve;
}
};
class ColorGradientCurve
{
public:
LUTf lut1; // [0.;1.] range (float values)
LUTf lut2; // [0.;1.] range (float values)
LUTf lut3; // [0.;1.] range (float values)
double low;
double high;
virtual ~ColorGradientCurve() {};
void Reset();
void SetXYZ(const Curve *pCurve, const double xyz_rgb[3][3], const double rgb_xyz[3][3], float satur, float lumin);
void SetXYZ(const std::vector<double> &curvePoints, const double xyz_rgb[3][3], const double rgb_xyz[3][3], float satur, float lumin);
void SetRGB(const Curve *pCurve, const double xyz_rgb[3][3], const double rgb_xyz[3][3]);
void SetRGB(const std::vector<double> &curvePoints, const double xyz_rgb[3][3], const double rgb_xyz[3][3]);
/**
* @brief Get the value of Red, Green and Blue corresponding to the requested index
* @param index value in the [0 ; 1] range
* @param r corresponding red value [0 ; 65535] (return value)
* @param g corresponding green value [0 ; 65535] (return value)
* @param b corresponding blue value [0 ; 65535] (return value)
*/
void getVal(float index, float &r, float &g, float &b) const;
operator bool (void) const
{
return lut1 && lut2 && lut3;
}
};
class ColorAppearance
{
public:
LUTf lutColCurve; // 0xffff range
virtual ~ColorAppearance() {};
void Reset();
void Set(const Curve &pCurve);
operator bool (void) const
{
return lutColCurve;
}
};
class Lightcurve : public ColorAppearance
{
public:
void Apply(float& Li) const;
};
//lightness curve
inline void Lightcurve::Apply (float& Li) const
{
assert (lutColCurve);
Li = lutColCurve[Li];
}
class Brightcurve : public ColorAppearance
{
public:
void Apply(float& Br) const;
};
//brightness curve
inline void Brightcurve::Apply (float& Br) const
{
assert (lutColCurve);
Br = lutColCurve[Br];
}
class Chromacurve : public ColorAppearance
{
public:
void Apply(float& Cr) const;
};
//Chroma curve
inline void Chromacurve::Apply (float& Cr) const
{
assert (lutColCurve);
Cr = lutColCurve[Cr];
}
class Saturcurve : public ColorAppearance
{
public:
void Apply(float& Sa) const;
};
//Saturation curve
inline void Saturcurve::Apply (float& Sa) const
{
assert (lutColCurve);
Sa = lutColCurve[Sa];
}
class Colorfcurve : public ColorAppearance
{
public:
void Apply(float& Cf) const;
};
//Colorfullness curve
inline void Colorfcurve::Apply (float& Cf) const
{
assert (lutColCurve);
Cf = lutColCurve[Cf];
}
class StandardToneCurve : public ToneCurve
{
public:
void Apply(float& r, float& g, float& b) const;
};
class StandardToneCurvebw : public ToneCurve
{
public:
void Apply(float& r, float& g, float& b) const;
};
class AdobeToneCurve : public ToneCurve
{
private:
void RGBTone(float& r, float& g, float& b) const; // helper for tone curve
public:
void Apply(float& r, float& g, float& b) const;
};
class AdobeToneCurvebw : public ToneCurve
{
private:
void RGBTone(float& r, float& g, float& b) const; // helper for tone curve
public:
void Apply(float& r, float& g, float& b) const;
};
class SatAndValueBlendingToneCurve : public ToneCurve
{
public:
void Apply(float& r, float& g, float& b) const;
};
class SatAndValueBlendingToneCurvebw : public ToneCurve
{
public:
void Apply(float& r, float& g, float& b) const;
};
class WeightedStdToneCurve : public ToneCurve
{
private:
float Triangle(float refX, float refY, float X2) const;
public:
void Apply(float& r, float& g, float& b) const;
};
class LuminanceToneCurve : public ToneCurve
{
public:
void Apply(float& r, float& g, float& b) const;
};
class PerceptualToneCurveState
{
public:
float Working2Prophoto[3][3];
float Prophoto2Working[3][3];
float cmul_contrast;
bool isProphoto;
};
// Tone curve whose purpose is to keep the color appearance constant, that is the curve changes contrast
// but colors appears to have the same hue and saturation as before. As contrast and saturation is tightly
// coupled in human vision saturation is modulated based on the curve's contrast, and that way the appearance
// can be kept perceptually constant (within limits).
class PerceptualToneCurve : public ToneCurve
{
private:
static float cf_range[2];
static float cf[1000];
// for ciecam02
static float f, c, nc, yb, la, xw, yw, zw, gamut;
static float n, d, nbb, ncb, cz, aw, wh, pfl, fl, pow1;
static void cubic_spline(const float x[], const float y[], const int len, const float out_x[], float out_y[], const int out_len);
static float find_minimum_interval_halving(float (*func)(float x, void *arg), void *arg, float a, float b, float tol, int nmax);
static float find_tc_slope_fun(float k, void *arg);
static float get_curve_val(float x, float range[2], float lut[], size_t lut_size);
float calculateToneCurveContrastValue() const;
public:
static void init();
void initApplyState(PerceptualToneCurveState & state, Glib::ustring workingSpace) const;
void Apply(float& r, float& g, float& b, PerceptualToneCurveState & state) const;
};
class WeightedStdToneCurvebw : public ToneCurve
{
private:
float Triangle(float refX, float refY, float X2) const;
public:
void Apply(float& r, float& g, float& b) const;
};
// Standard tone curve
inline void StandardToneCurve::Apply (float& r, float& g, float& b) const
{
assert (lutToneCurve);
r = lutToneCurve[r];
g = lutToneCurve[g];
b = lutToneCurve[b];
}
// Standard tone curve
inline void StandardToneCurvebw::Apply (float& r, float& g, float& b) const
{
assert (lutToneCurve);
r = lutToneCurve[r];
g = lutToneCurve[g];
b = lutToneCurve[b];
}
// Tone curve according to Adobe's reference implementation
// values in 0xffff space
// inlined to make sure there will be no cache flush when used
inline void AdobeToneCurve::Apply (float& r, float& g, float& b) const
{
assert (lutToneCurve);
if (r >= g) {
if (g > b) {
RGBTone (r, g, b); // Case 1: r >= g > b
} else if (b > r) {
RGBTone (b, r, g); // Case 2: b > r >= g
} else if (b > g) {
RGBTone (r, b, g); // Case 3: r >= b > g
} else { // Case 4: r >= g == b
r = lutToneCurve[r];
g = lutToneCurve[g];
b = g;
}
} else {
if (r >= b) {
RGBTone (g, r, b); // Case 5: g > r >= b
} else if (b > g) {
RGBTone (b, g, r); // Case 6: b > g > r
} else {
RGBTone (g, b, r); // Case 7: g >= b > r
}
}
}
inline void AdobeToneCurvebw::Apply (float& r, float& g, float& b) const
{
assert (lutToneCurve);
if (r >= g) {
if (g > b) {
RGBTone (r, g, b); // Case 1: r >= g > b
} else if (b > r) {
RGBTone (b, r, g); // Case 2: b > r >= g
} else if (b > g) {
RGBTone (r, b, g); // Case 3: r >= b > g
} else { // Case 4: r >= g == b
r = lutToneCurve[r];
g = lutToneCurve[g];
b = g;
}
} else {
if (r >= b) {
RGBTone (g, r, b); // Case 5: g > r >= b
} else if (b > g) {
RGBTone (b, g, r); // Case 6: b > g > r
} else {
RGBTone (g, b, r); // Case 7: g >= b > r
}
}
}
inline void AdobeToneCurve::RGBTone (float& r, float& g, float& b) const
{
float rold = r, gold = g, bold = b;
r = lutToneCurve[rold];
b = lutToneCurve[bold];
g = b + ((r - b) * (gold - bold) / (rold - bold));
}
inline void AdobeToneCurvebw::RGBTone (float& r, float& g, float& b) const
{
float rold = r, gold = g, bold = b;
r = lutToneCurve[rold];
b = lutToneCurve[bold];
g = b + ((r - b) * (gold - bold) / (rold - bold));
}
// Modifying the Luminance channel only
inline void LuminanceToneCurve::Apply(float &r, float &g, float &b) const
{
assert (lutToneCurve);
float currLuminance = r * 0.2126729f + g * 0.7151521f + b * 0.0721750f;
const float newLuminance = lutToneCurve[currLuminance];
currLuminance = currLuminance == 0.f ? 0.00001f : currLuminance;
const float coef = newLuminance / currLuminance;
r = LIM<float>(r * coef, 0.f, 65535.f);