ocio 0.3.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
// SPDX-License-Identifier: BSD-3-Clause
// Copyright Contributors to the OpenColorIO Project.
// Copyright (c) 2026 Philippe Llerena.

//! 3D LUT op (port of `Lut3DOpData.cpp`, `Lut3DOp.cpp`, `Lut3DOpCPU.cpp` and
//! `Lut3DTransform.cpp`).
//!
//! Values are stored with the **blue** index changing fastest:
//! `index = ((r * n + g) * n + b) * 3`.
//!
//! Forward evaluation is trilinear or tetrahedral. The exact inverse (CPU
//! only) searches the LUT cubes containing the value with a range tree and
//! inverts the tetrahedral interpolation; the fast inverse bakes the exact
//! inverse into a forward 48^3 LUT (see [`make_fast_lut3d_from_inverse`]).

mod cpu;
#[cfg(test)]
mod tests;

use self::cpu::Lut3DRenderer;
use crate::config::Config;
use crate::context::Context;
use crate::error::{Error, Result};
use crate::format_metadata::FormatMetadata;
use crate::ops::lut1d::{
    eval_transform, format_float_g, identity_replacement_ops, rgb_min_max, IdentityReplacement,
};
use crate::ops::{hash_f32, Op, OpRc, OpVec, Pixel};
use crate::transforms::{BuildOps, Lut3DTransform, Transform, Validate};
use crate::types::{BitDepth, Interpolation, OptimizationFlags, TransformDirection};
use std::any::Any;
use std::fmt;
use std::ops::{Index, IndexMut};
use std::sync::{Arc, OnceLock};

/// Maximum grid size of a 3D LUT (`Max3DLUTLength` in OCIO).
pub const MAX_3D_LUT_LENGTH: usize = 129;

/// Grid size of the forward LUT used by the fast inverse.
pub const FAST_INVERSE_GRID_SIZE: usize = 48;

// ---------------------------------------------------------------------------
// Index helpers.

/// Index of a red-fastest 3D LUT entry.
pub fn get_lut3d_index_red_fast(
    r: usize,
    g: usize,
    b: usize,
    size_r: usize,
    size_g: usize,
    _size_b: usize,
) -> usize {
    3 * (r + size_r * (g + size_g * b))
}

/// Index of a blue-fastest 3D LUT entry.
pub fn get_lut3d_index_blue_fast(
    r: usize,
    g: usize,
    b: usize,
    _size_r: usize,
    size_g: usize,
    size_b: usize,
) -> usize {
    3 * (b + size_b * (g + size_g * r))
}

/// Ordering of 3D LUT values.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Lut3DOrder {
    /// Red changes fastest.
    FastRed,
    /// Blue changes fastest.
    FastBlue,
}

/// Fill `img` with an identity 3D LUT of `edge_len`^3 pixels of
/// `num_channels` (at least 3) in the given order.
pub fn generate_identity_lut3d(
    img: &mut [f32],
    edge_len: usize,
    num_channels: usize,
    order: Lut3DOrder,
) -> Result<()> {
    if num_channels < 3 {
        crate::bail!("Cannot generate idenitity 3d LUT with less than 3 channels.");
    }
    let total = edge_len * edge_len * edge_len;
    if img.len() < total * num_channels {
        crate::bail!(
            "Cannot generate identity 3d LUT: buffer of {} values is too small for {} values.",
            img.len(),
            total * num_channels
        );
    }
    let c = 1.0f32 / (edge_len as f32 - 1.0);
    for i in 0..total {
        let (r, g, b) = match order {
            Lut3DOrder::FastRed => (
                i % edge_len,
                (i / edge_len) % edge_len,
                (i / edge_len / edge_len) % edge_len,
            ),
            Lut3DOrder::FastBlue => (
                (i / edge_len / edge_len) % edge_len,
                (i / edge_len) % edge_len,
                i % edge_len,
            ),
        };
        img[num_channels * i] = r as f32 * c;
        img[num_channels * i + 1] = g as f32 * c;
        img[num_channels * i + 2] = b as f32 * c;
    }
    Ok(())
}

