ocio 0.3.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
//! Primary grading op (port of `GradingPrimary.cpp`, `GradingPrimaryOpData.cpp`,
//! `GradingPrimaryOp.cpp`, `GradingPrimaryOpCPU.cpp` and
//! `GradingPrimaryTransform.cpp`).
//!
//! The module also hosts [`GradingValue`], the value holder shared by all the
//! grading ops: it keeps either a static value or a dynamic property, together
//! with the values precomputed for rendering, which are refreshed lazily when
//! the dynamic property changes.

use crate::config::Config;
use crate::context::Context;
use crate::dynamic_property::{DynamicProperty, SharedValue};
use crate::error::Result;
use crate::format_metadata::FormatMetadata;
use crate::ops::{sse, Op, OpRc, OpVec, Pixel};
use crate::transforms::grading::GradingPrimary;
use crate::transforms::{BuildOps, GradingPrimaryTransform, RangeTransform, Transform, Validate};
use crate::types::{DynamicPropertyType, GradingStyle, OptimizationFlags, TransformDirection};
use std::any::Any;
use std::fmt;
use std::sync::{Arc, Mutex, MutexGuard};

// ---------------------------------------------------------------------------
// Shared value holder for the grading ops.

/// Value of a grading op: a static value, or a dynamic property, together with
/// the precomputed render values (`P`) of the last valid value.
///
/// When the value is dynamic, [`GradingValue::current`] compares the
/// property value with the cached one and, when it changed, validates and
/// precomputes the new value. An invalid value (which OCIO would reject in
/// `DynamicProperty*::setValue`) is ignored and the last valid value keeps
/// being used.
#[derive(Debug)]
pub(crate) struct GradingValue<V, P> {
    property: Option<SharedValue<V>>,
    state: Mutex<Arc<(V, P)>>,
}

impl<V: Clone + PartialEq, P> GradingValue<V, P> {
    /// A new holder. `pre` must be the precomputation of `value`.
    pub(crate) fn new(value: V, pre: P, dynamic: bool) -> Self {
        let property = if dynamic {
            Some(SharedValue::new(value.clone()))
        } else {
            None
        };
        Self {
            property,
            state: Mutex::new(Arc::new((value, pre))),
        }
    }

    fn lock(&self) -> MutexGuard<'_, Arc<(V, P)>> {
        self.state.lock().unwrap_or_else(|e| e.into_inner())
    }

    /// True if the value is held by a dynamic property.
    pub(crate) fn is_dynamic(&self) -> bool {
        self.property.is_some()
    }

    /// The dynamic property handle, if dynamic.
    pub(crate) fn property(&self) -> Option<&SharedValue<V>> {
        self.property.as_ref()
    }

    /// Replace the dynamic property handle (only if already dynamic).
    pub(crate) fn replace_property(&mut self, prop: &SharedValue<V>) {
        if self.property.is_some() {
            self.property = Some(prop.clone());
        }
    }

    /// Current value and its precomputed render values. `compute` validates
    /// and precomputes a new value of the dynamic property.
    pub(crate) fn current(&self, compute: impl FnOnce(&V) -> Result<P>) -> Arc<(V, P)> {
        let mut guard = self.lock();
        if let Some(prop) = &self.property {
            let changed = prop.with(|v| *v != guard.0);
            if changed {
                let v = prop.get();
                if let Ok(p) = compute(&v) {
                    *guard = Arc::new((v, p));
                }
            }
        }
        guard.clone()
    }

    /// Static copy holding the current state.
    pub(crate) fn to_static(&self, compute: impl FnOnce(&V) -> Result<P>) -> Self {
        let state = self.current(compute);
        Self {
            property: None,
            state: Mutex::new(state),
        }
    }
}

impl<V, P> Clone for GradingValue<V, P> {
    fn clone(&self) -> Self {
        let state = self.state.lock().unwrap_or_else(|e| e.into_inner()).clone();
        Self {
            property: self.property.clone(),
            state: Mutex::new(state),
        }
    }
}

/// `std::min` semantic (returns `a` if the comparison fails, e.g. NaN).
#[inline]
pub(crate) fn std_min(a: f32, b: f32) -> f32 {
    if b < a {
        b
    } else {
        a
    }
}

/// `std::max` semantic (returns `a` if the comparison fails, e.g. NaN).
#[inline]
pub(crate) fn std_max(a: f32, b: f32) -> f32 {
    if a < b {
        b
    } else {
        a
    }
}

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

