ocio 0.2.0

A pure Rust port of OpenColorIO, the color management framework for visual effects and animation.
Documentation
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
//! Tonal grading op (port of `GradingTone.cpp`, `GradingToneOpData.cpp`,
//! `GradingToneOp.cpp`, `GradingToneOpCPU.cpp` and `GradingToneTransform.cpp`).

use super::grading_primary::{std_max, std_min, GradingValue};
use crate::config::Config;
use crate::context::Context;
use crate::dynamic_property::DynamicProperty;
use crate::error::Result;
use crate::format_metadata::FormatMetadata;
use crate::math_utils::clamp_f32;
use crate::ops::{Op, OpRc, OpVec, Pixel};
use crate::transforms::grading::{GradingRgbmsw, GradingTone};
use crate::transforms::{BuildOps, GradingToneTransform, Transform, Validate};
use crate::types::{DynamicPropertyType, GradingStyle, OptimizationFlags, TransformDirection};
use std::any::Any;
use std::fmt;
use std::sync::Arc;

/// Channel of a [`GradingRgbmsw`] (port of `RGBMChannel`).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RgbmChannel {
    /// Red.
    R = 0,
    /// Green.
    G = 1,
    /// Blue.
    B = 2,
    /// Master.
    M = 3,
}

const CHANNELS: [RgbmChannel; 4] = [
    RgbmChannel::R,
    RgbmChannel::G,
    RgbmChannel::B,
    RgbmChannel::M,
];

/// Value of a channel, as float (port of `GetChannelValue`).
pub fn channel_value(value: &GradingRgbmsw, channel: RgbmChannel) -> f32 {
    match channel {
        RgbmChannel::R => value.red as f32,
        RgbmChannel::G => value.green as f32,
        RgbmChannel::B => value.blue as f32,
        RgbmChannel::M => value.master as f32,
    }
}

// ---------------------------------------------------------------------------
// Precomputed values

/// Values precomputed from a [`GradingTone`] for rendering (port of
/// `GradingTonePreRender`).
#[derive(Debug, Clone, PartialEq)]
pub struct GradingTonePreRender {
    // Values used by CPU & GPU.
    /// Start of the shadows (kept above the pivot).
    pub shadows_start: f64,
    /// Pivot of the shadows (the `width` of the control).
    pub shadows_width: f64,
    /// Start of the highlights (kept below the pivot).
    pub highlights_start: f64,
    /// Pivot of the highlights (the `width` of the control).
    pub highlights_width: f64,
    /// Start of the blacks, after the shadows adjustment.
    pub blacks_start: f64,
    /// Width of the blacks, after the shadows adjustment.
    pub blacks_width: f64,
    /// Start of the whites, after the highlights adjustment.
    pub whites_start: f64,
    /// Width of the whites, after the highlights adjustment.
    pub whites_width: f64,

    // Arrays only used by CPU.
    /// Midtones spline knot x values, per channel (R, G, B, M).
    pub mid_x: [[f32; 6]; 4],
    /// Midtones spline knot y values, per channel.
    pub mid_y: [[f32; 6]; 4],
    /// Midtones spline knot slopes, per channel.
    pub mid_m: [[f32; 6]; 4],

    /// Highlights (`[0]`) and shadows (`[1]`) curve x values, per channel.
    pub hs_x: [[[f32; 3]; 4]; 2],
    /// Highlights and shadows curve y values, per channel.
    pub hs_y: [[[f32; 3]; 4]; 2],
    /// m1 not used, `hs_m[][][1]` is m2.
    pub hs_m: [[[f32; 2]; 4]; 2],

    /// Whites (`[0]`) and blacks (`[1]`) curve x values, per channel.
    pub wb_x: [[[f32; 2]; 4]; 2],
    /// Whites and blacks curve y values, per channel.
    pub wb_y: [[[f32; 2]; 4]; 2],
    /// Whites and blacks curve slopes, per channel.
    pub wb_m: [[[f32; 2]; 4]; 2],
    /// Whites and blacks gain (mean of the end slopes, slope increasing case),
    /// per channel.
    pub wb_gain: [[f32; 4]; 2],

    /// Top/bottom, 4 values.
    pub sc_x: [[f32; 4]; 2],
    /// S-contrast top/bottom curve y values.
    pub sc_y: [[f32; 4]; 2],
    /// m0 & m3.
    pub sc_m: [[f32; 2]; 2],

    // Values changing with the style.
    /// Top of the tonal range (depends on the style).
    pub top: f32,
    /// Top of the s-contrast curve (depends on the style).
    pub top_sc: f32,
    /// Bottom of the tonal range (depends on the style).
    pub bottom: f32,
    /// S-contrast pivot (depends on the style).
    pub pivot: f32,

    /// Do not apply the op if all params are identity.
    pub local_bypass: bool,

    style: GradingStyle,
}

impl GradingTonePreRender {
    /// Precomputation object for a style (values are not computed yet, see
    /// [`update`](Self::update)).
    pub fn new(style: GradingStyle) -> Self {
        let mut p = Self {
            shadows_start: 0.0,
            shadows_width: 0.0,
            highlights_start: 0.0,
            highlights_width: 0.0,
            blacks_start: 0.0,
            blacks_width: 0.0,
            whites_start: 0.0,
            whites_width: 0.0,
            mid_x: [[0.0; 6]; 4],
            mid_y: [[0.0; 6]; 4],
            mid_m: [[0.0; 6]; 4],
            hs_x: [[[0.0; 3]; 4]; 2],
            hs_y: [[[0.0; 3]; 4]; 2],
            hs_m: [[[0.0; 2]; 4]; 2],
            wb_x: [[[0.0; 2]; 4]; 2],
            wb_y: [[[0.0; 2]; 4]; 2],
            wb_m: [[[0.0; 2]; 4]; 2],
            wb_gain: [[0.0; 4]; 2],
            sc_x: [[0.0; 4]; 2],
            sc_y: [[0.0; 4]; 2],
            sc_m: [[0.0; 2]; 2],
            top: 1.0,
            top_sc: 1.0,
            bottom: 0.0,
            pivot: 0.4,
            local_bypass: false,
            style: GradingStyle::Log,
        };
        p.set_style(style);
        p
    }

    /// Precomputed values of `v` for `style`.
    pub fn from_value(style: GradingStyle, v: &GradingTone) -> Self {
        let mut p = Self::new(style);
        p.update(v);
        p
    }

    /// Style dependent values: (top, topSC, bottom, pivot).
    pub fn from_style(style: GradingStyle) -> (f32, f32, f32, f32) {
        match style {
            // Might like to move these for ACES, but cannot for ARRI K1S1.
            GradingStyle::Log => (1.0, 1.0, 0.0, 0.4),
            // Bottom is placed at breakpoint of lin-to-log.
            GradingStyle::Lin => (7.5, 6.5, -5.5, 0.0),
            // aces 0.18 --> 0.39.
            GradingStyle::Video => (1.0, 1.0, 0.0, 0.4),
        }
    }

