ocio 0.2.1

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
//! ASC CDL op (port of `CDLOpData`, `CDLOp`, `CDLOpCPU` and the op building
//! part of `CDLTransform.cpp`).
//!
//! `out = sat(clamp(slope * in + offset) ^ power)` (forward, the clamps only
//! apply to the ASC v1.2 style). A CDL without power is replaced by matrices
//! (and clamping ranges) under `OptimizationFlags::SIMPLIFY_OPS`.

use crate::config::Config;
use crate::context::Context;
use crate::error::Result;
use crate::format_metadata::FormatMetadata;
use crate::math_utils::{clamp_f32, equal_with_abs_error};
use crate::ops::exponent::{config_major_version, create_exponent_op};
use crate::ops::matrix::float_format::format_g;
use crate::ops::matrix::{
    create_matrix_op_from_data, create_saturation_op, create_scale_offset_op, MatrixOpData,
};
use crate::ops::range::{create_range_op_from_data, RangeOpData};
use crate::ops::{Op, OpRc, OpVec, Pixel};
use crate::transforms::{
    BuildOps, CdlTransform, MatrixTransform, Transform, Validate, METADATA_SOP_DESCRIPTION,
};
use crate::types::{CdlStyle, OptimizationFlags, TransformDirection};
use std::any::Any;
use std::sync::Arc;

const FLOAT_DECIMALS: usize = 7;

/// Luma weights used by the CDL saturation.
pub const CDL_LUMA_COEFS: [f64; 3] = [0.2126, 0.7152, 0.0722];

/// Styles of the CDL op (port of `CDLOpData::Style`).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum CdlOpStyle {
    /// ASC v1.2 forward (clamps to `[0, 1]`).
    AscFwd,
    /// ASC v1.2 reverse (clamps to `[0, 1]`).
    AscRev,
    /// Forward without clamping.
    #[default]
    NoClampFwd,
    /// Reverse without clamping.
    NoClampRev,
}

impl CdlOpStyle {
    /// Parse a CTF / CLF style name (case insensitive; `v1.2_Fwd`, `Fwd`,
    /// `noClampFwd`, `FwdNoClamp`, ...).
    pub fn parse(name: &str) -> Result<Self> {
        let pairs = [
            ("v1.2_Fwd", CdlOpStyle::AscFwd),
            ("Fwd", CdlOpStyle::AscFwd),
            ("v1.2_Rev", CdlOpStyle::AscRev),
            ("Rev", CdlOpStyle::AscRev),
            ("noClampFwd", CdlOpStyle::NoClampFwd),
            ("FwdNoClamp", CdlOpStyle::NoClampFwd),
            ("noClampRev", CdlOpStyle::NoClampRev),
            ("RevNoClamp", CdlOpStyle::NoClampRev),
        ];
        for (n, s) in pairs {
            if !name.is_empty() && n.eq_ignore_ascii_case(name) {
                return Ok(s);
            }
        }
        crate::bail!("Unknown style for CDL.")
    }

