image-ndarray 0.1.5

Zero-copy implementations for the Image crate to convert to and from ndarrays
Documentation
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
//! Implementations for ndarray casting and conversions for the ImageBuffer

#[cfg(feature = "image")]
use crate::error::{Error, Result};
#[cfg(feature = "image")]
use image::{ImageBuffer, Pixel};
#[cfg(feature = "image")]
use ndarray::{Array, Array3, ArrayView3, ArrayViewMut, ArrayViewMut3, Dimension};
use num_traits::{AsPrimitive, ToPrimitive};

#[cfg(feature = "image")]
/// Conversion methods for working with ndarrays.
///
/// All methods work without copying any data.
pub trait ImageArray<P: image::Pixel, ImageContainer> {
    /// Cast the ImageBuffer as an ArrayView3.
    ///
    /// * `Y` index is the row
    /// * `X` index is the columns
    /// * `Z` index is the channel
    ///
    /// So when referencing:
    /// `array[[y, x, z]]`
    ///
    /// This does not copy the data, as it is a reference to the actual data in the buffer.
    fn as_ndarray<'a>(&'a self) -> ArrayView3<'a, ImageContainer>;

    /// Cast the ImageBuffer as an ArrayViewMut3.
    ///
    /// * `Y` index is the row
    /// * `X` index is the columns
    /// * `Z` index is the channel
    ///
    /// So when referencing:
    /// `array[[y, x, z]]`
    ///
    /// This does not copy the data, as it is a reference to the actual data in the buffer.
    fn as_ndarray_mut<'a>(&'a mut self) -> ArrayViewMut3<'a, ImageContainer>;

    /// Interpret the ImageBuffer as an Array3.
    ///
    /// * `Y` index is the row
    /// * `X` index is the columns
    /// * `Z` index is the channel
    ///
    /// So when referencing:
    /// `array[[y, x, z]]`
    ///
    /// This does not copy the data, but it does consume the buffer.
    fn to_ndarray(self) -> Array3<ImageContainer>;

    /// Convert the provided array into the ImageBuffer
    ///
    /// * `Y` index is the row
    /// * `X` index is the columns
    /// * `Z` index is the channel
    ///
    /// So when referencing:
    /// `array[[y, x, z]]`
    ///
    /// This does not copy the data, but it does consume the buffer.
    fn from_ndarray<D: Dimension>(
        array: Array<ImageContainer, D>,
    ) -> Result<ImageBuffer<P, Vec<ImageContainer>>>;
}

#[cfg(feature = "image")]
impl<P, C> ImageArray<P, C> for ImageBuffer<P, Vec<C>>
where
    P: Pixel<Subpixel = C>,
    C: Clone + Copy,
{
    fn as_ndarray<'a>(&'a self) -> ArrayView3<'a, C> {
        let (width, height) = self.dimensions();
        unsafe {
            ArrayView3::from_shape_ptr(
                (height as usize, width as usize, P::CHANNEL_COUNT as usize),
                self.as_raw().as_ptr(),
            )
        }
    }

    fn to_ndarray(self) -> Array3<C> {
        let (width, height) = self.dimensions();
        unsafe {
            Array3::from_shape_vec_unchecked(
                (height as usize, width as usize, P::CHANNEL_COUNT as usize),
                self.into_raw(),
            )
        }
    }
    fn from_ndarray<D: Dimension>(mut array: Array<C, D>) -> Result<ImageBuffer<P, Vec<C>>> {
        let shape = array.shape();
        if shape.len() < 2 {
            return Err(Error::Dimensions);
        };

        let (width, height) = (shape[1], shape[0]);
        let channels = match shape.len() {
            2 => 1,
            3 => shape[2],
            _ => return Err(Error::Dimensions),
        };

        if channels != P::CHANNEL_COUNT.into() {
            return Err(Error::ChannelMismatch);
        }

        let data = array.as_mut_ptr();

        std::mem::forget(array);
        let size = height * width * channels;

        let vec_data = unsafe { Vec::from_raw_parts(data, size, size) };
        Self::from_raw(width as u32, height as u32, vec_data).ok_or(Error::ImageConstructFailed)
    }

    fn as_ndarray_mut<'a>(&'a mut self) -> ArrayViewMut3<'a, C> {
        let (width, height) = self.dimensions();

        unsafe {
            ArrayViewMut::from_shape_ptr(
                (height as usize, width as usize, P::CHANNEL_COUNT as usize),
                self.as_mut_ptr(),
            )
        }
    }
}

