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
use super::*;
use std::fmt;
use std::ptr;
use eval::FloatOrU16;
use foreign_types::ForeignTypeRef;

foreign_type! {
    type CType = ffi::Pipeline;
    fn drop = ffi::cmsPipelineFree;
    pub struct Pipeline;
    pub struct PipelineRef;
}

impl Pipeline {
    pub fn new(input_channels: usize, output_channels: usize) -> LCMSResult<Self> {
        unsafe {
            Error::if_null(ffi::cmsPipelineAlloc(ptr::null_mut(), input_channels as u32, output_channels as u32))
        }
    }
}

impl PipelineRef {
    pub fn cat(&mut self, append: &PipelineRef) -> bool {
        unsafe {
            ffi::cmsPipelineCat(self.as_ptr(), append.as_ptr()) != 0
        }
    }

    pub fn stage_count(&self) -> usize {
        unsafe {
            ffi::cmsPipelineStageCount(self.as_ptr()) as usize
        }
    }

    pub fn set_8bit(&mut self, on: bool) -> bool {
        unsafe {
            ffi::cmsPipelineSetSaveAs8bitsFlag(self.as_ptr(), on as i32) != 0
        }
    }

    pub fn input_channels(&self) -> usize {
        unsafe {
            ffi::cmsPipelineInputChannels(self.as_ptr()) as usize
        }
    }

    pub fn output_channels(&self) -> usize {
        unsafe {
            ffi::cmsPipelineOutputChannels(self.as_ptr()) as usize
        }
    }

    // Evaluates a pipeline usin u16 of f32 numbers. With u16 it's optionally using the optimized path.
    pub fn eval<Value: FloatOrU16>(&self, input: &[Value], output: &mut [Value]) {
        assert_eq!(self.input_channels(), input.len());
        assert_eq!(self.output_channels(), output.len());
        unsafe {
            self.eval_unchecked(input, output);
        }
    }

    // You must ensure that input and output have length sufficient for channels
    #[inline]
    pub unsafe fn eval_unchecked<Value: FloatOrU16>(&self, input: &[Value], output: &mut [Value]) {
        Value::eval_pipeline(self.as_ptr(), input, output);
    }
}

impl fmt::Debug for PipelineRef {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "Pipeline({}->{}ch, {} stages)", self.input_channels(), self.output_channels(), self.stage_count())
    }
}

#[test]
fn pipeline() {
    let p = Pipeline::new(123, 12);
    assert!(p.is_err());

    let p = Pipeline::new(4, 3).unwrap();
    assert_eq!(0, p.stage_count());
    assert_eq!(4, p.input_channels());
    assert_eq!(3, p.output_channels());
}