imageutil/
canvas.rs

1use image::{GenericImage, GenericImageView, Pixel};
2
3// from https://github.com/image-rs/imageproc/blob/master/src/drawing/canvas.rs
4
5pub trait Canvas {
6    type Pixel: Pixel;
7    fn dimensions(&self) -> (u32, u32);
8    fn width(&self) -> u32 {
9        self.dimensions().0
10    }
11    fn height(&self) -> u32 {
12        self.dimensions().1
13    }
14    fn get_pixel(&self, x: u32, y: u32) -> Self::Pixel;
15    fn draw_pixel(&mut self, x: u32, y: u32, color: Self::Pixel);
16}
17
18impl<I> Canvas for I
19where
20    I: GenericImage,
21{
22    type Pixel = I::Pixel;
23
24    fn dimensions(&self) -> (u32, u32) {
25        <I as GenericImageView>::dimensions(self)
26    }
27
28    fn get_pixel(&self, x: u32, y: u32) -> Self::Pixel {
29        self.get_pixel(x, y)
30    }
31
32    fn draw_pixel(&mut self, x: u32, y: u32, color: Self::Pixel) {
33        self.put_pixel(x, y, color)
34    }
35}
36
37pub struct Blend<I>(pub I);
38
39impl<I: GenericImage> Canvas for Blend<I> {
40    type Pixel = I::Pixel;
41
42    fn dimensions(&self) -> (u32, u32) {
43        self.0.dimensions()
44    }
45
46    fn get_pixel(&self, x: u32, y: u32) -> Self::Pixel {
47        self.0.get_pixel(x, y)
48    }
49
50    fn draw_pixel(&mut self, x: u32, y: u32, color: Self::Pixel) {
51        let mut pix = self.0.get_pixel(x, y);
52        pix.blend(&color);
53        self.0.put_pixel(x, y, pix);
54    }
55}