/// Trait for converting the provided value to a normalized float.
///
/// This is used for image processing where a lot of operations rely on floating values.
pub trait NormalizedFloat<T>
where
    T: AsPrimitive<f32> + AsPrimitive<f64>,
{
    /// Convert the value to a 32 bit float.
    ///
    /// The value will be in a normalized range according to color depths.
    ///
    /// For example in u8, a value of 255 would be represented as 1.0.
    ///
    /// Returns None if it overflows and could not be represented.
    fn to_f32_normalized(&self) -> Option<f32>;
    /// Convert the value to a 64 bit float
    ///
    /// The value will be in a normalized range according to color depths.
    ///
    /// For example in u8, a value of 255 would be represented as 1.0.
    ///
    /// Returns None if it overflows and could not be represented.
    fn to_f64_normalized(&self) -> Option<f64>;

    /// Converts the f32 value to the provided type
    ///
    /// Returns None if it overflows and could not be represented.
    fn from_f32_normalized(value: f32) -> Option<T>;

    /// Converts the f64 value to the provided type
    ///
    /// Returns None if it overflows and could not be represented.
    fn from_f64_normalized(value: f64) -> Option<T>;
}

impl NormalizedFloat<f32> for f32 {
    fn to_f32_normalized(&self) -> Option<f32> {
        Some(*self)
    }

    fn to_f64_normalized(&self) -> Option<f64> {
        self.to_f64()
    }
    fn from_f32_normalized(value: f32) -> Option<f32> {
        Some(value)
    }

    fn from_f64_normalized(value: f64) -> Option<f32> {
        value.to_f32()
    }
}

impl NormalizedFloat<f64> for f64 {
    fn to_f32_normalized(&self) -> Option<f32> {
        self.to_f32()
    }

    fn to_f64_normalized(&self) -> Option<f64> {
        Some(*self)
    }
    fn from_f32_normalized(value: f32) -> Option<f64> {
        value.to_f64()
    }

    fn from_f64_normalized(value: f64) -> Option<f64> {
        Some(value)
    }
}

#[macro_export]
macro_rules! impl_as_float {
    ($type:ty) => {
        impl NormalizedFloat<$type> for $type {
            fn to_f32_normalized(&self) -> Option<f32> {
                self.to_f32()
                    .map(|converted| converted / <$type>::MAX as f32)
            }

            fn to_f64_normalized(&self) -> Option<f64> {
                self.to_f64()
                    .map(|converted| converted / <$type>::MAX as f64)
            }

            fn from_f32_normalized(value: f32) -> Option<$type> {
                Some((value * <$type>::MAX as f32).as_())
            }

            fn from_f64_normalized(value: f64) -> Option<$type> {
                Some((value * <$type>::MAX as f64).as_())
            }
        }
    };
}

impl_as_float!(i32);
impl_as_float!(u32);
impl_as_float!(i16);
impl_as_float!(u16);
impl_as_float!(i8);
impl_as_float!(u8);

#[cfg(feature = "image")]
#[cfg(test)]
mod tests {
    use super::*;
    use image::{Luma, Rgb32FImage, Rgba32FImage};
    use ndarray::Array2;
    use rstest::*;