    /// Change the style.
    pub fn set_style(&mut self, style: GradingStyle) {
        if self.style != style {
            self.style = style;
            let (top, top_sc, bottom, pivot) = Self::from_style(style);
            self.top = top;
            self.top_sc = top_sc;
            self.bottom = bottom;
            self.pivot = pivot;
        }
    }

    /// Recompute the values.
    pub fn update(&mut self, v: &GradingTone) {
        self.local_bypass = v.is_identity();
        if self.local_bypass {
            return;
        }

        {
            let master = v.highlights.master;
            let start = v.highlights.start;
            let pivot = v.highlights.width;
            let startw = v.whites.start;
            let widthw = v.whites.width;

            self.highlights_start = if start > pivot - 0.01 {
                pivot - 0.01
            } else {
                start
            };
            self.highlights_width = pivot;

            let new_start =
                highlight_fwd_eval(startw, self.highlights_start, self.highlights_width, master);
            let new_end = highlight_fwd_eval(
                startw + widthw,
                self.highlights_start,
                self.highlights_width,
                master,
            );
            self.whites_start = new_start;
            self.whites_width = new_end - new_start;
        }
        {
            let master = v.shadows.master;
            let start = v.shadows.start;
            let pivot = v.shadows.width;
            let startb = v.blacks.start;
            let widthb = v.blacks.width;

            self.shadows_start = if start < pivot + 0.01 {
                pivot + 0.01
            } else {
                start
            };
            self.shadows_width = pivot;

            let new_start = shadow_fwd_eval(startb, self.shadows_width, self.shadows_start, master);
            let new_end = shadow_fwd_eval(
                startb - widthb,
                self.shadows_width,
                self.shadows_start,
                master,
            );
            self.blacks_start = new_start;
            self.blacks_width = new_start - new_end;
        }

        let (top, bottom, top_sc, pivot) = (self.top, self.bottom, self.top_sc, self.pivot);
        self.mids_precompute(v, top, bottom);
        self.highlight_shadow_precompute(v);
        self.white_black_precompute(v);
        self.scontrast_precompute(v, top_sc, bottom, pivot);
    }

    fn mids_precompute(&mut self, v: &GradingTone, top: f32, bottom: f32) {
        const HALO: f32 = 0.4;

        for channel in CHANNELS {
            let c = channel as usize;
            let mut mid_adj = clamp_f32(channel_value(&v.midtones, channel), 0.01, 1.99);
            if mid_adj == 1.0 {
                continue;
            }
            let x = &mut self.mid_x[c];
            let y = &mut self.mid_y[c];
            let m = &mut self.mid_m[c];

            x[0] = bottom;
            x[5] = top;

            let max_width = (x[5] - x[0]) * 0.95;
            let width = clamp_f32(v.midtones.width as f32, 0.01, max_width);
            let min_cent = x[0] + width * 0.51;
            let max_cent = x[5] - width * 0.51;
            let center = clamp_f32(v.midtones.start as f32, min_cent, max_cent);

            x[1] = center - width * 0.5;
            x[4] = x[1] + width;

            x[2] = x[1] + (x[4] - x[1]) * 0.25;
            x[3] = x[1] + (x[4] - x[1]) * 0.75;
            y[0] = x[0];
            m[0] = 1.0;
            m[5] = 1.0;

            let min_slope = 0.1f32;

            mid_adj -= 1.0;
            mid_adj *= 1.0 - min_slope;

            m[2] = 1.0 + mid_adj;
            m[3] = 1.0 - mid_adj;
            m[1] = 1.0 + mid_adj * HALO;
            m[4] = 1.0 - mid_adj * HALO;

            let (x0, x1, x2, x3, x4, x5) = (x[0], x[1], x[2], x[3], x[4], x[5]);
            if center <= (x5 + x0) * 0.5 {
                let (m0, m1, m2, m3, m5) = (m[0], m[1], m[2], m[3], m[5]);
                let area = (x1 - x0) * (m1 - m0) * 0.5
                    + (x2 - x1) * ((m1 - m0) + (m2 - m1) * 0.5)
                    + (center - x2) * (m2 - m0) * 0.5;
                m[4] = (-0.5 * (x5 - x4) * m5
                    + (x4 - x3) * (0.5 * m3 - m5)
                    + (x3 - center) * (m3 - m5) * 0.5
                    + area)
                    / (-0.5 * (x5 - x3));
            } else {
                let (m0, m2, m3, m4, m5) = (m[0], m[2], m[3], m[4], m[5]);
                let area = (x5 - x4) * (m4 - m5) * 0.5
                    + (x4 - x3) * ((m4 - m5) + (m3 - m4) * 0.5)
                    + (x3 - center) * (m3 - m5) * 0.5;
                m[1] = (-0.5 * (x1 - x0) * m0
                    + (x2 - x1) * (0.5 * m2 - m0)
                    + (center - x2) * (m2 - m0) * 0.5
                    + area)
                    / (-0.5 * (x2 - x0));
            }

            y[1] = y[0] + (m[0] + m[1]) * (x1 - x0) * 0.5;
            y[2] = y[1] + (m[1] + m[2]) * (x2 - x1) * 0.5;
            y[3] = y[2] + (m[2] + m[3]) * (x3 - x2) * 0.5;
            y[4] = y[3] + (m[3] + m[4]) * (x4 - x3) * 0.5;
            y[5] = y[4] + (m[4] + m[5]) * (x5 - x4) * 0.5;
        }
    }

    fn highlight_shadow_precompute(&mut self, v: &GradingTone) {
        for is_shadow in [false, true] {
            let s = is_shadow as usize;
            for channel in CHANNELS {
                let c = channel as usize;
                let mut val = if is_shadow {
                    channel_value(&v.shadows, channel)
                } else {
                    channel_value(&v.highlights, channel)
                };
                if !is_shadow {
                    val = 2.0 - val;
                }
                if val == 1.0 {
                    continue;
                }
                let start = (if is_shadow {
                    self.shadows_start
                } else {
                    self.highlights_start
                }) as f32;
                let pivot = (if is_shadow {
                    self.shadows_width
                } else {
                    self.highlights_width
                }) as f32;

                let x0 = if is_shadow { pivot } else { start };
                let x2 = if is_shadow { start } else { pivot };
                let y0 = x0;
                let y2 = x2;
                let x1 = x0 + (x2 - x0) * 0.5;
                self.hs_x[s][c] = [x0, x1, x2];
                self.hs_y[s][c][0] = y0;
                self.hs_y[s][c][2] = y2;

                if val < 1.0 {
                    let m0 = if is_shadow { std_max(0.01, val) } else { 1.0 };
                    let m2 = if is_shadow { 1.0 } else { std_max(0.01, val) };
                    self.hs_m[s][c] = [m0, m2];
                    self.hs_y[s][c][1] = (0.5 / (x2 - x0))
                        * ((2.0 * y0 + m0 * (x1 - x0)) * (x2 - x1)
                            + (2.0 * y2 - m2 * (x2 - x1)) * (x1 - x0));
                } else if val > 1.0 {
                    let m0 = if is_shadow {
                        std_max(0.01, 2.0 - val)
                    } else {
                        1.0
                    };
                    let m2 = if is_shadow {
                        1.0
                    } else {
                        std_max(0.01, 2.0 - val)
                    };
                    self.hs_m[s][c] = [m0, m2];
                    self.hs_y[s][c][1] = (0.5 / ((x2 - x1) + (x1 - x0)))
                        * ((2.0 * y0 + m0 * (x1 - x0)) * (x2 - x1)
                            + (2.0 * y2 - m2 * (x2 - x1)) * (x1 - x0));
                }
            }
        }
    }

