1use crate::error::Error;
2use std::mem::MaybeUninit;
3
4pub fn lib_czi_error(code: std::ffi::c_int) -> Result<(), Error> {
5 match code {
6 0 => Ok(()),
7 1 => Err(Error::LibCziApiInvalidArgument),
8 2 => Err(Error::LibCziApiInvalidHandle),
9 3 => Err(Error::LibCziApiOutOfMemory),
10 4 => Err(Error::LibCziApiIndexOutOfRange),
11 20 => Err(Error::LibCziApiLockUnlockSemanticViolated),
12 50 => Err(Error::LibCziApiUnspecifiedError),
13 _ => Err(Error::LibCziApiUnknownError(code as usize)),
14 }
15}
16
17#[derive(Clone, Debug)]
18pub enum Dimension {
19 Z = 1,
21 C = 2,
23 T = 3,
25 R = 4,
27 S = 5,
29 I = 6,
31 H = 7,
33 V = 8,
35 B = 9,
37}
38
39impl Dimension {
40 pub fn vec_from_bitflags(bit_flags: u32) -> Vec<Dimension> {
41 let mut bit_flags = bit_flags;
42 let mut dimensions = Vec::with_capacity(9);
43 for i in 1..=9 {
44 if (bit_flags & 1) > 0 {
45 dimensions.push(Dimension::try_from(i).expect("i must be 0 <= i <= 9"));
46 }
47 bit_flags >>= 1;
48 }
49 dimensions
50 }
51}
52
53impl TryFrom<i32> for Dimension {
54 type Error = Error;
55
56 fn try_from(dimension: i32) -> Result<Self, Error> {
57 match dimension {
58 1 => Ok(Dimension::Z),
59 2 => Ok(Dimension::C),
60 3 => Ok(Dimension::T),
61 4 => Ok(Dimension::R),
62 5 => Ok(Dimension::S),
63 6 => Ok(Dimension::I),
64 7 => Ok(Dimension::H),
65 8 => Ok(Dimension::V),
66 9 => Ok(Dimension::B),
67 _ => Err(Error::UnknownDimension(dimension.to_string())),
68 }
69 }
70}
71
72#[derive(Clone, Debug)]
74pub enum RawDataType {
75 Data = 0,
76 Metadata = 1,
77}
78
79impl TryFrom<i32> for RawDataType {
80 type Error = Error;
81
82 fn try_from(raw_data_type: i32) -> Result<Self, Error> {
83 match raw_data_type {
84 0 => Ok(RawDataType::Data),
85 1 => Ok(RawDataType::Metadata),
86 _ => Err(Error::UnknownDataType(raw_data_type)),
87 }
88 }
89}
90
91#[derive(Clone, Debug)]
93pub enum PixelType {
94 Gray8 = 0,
95 Gray16 = 1,
96 Gray32Float = 2,
97 Bgr24 = 3,
98 Bgr48 = 4,
99 Bgr96Float = 8,
100 Bgra32 = 9,
101 Gray64ComplexFloat = 10,
102 Bgr192ComplexFloat = 11,
103 Gray32 = 12,
104 Gray64Float = 13,
105}
106
107impl TryFrom<i32> for PixelType {
108 type Error = Error;
109
110 fn try_from(pixel_type: i32) -> Result<Self, Error> {
111 match pixel_type {
112 0 => Ok(PixelType::Gray8),
113 1 => Ok(PixelType::Gray16),
114 2 => Ok(PixelType::Gray32Float),
115 3 => Ok(PixelType::Bgr24),
116 4 => Ok(PixelType::Bgr48),
117 8 => Ok(PixelType::Bgr96Float),
118 9 => Ok(PixelType::Bgra32),
119 10 => Ok(PixelType::Gray64ComplexFloat),
120 11 => Ok(PixelType::Bgr192ComplexFloat),
121 12 => Ok(PixelType::Gray32),
122 13 => Ok(PixelType::Gray64Float),
123 _ => Err(Error::UnknownPixelType(pixel_type)),
124 }
125 }
126}
127
128pub trait Ptr {
129 type Pointer;
130
131 unsafe fn assume_init(ptr: MaybeUninit<Self::Pointer>) -> Self;
132
133 fn as_mut_ptr(&self) -> *mut Self::Pointer
134 where
135 Self: Sized;
136
137 fn as_ptr(&self) -> *const Self::Pointer
138 where
139 Self: Sized;
140}