    #[test]
    fn test_as_ndarray_rgba() {
        let (width, height, channels) = (256, 128, 4);
        let data = create_test_data(width, height, channels);
        let test_image = Rgba32FImage::from_vec(256, 128, data).unwrap();

        let array = test_image.as_ndarray();

        for ((y, x, channel), value) in array.indexed_iter() {
            assert_eq!(test_image.get_pixel(x as u32, y as u32)[channel], *value);
        }
    }

    #[test]
    fn test_as_ndarray_luma() {
        let (width, height, channels) = (256, 128, 1);
        let data = create_test_data(width, height, channels);
        let test_image: ImageBuffer<Luma<f32>, Vec<f32>> =
            ImageBuffer::from_vec(256, 128, data).unwrap();

        let array = test_image.as_ndarray();

        for ((y, x, channel), value) in array.indexed_iter() {
            assert_eq!(test_image.get_pixel(x as u32, y as u32)[channel], *value);
        }
    }

    #[test]
    fn test_as_ndarray_mut() {
        let (width, height, channels) = (256, 128, 4);
        let data = create_test_data(width, height, channels);
        let mut test_image = Rgba32FImage::from_vec(256, 128, data).unwrap();
        let compare = test_image.clone();

        let mut array = test_image.as_ndarray_mut();
        array += 1.0;

        for (x, y, pixel) in test_image.enumerate_pixels() {
            let compare_pixel = compare.get_pixel(x, y);
            for (channel, value) in pixel.channels().iter().enumerate() {
                assert_eq!(*value, compare_pixel[channel] + 1.0);
            }
        }
    }

    #[test]
    fn test_to_ndarray() {
        let (width, height, channels) = (256, 128, 4);
        let data = create_test_data(width, height, channels);
        let test_image = Rgba32FImage::from_vec(256, 128, data).unwrap();

        let mut array = test_image.clone().to_ndarray();

        array += 1.0;
        for ((y, x, channel), value) in array.indexed_iter() {
            assert_eq!(
                test_image.get_pixel(x as u32, y as u32)[channel] + 1.0,
                *value
            );
        }
    }

    #[test]
    fn test_from_ndarray() {
        let (width, height, channels) = (256, 128, 4);
        let data = create_test_data(width, height, channels);
        let test_image = Array3::from_shape_vec((height, width, channels), data).unwrap();
        let compare_data = test_image.clone();

        let result = Rgba32FImage::from_ndarray(test_image).unwrap();

        for (x, y, pixel) in result.enumerate_pixels() {
            for (channel, value) in pixel.channels().iter().enumerate() {
                assert_eq!(*value, compare_data[[y as usize, x as usize, channel]]);
            }
        }
    }

    #[test]
    fn test_from_ndarray_2d() {
        let (width, height, channels) = (256, 128, 1);
        let data = create_test_data(width, height, channels);
        let test_image = Array2::from_shape_vec((height, width), data).unwrap();
        println!("{}", test_image.shape().len());
        let compare_data = test_image.clone();

        let result = ImageBuffer::<Luma<f32>, Vec<f32>>::from_ndarray(test_image).unwrap();

        for (x, y, pixel) in result.enumerate_pixels() {
            for value in pixel.channels().iter() {
                assert_eq!(*value, compare_data[[y as usize, x as usize]]);
            }
        }
    }

    fn create_test_data(width: usize, height: usize, channels: usize) -> Vec<f32> {
        let total_elements = width * height * channels;
        (0..total_elements).map(|x| (x + 1) as f32).collect()
    }

    #[test]
    fn test_from_ndarray_with_invalid_channels() {
        let channels = 4;
        let (width, height) = (256.0, 128.0);
        let total_elements = (width * height * 4.0) as usize;
        let data: Vec<f32> = (0..total_elements).map(|x| (x + 1) as f32).collect();
        let test_image =
            Array3::from_shape_vec((height as usize, width as usize, channels), data).unwrap();

        let result = Rgb32FImage::from_ndarray(test_image.into_dyn())
            .err()
            .unwrap();

        assert_eq!(result, Error::ChannelMismatch);
    }