    fn white_black_precompute(&mut self, v: &GradingTone) {
        for is_black in [false, true] {
            let b = is_black as usize;
            for channel in CHANNELS {
                let c = channel as usize;
                let start = (if is_black {
                    self.blacks_start
                } else {
                    self.whites_start
                }) as f32;
                let width = (if is_black {
                    self.blacks_width
                } else {
                    self.whites_width
                }) as f32;

                let val = if is_black {
                    channel_value(&v.blacks, channel)
                } else {
                    channel_value(&v.whites, channel)
                };

                let x0 = if !is_black { start } else { start - width };
                let x1 = if !is_black { x0 + width } else { start };
                self.wb_x[b][c] = [x0, x1];

                let mtest = if !is_black { val } else { 2.0 - val };

                if mtest < 1.0 {
                    // Slope is decreasing case.
                    if !is_black {
                        let m0 = 1.0;
                        let m1 = std_max(0.01, val);
                        let y0 = x0;
                        let y1 = y0 + (m0 + m1) * (x1 - x0) * 0.5;
                        self.wb_m[b][c] = [m0, m1];
                        self.wb_y[b][c] = [y0, y1];
                    } else {
                        let m0 = std_max(0.01, 2.0 - val);
                        let m1 = 1.0;
                        let y1 = x1;
                        let y0 = y1 - (m0 + m1) * (x1 - x0) * 0.5;
                        self.wb_m[b][c] = [m0, m1];
                        self.wb_y[b][c] = [y0, y1];
                    }
                } else if mtest > 1.0 {
                    // Slope is increasing case.
                    let (m0, m1);
                    if !is_black {
                        m0 = 1.0;
                        m1 = std_max(0.01, 2.0 - val);
                        // y1 won't be used.
                        self.wb_y[b][c][0] = x0;
                    } else {
                        m0 = std_max(0.01, val);
                        m1 = 1.0;
                        let y1 = x1;
                        let y0 = y1 - (m0 + m1) * (x1 - x0) * 0.5;
                        self.wb_y[b][c] = [y0, y1];
                    }
                    self.wb_m[b][c] = [m0, m1];
                    self.wb_gain[b][c] = (m0 + m1) * 0.5;
                }
            }
        }
    }

    fn scontrast_precompute(&mut self, v: &GradingTone, top_sc: f32, bottom: f32, pivot: f32) {
        let mut contrast = v.s_contrast as f32;
        if contrast == 1.0 {
            return;
        }
        // Limit the range of values to prevent reversals.
        contrast = if contrast > 1.0 {
            1.0 / (1.8125 - 0.8125 * std_min(contrast, 1.99))
        } else {
            0.28125 + 0.71875 * std_max(contrast, 0.01)
        };

        // Top end.
        {
            let x3 = top_sc;
            let y3 = top_sc;
            let y0 = pivot + (y3 - pivot) * 0.25;
            let m0 = contrast;
            let x0 = pivot + (y0 - pivot) / m0;
            let min_width = (x3 - x0) * 0.3;
            let mut m3 = 1.0 / m0;
            // NB: Due to the if (contrast != 1.) clause above, m0 != m3.
            let center = (y3 - y0 - m3 * x3 + m0 * x0) / (m0 - m3);
            let mut x1 = x0;
            let mut x2 = 2.0 * center - x1;
            if x2 > x3 {
                x2 = x3;
                x1 = 2.0 * center - x2;
            } else if (x2 - x1) < min_width {
                x2 = x1 + min_width;
                let new_center = (x2 + x1) * 0.5;
                m3 = (y3 - y0 + m0 * x0 - new_center * m0) / (x3 - new_center);
            }
            let y1 = y0;
            let y2 = y1 + (m0 + m3) * (x2 - x1) * 0.5;
            self.sc_x[0] = [x0, x1, x2, x3];
            self.sc_y[0] = [y0, y1, y2, y3];
            self.sc_m[0] = [m0, m3];
        }

        // Bottom end.
        {
            let x0 = bottom;
            let y0 = bottom;
            let y3 = pivot - (pivot - y0) * 0.25;
            let m3 = contrast;
            let x3 = pivot - (pivot - y3) / m3;
            let min_width = (x3 - x0) * 0.3;
            let mut m0 = 1.0 / m3;
            let center = (y3 - y0 - m3 * x3 + m0 * x0) / (m0 - m3);
            let mut x2 = x3;
            let mut x1 = 2.0 * center - x2;
            if x1 < x0 {
                x1 = x0;
                x2 = 2.0 * center - x1;
            } else if (x2 - x1) < min_width {
                x1 = x2 - min_width;
                let new_center = (x2 + x1) * 0.5;
                m0 = (y3 - y0 - m3 * x3 + new_center * m3) / (new_center - x0);
            }
            let y2 = y3;
            let y1 = y2 - (m0 + m3) * (x2 - x1) * 0.5;
            self.sc_x[1] = [x0, x1, x2, x3];
            self.sc_y[1] = [y0, y1, y2, y3];
            self.sc_m[1] = [m0, m3];
        }
    }
}

#[allow(clippy::too_many_arguments)]
fn faux_cubic_fwd_eval(
    t: f64,
    x0: f64,
    x2: f64,
    y0: f64,
    y2: f64,
    m0: f64,
    m2: f64,
    x1: f64,
) -> f64 {
    let y1 = (0.5 / ((x2 - x1) + (x1 - x0)))
        * ((2. * y0 + m0 * (x1 - x0)) * (x2 - x1) + (2. * y2 - m2 * (x2 - x1)) * (x1 - x0));

    let tl = (t - x0) / (x1 - x0);
    let tr = (t - x1) / (x2 - x1);
    let fl = y0 * (1. - tl * tl) + y1 * tl * tl + m0 * (1. - tl) * tl * (x1 - x0);
    let fr = y1 * (1. - tr) * (1. - tr) + y2 * (2. - tr) * tr + m2 * (tr - 1.) * tr * (x2 - x1);

    let mut res = if t < x1 { fl } else { fr };
    if t < x0 {
        res = y0 + (t - x0) * m0;
    }
    if t > x2 {
        res = y2 + (t - x2) * m2;
    }
    res
}

