-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCAM_DRT_v052_pex_gc.blink
executable file
·3146 lines (2674 loc) · 97.6 KB
/
CAM_DRT_v052_pex_gc.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<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;
// Viewing Conditions (for output)
// 0: Dark
// 1: Dim
// 2: Average
int viewingConditions;
int outputViewingConditions;
// Toggle Tone Mapping
bool applyTonecurve;
// Target Peak Luminance
float peakLuminance;
// Toggle chroma compression
bool applyChromaCompression;
bool applyInGamutExpansion;
bool applyInGamutCompression;
bool applyReachClamp;
bool monochrome;
// Chroma compression params
float chroma_compress;
float chroma_expand;
int cc_et;
int ccReach;
// xy coordintes for chroma compression gamut
float2 crxy;
float2 cgxy;
float2 cbxy;
float2 cwxy;
//
// Gamut Mapping Parameters
//
// Primaries of the Target Gamut
// 0: AP0-ACES
// 1: AP1-ACES
// 2: sRGB/Rec.709-D65 # now just Rec.709
// 3: Rec.2020-D65 # now just Rec.2020
// 4: P3-D65 # now just P3
// 5: P3-DCI # no longer included
int primariesLimit;
// White point of the limiting gamut
// effectively the "creative white"
// 0: ACES white
// 1: D65
// Could add others, or user white point
int whiteLimit;
// 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
// 7: Chroma Compression Space
int primariesReach;
// Toggle Gamut Compression
bool applyGamutCompression;
// Blend Between Compressing towards
// Target Gamut Cusp Luminance (0.0)
// and Mid Luminance (1.0)
float cuspMidBlend;
// Focus distance of the compression focal point from the achromatic axis
float focusDistance;
float focusAdjustB;
float focusAdjustD;
// 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 reachNick;
bool Locuscompressmode;
bool iterativeGamutCompressor;
int iterativeGamutCompressorIterations;
int boundryIntersectionMethod;
// 36 gamut parameters
float upperHullGamma[36];
bool disableUpperHullGamma;
float lowerHullGamma;
// 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 fitWhite;
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
float compr; // Compression
float sat; // Saturation
float sat_thr; // Threshold to start expanding saturation
// 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 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;
// Hellwig model's gamma (1 / cz)
float model_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];
float3 cgamutReachTable[360];
float gamutTopGamma[360];
float LocusLimitMTable[360];
void define()
{
}
// Functions used in ACES 2.0 candidate CAM DRT
// Derived from v49
// 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;
}
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;
}
// "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;
}
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;
}
// basic 3D hypotenuse function, does not deal with under/overflow
float hypot_float3(float3 xyz)
{
return sqrt(xyz.x*xyz.x + xyz.y*xyz.y + xyz.z*xyz.z);
}
float3 compress_bjorn(float3 xyz)
{
float C = (xyz.x+xyz.y+xyz.z)/3;
float3 xyz_temp = xyz - C;
float R = hypot_float3(xyz_temp);
if (R == 0.0f || C == 0.0f)
return xyz;
R = R * 0.816496580927726; // sqrt(2/3)
xyz_temp = xyz_temp / R;
float r = R/C ;
r = r*r ;
r = sqrt(4.0f / r + 1.0f) - 1.0f;
float s = -min(xyz_temp.x, min(xyz_temp.y, xyz_temp.z));
s = s -0.5f;
float t = 0.5f + sqrt(s*s + r*r / 4.0f);
t = C / t; // t is always >= 0.5f
xyz_temp = xyz_temp * t + C;
return xyz_temp;
}
float3 uncompress_bjorn(float3 xyz)
{
float C = (xyz.x+xyz.y+xyz.z)/3;
float3 xyz_temp = xyz - C;
float R = hypot_float3(xyz_temp);
if (R == 0.0f || C == 0.0f)
return xyz;
R = R * 0.816496580927726; // sqrt(2/3)
xyz_temp = xyz_temp / R;
float t = C/R;
t = t -0.5f;
float s = -min(xyz_temp.x, min(xyz_temp.y, xyz_temp.z));
s = s -0.5f;
float r = 2 * sqrt(fabs(t*t - s*s)) + 1;
r = sqrt(fabs(r*r - 1));
if (r == 0.0)
return xyz;
r = C * 2/r;
xyz_temp = xyz_temp * r + C;
return xyz_temp;
}
float hue_angle_dependency_Hellwig2022(float hr)
{
return
-0.160 * cos(hr)
+ 0.132 * cos(2 * hr)
- 0.405 * sin(hr)
+ 0.080 * sin(2 * hr)
+ 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;
}
}
// 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;
}
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;
}
// convert HSV cylindrical projection values to RGB
float3 HSV_to_RGB( float3 HSV )
{
float C = HSV.z*HSV.y;
float X = C*(1.0f-fabs(fmod(HSV.x*6.0f,2.0f)-1.0f));
float m = HSV.z-C;
float3 RGB;
RGB.x = (HSV.x<1.0f/6.0f? C :HSV.x<2.0f/6.0f? X :HSV.x<3.0f/6.0f?0.0f:HSV.x<4.0f/6.0f?0.0f:HSV.x<5.0f/6.0f? X : C )+m;
RGB.y = (HSV.x<1.0f/6.0f? X :HSV.x<2.0f/6.0f? C :HSV.x<3.0f/6.0f? C :HSV.x<4.0f/6.0f? X :HSV.x<5.0f/6.0f?0.0f:0.0f)+m;
RGB.z = (HSV.x<1.0f/6.0f?0.0f:HSV.x<2.0f/6.0f?0.0f:HSV.x<3.0f/6.0f? X :HSV.x<4.0f/6.0f? C :HSV.x<5.0f/6.0f? C : X )+m;
return RGB;
}
// convert RGB to HSV cylindrical projection values
float3 RGB_to_HSV( float3 RGB )
{
float cmax = max(RGB.x,max(RGB.y,RGB.z));
float cmin = min(RGB.x,min(RGB.y,RGB.z));
float delta = cmax-cmin;
float3 HSV;
HSV.x = delta==0.0f?0.0f:cmax==RGB.x?(fmod((RGB.y-RGB.z)/delta+6.0f,6.0f))/6.0f:cmax==RGB.y?(((RGB.z-RGB.x)/delta+2.0f)/6.0f):(((RGB.x-RGB.y)/delta+4.0f)/6.0f);
HSV.y = cmax == 0.0f ? 0.0f : delta / cmax;
HSV.z = cmax;
return HSV;
}
// Smooth minimum of a and b
float smin(float a, float b, float s)
{
float h = max(s - fabs(a - b), 0.0) / s;
return min(a, b) - h * h * h * s * (1.0f / 6.0f);
}
// reimplemented from https://github.com/nick-shaw/aces-ot-vwg-experiments/blob/master/python/intersection_approx.py
float solve_J_intersect(float2 JM, float focusJ, float maxJ, float slope_gain)
{
float a = JM.y / (focusJ * slope_gain);
float b = 0.0f;
float c = 0.0f;
float intersectJ = 0.0f;
if (JM.x < focusJ)
{
b = 1.0f - JM.y / slope_gain;
}
else
{
b= -(1.0f + JM.y / slope_gain + maxJ * JM.y / (focusJ * slope_gain));
}
if (JM.x < focusJ)
{
c = -JM.x;
}
else
{
c = maxJ * JM.y / slope_gain + JM.x;
}
float root = sqrt(b*b - 4.0f * a * c);
if (JM.x < focusJ)
{
intersectJ = 2.0f * c / (-b - root);
}
else
{
intersectJ = 2.0f * c / (-b + root);
}
return intersectJ;
}
// check if the 3D point 'v' is inside a cube with the dimensions cubeSize x cubeSize x cubeSize
// the 'smoothing' parameter rounds off the edges and corners of the cube with the exception of the 0,0,0 and cubeSize x cubeSize x cubeSize corners
// a smoothing value of 0.0 applies no smoothing and 1.0 the maximum amount (smoothing values > 1.0 result in undefined behavior )
int isInsideCube( float3 v, float cubeSize, float smoothing)
{
float3 normv = v / cubeSize;
float minv = min(normv.x, min(normv.y, normv.z));
float maxv = max(normv.x, max(normv.y, normv.z));
if( smoothing <= 0.0f )
{
// when not smoothing we can use a much simpler test
if(minv < 0.0f || maxv > 1.0f)
{
return 0;
}
return 1;
}
float3 clamped = normv;
float radius = smoothing/2.0f;
radius = clamp(radius*maxv*(1.0f-minv), 0.0f, radius);
clamped.x = clamp(normv.x, radius, 1.0f-radius);
clamped.y = clamp(normv.y, radius, 1.0f-radius);
clamped.z = clamp(normv.z, radius, 1.0f-radius);
if( length(normv - clamped ) > radius)
{
return 0;
}
return 1;
}
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;
}
// convert XYZ tristimulus values to the CAM J (lightness), M (colorfulness) and h (hue) correlates
// needs XYZ tristimulus values for the reference white and the viewing conditions as parameters
float3 XYZ_to_JMh( float3 XYZ, float3 referenceWhite, 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 the viewing conditions as parameters
float3 JMh_to_XYZ( float3 JMh, float3 referenceWhite, int viewingConditions , float L_A, float Y_b, bool discountIlluminant, bool HK_mode)
{
return Hellwig2022_JMh_to_XYZ(JMh, referenceWhite, L_A, Y_b, viewingConditionsToSurround(viewingConditions), discountIlluminant,HK_mode);
}
// 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, 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, 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, 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;
}
// First matrix to limiting gamut for clamping
float3 luminanceRGB = vector_dot(XYZ_to_RGB_limit, luminanceXYZ);
if( fitWhite)
{
// Scale to fit maximum creative white channel value
float3 creativeWhiteXYZ = vector_dot(RGB_to_XYZ_limit, float3(1.0f, 1.0f, 1.0f));