    /// CLF name of the style.
    pub fn as_str(&self) -> &'static str {
        match self {
            CdlOpStyle::AscFwd => "Fwd",
            CdlOpStyle::AscRev => "Rev",
            CdlOpStyle::NoClampFwd => "FwdNoClamp",
            CdlOpStyle::NoClampRev => "RevNoClamp",
        }
    }

    /// Style from the transform style and a direction.
    pub fn from_transform_style(style: CdlStyle, dir: TransformDirection) -> Self {
        let fwd = dir == TransformDirection::Forward;
        match (style, fwd) {
            (CdlStyle::Asc, true) => CdlOpStyle::AscFwd,
            (CdlStyle::Asc, false) => CdlOpStyle::AscRev,
            (CdlStyle::NoClamp, true) => CdlOpStyle::NoClampFwd,
            (CdlStyle::NoClamp, false) => CdlOpStyle::NoClampRev,
        }
    }

    /// The transform style.
    pub fn transform_style(&self) -> CdlStyle {
        match self {
            CdlOpStyle::AscFwd | CdlOpStyle::AscRev => CdlStyle::Asc,
            CdlOpStyle::NoClampFwd | CdlOpStyle::NoClampRev => CdlStyle::NoClamp,
        }
    }

    /// Direction encoded in the style.
    pub fn direction(&self) -> TransformDirection {
        match self {
            CdlOpStyle::AscFwd | CdlOpStyle::NoClampFwd => TransformDirection::Forward,
            CdlOpStyle::AscRev | CdlOpStyle::NoClampRev => TransformDirection::Inverse,
        }
    }

    /// The inverse style.
    pub fn inverse(&self) -> CdlOpStyle {
        match self {
            CdlOpStyle::AscFwd => CdlOpStyle::AscRev,
            CdlOpStyle::AscRev => CdlOpStyle::AscFwd,
            CdlOpStyle::NoClampFwd => CdlOpStyle::NoClampRev,
            CdlOpStyle::NoClampRev => CdlOpStyle::NoClampFwd,
        }
    }

    /// True for the reverse styles.
    pub fn is_reverse(&self) -> bool {
        self.direction() == TransformDirection::Inverse
    }

    /// True for the clamping (ASC) styles.
    pub fn is_clamping(&self) -> bool {
        matches!(self, CdlOpStyle::AscFwd | CdlOpStyle::AscRev)
    }
}

/// Channel parameters comparison (1e-9 absolute tolerance, as OCIO's
/// `ChannelParams::operator==`).
pub fn channel_params_equal(a: &[f64; 3], b: &[f64; 3]) -> bool {
    (0..3).all(|i| equal_with_abs_error(a[i], b[i], 1e-9))
}

/// Parameters of a [`CdlOp`] (port of OCIO's `CDLOpData`).
#[derive(Debug, Clone)]
pub struct CdlOpData {
    /// Style (encodes the direction).
    pub style: CdlOpStyle,
    /// RGB slopes.
    pub slope: [f64; 3],
    /// RGB offsets.
    pub offset: [f64; 3],
    /// RGB powers.
    pub power: [f64; 3],
    /// Saturation.
    pub saturation: f64,
    /// Metadata.
    pub metadata: FormatMetadata,
}

impl Default for CdlOpData {
    /// Identity CDL of the default style (no clamp, forward).
    fn default() -> Self {
        Self {
            style: CdlOpStyle::NoClampFwd,
            slope: [1.0; 3],
            offset: [0.0; 3],
            power: [1.0; 3],
            saturation: 1.0,
            metadata: FormatMetadata::default(),
        }
    }
}

impl PartialEq for CdlOpData {
    /// Equality as defined by OCIO (tolerance on the SOP values, metadata
    /// ignored).
    fn eq(&self, other: &Self) -> bool {
        self.style == other.style
            && channel_params_equal(&self.slope, &other.slope)
            && channel_params_equal(&self.offset, &other.offset)
            && channel_params_equal(&self.power, &other.power)
            && self.saturation == other.saturation
    }
}

fn validate_greater_equal(name: &str, value: f64, threshold: f64) -> Result<()> {
    if !(value >= threshold) {
        crate::bail!(
            "CDL: Invalid '{}' {} should be greater than {}.",
            name,
            format_g(value, 6),
            format_g(threshold, 6)
        );
    }
    Ok(())
}

fn validate_greater_than(name: &str, value: f64, threshold: f64) -> Result<()> {
    if !(value > threshold) {
        crate::bail!(
            "CDLOpData: Invalid '{}' {} should be greater than {}.",
            name,
            format_g(value, 6),
            format_g(threshold, 6)
        );
    }
    Ok(())
}

fn params_string(p: &[f64; 3]) -> String {
    format!(
        "{}, {}, {}",
        format_g(p[0], FLOAT_DECIMALS),
        format_g(p[1], FLOAT_DECIMALS),
        format_g(p[2], FLOAT_DECIMALS)
    )
}