#[allow(clippy::too_many_arguments)]
fn faux_cubic_rev_eval(
    t: f64,
    x0: f64,
    x2: f64,
    y0: f64,
    y2: f64,
    m0: f64,
    m2: f64,
    x1: f64,
) -> f64 {
    let y1 = (0.5 / ((x2 - x1) + (x1 - x0)))
        * ((2. * y0 + m0 * (x1 - x0)) * (x2 - x1) + (2. * y2 - m2 * (x2 - x1)) * (x1 - x0));

    let cl = y0 - t;
    let bl = m0 * (x1 - x0);
    let al = y1 - y0 - m0 * (x1 - x0);
    let discrim_l = (bl * bl - 4. * al * cl).sqrt();
    let tmp_l = (2. * cl) / (-discrim_l - bl);
    let out_l = tmp_l * (x1 - x0) + x0;

    let cr = y1 - t;
    let br = 2. * y2 - 2. * y1 - m2 * (x2 - x1);
    let ar = y1 - y2 + m2 * (x2 - x1);
    let discrim_r = (br * br - 4. * ar * cr).sqrt();
    let tmp_r = (2. * cr) / (-discrim_r - br);
    let out_r = tmp_r * (x2 - x1) + x1;

    let mut res = if t < y1 { out_l } else { out_r };
    if t < y0 {
        res = x0 + (t - y0) / m0;
    }
    if t > y2 {
        res = x2 + (t - y2) / m2;
    }
    res
}

fn highlight_fwd_eval(t: f64, start: f64, pivot: f64, val: f64) -> f64 {
    let x0 = start;
    let x2 = pivot;
    let y0 = x0;
    let y2 = x2;
    let m0 = 1.;
    let x1 = x0 + (x2 - x0) * 0.5;
    let val = 2. - val;
    if val <= 1. {
        let m2 = if val < 0.01 { 0.01 } else { val };
        faux_cubic_fwd_eval(t, x0, x2, y0, y2, m0, m2, x1)
    } else {
        let m2 = if 2. - val < 0.01 { 0.01 } else { 2. - val };
        faux_cubic_rev_eval(t, x0, x2, y0, y2, m0, m2, x1)
    }
}

fn shadow_fwd_eval(t: f64, start: f64, pivot: f64, val: f64) -> f64 {
    let x0 = start;
    let x2 = pivot;
    let y0 = x0;
    let y2 = x2;
    let m2 = 1.;
    let x1 = x0 + (x2 - x0) * 0.5;
    if val <= 1. {
        let m0 = if val < 0.01 { 0.01 } else { val };
        faux_cubic_fwd_eval(t, x0, x2, y0, y2, m0, m2, x1)
    } else {
        let m0 = if 2. - val < 0.01 { 0.01 } else { 2. - val };
        faux_cubic_rev_eval(t, x0, x2, y0, y2, m0, m2, x1)
    }
}

// ---------------------------------------------------------------------------
// CPU rendering

/// `val < limit ? below : above`.
#[inline]
fn on_limit(val: f32, limit: f32, below: f32, above: f32) -> f32 {
    if val < limit {
        below
    } else {
        above
    }
}

/// Channels processed by a zone function: one channel, or R, G & B for the
/// master channel (the computations are component-wise).
#[inline]
fn channel_range(channel: RgbmChannel) -> std::ops::Range<usize> {
    if channel == RgbmChannel::M {
        0..3
    } else {
        let c = channel as usize;
        c..c + 1
    }
}

fn mids_fwd(v: &GradingTone, vpr: &GradingTonePreRender, channel: RgbmChannel, out: &mut Pixel) {
    let mid_adj = clamp_f32(channel_value(&v.midtones, channel), 0.01, 1.99);
    if mid_adj == 1.0 {
        return;
    }
    let c = channel as usize;
    let [x0, x1, x2, x3, x4, x5] = vpr.mid_x[c];
    let [y0, y1, y2, y3, y4, y5] = vpr.mid_y[c];
    let [m0, m1, m2, m3, m4, m5] = vpr.mid_m[c];

    let eval = |t: f32| {
        let tl = (t - x0) / (x1 - x0);
        let tm = (t - x1) / (x2 - x1);
        let tr = (t - x2) / (x3 - x2);
        let tr2 = (t - x3) / (x4 - x3);
        let tr3 = (t - x4) / (x5 - x4);

        let fl = tl * (x1 - x0) * (tl * 0.5 * (m1 - m0) + m0) + y0;
        let fm = tm * (x2 - x1) * (tm * 0.5 * (m2 - m1) + m1) + y1;
        let fr = tr * (x3 - x2) * (tr * 0.5 * (m3 - m2) + m2) + y2;
        let fr2 = tr2 * (x4 - x3) * (tr2 * 0.5 * (m4 - m3) + m3) + y3;
        let fr3 = tr3 * (x5 - x4) * (tr3 * 0.5 * (m5 - m4) + m4) + y4;
        (fl, fm, fr, fr2, fr3)
    };

    if channel != RgbmChannel::M {
        let t = out[c];
        let (fl, fm, fr, fr2, fr3) = eval(t);
        let mut res = if t < x1 { fl } else { fm };
        if t > x2 {
            res = fr;
        }
        if t > x3 {
            res = fr2;
        }
        if t > x4 {
            res = fr3;
        }
        if t < x0 {
            res = y0 + (t - x0) * m0;
        }
        if t > x5 {
            res = y5 + (t - x5) * m5;
        }
        out[c] = res;
    } else {
        for o in out.iter_mut().take(3) {
            let t = *o;
            let (fl, fm, fr, fr2, fr3) = eval(t);
            let fr4 = (t - x0) * m0 + y0;
            let fr5 = (t - x5) * m5 + y5;
            let mut res = on_limit(t, x1, fl, fm);
            res = on_limit(t, x2, res, fr);
            res = on_limit(t, x3, res, fr2);
            res = on_limit(t, x4, res, fr3);
            res = on_limit(t, x0, fr4, res);
            res = on_limit(t, x5, res, fr5);
            *o = res;
        }
    }
}

