lcms2/
pipeline.rs

1use crate::eval::FloatOrU16;
2use crate::stage::{StageRef, StagesIter};
3use crate::{ffi, Error, LCMSResult};
4use foreign_types::{foreign_type, ForeignTypeRef};
5use std::fmt;
6use std::ptr;
7
8foreign_type! {
9    /// Pipelines are a convenient way to model complex operations on image data.
10    ///
11    /// Each pipeline may contain an arbitrary number of stages. Each stage performs a single operation.
12    /// Pipelines may be optimized to be executed on a certain format (8 bits, for example) and can be saved as LUTs in ICC profiles.
13    ///
14    /// This is an owned version of `PipelineRef`.
15    #[doc(hidden)]
16    pub unsafe type Pipeline {
17        type CType = ffi::Pipeline;
18        fn drop = ffi::cmsPipelineFree;
19        fn clone = ffi::cmsPipelineDup;
20    }
21}
22
23impl Pipeline {
24    /// Allocates an empty pipeline. Final Input and output channels must be specified at creation time.
25    pub fn new(input_channels: usize, output_channels: usize) -> LCMSResult<Self> {
26        unsafe {
27            Error::if_null(ffi::cmsPipelineAlloc(ptr::null_mut(), input_channels as u32, output_channels as u32))
28        }
29    }
30}
31
32impl PipelineRef {
33    /// Appends pipeline given as argument at the end of this pipeline. Channel count must match.
34    pub fn cat(&mut self, append: &PipelineRef) -> bool {
35        if append.input_channels() != self.output_channels() {
36            return false;
37        }
38        unsafe { ffi::cmsPipelineCat((self as *mut Self).cast(), append.as_ptr()) != 0 }
39    }
40
41    #[must_use]
42    pub fn stage_count(&self) -> usize {
43        unsafe { ffi::cmsPipelineStageCount(self.as_ptr()) as usize }
44    }
45
46    #[must_use]
47    pub fn first_stage(&self) -> Option<&StageRef> {
48        unsafe {
49            let f = ffi::cmsPipelineGetPtrToFirstStage(self.as_ptr());
50            if !f.is_null() {
51                Some(ForeignTypeRef::from_ptr(f))
52            } else {
53                None
54            }
55        }
56    }
57
58    #[must_use]
59    pub fn last_stage(&self) -> Option<&StageRef> {
60        unsafe {
61            let f = ffi::cmsPipelineGetPtrToLastStage(self.as_ptr());
62            if !f.is_null() {
63                Some(ForeignTypeRef::from_ptr(f))
64            } else {
65                None
66            }
67        }
68    }
69
70    #[must_use]
71    pub fn stages(&self) -> StagesIter<'_> {
72        StagesIter(self.first_stage())
73    }
74
75    pub fn set_8bit(&mut self, on: bool) -> bool {
76        unsafe { ffi::cmsPipelineSetSaveAs8bitsFlag((self as *mut Self).cast(), i32::from(on)) != 0 }
77    }
78
79    #[must_use]
80    pub fn input_channels(&self) -> usize {
81        unsafe { ffi::cmsPipelineInputChannels(self.as_ptr()) as usize }
82    }
83
84    #[must_use]
85    pub fn output_channels(&self) -> usize {
86        unsafe { ffi::cmsPipelineOutputChannels(self.as_ptr()) as usize }
87    }
88
89    // Evaluates a pipeline usin u16 of f32 numbers. With u16 it's optionally using the optimized path.
90    pub fn eval<Value: FloatOrU16>(&self, input: &[Value], output: &mut [Value]) {
91        assert_eq!(self.input_channels(), input.len());
92        assert_eq!(self.output_channels(), output.len());
93        unsafe {
94            self.eval_unchecked(input, output);
95        }
96    }
97
98    // You must ensure that input and output have length sufficient for channels
99    #[inline]
100    pub unsafe fn eval_unchecked<Value: FloatOrU16>(&self, input: &[Value], output: &mut [Value]) {
101        Value::eval_pipeline(self.as_ptr(), input, output);
102    }
103}
104
105impl fmt::Debug for PipelineRef {
106    #[cold]
107    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
108        write!(f, "Pipeline({}->{}ch, {} stages)", self.input_channels(), self.output_channels(), self.stage_count())
109    }
110}
111
112#[test]
113fn pipeline() {
114    let p = Pipeline::new(123, 12);
115    assert!(p.is_err());
116
117    let p = Pipeline::new(4, 3).unwrap();
118    assert_eq!(0, p.stage_count());
119    assert_eq!(4, p.input_channels());
120    assert_eq!(3, p.output_channels());
121}