-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathOkishDRT_v1.blink
executable file
·1307 lines (1112 loc) · 37.8 KB
/
OkishDRT_v1.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 Okish_DRT_v1_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;
// Chomatic Adaptation Transform to Use
// 0: None
// 1: XYZ Scaling
// 2: Bradford
// 3: CAT02
// 4: Zhai2018 (two-step)
int catType;
// 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;
// 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;
//
// Tonescale Parameters
//
// Use lightness instead of mid(rgb) norm
bool useLightness;
// SSTS Luminances Min/Mid/Peak
float3 sstsLuminance;
//
// 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;
// Toggle Gamut Compression
bool applyGamutCompression;
bool applyGamutApproximation;
// MacAdam limit
bool applyMacAdamLimit;
// Blend Between Compressing towards
// Target Gamut Cusp Luminance (0.0)
// and SSTS Mid Luminance (1.0)
float cuspMidBlend;
// the distance of the compression focal point
// from the achromatic axis
// normalised to the distance of the gamut cusp
float focusDistance;
// Gamut Compression Fuction Parameters
// Threshold / Limit / Power
float3 compressionFuncParams;
// 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;
// When solving for the target gamut boundary
// how many search interval halving steps to perform
int boundarySolvePrecision;
// Number of iterations to converge on the uncompressed J value
// Because of the compression focus point changes depending on the J value of the uncompressed sample
// we cannot perfectly invert it since the original J value has now been changed by the forward compression
// we can converge on a reasonable approximation of the original J value by iterating the inverse compression
// although this is quite an expensive operation
int inverseSolverIterations;
//
// 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;
//
// Extra Parameters
//
// Toggle Inverse Transform
bool invert;
// Tonescale select
bool mmTonescaleMode;
// Tonescale parameters
float Lp;
float su;
float c0;
float cs;
float c1;
float p;
float w1;
float s1;
float ex;
float eb;
float e0;
float s0;
float fl;
float mmScaleFactor;
float desat_offset;
float chroma_scale;
local:
// constants
float HALF_MIN;
float HALF_MAX;
// CAT vars
float cat_adaptDegree;
// 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;
// matrix vars
float3x3 identity_matrix;
float3x3 XYZ_to_LMS_Bradford;
float3x3 XYZ_to_LMS_CAT02;
float3x3 XYZ_to_LMS_M1;
float3x3 XYZ_to_LMS_M2;
float3x3 M_to_RGBish;
float3x3 XYZ_to_RGB_input;
float3x3 XYZ_to_RGB_limit;
float3x3 XYZ_to_RGB_output;
float3x3 RGB_to_XYZ_input;
float3x3 RGB_to_XYZ_limit;
float3x3 RGB_to_XYZ_output;
// white points
float3 d65White;
float3 inWhite;
float3 refWhite;
// the maximum RGB value of the limiting gamut
float boundaryRGB;
// the maximum lightness value of the limiting gamut
float limitLmax;
// the maximum colorfulness value of the limiting gamut
float limitCmax;
// the 1D LUT used for quickly findig the approximate limiting gamut cusp LCh coordinates
// the samples are spaced by HSV hue increments of the limiting RGB gamut
// so to find the correct entry for a given hue (h) value
// one must search the table entries for the mathcing 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 LCh.h=0 line up
// the entries are then wrap-around shifted
// so that the 'gamutCuspTable' starts with the lowest LCh.h value
// both tables need to be declared here since tempoary array variables
// in the init() fuction seem to crash Nuke on some systems
float3 gamutCuspTableUnsorted[360];
float3 gamutCuspTable[360];
// local version of the public focusDistance parameter
// this one will be clamped to a value > 0.0
float focusDistanceClamped;
float3 gamutCornersTable[6];
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);
}
// "safe" power function to avoid NANs or INFs when taking a fractional power of a negative base
// this one initially retured -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 )
{
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;
}
// 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;
}
// apply chromatic adaptation transform to 'XYZ' from 'XYZ_ws' to 'XYZ_wd' white points
// 'type' selects the cone fundamentals matrix
float3 apply_CAT( float3 XYZ, float3 XYZ_ws, float3 XYZ_wd, int type)
{
float3x3 XYZ_to_LMS;
if( type == 1 )
{
// XYZ Scaling
XYZ_to_LMS = identity_matrix;
}
else if( type == 2 )
{
// Bradford
XYZ_to_LMS = XYZ_to_LMS_Bradford;
}
else if( type == 3 )
{
// CAT02
XYZ_to_LMS = XYZ_to_LMS_CAT02;
}
else
{
// None
return XYZ;
}
float3 LMS_ws = vector_dot(XYZ_to_LMS, XYZ_ws);
float3 LMS_wd = vector_dot(XYZ_to_LMS, XYZ_wd);
float3x3 Mscale = identity_matrix;
Mscale[0][0] = LMS_wd.x / LMS_ws.x;
Mscale[1][1] = LMS_wd.y / LMS_ws.y;
Mscale[2][2] = LMS_wd.z / LMS_ws.z;
float3x3 M = XYZ_to_LMS.invert() * Mscale * XYZ_to_LMS;
return vector_dot(M, XYZ);
}
// 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;
}
// 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;
}
float3 XYZ_to_Oklab_LMS(float3 XYZ)
{
float3 LMS = vector_dot(XYZ_to_LMS_M1, XYZ);
LMS.x = spow(LMS.x, 1.0f / 3.0f);
LMS.y = spow(LMS.y, 1.0f / 3.0f);
LMS.z = spow(LMS.z, 1.0f / 3.0f);
return LMS;
}
float3 Oklab_LMS_to_XYZ(float3 LMS)
{
LMS = LMS * LMS * LMS;
return vector_dot(XYZ_to_LMS_M1.invert(), LMS);
}
float3 Oklab_LMS_to_Lab(float3 LMS)
{
return vector_dot(XYZ_to_LMS_M2, LMS);
}
float3 Lab_to_Oklab_LMS(float3 lab)
{
return vector_dot(XYZ_to_LMS_M2.invert(), lab);
}
float3 Lab_to_LCh(float3 lab)
{
float3 lch;
lch.x = lab.x;
lch.y = sqrt(lab.y * lab.y + lab.z * lab.z);
lch.z = fmod(degrees(atan2(lab.z, lab.y)) + 360.0f, 360.0f);
return lch;
}
float3 LCh_to_Lab(float3 lch)
{
float3 lab;
float h = radians(lch.z);
lab.x = lch.x;
lab.y = lch.y * cos(h);
lab.z = lch.y * sin(h);
return lab;
}
float3 limit_RGB_to_LMS(float3 RGB)
{
float3 luminanceRGB = RGB * boundaryRGB * referenceLuminance;
float3 XYZ = vector_dot(RGB_to_XYZ_limit, luminanceRGB);
float3 LMS = XYZ_to_Oklab_LMS(XYZ);
return LMS;
}
float3 limit_RGB_to_LCh(float3 RGB)
{
float3 luminanceRGB = RGB * boundaryRGB * referenceLuminance;
float3 XYZ = vector_dot(RGB_to_XYZ_limit, luminanceRGB);
float3 LMS = XYZ_to_Oklab_LMS(XYZ);
return Lab_to_LCh(Oklab_LMS_to_Lab(LMS));
}
// convert RGB values in the input colorspace to XYZ D65
float3 input_RGB_to_XYZ(float3 inputRGB)
{
// clamp input to +/- HALF_MAX range (to remove inf values, etc.)
inputRGB = clamp3(inputRGB, -HALF_MAX, HALF_MAX);
// convert to linear XYZ luminance values
float3 luminanceRGB = encodingToLuminance3(encodingIn, inputRGB);
float3 luminanceXYZ = vector_dot(RGB_to_XYZ_input, luminanceRGB);
return apply_CAT(luminanceXYZ, inWhite, d65White, catType);
}
// convert XYZ D65 to RGB values in the output colorspace
float3 XYZ_to_output_RGB(float3 XYZ)
{
float3 luminanceXYZ = apply_CAT(XYZ, d65White, refWhite, catType);
float3 luminanceRGB = vector_dot(XYZ_to_RGB_output, luminanceXYZ);
float3 outputRGB = luminanceToEncoding3( encodingOut, luminanceRGB);
if (clampOutput)
outputRGB = clamp3(outputRGB, 0.0f, 1.0f);
return outputRGB;
}
// 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;
}
// retrieve the LC coordinates of the limiting gamut cusp at the hue slice 'h'
// cusps are very expensive to compute
// and the DRT is only using them for lightness mapping
// which does not require a high degree of accuracy
// so instead we use a pre-computed table of cusp points
// sampled at 1 degree hue intervals of the the RGB target gamut
// and lerp between them to get the approximate J & M values
float2 cuspFromTable(float h)
{
float3 lo;
float3 hi;
if( h <= gamutCuspTable[0].z )
{
lo = gamutCuspTable[gamutCuspTableSize-1];
lo.z = lo.z-360.0f;
hi = gamutCuspTable[0];
}
else if( h >= gamutCuspTable[gamutCuspTableSize-1].z )
{
lo = gamutCuspTable[gamutCuspTableSize-1];
hi = gamutCuspTable[0];
hi.z = hi.z+360.f;
}
else
{
for(int i = 1; i < gamutCuspTableSize; ++i)
{
if( h <= gamutCuspTable[i].z )
{
lo = gamutCuspTable[i-1];
hi = gamutCuspTable[i];
break;
}
}
}
float t = (h - lo.z) / (hi.z - lo.z);
float cuspJ = lerp(lo.x, hi.x, t);
float cuspM = lerp(lo.y, hi.y, t);
return float2(cuspJ,cuspM);
}
// find the LC coordinates of the smoothed boundary of the limiting gamaut in at the hue slice 'h'
// by searching along the line defined by 'LCSource' and 'LCfocus'
// the function will search outwards from where the line intersects the achromatic axis with a staring incement of 'startStepSize'
// once the boundary has been crossed it will search in the opposite direction with half the step size
// and will repeat this as as many times as is set by the 'precision' paramter
float2 findBoundary(float2 LCSource, float2 LCfocus, float h, float3 XYZw, float3 XYZd65, float3x3 XYZ_to_RGB, float smoothing, int precision, float startStepSize )
{
float2 achromaticIntercept = float2(LCfocus.x - (((LCSource.x-LCfocus.x) / (LCSource.y-LCfocus.y))*LCfocus.y), 0.0f);
if( achromaticIntercept.x <= 0.0f || achromaticIntercept.x >= limitLmax )
{
return achromaticIntercept;
}
float stepSize = startStepSize;
float2 unitVector = normalize(achromaticIntercept - LCfocus);
float2 LCtest = achromaticIntercept;
int searchOutwards = 1;
for( int i = 0; i < precision; ++i )
{
// while( 1 )
for( int j = 0; j < precision; ++j )
{
LCtest = LCtest + unitVector * stepSize;
float3 XYZ = Oklab_LMS_to_XYZ(Lab_to_Oklab_LMS(LCh_to_Lab(float3(LCtest.x, LCtest.y, h))));
XYZ = apply_CAT(XYZ, XYZd65, XYZw, catType);
int inside = isInsideCube( vector_dot(XYZ_to_RGB, XYZ / referenceLuminance ), boundaryRGB, smoothing);
if( searchOutwards )
{
if( LCtest.x < 0.0f || LCtest.x > limitLmax || LCtest.y > limitCmax || !inside )
{
searchOutwards = 0;
stepSize = -fabs(stepSize) / 2.0f;
break;
}
}
else
{
if( LCtest.y < 0.0f || inside )
{
searchOutwards = 1;
stepSize = fabs(stepSize) / 2.0f;
break;
}
}
}
}
float2 LCboundary = float2( clamp(LCtest.x, 0.0f, limitLmax), clamp(LCtest.y, 0.0f, limitCmax) );
return LCboundary;
}
void init()
{
HALF_MIN = 0.0000000596046448f;
HALF_MAX = 65504.0f;
if( discountIlluminant )
{
cat_adaptDegree = 1.0f;
}
else
{
float viewingConditionsCoeff = 1.0f;
if( viewingConditions == 0 )
{
viewingConditionsCoeff = 0.8f;
}
else if( viewingConditions == 1 )
{
viewingConditionsCoeff = 0.9f;
}
else if( viewingConditions == 2 )
{
viewingConditionsCoeff = 1.0f;
}
}
st2084_m_1=2610.0f / 4096.0f * (1.0f / 4.0f);
st2084_m_2=2523.0f / 4096.0f * 128.0f;
st2084_c_1=3424.0f / 4096.0f;
st2084_c_2=2413.0f / 4096.0f * 32.0f;
st2084_c_3=2392.0f / 4096.0f * 32.0f;
st2084_m_1_d = 1.0f / st2084_m_1;
st2084_m_2_d = 1.0f / st2084_m_2;
st2084_L_p = 10000.0f;
float identity_matrix_data[]={ 1.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 1.0f };
float XYZ_to_LMS_Bradford_data[]={ 0.8951f, 0.2664f,-0.1614f,
-0.7502f, 1.7135f, 0.0367f,
0.0389f,-0.0685f, 1.0296f };
float XYZ_to_LMS_CAT02_data[]={ 0.7328f, 0.4296f,-0.1624f,
-0.7036f, 1.6975f, 0.0061f,
0.0030f, 0.0136f, 0.9834f };
float XYZ_to_LMS_Oklab_M1_data[]={
0.8189330101, 0.3618667424, -0.1288597137,
0.0329845436, 0.9293118715, 0.0361456387,
0.0482003018, 0.2643662691, 0.6338517070 };
float XYZ_to_LMS_Oklab_M2_data[]={
0.2104542553, 0.7936177850, -0.0040720468,
1.9779984951, -2.4285922050, 0.4505937099,
0.0259040371, 0.7827717662, -0.8086757660 };
identity_matrix.setArray(identity_matrix_data);
XYZ_to_LMS_Bradford.setArray(XYZ_to_LMS_Bradford_data);
XYZ_to_LMS_CAT02.setArray(XYZ_to_LMS_CAT02_data);
XYZ_to_LMS_M1.setArray(XYZ_to_LMS_Oklab_M1_data);
XYZ_to_LMS_M2.setArray(XYZ_to_LMS_Oklab_M2_data);
// Blink does not seem to support initialising multidimensional arrays
// So instead of being able to index the matrix data directly from one
// we need to use long if/else statements to populate the
// input, limit & output primary matrices
// (maybe there is a better way?)
float XYZ_to_AP0_ACES_matrix_data[]=
{
1.0498110175f, 0.0000000000f, -0.0000974845f,
-0.4959030231f, 1.3733130458f, 0.0982400361f,
0.0000000000f, 0.0000000000f, 0.9912520182f
};
float XYZ_to_AP1_ACES_matrix_data[]=
{
1.6410233797f, -0.3248032942f, -0.2364246952f,
-0.6636628587f, 1.6153315917f, 0.0167563477f,
0.0117218943f, -0.0082844420f, 0.9883948585f,
};
float XYZ_to_Rec709_D65_matrix_data[]=
{
3.2409699419f, -1.5373831776f, -0.4986107603f,
-0.9692436363f, 1.8759675015f, 0.0415550574f,
0.0556300797f, -0.2039769589f, 1.0569715142f,
};
float XYZ_to_Rec2020_D65_matrix_data[]=
{
1.7166511880f, -0.3556707838f, -0.2533662814f,
-0.6666843518f, 1.6164812366f, 0.0157685458f,
0.0176398574f, -0.0427706133f, 0.9421031212f,
};
float XYZ_to_P3_D65_matrix_data[]=
{
2.4934969119f, -0.9313836179f, -0.4027107845f,
-0.8294889696f, 1.7626640603f, 0.0236246858f,
0.0358458302f, -0.0761723893f, 0.9568845240f,
};
float XYZ_to_P3_DCI_matrix_data[]=
{
2.7253940305f, -1.0180030062f, -0.4401631952f,
-0.7951680258f, 1.6897320548f, 0.0226471906f,
0.0412418914f, -0.0876390192f, 1.1009293786f
};
// populate the input primaries matrix
if( primariesIn == 0 )
{
XYZ_to_RGB_input.setArray(XYZ_to_AP0_ACES_matrix_data);
}
else if( primariesIn == 1 )
{
XYZ_to_RGB_input.setArray(XYZ_to_AP1_ACES_matrix_data);
}
else if( primariesIn == 2 )
{
XYZ_to_RGB_input.setArray(XYZ_to_Rec709_D65_matrix_data);
}
else if( primariesIn == 3 )
{
XYZ_to_RGB_input.setArray(XYZ_to_Rec2020_D65_matrix_data);
}
else if( primariesIn == 4 )
{
XYZ_to_RGB_input.setArray(XYZ_to_P3_D65_matrix_data);
}
else if( primariesIn == 5 )
{
XYZ_to_RGB_input.setArray(XYZ_to_P3_DCI_matrix_data);
}
else
{
XYZ_to_RGB_input.setArray(identity_matrix_data);
}
// populate the limiting primaries matrix
if( primariesLimit == 0 )
{
XYZ_to_RGB_limit.setArray(XYZ_to_AP0_ACES_matrix_data);
}
else if( primariesLimit == 1 )
{
XYZ_to_RGB_limit.setArray(XYZ_to_AP1_ACES_matrix_data);
}
else if( primariesLimit == 2 )
{
XYZ_to_RGB_limit.setArray(XYZ_to_Rec709_D65_matrix_data);
}
else if( primariesLimit == 3 )
{
XYZ_to_RGB_limit.setArray(XYZ_to_Rec2020_D65_matrix_data);
}
else if( primariesLimit == 4 )
{
XYZ_to_RGB_limit.setArray(XYZ_to_P3_D65_matrix_data);
}
else if( primariesLimit == 5 )
{
XYZ_to_RGB_limit.setArray(XYZ_to_P3_DCI_matrix_data);
}
else
{
XYZ_to_RGB_limit.setArray(identity_matrix_data);
}
// populate the output primaries matrix
if( primariesOut == 0 )
{
XYZ_to_RGB_output.setArray(XYZ_to_AP0_ACES_matrix_data);
}
else if( primariesOut == 1 )
{
XYZ_to_RGB_output.setArray(XYZ_to_AP1_ACES_matrix_data);
}
else if( primariesOut == 2 )
{
XYZ_to_RGB_output.setArray(XYZ_to_Rec709_D65_matrix_data);
}
else if( primariesOut == 3 )
{
XYZ_to_RGB_output.setArray(XYZ_to_Rec2020_D65_matrix_data);
}
else if( primariesOut == 4 )
{
XYZ_to_RGB_output.setArray(XYZ_to_P3_D65_matrix_data);
}
else if( primariesOut == 5 )
{
XYZ_to_RGB_output.setArray(XYZ_to_P3_DCI_matrix_data);
}
else
{
XYZ_to_RGB_output.setArray(identity_matrix_data);
}
RGB_to_XYZ_input = XYZ_to_RGB_input.invert();
RGB_to_XYZ_limit = XYZ_to_RGB_limit.invert();
RGB_to_XYZ_output = XYZ_to_RGB_output.invert();
float3x3 XYZ_to_RGB_sRGB;
XYZ_to_RGB_sRGB.setArray(XYZ_to_Rec709_D65_matrix_data);
float3 white(1.0f, 1.0f, 1.0f);
d65White = vector_dot(XYZ_to_RGB_sRGB.invert(), white);
inWhite = vector_dot(RGB_to_XYZ_input, white);
refWhite = vector_dot(RGB_to_XYZ_limit, white);
boundaryRGB = sstsLuminance.z / referenceLuminance;
//
// solving the RGB cusp from LCh is very expensive
// instead we go the other way and start with a RGB cusp sweep
// which is easily calculated by converting via HSV (Hue, 1.0, 1.0)
// we then convert each cusp to LCh and add them to a table
//
gamutCuspTableSize = 360;
for( int i = 0; i < gamutCuspTableSize; ++i )
{
float hNorm = float(i) / (gamutCuspTableSize);
float3 RGB = HSV_to_RGB(float3(hNorm, 1.0f, 1.0f));
gamutCuspTableUnsorted[i] = limit_RGB_to_LCh(RGB);
}
int minhIndex = 0;
for( int i = 1; i < gamutCuspTableSize; ++i )
{
if( gamutCuspTableUnsorted[i].z < gamutCuspTableUnsorted[minhIndex].z)
{
minhIndex = i;
}
}
for( int i = 0; i < gamutCuspTableSize; ++i )
{
gamutCuspTable[i] = gamutCuspTableUnsorted[(minhIndex+i)%gamutCuspTableSize];
}
// calculate the maximum expected J & M values for the given limit gamut
// these are used as limiting values for the gamut boundary searches
// limitLmax (asumed to match limitRGB white)
limitLmax = limit_RGB_to_LCh(float3(1.0f)).x;
// limitCmax (asumed to coincide with one of the RGBCMY corners of the limitRGB cube)
gamutCornersTable[0] = limit_RGB_to_LCh(float3(1.0f, 0.0f, 0.0f));
gamutCornersTable[1] = limit_RGB_to_LCh(float3(1.0f, 1.0f, 0.0f));