fn mids_rev(v: &GradingTone, vpr: &GradingTonePreRender, channel: RgbmChannel, out: &mut Pixel) {
    let mid_adj = clamp_f32(channel_value(&v.midtones, channel), 0.01, 1.99);
    if mid_adj == 1.0 {
        return;
    }
    let c = channel as usize;
    let [x0, x1, x2, x3, x4, x5] = vpr.mid_x[c];
    let [y0, y1, y2, y3, y4, y5] = vpr.mid_y[c];
    let [m0, m1, m2, m3, m4, m5] = vpr.mid_m[c];

    // Inverse of the quadratic segment starting at (xa, ya).
    let seg = |t: f32, xa: f32, xb: f32, ya: f32, ma: f32, mb: f32| {
        let cc = ya - t;
        let b = ma * (xb - xa);
        let a = 0.5 * (mb - ma) * (xb - xa);
        let discrim = (b * b - 4.0 * a * cc).sqrt();
        let tmp = (2.0 * cc) / (-discrim - b);
        tmp * (xb - xa) + xa
    };

    if channel != RgbmChannel::M {
        let t = out[c];
        let res = if t >= y5 {
            x0 + (t - y0) / m0
        } else if t >= y4 {
            seg(t, x4, x5, y4, m4, m5)
        } else if t >= y3 {
            seg(t, x3, x4, y3, m3, m4)
        } else if t >= y2 {
            seg(t, x2, x3, y2, m2, m3)
        } else if t >= y1 {
            seg(t, x1, x2, y1, m1, m2)
        } else if t >= y0 {
            seg(t, x0, x1, y0, m0, m1)
        } else {
            x0 + (t - y0) / m0
        };
        out[c] = res;
    } else {
        // Same computations as OCIO's float3 version ((2c) / (-b - discrim)).
        let seg3 = |t: f32, xa: f32, xb: f32, ya: f32, ma: f32, mb: f32| {
            let cc = ya - t;
            let b = ma * (xb - xa);
            let a = 0.5 * (mb - ma) * (xb - xa);
            let discrim = (b * b - 4.0 * a * cc).sqrt();
            let tmp = (2.0 * cc) / (-b - discrim);
            tmp * (xb - xa) + xa
        };
        for o in out.iter_mut().take(3) {
            let t = *o;
            let out_r4 = x5 + (t - y5) / m5;
            let out_r3 = seg3(t, x4, x5, y4, m4, m5);
            let out_r2 = seg3(t, x3, x4, y3, m3, m4);
            let out_r = seg3(t, x2, x3, y2, m2, m3);
            let out_m = seg3(t, x1, x2, y1, m1, m2);
            let out_l = seg3(t, x0, x1, y0, m0, m1);
            let out_l0 = x0 + (t - y0) / m0;

            let mut res = on_limit(t, y1, out_l, out_m);
            res = on_limit(t, y2, res, out_r);
            res = on_limit(t, y3, res, out_r2);
            res = on_limit(t, y4, res, out_r3);
            res = on_limit(t, y0, out_l0, res);
            res = on_limit(t, y5, res, out_r4);
            *o = res;
        }
    }
}

#[allow(clippy::too_many_arguments)]
#[inline]
fn compute_hs_fwd(
    x0: f32,
    x1: f32,
    x2: f32,
    y0: f32,
    y1: f32,
    y2: f32,
    m0: f32,
    m2: f32,
    t: f32,
) -> f32 {
    let tl = (t - x0) / (x1 - x0);
    let tr = (t - x1) / (x2 - x1);
    let fl = y0 * (1.0 - tl * tl) + y1 * tl * tl + m0 * (1.0 - tl) * tl * (x1 - x0);
    let fr = y1 * (1.0 - tr) * (1.0 - tr) + y2 * (2.0 - tr) * tr + m2 * (tr - 1.0) * tr * (x2 - x1);

    let mut res = on_limit(t, x1, fl, fr);
    let r0 = (t - x0) * m0 + y0;
    res = on_limit(t, x0, r0, res);
    let r2 = (t - x2) * m2 + y2;
    on_limit(t, x2, res, r2)
}

#[allow(clippy::too_many_arguments)]
#[inline]
fn compute_hs_rev(
    x0: f32,
    x1: f32,
    x2: f32,
    y0: f32,
    y1: f32,
    y2: f32,
    m0: f32,
    m2: f32,
    t: f32,
) -> f32 {
    let bl = m0 * (x1 - x0);
    let al = y1 - y0 - m0 * (x1 - x0);
    let cl = y0 - t;
    let discrim_l = (bl * bl - 4.0 * al * cl).sqrt();
    let out_l = (-2.0 * cl) / (discrim_l + bl) * (x1 - x0) + x0;
    let br = 2.0 * y2 - 2.0 * y1 - m2 * (x2 - x1);
    let ar = y1 - y2 + m2 * (x2 - x1);
    let cr = y1 - t;
    let discrim_r = (br * br - 4.0 * ar * cr).sqrt();
    let out_r = (-2.0 * cr) / (discrim_r + br) * (x2 - x1) + x1;

    let mut res = on_limit(t, y1, out_l, out_r);
    let r0 = (t - y0) / m0 + x0;
    res = on_limit(t, y0, r0, res);
    let r2 = (t - y2) / m2 + x2;
    on_limit(t, y2, res, r2)
}

fn highlight_shadow(
    v: &GradingTone,
    vpr: &GradingTonePreRender,
    channel: RgbmChannel,
    is_shadow: bool,
    reverse: bool,
    out: &mut Pixel,
) {
    // The effect of val is symmetric around 1 (<1 uses Fwd algorithm, >1 uses Rev algorithm).
    let mut val = if is_shadow {
        channel_value(&v.shadows, channel)
    } else {
        channel_value(&v.highlights, channel)
    };
    if !is_shadow {
        val = 2.0 - val;
    }
    if val == 1.0 {
        return;
    }
    let s = is_shadow as usize;
    let c = channel as usize;
    let [x0, x1, x2] = vpr.hs_x[s][c];
    let [y0, y1, y2] = vpr.hs_y[s][c];
    let [m0, m2] = vpr.hs_m[s][c];

    let use_fwd = (val < 1.0) != reverse;
    for i in channel_range(channel) {
        let t = out[i];
        out[i] = if use_fwd {
            compute_hs_fwd(x0, x1, x2, y0, y1, y2, m0, m2, t)
        } else {
            compute_hs_rev(x0, x1, x2, y0, y1, y2, m0, m2, t)
        };
    }
}

#[allow(clippy::too_many_arguments)]
fn compute_wb_fwd(
    is_black: bool,
    val: f32,
    x0: f32,
    x1: f32,
    y0: f32,
    y1: f32,
    m0: f32,
    m1: f32,
    gain: f32,
    t: f32,
) -> Option<f32> {
    let mtest = if !is_black { val } else { 2.0 - val };

    if mtest < 1.0 {
        // Slope is decreasing case.
        let tlocal = (t - x0) / (x1 - x0);
        let mut res = tlocal * (x1 - x0) * (tlocal * 0.5 * (m1 - m0) + m0) + y0;
        let res0 = y0 + (t - x0) * m0;
        res = on_limit(t, x0, res0, res);
        let res1 = y1 + (t - x1) * m1;
        Some(on_limit(t, x1, res, res1))
    } else if mtest > 1.0 {
        // Slope is increasing case.
        let mut t = if !is_black {
            (t - x0) * gain + x0
        } else {
            (t - x1) * gain + x1
        };

        let a = 0.5 * (m1 - m0) * (x1 - x0);
        let b = m0 * (x1 - x0);

        let c = y0 - t;
        let discrim = (b * b - 4.0 * a * c).sqrt();
        let tmp = (-2.0 * c) / (discrim + b);
        let mut res = tmp * (x1 - x0) + x0;
        let res0 = x0 + (t - y0) / m0;
        res = on_limit(t, y0, res0, res);

        if !is_black {
            res = (res - x0) / gain + x0;
            // Quadratic extrapolation for better HDR control.
            let new_y1 = (x1 - x0) / gain + x0;
            let xd = x0 + (x1 - x0) * 0.99;
            let mut md = m0 + (xd - x0) * (m1 - m0) / (x1 - x0);
            md = 1.0 / md;
            let aa = 0.5 * (1.0 / m1 - md) / (x1 - xd);
            let bb = 1.0 / m1 - 2.0 * aa * x1;
            let cc = new_y1 - bb * x1 - aa * x1 * x1;
            t = (t - x0) / gain + x0;

            let res1 = (aa * t + bb) * t + cc;
            res = on_limit(t, x1, res, res1);
        } else {
            let res1 = x1 + (t - y1) / m1;
            res = on_limit(t, y1, res, res1);
            res = (res - x1) / gain + x1;
        }
        Some(res)
    } else {
        None
    }
}