/// Values precomputed from a [`GradingPrimary`] for rendering (port of
/// `GradingPrimaryPreRender`). The values are already inverted according to
/// the direction.
#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub struct GradingPrimaryPreRender {
    brightness: [f32; 3],
    contrast: [f32; 3],
    gamma: [f32; 3],
    exposure: [f32; 3],
    offset: [f32; 3],
    slope: [f32; 3],
    pivot: f64,
    is_power_identity: bool,
    local_bypass: bool,
}

impl GradingPrimaryPreRender {
    /// Precomputed values for `v` in `style` and direction `dir`.
    pub fn new(style: GradingStyle, dir: TransformDirection, v: &GradingPrimary) -> Self {
        let mut p = Self::default();
        p.update(style, dir, v);
        p
    }

    /// Do not apply the op if all params are identity.
    pub fn local_bypass(&self) -> bool {
        self.local_bypass
    }
    /// Brightness per channel (log style).
    pub fn brightness(&self) -> [f32; 3] {
        self.brightness
    }
    /// Contrast per channel (log and lin styles).
    pub fn contrast(&self) -> [f32; 3] {
        self.contrast
    }
    /// Gamma exponent per channel (log and video styles).
    pub fn gamma(&self) -> [f32; 3] {
        self.gamma
    }
    /// Contrast pivot (log and lin styles).
    pub fn pivot(&self) -> f64 {
        self.pivot
    }
    /// True if the gamma is identity (log and video styles).
    pub fn is_gamma_identity(&self) -> bool {
        self.is_power_identity
    }
    /// Exposure gain per channel (lin style).
    pub fn exposure(&self) -> [f32; 3] {
        self.exposure
    }
    /// Offset per channel (lin and video styles).
    pub fn offset(&self) -> [f32; 3] {
        self.offset
    }
    /// True if the contrast is identity (lin style).
    pub fn is_contrast_identity(&self) -> bool {
        self.is_power_identity
    }
    /// Slope per channel (video style).
    pub fn slope(&self) -> [f32; 3] {
        self.slope
    }

    /// Recompute the values.
    pub fn update(&mut self, style: GradingStyle, dir: TransformDirection, v: &GradingPrimary) {
        self.local_bypass = v.saturation == 1.0
            && v.clamp_black == GradingPrimary::NO_CLAMP_BLACK
            && v.clamp_white == GradingPrimary::NO_CLAMP_WHITE;

        let fwd = dir == TransformDirection::Forward;
        match style {
            GradingStyle::Log => {
                let b = &v.brightness;
                let c = &v.contrast;
                let g = &v.gamma;
                let br = [
                    ((b.master + b.red) * 6.25 / 1023.0) as f32,
                    ((b.master + b.green) * 6.25 / 1023.0) as f32,
                    ((b.master + b.blue) * 6.25 / 1023.0) as f32,
                ];
                let cs = [c.master * c.red, c.master * c.green, c.master * c.blue];
                let gs = [g.master * g.red, g.master * g.green, g.master * g.blue];
                if fwd {
                    self.brightness = br;
                    self.contrast = cs.map(|x| x as f32);
                    self.gamma = gs.map(|x| (1.0 / x) as f32);
                } else {
                    self.brightness = br.map(|x| -x);
                    self.contrast = cs.map(|x| (1.0 / if x == 0.0 { 1.0 } else { x }) as f32);
                    self.gamma = gs.map(|x| x as f32);
                }
                self.is_power_identity = self.gamma == [1.0; 3];
                self.pivot = 0.5 + v.pivot * 0.5;
                self.local_bypass = self.local_bypass
                    && self.is_power_identity
                    && self.brightness == [0.0; 3]
                    && self.contrast == [1.0; 3];
            }
            GradingStyle::Lin => {
                let o = &v.offset;
                let e = &v.exposure;
                let c = &v.contrast;
                let os = [
                    (o.master + o.red) as f32,
                    (o.master + o.green) as f32,
                    (o.master + o.blue) as f32,
                ];
                let es = [
                    (e.master + e.red) as f32,
                    (e.master + e.green) as f32,
                    (e.master + e.blue) as f32,
                ];
                let cs = [c.master * c.red, c.master * c.green, c.master * c.blue];
                if fwd {
                    self.offset = os;
                    self.exposure = es.map(|x| 2.0f32.powf(x));
                    self.contrast = cs.map(|x| x as f32);
                } else {
                    self.offset = os.map(|x| -x);
                    self.exposure = es.map(|x| 1.0 / 2.0f32.powf(x));
                    // Validate ensures contrast is above a threshold.
                    self.contrast = cs.map(|x| (1.0 / x) as f32);
                }
                self.is_power_identity = self.contrast == [1.0; 3];
                self.pivot = 0.18 * 2.0f64.powf(v.pivot);
                self.local_bypass = self.local_bypass
                    && self.is_power_identity
                    && self.exposure == [1.0; 3]
                    && self.offset == [0.0; 3];
            }
            GradingStyle::Video => {
                let o = &v.offset;
                let l = &v.lift;
                let g = &v.gamma;
                let non_zero = |x: f64| if x == 0.0 { 1.0 } else { x };
                let gain = [
                    non_zero(v.gain.master * v.gain.red),
                    non_zero(v.gain.master * v.gain.green),
                    non_zero(v.gain.master * v.gain.blue),
                ];
                let lift = [l.master + l.red, l.master + l.green, l.master + l.blue];
                // Summed left to right, as in OCIO.
                let off = [
                    o.master + o.red + l.master + l.red,
                    o.master + o.green + l.master + l.green,
                    o.master + o.blue + l.master + l.blue,
                ];
                let gs = [g.master * g.red, g.master * g.green, g.master * g.blue];
                let pw = v.pivot_white;
                let pb = v.pivot_black;
                if fwd {
                    for i in 0..3 {
                        self.offset[i] = off[i] as f32;
                        let slope_den = pw / gain[i] + lift[i] - pb;
                        self.slope[i] = ((pw - pb) / non_zero(slope_den)) as f32;
                        self.gamma[i] = (1.0 / gs[i]) as f32;
                    }
                } else {
                    for i in 0..3 {
                        self.offset[i] = -(off[i] as f32);
                        self.slope[i] = ((pw / gain[i] + (lift[i] - pb)) / (pw - pb)) as f32;
                        self.gamma[i] = gs[i] as f32;
                    }
                }
                self.is_power_identity = self.gamma == [1.0; 3];
                self.local_bypass = self.local_bypass
                    && self.is_power_identity
                    && self.slope == [1.0; 3]
                    && self.offset == [0.0; 3];
            }
        }
    }
}

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

