This commit is contained in:
@@ -31,4 +31,8 @@ pub enum Error {
|
||||
UnknownDataType(i32),
|
||||
#[error("unknown pixel type: {0}")]
|
||||
UnknownPixelType(i32),
|
||||
#[error("wrong attachment type: {0}")]
|
||||
InvalidAttachmentType(String),
|
||||
#[error("raw data is malformed")]
|
||||
MalformedData,
|
||||
}
|
||||
|
||||
+13
-2
@@ -463,7 +463,7 @@ impl SubBlock {
|
||||
///
|
||||
/// \\returns An error-code indicating success or failure of the operation.
|
||||
pub fn get_raw_data(&self, tp: RawDataType, size: i32) -> Result<(i32, Vec<u8>), Error> {
|
||||
let mut data = Vec::<u8>::with_capacity(size as usize);
|
||||
let mut data = vec![0u8; size as usize];
|
||||
let size = Box::into_raw(Box::new(size as c_ulong));
|
||||
lib_czi_error(unsafe {
|
||||
libCZI_SubBlockGetRawData(**self, tp as c_int, size, data.as_mut_ptr() as *mut c_void)
|
||||
@@ -510,7 +510,7 @@ impl Attachment {
|
||||
///
|
||||
/// \\returns An error-code indicating success or failure of the operation.
|
||||
pub fn get_raw_data(&self, size: i32) -> Result<(i32, Vec<u8>), Error> {
|
||||
let mut data = Vec::<u8>::with_capacity(size as usize);
|
||||
let mut data = vec![0u8; size as usize];
|
||||
let size = Box::into_raw(Box::new(size as c_ulong));
|
||||
lib_czi_error(unsafe {
|
||||
libCZI_AttachmentGetRawData(**self, size, data.as_mut_ptr() as *mut c_void)
|
||||
@@ -518,6 +518,17 @@ impl Attachment {
|
||||
Ok((unsafe { *Box::from_raw(size) as i32 }, data))
|
||||
}
|
||||
|
||||
/// convenience method that extracts some types of data
|
||||
pub fn get_data(&self) -> Result<AttachmentData, Error> {
|
||||
let (n, _) = self.get_raw_data(0)?;
|
||||
let (_, data) = self.get_raw_data(n)?;
|
||||
Ok(match self.get_info()?.get_content_file_type()?.as_str() {
|
||||
"CZTIMS" | "CZFOC" => AttachmentData::from_float(data.as_slice())?,
|
||||
"CZEXP" | "CZHWS" | "CZMVM" | "CZFBMX" => AttachmentData::from_xml(data.as_slice())?,
|
||||
_ => AttachmentData::Unknown(data),
|
||||
})
|
||||
}
|
||||
|
||||
/// Release the specified attachment object.
|
||||
///
|
||||
/// \\param attachment_object The attachment object to be released.
|
||||
|
||||
+24
-22
@@ -293,7 +293,7 @@ impl ExternalOutputStreamStruct {
|
||||
}
|
||||
}
|
||||
|
||||
/// This structure gather the information needed to create a reader object.
|
||||
/// This structure gathers the information needed to create a reader object.
|
||||
impl ReaderOpenInfo {
|
||||
pub fn new(stream: &InputStream) -> Self {
|
||||
Self(ReaderOpenInfoInterop {
|
||||
@@ -706,25 +706,27 @@ impl AttachmentInfo {
|
||||
pub fn get_guid(&self) -> [u8; 16] {
|
||||
self.0.guid
|
||||
}
|
||||
pub fn get_content_file_type(&self) -> [u8; 9] {
|
||||
self.0.content_file_type
|
||||
}
|
||||
|
||||
pub fn get_name(&self) -> Result<String, Error> {
|
||||
Ok(
|
||||
CStr::from_bytes_until_nul(&self.0.name.iter().map(|&i| i as u8).collect::<Vec<_>>())?
|
||||
.to_str()?
|
||||
.to_string(),
|
||||
)
|
||||
if self.0.name_overflow {
|
||||
Ok(
|
||||
unsafe { CString::from_raw(self.0.name_in_case_of_overflow as *mut c_char) }
|
||||
.to_string_lossy()
|
||||
.to_string(),
|
||||
)
|
||||
} else {
|
||||
Ok(CStr::from_bytes_until_nul(
|
||||
&self.0.name.iter().map(|&i| i as u8).collect::<Vec<_>>(),
|
||||
)?
|
||||
.to_string_lossy()
|
||||
.to_string())
|
||||
}
|
||||
}
|
||||
pub fn get_name_overflow(&self) -> bool {
|
||||
self.0.name_overflow
|
||||
}
|
||||
pub fn get_name_in_case_of_overflow(&self) -> Result<String, Error> {
|
||||
Ok(
|
||||
unsafe { CString::from_raw(self.0.name_in_case_of_overflow as *mut c_char) }
|
||||
.to_str()?
|
||||
.to_string(),
|
||||
)
|
||||
|
||||
pub fn get_content_file_type(&self) -> Result<String, Error> {
|
||||
Ok(CStr::from_bytes_until_nul(&self.0.content_file_type)?
|
||||
.to_string_lossy()
|
||||
.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1041,10 +1043,10 @@ impl AccessorOptions {
|
||||
pub fn get_use_visibility_check_optimization(&self) -> bool {
|
||||
self.0.use_visibility_check_optimization
|
||||
}
|
||||
pub fn get_additional_parameters(&self) -> Result<String, Error> {
|
||||
Ok(unsafe { CStr::from_ptr(self.0.additional_parameters) }
|
||||
.to_str()?
|
||||
.to_string())
|
||||
pub fn get_additional_parameters(&self) -> String {
|
||||
unsafe { CStr::from_ptr(self.0.additional_parameters) }
|
||||
.to_string_lossy()
|
||||
.to_string()
|
||||
}
|
||||
pub fn set_background_color_r(&mut self, back_ground_color_r: f32) {
|
||||
self.0.back_ground_color_r = back_ground_color_r
|
||||
|
||||
+1
-1
@@ -10,7 +10,7 @@ pub mod sys;
|
||||
pub use functions::*;
|
||||
pub use handle::*;
|
||||
pub use interop::*;
|
||||
pub use misc::{Dimension, PixelType, RawDataType};
|
||||
pub use misc::{AttachmentData, Dimension, PixelType, RawDataType};
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
|
||||
+70
-1
@@ -1,4 +1,5 @@
|
||||
use crate::error::Error;
|
||||
use std::ffi::CStr;
|
||||
use std::mem::MaybeUninit;
|
||||
|
||||
pub fn lib_czi_error(code: std::ffi::c_int) -> Result<(), Error> {
|
||||
@@ -14,7 +15,71 @@ pub fn lib_czi_error(code: std::ffi::c_int) -> Result<(), Error> {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
/// container for some types of attachment data
|
||||
pub enum AttachmentData {
|
||||
Float(Vec<f64>),
|
||||
Xml(String),
|
||||
Unknown(Vec<u8>),
|
||||
}
|
||||
|
||||
impl AttachmentData {
|
||||
pub fn attachment_type(&self) -> &str {
|
||||
match self {
|
||||
Self::Float(_) => "float",
|
||||
Self::Xml(_) => "xml",
|
||||
Self::Unknown(_) => "unknown",
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn from_float(raw: &[u8]) -> Result<Self, Error> {
|
||||
if raw.len() < 8 {
|
||||
Err(Error::MalformedData)
|
||||
} else {
|
||||
let number = u32::from_le_bytes(raw[4..8].try_into().unwrap()) as usize;
|
||||
if raw.len() < 8 * number + 8 {
|
||||
Err(Error::MalformedData)
|
||||
} else {
|
||||
let mut data = Vec::with_capacity(number);
|
||||
for i in 0..number {
|
||||
data.push(f64::from_le_bytes(
|
||||
raw[8 + 8 * i..16 + 8 * i].try_into().unwrap(),
|
||||
));
|
||||
}
|
||||
Ok(Self::Float(data))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn from_xml(raw: &[u8]) -> Result<Self, Error> {
|
||||
Ok(Self::Xml(
|
||||
CStr::from_bytes_until_nul(raw)?
|
||||
.to_string_lossy()
|
||||
.to_string(),
|
||||
))
|
||||
}
|
||||
|
||||
pub fn try_into_float(&self) -> Result<Vec<f64>, Error> {
|
||||
if let AttachmentData::Float(data) = self {
|
||||
Ok(data.clone())
|
||||
} else {
|
||||
Err(Error::InvalidAttachmentType(
|
||||
self.attachment_type().to_string(),
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn try_into_xml(&self) -> Result<String, Error> {
|
||||
if let AttachmentData::Xml(data) = self {
|
||||
Ok(data.clone())
|
||||
} else {
|
||||
Err(Error::InvalidAttachmentType(
|
||||
self.attachment_type().to_string(),
|
||||
))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
|
||||
pub enum Dimension {
|
||||
/// The Z-dimension.
|
||||
Z = 1,
|
||||
@@ -48,6 +113,10 @@ impl Dimension {
|
||||
}
|
||||
dimensions
|
||||
}
|
||||
|
||||
pub fn is_valid(&self, v: u32) -> bool {
|
||||
v & (*self as u32).pow(2) > 0
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<i32> for Dimension {
|
||||
|
||||
Reference in New Issue
Block a user