#[allow(clippy::too_many_arguments)]
fn compute_wb_rev(
    is_black: bool,
    val: f32,
    x0: f32,
    x1: f32,
    y0: f32,
    y1: f32,
    m0: f32,
    m1: f32,
    gain: f32,
    t: f32,
) -> Option<f32> {
    let mtest = if !is_black { val } else { 2.0 - val };

    if mtest < 1.0 {
        // Slope is decreasing case.
        let a = 0.5 * (m1 - m0) * (x1 - x0);
        let b = m0 * (x1 - x0);

        let c = y0 - t;
        let discrim = (b * b - 4.0 * a * c).sqrt();
        let tmp = (-2.0 * c) / (discrim + b);
        let mut res = tmp * (x1 - x0) + x0;
        let res0 = x0 + (t - y0) / m0;
        res = on_limit(t, y0, res0, res);

        let res1 = x1 + (t - y1) / m1;
        Some(on_limit(t, y1, res, res1))
    } else if mtest > 1.0 {
        // Slope is increasing case.
        let mut t = if !is_black {
            (t - x0) * gain + x0
        } else {
            (t - x1) * gain + x1
        };

        let tlocal = (t - x0) / (x1 - x0);
        let mut res = tlocal * (x1 - x0) * (tlocal * 0.5 * (m1 - m0) + m0) + y0;
        let res0 = y0 + (t - x0) * m0;
        res = on_limit(t, x0, res0, res);

        if !is_black {
            res = (res - x0) / gain + x0;
            // Quadratic extrapolation for better HDR control.
            let new_y1 = (x1 - x0) / gain + x0;
            let xd = x0 + (x1 - x0) * 0.99;
            let mut md = m0 + (xd - x0) * (m1 - m0) / (x1 - x0);
            md = 1.0 / md;
            let aa = 0.5 * (1.0 / m1 - md) / (x1 - xd);
            let bb = 1.0 / m1 - 2.0 * aa * x1;
            let cc = new_y1 - bb * x1 - aa * x1 * x1;
            t = (t - x0) / gain + x0;

            let c = cc - t;
            let discrim = (bb * bb - 4.0 * aa * c).sqrt();
            let res1 = (-2.0 * c) / (discrim + bb);
            let brk = (aa * x1 + bb) * x1 + cc;
            res = on_limit(t, brk, res, res1);
        } else {
            let res1 = y1 + (t - x1) * m1;
            res = on_limit(t, x1, res, res1);
            res = (res - x1) / gain + x1;
        }
        Some(res)
    } else {
        None
    }
}

fn white_black(
    v: &GradingTone,
    vpr: &GradingTonePreRender,
    channel: RgbmChannel,
    is_black: bool,
    reverse: bool,
    out: &mut Pixel,
) {
    let val = if is_black {
        channel_value(&v.blacks, channel)
    } else {
        channel_value(&v.whites, channel)
    };
    let b = is_black as usize;
    let c = channel as usize;
    let [x0, x1] = vpr.wb_x[b][c];
    let [y0, y1] = vpr.wb_y[b][c];
    let [m0, m1] = vpr.wb_m[b][c];
    let gain = vpr.wb_gain[b][c];

    for i in channel_range(channel) {
        let t = out[i];
        let res = if reverse {
            compute_wb_rev(is_black, val, x0, x1, y0, y1, m0, m1, gain, t)
        } else {
            compute_wb_fwd(is_black, val, x0, x1, y0, y1, m0, m1, gain, t)
        };
        if let Some(r) = res {
            out[i] = r;
        }
    }
}

fn scontrast_contrast(v: &GradingTone) -> Option<f32> {
    let contrast = v.s_contrast as f32;
    if contrast == 1.0 {
        return None;
    }
    // Limit the range of values to prevent reversals.
    Some(if contrast > 1.0 {
        1.0 / (1.8125 - 0.8125 * std_min(contrast, 1.99))
    } else {
        0.28125 + 0.71875 * std_max(contrast, 0.01)
    })
}

fn scontrast_fwd(v: &GradingTone, vpr: &GradingTonePreRender, out: &mut Pixel) {
    let Some(contrast) = scontrast_contrast(v) else {
        return;
    };
    for o in out.iter_mut().take(3) {
        let t = *o;
        let mut out_color = (t - vpr.pivot) * contrast + vpr.pivot;

        // Top end.
        {
            let x1 = vpr.sc_x[0][1];
            let x2 = vpr.sc_x[0][2];
            let y1 = vpr.sc_y[0][1];
            let y2 = vpr.sc_y[0][2];
            let m0 = vpr.sc_m[0][0];
            let m3 = vpr.sc_m[0][1];

            let tr = (t - x1) / (x2 - x1);
            let res = tr * (x2 - x1) * (tr * 0.5 * (m3 - m0) + m0) + y1;
            out_color = on_limit(t, x1, out_color, res);
            let res2 = y2 + (t - x2) * m3;
            out_color = on_limit(t, x2, out_color, res2);
        }

        // Bottom end.
        {
            let x1 = vpr.sc_x[1][1];
            let x2 = vpr.sc_x[1][2];
            let y1 = vpr.sc_y[1][1];
            let m0 = vpr.sc_m[1][0];
            let m3 = vpr.sc_m[1][1];

            let tr = (t - x1) / (x2 - x1);
            let res = tr * (x2 - x1) * (tr * 0.5 * (m3 - m0) + m0) + y1;
            out_color = on_limit(t, x2, res, out_color);
            let res1 = y1 + (t - x1) * m0;
            out_color = on_limit(t, x1, res1, out_color);
        }
        *o = out_color;
    }
}

