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
use fb;
use image::DynamicImage;
use mxc_types::mxcfb_rect;
use rusttype::{Scale, point};
use libc;
use line_drawing;

use image::GenericImage;

macro_rules! min {
        ($x: expr) => ($x);
        ($x: expr, $($z: expr),+) => (::std::cmp::min($x, min!($($z),*)));
}

macro_rules! max {
        ($x: expr) => ($x);
        ($x: expr, $($z: expr),+) => (::std::cmp::max($x, max!($($z),*)));
}

impl<'a> fb::Framebuffer<'a> {
    pub fn draw_image(&mut self, img: &DynamicImage, top: usize, left: usize) -> mxcfb_rect {
        for (x, y, pixel) in img.to_luma().enumerate_pixels() {
            self.write_pixel(top + y as usize, left + x as usize, pixel.data[0]);
        }
        return mxcfb_rect {
            top: top as u32,
            left: left as u32,
            width: img.width(),
            height: img.height(),
        };
    }

    pub fn draw_line(&mut self, y0: i32, x0: i32, y1: i32, x1: i32, color: u8) -> mxcfb_rect {
        // Create local variables for moving start point
        let mut x0 = x0;
        let mut y0 = y0;

        // Get absolute x/y offset
        let dx = if x0 > x1 { x0 - x1 } else { x1 - x0 };
        let dy = if y0 > y1 { y0 - y1 } else { y1 - y0 };

        // Get slopes
        let sx = if x0 < x1 { 1 } else { -1 };
        let sy = if y0 < y1 { 1 } else { -1 };

        // Initialize error
        let mut err = if dx > dy { dx } else { -dy } / 2;
        let mut err2;

        let mut minx = 0;
        let mut miny = 0;
        let mut maxx = 0;
        let mut maxy = 0;
        loop {
            // Set pixel
            self.write_pixel(y0 as usize, x0 as usize, color);
            maxy = max!(maxy, y0);
            miny = min!(miny, y0);
            minx = min!(minx, x0);
            maxx = max!(maxx, x0);

            // Check end condition
            if x0 == x1 && y0 == y1 { break; };

            // Store old error
            err2 = 2 * err;

            // Adjust error and start position
            if err2 > -dx {
                err -= dy;
                x0 += sx;
            }
            if err2 < dy {
                err += dx;
                y0 += sy;
            }
        }

        return mxcfb_rect {
            top: miny as u32,
            left: minx as u32,
            width: (maxx - minx) as u32,
            height: (maxy - miny) as u32,
        };
    }

    pub fn draw_circle(&mut self, y: usize, x: usize, rad: usize, color: u8) -> mxcfb_rect {
        for (x, y) in line_drawing::BresenhamCircle::new(x as i32, y as i32, rad as i32) {
            self.write_pixel(y as usize, x as usize, color);
        }
        return mxcfb_rect {
            top: y as u32 - rad as u32,
            left: x as u32 - rad as u32,
            width: 2 * rad as u32,
            height: 2 * rad as u32,
        };
    }

    pub fn fill_circle(&mut self, y: usize, x: usize, rad: usize, color: u8) -> mxcfb_rect {
        for current in { 1..rad + 1 } {
            for (x, y) in line_drawing::BresenhamCircle::new(x as i32, y as i32, current as i32) {
                self.write_pixel(y as usize, x as usize, color);
            }
        }
        return mxcfb_rect {
            top: y as u32 - rad as u32,
            left: x as u32 - rad as u32,
            width: 2 * rad as u32,
            height: 2 * rad as u32,
        };
    }

