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
|
use crate::eval::FloatOrU16;
use crate::stage::{StageRef, StagesIter};
use crate::{ffi, Error, LCMSResult};
use foreign_types::{foreign_type, ForeignTypeRef};
use std::fmt;
use std::ptr;
foreign_type! {
/// Pipelines are a convenient way to model complex operations on image data.
///
/// Each pipeline may contain an arbitrary number of stages. Each stage performs a single operation.
/// Pipelines may be optimized to be executed on a certain format (8 bits, for example) and can be saved as LUTs in ICC profiles.
///
/// This is an owned version of `PipelineRef`.
#[doc(hidden)]
pub unsafe type Pipeline {
type CType = ffi::Pipeline;
fn drop = ffi::cmsPipelineFree;
fn clone = ffi::cmsPipelineDup;
}
}
impl Pipeline {
/// Allocates an empty pipeline. Final Input and output channels must be specified at creation time.
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 {
/// Appends pipeline given as argument at the end of this pipeline. Channel count must match.
pub fn cat(&mut self, append: &PipelineRef) -> bool {
if append.input_channels() != self.output_channels() {
return false;
}
unsafe { ffi::cmsPipelineCat((self as *mut Self).cast(), append.as_ptr()) != 0 }
}
#[must_use]
pub fn stage_count(&self) -> usize {
unsafe { ffi::cmsPipelineStageCount(self.as_ptr()) as usize }
}
#[must_use]
pub fn first_stage(&self) -> Option<&StageRef> {
unsafe {
let f = ffi::cmsPipelineGetPtrToFirstStage(self.as_ptr());
if !f.is_null() {
Some(ForeignTypeRef::from_ptr(f))
} else {
None
}
}
}
#[must_use]
pub fn last_stage(&self) -> Option<&StageRef> {
unsafe {
let f = ffi::cmsPipelineGetPtrToLastStage(self.as_ptr());
if !f.is_null() {
Some(ForeignTypeRef::from_ptr(f))
} else {
None
}
}
}
#[must_use]
pub fn stages(&self) -> StagesIter<'_> {
StagesIter(self.first_stage())
}
pub fn set_8bit(&mut self, on: bool) -> bool {
unsafe { ffi::cmsPipelineSetSaveAs8bitsFlag((self as *mut Self).cast(), i32::from(on)) != 0 }
}
#[must_use]
pub fn input_channels(&self) -> usize {
unsafe { ffi::cmsPipelineInputChannels(self.as_ptr()) as usize }
}
#[must_use]
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 {
#[cold]
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());
}
|