fn scontrast_rev(v: &GradingTone, vpr: &GradingTonePreRender, out: &mut Pixel) {
    let Some(contrast) = scontrast_contrast(v) else {
        return;
    };
    for o in out.iter_mut().take(3) {
        let t = *o;
        let mut out_color = (t - vpr.pivot) / contrast + vpr.pivot;

        // Top end.
        {
            let x1 = vpr.sc_x[0][1];
            let x2 = vpr.sc_x[0][2];
            let y1 = vpr.sc_y[0][1];
            let y2 = vpr.sc_y[0][2];
            let m0 = vpr.sc_m[0][0];
            let m3 = vpr.sc_m[0][1];

            let b = m0 * (x2 - x1);
            let a = (m3 - m0) * 0.5 * (x2 - x1);
            let c = y1 - t;
            let discrim = (b * b - 4.0 * a * c).sqrt();
            let res = (x2 - x1) * (-2.0 * c) / (discrim + b) + x1;

            out_color = on_limit(t, y1, out_color, res);
            out_color = on_limit(t, y2, out_color, x2 + (t - y2) / m3);
        }

        // Bottom end.
        {
            let x1 = vpr.sc_x[1][1];
            let x2 = vpr.sc_x[1][2];
            let y1 = vpr.sc_y[1][1];
            let y2 = vpr.sc_y[1][2];
            let m0 = vpr.sc_m[1][0];
            let m3 = vpr.sc_m[1][1];

            let b = m0 * (x2 - x1);
            let a = (m3 - m0) * 0.5 * (x2 - x1);
            let c = y1 - t;
            let discrim = (b * b - 4.0 * a * c).sqrt();
            let res = (x2 - x1) * (-2.0 * c) / (discrim + b) + x1;

            out_color = on_limit(t, y2, res, out_color);
            out_color = on_limit(t, y1, x1 + (t - y1) / m0, out_color);
        }
        *o = out_color;
    }
}

#[inline]
fn clamp_max_rgb(out: &mut Pixel) {
    // The grading controls at high values are able to push values above the max
    // half-float at which point they overflow to infinity.
    for o in out.iter_mut().take(3) {
        *o = std_min(*o, 65504.0);
    }
}

/// Constants of the lin-to-log conversion used by the linear style (shared
/// with the RGB curve and hue curve ops).
#[allow(clippy::excessive_precision)]
pub(crate) mod log_lin {
    pub const XBRK: f32 = 0.0041318374739483946;
    pub const SHIFT: f32 = -0.000157849851665374;
    pub const M: f32 = 1.0 / (0.18 + SHIFT);
    pub const GAIN: f32 = 363.034608563;
    pub const OFFS: f32 = -7.0;
    pub const YBRK: f32 = -5.5;
    /// 1/log(2).
    pub const BASE2: f32 = std::f32::consts::LOG2_E;

    /// Lin to log of one value.
    #[inline]
    pub fn lin_log(v: f32) -> f32 {
        if v < XBRK {
            v * GAIN + OFFS
        } else {
            BASE2 * ((v + SHIFT) * M).ln()
        }
    }

    /// Log to lin of one value.
    #[inline]
    pub fn log_lin(v: f32) -> f32 {
        if v < YBRK {
            (v - OFFS) / GAIN
        } else {
            2.0f32.powf(v) * (0.18 + SHIFT) - SHIFT
        }
    }

    /// [`lin_log`] as computed by the `OCIO_USE_SSE2` branch of the RGB
    /// curve and tone renderers, which OCIO's SSE2 builds always use: the
    /// fast `sseLog2`, and the log segment for the values greater than the
    /// break point (a NaN takes the linear segment).
    #[inline]
    pub fn lin_log_sse(v: f32) -> f32 {
        use crate::ops::sse;
        let lin = v * GAIN + OFFS;
        let log = sse::log2((v + SHIFT) * M);
        if v > XBRK {
            log
        } else {
            lin
        }
    }

    /// [`log_lin`] as computed by the `OCIO_USE_SSE2` branch (see
    /// [`lin_log_sse`]): `ssePower(2, v)`.
    #[inline]
    pub fn log_lin_sse(v: f32) -> f32 {
        use crate::ops::sse;
        let lin = (v - OFFS) * (1.0 / GAIN);
        let log = sse::power(2.0, v) * (SHIFT + 0.18) - SHIFT;
        if v > YBRK {
            log
        } else {
            lin
        }
    }
}

use RgbmChannel::{B, G, M, R};

fn apply_fwd_pixel(v: &GradingTone, vpr: &GradingTonePreRender, out: &mut Pixel) {
    for ch in [R, G, B, M] {
        mids_fwd(v, vpr, ch, out);
    }
    for ch in [R, G, B, M] {
        highlight_shadow(v, vpr, ch, false, false, out);
    }
    for ch in [R, G, B, M] {
        white_black(v, vpr, ch, false, false, out);
    }
    for ch in [R, G, B, M] {
        highlight_shadow(v, vpr, ch, true, false, out);
    }
    for ch in [R, G, B, M] {
        white_black(v, vpr, ch, true, false, out);
    }
    scontrast_fwd(v, vpr, out);
}

fn apply_rev_pixel(v: &GradingTone, vpr: &GradingTonePreRender, out: &mut Pixel) {
    scontrast_rev(v, vpr, out);
    for ch in [M, R, G, B] {
        white_black(v, vpr, ch, true, true, out);
    }
    for ch in [M, R, G, B] {
        highlight_shadow(v, vpr, ch, true, true, out);
    }
    for ch in [M, R, G, B] {
        white_black(v, vpr, ch, false, true, out);
    }
    for ch in [M, R, G, B] {
        highlight_shadow(v, vpr, ch, false, true, out);
    }
    for ch in [M, R, G, B] {
        mids_rev(v, vpr, ch, out);
    }
}

// ---------------------------------------------------------------------------
// The op

/// Tonal grading op (port of `GradingToneOp` / `GradingToneOpData`).
#[derive(Debug, Clone)]
pub struct GradingToneOp {
    style: GradingStyle,
    direction: TransformDirection,
    value: GradingValue<GradingTone, GradingTonePreRender>,
    metadata: FormatMetadata,
}

impl GradingToneOp {
    /// Create the op. The value is validated.
    pub fn new(
        style: GradingStyle,
        value: GradingTone,
        direction: TransformDirection,
        dynamic: bool,
    ) -> Result<Self> {
        value.validate()?;
        let pre = GradingTonePreRender::from_value(style, &value);
        Ok(Self {
            style,
            direction,
            value: GradingValue::new(value, pre, dynamic),
            metadata: FormatMetadata::default(),
        })
    }

    /// Identity op of the style.
    pub fn identity(style: GradingStyle) -> Self {
        let value = GradingTone::new(style);
        let pre = GradingTonePreRender::from_value(style, &value);
        Self {
            style,
            direction: TransformDirection::Forward,
            value: GradingValue::new(value, pre, false),
            metadata: FormatMetadata::default(),
        }
    }

    /// The grading style.
    pub fn style(&self) -> GradingStyle {
        self.style
    }
    /// The direction.
    pub fn direction(&self) -> TransformDirection {
        self.direction
    }
    /// The format metadata.
    pub fn metadata(&self) -> &FormatMetadata {
        &self.metadata
    }
    /// Set the format metadata.
    pub fn set_metadata(&mut self, metadata: FormatMetadata) {
        self.metadata = metadata;
    }