/// Primary grading op (port of `GradingPrimaryOp` / `GradingPrimaryOpData`).
#[derive(Debug, Clone)]
pub struct GradingPrimaryOp {
    style: GradingStyle,
    direction: TransformDirection,
    value: GradingValue<GradingPrimary, GradingPrimaryPreRender>,
    metadata: FormatMetadata,
}

impl GradingPrimaryOp {
    /// Create the op. The value is validated.
    pub fn new(
        style: GradingStyle,
        value: GradingPrimary,
        direction: TransformDirection,
        dynamic: bool,
    ) -> Result<Self> {
        value.validate(style)?;
        let pre = compute_pre_render(style, direction, &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 = GradingPrimary::new(style);
        let pre = compute_pre_render(style, TransformDirection::Forward, &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;
    }

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

    fn state(&self) -> Arc<(GradingPrimary, GradingPrimaryPreRender)> {
        let (style, dir) = (self.style, self.direction);
        self.value.current(|v| {
            v.validate(style)?;
            Ok(compute_pre_render(style, dir, v))
        })
    }

    /// The same op in the opposite direction.
    pub fn inverse(&self) -> Self {
        let mut res = self.clone();
        res.direction = self.direction.inverse();
        let value = self.value();
        let pre = compute_pre_render(res.style, res.direction, &value);
        res.value = GradingValue {
            property: self.value.property.clone(),
            state: Mutex::new(Arc::new((value, pre))),
        };
        res
    }

    /// True if `other` is the inverse of `self` (never true for dynamic ops).
    pub fn is_inverse(&self, other: &GradingPrimaryOp) -> 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
    }

    /// The ops replacing the pair `self` + inverse: a range emulating the
    /// clamps, or nothing when there are no clamps. `None` if the range op
    /// cannot be created.
    fn identity_replacement(&self) -> Option<OpVec> {
        let v = self.value();
        let low = (v.clamp_black != GradingPrimary::NO_CLAMP_BLACK).then_some(v.clamp_black);
        let high = (v.clamp_white != GradingPrimary::NO_CLAMP_WHITE).then_some(v.clamp_white);
        if low.is_none() && high.is_none() {
            return Some(OpVec::new());
        }
        let range = Transform::Range(RangeTransform::new(low, high, low, high));
        let mut ops = OpVec::new();
        crate::transforms::build::build_ops(
            &mut ops,
            &Config::create_raw(),
            &Context::new(),
            &range,
            TransformDirection::Forward,
        )
        .ok()?;
        Some(ops)
    }

    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
    }
}

