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

const RGBA_ALPHA_TRESHOLD: u8 = 127;

/// A trait adding blitting functions to image types.
pub trait BlitExt {
    /// Convert the image to a custom `BlitBuffer` type which is optimized for applying the
    /// blitting operations.
    fn to_blit_buffer(&self, mask_color: Color) -> BlitBuffer;

    /// Blit the image directly on a buffer.
    fn blit(&self, dst: &mut [u32], dst_width: usize, offset: (i32, i32), mask_color: Color);
}

impl BlitExt for RgbImage {
    fn to_blit_buffer(&self, mask_color: Color) -> BlitBuffer {
        let (width, height) = self.dimensions();

        let pixels = (width * height) as usize;
        let mut color: Vec<Color> = vec![Color::from_u32(0); pixels];
        let mut mask: Vec<Color> = vec![Color::from_u32(0); pixels];

        let mut index = 0;
        for y in 0..height {
            for x in 0..width {
                let pixel = self.get_pixel(x, y).data;

                // Convert pixel to Color
                let raw = Color::from_u8(pixel[0], pixel[1], pixel[2]);

                if raw == mask_color {
                    mask[index] = Color::from_u32(0xFFFFFF);
                } else {
                    color[index] = raw;
                }

                index += 1;
            }
        }

        BlitBuffer { 
            width: width as i32,
            height: height as i32,
            color,
            mask
        }
    }

    fn blit(&self, dst: &mut [u32], dst_width: usize, offset: (i32, i32), mask_color: Color) {
        let dst_size = (dst_width as i32, (dst.len() / dst_width) as i32);

        let (width, height) = self.dimensions();

        // Make sure only the pixels get rendered that are inside the dst
        let min_x = cmp::max(-offset.0, 0);
        let min_y = cmp::max(-offset.1, 0);

        let max_x = cmp::min(dst_size.0 - offset.0, width as i32);
        let max_y = cmp::min(dst_size.1 - offset.1, height as i32);

        for y in min_y..max_y {
            for x in min_x..max_x {
                let pixel = self.get_pixel(x as u32, y as u32).data;

                // Convert pixel to Color
                let raw = Color::from_u8(pixel[0], pixel[1], pixel[2]);

                // Check if the pixel isn't the mask
                if raw != mask_color {
                    // Apply the offsets
                    let dst_x = (x + offset.0) as usize;
                    let dst_y = (y + offset.1) as usize;

                    // Calculate the index
                    let index = dst_x + dst_y * dst_size.0 as usize;
                    dst[index] = raw.u32();
                }
            }
        }
    }
}

impl BlitExt for RgbaImage {
    fn to_blit_buffer(&self, mask_color: Color) -> BlitBuffer {
        let (width, height) = self.dimensions();

        let pixels = (width * height) as usize;
        let mut color: Vec<Color> = vec![Color::from_u32(0); pixels];
        let mut mask: Vec<Color> = vec![Color::from_u32(0); pixels];

        let mut index = 0;
        for y in 0..height {
            for x in 0..width {
                let pixel = self.get_pixel(x, y).data;

                // Convert pixel to Color
                let raw = Color::from_u8(pixel[0], pixel[1], pixel[2]);

                if raw == mask_color || pixel[3] < RGBA_ALPHA_TRESHOLD {
                    mask[index] = Color::from_u32(0xFFFFFF);
                } else {
                    color[index] = raw;
                }

                index += 1;
            }
        }

        BlitBuffer { 
            width: width as i32,
            height: height as i32,
            color,
            mask
        }
    }

    fn blit(&self, dst: &mut [u32], dst_width: usize, offset: (i32, i32), mask_color: Color) {
        let dst_size = (dst_width as i32, (dst.len() / dst_width) as i32);

        let (width, height) = self.dimensions();

        // Make sure only the pixels get rendered that are inside the dst
        let min_x = cmp::max(-offset.0, 0);
        let min_y = cmp::max(-offset.1, 0);

        let max_x = cmp::min(dst_size.0 - offset.0, width as i32);
        let max_y = cmp::min(dst_size.1 - offset.1, height as i32);

        for y in min_y..max_y {
            for x in min_x..max_x {
                let pixel = self.get_pixel(x as u32, y as u32).data;

                // Convert pixel to Color
                let raw = Color::from_u8(pixel[0], pixel[1], pixel[2]);

                // Check if the pixel isn't the mask
                if raw != mask_color && pixel[3] >= RGBA_ALPHA_TRESHOLD {
                    // Apply the offsets
                    let dst_x = (x + offset.0) as usize;
                    let dst_y = (y + offset.1) as usize;

                    // Calculate the index
                    let index = dst_x + dst_y * dst_size.0 as usize;
                    dst[index] = raw.u32();
                }
            }
        }
    }
}