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
mod gray;
mod rgb;

pub use self::gray::*;
pub use self::rgb::*;

#[cfg(feature = "png")]
mod png;

/// Bitmap pixel
pub trait Pixel {
    type Component;

    fn components(&self) -> &[Self::Component];
    fn components_mut(&mut self) -> &mut [Self::Component];

    fn invert(&mut self);
}

/// Bitmap object
#[repr(C)]
pub struct Bitmap<T> {
    pixels: *mut T,
    width: u32,
    height: u32,
}

impl<T> Bitmap<T> {
    /// Create new bitmap with specified size
    pub fn new(width: u32, height: u32) -> Self {
        let size = (width * height) as usize;

        let mut pixels = Vec::with_capacity(size);
        unsafe { pixels.set_len(size); }

        let pixels = core::mem::ManuallyDrop::new(pixels).as_mut_ptr();

        Self {
            pixels,
            width,
            height,
        }
    }
}

impl<T> Drop for Bitmap<T> {
    fn drop(&mut self) {
        let size = (self.width * self.height) as usize;
        let _pixels = unsafe {
            Vec::from_raw_parts(
                self.pixels,
                size,
                size,
            )
        };
    }
}

impl<T> Bitmap<T> {
    /// Get width of bitmap in pixels
    pub fn width(&self) -> u32 {
        self.width
    }

    /// Get height of bitmap in pixels
    pub fn height(&self) -> u32 {
        self.height
    }

    /// Get pixel data slice for reading from
    pub fn pixels(&self) -> &[T] {
        unsafe { std::slice::from_raw_parts(
            self.pixels,
            (self.width * self.height) as usize,
        ) }
    }

    /// Get pixel data slice for writing to
    pub fn pixels_mut(&mut self) -> &mut [T] {
        unsafe { std::slice::from_raw_parts_mut(
            self.pixels,
            (self.width * self.height) as usize,
        ) }
    }

    /// Get raw pixels data for reading from
    pub fn raw_pixels(&self) -> &[u8] {
        let pixels = self.pixels();

        unsafe {
            core::slice::from_raw_parts(
                pixels.as_ptr() as _,
                pixels.len() * core::mem::size_of::<T>()
            )
        }
    }

    /// Get raw pixels data for writing to
    pub fn raw_pixels_mut(&mut self) -> &mut [u8] {
        let pixels = self.pixels_mut();

        unsafe {
            core::slice::from_raw_parts_mut(
                pixels.as_mut_ptr() as _,
                pixels.len() * core::mem::size_of::<T>()
            )
        }
    }

    /// Get pixel with specified coordinates
    pub fn pixel(&self, x: u32, y: u32) -> &T {
        let index = x + y * self.width();
        &self.pixels()[index as usize]
    }

    /// Get pixel with specified coordinates
    pub fn pixel_mut(&mut self, x: u32, y: u32) -> &mut T {
        let index = x + y * self.width();
        &mut self.pixels_mut()[index as usize]
    }

    /// Invert pixels colors
    pub fn invert(&mut self)
    where
        T: Pixel,
    {
        self.pixels_mut().iter_mut().for_each(|pixel| {
            pixel.invert();
        })
    }

    /// Flip pixels around y axis
    pub fn flip_x(&mut self) {
        let width = self.width();
        let height = self.height();
        let pixels = self.pixels_mut();

        for y in 0..height {
            for x in 0..width / 2 {
                let nx = width - x - 1;
                unsafe { core::ptr::swap(
                    &mut pixels[(x + y * width) as usize],
                    &mut pixels[(nx + y * width) as usize],
                ); }
            }
        }
    }

    /// Flip pixels around y axis
    pub fn flip_y(&mut self) {
        let width = self.width();
        let height = self.height();
        let pixels = self.pixels_mut();

        for y in 0..height / 2 {
            for x in 0..width {
                let ny = height - y - 1;
                unsafe { core::ptr::swap(
                    &mut pixels[(x + y * width) as usize],
                    &mut pixels[(x + ny * width) as usize],
                ); }
            }
        }
    }

    /// Convert bitmap data type
    pub fn convert<R>(&self) -> Bitmap<R>
    where
        T: Copy,
        R: From<T>,
    {
        let mut bitmap = Bitmap::<R>::new(self.width(), self.height());
        bitmap.pixels_mut().iter_mut().zip(self.pixels().iter())
            .for_each(|(out_pixel, in_pixel)|
                      *out_pixel = From::from(*in_pixel));
        bitmap
    }
}

impl<T> Bitmap<T> {
    pub(crate) fn as_raw(&self) -> *const u8 {
        unsafe { core::mem::transmute(self) }
    }

    pub(crate) fn as_raw_mut(&mut self) -> *mut u8 {
        unsafe { core::mem::transmute(self) }
    }
}

impl<'a, A, T> From<&'a Bitmap<A>> for Bitmap<T>
where
    A: Copy,
    T: From<A>,
{
    fn from(bitmap: &'a Bitmap<A>) -> Self {
        bitmap.convert()
    }
}