-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCAM_DRT_v042_pex3.blink
executable file
·2716 lines (2294 loc) · 83 KB
/
CAM_DRT_v042_pex3.blink
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
kernel DRT_CAM_Kernel : ImageComputationKernel<ePixelWise>
{
Image<eRead, eAccessPoint, eEdgeClamped> src; // the input image
// Image<eRead, eAccessPoint, eEdgeClamped> extra; // the input image
Image<eWrite> dst; // the output image
param:
//
// Input Parameters
//
// Encoding of the Input Image
// 0: Linear
// 1: ACEScct
// 2: sRGB
// 3: BT.1886 (Gamma 2.4)
// 4: Gamma 2.6
// 5: ST2084
int encodingIn;
// Primaries of the Input Image
// 0: AP0-ACES
// 1: AP1-ACES
// 2: sRGB/Rec.709-D65
// 3: Rec.2020-D65
// 4: P3-D65
// 5: P3-DCI
int primariesIn;
// Tonescale mode
// 0: Linear
// 1: Daniele Evo Curve
int toneScaleMode;
// Disable Degree of Adaptation Model for Zhai2018 CAT
// This is only effective if the limit primaries have a non-D65 white point
// since the input conversion is assumed to be fully adapted
// and the output conversion does not apply a CAT
bool discountIlluminant_in;
bool discountIlluminant_mid;
bool discountIlluminant_out;
// Toggles for Hellwig 2022 specific params
bool HK_mode_in;
bool HK_mode_mid;
bool HK_mode_out;
int compressMode;
// Reference Luminance in Cd/sqm
float referenceLuminance;
// Background Luminance in Cd/sqm
float backgroundLuminance;
// Viewing Conditions (for output)
// 0: Dark
// 1: Dim
// 2: Average
int viewingConditions;
int outputViewingConditions;
// Toggle Tone Mapping
bool applyTonecurve;
// SSTS Luminances Min/Mid/Peak
float3 sstsLuminance;
// Toggle chroma compression
bool applyChromaCompression;
bool applyInGamutCompression;
bool monochrome;
// Chroma compression params (limit, k1, k2)
float3 chromaCParams;
int cc_et;
// xy coordintes for chroma compression gamut
float2 crxy;
float2 cgxy;
float2 cbxy;
float2 cwxy;
// Global saturation
float sat;
//
// Gamut Mapping Parameters
//
// Primaries of the Target Gamut
// 0: AP0-ACES
// 1: AP1-ACES
// 2: sRGB/Rec.709-D65
// 3: Rec.2020-D65
// 4: P3-D65
// 5: P3-DCI
int primariesLimit;
// Primaries of the Gamut reached by the gamut compressor
// 0: AP0-ACES
// 1: AP1-ACES
// 2: sRGB/Rec.709-D65
// 3: Rec.2020-D65
// 4: P3-D65
// 5: P3-DCI
// 6: Spectral Locus
int primariesReach;
// Toggle Gamut Compression
bool applyGamutCompression;
// Blend Between Compressing towards
// Target Gamut Cusp Luminance (0.0)
// and SSTS Mid Luminance (1.0)
float cuspMidBlend;
// Focus distance of the compression focal point from the achromatic axis
float focusDistance;
// Gamut Compression Fuction Parameters
// Threshold / min Limit / max Limit / Power
float4 compressionFuncParams;
bool sixAxisCompressionMode;
float4 compressionFuncParamsR;
float4 compressionFuncParamsY;
float4 compressionFuncParamsG;
float4 compressionFuncParamsC;
float4 compressionFuncParamsB;
float4 compressionFuncParamsM;
bool Reachcompressmode;
bool Locuscompressmode;
bool iterativeGamutCompressor;
int iterativeGamutCompressorIterations;
// How much the edges of the target RGB cube are smoothed when finding the gamut boundary
// in order to reduce visible contours at the gamut cusps
float smoothCusps;
//
// Output Parameters
//
// Encoding of the Output Image
// 0: Linear
// 1: ACEScct
// 2: sRGB
// 3: BT.1886 (Gamma 2.4)
// 4: Gamma 2.6
// 5: ST2084
int encodingOut;
// Primaries of the Output Image
// 0: AP0-ACES
// 1: AP1-ACES
// 2: sRGB/Rec.709-D65
// 3: Rec.2020-D65
// 4: P3-D65
// 5: P3-DCI
int primariesOut;
// Clamp output values to 0.0 - 1.0
bool clampOutput;
bool softclampOutput;
float clamp_thr;
float clamp_dist;
//
// Extra Parameters
//
// Toggle Inverse Transform
bool invert;
// Diagnostic path modes
int diagnosticMode;
// DanieleEvoCurve (ACES2 candidate) parameters
float mmScaleFactor;
float daniele_n; // peak white
float daniele_n_r; // Normalized white in nits (what 1.0 should be)
float daniele_g; // surround / contrast
float daniele_c; // scene-referred grey
float daniele_c_d; // display-referred grey (in nits)
float daniele_w_g; // grey change between different peak luminance
float daniele_t_1; // shadow toe, flare/glare compensation - how ever you want to call it
float daniele_r_hit_min; // Scene-referred value "hitting the roof" at 100 nits
float daniele_r_hit_max; // Scene-referred value "hitting the roof" at 10,000 nits
// Hellwig 2022 CAM params
// the kernel parameters
// 0 = Stock CAT16
// 1 = Thomas's custom primaries
// 2 = live from params below
int catDataSelection; // original vs modified CAT16 matrix
// xy coordintes for custom CAT matrix
float2 rxy;
float2 gxy;
float2 bxy;
float2 wxy;
float ra;
float ba;
// Input vars
float3 XYZ_w;
float XYZ_w_scaler;
float L_A;
float Y_b;
float3 L_B;
float3 userSurround;
bool discount_illuminant;
// Output vars
float L_A_out;
float Y_b_out;
local:
// constants
float HALF_MINIMUM;
float HALF_MAXIMUM;
// Hellwig 2022 constants
float3x3 CAT_CAT16;
float3x3 panlrcm;
float daniele_r_hit;
float daniele_m_0;
float daniele_m_1;
float daniele_u;
float daniele_m;
float daniele_w_i;
float daniele_c_t;
float daniele_g_ip;
float daniele_g_ipp2;
float daniele_w_2;
float daniele_s_2;
float daniele_u_2;
float daniele_m_2;
// Chroma compression pre-calculated constants
float3 ccParams;
float el;
float ef;
float nJ_exp; // Tonescale based J exponent
float toe; // Shadow compression
// ST2084 vars
float st2084_m_1;
float st2084_m_2;
float st2084_c_1;
float st2084_c_2;
float st2084_c_3;
float st2084_m_1_d;
float st2084_m_2_d;
float st2084_L_p;
// using the float3x3 type to store the array of 6 coefficients
// because Blink does not support generic array assignments
// matrix vars
float3x3 identity_matrix;
float3x3 XYZ_to_RGB_input;
float3x3 XYZ_to_RGB_limit;
float3x3 XYZ_to_RGB_reach;
float3x3 XYZ_to_RGB_output;
float3x3 RGB_to_XYZ_input;
float3x3 RGB_to_XYZ_limit;
float3x3 RGB_to_XYZ_reach;
float3x3 RGB_to_XYZ_output;
float3x3 AP1_to_XYZ;
float3x3 XYZ_to_AP1;
// white points
float3 d65White;
float3 inWhite;
float3 outWhite;
float3 refWhite;
float3 limitWhite;
// the maximum RGB value of the limiting gamut
float boundaryRGB;
// the maximum lightness value of the limiting gamut
float limitJmax;
// the maximum colorfulness value of the limiting gamut
float limitMmax;
// Middle gray J
float midJ;
// Gamut intersection line gamma
float gamut_gamma;
// the 1D LUT used for quickly findig the approximate limiting gamut cusp JMh coordinates
// the samples are spaced by HSV hue increments of the limiting RGB gamut
// so to find the correct entry for a given CAM hue (h) value
// one must search the table entries for the matching entry.z component
int gamutCuspTableSize;
// the 'gamutCuspTableUnsorted' table is populated
// in increments of H of the limiting gamut HSV space starting at H=0.0
// since it is unlikely that HSV.H=0 and JMh.h=0 line up
// the entries are then wrap-around shifted
// so that the 'gamutCuspTable' starts with the lowest JMh.h value
// both tables need to be declared here since temporary array variables
// in the init() fuction seem to crash Nuke on some systems
float3 gamutCuspTableUnsorted[360];
float3 gamutCuspTable[360];
float3 gamutCuspTableUnsortedAP1[360];
float3 gamutCuspTableAP1[360];
float3 gamutCuspTableReach[360];
float3 cgamutCuspTable[360];
float LocusLimitMTable[360];
void define()
{
}
// multiplies a 3D vector with a 3x3 matrix
float3 vector_dot( float3x3 m, float3 v)
{
float3 r = 1.0f;
for(int c = 0; c<3; c++)
{
r[c] = m[c][0]*v.x + m[c][1]*v.y + m[c][2]*v.z;
}
return r;
}
// linear interpolation between two values a & b with the bias t
float lerp(float a, float b, float t)
{
return a + t * (b - a);
}
// linear interpolation between two float4 values a & b with the bias t
float4 lerp4(float4 a, float4 b, float t)
{
return a + t * (b - a);
}
// "safe" power function to avoid NANs or INFs when taking a fractional power of a negative base
// this one initially returned -pow(abs(b), e) for negative b
// but this ended up producing undesirable results in some cases
// so now it just returns 0.0 instead
float spow( float base, float exponent )
{
// a = np.atleast_1d(a)
float a = base;
float b = exponent;
// // p = as_float_array(p)
// float a_p = sign(a) * pow( fabs(a) ,p) ;
// // a_p[np.isnan(a_p)] = 0
// return a_p;
// np.sign(a) * pow(np.abs(a) , b)
// float a_p = sign(a) * pow(fabs(a) , b) ;
// if ( isnan(a_p) )
// {
// a_p = a_p;
// }
// else
// {
// a_p = 0.0;
// }
// return a_p;
if(base < 0.0f && exponent != floor(exponent) )
{
return 0.0f;
}
else
{
return pow(base, exponent);
}
}
// clamp the components of a 3D vector between a min & max value
float3 clamp3(float3 v, float min, float max)
{
v.x = clamp(v.x, min, max);
v.y = clamp(v.y, min, max);
v.z = clamp(v.z, min, max);
return v;
}
float3 float3spow( float3 base, float exponent )
{
return float3(spow(base.x, exponent), spow(base.y, exponent), spow(base.z, exponent));
}
float3 float3sign( float3 v )
{
return float3(sign(v.x), sign(v.y), sign(v.z));
}
float3 float3abs( float3 a )
{
return fabs(a);
}
// "safe" div
float sdiv( float a, float b )
{
if(b == 0.0f)
{
return 0.0f;
}
else
{
return a / b;
}
}
float clip(float x, float a, float b)
{
return max(a, min(x, b));
}
float mod(float a, float N)
{
return a - N*floor(a/N);
}
float degree_of_adaptation(float F, float L_A )
{
float D = F * (1 - (1 / 3.6) * exp((-L_A - 42) / 92));
return D;
}
// convert radians to degrees
float degrees( float radians )
{
return radians * 180.0f / PI;
}
// convert degrees to radians
float radians( float degrees )
{
return degrees / 180.0f * PI;
}
float3 compress_aces(float3 rgb, float3 c, float3 m, float3 y, int invert)
{
float ach = max(rgb.x, max(rgb.y, rgb.z));
float3 d = 0.0f;
if (ach)
{
d.x = (ach - rgb.x) / fabs(ach);
d.y = (ach - rgb.y) / fabs(ach);
d.z = (ach - rgb.z) / fabs(ach);
}
rgb.x = compressPowerP(d.x, c.x, c.y, c.z, invert);
rgb.y = compressPowerP(d.y, m.x, m.y, m.z, invert);
rgb.z = compressPowerP(d.z, y.x, y.y, y.z, invert);
rgb = ach - rgb * fabs(ach);
return rgb;
}
float3 compress_bjorn(float3 xyz)
{
float x = xyz.x;
float y = xyz.y;
float z = xyz.z;
float C = (x+y+z)/3;
if (C == 0.0f)
return xyz;
float R = sqrt(spow((x-C),2) + spow((y-C),2) + spow((z-C),2)) ;
// np.sqrt(2/3)
// 0.816496580927726
R = R * 0.816496580927726;
if (R != 0.0)
{
x = (x-C)/R ;
y = (y-C)/R ;
z = (z-C)/R ;
}
float r = R/C ;
float s = -min(x, min(y, z));
float t = 0.0;
if (r > 0.000001f)
{
t = (0.5+spow((spow((s-0.5),2) + spow((sqrt(4/spow(r,2)+1)-1),2)/4),0.5));
if (t < 0.000001f)
return xyz;
t = 1/t;
}
x = C*x*t + C ;
y = C*y*t + C ;
z = C*z*t + C ;
return float3(x,y,z);
}
float3 uncompress_bjorn(float3 xyz)
{
float x = xyz.x;
float y = xyz.y;
float z = xyz.z;
float C = (x+y+z)*(1.0/3.0) ;
if (C == 0.0)
return xyz;
float R = sqrt(spow((x-C),2) + spow((y-C),2) + spow((z-C),2));
// np.sqrt(2/3)
// 0.816496580927726
R = R * 0.816496580927726;
if (R != 0.0)
{
x = (x-C)/R ;
y = (y-C)/R ;
z = (z-C)/R ;
}
float t = R/C ;
float s = -min(x, min(y, z));
float r = 0.0;
if (t > 0.000001f)
{
r = sqrt(spow((2*sqrt(spow((1/t-0.5),2)-spow((s-0.5),2))+1),2)-1);
if (r < 0.000001f)
return xyz;
r = 2/r;
}
x = C*x*r + C ;
y = C*y*r + C ;
z = C*z*r + C ;
return float3(x,y,z);
}
float3 compress(float3 rgb)
{
return compress_bjorn(rgb);
}
float3 uncompress(float3 rgb)
{
return uncompress_bjorn(rgb);
}
float hue_angle_dependency_Hellwig2022(float h)
{
// h = as_float_array(h)
return float( \
-0.160 * cos(h) \
+ 0.132 * cos(2 * h) \
- 0.405 * sin(h) \
+ 0.080 * sin(2 * h) \
+ 0.792 \
);
}
float3x3 RGBPrimsToXYZMatrix(float2 rxy, float2 gxy, float2 bxy, float2 wxy,float Y, bool direction)
{
// # given r g b chromaticities and whitepoint, convert RGB colors to XYZ
// # based on CtlColorSpace.cpp from the CTL source code : 77
// # param: xy - dict of chromaticity xy coordinates: rxy: float2(x, y) etc
// # param: Y - luminance of "white" - defaults to 1.0
// # param: inverse - calculate XYZ to RGB instead
float2 r = rxy;
float2 g = gxy;
float2 b = bxy;
float2 w = wxy;
float X = w.x * Y / w.y;
float Z = (1 - w.x - w.y) * Y / w.y;
// # Scale factors for matrix rows
float d = r.x * (b.y - g.y) + b.x * (g.y - r.y) + g.x * (r.y - b.y);
float Sr = (X * (b.y - g.y) - \
g.x * (Y * (b.y - 1.0f) + \
b.y * (X + Z)) + \
b.x * (Y * (g.y - 1.0f) + \
g.y * (X + Z))) / d ;
float Sg = (X * (r.y - b.y) + \
r.x * (Y * (b.y - 1.0f) + \
b.y * (X + Z)) - \
b.x * (Y * (r.y - 1.0f) + \
r.y * (X + Z))) / d ;
float Sb = (X * (g.y - r.y) - \
r.x * (Y * (g.y - 1.0f) + \
g.y * (X + Z)) + \
g.x * (Y * (r.y - 1.0f) + \
r.y * (X + Z))) / d ;
// # Assemble the matrix
float Mdata[] =
{
Sr * r.x, Sr * r.y, Sr * (1.0f - r.x - r.y),
Sg * g.x, Sg * g.y, Sg * (1.0f - g.x - g.y),
Sb * b.x, Sb * b.y, Sb * (1.0f - b.x - b.y),
};
float MdataNukeOrder[] = {
Mdata[0], Mdata[3], Mdata[6],
Mdata[1], Mdata[4], Mdata[7],
Mdata[2], Mdata[5], Mdata[8],
};
float3x3 newMatrix;
newMatrix.setArray(MdataNukeOrder);
// create inverse matrix
float3x3 newMatrixInverse = newMatrix.invert();
// return forward or inverse matrix
if (direction == 0)
{
return newMatrix;
}
else if (direction == 1)
{
return newMatrixInverse;
}
}
float3 viewingConditionsToSurround(int viewingConditions)
{
float3 newSurround;
// hack to turn incoming int value into surround coeffs
if (viewingConditions == 0)
{
// "Dark": InductionFactors_CIECAM02(0.8, 0.525, 0.8),
newSurround = float3(0.8, 0.525, 0.8);
}
else if (viewingConditions == 1)
{
// "Dim": InductionFactors_CIECAM02(0.9, 0.59, 0.9),
newSurround = float3(0.9, 0.59, 0.9);
}
else if (viewingConditions == 2)
{
// "Average": InductionFactors_CIECAM02(1, 0.69, 1),
newSurround = float3(1.0, 0.69, 1.0);
}
else if (viewingConditions == 3)
{
// Pull from external input
newSurround = userSurround;
}
return newSurround;
}
// "PowerP" compression function (also used in the ACES Reference Gamut Compression transform)
// values of v above 'treshold' are compressed by a 'power' function
// so that an input value of 'limit' results in an output of 1.0
float compressPowerP( float v, float threshold, float limit, float power, int inverse )
{
float s = (limit-threshold)/pow(pow((1.0f-threshold)/(limit-threshold),-power)-1.0f,1.0f/power);
float vCompressed;
if( inverse )
{
vCompressed = (v<threshold||limit<1.0001f||v>threshold+s)?v:threshold+s*pow(-(pow((v-threshold)/s,power)/(pow((v-threshold)/s,power)-1.0f)),1.0f/power);
}
else
{
vCompressed = (v<threshold||limit<1.0001f)?v:threshold+s*((v-threshold)/s)/(pow(1.0f+pow((v-threshold)/s,power),1.0f/power));
}
return vCompressed;
}
// convert XYZ tristimulus values to the CAM J (lightness), M (colorfulness) and h (hue) correlates
// needs XYZ tristimulus values for the reference white and a D65 white as well as the viewing conditions as parameters
float3 XYZ_to_JMh( float3 XYZ, float3 referenceWhite, float3 d65White, int viewingConditions , float L_A, float Y_b, bool discountIlluminant, bool HK_mode)
{
return XYZ_to_Hellwig2022_JMh(XYZ, referenceWhite, L_A, Y_b,viewingConditionsToSurround(viewingConditions),discountIlluminant,HK_mode);
}
// convert the CAM J (lightness), M (colorfulness) and h (hue) correlates to XYZ tristimulus values
// needs XYZ tristimulus values for the reference white and a D65 white as well as the viewing conditions as parameters
float3 JMh_to_XYZ( float3 JMh, float3 referenceWhite, float3 d65White, int viewingConditions , float L_A, float Y_b, bool discountIlluminant, bool HK_mode)
{
float3 XYZ;
XYZ = Hellwig2022_JMh_to_XYZ(JMh, referenceWhite, L_A, Y_b, viewingConditionsToSurround(viewingConditions), discountIlluminant,HK_mode);
return XYZ;
}
// convert ACEScct encoded values to linear
float ACEScct_to_linear( float v )
{
return v > 0.155251141552511f ? spow( 2.0f, v * 17.52f - 9.72f) : (v - 0.0729055341958355f) / 10.5402377416545f;
}
// encode linear values as ACEScct
float linear_to_ACEScct( float v )
{
return v > 0.0078125f ? (log2(v) + 9.72f) / 17.52f : 10.5402377416545f * v + 0.0729055341958355f;
}
// convert sRGB gamma encoded values to linear
float sRGB_to_linear( float v )
{
return v < 0.04045f ? v / 12.92f : spow((v + 0.055f) / 1.055f, 2.4f);
}
// encode linear values as sRGB gamma
float linear_to_sRGB( float v )
{
return v <= 0.0031308f ? 12.92f * v : 1.055 * (spow(v, 1.0f / 2.4f)) - 0.055f;
}
// convert ST2084 PQ encoded values to linear
float ST2084_to_linear( float v )
{
float V_p = spow(v, st2084_m_2_d);
return spow((max(0.0f, V_p - st2084_c_1) / (st2084_c_2 - st2084_c_3 * V_p)), st2084_m_1_d)*st2084_L_p;
}
// encode linear values as ST2084 PQ
float linear_to_ST2084( float v )
{
float Y_p = spow(max(0.0f, v) / st2084_L_p, st2084_m_1);
return spow((st2084_c_1 + st2084_c_2 * Y_p) / (st2084_c_3 * Y_p + 1.0f), st2084_m_2);
}
// decode value 'v' with the inverse of the selected encoding fuction to luminance
float encodingToLuminance(int encoding, float v)
{
if( encoding == 1 )
{
// ACEScct
return ACEScct_to_linear(v) * referenceLuminance;
}
else if( encoding == 2 )
{
// sRGB
return sRGB_to_linear(v) * referenceLuminance;
}
else if( encoding == 3 )
{
// BT.1886 (Gamma 2.4)
return spow(v, 2.4f) * referenceLuminance;
}
else if( encoding == 4 )
{
// Gamma 2.6
return spow(v, 2.6f) * referenceLuminance;
}
else if( encoding == 5 )
{
// ST2084
return ST2084_to_linear(v);
}
else
{
// Linear
// default
return v * referenceLuminance;
}
}
// decode the components of a 3D vector 'v' with the inverse of the selected encoding fuction to luminance
float3 encodingToLuminance3(int encoding, float3 v)
{
float3 lin;
lin.x = encodingToLuminance(encoding, v.x);
lin.y = encodingToLuminance(encoding, v.y);
lin.z = encodingToLuminance(encoding, v.z);
return lin;
}
// encode the linear luminance value 'v' with the encoding fuction selected by 'encoding'
float luminanceToEncoding(int encoding, float v)
{
if( encoding == 1 )
{
// ACEScct
return linear_to_ACEScct(v / referenceLuminance);
}
else if( encoding == 2 )
{
// sRGB
return linear_to_sRGB(v / referenceLuminance);
}
else if( encoding == 3 )
{
// BT.1886 (Gamma 2.4)
return spow(v / referenceLuminance, 1.0f/2.4f);
}
else if( encoding == 4 )
{
// Gamma 2.6
return spow(v / referenceLuminance, 1.0f/2.6f);
}
else if( encoding == 5 )
{
// ST2084
return linear_to_ST2084(v);
}
else
{
// Linear
// default
return v / referenceLuminance;
}
}
// encode the linear luminance value components of a 3D vector 'v' with the encoding fuction selected by 'encoding'
float3 luminanceToEncoding3(int encoding, float3 v)
{
float3 enc;
enc.x = luminanceToEncoding(encoding, v.x);
enc.y = luminanceToEncoding(encoding, v.y);
enc.z = luminanceToEncoding(encoding, v.z);
return enc;
}
// convert RGB values in the output colorspace to the CAM J (lightness), M (colorfulness) and h (hue) correlates
float3 output_RGB_to_JMh(float3 RGB)
{
float3 luminanceRGB = encodingToLuminance3(encodingOut, RGB);
float3 XYZ = vector_dot(RGB_to_XYZ_output, luminanceRGB);
float3 JMh = XYZ_to_JMh(XYZ, limitWhite, d65White, viewingConditions, L_A_out, Y_b_out, discountIlluminant_out,HK_mode_out);
return JMh;
}
// convert RGB values in the output colorspace to the CAM J (lightness), M (colorfulness) and h (hue) correlates
float3 luminance_RGB_to_JMh(float3 luminanceRGB)
{
float3 XYZ = vector_dot(RGB_to_XYZ_output, luminanceRGB);
float3 JMh = XYZ_to_JMh(XYZ, refWhite, d65White, outputViewingConditions, L_A, Y_b, discountIlluminant_mid, HK_mode_mid);
return JMh;
}
// convert CAM J (lightness), M (colorfulness) and h (hue) correlates to RGB values in the output colorspace
float3 JMh_to_output_RGB(float3 JMh)
{
float3 luminanceXYZ = JMh_to_XYZ( JMh, limitWhite, d65White, outputViewingConditions , L_A_out, Y_b_out, discountIlluminant_out, HK_mode_out);
// switch to treat incoming data as luminanceXYZ when in breakout mode
if (diagnosticMode == 105)
{
luminanceXYZ = JMh;
}
float3 luminanceRGB = vector_dot(XYZ_to_RGB_output, luminanceXYZ);
if( softclampOutput )
{
// Soft clamp by compressing negative display linear values
float3 compr = float3(clamp_thr, clamp_dist, 1.2f);
luminanceRGB = compress_aces(luminanceRGB, compr, compr, compr, 0);
}
// return luminanceRGB when in breakout mode
if (diagnosticMode == 105)
{
return luminanceRGB;
}
float3 outputRGB = luminanceToEncoding3( encodingOut, luminanceRGB);
if( clampOutput )
{
outputRGB = clamp3(outputRGB, 0.0f, 1.0f);
}
return outputRGB;
}
// convert CAM J (lightness), M (colorfulness) and h (hue) correlates to RGB values in the output colorspace
float3 JMh_to_luminance_RGB(float3 JMh)
{
float3 luminanceXYZ = JMh_to_XYZ( JMh, refWhite, d65White, outputViewingConditions , L_A, Y_b, discountIlluminant_mid, HK_mode_mid);
float3 luminanceRGB = vector_dot(XYZ_to_RGB_output, luminanceXYZ);
return luminanceRGB;
}
// convert linear RGB values with the limiting primaries to CAM J (lightness), M (colorfulness) and h (hue) correlates
float3 limit_RGB_to_JMh(float3 RGB)
{
float3 luminanceRGB = RGB * boundaryRGB *referenceLuminance;
float3 XYZ = vector_dot(RGB_to_XYZ_limit, luminanceRGB);
float3 JMh = XYZ_to_JMh(XYZ, refWhite, d65White, viewingConditions, L_A, Y_b, discountIlluminant_mid, HK_mode_mid);
return JMh;
}
// convert linear RGB values with the limiting primaries to CAM J (lightness), M (colorfulness) and h (hue) correlates
float3 reach_RGB_to_JMh(float3 RGB)
{
float3 luminanceRGB = RGB * boundaryRGB *referenceLuminance;
float3 XYZ = vector_dot(RGB_to_XYZ_reach, luminanceRGB);
float3 JMh = XYZ_to_JMh(XYZ, refWhite, d65White, viewingConditions, L_A, Y_b, discountIlluminant_mid, HK_mode_mid);
return JMh;
}
// convert linear RGB values with the AP1 primaries to CAM J (lightness), M (colorfulness) and h (hue) correlates
float3 AP1_RGB_to_JMh(float3 RGB)
{
float3 luminanceRGB = RGB * boundaryRGB *referenceLuminance;
float3 XYZ = vector_dot(AP1_to_XYZ, luminanceRGB);
float3 JMh = XYZ_to_JMh(XYZ, refWhite, d65White, viewingConditions, L_A, Y_b, discountIlluminant_mid, HK_mode_mid);
return JMh;
}
// convert CAM J (lightness), M (colorfulness) and h (hue) correlates to linear RGB values with the limiting primaries
float3 JMh_to_limit_RGB(float3 JMh)
{
float3 luminanceXYZ = JMh_to_XYZ( JMh, refWhite, d65White, viewingConditions, L_A, Y_b, discountIlluminant_mid, HK_mode_mid );
float3 luminanceRGB = vector_dot(XYZ_to_RGB_output, luminanceXYZ); // this seems wrong, should be XYZ_to_RGB_limit?
float3 RGB = luminanceRGB / boundaryRGB / referenceLuminance;
return RGB;
}
// convert CAM J (lightness), M (colorfulness) and h (hue) correlates to linear RGB values with the reach primaries
float3 JMh_to_reach_RGB(float3 JMh)
{
float3 luminanceXYZ = JMh_to_XYZ( JMh, refWhite, d65White, viewingConditions, L_A, Y_b, discountIlluminant_mid, HK_mode_mid );
float3 luminanceRGB = vector_dot(XYZ_to_RGB_reach, luminanceXYZ);
float3 RGB = luminanceRGB / boundaryRGB / referenceLuminance;
return RGB;
}
float3 post_adaptation_non_linear_response_compression_forward(float3 RGB, float F_L)
{
float3 F_L_RGB = float3spow(F_L * float3abs(RGB) / 100.0f, 0.42f);
float3 RGB_c = (400.0f * sign(RGB) * F_L_RGB) / (27.13f + F_L_RGB);
return RGB_c;
}
float3 post_adaptation_non_linear_response_compression_inverse(float3 RGB,float F_L)
{
float3 RGB_p = (float3sign(RGB) * 100.0f / F_L * float3spow((27.13f * float3abs(RGB)) / (400.0f - float3abs(RGB)), 1.0f / 0.42f) );
return RGB_p;
}
// XYZ to Hellwig2020 JMh
//
// XYZ
// *CIE XYZ* tristimulus values of test sample / stimulus.
// XYZ_w
// *CIE XYZ* tristimulus values of reference white.
// L_A
// Adapting field *luminance* :math:`L_A` in :math:`cd/m^2`, (often taken
// to be 20% of the luminance of a white object in the scene).
// Y_b
// Luminous factor of background :math:`Y_b` such as
// :math:`Y_b = 100 x L_b / L_w` where :math:`L_w` is the luminance of the
// light source and :math:`L_b` is the luminance of the background. For
// viewing images, :math:`Y_b` can be the average :math:`Y` value for the
// pixels in the entire image, or frequently, a :math:`Y` value of 20,
// approximate an :math:`L^*` of 50 is used.
// surround
// Surround viewing conditions induction factors.
// Truth value indicating if the illuminant should be discounted.
// discount_illuminant
//
// NOTE: Following modifications have been made to stock Hellwig2022 model for this DRT:
//