/// Infer the edge length of a cube from its number of pixels.
pub fn get_3d_lut_edge_len_from_num_pixels(num_pixels: usize) -> Result<usize> {
    let dim = (num_pixels as f32).powf(1.0 / 3.0).round() as usize;
    if dim.checked_mul(dim).and_then(|d| d.checked_mul(dim)) != Some(num_pixels) {
        crate::bail!(
            "Cannot infer 3D LUT size. {num_pixels} element(s) does not correspond to a unform cube edge length. \
             (nearest edge length is {dim})."
        );
    }
    Ok(dim)
}

// ---------------------------------------------------------------------------
// Array.

/// The values of a 3D LUT (port of `Lut3DOpData::Lut3DArray`), blue fastest.
#[derive(Clone, PartialEq)]
pub struct Lut3DArray {
    length: usize,
    values: Vec<f32>,
}

impl fmt::Debug for Lut3DArray {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Lut3DArray")
            .field("length", &self.length)
            .finish_non_exhaustive()
    }
}

impl Lut3DArray {
    /// Identity array of the given grid size.
    pub fn new(length: usize) -> Result<Self> {
        let mut a = Self {
            length: 0,
            values: Vec::new(),
        };
        a.resize(length, 3)?;
        a.fill();
        Ok(a)
    }

    fn fill(&mut self) {
        let length = self.length;
        let step = 1.0f32 / (length as f32 - 1.0);
        let max_entries = length * length * length;
        for idx in 0..max_entries {
            self.values[3 * idx] = ((idx / length / length) % length) as f32 * step;
            self.values[3 * idx + 1] = ((idx / length) % length) as f32 * step;
            self.values[3 * idx + 2] = (idx % length) as f32 * step;
        }
    }

    /// Resize the array (new values are zeros). The number of color
    /// components is always 3.
    pub fn resize(&mut self, length: usize, _num_color_components: usize) -> Result<()> {
        if length > MAX_3D_LUT_LENGTH {
            crate::bail!(
                "LUT 3D: Grid size '{length}' must not be greater than '{MAX_3D_LUT_LENGTH}'."
            );
        }
        self.length = length;
        self.values.resize(self.num_values(), 0.0);
        Ok(())
    }

    /// Grid size.
    pub fn length(&self) -> usize {
        self.length
    }

    /// Always 3.
    pub fn num_color_components(&self) -> usize {
        3
    }

    /// Always 3.
    pub fn max_color_components(&self) -> usize {
        3
    }

    /// Expected number of values (`length^3 * 3`).
    pub fn num_values(&self) -> usize {
        self.length * self.length * self.length * 3
    }

    /// The LUT values (RGB triplets, blue fastest).
    pub fn values(&self) -> &[f32] {
        &self.values
    }

    /// The LUT values, mutable.
    pub fn values_mut(&mut self) -> &mut Vec<f32> {
        &mut self.values
    }

    /// Check the array consistency.
    pub fn validate(&self) -> Result<()> {
        if self.length == 0 {
            crate::bail!("Array content is empty.");
        }
        if self.values.len() != self.num_values() {
            crate::bail!(
                "Array contains: {} values, but {} are expected.",
                self.values.len(),
                self.num_values()
            );
        }
        Ok(())
    }

    /// RGB value at grid indices `(i, j, k)` (red, green, blue).
    pub(crate) fn rgb(&self, i: usize, j: usize, k: usize) -> [f32; 3] {
        let off = (i * self.length * self.length + j * self.length + k) * 3;
        [self.values[off], self.values[off + 1], self.values[off + 2]]
    }

    /// Set the RGB value at grid indices `(i, j, k)`.
    pub(crate) fn set_rgb(&mut self, i: usize, j: usize, k: usize, rgb: [f32; 3]) {
        let off = (i * self.length * self.length + j * self.length + k) * 3;
        self.values[off..off + 3].copy_from_slice(&rgb);
    }

    /// Multiply all the values.
    pub fn scale(&mut self, scale: f32) {
        if scale != 1.0 {
            for v in &mut self.values {
                *v *= scale;
            }
        }
    }
}