impl CdlOpData {
    /// Build from all the parameters and validate them.
    pub fn new(
        style: CdlOpStyle,
        slope: [f64; 3],
        offset: [f64; 3],
        power: [f64; 3],
        saturation: f64,
    ) -> Result<Self> {
        let d = Self {
            style,
            slope,
            offset,
            power,
            saturation,
            metadata: FormatMetadata::default(),
        };
        d.validate()?;
        Ok(d)
    }

    /// Build from a [`CdlTransform`] (not validated).
    pub fn from_transform(t: &CdlTransform) -> Self {
        Self {
            style: CdlOpStyle::from_transform_style(t.style, t.direction),
            slope: t.slope,
            offset: t.offset,
            power: t.power,
            saturation: t.sat,
            metadata: t.metadata.clone(),
        }
    }

    /// The `id` metadata attribute.
    pub fn id(&self) -> &str {
        self.metadata.id()
    }

    /// Validate: slope >= 0, power > 0, saturation >= 0 (ASC v1.2 spec).
    pub fn validate(&self) -> Result<()> {
        for v in self.slope {
            validate_greater_equal("slope", v, 0.0)?;
        }
        for v in self.power {
            validate_greater_than("power", v, 0.0)?;
        }
        validate_greater_equal("saturation", self.saturation, 0.0)
    }

    /// Direction encoded in the style.
    pub fn direction(&self) -> TransformDirection {
        self.style.direction()
    }

    /// Set the direction (inverts the style if needed).
    pub fn set_direction(&mut self, dir: TransformDirection) {
        if self.direction() != dir {
            self.style = self.style.inverse();
        }
    }

    /// True for the reverse styles.
    pub fn is_reverse(&self) -> bool {
        self.style.is_reverse()
    }

    /// True for the clamping (ASC) styles.
    pub fn is_clamping(&self) -> bool {
        self.style.is_clamping()
    }

    /// True if the SOP and saturation are the identity values (the op may
    /// still clamp).
    pub fn is_identity(&self) -> bool {
        channel_params_equal(&self.slope, &[1.0; 3])
            && channel_params_equal(&self.offset, &[0.0; 3])
            && channel_params_equal(&self.power, &[1.0; 3])
            && self.saturation == 1.0
    }

    /// An identity that does not clamp.
    pub fn is_no_op(&self) -> bool {
        self.is_identity() && !self.is_clamping()
    }

    /// The saturation mixes the channels.
    pub fn has_channel_crosstalk(&self) -> bool {
        self.saturation != 1.0
    }

    /// Copy with the inverse style.
    pub fn inverse(&self) -> CdlOpData {
        let mut c = self.clone();
        c.style = c.style.inverse();
        c
    }

    /// True if `r` is the inverse of `self`.
    pub fn is_inverse(&self, r: &CdlOpData) -> bool {
        *r == self.inverse()
    }

    /// The op replacing an identity (or a pair of inverse CDLs): a `[0, 1]`
    /// clamp for the ASC styles, `None` (an identity matrix) otherwise.
    pub fn identity_replacement(&self) -> Option<RangeOpData> {
        if self.is_clamping() {
            RangeOpData::new(0.0, 1.0, 0.0, 1.0).ok()
        } else {
            None
        }
    }

    /// Simpler ops replacing a CDL whose power is 1 (port of
    /// `getSimplerReplacement`): matrices for the slope / offset and the
    /// saturation, and ranges for the clamps. `None` if the power is used or
    /// for identities (see [`CdlOpData::identity_replacement`]).
    pub fn simpler_replacement(&self) -> Option<OpVec> {
        if !channel_params_equal(&self.power, &[1.0; 3]) || self.is_identity() {
            return None;
        }
        let dir = self.direction();
        let clamp = || RangeOpData::new(0.0, 1.0, 0.0, 1.0).ok();
        let mut ops = OpVec::new();

        // Slope + offset.
        let mut m44 = [0.0; 16];
        m44[0] = self.slope[0];
        m44[5] = self.slope[1];
        m44[10] = self.slope[2];
        m44[15] = 1.0;
        let offset4 = [self.offset[0], self.offset[1], self.offset[2], 0.0];
        create_matrix_op_from_data(
            &mut ops,
            &MatrixOpData::from_values(&m44, &offset4, dir),
            TransformDirection::Forward,
        )
        .ok()?;

        // Saturation.
        if self.saturation != 1.0 {
            if self.is_clamping() {
                // Same in both directions.
                create_range_op_from_data(&mut ops, &clamp()?, TransformDirection::Forward).ok()?;
            }
            let (m, o) = MatrixTransform::sat(self.saturation, &CDL_LUMA_COEFS);
            create_matrix_op_from_data(
                &mut ops,
                &MatrixOpData::from_values(&m, &o, dir),
                TransformDirection::Forward,
            )
            .ok()?;
        }

        // Clamping.
        if self.is_clamping() {
            create_range_op_from_data(&mut ops, &clamp()?, TransformDirection::Forward).ok()?;
        }

        if dir == TransformDirection::Inverse {
            ops.reverse();
        }
        Some(ops)
    }

