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
use crate::core::traits::{Pixel, TryConvertSlice};

#[macro_export]
/// Implement the Pixel trait for a pixel
macro_rules! impl_Pixel {
    ($name:ident, $channels:expr, $subpixels:expr) => {
        impl<T: StorageType> Pixel for $name<T> {
            type T = T;

            fn at(&self, index: usize) -> Self::T {
                self.0[index]
            }

            fn try_from(raw: &[Self::T]) -> Result<Self, array::TryFromSliceError> {
                match <[T; $channels]>::try_from(raw) {
                    Ok(components) => Ok($name { 0: components }),
                    Err(e) => Err(e),
                }
            }

            fn channels() -> u8 {
                $channels
            }

            fn subpixels() -> u8 {
                $subpixels
            }
        }

        impl<T: StorageType> std::ops::Index<usize> for $name<T> {
            type Output = T;

            fn index(&self, i: usize) -> &Self::Output {
                &self.0[i]
            }
        }

        impl<T: StorageType> std::ops::IndexMut<usize> for $name<T> {
            fn index_mut(&mut self, i: usize) -> &mut Self::Output {
                &mut self.0[i]
            }
        }
    };
}

#[macro_export]
/// Define a new pixel struct
macro_rules! define_pixel {
    ($name:ident, $channels:expr, #[$doc:meta]) => {
        #[repr(C)]
        #[derive(Default, Debug, Copy, Clone, PartialEq, Eq)]
        #[$doc]
        pub struct $name<T: StorageType>(pub [T; $channels]);

        impl<T: StorageType> $name<T> {
            /// Returns a new pixel
            ///
            /// # Arguments
            ///
            /// * `channels` - Channel values
            ///
            /// # Example
            ///
            /// ```
            /// use std::array;
            /// use std::convert::{From, TryFrom};
            /// use std::ops::{Index, IndexMut};
            ///
            /// use ffimage::{create_pixel, define_pixel, impl_Pixel};
            /// use ffimage::core::{Pixel, StorageType};
            ///
            /// // define a new pixel type
            /// create_pixel!(Rgb, 3, #[doc = "RGB pixel"]);
            ///
            /// // use the newly created type
            /// let pix = Rgb::<u8>::new([0, 0, 0]);
            /// ```
            pub fn new(channels: [T; $channels]) -> Self {
                $name { 0: channels }
            }
        }
    };
}

#[macro_export]
/// Create a new pixel type
///
/// A pixel is defined by its number of channels and the storage type (which is the same for each
/// channel). The memory layout is C compatible by default and the Debug, Copy and Clone traits
/// are derived. The Pixel trait is implemented automatically providing the basic building blocks
/// for higher level structures such as image view and buffer types.
///
/// # Example
///
/// ```
/// use std::array;
/// use std::convert::{From, TryFrom};
/// use std::ops::{Index, IndexMut};
///
/// use ffimage::{create_pixel, define_pixel, impl_Pixel};
/// use ffimage::core::traits::{Pixel, StorageType};
///
/// create_pixel!(Rgb, 3, #[doc = "RGB pixel"]);
/// ```
macro_rules! create_pixel {
    ($name:ident, $channels:expr, #[$doc:meta]) => {
        define_pixel!($name, $channels, #[$doc]);
        impl_Pixel!($name, $channels, 1);
    };
}

#[macro_export]
/// Create a new macropixel type
///
/// The term 'macropixel' does not seem to be defined as well as the 'pixel' term. We use it to
/// describe storage pixels used to store image buffers. To view or render such an image, one must
/// first convert from the macropixel representation to an image pixel (regular 'pixel') one.
///
/// A famous application example is YUV chroma subsampling: the YUYV format samples 4:2:2, meaning
/// for each macropixel, there is a full chroma and two luma samples at half the bit width.
/// YUYV buffers are converted into YUV buffers (each YUYV macropixel ends up as two full YUV
/// pixels) for rendering.
///
/// # Example
///
/// ```
/// use std::array;
/// use std::convert::{From, TryFrom};
/// use std::ops::{Index, IndexMut};
///
/// use ffimage::{create_macropixel, create_pixel, define_pixel, impl_Pixel};
/// use ffimage::core::traits::{Pixel, StorageType};
///
/// create_macropixel!(Yuyv, 2, 2, #[doc = "YUYV macropixel"]);
/// ```
macro_rules! create_macropixel {
    ($name:ident, $channels:expr, $subpixels:expr, #[$doc:meta]) => {
        define_pixel!($name, $channels, #[$doc]);
        impl_Pixel!($name, $channels, $subpixels);
    };
}

// Blanket implementation for pixel row conversion.
// If we know how to convert a single pixel into another one, we can automatically convert between
// rows as well. This obviously does not work for macropixels, where one pixel may transform into
// several, so you need to implement the trait yourself for those types.

impl<SP: Pixel, DP: Pixel + From<SP>> TryConvertSlice<DP> for SP {
    type Error = ();

    fn try_convert(input: &[SP], output: &mut [DP]) -> Result<(), Self::Error> {
        if input.len() != output.len() {
            return Err(());
        }

        for i in 0..input.len() {
            output[i] = DP::from(input[i]);
        }

        Ok(())
    }
}