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
use super::*;

use image::{ImageBuffer, Rgba};

impl Texture for ImageBuffer<Rgba<u8>, Vec<u8>> {
    fn width(&self) -> u32 {
        ImageBuffer::width(self)
    }

    fn height(&self) -> u32 {
        ImageBuffer::height(self)
    }

    fn put_rect(&mut self, pixel: PixelType, data: &[u8], gpu: &TextureGlyph) {
        use PixelType::*;

        assert!(gpu.bounds.x >= 0);
        assert!(gpu.bounds.y >= 0);
        let bx = gpu.bounds.x as u32;
        let by = gpu.bounds.y as u32;

        match pixel {
            Alpha => {
                for x in 0..gpu.bounds.width {
                    for y in 0..gpu.bounds.height {
                        for i in 0..3 {
                            self.get_pixel_mut(bx + x, by + y).0[i] = 255;
                        }
                        self.get_pixel_mut(bx + x, by + y).0[3] =
                            data[(x + y * gpu.bounds.width) as usize];
                    }
                }
            }
            RGBA => {
                for x in bx..gpu.bounds.width {
                    for y in by..gpu.bounds.height {
                        let index = ((x + y * gpu.bounds.height) * 4) as usize;
                        let pixel = &data[index..(4 + index)];
                        self.get_pixel_mut(x, y).0.clone_from_slice(pixel);
                    }
                }
            }
        }
    }
}