fn compute_pre_render(
    style: GradingStyle,
    dir: TransformDirection,
    v: &GradingPrimary,
) -> GradingPrimaryPreRender {
    GradingPrimaryPreRender::new(style, dir, v)
}

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

// ---------------------------------------------------------------------------
// CPU rendering
//
// Port of the `OCIO_USE_SSE2` branch of `GradingPrimaryOpCPU.cpp`, which
// OCIO's SSE2 builds always use: the four channels are processed as SSE
// lanes (the parameters of alpha are identities and alpha is restored at the
// end, but its weight of 0 is part of the luma sum), the powers are the fast
// `ssePower`, and the clamps follow `_mm_max_ps` / `_mm_min_ps` (a NaN
// becomes the black clamp).

/// Parameters of the RGB lanes, with `alpha` for the alpha lane.
#[inline]
fn lanes(v: [f32; 3], alpha: f32) -> [f32; 4] {
    [v[0], v[1], v[2], alpha]
}

#[inline]
fn apply_contrast(pix: &mut Pixel, contrast: &[f32; 4], pivot: f32) {
    for c in 0..4 {
        pix[c] = (pix[c] - pivot) * contrast[c] + pivot;
    }
}

#[inline]
fn apply_lin_contrast(pix: &mut Pixel, contrast: &[f32; 4], pivot: f32) {
    for c in 0..4 {
        let x = pix[c] / pivot;
        let sign = sse::sign_bits(x);
        pix[c] = sse::xor_sign(sse::power(sse::abs(x), contrast[c]) * pivot, sign);
    }
}

#[inline]
fn apply_slope(pix: &mut Pixel, slope: &[f32; 4]) {
    for c in 0..4 {
        pix[c] *= slope[c];
    }
}

#[inline]
fn apply_offset(pix: &mut Pixel, offset: &[f32; 4]) {
    for c in 0..4 {
        pix[c] += offset[c];
    }
}

#[inline]
fn apply_gamma(pix: &mut Pixel, gamma: &[f32; 4], black_pivot: f32, white_pivot: f32) {
    let range = white_pivot - black_pivot;
    for c in 0..4 {
        let x = pix[c] - black_pivot;
        let sign = sse::sign_bits(x);
        let y = sse::power(sse::abs(x) / range, gamma[c]);
        pix[c] = sse::xor_sign(y, sign) * range + black_pivot;
    }
}

#[inline]
fn apply_saturation(pix: &mut Pixel, saturation: f32) {
    let luma = sse::luma(pix, &[0.2126, 0.7152, 0.0722]);
    for c in 0..4 {
        pix[c] = luma + saturation * (pix[c] - luma);
    }
}

#[inline]
fn apply_clamp(pix: &mut Pixel, clamp_min: f32, clamp_max: f32) {
    for c in 0..4 {
        pix[c] = sse::min(sse::max(pix[c], clamp_min), clamp_max);
    }
}

/// The saturation of the forward renderers, if it is applied.
#[inline]
fn fwd_saturation(v: &GradingPrimary) -> Option<f32> {
    (v.saturation != 1.0).then_some(v.saturation as f32)
}

/// The inverse saturation of the reverse renderers, if it is applied.
#[inline]
fn rev_saturation(v: &GradingPrimary) -> Option<f32> {
    (v.saturation != 1.0 && v.saturation != 0.0).then(|| (1.0 / v.saturation) as f32)
}

/// Run `f` on each pixel, restoring alpha.
#[inline]
fn for_each_pixel(pixels: &mut [Pixel], mut f: impl FnMut(&mut Pixel)) {
    for out in pixels.iter_mut() {
        let alpha = out[3];
        f(out);
        out[3] = alpha;
    }
}

fn apply_log_fwd(v: &GradingPrimary, comp: &GradingPrimaryPreRender, pixels: &mut [Pixel]) {
    let brightness = lanes(comp.brightness, 0.0);
    let contrast = lanes(comp.contrast, 1.0);
    let gamma = lanes(comp.gamma, 1.0);
    let actual_pivot = comp.pivot as f32;
    let saturation = fwd_saturation(v);
    let pivot_black = v.pivot_black as f32;
    let pivot_white = v.pivot_white as f32;
    let clamp_black = v.clamp_black as f32;
    let clamp_white = v.clamp_white as f32;
    let gamma_identity = comp.is_gamma_identity();
    for_each_pixel(pixels, |out| {
        apply_offset(out, &brightness);
        apply_contrast(out, &contrast, actual_pivot);
        if !gamma_identity {
            apply_gamma(out, &gamma, pivot_black, pivot_white);
        }
        if let Some(sat) = saturation {
            apply_saturation(out, sat);
        }
        apply_clamp(out, clamp_black, clamp_white);
    });
}

