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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#[cfg(feature = "client")]
mod client;
#[cfg(feature = "server")]
mod server;

#[cfg(feature = "server")]
pub(crate) use server::ImageBytesResponse;

use bytemuck::{Pod, Zeroable};
use ndarray::{Array2, Array3, ArrayView2, ArrayView3, Axis};
use num_enum::{IntoPrimitive, TryFromPrimitive};
use serde_repr::{Deserialize_repr, Serialize_repr};
use std::num::NonZeroU32;
use std::ops::Deref;

// Missing alias in ndarray.
type ArcArray3<T> = ndarray::ArcArray<T, ndarray::Ix3>;

/// Rank of an image array.
#[derive(
    Debug,
    PartialEq,
    Eq,
    Clone,
    Copy,
    Serialize_repr,
    Deserialize_repr,
    TryFromPrimitive,
    IntoPrimitive,
)]
#[repr(i32)]
#[allow(clippy::default_numeric_fallback)] // false positive https://github.com/rust-lang/rust-clippy/issues/9656
pub enum ImageArrayRank {
    /// 2D
    Rank2 = 2_i32,
    /// 3D
    Rank3 = 3_i32,
}

#[derive(Debug, PartialEq, Eq, Clone, Copy, IntoPrimitive, TryFromPrimitive)]
#[repr(i32)]
#[allow(clippy::default_numeric_fallback)] // false positive https://github.com/rust-lang/rust-clippy/issues/9656
pub(crate) enum TransmissionElementType {
    I16 = 1,
    I32 = 2,
    U8 = 6,
    U16 = 8,
}

// Limited to the only supported element type; useful for serde purposes.
#[derive(
    Debug,
    PartialEq,
    Eq,
    Clone,
    Copy,
    Serialize_repr,
    Deserialize_repr,
    IntoPrimitive,
    TryFromPrimitive,
)]
#[repr(i32)]
pub(crate) enum ImageElementType {
    I32 = TransmissionElementType::I32 as i32,
}

trait AsTransmissionElementType: 'static + TryFrom<i32> + Into<i32> + Copy {
    const TYPE: TransmissionElementType;
}

impl AsTransmissionElementType for i16 {
    const TYPE: TransmissionElementType = TransmissionElementType::I16;
}

impl AsTransmissionElementType for i32 {
    const TYPE: TransmissionElementType = TransmissionElementType::I32;
}

impl AsTransmissionElementType for u16 {
    const TYPE: TransmissionElementType = TransmissionElementType::U16;
}

impl AsTransmissionElementType for u8 {
    const TYPE: TransmissionElementType = TransmissionElementType::U8;
}

/// Image array.
///
/// Image is represented as a 3D array regardless of its actual rank.
/// If the image is a 2D image, the third dimension will have length 1.
///
/// You can retrieve rank as an enum via the [`ImageArray::rank`] method.
///
/// This type is cheaply clonable.
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct ImageArray {
    data: ArcArray3<i32>,
    transmission_element_type: TransmissionElementType,
}

const COLOUR_AXIS: Axis = Axis(2);

impl<T: AsTransmissionElementType> From<ArrayView3<'_, T>> for ImageArray {
    fn from(array: ArrayView3<'_, T>) -> Self {
        let data = array.mapv(Into::into);
        let transmission_element_type = T::TYPE;
        Self {
            data: data.into_shared(),
            transmission_element_type,
        }
    }
}

impl<T: AsTransmissionElementType> From<Array3<T>> for ImageArray {
    fn from(array: Array3<T>) -> Self {
        let data = array.mapv_into_any(Into::into);
        let transmission_element_type = T::TYPE;
        Self {
            data: data.into_shared(),
            transmission_element_type,
        }
    }
}

impl<T: AsTransmissionElementType> From<ArrayView2<'_, T>> for ImageArray {
    fn from(array: ArrayView2<'_, T>) -> Self {
        array.insert_axis(COLOUR_AXIS).into()
    }
}

impl<T: AsTransmissionElementType> From<Array2<T>> for ImageArray {
    fn from(array: Array2<T>) -> Self {
        array.insert_axis(COLOUR_AXIS).into()
    }
}

impl Deref for ImageArray {
    type Target = ArcArray3<i32>;

    fn deref(&self) -> &Self::Target {
        &self.data
    }
}

impl ImageArray {
    /// Retrieve actual rank of the image.
    pub fn rank(&self) -> ImageArrayRank {
        match self.data.len_of(COLOUR_AXIS) {
            1 => ImageArrayRank::Rank2,
            _ => ImageArrayRank::Rank3,
        }
    }
}

#[cfg(not(target_endian = "little"))]
compile_error!(
"Image handling is currently only supported on little-endian platforms for simplicity & performance.
If you have a real-world use case for big-endian support, please open an issue on GitHub."
);

#[cfg(any(feature = "client", feature = "server"))]
#[repr(C)]
#[derive(Clone, Copy, Zeroable, Pod)]
struct ImageBytesMetadata {
    metadata_version: i32,
    error_number: i32,
    client_transaction_id: Option<NonZeroU32>,
    server_transaction_id: Option<NonZeroU32>,
    data_start: i32,
    image_element_type: i32,
    transmission_element_type: i32,
    rank: i32,
    dimension_1: i32,
    dimension_2: i32,
    dimension_3: i32,
}

const IMAGE_BYTES_TYPE: &str = "application/imagebytes";