use ndarray::Array3;
use crate::error::AsyncTiffError;
use crate::{Array, TypedArray};
pub enum NdArray {
Bool(Array3<bool>),
Uint8(Array3<u8>),
Uint16(Array3<u16>),
Uint32(Array3<u32>),
Uint64(Array3<u64>),
Int8(Array3<i8>),
Int16(Array3<i16>),
Int32(Array3<i32>),
Int64(Array3<i64>),
Float32(Array3<f32>),
Float64(Array3<f64>),
}
impl TryFrom<Array> for NdArray {
type Error = crate::error::AsyncTiffError;
fn try_from(value: Array) -> Result<Self, Self::Error> {
value
.data_type
.ok_or_else(|| AsyncTiffError::General("Unknown data type".to_string()))?;
match value.data {
TypedArray::Bool(data) => Ok(NdArray::Bool(
Array3::from_shape_vec(value.shape, data).map_err(|e| {
AsyncTiffError::General(format!("Failed to create ndarray: {}", e))
})?,
)),
TypedArray::UInt8(data) => Ok(NdArray::Uint8(
Array3::from_shape_vec(value.shape, data).map_err(|e| {
AsyncTiffError::General(format!("Failed to create ndarray: {}", e))
})?,
)),
TypedArray::UInt16(data) => Ok(NdArray::Uint16(
Array3::from_shape_vec(value.shape, data).map_err(|e| {
AsyncTiffError::General(format!("Failed to create ndarray: {}", e))
})?,
)),
TypedArray::UInt32(data) => Ok(NdArray::Uint32(
Array3::from_shape_vec(value.shape, data).map_err(|e| {
AsyncTiffError::General(format!("Failed to create ndarray: {}", e))
})?,
)),
TypedArray::UInt64(data) => Ok(NdArray::Uint64(
Array3::from_shape_vec(value.shape, data).map_err(|e| {
AsyncTiffError::General(format!("Failed to create ndarray: {}", e))
})?,
)),
TypedArray::Int8(data) => Ok(NdArray::Int8(
Array3::from_shape_vec(value.shape, data).map_err(|e| {
AsyncTiffError::General(format!("Failed to create ndarray: {}", e))
})?,
)),
TypedArray::Int16(data) => Ok(NdArray::Int16(
Array3::from_shape_vec(value.shape, data).map_err(|e| {
AsyncTiffError::General(format!("Failed to create ndarray: {}", e))
})?,
)),
TypedArray::Int32(data) => Ok(NdArray::Int32(
Array3::from_shape_vec(value.shape, data).map_err(|e| {
AsyncTiffError::General(format!("Failed to create ndarray: {}", e))
})?,
)),
TypedArray::Int64(data) => Ok(NdArray::Int64(
Array3::from_shape_vec(value.shape, data).map_err(|e| {
AsyncTiffError::General(format!("Failed to create ndarray: {}", e))
})?,
)),
TypedArray::Float32(data) => Ok(NdArray::Float32(
Array3::from_shape_vec(value.shape, data).map_err(|e| {
AsyncTiffError::General(format!("Failed to create ndarray: {}", e))
})?,
)),
TypedArray::Float64(data) => Ok(NdArray::Float64(
Array3::from_shape_vec(value.shape, data).map_err(|e| {
AsyncTiffError::General(format!("Failed to create ndarray: {}", e))
})?,
)),
}
}
}