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
//! Raster implementation.

use std::slice;

use ::RasterMut;

/// Depth of a raster.
#[derive(Clone,Copy,Debug,Eq,PartialEq)]
pub enum RasterDepth {
    Depth8,
    Depth16,
}

impl<'a> RasterMut<'a> {
    /// Allocate a new raster for the given destination buffer slice.
    ///
    /// # Examples
    ///
    /// ```
    /// const IMG_W: usize = 320;
    /// const IMG_H: usize = 200;
    /// let mut buf = [0; 3 * IMG_W * IMG_H];
    ///
    /// bayer::RasterMut::new(
    ///         IMG_W, IMG_H, bayer::RasterDepth::Depth8,
    ///         &mut buf);
    /// ```
    pub fn new(w: usize, h: usize, depth: RasterDepth, buf: &'a mut [u8])
            -> Self {
        let bytes_per_pixel = depth.bytes_per_pixel();
        let stride = w.checked_mul(bytes_per_pixel).expect("overflow");
        Self::with_offset(0, 0, w, h, stride, depth, buf)
    }

    /// Allocate a new raster for the given destination buffer slice.
    /// Stride is in number of bytes.
    ///
    /// # Examples
    ///
    /// ```
    /// const IMG_W: usize = 320;
    /// const IMG_H: usize = 200;
    /// let mut buf = [0; 3 * IMG_W * IMG_H];
    ///
    /// bayer::RasterMut::with_offset(
    ///         0, 0, IMG_W, IMG_H, 3 * IMG_W, bayer::RasterDepth::Depth8,
    ///         &mut buf);
    /// ```
    pub fn with_offset(
            x: usize, y: usize, w: usize, h: usize, stride: usize,
            depth: RasterDepth, buf: &'a mut [u8])
            -> Self {
        let x1 = x.checked_add(w).expect("overflow");
        let y1 = y.checked_add(h).expect("overflow");
        let bytes_per_pixel = depth.bytes_per_pixel();
        assert!(x < x1 && x1.checked_mul(bytes_per_pixel).expect("overflow") <= stride && h > 0);
        assert!(stride.checked_mul(y1).expect("overflow") <= buf.len());
        assert_eq!(stride % bytes_per_pixel, 0);

        RasterMut {
            x, y, w, h, stride, depth, buf,
        }
    }

    /// Borrow a mutable u8 row slice.
    ///
    /// # Panics
    ///
    /// Panics if the raster is not 8-bpp.
    pub fn borrow_row_u8_mut(&mut self, y: usize)
            -> &mut [u8] {
        assert!(self.depth == RasterDepth::Depth8);
        assert!(y < self.h);

        let bytes_per_pixel = 3;
        let start = self.stride * (self.y + y) + bytes_per_pixel * self.x;
        let end = start + bytes_per_pixel * self.w;
        &mut self.buf[start..end]
    }

    /// Borrow a mutable u16 row slice.
    ///
    /// # Panics
    ///
    /// Panics if the raster is not 16-bpp.
    pub fn borrow_row_u16_mut(&mut self, y: usize)
            -> &mut [u16] {
        assert!(self.depth == RasterDepth::Depth16);
        assert!(y < self.h);

        let bytes_per_pixel = 6;
        let start = self.stride * (self.y + y) + bytes_per_pixel * self.x;
        let end = start + bytes_per_pixel * self.w;
        let s = &mut self.buf[start..end];

        unsafe {
            slice::from_raw_parts_mut(s.as_mut_ptr() as *mut u16, 3 * self.w)
        }
    }
}

impl RasterDepth {
    /// The number of bytes per pixel for a raster of the given depth.
    fn bytes_per_pixel(self) -> usize {
        match self {
            RasterDepth::Depth8 => 3,
            RasterDepth::Depth16 => 6,
        }
    }
}

#[cfg(test)]
mod tests {
    use ::RasterMut;
    use super::RasterDepth;

    #[test]
    #[should_panic]
    fn test_raster_mut_overflow() {
        let mut buf = [0; 1];
        let _ = RasterMut::new(
                ::std::usize::MAX, ::std::usize::MAX, RasterDepth::Depth8, &mut buf);
    }

    #[test]
    fn test_borrow_row_u16_mut() {
        let expected = [
            0x00,0x00, 0x01,0x01, 0x02,0x02,
            0x03,0x03, 0x04,0x04, 0x05,0x05,
            0x06,0x06, 0x07,0x07, 0x08,0x08,
            0x09,0x09, 0x0A,0x0A, 0x0B,0x0B ];

        const IMG_W: usize = 4;
        const IMG_H: usize = 1;
        let mut buf = [0u8; 6 * IMG_W * IMG_H];

        {
            let mut dst = RasterMut::new(
                    IMG_W, IMG_H, RasterDepth::Depth16, &mut buf);
            let row = dst.borrow_row_u16_mut(0);

            for (i, elt) in row.iter_mut().enumerate() {
                // Work around different endians.
                let i = i as u16;
                *elt = (i << 8) | i;
            }
        }

        assert_eq!(&buf[0..6 * IMG_W * IMG_H], &expected[..]);
    }
}