-
Notifications
You must be signed in to change notification settings - Fork 2
/
tonemap.html
1688 lines (1477 loc) · 52.8 KB
/
tonemap.html
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
<html>
<head>
<script>
var canvas;
var gl;
var program;
var filename;
var texture;
var texture_w;
var texture_h;
var texture_pixels;
var hlg_ootf_gamma = 1.2;
var viewport_w;
var viewport_h;
var save_requested = false;
var saved_settings1 = {};
var saved_settings2 = {};
var size_mode = 0;
let clamp = function(x, a, b) {
return Math.min(Math.max(x, a), b);
}
let log2 = function(x) { return Math.log(x) / Math.log(2); }
let exp2 = function(x) { return Math.exp(x * Math.log(2)); }
let mix = function(x, y, amount) {
amount = clamp(amount, 0, 1);
return (1-amount)*x + (amount)*y;
}
let linearToSrgb = function (x) {
let a = 1.1371188301409823;
let c = 12.919999999992248;
let d = 0.003130800090713953;
let e = -0.05499994754780801;
let g = 0.4166666666666667;
if (x < d)
return c*x;
return Math.pow(a*x, g) + e;
}
let pqSignalToNits = function(v) {
const c1 = 107.0 / 128.0;
const c2 = 2413.0 / 128.0;
const c3 = 2392.0 / 128.0;
const m1 = 1305.0 / 8192.0;
const m2 = 2523.0 / 32.0;
const p = Math.pow(Math.min(Math.max(v, 0.0), 1.0), 1.0 / m2);
return 10000.0 * Math.pow(Math.max(p - c1, 0.0) / (c2 - c3 * p), 1.0 / m1);
}
const kAdaptationModeSpecific = 0;
const kAdaptationModeGainmap = 1;
const kGamutMapNone = 0;
const kGamutMapMark = 1;
const kGamutMapClamp = 2;
const kGamutMapScaleRGB = 3;
const kGamutMapScaleUV = 4;
const kGamutMapL2RGB = 5;
const kGamutMapOkLabConstantH = 6;
const kToneMapNone = 0;
const kToneMapNativePQ = 1;
const kToneMapNativeHLG = 2;
const kToneMapClamp = 3;
const kToneMapReinhard = 4;
const kToneMapBT2408 = 5;
const kToneMapBT2446A = 6;
const kToneMapBT2446C = 7;
const kToneMapBT2100HLG = 8;
const kToneMapHLGAs22 = 9;
const kToneMapBT2446A_redo = 10;
const ToneMapInputMaxRGB = 0;
const ToneMapInputYRGB = 1;
const ToneMapInputRpGpBp = 2;
const ToneMapInputMix = 3;
const kTransferSRGB = 0;
const kTransferHLG = 1;
const kTransferPQ = 2;
const kTransferG22_AsHLG = 5;
const kTransferPQ_G = 3;
const kTransferG22 = 4;
const kGamutSRGB = 0;
const kGamutP3 = 1;
const kGamutRec2020 = 2;
const fs_constants = `
int kAdaptationModeSpecific = 0;
int kAdaptationModeGainmap = 1;
int kGamutMapNone = 0;
int kGamutMapMark = 1;
int kGamutMapClamp = 2;
int kGamutMapScaleRGB = 3;
int kGamutMapScaleUV = 4;
int kGamutMapL2RGB = 5;
int kGamutMapOkLabConstantH = 6;
int kToneMapNone = 0;
int kToneMapNativePQ = 1;
int kToneMapNativeHLG = 2;
int kToneMapClamp = 3;
int kToneMapReinhard = 4;
int kToneMapBT2408 = 5;
int kToneMapBT2446A = 6;
int kToneMapBT2446C = 7;
int kToneMapBT2100HLG = 8;
int kToneMapHLGAs22 = 9;
int kToneMapBT2446A_redo = 10;
int ToneMapInputMaxRGB = 0;
int ToneMapInputYRGB = 1;
int ToneMapInputRpGpBp = 2;
int ToneMapInputMix = 3;
int kTransferSRGB = 0;
int kTransferHLG = 1;
int kTransferPQ = 2;
int kTransferG22_AsHLG = 5;
int kTransferPQ_G = 3;
int kTransferG22 = 4;
int kGamutSRGB = 0;
int kGamutP3 = 1;
int kGamutRec2020 = 2;
int kFakeGamutOkLab = 3;
`
const vs = `attribute vec2 position;
varying vec2 texcoord;
void main() {
texcoord = vec2(0.5+0.5*position.x, 0.5-0.5*position.y);
gl_Position = vec4(position, 0.0, 1.0);
}`;
const fs = `
precision highp float;
uniform sampler2D texture;
uniform float raise_gamma;
uniform float clli_max_nits;
uniform float ndwl_white_nits;
uniform float hlg_ootf_gamma;
uniform float hlg_scale;
uniform int hlg_ootf_per_channel;
uniform int adaptation_mode;
uniform int tonemap_mode;
uniform int tonemap_weight_before;
uniform float tonemap_weight_maxRGB;
uniform float tonemap_weight_RpGpBp;
uniform float tonemap_weight_YRGB;
uniform int gamut_map_mode;
uniform int final_gamut_mode;
uniform int final_gamut;
uniform int signal_transfer;
uniform int signal_primaries;
uniform int use_bgr;
uniform float target_headroom_log2;
varying vec2 texcoord;
` + fs_constants + `
// Primary conversion matrices.
mat3 p3_to_xyzd50 = mat3( 0.515102, 0.241182, -0.00104941,
0.291965, 0.692236, 0.0418818,
0.157153, 0.0665819, 0.784378);
mat3 srgb_to_xyzd50 = mat3( 0.43606567, 0.2224884, 0.01391602,
0.38514709, 0.71687317, 0.09707642,
0.14306641, 0.06060791, 0.71409607);
mat3 rec2020_to_xyzd50 = mat3( 0.673459, 0.279033, -0.00193139,
0.165661, 0.675338, 0.0299794,
0.1251, 0.0456288, 0.797162);
mat3 xyzd50_to_rec2020 = mat3( 1.6472752, -0.68261762, 0.02966273,
-0.39360248, 1.64761778, -0.06291669,
-0.23598029, 0.01281627, 1.25339643);
mat3 xyzd50_to_p3 = mat3( 2.40404516, -0.84222838, 0.04818706,
-0.98989869, 1.79885051, -0.09737385,
-0.39763172, 0.01604817, 1.27350664);
mat3 xyzd50_to_srgb = mat3( 3.13411215, -0.97878729, 0.07198304,
-1.61739246, 1.91627959, -0.22898585,
-0.4906334, 0.03345471, 1.40538513);
mat3 xyzd50_to_xyzd65 = mat3( 0.95547345, -0.02836971, 0.01231400,
-0.02309854, 1.00999546, -0.02050770,
0.06325931, 0.02104140, 1.33036594);
mat3 xyzd65_to_xyzd50 = mat3( 1.04792976, 0.02962780, -0.00924303,
0.02294695, 0.99043437, 0.01505523,
-0.05019232, -0.01707377, 0.75187428);
mat3 xyzd65_to_lms = mat3( 0.81902244, 0.03298367, 0.04817720,
0.36190626, 0.92928685, 0.26423952,
-0.12887378, 0.03614467, 0.63354783);
mat3 lms_to_xyzd65 = mat3( 1.22687987, -0.04057576, -0.07637295,
-0.55781500, 1.11228683, -0.42149332,
0.28139105, -0.07171107, 1.58692402);
mat3 lms_to_oklab = mat3( 0.21045426, 1.97799850, 0.02590404,
0.79361779, -2.42859221, 0.78277177,
-0.00407205, 0.45059371, -0.80867577);
mat3 oklab_to_lms = mat3( 1.00000000, 1.00000001, 1.00000005,
0.39633779, -0.10556134, -0.08948418,
0.21580376, -0.06385417, -1.29148554);
// Given a coordinate x, return the coordinate y such that (x,y) is on the
// Bezier curve defined by control points p0,p1,s2 that satisfy
// satisfy p0.x < p1.x < p2.x, and p0.y < p1.y = p2.y
float bezier_y_of_x(float x, vec2 p0, vec2 p1, vec2 p2) {
if (x <= p0.x) {
return p0.y;
}
if (x >= p2.x) {
return p2.y;
}
// Compute the coefficients so that the Bezier curve is
// B2D(t) = (a_x * t*t + b_x * t + c_x,
// a_y * t*t + b_y * t + c_y)
float a_x = p0.x - 2.0*p1.x + p2.x;
float b_x = 2.0*p1.x - 2.0*p2.x;
float c_x = p2.x;
float a_y = p0.y - 2.0*p1.y + p2.y;
float b_y = 2.0*p1.y - 2.0*p2.y;
float c_y = p2.y;
// Using three steps of Halleys' method, solve for t:
// a_x*t*t + b_x*t + c_x = x
float t = 0.0;
float f2 = 2.0*a_x;
for (int i = 0; i < 3; ++i) {
float f1 = 2.0*a_x*t + b_x;
float f0 = a_x*t*t + b_x*t + (c_x-x);
t -= 2.0*f0*f1 / (2.0*f1*f1 - f0*f2);
}
return (a_y*t*t + b_y*t + c_y);
}
// Y_rec2020 is the luminance vector in rec2020 primaries.
vec3 rec2020_rgb_to_yuv(vec3 rgb) {
mat4 rgb_to_yuv = mat4(
0.262700, -0.139630, 0.500000, 0.0,
0.678000, -0.360370, -0.459786, 0.0,
0.059300, 0.500000, -0.040214, 0.0,
0.000000, 0.500122, 0.500122, 1.0);
return (rgb_to_yuv * vec4(rgb, 1.0)).rgb;
}
vec3 rec2020_yuv_to_rgb(vec3 yuv) {
mat4 yuv_to_rgb = mat4(
1.0, 1.0, 1.0, 0.0,
-0.00000089, -0.16455278, 1.88139998, 0.0,
1.47459975, -0.57135301, -0.00000024, 0.0,
-0.73747933, 0.36804268, -0.9409294, 1.0);
return (yuv_to_rgb * vec4(yuv, 1.0)).rgb;
}
vec3 xyzd50_to_oklab(vec3 xyzd50) {
vec3 lms = xyzd65_to_lms * xyzd50_to_xyzd65 * xyzd50;
vec3 lms_cbrt = (sign(lms) * pow(abs(lms), vec3(1.0/3.0)));
vec3 Lab = lms_to_oklab * lms_cbrt;
return Lab;
}
vec3 oklab_to_xyzd50(vec3 Lab) {
vec3 lms_cbrt = oklab_to_lms * Lab;
vec3 lms = sign(lms_cbrt) * pow(abs(lms_cbrt), vec3(3.0));
return xyzd65_to_xyzd50 * lms_to_xyzd65 * lms;
}
float srgbToLinear(float x) {
if (x < 0.0)
return 0.0;
if (x > 1.0)
return 1.0;
if (x < 0.04045)
return x / 12.92;
return pow((x + 0.055)/1.055, 2.4);
}
float linearToSrgb(float x) {
if (x < 0.003130800090713953)
return 12.919999999992248*x;
return pow(1.1371188301409823*x, 0.4166666666666667) - 0.05499994754780801;
}
vec3 srgbToLinear(vec3 c) {
return vec3(srgbToLinear(c.r), srgbToLinear(c.g), srgbToLinear(c.b));
}
vec3 linearToSrgb(vec3 c) {
return vec3(linearToSrgb(c.r), linearToSrgb(c.g), linearToSrgb(c.b));
}
float pqSignalToNits(float v) {
float c1 = 107.0 / 128.0;
float c2 = 2413.0 / 128.0;
float c3 = 2392.0 / 128.0;
float m1 = 1305.0 / 8192.0;
float m2 = 2523.0 / 32.0;
float p = pow(clamp(v, 0.0, 1.0), 1.0 / m2);
return 10000.0 * pow(max(p - c1, 0.0) / (c2 - c3 * p), 1.0 / m1);
}
float pqNitsToSignal(float L) {
float c1 = 107.0 / 128.0;
float c2 = 2413.0 / 128.0;
float c3 = 2392.0 / 128.0;
float m1 = 1305.0 / 8192.0;
float m2 = 2523.0 / 32.0;
float v = pow(clamp(L / 10000.0, 0.0, 1.0), m1);
return pow((c1 + c2 * v) / (1.0 + c3 * v), m2);
}
float pqNormalizedSignal(float L, float Lw, float Lb) {
return (pqNitsToSignal(L) - pqNitsToSignal(Lb)) /
(pqNitsToSignal(Lw) - pqNitsToSignal(Lb));
}
float hlgOetfInv(float x) {
const float a = 0.17883277;
const float b = 1.0 - 4.0*a;
const float c = 0.5 - a * log(4.0 * a);
if (x <= 0.5) {
return pow(x, 2.0) / 3.0;
} else {
return (exp((x - c) / a) + b) / 12.0;
}
}
float hlgOetf(float x) {
const float a = 0.17883277;
const float b = 1.0 - 4.0*a;
const float c = 0.5 - a * log(4.0 * a);
float E = 12.0 * x;
if (E < 1.0) {
return 0.5 * sqrt(E);
}
return a * log(E - b) + c;
}
vec3 hlgOetf(vec3 c) {
return vec3(hlgOetf(c.r), hlgOetf(c.g), hlgOetf(c.b));
}
float toneMapGainReinhard(float L, float Lc, float Ld) {
if (Ld < Lc) {
float a = Ld / (Lc*Lc);
float b = 1.0 / Ld;
return (1.0 + a*L) / (1.0 + b*L);
}
return 1.0;
}
// Content is in [Lb, Lw]
// Display is [Lmin, Lmax]
float toneMapGainBT2408(float L, float Lb, float Lw, float Lmin, float Lmax) {
if (Lmax >= Lw) {
return 1.0;
}
if (L >= Lw - 0.001) {
return Lmax / L;
}
float Eprime = L;
float E1 = pqNormalizedSignal(Eprime, Lw, Lb);
float minLum = pqNormalizedSignal(Lmin, Lw, Lb);
float maxLum = pqNormalizedSignal(Lmax, Lw, Lb);
float KS = 1.5 * maxLum - 0.5;
float b = minLum;
float E2 = 0.0;
if (E1 <= KS) {
return 1.0;
E2 = E1;
} else {
float TB1 = (E1 - KS) / (1.0 - KS);
float TB2 = TB1 * TB1;
float TB3 = TB2 * TB1;
E2 = ( 2.0*TB3 - 3.0*TB2 + 1.0) * KS + \
( TB3 - 2.0*TB2 + TB1) * (1.0 - KS) + \
(-2.0*TB3 + 3.0*TB2 ) * maxLum;
}
float E3 = E2 + b*pow(max(1.0 - E2, 0.0), 4.0);
float E4 = E3 * (pqNitsToSignal(Lw) - pqNitsToSignal(Lb)) + pqNitsToSignal(Lb);
return pqSignalToNits(E4) / Eprime;
}
vec3 toneMapBT2446a(vec3 RGB) {
float L_HDR = clli_max_nits;
float L_SDR = ndwl_white_nits;
float rho_HDR = 1.0 + 32.0 * pow(L_HDR / 10000.0, 1.0/2.4);
float rho_SDR = 1.0 + 32.0 * pow(L_SDR / 10000.0, 1.0/2.4);
// A normalized (to L_HDR) full-range linear display-light HDR signal RGB.
RGB /= L_HDR;
RGB = clamp(RGB, 0.0, 1.0);
float Rprime = pow(RGB.r, 1.0/2.4);
float Gprime = pow(RGB.g, 1.0/2.4);
float Bprime = pow(RGB.b, 1.0/2.4);
float Yprime = 0.2627*Rprime + 0.6780*Gprime + 0.0593*Bprime;
float Yprime_p = log(1.0 + (rho_HDR - 1.0)*Yprime) / log(rho_HDR);
float Yprime_c = 0.0;
if (Yprime_p < 0.0) {
Yprime_c = 0.0;
} else if (Yprime_p < 0.7399) {
Yprime_c = 1.0770 * Yprime_p;
} else if (Yprime_p < 0.9909) {
Yprime_c = -1.1510 * pow(Yprime_p, 2.0) + 2.7811*Yprime_p - 0.6302;
} else if (Yprime_p <= 1.0) {
Yprime_c = 0.5 * Yprime_p + 0.5;
} else {
Yprime_c = 1.0;
}
float Yprime_SDR = (pow(rho_SDR, Yprime_c) - 1.0) / (rho_SDR - 1.0);
return (pow(Yprime_SDR, 2.4) / Yprime) * RGB;
float f_Yprime_SDR = Yprime_SDR / (1.1 * Yprime);
float Cprime_b_TMO = f_Yprime_SDR * (Bprime - Yprime) / 1.8814;
float Cprime_r_TMO = f_Yprime_SDR * (Rprime - Yprime) / 1.4746;
float Yprime_TMO = Yprime_SDR - max(0.1 * Cprime_r_TMO, 0.0);
vec3 YCbCr_TMO = vec3(Yprime_TMO, Cprime_b_TMO, Cprime_r_TMO);
return rec2020_yuv_to_rgb(YCbCr_TMO);
}
float toneMapGainBT2446a(float Y_HDR, float L_HDR, float L_SDR) {
float rho_hdr = 1.0 + 32.0 * pow(L_HDR / 10000.0, 1.0/2.4);
float rho_sdr = 1.0 + 32.0 * pow(L_SDR / 10000.0, 1.0/2.4);
// Y_HDR is "A normalized full-range linear display-light HDR signal"
float Yprime = pow(Y_HDR / L_HDR, 1.0/2.4);
float Yp_prime = log(1.0 + (rho_hdr - 1.0) * Yprime) / log(rho_hdr);
float Yc_prime = 0.0;
if (Yp_prime < 0.0) {
Yc_prime = 0.0;
} else if (Yp_prime < 0.7399) {
Yc_prime = 1.0770 * Yp_prime;
} else if (Yp_prime < 0.9909) {
Yc_prime = -1.1510 * pow(Yp_prime, 2.0) + 2.7811*Yp_prime - 0.6302;
} else if (Yp_prime <= 1.0) {
Yc_prime = 0.5 * Yp_prime + 0.5;
} else {
Yc_prime = 1.0;
}
float Ysdr_prime = (pow(rho_sdr, Yc_prime) - 1.0) / (rho_sdr - 1.0);
// This is not explicitly in the formulation, but I think was intended.
float Ysdr = pow(Ysdr_prime, 2.4);
return (Ysdr * L_SDR) / Y_HDR;
}
float toneMapGainBT2446c(float Y_hdr) {
float k1 = 0.83802;
float k2 = 15.09968;
float k3 = 0.74204;
float k4 = 78.99439;
float HDR_ip = 58.5 / k1;
float Y_sdr = 0.0;
if (Y_hdr < HDR_ip) {
Y_sdr = k1 * Y_hdr;
} else {
Y_sdr = k2 * log(Y_hdr / HDR_ip - k3) + k4;
}
const float kSdrMax = 120.0;
if (Y_sdr > kSdrMax) {
Y_sdr = kSdrMax;
}
Y_sdr *= ndwl_white_nits / kSdrMax;
return Y_sdr / Y_hdr;
}
// Intersect the line segment between a and b with the unit cube.
// Assumes that b is always inside the unit cube.
vec3 intersectWithUnitCube(vec3 a, vec3 b) {
float alpha = 1.0;
for (int i = 0; i < 3; ++i) {
if (a[i] > 1.0) {
float alpha_i = (1.0 - b[i]) / (a[i] - b[i]);
alpha = min(alpha, alpha_i);
} else if (a[i] < 0.0) {
float alpha_i = (0.0 - b[i]) / (a[i] - b[i]);
alpha = min(alpha, alpha_i);
}
}
alpha = max(alpha, 0.0);
return alpha * a + (1.0 - alpha) * b;
}
vec3 hlgRefOotf(vec3 c) {
float Y = rec2020_rgb_to_yuv(c).r;
c *= pow(Y, 0.2);
c *= 1000.0;
return c;
}
vec3 hlgRefOotfInv(vec3 c) {
c /= 1000.0;
c = clamp(c, 0.0, 1.0);
c *= pow(rec2020_rgb_to_yuv(c).r, -0.2 / 1.2);
return c;
}
// Find alpha in [0, 1] that minimizes |c0 - (alpha*c1 + (1-alpha)*c2)|
vec3 nearestPointOnSegment(vec3 c0, vec3 c1, vec3 c2) {
// let u = c2 - c1
// let v = c0 - c1
// minimize dist2(c0, c1 + alpha*(c2 - c1)) =
// dist2(c0, c1 + alpha*u) =
// dot(alpha*u - v, alpha*u - v) =
// alpha^2 * dot(u, u) - alpha*2*dot(u, v) + dot(v, v)
// this is minimized at alpha = dot(u, v) / dot(u, u)
vec3 u = c2 - c1;
vec3 v = c0 - c1;
float a = dot(u, u);
float b = 2.0 * dot(u, v);
if (a <= 0.0) {
return c0;
}
float alpha = clamp(dot(u, v) / a, 0.0, 1.0);
return (1.0 - alpha) * c1 + alpha * c2;
}
vec3 convert(vec3 c, int from, int to) {
if (from == to) {
return c;
}
// Convert to XYZD50
if (from == kGamutSRGB) {
c = srgb_to_xyzd50 * c;
}
else if (from == kGamutP3) {
c = p3_to_xyzd50 * c;
}
else if (from == kGamutRec2020) {
c = rec2020_to_xyzd50 * c;
}
else if (from == kFakeGamutOkLab) {
c = oklab_to_xyzd50(c);
}
// Convert from from XYZD50
if (to == kGamutSRGB) {
c = xyzd50_to_srgb * c;
}
else if (to == kGamutP3) {
c = xyzd50_to_p3 * c;
}
else if (to == kGamutRec2020) {
c = xyzd50_to_rec2020 * c;
}
else if (to == kFakeGamutOkLab) {
c = xyzd50_to_oklab(c);
}
return c;
}
bool inCube(vec3 c, float H, float epsilon) {
return c.r >= -epsilon && c.r <= H + epsilon &&
c.g >= -epsilon && c.g <= H + epsilon &&
c.b >= -epsilon && c.b <= H + epsilon;
}
bool inUnitCube(vec3 c, float epsilon) {
return inCube(c, 1.0, epsilon);
}
vec3 gamutMapOkLabConstantH(vec3 c, int gamut) {
if (inUnitCube(c, 0.0)) {
return c;
}
vec3 Lab = convert(c, gamut, kFakeGamutOkLab);
vec3 Loo = vec3(min(Lab.x, 1.0), 0.0, 0.0);
float alpha0 = 0.0;
float alpha1 = 1.0;
for (int i = 0; i < 8; ++i) {
float alpha_test = 0.5 * (alpha0 + alpha1);
vec3 Lab_test = alpha_test * Lab + (1.0 - alpha_test) * Loo;
vec3 c_test = convert(Lab_test, kFakeGamutOkLab, gamut);
if (inUnitCube(c_test, 0.0)) {
alpha0 = alpha_test;
} else {
alpha1 = alpha_test;
}
}
float alpha_final = 0.5 * (alpha0 + alpha1);
vec3 Lab_final = alpha_final * Lab + (1.0 - alpha_final) * Loo;
return convert(Lab_final, kFakeGamutOkLab, gamut);
}
// Gamut map to |gamut| using |mode|. The input color |c| must be rec2020.
vec3 gamutMap(vec3 c, int mode, int gamut) {
if (mode == kGamutMapNone) {
return c;
}
// Convert to the space for gamut mapping.
c = convert(c, kGamutRec2020, gamut);
if (mode == kGamutMapClamp) {
c = clamp(c, 0.0, exp2(target_headroom_log2));
}
if (mode == kGamutMapMark) {
if (!inCube(c, exp2(target_headroom_log2), 1.0 / 255.0)) {
c = vec3(1.0, 0.0, 0.0);
}
}
if (mode == kGamutMapScaleUV) {
float Y = rec2020_rgb_to_yuv(c).r;
c = intersectWithUnitCube(c, vec3(Y, Y, Y));
c = clamp(c, 0.0, 1.0);
}
if (mode == kGamutMapScaleRGB) {
c = intersectWithUnitCube(c, vec3(0.0, 0.0, 0.0));
c = clamp(c, 0.0, 1.0);
}
if (mode == kGamutMapL2RGB) {
// This is finding the nearest L2 point in RGB space that is in the
// plane defined by black, white, and the color c itself.
float M = max(max(c.r, c.g), c.b);
if (M > 1.0) {
vec3 u = vec3(1.0, 1.0, 1.0);
vec3 v = u - (1.0/M) * c;
vec3 w = (1.0 - 1.0/M) * c;
float alpha = dot(v, w) / dot(v, v);
c = ((1.0 - alpha)/M) * c + (alpha)*u;
}
c = clamp(c, 0.0, 1.0);
}
if (mode == kGamutMapOkLabConstantH) {
c = gamutMapOkLabConstantH(c, gamut);
}
// Convert back to Rec2020.
c = convert(c, gamut, kGamutRec2020);
return c;
}
vec3 readTextureAsRec2100Nits() {
vec3 c = texture2D(texture, texcoord).rgb;
// Convert to linear.
if (signal_transfer == kTransferSRGB) {
c = srgbToLinear(c);
c = ndwl_white_nits * c;
}
if (signal_transfer == kTransferG22_AsHLG) {
c = pow(c, vec3(2.2));
}
if (signal_transfer == kTransferG22) {
c = pow(c, vec3(2.2));
c = ndwl_white_nits * c;
}
if (signal_transfer == kTransferPQ) {
c = vec3(pqSignalToNits(c.r), pqSignalToNits(c.g), pqSignalToNits(c.b));
}
if (signal_transfer == kTransferHLG) {
c = vec3(hlgOetfInv(c.r), hlgOetfInv(c.g), hlgOetfInv(c.b));
}
if (signal_transfer == kTransferPQ_G) {
c = vec3(pqSignalToNits(c.r), pqSignalToNits(c.g), pqSignalToNits(c.b));
float Y = rec2020_rgb_to_yuv(c).r / ndwl_white_nits;
c *= pow(Y, 1.0/1.2 - 1.0);
}
// Convert from image primaries to rec2020 primaries.
if (signal_primaries == 0) {
c = xyzd50_to_rec2020 * srgb_to_xyzd50 * c;
}
if (signal_primaries == 1) {
c = xyzd50_to_rec2020 * p3_to_xyzd50 * c;
}
if (signal_transfer == kTransferG22_AsHLG) {
c = hlgOetf(c);
c = vec3(hlgOetfInv(c.r), hlgOetfInv(c.g), hlgOetfInv(c.b));
}
// Apply the HLG OOTF (and scale up) in rec2020.
if (signal_transfer == kTransferHLG ||
signal_transfer == kTransferG22_AsHLG) {
c = hlgRefOotf(c);
}
return c;
}
float toneMapGain(float x) {
if (tonemap_mode == kToneMapReinhard) {
if (adaptation_mode == kAdaptationModeGainmap) {
return toneMapGainReinhard(x, clli_max_nits, ndwl_white_nits);
}
if (adaptation_mode == kAdaptationModeSpecific) {
return toneMapGainReinhard(x, clli_max_nits, ndwl_white_nits * exp2(target_headroom_log2));
}
} else if (tonemap_mode == kToneMapBT2408) {
// ITU-R BT.2408.
if (adaptation_mode == kAdaptationModeGainmap) {
return toneMapGainBT2408(x, 0.0, clli_max_nits, 0.0, ndwl_white_nits);
}
if (adaptation_mode == kAdaptationModeSpecific) {
float target_nits = ndwl_white_nits * exp2(target_headroom_log2);
if (target_nits > clli_max_nits) {
return 1.0;
}
return toneMapGainBT2408(x, 0.0, clli_max_nits, 0.0, target_nits);
}
} else if (tonemap_mode == kToneMapBT2446A) {
// ITU-R BT.2446 Method A.
return toneMapGainBT2446a(x, clli_max_nits, ndwl_white_nits);
} else if (tonemap_mode == kToneMapBT2446C) {
// ITU-R BT.2446 Method C.
return toneMapGainBT2446c(x);
}
return 0.0;
}
vec3 computeGain(vec3 tonemap_input) {
return vec3(toneMapGain(tonemap_input.r),
toneMapGain(tonemap_input.g),
toneMapGain(tonemap_input.b));
}
vec3 toneMapRec2100NitsToRec2020Linear(vec3 c) {
// Clamp to the indicated maximum
c = min(c, clli_max_nits);
if (tonemap_mode == kToneMapNone) {
c /= ndwl_white_nits;
float Y = rec2020_rgb_to_yuv(c).r;
c *= pow(Y, raise_gamma - 1.0);
}
else if (tonemap_mode == kToneMapClamp) {
c /= ndwl_white_nits;
c = max(c, vec3(0.0, 0.0, 0.0));
c = min(c, vec3(1.0, 1.0, 1.0));
}
else if (tonemap_mode == kToneMapBT2100HLG) {
// If the mode is ITU-R BT.2100, just apply the custom OOTF gamma.
// Convert back to linear [0, 1] before the OOTF.
c = hlgRefOotfInv(c);
// Apply the specified gamma.
if (hlg_ootf_per_channel == 1) {
c *= pow(c, vec3(hlg_ootf_gamma - 1.0));
} else {
float Y = rec2020_rgb_to_yuv(c).r;
c *= pow(Y, hlg_ootf_gamma - 1.0);
}
c *= hlg_scale;
}
else if (tonemap_mode == kToneMapHLGAs22) {
vec3 c_rec2100hlg = hlgOetf(hlgRefOotfInv(c));
c = pow(c_rec2100hlg, vec3(2.2, 2.2, 2.2));
}
else if (tonemap_mode == kToneMapBT2446A_redo) {
c = toneMapBT2446a(c);
}
else if (tonemap_mode == kToneMapReinhard ||
tonemap_mode == kToneMapBT2408 ||
tonemap_mode == kToneMapBT2446A ||
tonemap_mode == kToneMapBT2446C) {
// For curve-based tonemap functions, compute the per-channel inputs.
vec3 tonemap_input_maxRGB = vec3(max(max(c.r, c.g), c.b));
vec3 tonemap_input_RpGpBp = c;
vec3 tonemap_input_YRGB = vec3(rec2020_rgb_to_yuv(c).r);
vec3 gain;
if (tonemap_weight_before == 1) {
gain = computeGain(tonemap_weight_maxRGB * tonemap_input_maxRGB +
tonemap_weight_RpGpBp * tonemap_input_RpGpBp +
tonemap_weight_YRGB * tonemap_input_YRGB);
} else {
gain = tonemap_weight_maxRGB * computeGain(tonemap_input_maxRGB) +
tonemap_weight_RpGpBp * computeGain(tonemap_input_RpGpBp) +
tonemap_weight_YRGB * computeGain(tonemap_input_YRGB);
}
c *= gain;
c /= ndwl_white_nits;
} else {
c *= 0.0;
}
// Perform gamut mapping in Rec2020.
c = gamutMap(c, gamut_map_mode, kGamutRec2020);
// Perform SDR gamut mapping.
c = gamutMap(c, final_gamut_mode, final_gamut);
return c;
}
void main() {
// Treat everything as opaque. Just write RGB from here on.
gl_FragColor.a = 1.0;
vec3 c = readTextureAsRec2100Nits();
// Clamp to the indicated maximum
c = min(c, clli_max_nits);
if (tonemap_mode == kToneMapNativePQ) {
// For Native PQ, just write the value and return.
vec3 c_rec2100pq = vec3(pqNitsToSignal(c.r), pqNitsToSignal(c.g), pqNitsToSignal(c.b));
gl_FragColor.rgb = c_rec2100pq;
return;
}
if (tonemap_mode == kToneMapNativeHLG) {
// For Native HLG, just write the value and return.
vec3 c_rec2100hlg = hlgOetf(hlgRefOotfInv(c));
gl_FragColor.rgb = c_rec2100hlg;
return;
}
vec3 c_linear = c / ndwl_white_nits;
vec3 c_sdr = toneMapRec2100NitsToRec2020Linear(c);
if (adaptation_mode == kAdaptationModeSpecific) {
c = c_sdr;
}
if (adaptation_mode == kAdaptationModeGainmap) {
if (target_headroom_log2 != 0.0) {
float hdr_headroom = log2(clli_max_nits / ndwl_white_nits);
float w = clamp(target_headroom_log2 / hdr_headroom, 0.0, 1.0);
vec3 eps = vec3(0.0001);
vec3 g = log2((c_linear + eps) / (c_sdr + eps));
c = c_sdr * exp2(g * w);
} else {
c = c_sdr;
}
}
// Convert from rec2020 to P3.
c = xyzd50_to_p3 * rec2020_to_xyzd50 * c;
// Convert to sRGB-encoded.
c = linearToSrgb(c);
gl_FragColor.rgb = c;
}`;
let compileShader = function(gl, vertex_source, fragment_source) {
let vertex_shader = gl.createShader(gl.VERTEX_SHADER);
gl.shaderSource(vertex_shader, vertex_source);
gl.compileShader(vertex_shader);
if (!gl.getShaderParameter(vertex_shader, gl.COMPILE_STATUS))
throw new Error(gl.getShaderInfoLog(vertex_shader));
let fragment_shader = gl.createShader(gl.FRAGMENT_SHADER);
gl.shaderSource(fragment_shader, fragment_source);
gl.compileShader(fragment_shader);
if (!gl.getShaderParameter(fragment_shader, gl.COMPILE_STATUS))
throw new Error(gl.getShaderInfoLog(fragment_shader));
let program = gl.createProgram();
gl.attachShader(program, vertex_shader);
gl.attachShader(program, fragment_shader);
gl.linkProgram(program);
if (!gl.getProgramParameter(program, gl.LINK_STATUS))
throw new Error(gl.getProgramInfoLog(program));
return program;
}
let createTexture = function(image, w = 0, h = 0) {
texture = gl.createTexture();
if (w == 0) {
texture_w = image.width;
texture_h = image.height;
} else {
texture_w = w;
texture_h = h;
}
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.pixelStorei(gl.UNPACK_COLORSPACE_CONVERSION_WEBGL, gl.NONE);
// gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA16F, texture_w, texture_h, 0, gl.RGBA, gl.FLOAT, image);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA16F, texture_w, texture_h, 0, gl.RGBA, gl.FLOAT, null);
gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, texture_w, texture_h, gl.RGBA, gl.FLOAT, image);
gl.generateMipmap(gl.TEXTURE_2D);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
gl.bindTexture(gl.TEXTURE_2D, null);
var fb = gl.createFramebuffer();
gl.bindFramebuffer(gl.FRAMEBUFFER, fb);
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, texture, 0);
if (gl.checkFramebufferStatus(gl.FRAMEBUFFER) == gl.FRAMEBUFFER_COMPLETE) {
texture_pixels = new Float32Array(texture_w * texture_h * 4);
gl.readPixels(0, 0, texture_w, texture_h, gl.RGBA, gl.FLOAT, texture_pixels);
}
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
gl.deleteFramebuffer(fb);
}
let computeImageMaxRGB = function() {
let image_max_rgb = 0;
let image_max_Y = 0;
for (let x = 0; x < texture_w; ++x) {
for (let y = 0; y < texture_h; ++y) {
const offset = 4*(x + y*texture_w);
image_max_rgb = Math.max(image_max_rgb, texture_pixels[offset+0]);
image_max_rgb = Math.max(image_max_rgb, texture_pixels[offset+1]);
image_max_rgb = Math.max(image_max_rgb, texture_pixels[offset+2]);
let Y = 0.2627 * texture_pixels[offset+0] +
0.6780 * texture_pixels[offset+1] +
0.0593 * texture_pixels[offset+2];
image_max_Y = Math.max(image_max_Y, Y);
}
}
const image_max_nits_Y = pqSignalToNits(image_max_Y).toFixed();
const image_max_nits_rgb = pqSignalToNits(image_max_rgb).toFixed();
ImageMax.innerHTML =
'Max RGB: ' + image_max_rgb.toFixed(2) + ' (' + image_max_nits_rgb + ' nits)<br>' +
'Max Y: ' + image_max_Y.toFixed(2) + ' (' + image_max_nits_Y + ' nits)';
if (SignalTransfer.value == 'pq') {
CLLIMaxNits.value = image_max_nits_Y;
}
if (SignalTransfer.value == 'hlg') {
CLLIMaxNits.value = 1000;
}
if (SignalTransfer.value == 'srgb') {
CLLIMaxNits.value = 203;
}
}
let draw = function() {
gl.useProgram(program);
// Draw using that program.
let vertices = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, vertices);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([-1,-1, 1,-1, 1,1, -1,1]), gl.STATIC_DRAW);
let indices = gl.createBuffer();
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indices);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array([0,1,2, 0,2,3]), gl.STATIC_DRAW);
let positionLocation = gl.getAttribLocation(program, 'position');
gl.vertexAttribPointer(positionLocation, 2, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(positionLocation);
{
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.uniform1i(gl.getUniformLocation(program, 'texture'), 0);
}
gl.uniform1f(gl.getUniformLocation(program, 'ndwl_white_nits'), NDWLWhiteNits.value);
gl.uniform1f(gl.getUniformLocation(program, 'clli_max_nits'), CLLIMaxNits.value);
gl.uniform1f(gl.getUniformLocation(program, 'target_headroom_log2'), TargetHeadroomSlider.value);
TargetHeadroomSliderLabel.innerText = (TargetHeadroomSlider.value * 1.0).toFixed(2);
TargetHeadroomSliderLinear.innerText = exp2(TargetHeadroomSlider.value).toFixed(2);
CLLILinear.innerText = (CLLIMaxNits.value / NDWLWhiteNits.value).toFixed(2);
CLLILog2.innerText = log2(CLLIMaxNits.value / NDWLWhiteNits.value).toFixed(2);
gl.uniform1f(gl.getUniformLocation(program, 'hlg_ootf_gamma'), hlg_ootf_gamma);
gl.uniform1f(gl.getUniformLocation(program, 'hlg_scale'), HLG_Scale.value);
gl.uniform1i(gl.getUniformLocation(program, 'hlg_ootf_per_channel'), HLG_PerChannel.checked);
gl.uniform1f(gl.getUniformLocation(program, 'raise_gamma'), RaiseGamma.value);
gl.uniform1i(gl.getUniformLocation(program, 'tonemap_weight_before'), ToneMapCurveBefore.checked);
gl.uniform1f(gl.getUniformLocation(program, 'tonemap_weight_maxRGB'), ToneMapCurveMaxRGB.value);
gl.uniform1f(gl.getUniformLocation(program, 'tonemap_weight_RpGpBp'), ToneMapCurveRpGpBp.value);
gl.uniform1f(gl.getUniformLocation(program, 'tonemap_weight_YRGB'), ToneMapCurveYRGB.value);
let desc = '';
let use_tonemap_curve = false;
switch(ToneMap.value) {
case 'None':
gl.uniform1i(gl.getUniformLocation(program, 'tonemap_mode'), kToneMapNone);
break;
case 'NativePQ':
desc == 'native-pq';
gl.uniform1i(gl.getUniformLocation(program, 'tonemap_mode'), kToneMapNativePQ);
break;
case 'NativeHLG':
desc == 'native-hlg';
gl.uniform1i(gl.getUniformLocation(program, 'tonemap_mode'), kToneMapNativeHLG);
break;
case 'Clamp':
desc = 'clamp';
gl.uniform1i(gl.getUniformLocation(program, 'tonemap_mode'), kToneMapClamp);
break;
case 'Reinhard':