    /// Slope as a string.
    pub fn slope_string(&self) -> String {
        params_string(&self.slope)
    }
    /// Offset as a string.
    pub fn offset_string(&self) -> String {
        params_string(&self.offset)
    }
    /// Power as a string.
    pub fn power_string(&self) -> String {
        params_string(&self.power)
    }
    /// Saturation as a string.
    pub fn saturation_string(&self) -> String {
        format_g(self.saturation, FLOAT_DECIMALS)
    }

    /// Cache id of the parameters.
    pub fn cache_id(&self) -> String {
        let mut s = String::new();
        if !self.id().is_empty() {
            s.push_str(self.id());
            s.push(' ');
        }
        s.push_str(self.style.as_str());
        s.push(' ');
        s.push_str(&self.slope_string());
        s.push(' ');
        s.push_str(&self.offset_string());
        s.push(' ');
        s.push_str(&self.power_string());
        s.push(' ');
        s.push_str(&self.saturation_string());
        s.push(' ');
        s
    }
}

// ---------------------------------------------------------------------------
// CPU renderer.

const RCP_MIN_VALUE: f32 = 1e-2;

fn reciprocal(x: f32) -> f32 {
    1.0 / if x < RCP_MIN_VALUE { RCP_MIN_VALUE } else { x }
}

/// Render parameters of the CDL (port of `RenderParams`).
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct CdlRenderParams {
    /// Slopes (inverted for the reverse styles).
    pub slope: [f32; 3],
    /// Offsets (negated for the reverse styles).
    pub offset: [f32; 3],
    /// Powers (inverted for the reverse styles).
    pub power: [f32; 3],
    /// Saturation (inverted for the reverse styles).
    pub saturation: f32,
    /// Reverse style.
    pub is_reverse: bool,
    /// Non clamping style.
    pub is_no_clamp: bool,
}

impl CdlRenderParams {
    /// Compute the render parameters.
    pub fn new(cdl: &CdlOpData) -> Self {
        let sat = cdl.saturation as f32;
        let is_reverse = cdl.is_reverse();
        let is_no_clamp = !cdl.is_clamping();
        if is_reverse {
            Self {
                slope: cdl.slope.map(|v| reciprocal(v as f32)),
                offset: cdl.offset.map(|v| (-v) as f32),
                power: cdl.power.map(|v| reciprocal(v as f32)),
                saturation: reciprocal(sat),
                is_reverse,
                is_no_clamp,
            }
        } else {
            Self {
                slope: cdl.slope.map(|v| v as f32),
                offset: cdl.offset.map(|v| v as f32),
                power: cdl.power.map(|v| v as f32),
                saturation: sat,
                is_reverse,
                is_no_clamp,
            }
        }
    }
}

#[inline]
fn apply_slope(p: &mut Pixel, s: &[f32; 3]) {
    p[0] *= s[0];
    p[1] *= s[1];
    p[2] *= s[2];
}

#[inline]
fn apply_offset(p: &mut Pixel, o: &[f32; 3]) {
    p[0] += o[0];
    p[1] += o[1];
    p[2] += o[2];
}