impl Index<usize> for Lut3DArray {
    type Output = f32;
    fn index(&self, i: usize) -> &f32 {
        &self.values[i]
    }
}

impl IndexMut<usize> for Lut3DArray {
    fn index_mut(&mut self, i: usize) -> &mut f32 {
        &mut self.values[i]
    }
}

// ---------------------------------------------------------------------------
// Op data.

/// Parameters of a 3D LUT (port of `Lut3DOpData`).
#[derive(Clone)]
pub struct Lut3DOpData {
    interpolation: Interpolation,
    array: Lut3DArray,
    direction: TransformDirection,
    file_out_bit_depth: BitDepth,
    metadata: FormatMetadata,
}

impl fmt::Debug for Lut3DOpData {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Lut3DOpData")
            .field("direction", &self.direction)
            .field("interpolation", &self.interpolation)
            .field("array", &self.array)
            .field("file_out_bit_depth", &self.file_out_bit_depth)
            .finish_non_exhaustive()
    }
}

/// Equality as OCIO's `Lut3DOpData::equals`: same direction, interpolation
/// and array (metadata ignored).
impl PartialEq for Lut3DOpData {
    fn eq(&self, other: &Self) -> bool {
        self.direction == other.direction
            && self.interpolation == other.interpolation
            && self.array == other.array
    }
}

impl Lut3DOpData {
    /// Identity LUT of the given grid size.
    pub fn new(grid_size: usize) -> Result<Self> {
        Self::with_interpolation(Interpolation::Default, grid_size)
    }

    /// Identity LUT with the given direction.
    pub fn with_direction(grid_size: usize, dir: TransformDirection) -> Result<Self> {
        let mut l = Self::new(grid_size)?;
        l.direction = dir;
        Ok(l)
    }

    /// Identity LUT with the given interpolation.
    pub fn with_interpolation(interpolation: Interpolation, grid_size: usize) -> Result<Self> {
        Ok(Self {
            interpolation,
            array: Lut3DArray::new(grid_size)?,
            direction: TransformDirection::Forward,
            file_out_bit_depth: BitDepth::Unknown,
            metadata: FormatMetadata::default(),
        })
    }

    /// Build from a transform (no validation of the interpolation).
    pub fn from_transform(t: &Lut3DTransform) -> Result<Self> {
        if t.grid_size > MAX_3D_LUT_LENGTH {
            crate::bail!(
                "LUT 3D: Grid size '{}' must not be greater than '{MAX_3D_LUT_LENGTH}'.",
                t.grid_size
            );
        }
        Ok(Self {
            interpolation: t.interpolation,
            array: Lut3DArray {
                length: t.grid_size,
                values: t.values.clone(),
            },
            direction: t.direction,
            file_out_bit_depth: t.file_output_bit_depth,
            metadata: t.metadata.clone(),
        })
    }

    /// Convert to a transform.
    pub fn to_transform(&self) -> Lut3DTransform {
        Lut3DTransform {
            direction: self.direction,
            grid_size: self.array.length(),
            values: self.array.values.clone(),
            interpolation: self.interpolation,
            file_output_bit_depth: self.file_out_bit_depth,
            metadata: self.metadata.clone(),
        }
    }

    /// The requested interpolation.
    pub fn interpolation(&self) -> Interpolation {
        self.interpolation
    }

    /// The interpolation actually used.
    pub fn concrete_interpolation(&self) -> Interpolation {
        Self::get_concrete_interpolation(self.interpolation)
    }

    /// Best and tetrahedral map to tetrahedral, everything else to linear
    /// (OCIO v2 implements nearest as trilinear; invalid interpolations make
    /// `validate` fail).
    pub fn get_concrete_interpolation(interp: Interpolation) -> Interpolation {
        match interp {
            Interpolation::Best | Interpolation::Tetrahedral => Interpolation::Tetrahedral,
            _ => Interpolation::Linear,
        }
    }

    /// Set the interpolation.
    pub fn set_interpolation(&mut self, interpolation: Interpolation) {
        self.interpolation = interpolation;
    }