    #[rstest]
    #[case(1.0)]
    #[case(255.0)]
    #[case(0.5)]
    #[case(-1.0)]
    #[case(-255.0)]
    fn test_f32(#[case] float: f32) {
        assert_eq!(float.to_f32_normalized().unwrap(), float);
        assert_eq!(f32::from_f32_normalized(float).unwrap(), float);

        let float_64: f64 = float.as_();
        assert_eq!(float_64.to_f64_normalized().unwrap(), float_64);
        assert_eq!(f64::from_f64_normalized(float_64).unwrap(), float_64);

        let converted_to_float64 = float.to_f64_normalized().unwrap();
        assert_eq!(converted_to_float64, float as f64);

        let converted_back_to_float32 = float_64.to_f32_normalized().unwrap();
        assert_eq!(converted_back_to_float32, float);
    }

    #[macro_export]
    macro_rules! test_unsigned_ints {
        ($name:ident, $type:ty) => {
            #[rstest]
            #[case(0)]
            #[case(1)]
            #[case($type::MAX)]
            #[case($type::MIN)]
            fn $name(#[case] int: $type) {
                let normalized_f32 = int.to_f32_normalized().unwrap();
                let expected_normalized_f32 = int as f32 / <$type>::MAX as f32;
                assert_eq!(normalized_f32, expected_normalized_f32);

                let int_from_float32 =
                    <$type>::from_f32_normalized(expected_normalized_f32).unwrap();
                let expected_int_from_float32 =
                    (expected_normalized_f32 * <$type>::MAX as f32) as $type;
                assert_eq!(int_from_float32, expected_int_from_float32);

                let normalized_f64 = int.to_f64_normalized().unwrap();
                let expected_normalized_f64 = int as f64 / <$type>::MAX as f64;
                assert_eq!(normalized_f64, expected_normalized_f64);

                let int_from_float64 =
                    <$type>::from_f64_normalized(expected_normalized_f64).unwrap();
                let expected_int_from_float64 =
                    (expected_normalized_f64 * <$type>::MAX as f64) as $type;
                assert_eq!(int_from_float64, expected_int_from_float64);
            }
        };
    }

    #[macro_export]
    macro_rules! test_signed_ints {
        ($name:ident, $type:ty) => {
            #[rstest]
            #[case(0)]
            #[case(1)]
            #[case($type::MAX)]
            #[case($type::MIN)]
            #[case(-1)]
            #[case(-$type::MAX)]
            fn $name(#[case] int: $type) {
                let normalized_f32 = int.to_f32_normalized().unwrap();
                let expected_normalized_f32 = int as f32 / <$type>::MAX as f32;
                assert_eq!(normalized_f32, expected_normalized_f32);

                let int_from_float32 =
                    <$type>::from_f32_normalized(expected_normalized_f32).unwrap();
                let expected_int_from_float32 =
                    (expected_normalized_f32 * <$type>::MAX as f32) as $type;
                assert_eq!(int_from_float32, expected_int_from_float32);

                let normalized_f64 = int.to_f64_normalized().unwrap();
                let expected_normalized_f64 = int as f64 / <$type>::MAX as f64;
                assert_eq!(normalized_f64, expected_normalized_f64);

                let int_from_float64 =
                    <$type>::from_f64_normalized(expected_normalized_f64).unwrap();
                let expected_int_from_float64 =
                    (expected_normalized_f64 * <$type>::MAX as f64) as $type;
                assert_eq!(int_from_float64, expected_int_from_float64);
            }
        };
    }
    // Using the macro to generate tests for i32
    test_signed_ints!(test_i32, i32);
    test_signed_ints!(test_i16, i16);
    test_signed_ints!(test_i8, i8);
    test_unsigned_ints!(test_u32, u32);
    test_unsigned_ints!(test_u16, u16);
    test_unsigned_ints!(test_u8, u8);
}