#[inline]
fn apply_saturation(p: &mut Pixel, sat: f32) {
    const LUMA: [f32; 3] = [0.2126, 0.7152, 0.0722];
    let src = [p[0], p[1], p[2]];
    let luma = src[0] * LUMA[0] + src[1] * LUMA[1] + src[2] * LUMA[2];
    for i in 0..3 {
        p[i] = luma + sat * (src[i] - luma);
    }
}

#[inline]
fn apply_clamp(p: &mut Pixel) {
    // NaNs become 0.
    for v in p.iter_mut().take(3) {
        *v = clamp_f32(*v, 0.0, 1.0);
    }
}

#[inline]
fn apply_power_clamp(p: &mut Pixel, power: &[f32; 3]) {
    apply_clamp(p);
    for i in 0..3 {
        p[i] = p[i].powf(power[i]);
    }
}

#[inline]
fn apply_power_no_clamp(p: &mut Pixel, power: &[f32; 3]) {
    // NaNs are set to 0, negative values are passed through.
    for i in 0..3 {
        let v = p[i];
        p[i] = if v.is_nan() {
            0.0
        } else if v < 0.0 {
            v
        } else {
            v.powf(power[i])
        };
    }
}

/// CPU renderer of the CDL op.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct CdlRenderer {
    params: CdlRenderParams,
}

impl CdlRenderer {
    /// Initialize the renderer.
    pub fn new(cdl: &CdlOpData) -> Self {
        Self {
            params: CdlRenderParams::new(cdl),
        }
    }

    /// The render parameters.
    pub fn params(&self) -> &CdlRenderParams {
        &self.params
    }

    /// Process pixels in place (alpha is not modified).
    pub fn apply(&self, pixels: &mut [Pixel]) {
        let p = &self.params;
        let clamp = !p.is_no_clamp;
        if !p.is_reverse {
            for px in pixels.iter_mut() {
                apply_slope(px, &p.slope);
                apply_offset(px, &p.offset);
                if clamp {
                    apply_power_clamp(px, &p.power);
                } else {
                    apply_power_no_clamp(px, &p.power);
                }
                apply_saturation(px, p.saturation);
                if clamp {
                    apply_clamp(px);
                }
            }
        } else {
            for px in pixels.iter_mut() {
                if clamp {
                    apply_clamp(px);
                }
                apply_saturation(px, p.saturation);
                if clamp {
                    apply_power_clamp(px, &p.power);
                } else {
                    apply_power_no_clamp(px, &p.power);
                }
                apply_offset(px, &p.offset);
                apply_slope(px, &p.slope);
                if clamp {
                    apply_clamp(px);
                }
            }
        }
    }
}

// Fast renderers (port of `CDLRendererFwdSSE` / `CDLRendererRevSSE`). They
// process the four channels as SSE lanes: the slope, offset and power of
// alpha are 1, 0 and 1, and alpha is restored at the end, but it goes
// through the power and its weight of 0 is part of the luma sum, so a non
// finite alpha makes the RGB channels NaN.

#[inline]
fn fast_clamp(p: &mut Pixel) {
    use crate::ops::sse;
    for v in p.iter_mut() {
        *v = sse::min(sse::max(*v, 0.0), 1.0);
    }
}

#[inline]
fn fast_power(p: &mut Pixel, power: &[f32; 4], clamp: bool) {
    use crate::ops::sse;
    if clamp {
        fast_clamp(p);
        for c in 0..4 {
            p[c] = sse::power(p[c], power[c]);
        }
    } else {
        // Negative values are passed through (a NaN gives 0).
        for c in 0..4 {
            let pix_power = sse::power(p[c], power[c]);
            if !(p[c] < 0.0) {
                p[c] = pix_power;
            }
        }
    }
}

#[inline]
fn fast_saturation(p: &mut Pixel, sat: f32) {
    let luma = crate::ops::sse::luma(p, &[0.2126, 0.7152, 0.0722]);
    for v in p.iter_mut() {
        *v = luma + sat * (*v - luma);
    }
}