    /// Best, tetrahedral, default, linear and nearest are valid.
    pub fn is_valid_interpolation(interpolation: Interpolation) -> bool {
        matches!(
            interpolation,
            Interpolation::Best
                | Interpolation::Tetrahedral
                | Interpolation::Default
                | Interpolation::Linear
                | Interpolation::Nearest
        )
    }

    /// The direction.
    pub fn direction(&self) -> TransformDirection {
        self.direction
    }

    /// Set the direction.
    pub fn set_direction(&mut self, dir: TransformDirection) {
        self.direction = dir;
    }

    /// The LUT values.
    pub fn array(&self) -> &Lut3DArray {
        &self.array
    }

    /// The LUT values, mutable.
    pub fn array_mut(&mut self) -> &mut Lut3DArray {
        &mut self.array
    }

    /// Grid size.
    pub fn grid_size(&self) -> usize {
        self.array.length()
    }

    /// Set the values from a red-fastest array.
    pub fn set_array_from_red_fastest_order(&mut self, lut: &[f32]) -> Result<()> {
        let n = self.array.length();
        if n * n * n * 3 != lut.len() {
            crate::bail!(
                "Lut3D length '{n} * {n} * {n} * 3' does not match the vector size '{}'.",
                lut.len()
            );
        }
        if self.array.values.len() != n * n * n * 3 {
            self.array.values.resize(n * n * n * 3, 0.0);
        }
        for b in 0..n {
            for g in 0..n {
                for r in 0..n {
                    let blue_fast = 3 * ((r * n + g) * n + b);
                    let red_fast = 3 * ((b * n + g) * n + r);
                    self.array.values[blue_fast..blue_fast + 3]
                        .copy_from_slice(&lut[red_fast..red_fast + 3]);
                }
            }
        }
        Ok(())
    }

    /// Structural checks needed before evaluating the LUT.
    ///
    /// Note: unlike OCIO, a grid size of 1 is rejected (the inverse
    /// evaluation would not terminate).
    pub fn check_structure(&self) -> Result<()> {
        if let Err(e) = self.array.validate() {
            crate::bail!("Lut3D content array issue: {}", e.message());
        }
        if self.array.length() > MAX_3D_LUT_LENGTH {
            crate::bail!("Lut3D length: {} is not supported. ", self.array.length());
        }
        if self.array.length() < 2 {
            crate::bail!("Lut3D grid size needs to be at least 2.");
        }
        Ok(())
    }

    /// Validate the parameters.
    pub fn validate(&self) -> Result<()> {
        if !Self::is_valid_interpolation(self.interpolation) {
            crate::bail!(
                "Lut3D does not support interpolation algorithm: {}.",
                self.interpolation.as_str()
            );
        }
        self.check_structure()
    }

    /// A 3D LUT clamps to its domain, so it is never a no-op.
    pub fn is_no_op(&self) -> bool {
        false
    }

    /// A 3D LUT clamps to its domain, so it is never an identity.
    pub fn is_identity(&self) -> bool {
        false
    }

    /// Always true.
    pub fn has_channel_crosstalk(&self) -> bool {
        true
    }

    /// Replacement of an identity 3D LUT (a clamp to `[0, 1]`).
    pub fn identity_replacement(&self) -> IdentityReplacement {
        IdentityReplacement::Clamp { min: 0.0, max: 1.0 }
    }

    /// Copy with the direction flipped.
    pub fn inverse(&self) -> Lut3DOpData {
        let mut inv = self.clone();
        inv.direction = self.direction.inverse();
        inv
    }

    /// True if `other` is the inverse of `self` (same array, opposite
    /// direction).
    pub fn is_inverse(&self, other: &Lut3DOpData) -> bool {
        self.direction != other.direction && self.array == other.array
    }

    /// The original scaling of the LUT values.
    pub fn file_output_bit_depth(&self) -> BitDepth {
        self.file_out_bit_depth
    }

    /// Record the original scaling of the LUT values.
    pub fn set_file_output_bit_depth(&mut self, bd: BitDepth) {
        self.file_out_bit_depth = bd;
    }

    /// The format metadata.
    pub fn format_metadata(&self) -> &FormatMetadata {
        &self.metadata
    }

