picasso_core/renderer/
mod.rs

1mod modes;
2
3use crate::{Picasso, PicassoResult};
4use image::{imageops::FilterType, GenericImageView, Rgba};
5pub use modes::PicassoMode;
6use purrmitive::{
7    core::{average_color, diff_full, PurrContext, PurrHillClimbModel},
8    DynamicImage,
9};
10use std::sync::{Arc, RwLock};
11
12
13impl Picasso {
14    pub fn render(&mut self, img: DynamicImage, output: &str) -> PicassoResult<()> {
15        let (w, h ) = img.dimensions();
16        let origin_img = img.to_rgba8();
17        let color = average_color(&origin_img);
18
19        let mut current_img = image::ImageBuffer::new(w, h);
20        for y in 0..h {
21            for x in 0..w {
22                let pixel: &mut Rgba<u8> = current_img.get_pixel_mut(x as u32, y as u32);
23                pixel.0 = color.0;
24            }
25        }
26
27        let score = diff_full(&origin_img, &current_img);
28        let scale = self.out_size as f32 / w.max(h) as f32;
29
30        let model_ctx = PurrContext {
31            w,
32            h,
33            scale,
34            origin_img: Arc::new(origin_img),
35            current_img: Arc::new(RwLock::new(current_img)),
36            rng: self.rng.clone(),
37            score,
38            bg: color,
39            alpha: self.alpha,
40        };
41
42        let mut model_hillclimb = PurrHillClimbModel::new(model_ctx, 1000, 16, 100);
43        let mut model_runner = self.shape_type.build_runner(self.num_shapes, num_cpus::get());
44        model_runner.run(&mut model_hillclimb);
45        model_runner.save(&model_hillclimb.context, output);
46        Ok(())
47    }
48}