-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExposure_Zones.dctl
684 lines (609 loc) · 21.1 KB
/
Exposure_Zones.dctl
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
/*
Exposure Zones
Created by Greyson Sawyer
This DCTL creates a visualization of exposure zones based on the stops from middle gray.
Based on Ansel Adams' Zone System and the El Zone System created by Ed Lachman.
BASED ON:
Ansel Adams' and Fred Archer's Zone System: https://en.wikipedia.org/wiki/Zone_System
Ed Lachman's El Zone System: https://www.elzonesystem.com/
PROCESS OVERVIEW:
1. Convert input gamma to linear light.
2. Handle display-referred sources by applying inverse OOTF and tone mapping.
3. Calculate luminance.
4. Compute the stops from middle gray (0.18).
5. Quantize the stop.
6. Clamp stop between -7.0 and +7.0.
7. Map the stop to specific colors.
*/
// ===============================
// SECTION 1: 🛠️ UI Parameters
// ===============================
// Input Gamma
DEFINE_UI_PARAMS(input_gamma, Input Gamma, DCTLUI_COMBO_BOX, 9, { ig0, ig1, ig2, ig3, ig4, ig5, ig6, ig7, ig8, ig9, ig10, ig11, ig12, ig13, ig14 }, { ACEScc, ACEScct, Apple Log, ARRI LogC3, ARRI LogC4, DaVinci Intermediate, Fuji F-Log, Fuji F-Log2, Gamma 2.2, Gamma 2.4, Gamma 2.6, Linear, Rec.709, RED Log3G10, Sony S-Log3 })
// Chart
DEFINE_UI_PARAMS(show_chart, Show Chart, DCTLUI_CHECK_BOX, 1)
DEFINE_UI_PARAMS(chart_size, Chart Width, DCTLUI_SLIDER_FLOAT, 10.0, 0.0, 100.0, 0.2)
// Handle display-reffered sources
DEFINE_UI_PARAMS(is_display_referred, Display-Referred Source, DCTLUI_CHECK_BOX, 1)
// ===============================
// SECTION 2: 📌 Constants & Utility
// ===============================
__CONSTANT__ float LUMINANCE_EPSILON = 0.000001f;
__CONSTANT__ float MIDDLE_GRAY = 0.18f;
__CONSTANT__ float3 LUMINANCE_COEFFICIENTS = make_float3(0.2126f, 0.7152f, 0.0722f);
// Enumeration for gamma types
// Necessary for gamma encoding and decoding functions.
typedef enum {
ACEScc,
ACEScct,
Apple_Log,
ARRI_LogC3,
ARRI_LogC4,
DaVinci_Intermediate,
Fuji_F_Log,
Fuji_F_Log2,
Gamma_2_2,
Gamma_2_4,
Gamma_2_6,
Linear,
Rec709,
RED_Log3G10,
Sony_S_Log3
} GammaType;
// Custom powf function to handle negative base values.
__DEVICE__ float powf(float base, float exp) {
return _copysignf(_powf(_fabs(base), exp), base);
}
// ===============================
// SECTION 3: 🌘 Gamma Conversion
// ===============================
__DEVICE__ float gamma_to_linear(float in, GammaType gamma_type) {
switch (gamma_type) {
case ACEScc:
{
// ACEScc to Linear
const float a = 17.52f;
const float b = 9.72f;
const float cut1 = -b / a;
const float cut2 = 1.468f;
if (in <= cut1) {
return (powf(2.0f, (in * a - b)) - powf(2.0f, -16.0f)) * 0.5f;
} else if (in < cut2) {
return powf(2.0f, in * a - b);
} else {
return 65504.0f;
}
}
case ACEScct:
{
// ACEScct to Linear
const float a = 17.52f;
const float b = 9.72f;
const float m = 10.5402377416545f;
const float k0 = 0.0729055341958355f;
const float k1 = 0.155251141552511f;
const float cut = 1.468f;
if (in <= k1) {
return (in - k0) / m;
} else if (in < cut) {
return powf(2.0f, in * a - b);
} else {
return 65504.0f;
}
}
case ARRI_LogC3:
{
// ARRI LogC3 to Linear
const float a = 5.555556f;
const float b = 0.052272f;
const float c = 0.247190f;
const float d = 0.385537f;
const float e = 5.367655f;
const float f = 0.092809f;
const float cut = (e * 0.010591f) + f;
if (in > cut) {
return (powf(10.0f, (in - d) / c) - b) / a;
} else {
return (in - f) / e;
}
}
case ARRI_LogC4:
{
// ARRI LogC4 to Linear
const float a = 2231.91177f;
const float b = 0.9070295f;
const float c = 0.0929705f;
const float s = 0.11343535f;
const float t = -0.01807264f;
if (in < 0.0f) {
return in * s + t;
} else {
float p = 14.0f * (in - c) / b + 6.0f;
return (powf(2.0f, p) - 64.0f) / a;
}
}
case Apple_Log:
{
// Apple Log to Linear
const float R0 = -0.05641088f;
const float Rt = 0.01f;
const float c = 47.28711236f;
const float b = 0.00964052f;
const float y = 0.08550479f;
const float d = 0.69336945f;
const float Pt = c * powf(Rt - R0, 2.0f);
if (in >= Pt) {
return _exp2f((in - d) / y) - b;
} else if (in > 0.0f) {
return _sqrtf(in / c) + R0;
} else {
return R0;
}
}
case DaVinci_Intermediate:
{
// DaVinci Intermediate to Linear
const float A = 0.0075f;
const float B = 7.0f;
const float C = 0.07329248f;
const float M = 10.44426855f;
const float log_cut = 0.02740668f;
if (in > log_cut) {
return powf(2.0f, (in / C) - B) - A;
} else {
return in / M;
}
}
case Fuji_F_Log:
{
// Fuji F-Log to Linear
const float a = 0.555556f;
const float b = 0.009468f;
const float c = 0.344676f;
const float d = 0.790453f;
const float e = 8.735631f;
const float f = 0.092864f;
const float cut = 0.100537775223865f;
if (in >= cut) {
return (powf(10.0f, (in - d) / c) - b) / a;
} else {
return (in - f) / e;
}
}
case Fuji_F_Log2:
{
// Fuji F-Log2 to Linear
const float a = 5.555556f;
const float b = 0.064829f;
const float c = 0.245281f;
const float d = 0.384316f;
const float e = 8.799461f;
const float f = 0.092864f;
const float cut = 0.100686685370811f;
if (in >= cut) {
return (powf(10.0f, (in - d) / c) - b) / a;
} else {
return (in - f) / e;
}
}
case RED_Log3G10:
{
// RED Log3G10 to Linear
const float a = 0.224282f;
const float b = 155.975327f;
const float c = 0.01f;
const float g = 15.1927f;
if (in < 0.0f) {
return (in / g) - c;
} else {
return (powf(10.0f, in / a) - 1.0f) / b - c;
}
}
case Sony_S_Log3:
{
// Sony S-Log3 to Linear
const float cut1 = 171.2102947f / 1023.0f;
const float a = 10.0f;
const float b = 0.19f;
const float c = -0.01f;
const float d = 0.01125f;
const float e = 95.0f;
const float f = 171.2102947f;
const float denom = f - e;
if (in >= cut1) {
float num = in * 1023.0f - 420.0f;
float exponent = num / 261.5f;
return powf(a, exponent) * b + c;
} else {
float num = in * 1023.0f - e;
return num * d / denom;
}
}
case Rec709:
{
// Rec.709 to Linear
if (in < 0.081f) {
return in / 4.5f;
} else {
return powf((in + 0.099f) / 1.099f, 1.0f / 0.45f);
}
}
case Gamma_2_2:
{
// Gamma 2.2 to Linear
return powf(in, 2.2f);
}
case Gamma_2_4:
{
// Gamma 2.4 to Linear
return powf(in, 2.4f);
}
case Gamma_2_6:
{
// Gamma 2.6 to Linear
return powf(in, 2.6f);
}
case Linear:
default:
{
// Linear (no conversion)
return in;
}
}
}
__DEVICE__ float linear_to_gamma(float in, GammaType gamma_type) {
switch (gamma_type) {
case ACEScc:
{
// Linear to ACEScc
const float a = 17.52f;
const float b = 9.72f;
const float c = powf(2.0f, -16.0f);
const float cut = c * 0.5f;
if (in <= 0.0f) {
return (-16.0f + b) / a;
} else if (in < cut) {
return (_log2f(c + in * 0.5f) + b) / a;
} else {
return (_log2f(in) + b) / a;
}
}
case ACEScct:
{
// Linear to ACEScct
const float a = 17.52f;
const float b = 9.72f;
const float m = 10.5402377416545f;
const float k0 = 0.0729055341958355f;
const float c1 = 0.0078125f;
if (in <= c1) {
return m * in + k0;
} else {
return (_log2f(in) + b) / a;
}
}
case ARRI_LogC3:
{
// Linear to ARRI LogC3
const float a = 5.555556f;
const float b = 0.052272f;
const float c = 0.247190f;
const float d = 0.385537f;
const float e = 5.367655f;
const float f = 0.092809f;
const float cut = 0.010591f;
if (in > cut) {
return c * _log10f(a * in + b) + d;
} else {
return e * in + f;
}
}
case ARRI_LogC4:
{
// Linear to ARRI LogC4
const float a = 2231.91177f;
const float b = 0.9070295f;
const float c = 0.0929705f;
const float s = 0.11343535f;
const float t = -0.01807264f;
if (in < t) {
return (in - t) / s;
} else {
return ((_log2f(a * in + 64.0f) - 6.0f) / 14.0f) * b + c;
}
}
case Apple_Log:
{
// Linear to Apple Log
const float R0 = -0.05641088f;
const float Rt = 0.01f;
const float c = 47.28711236f;
const float b = 0.00964052f;
const float y = 0.08550479f;
const float d = 0.69336945f;
if (in >= Rt) {
return y * _log2f(in + b) + d;
} else if (in > R0) {
return c * powf(in - R0, 2.0f);
} else {
return 0.0f;
}
}
case DaVinci_Intermediate:
{
// Linear to DaVinci Intermediate
const float A = 0.0075f;
const float B = 7.0f;
const float C = 0.07329248f;
const float M = 10.44426855f;
const float lin_cut = 0.00262409f;
if (in > lin_cut) {
return (_log2f(in + A) + B) * C;
} else {
return in * M;
}
}
case Fuji_F_Log:
{
// Linear to Fuji F-Log
const float a = 0.555556f;
const float b = 0.009468f;
const float c = 0.344676f;
const float d = 0.790453f;
const float e = 8.735631f;
const float f = 0.092864f;
const float cut = 0.00089f;
if (in >= cut) {
return c * _log10f(a * in + b) + d;
} else {
return e * in + f;
}
}
case Fuji_F_Log2:
{
// Linear to Fuji F-Log2
const float a = 5.555556f;
const float b = 0.064829f;
const float c = 0.245281f;
const float d = 0.384316f;
const float e = 8.799461f;
const float f = 0.092864f;
const float cut = 0.000889f;
if (in >= cut) {
return c * _log10f(a * in + b) + d;
} else {
return e * in + f;
}
}
case RED_Log3G10:
{
// Linear to RED Log3G10
const float a = 0.224282f;
const float b = 155.975327f;
const float c = 0.01f;
const float g = 15.1927f;
in = in + c;
if (in < 0.0f) {
return in * g;
} else {
return a * _log10f(in * b + 1.0f);
}
}
case Sony_S_Log3:
{
// Linear to Sony S-Log3
const float cut2 = 0.01125f;
const float a = 420.0f;
const float b = 261.5f;
const float c = 0.19f;
const float d = 95.0f;
const float e = 171.2102947f;
const float denom = 0.01125f;
const float num_factor = e - d;
if (in >= cut2) {
return (a + _log10f((in + 0.01f) / c) * b) / 1023.0f;
} else {
return (in * num_factor / denom + d) / 1023.0f;
}
}
case Rec709:
{
// Linear to Rec.709
if (in < 0.018f) {
return 4.5f * in;
} else {
return 1.099f * powf(in, 0.45f) - 0.099f;
}
}
case Gamma_2_2:
{
// Linear to Gamma 2.2
return powf(in, 1.0f / 2.2f);
}
case Gamma_2_4:
{
// Linear to Gamma 2.4
return powf(in, 1.0f / 2.4f);
}
case Gamma_2_6:
{
// Linear to Gamma 2.6
return powf(in, 1.0f / 2.6f);
}
case Linear:
default:
{
// Linear (no conversion)
return in;
}
}
}
// ===============================
// SECTION 4: 🎢 Tone Mapping
// Credit: This code was adapted from Thatcher Freeman's DCTL.
// https://github.com/thatcherfreeman/utility-dctls/blob/main/Utilities/DaVinci%20Tone%20Mapping.dctl
// It's similar to Resolve's tone mapping but not identical.
// ===============================
__DEVICE__ float rolloff_function(float x, float a, float b) {
return a * (x / (x + b));
}
__DEVICE__ float3 tone_mapping(float3 in, float max_input_nits, float max_output_nits) {
float input_white = max_input_nits / 100.0;
float output_white = max_output_nits / 100.0;
float adaptation = 9.0;
float b = (input_white - (adaptation / 100.0) * (input_white / output_white)) / ((input_white / output_white) - 1);
// Clamp the input to the input white point
in.x = _fminf(in.x, input_white);
in.y = _fminf(in.y, input_white);
in.z = _fminf(in.z, input_white);
// Constraint 1: f(W_in) = W_out
float a = output_white / (input_white / (input_white + b));
if (input_white != output_white) {
in.x = rolloff_function(in.x, a, b);
in.y = rolloff_function(in.y, a, b);
in.z = rolloff_function(in.z, a, b);
}
// Clamp to the output white point
in.x = _clampf(in.x, 0.0f, output_white);
in.y = _clampf(in.y, 0.0f, output_white);
in.z = _clampf(in.z, 0.0f, output_white);
return in;
}
// ===============================
// SECTION 5: 🧩 Applying OOTF
// ===============================
__DEVICE__ float3 apply_inverse_ootf(float3 in) {
// Step 1: Convert linear light to gamma 2.4
in.x = linear_to_gamma(in.x, Gamma_2_4);
in.y = linear_to_gamma(in.y, Gamma_2_4);
in.z = linear_to_gamma(in.z, Gamma_2_4);
// Step 2: Convert gamma 2.4 to linear light as if it were rec709 gamma curve
in.x = gamma_to_linear(in.x, Rec709);
in.y = gamma_to_linear(in.y, Rec709);
in.z = gamma_to_linear(in.z, Rec709);
return in;
}
// ===============================
// SECTION 6: 🤝 Helper Functions
// ===============================
// Helper function to convert gamma for a float3 color
float3 gamma_to_linear_float3(float3 color, GammaType gamma_type) {
color.x = gamma_to_linear(color.x, gamma_type);
color.y = gamma_to_linear(color.y, gamma_type);
color.z = gamma_to_linear(color.z, gamma_type);
return color;
}
// Helper function to process display-referred colors
// Applies inverse OOTF and tone mapping
float3 process_display_referred(float3 color) {
color = apply_inverse_ootf(color);
color = tone_mapping(color, 100, 10000);
return color;
}
// Helper function to map stop values to colors
float3 stop_to_color(float stop) {
if (stop >= 7.0f) {
return make_float3(1.0f, 1.0f, 1.0f); // White (#FFFFFF)
} else if (stop == 6.0f) {
return make_float3(0.9451f, 0.7490f, 0.7529f); // #F1BFC0
} else if (stop == 5.0f) {
return make_float3(0.8980f, 0.4941f, 0.5490f); // #E57E8C
} else if (stop == 4.0f) {
return make_float3(0.9373f, 0.1059f, 0.1490f); // #EF1B26
} else if (stop == 3.0f) {
return make_float3(0.9686f, 0.6667f, 0.2784f); // #F7AA47
} else if (stop == 2.0f) {
return make_float3(0.9569f, 0.4392f, 0.1647f); // #F4702A
} else if (stop == 1.0f) {
return make_float3(1.0f, 0.9725f, 0.6510f); // #FFF8A6
} else if (stop == 0.5f) {
return make_float3(0.9765f, 0.9137f, 0.0f); // #F9E900
} else if (stop == 0.0f) {
return make_float3(0.5020f, 0.5020f, 0.5020f); // #808080
} else if (stop == -0.5f) {
return make_float3(0.5804f, 0.7804f, 0.2588f); // #94C742
} else if (stop == -1.0f) {
return make_float3(0.3686f, 0.7333f, 0.2784f); // #5EBB47
} else if (stop == -2.0f) {
return make_float3(0.1059f, 0.6588f, 0.2941f); // #1BA84B
} else if (stop == -3.0f) {
return make_float3(0.1490f, 0.6863f, 0.8941f); // #26AFE4
} else if (stop == -4.0f) {
return make_float3(0.0902f, 0.4549f, 0.6549f); // #1774A7
} else if (stop == -5.0f) {
return make_float3(0.6235f, 0.4980f, 0.7216f); // #9F7FB8
} else if (stop == -6.0f) {
return make_float3(0.3843f, 0.2784f, 0.6078f); // #62479B
} else if (stop <= -7.0f) {
return make_float3(0.0275f, 0.0275f, 0.0275f); // #070707
} else {
return make_float3(0.5f, 0.5f, 0.5f); // Gray
}
}
// Helper function to map segment index to stop value
float segment_index_to_stop(int segment_index) {
if (segment_index >= 24 && segment_index <= 26) {
return 1.0f;
} else if (segment_index >= 27 && segment_index <= 28) {
return 0.5f;
} else if (segment_index >= 29 && segment_index <= 30) {
return 0.0f;
} else if (segment_index >= 31 && segment_index <= 32) {
return -0.5f;
} else if (segment_index >= 33 && segment_index <= 35) {
return -1.0f;
} else {
return 7.0f - _floorf(segment_index / 4.0f);
}
}
// Helper function to create the chart
__DEVICE__ float3 process_chart_pixel(int p_Width, int p_Height, int p_X, int p_Y) {
// Total number of stops (from -7 to +7)
const int total_stops = 15;
const int total_segments = total_stops * 4; // 60
// Compute segment index (0 to 59)
int segment_index = (int)((p_Y * total_segments) / p_Height);
// Clamp segment_index to valid range
segment_index = _clamp(segment_index, 0, total_segments - 1);
// Map segment index to stop value
float stop = segment_index_to_stop(segment_index);
// Map stop to color
float3 color = stop_to_color(stop);
// Return the color for the chart pixel
return color;
}
// ===============================
// SECTION 7: 🌵 Main Function
// ===============================
__DEVICE__ float3 transform(int p_Width, int p_Height, int p_X, int p_Y, float p_R, float p_G, float p_B)
{
float3 in = make_float3(p_R, p_G, p_B);
float3 color;
// Define chart width (10% of the image width)
float chart_width = p_Width * (chart_size / 100.0);
if (p_X >= chart_width || !show_chart) {
// Pixel is outside the chart area; process normally
// Step 1: Convert input gamma to linear light
in = gamma_to_linear_float3(in, (GammaType)input_gamma);
// Step 2: Handle display-referred sources by applying inverse OOTF and tone mapping.
if (is_display_referred) {
in = process_display_referred(in);
}
// Step 3: Calculate luminance
float luminance = dot(in, LUMINANCE_COEFFICIENTS);
// Avoid zero or negative luminance to prevent log errors
luminance = _fmaxf(luminance, LUMINANCE_EPSILON);
// Step 4: Compute the stops from middle gray (0.18)
float n = _log2f(luminance / MIDDLE_GRAY);
// Step 5: Quantize the stop
float stop = (n >= -1.0f && n <= 1.0f) ? (_round(n * 2.0f) / 2.0f) : _round(n);
// Step 6: Clamp stop between -7.0f and +7.0f
stop = _clampf(stop, -7.0f, 7.0f);
// Step 7: Map the stop to specific colors.
color = stop_to_color(stop);
return color;
} else {
float3 color = process_chart_pixel(p_Width, p_Height, p_X, p_Y);
return color;
}
}