impl CdlRenderer {
    /// Process pixels in place with the fast power of OCIO's SSE renderers
    /// (port of `CDLRendererFwdSSE` / `CDLRendererRevSSE`, used under
    /// `OptimizationFlags::FAST_LOG_EXP_POW`). Alpha is not modified.
    pub fn apply_fast(&self, pixels: &mut [Pixel]) {
        let p = &self.params;
        let clamp = !p.is_no_clamp;
        let slope = [p.slope[0], p.slope[1], p.slope[2], 1.0];
        let offset = [p.offset[0], p.offset[1], p.offset[2], 0.0];
        let power = [p.power[0], p.power[1], p.power[2], 1.0];
        for px in pixels.iter_mut() {
            let alpha = px[3];
            if !p.is_reverse {
                for c in 0..4 {
                    px[c] = px[c] * slope[c] + offset[c];
                }
                fast_power(px, &power, clamp);
                fast_saturation(px, p.saturation);
                if clamp {
                    fast_clamp(px);
                }
            } else {
                if clamp {
                    fast_clamp(px);
                }
                fast_saturation(px, p.saturation);
                fast_power(px, &power, clamp);
                for c in 0..4 {
                    px[c] = (px[c] + offset[c]) * slope[c];
                }
                if clamp {
                    fast_clamp(px);
                }
            }
            px[3] = alpha;
        }
    }
}

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

/// CDL op (port of OCIO's `CDLOp`).
#[derive(Debug, Clone)]
pub struct CdlOp {
    data: CdlOpData,
    renderer: CdlRenderer,
    /// Use the fast power (see [`CdlRenderer::apply_fast`]).
    fast_power: bool,
}

impl CdlOp {
    /// Create the op (the parameters are validated).
    pub fn new(data: CdlOpData) -> Result<Self> {
        data.validate()?;
        let renderer = CdlRenderer::new(&data);
        Ok(Self {
            data,
            renderer,
            fast_power: false,
        })
    }

    /// The parameters.
    pub fn data(&self) -> &CdlOpData {
        &self.data
    }
}

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

    fn apply(&self, pixels: &mut [Pixel]) {
        if self.fast_power {
            self.renderer.apply_fast(pixels);
        } else {
            self.renderer.apply(pixels);
        }
    }

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

    fn has_channel_crosstalk(&self) -> bool {
        self.data.has_channel_crosstalk()
    }

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

    fn cpu_op(&self, fast_log_exp_pow: bool) -> Option<OpRc> {
        fast_log_exp_pow.then(|| {
            Arc::new(Self {
                fast_power: true,
                ..self.clone()
            }) as OpRc
        })
    }

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

    fn combine_with(&self, next: &dyn Op, flags: OptimizationFlags) -> Option<OpVec> {
        if !flags.contains(OptimizationFlags::PAIR_IDENTITY_CDL) {
            return None;
        }
        let other = next.downcast_ref::<CdlOp>()?;
        if !self.data.is_inverse(&other.data) {
            return None;
        }
        let mut ops = OpVec::new();
        if let Some(range) = self.data.identity_replacement() {
            create_range_op_from_data(&mut ops, &range, TransformDirection::Forward).ok()?;
        }
        Some(ops)
    }

    fn simplify(&self, flags: OptimizationFlags) -> Option<OpVec> {
        if flags.contains(OptimizationFlags::IDENTITY) && self.data.is_identity() {
            let mut ops = OpVec::new();
            if let Some(range) = self.data.identity_replacement() {
                create_range_op_from_data(&mut ops, &range, TransformDirection::Forward).ok()?;
            }
            return Some(ops);
        }
        if flags.contains(OptimizationFlags::SIMPLIFY_OPS) {
            return self.data.simpler_replacement();
        }
        None
    }

    fn to_transform(&self) -> Option<Transform> {
        let d = &self.data;
        Some(Transform::Cdl(CdlTransform {
            direction: d.direction(),
            style: d.style.transform_style(),
            slope: d.slope,
            offset: d.offset,
            power: d.power,
            sat: d.saturation,
            metadata: d.metadata.clone(),
        }))
    }

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