    pub fn draw_bezier(&mut self, startpt: (f32, f32), ctrlpt: (f32, f32), endpt: (f32, f32), color: u8) -> mxcfb_rect {
        let mut upperleft: (usize, usize) = (0, 0);
        let mut lowerright: (usize, usize) = (0, 0);
        for pt in self.sample_bezier(startpt, ctrlpt, endpt) {
            let approx = (pt.0 as usize, pt.1 as usize);
            upperleft.1 = min!(upperleft.1, approx.1);
            upperleft.0 = min!(upperleft.0, approx.0);
            lowerright.1 = max!(lowerright.1, approx.1);
            lowerright.0 = max!(lowerright.0, approx.0);
            self.write_pixel(approx.1, approx.0, color);
        }
        return mxcfb_rect {
            top: upperleft.1 as u32,
            left: upperleft.0 as u32,
            width: (lowerright.0 - upperleft.0) as u32,
            height: (lowerright.1 - upperleft.1) as u32,
        };
    }

    fn sample_bezier(&mut self, startpt: (f32, f32), ctrlpt: (f32, f32), endpt: (f32, f32)) -> Vec<(f32, f32)> {
        let mut points = Vec::new();
        let mut lastpt = (-100, -100);
        for i in 0..1000 {
            let t = (i as f32) / 1000.0;
            let precisept = (
                (1.0 - t).powf(2.0) * startpt.0 + 2.0 * (1.0 - t) * t * ctrlpt.0
                    + t.powf(2.0) * endpt.0,
                (1.0 - t).powf(2.0) * startpt.1 + 2.0 * (1.0 - t) * t * ctrlpt.1
                    + t.powf(2.0) * endpt.1,
            );
            let pt = (precisept.0 as i32, precisept.1 as i32);
            // prevent oversampling
            if pt != lastpt {
                points.push(precisept);
                lastpt = pt;
            }
        }
        return points;
    }

    pub fn draw_text(
        &mut self,
        y: usize,
        x: usize,
        text: String,
        size: usize,
        color: u8,
    ) -> mxcfb_rect {
        let scale = Scale {
            x: size as f32,
            y: size as f32,
        };

        // The starting positioning of the glyphs (top left corner)
        let start = point(x as f32, y as f32);

        let dfont = &mut self.default_font.clone();

        let mut miny = y;
        let mut maxy = y;
        let mut minx = x;
        let mut maxx = x;

        // Loop through the glyphs in the text, positing each one on a line
        for glyph in dfont.layout(&text, scale, start) {
            if let Some(bounding_box) = glyph.pixel_bounding_box() {
                // Draw the glyph into the image per-pixel by using the draw closure
                let bbmaxy = bounding_box.max.y as usize;
                let bbmaxx = bounding_box.max.x as usize;
                let bbminy = bounding_box.min.y as usize;
                let bbminx = bounding_box.min.x as usize;
                if bbmaxy > maxy {
                    maxy = bbmaxy;
                }
                if bbmaxx > maxx {
                    maxx = bbmaxx;
                }
                if bbminy < miny {
                    miny = bbminy;
                }
                if bbminx < minx {
                    minx = bbminx;
                }
                glyph.draw(|x, y, v| {
                    /* TODO: We have a small issue with color interpolation here allowing only
                             black text to be displayed. However this is only due to the code below,
                             not an inherent limitation.
                     */
                    self.write_pixel(
                        (y + bounding_box.min.y as u32) as usize,
                        (x + bounding_box.min.x as u32) as usize,
                        !((v * !color as f32) as u8),
                    )
                });
            }
        }
        // return the height and width of the drawn text so that refresh can be called on it
        return mxcfb_rect {
            top: miny as u32,
            left: minx as u32,
            height: (maxy - miny) as u32,
            width: (maxx - minx) as u32,
        };
    }

    pub fn fill_rect(&mut self, y: usize, x: usize, height: usize, width: usize, color: u8) {
        for ypos in y..y + height {
            for xpos in x..x + width {
                self.write_pixel(ypos, xpos, color);
            }
        }
    }

    pub fn clear(&mut self) {
        let h = self.var_screen_info.yres as usize;
        let line_length = self.fix_screen_info.line_length as usize;
        unsafe {
            libc::memset(self.frame.data() as *mut libc::c_void, 255, line_length * h);
        }
    }
}