pixelization/
pixelizer.rs1extern crate image;
2use image::DynamicImage;
3use thiserror::Error;
4use rand::Rng;
5
6pub enum CropMethod{
8 NoCrop,
10 CropEqual,
12 CropRandom
14}
15
16pub fn scale_to_size<'a>(img: DynamicImage, scale: &u32, crop: CropMethod) -> (DynamicImage, (u32, u32)){
19 let (w, h) = (img.width()/scale, img.height()/scale);
20 match crop {
21 CropMethod::NoCrop => (img, (w, h)),
22 CropMethod::CropEqual => {
23 let (new_w, new_h) = (w*scale, h*scale);
24 let left = (img.width() - new_w) / 2;
25 let top = (img.height() - new_h) / 2;
26 (img.crop_imm(left, top, new_w, new_h), (w, h))
27 },
28 CropMethod::CropRandom => {
29 let mut rng = rand::thread_rng();
30 let (new_w, new_h) = (w*scale, h*scale);
31 let left = rng.gen_range(0..(img.width() - new_w));
32 let top = rng.gen_range(0..(img.height() - new_h));
33 (img.crop_imm(left, top, new_w, new_h), (w, h))
34 }
35 }
36}
37
38#[derive(Debug, Clone, Copy)]
40pub enum ColorType {
41 Rgb,
43 Lab
45}
46
47#[derive(Debug, Error)]
49pub enum PixelizationError {
50 #[error("Dimension error: {0}")]
51 DimensionError(String),
52 #[error("Color error: {0}")]
53 ColorError(String),
54}
55
56pub trait Pixelizer{
58 fn pixelize(&self, img: &DynamicImage, width : u32, height: u32, num_colors : usize) -> Result<DynamicImage, PixelizationError>;
66}
67
68pub mod kmeans_pixelizer;
69pub mod pia;