fn apply_log_rev(v: &GradingPrimary, comp: &GradingPrimaryPreRender, pixels: &mut [Pixel]) {
    let brightness_inv = lanes(comp.brightness, 0.0);
    let contrast_inv = lanes(comp.contrast, 1.0);
    let gamma_inv = lanes(comp.gamma, 1.0);
    let pivot_black = v.pivot_black as f32;
    let pivot_white = v.pivot_white as f32;
    let clamp_black = v.clamp_black as f32;
    let clamp_white = v.clamp_white as f32;
    let actual_pivot = comp.pivot as f32;
    let sat_inv = rev_saturation(v);
    let gamma_identity = comp.is_gamma_identity();
    for_each_pixel(pixels, |out| {
        apply_clamp(out, clamp_black, clamp_white);
        if let Some(sat) = sat_inv {
            apply_saturation(out, sat);
        }
        if !gamma_identity {
            apply_gamma(out, &gamma_inv, pivot_black, pivot_white);
        }
        apply_contrast(out, &contrast_inv, actual_pivot);
        apply_offset(out, &brightness_inv);
    });
}

fn apply_lin_fwd(v: &GradingPrimary, comp: &GradingPrimaryPreRender, pixels: &mut [Pixel]) {
    let offset = lanes(comp.offset, 0.0);
    let exposure = lanes(comp.exposure, 1.0);
    let contrast = lanes(comp.contrast, 1.0);
    let actual_pivot = comp.pivot as f32;
    let saturation = fwd_saturation(v);
    let clamp_black = v.clamp_black as f32;
    let clamp_white = v.clamp_white as f32;
    let contrast_identity = comp.is_contrast_identity();
    for_each_pixel(pixels, |out| {
        apply_offset(out, &offset);
        apply_slope(out, &exposure);
        if !contrast_identity {
            apply_lin_contrast(out, &contrast, actual_pivot);
        }
        if let Some(sat) = saturation {
            apply_saturation(out, sat);
        }
        apply_clamp(out, clamp_black, clamp_white);
    });
}

fn apply_lin_rev(v: &GradingPrimary, comp: &GradingPrimaryPreRender, pixels: &mut [Pixel]) {
    let offset_inv = lanes(comp.offset, 0.0);
    let exposure_inv = lanes(comp.exposure, 1.0);
    let contrast_inv = lanes(comp.contrast, 1.0);
    let actual_pivot = comp.pivot as f32;
    let sat_inv = rev_saturation(v);
    let clamp_black = v.clamp_black as f32;
    let clamp_white = v.clamp_white as f32;
    // Note: without saturation, OCIO's renderer applies the contrast even
    // when it is an identity (the fast power is not an exact identity).
    let apply_contrast = !comp.is_contrast_identity() || sat_inv.is_none();
    for_each_pixel(pixels, |out| {
        apply_clamp(out, clamp_black, clamp_white);
        if let Some(sat) = sat_inv {
            apply_saturation(out, sat);
        }
        if apply_contrast {
            apply_lin_contrast(out, &contrast_inv, actual_pivot);
        }
        apply_slope(out, &exposure_inv);
        apply_offset(out, &offset_inv);
    });
}

fn apply_vid_fwd(v: &GradingPrimary, comp: &GradingPrimaryPreRender, pixels: &mut [Pixel]) {
    let gamma = lanes(comp.gamma, 1.0);
    let offset = lanes(comp.offset, 0.0);
    let slope = lanes(comp.slope, 1.0);
    let saturation = fwd_saturation(v);
    let clamp_black = v.clamp_black as f32;
    let clamp_white = v.clamp_white as f32;
    let pivot_black = v.pivot_black as f32;
    let pivot_white = v.pivot_white as f32;
    let gamma_identity = comp.is_gamma_identity();
    for_each_pixel(pixels, |out| {
        apply_offset(out, &offset);
        apply_contrast(out, &slope, pivot_black);
        if !gamma_identity {
            apply_gamma(out, &gamma, pivot_black, pivot_white);
        }
        if let Some(sat) = saturation {
            apply_saturation(out, sat);
        }
        apply_clamp(out, clamp_black, clamp_white);
    });
}