    /// The format metadata, mutable.
    pub fn format_metadata_mut(&mut self) -> &mut FormatMetadata {
        &mut self.metadata
    }

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

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

    /// Set the `name` metadata attribute.
    pub fn set_name(&mut self, name: &str) {
        self.metadata.set_name(name);
    }

    /// Multiply all the values.
    pub fn scale(&mut self, scale: f32) {
        self.array.scale(scale);
    }

    /// Cache identifier.
    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(&format!(
            "{} {} {} ",
            hash_f32(self.array.values()),
            self.interpolation.as_str(),
            self.direction.as_str()
        ));
        s
    }

    /// Functional composition of two 3D LUTs (port of
    /// `Lut3DOpData::Compose`). The result is at least as big as either LUT.
    pub fn compose(lutc1: &Lut3DOpData, lutc2: &Lut3DOpData) -> Result<Lut3DOpData> {
        let mut lut1 = lutc1.clone();
        let mut lut2 = lutc2.clone();
        let mut restore_inverse = false;
        if lut1.direction == TransformDirection::Inverse
            && lut2.direction == TransformDirection::Inverse
        {
            // Using the fact that: inv(l2 x l1) = inv(l1) x inv(l2).
            std::mem::swap(&mut lut1, &mut lut2);
            lut1.direction = TransformDirection::Forward;
            lut2.direction = TransformDirection::Forward;
            restore_inverse = true;
        }

        let min_sz = lut2.array.length();
        let n = lut1.array.length();
        let domain_size = min_sz.max(n);
        let mut ops = OpVec::new();

        let mut result = if n >= min_sz && lut1.direction != TransformDirection::Inverse {
            // The range of the first LUT becomes the domain to interp in the
            // second. Use the original domain.
            lut1.clone()
        } else {
            // Since the 2nd LUT is more finely sampled, use its grid size and
            // interpolate through both LUTs.
            let mut r = Lut3DOpData::with_interpolation(lut1.interpolation, domain_size)?;
            r.metadata = lut1.metadata.clone();
            ops.push(Arc::new(Lut3DOp::new(lut1.clone())?));
            r
        };

        ops.push(Arc::new(Lut3DOp::new(lut2.clone())?));

        let file_out_bd = lut1.file_out_bit_depth;
        result.metadata.combine(&lut2.metadata);
        result.file_out_bit_depth = file_out_bd;

        eval_transform(&mut result.array.values, &ops);

        if restore_inverse {
            result.direction = TransformDirection::Inverse;
        }
        Ok(result)
    }
}

/// Make a forward 3D LUT (48^3) approximating the exact inverse of an inverse
/// LUT, for the fast inversion style (`OptimizationFlags::LUT_INV_FAST`).
pub fn make_fast_lut3d_from_inverse(lut: &Lut3DOpData) -> Result<Lut3DOpData> {
    if lut.direction() != TransformDirection::Inverse {
        crate::bail!("MakeFastLut3DFromInverse expects an inverse LUT");
    }
    // Note (OCIO TODO): the fast LUT limits inputs to [0,1].
    let mut new_domain = Lut3DOpData::new(FAST_INVERSE_GRID_SIZE)?;
    new_domain.set_file_output_bit_depth(lut.file_output_bit_depth());
    // Compose the new domain with the inverse LUT (exact inversion).
    Lut3DOpData::compose(&new_domain, lut)
}

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

/// A 3D LUT op. The CPU renderer is built on first use.
#[derive(Clone)]
pub struct Lut3DOp {
    data: Arc<Lut3DOpData>,
    renderer: OnceLock<Arc<Lut3DRenderer>>,
}

impl fmt::Debug for Lut3DOp {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Lut3DOp").field("data", &self.data).finish()
    }
}

impl Lut3DOp {
    /// Create an op (the data structure is checked).
    pub fn new(data: Lut3DOpData) -> Result<Self> {
        data.check_structure()?;
        Ok(Self {
            data: Arc::new(data),
            renderer: OnceLock::new(),
        })
    }

    /// The LUT data.
    pub fn data(&self) -> &Lut3DOpData {
        &self.data
    }

