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
extern crate enigo;
extern crate failure;
extern crate image;

use easel::{Easel, Orientation, Tool};
use colors::{Palette, PaletteColor};
use coords::Coord;
use self::image::DynamicImage;
use self::image::FilterType;
use self::image::GenericImage;
use self::image::Rgba;
use self::failure::Error;
use image::imageops::ColorMap;

pub struct ImageDrawer<'a> {
    easel: &'a mut Easel,
    palette: Palette,
    current_color: PaletteColor,

    // The size of the easel along x.
    easel_size: Coord,

    // The starting point to use for the next draw operation.
    current_pos: Coord,

    // The size of the image.
    image_size: Coord,

    // The offset of the image to center it on the easel.
    offset: Coord,
}

pub fn size_to_easel(image: &DynamicImage, easel: &Easel) -> DynamicImage {
    let (size_x, size_y) = image.dimensions();
    let (ul_corner, br_corner) = if size_x > size_y {
        easel.easel_coords.landscape_bounds
    } else {
        easel.easel_coords.portrait_bounds
    };
    let x_bounds = br_corner.x - ul_corner.x;
    let y_bounds = br_corner.y - ul_corner.y;
    image.resize(x_bounds as u32, y_bounds as u32, FilterType::Lanczos3)
}

impl<'a> ImageDrawer<'a> {
    pub fn new(
        easel: &'a mut Easel,
        size_x: u32,
        size_y: u32,
    ) -> ImageDrawer<'a> {
        // For drawing images, we need the brush to be as small as possible.
        easel.change_brush_size(0);
        easel.change_tool(Tool::Paintbrush);

        if (size_x > size_y && easel.orientation == Orientation::Portrait)
            || (size_y > size_x && easel.orientation == Orientation::Landscape)
        {
            easel.change_orientation();
        }

        let current_color = easel.current_color;

        let (ulcorner, lrcorner) = easel.get_bounds();
        let easel_x = lrcorner.x - ulcorner.x - 1;
        let easel_y = lrcorner.y - ulcorner.y - 1;
        let easel_size = Coord::new(easel_x, easel_y);

        let size_x = size_x as i32;
        let size_y = size_y as i32;
        let image_size = Coord::new(size_x, size_y);
        //
        // Offsets used to center the image as best as possible on the easel.
        let offset_x = (easel_x - size_x + 1) / 2;
        let offset_y = (easel_y - size_y) / 2;
        let offset = Coord::new(offset_x, offset_y);

        let start_x = offset_x;
        let start_y = offset_y;
        let current_pos = Coord::new(start_x, start_y);

        ImageDrawer {
            easel: easel,
            palette: Palette::new(),
            current_color: current_color,
            easel_size: easel_size,
            image_size: image_size,
            current_pos: current_pos,
            offset: offset,
        }
    }

    /// Draw the top white border for centering the image along the y-axis.
    ///
    /// If the image completely fills the y axis of the easel, this method
    /// does no drawing.
    pub fn draw_top_border(&mut self) -> Result<(), Error> {
        for iy in 0..self.offset.y {
            self.easel.draw_line(
                Coord::new(0, iy),
                Coord::new(self.easel_size.x, iy),
                &PaletteColor::White,
            )?;
        }
        self.current_color = self.easel.current_color;
        Ok(())
    }

    /// Process the next pixel from a given image.
    ///
    /// Pixels are not drawn to the screen unless we've hit the end of a row
    /// and must draw or there is a color change where we will draw everything
    /// up to the current pixel. If you want to draw a pixel by itself, use
    /// Easel::draw_pixel() instead.
    ///
    /// # Arguments
    ///
    /// * `rgba`: The RGBA pixel to handle.
    /// * `x`: The x coordinate of the pixel in image coordinates.
    /// * `y`: The x coordinate of the pixel in image coordinates.
    ///
    pub fn handle_pixel(
        &mut self,
        rgba: &mut Rgba<u8>,
        x: u32,
        y: u32,
    ) -> Result<(), Error> {
        let mut in_coord = Coord::new(x as i32, y as i32);
        in_coord = in_coord + &self.offset;
        let closest_color = self.palette.colormap[self.palette.index_of(rgba)];

        // If we've hit the end of a row, draw the rest of the row before
        // moving on to the next row.
        if in_coord.y > self.current_pos.y {
            self.easel.draw_line(
                self.current_pos,
                Coord::new(
                    self.image_size.x + self.offset.x,
                    self.current_pos.y,
                ),
                &self.current_color,
            )?;
            self.current_pos = in_coord;
            self.current_color = closest_color;
        }

        // If there's a color change, draw the line up to this pixel and stop.
        if closest_color != self.current_color {
            self.easel.draw_line(
                self.current_pos,
                in_coord - &Coord::new(1, 0),
                &self.current_color,
            )?;
            self.current_pos = in_coord;
            self.current_color = closest_color;
        }

        Ok(())
    }

    /// Draw the bottom white border and clean up the horizontal edges.
    pub fn cleanup_image(&mut self) -> Result<(), Error> {
        // Clean up the left-most edge of the picture if one exists.
        let left_edge = self.offset.x - 1;
        if left_edge > 0 {
            self.easel.draw_line(
                Coord::new(left_edge, 0),
                Coord::new(left_edge, self.image_size.y),
                &PaletteColor::White,
            )?;
            for ix in self.offset.y..self.image_size.y + self.offset.y {
                self.easel.draw_line(
                    Coord::new(0, ix),
                    Coord::new(left_edge, ix),
                    &PaletteColor::White,
                )?;
            }
        }

        // Clean up the right-most edge of the picture if one exists.
        let right_edge = self.image_size.x + self.offset.x + 1;
        if right_edge < self.easel_size.x {
            self.easel.draw_line(
                Coord::new(right_edge, 0),
                Coord::new(right_edge, self.image_size.y),
                &PaletteColor::White,
            )?;
            for ix in self.offset.y..self.image_size.y + self.offset.y {
                self.easel.draw_line(
                    Coord::new(right_edge, ix),
                    Coord::new(self.easel_size.x, ix),
                    &PaletteColor::White,
                )?;
            }
        }

        // Once we've hit the end of the picture, tidy up the bottom by drawing
        // white lines to fill in the entire canvas.
        if self.current_pos.y < self.easel_size.y {
            for iy in self.current_pos.y..self.easel_size.y {
                self.easel.draw_line(
                    Coord::new(0, iy),
                    Coord::new(self.easel_size.x, iy),
                    &PaletteColor::White,
                )?;
            }
        }

        Ok(())
    }
}