    fn state(&self) -> Arc<(GradingTone, GradingTonePreRender)> {
        let style = self.style;
        self.value.current(|v| {
            v.validate()?;
            Ok(GradingTonePreRender::from_value(style, v))
        })
    }

    /// The current value (of the dynamic property if dynamic).
    pub fn value(&self) -> GradingTone {
        self.state().0
    }

    /// The same op in the opposite direction.
    pub fn inverse(&self) -> Self {
        let mut res = self.clone();
        res.direction = self.direction.inverse();
        res
    }

    /// True if `other` is the inverse of `self` (never true for dynamic ops).
    pub fn is_inverse(&self, other: &GradingToneOp) -> bool {
        if self.is_dynamic() || other.is_dynamic() {
            return false;
        }
        self.style == other.style
            && self.value() == other.value()
            && self.direction.combine(other.direction) == TransformDirection::Inverse
    }

    fn data_cache_id(&self) -> String {
        let mut s = String::new();
        if !self.metadata.id().is_empty() {
            s.push_str(self.metadata.id());
            s.push(' ');
        }
        s.push_str(self.style.as_str());
        s.push(' ');
        s.push_str(self.direction.as_str());
        s.push(' ');
        if !self.is_dynamic() {
            s.push_str(&format!("{:.7}", self.value()));
        }
        s
    }
}

/// Create a tonal grading op (port of `CreateGradingToneOp`): the op is
/// inverted if `direction` is inverse.
pub fn create_grading_tone_op(
    ops: &mut OpVec,
    style: GradingStyle,
    value: &GradingTone,
    op_direction: TransformDirection,
    dynamic: bool,
    direction: TransformDirection,
) -> Result<()> {
    let op = GradingToneOp::new(style, *value, op_direction.combine(direction), dynamic)?;
    ops.push(Arc::new(op));
    Ok(())
}

impl Op for GradingToneOp {
    fn name(&self) -> &'static str {
        "GradingTone"
    }

    fn apply(&self, pixels: &mut [Pixel]) {
        let state = self.state();
        let (v, vpr) = (&state.0, &state.1);
        if vpr.local_bypass {
            return;
        }
        let lin = self.style == GradingStyle::Lin;
        let fwd = self.direction == TransformDirection::Forward;
        for out in pixels.iter_mut() {
            if lin {
                // As in OCIO's SSE2 renderers, the shapers process the four
                // lanes and alpha is not restored when the processing is done
                // in place, as OCIO's CPU processors do.
                for o in out.iter_mut() {
                    *o = log_lin::lin_log_sse(*o);
                }
            }
            if fwd {
                apply_fwd_pixel(v, vpr, out);
            } else {
                apply_rev_pixel(v, vpr, out);
            }
            if lin {
                for o in out.iter_mut() {
                    *o = log_lin::log_lin_sse(*o);
                }
            }
            clamp_max_rgb(out);
        }
    }

    fn is_no_op(&self) -> bool {
        self.is_identity()
    }

    fn is_identity(&self) -> bool {
        !self.is_dynamic() && self.value().is_identity()
    }

    fn has_channel_crosstalk(&self) -> bool {
        false
    }

    fn cache_id(&self) -> String {
        format!("<GradingToneOp {}>", self.data_cache_id())
    }

    fn as_any(&self) -> &dyn Any {
        self
    }

    fn combine_with(&self, next: &dyn Op, flags: OptimizationFlags) -> Option<OpVec> {
        if !flags.contains(OptimizationFlags::PAIR_IDENTITY_GRADING) {
            return None;
        }
        let other = next.downcast_ref::<GradingToneOp>()?;
        self.is_inverse(other).then(OpVec::new)
    }

    fn is_dynamic(&self) -> bool {
        self.value.is_dynamic()
    }

    fn dynamic_property(&self, ty: DynamicPropertyType) -> Option<DynamicProperty> {
        if ty != DynamicPropertyType::GradingTone {
            return None;
        }
        self.value
            .property()
            .map(|p| DynamicProperty::GradingTone(p.clone()))
    }

    fn replace_dynamic_property(&mut self, prop: &DynamicProperty) {
        if let Some(p) = prop.as_grading_tone() {
            self.value.replace_property(p);
        }
    }

    fn make_non_dynamic(&self) -> Option<OpRc> {
        if !self.is_dynamic() {
            return None;
        }
        let style = self.style;
        let value = self.value.to_static(|v| {
            v.validate()?;
            Ok(GradingTonePreRender::from_value(style, v))
        });
        Some(Arc::new(Self {
            value,
            ..self.clone()
        }))
    }

    fn to_transform(&self) -> Option<Transform> {
        Some(Transform::GradingTone(GradingToneTransform {
            direction: self.direction,
            style: self.style,
            value: self.value(),
            dynamic: self.is_dynamic(),
            metadata: self.metadata.clone(),
        }))
    }

    fn clone_box(&self) -> Box<dyn Op> {
        Box::new(self.clone())
    }
}

// ---------------------------------------------------------------------------
// Transform

impl GradingToneTransform {
    /// Change the style (values are reset to the defaults of the new style).
    pub fn set_style(&mut self, style: GradingStyle) {
        if style != self.style {
            self.style = style;
            self.value = GradingTone::new(style);
        }
    }

    /// Set the values (they are validated first).
    pub fn set_value(&mut self, value: GradingTone) -> Result<()> {
        value.validate()?;
        self.value = value;
        Ok(())
    }

    /// True if the created op holds a dynamic property.
    pub fn is_dynamic(&self) -> bool {
        self.dynamic
    }
    /// Make the created op hold a dynamic property.
    pub fn make_dynamic(&mut self) {
        self.dynamic = true;
    }
    /// Make the created op static.
    pub fn make_non_dynamic(&mut self) {
        self.dynamic = false;
    }
}

impl Validate for GradingToneTransform {
    fn validate(&self) -> Result<()> {
        self.value
            .validate()
            .map_err(|e| e.prefixed("GradingToneTransform validation failed: "))
    }
}

impl BuildOps for GradingToneTransform {
    fn build_ops(
        &self,
        ops: &mut OpVec,
        _config: &Config,
        _context: &Context,
        dir: TransformDirection,
    ) -> Result<()> {
        let mut op = GradingToneOp::new(
            self.style,
            self.value,
            self.direction.combine(dir),
            self.dynamic,
        )?;
        op.set_metadata(self.metadata.clone());
        ops.push(Arc::new(op));
        Ok(())
    }
}

impl fmt::Display for GradingToneTransform {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "<GradingToneTransform direction={}, style={}, values={}",
            self.direction.as_str(),
            self.style.as_str(),
            self.value
        )?;
        if self.dynamic {
            f.write_str(", dynamic")?;
        }
        f.write_str(">")
    }
}

#[cfg(test)]
mod tests;