smartcrop/
image.rs

1use image::{imageops, GenericImage, ImageBuffer, Pixel};
2
3use super::{Image, ResizableImage, RGB};
4
5impl<I, P> Image for I
6where
7    I: GenericImage<Pixel = P> + 'static,
8    P: Pixel<Subpixel = u8> + 'static,
9{
10    fn width(&self) -> u32 {
11        self.width()
12    }
13
14    fn height(&self) -> u32 {
15        self.height()
16    }
17
18    fn get(&self, x: u32, y: u32) -> RGB {
19        let px = self.get_pixel(x, y).to_rgb();
20
21        let r = px[0];
22        let g = px[1];
23        let b = px[2];
24        RGB { r, g, b }
25    }
26}
27
28impl<I, P> ResizableImage<ImageBuffer<P, std::vec::Vec<u8>>> for I
29where
30    I: GenericImage<Pixel = P> + 'static,
31    P: Pixel<Subpixel = u8> + 'static,
32{
33    fn resize(&self, width: u32, height: u32) -> ImageBuffer<P, std::vec::Vec<u8>> {
34        imageops::resize(self, width, height, imageops::FilterType::Triangle)
35    }
36
37    fn crop_and_resize(&self, crop: crate::Crop, width: u32, height: u32) -> ImageBuffer<P, std::vec::Vec<u8>> {
38        let cropped = imageops::crop_imm(self, crop.x, crop.y, crop.width, crop.height);
39        imageops::resize(cropped.inner(), width, height, imageops::FilterType::Triangle)
40    }
41}