libczirw_sys/
misc.rs

1use anyhow::{Error, Result, anyhow};
2use std::fmt;
3use std::mem::MaybeUninit;
4use std::os::raw::c_int;
5
6/// the error type for libCZIAPI
7#[derive(Clone, Debug)]
8pub enum LibCZIApiError {
9    OK,
10    InvalidArgument,
11    InvalidHandle,
12    OutOfMemory,
13    IndexOutOfRange,
14    LockUnlockSemanticViolated,
15    UnspecifiedError,
16}
17
18impl std::error::Error for LibCZIApiError {}
19
20impl TryFrom<c_int> for LibCZIApiError {
21    type Error = Error;
22
23    fn try_from(code: c_int) -> Result<Self> {
24        match code {
25            0 => Ok(LibCZIApiError::OK),
26            1 => Err(Error::from(LibCZIApiError::InvalidArgument)),
27            2 => Err(Error::from(LibCZIApiError::InvalidHandle)),
28            3 => Err(Error::from(LibCZIApiError::OutOfMemory)),
29            4 => Err(Error::from(LibCZIApiError::IndexOutOfRange)),
30            20 => Err(Error::from(LibCZIApiError::LockUnlockSemanticViolated)),
31            50 => Err(Error::from(LibCZIApiError::UnspecifiedError)),
32            _ => Err(anyhow!("Unknown error code {}", code)),
33        }
34    }
35}
36
37impl fmt::Display for LibCZIApiError {
38    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
39        write!(f, "LibCZIApi {self:?}")
40    }
41}
42
43/// enum for SubBlock.get_raw_data
44pub enum RawDataType {
45    Data,
46    Metadata,
47}
48
49/// pixel type
50pub enum PixelType {
51    Gray8,
52    Gray16,
53    Gray32Float,
54    Bgr24,
55    Bgr48,
56    Bgr96Float,
57    Bgra32,
58    Gray64ComplexFloat,
59    Bgr192ComplexFloat,
60    Gray32,
61    Gray64Float,
62}
63
64impl TryFrom<i32> for PixelType {
65    type Error = Error;
66
67    fn try_from(pixel_type: i32) -> Result<Self> {
68        match pixel_type {
69            0 => Ok(PixelType::Gray8),
70            1 => Ok(PixelType::Gray16),
71            2 => Ok(PixelType::Gray32Float),
72            3 => Ok(PixelType::Bgr24),
73            4 => Ok(PixelType::Bgr48),
74            8 => Ok(PixelType::Bgr96Float),
75            9 => Ok(PixelType::Bgra32),
76            10 => Ok(PixelType::Gray64ComplexFloat),
77            11 => Ok(PixelType::Bgr192ComplexFloat),
78            12 => Ok(PixelType::Gray32),
79            13 => Ok(PixelType::Gray64Float),
80            _ => Err(anyhow!("Unknown pixel type {}", pixel_type)),
81        }
82    }
83}
84
85impl From<PixelType> for i32 {
86    fn from(pixel_type: PixelType) -> Self {
87        match pixel_type {
88            PixelType::Gray8 => 0,
89            PixelType::Gray16 => 1,
90            PixelType::Gray32Float => 2,
91            PixelType::Bgr24 => 3,
92            PixelType::Bgr48 => 4,
93            PixelType::Bgr96Float => 8,
94            PixelType::Bgra32 => 9,
95            PixelType::Gray64ComplexFloat => 10,
96            PixelType::Bgr192ComplexFloat => 11,
97            PixelType::Gray32 => 12,
98            PixelType::Gray64Float => 13,
99        }
100    }
101}
102
103pub trait Ptr {
104    type Pointer;
105
106    unsafe fn assume_init(ptr: MaybeUninit<Self::Pointer>) -> Self;
107
108    fn as_mut_ptr(&self) -> *mut Self::Pointer
109    where
110        Self: Sized;
111
112    fn as_ptr(&self) -> *const Self::Pointer
113    where
114        Self: Sized;
115}