    fn renderer(&self) -> &Lut3DRenderer {
        self.renderer
            .get_or_init(|| Arc::new(Lut3DRenderer::new(&self.data)))
    }
}

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

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

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

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

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

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

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

    fn combine_with(&self, next: &dyn Op, flags: OptimizationFlags) -> Option<OpVec> {
        let other = next.downcast_ref::<Lut3DOp>()?;
        if flags.contains(OptimizationFlags::PAIR_IDENTITY_LUT3D)
            && self.data.is_inverse(&other.data)
        {
            // A forward + inverse pair still clamps to the LUT domain.
            if let Some(ops) = identity_replacement_ops(self.data.identity_replacement()) {
                return Some(ops);
            }
        }
        if flags.contains(OptimizationFlags::COMP_LUT3D) {
            let composed = Lut3DOpData::compose(&self.data, &other.data).ok()?;
            let op = Lut3DOp::new(composed).ok()?;
            return Some(vec![Arc::new(op)]);
        }
        None
    }

    fn to_transform(&self) -> Option<Transform> {
        Some(Transform::Lut3D(self.data.to_transform()))
    }

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

// ---------------------------------------------------------------------------
// Builders & helpers.

/// Append a 3D LUT op built from op data (port of `CreateLut3DOp`). With
/// `TransformDirection::Inverse`, the LUT direction is flipped.
pub fn create_lut3d_op_from_data(
    ops: &mut OpVec,
    lut: &Lut3DOpData,
    dir: TransformDirection,
) -> Result<()> {
    let data = match dir {
        TransformDirection::Forward => lut.clone(),
        TransformDirection::Inverse => lut.inverse(),
    };
    ops.push(Arc::new(Lut3DOp::new(data)?));
    Ok(())
}

/// Append the op of a [`Lut3DTransform`] (validated) in the requested
/// direction (port of `BuildLut3DOp`).
pub fn create_lut3d_op(
    ops: &mut OpVec,
    lut: &Lut3DTransform,
    dir: TransformDirection,
) -> Result<()> {
    let data = Lut3DOpData::from_transform(lut)?;
    data.validate()?;
    create_lut3d_op_from_data(ops, &data, dir)
}

/// Bake an op list into a 3D LUT of the given grid size by evaluating an
/// identity lattice through the ops.
pub fn bake_ops_to_lut3d(ops: &[OpRc], grid_size: usize) -> Result<Lut3DOpData> {
    let mut lut = Lut3DOpData::new(grid_size)?;
    lut.check_structure()?;
    eval_transform(&mut lut.array.values, ops);
    Ok(lut)
}

// ---------------------------------------------------------------------------
// Transform.

impl Validate for Lut3DTransform {
    fn validate(&self) -> Result<()> {
        let res = Lut3DOpData::from_transform(self).and_then(|d| d.validate());
        res.map_err(|e| Error::msg(format!("Lut3DTransform validation failed: {}", e.message())))
    }
}

impl BuildOps for Lut3DTransform {
    fn build_ops(
        &self,
        ops: &mut OpVec,
        _config: &Config,
        _context: &Context,
        dir: TransformDirection,
    ) -> Result<()> {
        create_lut3d_op(ops, self, dir)
    }
}

impl fmt::Display for Lut3DTransform {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "<Lut3DTransform direction={}, fileoutdepth={}, interpolation={}, ",
            self.direction.as_str(),
            self.file_output_bit_depth.as_str(),
            self.interpolation.as_str()
        )?;
        let l = self.grid_size;
        write!(f, "gridSize={l}, ")?;
        if l > 0 {
            let n = (l * l * l * 3).min(self.values.len());
            let (mn, mx) = rgb_min_max(&self.values[..n]);
            write!(
                f,
                "minrgb=[{}, {}, {}], maxrgb=[{}, {}, {}]",
                format_float_g(mn[0]),
                format_float_g(mn[1]),
                format_float_g(mn[2]),
                format_float_g(mx[0]),
                format_float_g(mx[1]),
                format_float_g(mx[2])
            )?;
        }
        write!(f, ">")
    }
}