fn apply_vid_rev(v: &GradingPrimary, comp: &GradingPrimaryPreRender, pixels: &mut [Pixel]) {
    let gamma_inv = lanes(comp.gamma, 1.0);
    let offset_inv = lanes(comp.offset, 0.0);
    let slope_inv = lanes(comp.slope, 1.0);
    let pivot_black = v.pivot_black as f32;
    let pivot_white = v.pivot_white as f32;
    let clamp_black = v.clamp_black as f32;
    let clamp_white = v.clamp_white as f32;
    let sat_inv = rev_saturation(v);
    let gamma_identity = comp.is_gamma_identity();
    for_each_pixel(pixels, |out| {
        apply_clamp(out, clamp_black, clamp_white);
        if let Some(sat) = sat_inv {
            apply_saturation(out, sat);
        }
        if !gamma_identity {
            apply_gamma(out, &gamma_inv, pivot_black, pivot_white);
        }
        apply_contrast(out, &slope_inv, pivot_black);
        apply_offset(out, &offset_inv);
    });
}

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

    fn apply(&self, pixels: &mut [Pixel]) {
        let state = self.state();
        let (v, comp) = (&state.0, &state.1);
        if comp.local_bypass() {
            return;
        }
        match (self.style, self.direction) {
            (GradingStyle::Log, TransformDirection::Forward) => apply_log_fwd(v, comp, pixels),
            (GradingStyle::Log, TransformDirection::Inverse) => apply_log_rev(v, comp, pixels),
            (GradingStyle::Lin, TransformDirection::Forward) => apply_lin_fwd(v, comp, pixels),
            (GradingStyle::Lin, TransformDirection::Inverse) => apply_lin_rev(v, comp, pixels),
            (GradingStyle::Video, TransformDirection::Forward) => apply_vid_fwd(v, comp, pixels),
            (GradingStyle::Video, TransformDirection::Inverse) => apply_vid_rev(v, comp, pixels),
        }
    }

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

    fn is_identity(&self) -> bool {
        if self.is_dynamic() {
            return false;
        }
        let def = GradingPrimary::new(self.style);
        let v = self.value();
        if def.saturation == v.saturation
            && def.clamp_black == v.clamp_black
            && def.clamp_white == v.clamp_white
        {
            // Pivot values can be ignored if the other values are identity.
            match self.style {
                GradingStyle::Log => {
                    def.pivot_black == v.pivot_black
                        && def.pivot_white == v.pivot_white
                        && def.brightness == v.brightness
                        && def.contrast == v.contrast
                        && def.gamma == v.gamma
                }
                GradingStyle::Lin => {
                    def.contrast == v.contrast
                        && def.offset == v.offset
                        && def.exposure == v.exposure
                }
                GradingStyle::Video => {
                    def.gamma == v.gamma
                        && def.offset == v.offset
                        && def.lift == v.lift
                        && def.gain == v.gain
                }
            }
        } else {
            false
        }
    }

    fn has_channel_crosstalk(&self) -> bool {
        self.value().saturation != 1.0
    }

    fn cache_id(&self) -> String {
        format!("<GradingPrimaryOp {}>", 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::<GradingPrimaryOp>()?;
        if self.is_inverse(other) {
            self.identity_replacement()
        } else {
            None
        }
    }

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

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

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

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

    fn to_transform(&self) -> Option<Transform> {
        Some(Transform::GradingPrimary(GradingPrimaryTransform {
            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 GradingPrimaryTransform {
    /// 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 = GradingPrimary::new(style);
        }
    }

    /// Set the values (they are validated first).
    pub fn set_value(&mut self, value: GradingPrimary) -> Result<()> {
        value.validate(self.style)?;
        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 GradingPrimaryTransform {
    fn validate(&self) -> Result<()> {
        self.value
            .validate(self.style)
            .map_err(|e| e.prefixed("GradingPrimaryTransform validation failed: "))
    }
}

impl BuildOps for GradingPrimaryTransform {
    fn build_ops(
        &self,
        ops: &mut OpVec,
        _config: &Config,
        _context: &Context,
        dir: TransformDirection,
    ) -> Result<()> {
        let mut op = GradingPrimaryOp::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 GradingPrimaryTransform {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "<GradingPrimaryTransform 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)]
pub(crate) mod tests;