// ---------------------------------------------------------------------------
// Op builders.

/// Append a CDL op built from `data` in direction `dir` (combined with the
/// direction of the data style).
pub fn create_cdl_op_from_data(
    ops: &mut OpVec,
    data: &CdlOpData,
    dir: TransformDirection,
) -> Result<()> {
    let cdl = match dir {
        TransformDirection::Forward => data.clone(),
        TransformDirection::Inverse => data.inverse(),
    };
    ops.push(Arc::new(CdlOp::new(cdl)?));
    Ok(())
}

/// Append a CDL op (port of `CreateCDLOp`).
pub fn create_cdl_op(
    ops: &mut OpVec,
    style: CdlOpStyle,
    slope3: &[f64; 3],
    offset3: &[f64; 3],
    power3: &[f64; 3],
    saturation: f64,
    dir: TransformDirection,
) -> Result<()> {
    let data = CdlOpData::new(style, *slope3, *offset3, *power3, saturation)?;
    create_cdl_op_from_data(ops, &data, dir)
}

/// Build the ops of a `CdlTransform` for a config of the given major version
/// (port of `BuildCDLOp`): v1 configs use a scale / offset matrix, a
/// (clamping) exponent and a saturation matrix.
pub fn build_cdl_ops(
    ops: &mut OpVec,
    transform: &CdlTransform,
    dir: TransformDirection,
    config_major_version: u32,
) -> Result<()> {
    if config_major_version == 1 {
        let combined = dir.combine(transform.direction);
        let s = &transform.slope;
        let o = &transform.offset;
        let p = &transform.power;
        let slope4 = [s[0], s[1], s[2], 1.0];
        let offset4 = [o[0], o[1], o[2], 0.0];
        let power4 = [p[0], p[1], p[2], 1.0];
        let luma = transform.sat_luma_coefs();
        let sat = transform.sat;
        match combined {
            TransformDirection::Forward => {
                // 1) Scale + Offset.
                create_scale_offset_op(ops, &slope4, &offset4, TransformDirection::Forward)?;
                // 2) Power + Clamp at 0 (NB: This is not in accord with the
                //    ASC v1.2 spec since it also requires clamping at 1).
                create_exponent_op(ops, &power4, TransformDirection::Forward)?;
                // 3) Saturation (NB: Does not clamp at 0 and 1 as per ASC v1.2 spec).
                create_saturation_op(ops, sat, &luma, TransformDirection::Forward)
            }
            TransformDirection::Inverse => {
                create_saturation_op(ops, sat, &luma, TransformDirection::Inverse)?;
                create_exponent_op(ops, &power4, TransformDirection::Inverse)?;
                create_scale_offset_op(ops, &slope4, &offset4, TransformDirection::Inverse)
            }
        }
    } else {
        // Starting with the version 2, OCIO uses a CDL op complying with the
        // Common LUT Format (i.e. CLF) specification.
        transform.validate()?;
        create_cdl_op_from_data(ops, &CdlOpData::from_transform(transform), dir)
    }
}

// ---------------------------------------------------------------------------
// CdlTransform.

impl CdlTransform {
    /// Equality as defined by OCIO (1e-9 tolerance on the SOP values,
    /// metadata ignored).
    pub fn equals(&self, other: &CdlTransform) -> bool {
        CdlOpData::from_transform(self) == CdlOpData::from_transform(other)
    }

    /// Remove the first `SOPDescription` child element (OCIO's
    /// `setFirstSOPDescription(nullptr)`).
    pub fn clear_first_sop_description(&mut self) {
        if let Some(i) = self.metadata.first_child_index(METADATA_SOP_DESCRIPTION) {
            self.metadata.children.remove(i);
        }
    }
}

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

impl BuildOps for CdlTransform {
    fn build_ops(
        &self,
        ops: &mut OpVec,
        config: &Config,
        _context: &Context,
        dir: TransformDirection,
    ) -> Result<()> {
        build_cdl_ops(ops, self, dir, config_major_version(config))
    }
}

#[cfg(test)]
mod tests;