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