diff --git a/Cargo.toml b/Cargo.toml index e69d5f0..bebbb96 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,9 +1,9 @@ [package] name = "franklin" description = "A utility for generating images through evolution." -version = "0.1.3" +version = "0.1.4" edition = "2021" -rust-version = "1.58" +rust-version = "1.69" authors = ["Kamil Rusin"] readme = "README.md" homepage = "https://github.com/nathiss/franklin" @@ -20,9 +20,9 @@ include = [ [dependencies] anyhow = "1.0.53" -show-image = "0.10.1" +show-image = "0.13.1" rand = "0.8.4" -clap = { version = "3.1.0", features = ["cargo"] } +clap = { version = "3.2.25", features = ["cargo"] } rayon = "1.5.1" num-integer = "0.1.44" diff --git a/src/bin.rs b/src/bin.rs index 7c3d85a..fc9d0ef 100644 --- a/src/bin.rs +++ b/src/bin.rs @@ -46,7 +46,7 @@ use franklin::{ LeftOrRightCloneCrossover, }, fitness::{AbsoluteDistance, FitnessFunction, SquareDistance}, - mutators::{Mutator, RectangleMutator, TriangleMutator, CircleMutator}, + mutators::{CircleMutator, Mutator, RectangleMutator, TriangleMutator}, ArgParser, ColorMode, DisplayCondition, EnvironmentBuilder, ImageReader, SaveCondition, }; @@ -62,9 +62,9 @@ fn get_color_mode_from_name(name: &str) -> Result { #[doc(hidden)] fn get_mutator_from_name(name: &str) -> Result> { match name { - "Rectangle" => Ok(Box::new(RectangleMutator::default())), - "Triangle" => Ok(Box::new(TriangleMutator::default())), - "Circle" => Ok(Box::new(CircleMutator::default())), + "Rectangle" => Ok(Box::::default()), + "Triangle" => Ok(Box::::default()), + "Circle" => Ok(Box::::default()), _ => Err(Error::msg("Unknown mutator.")), } } @@ -72,8 +72,8 @@ fn get_mutator_from_name(name: &str) -> Result Result> { match name { - "SquareDistance" => Ok(Box::new(SquareDistance::default())), - "AbsoluteDistance" => Ok(Box::new(AbsoluteDistance::default())), + "SquareDistance" => Ok(Box::::default()), + "AbsoluteDistance" => Ok(Box::::default()), _ => Err(Error::msg("Unknown fitness function.")), } } @@ -81,9 +81,9 @@ fn get_fitness_from_name(name: &str) -> Result Result> { match name { - "LeftOrRight" => Ok(Box::new(LeftOrRightCloneCrossover::default())), - "EqualHalfs" => Ok(Box::new(EqualHalfsCrossover::default())), - "ArithmeticAverage" => Ok(Box::new(ArithmeticAverageCrossover::default())), + "LeftOrRight" => Ok(Box::::default()), + "EqualHalfs" => Ok(Box::::default()), + "ArithmeticAverage" => Ok(Box::::default()), _ => Err(Error::msg("Unknown crossover function.")), } } diff --git a/src/environment.rs b/src/environment.rs index c68ba70..7544eda 100644 --- a/src/environment.rs +++ b/src/environment.rs @@ -32,15 +32,8 @@ fn get_first_generation( image_height: usize, image_width: usize, ) -> Vec<(Image, usize)> { - let mut vec = Vec::with_capacity(vec_len); - let pixel = Pixel::white(); - - vec.resize_with(vec_len, || { - (Image::blank(image_height, image_width, &pixel), usize::MAX) - }); - - vec + vec![(Image::blank(image_height, image_width, &pixel), usize::MAX); vec_len] } pub struct Environment { @@ -107,13 +100,13 @@ impl Environment { context.get_mutator().mutate_rgb(&mut entry.0); entry.1 = context .get_fitness() - .calculate_fitness_rgb(&*context.get_image(), &entry.0); + .calculate_fitness_rgb(context.get_image(), &entry.0); } ColorMode::Grayscale => { context.get_mutator().mutate_grayscale(&mut entry.0); entry.1 = context .get_fitness() - .calculate_fitness_grayscale(&*context.get_image(), &entry.0); + .calculate_fitness_grayscale(context.get_image(), &entry.0); } } diff --git a/src/environment_builder.rs b/src/environment_builder.rs index 741d042..a6982f7 100644 --- a/src/environment_builder.rs +++ b/src/environment_builder.rs @@ -299,9 +299,9 @@ impl Default for EnvironmentBuilder { Self { image: None, color_mode: ColorMode::Rgb, - mutator: Box::new(RectangleMutator::default()), - fitness: Box::new(SquareDistance::default()), - crossover: Box::new(EqualHalfsCrossover::default()), + mutator: Box::::default(), + fitness: Box::::default(), + crossover: Box::::default(), generation_size: 100, threads: 1, display_condition: DisplayCondition::None, diff --git a/src/fitness/absolute_distance.rs b/src/fitness/absolute_distance.rs index 3e099b8..bc0fe93 100644 --- a/src/fitness/absolute_distance.rs +++ b/src/fitness/absolute_distance.rs @@ -18,9 +18,9 @@ impl FitnessFunction for AbsoluteDistance { let diff_g = isize::from(p1.get_g()) - isize::from(p2.get_g()); let diff_b = isize::from(p1.get_b()) - isize::from(p2.get_b()); - sum += diff_r.abs() as usize; - sum += diff_g.abs() as usize; - sum += diff_b.abs() as usize; + sum += diff_r.unsigned_abs(); + sum += diff_g.unsigned_abs(); + sum += diff_b.unsigned_abs(); sum }; @@ -37,7 +37,7 @@ impl FitnessFunction for AbsoluteDistance { let diff_red = isize::from(p1.get_r()) - isize::from(p2.get_r()); let diff_grayscale = diff_red * 3; - sum += diff_grayscale.abs() as usize; + sum += diff_grayscale.unsigned_abs(); sum }; diff --git a/src/models/image.rs b/src/models/image.rs index 77f43d7..bb62ad5 100644 --- a/src/models/image.rs +++ b/src/models/image.rs @@ -22,17 +22,9 @@ impl Image { #[must_use] pub fn blank(height: usize, width: usize, pixel: &Pixel) -> Self { let size = height * width; - let mut pixels = Vec::with_capacity(size as usize); + let pixels = vec![pixel.clone(); size]; - for _ in 0..size { - pixels.push(pixel.clone()); - } - - Self { - height, - width, - pixels, - } + Self::new(height, width, pixels) } pub fn width(&self) -> usize { diff --git a/src/mutators/circle_mutator.rs b/src/mutators/circle_mutator.rs index 1a03eff..7e62ada 100644 --- a/src/mutators/circle_mutator.rs +++ b/src/mutators/circle_mutator.rs @@ -1,6 +1,6 @@ use num_integer::Roots; -use crate::{util::Random, models::Image}; +use crate::{models::Image, util::Random}; use super::Mutator; @@ -19,18 +19,17 @@ impl CircleMutator { let image_width = image.width() as i64; let image_height = image.height() as i64; - let x = random.get_random(1, image_width) as i64; - let y = random.get_random(1, image_height) as i64; + let x = random.get_random(1, image_width); + let y = random.get_random(1, image_height); - let n = *[x, y, image_width - x, image_height - y].iter().min().unwrap(); + let n = *[x, y, image_width - x, image_height - y] + .iter() + .min() + .unwrap(); let r = random.get_random(1, n + 1); - RandomCircle { - x, - y, - r, - } + RandomCircle { x, y, r } } } diff --git a/src/mutators/rectangle_mutator.rs b/src/mutators/rectangle_mutator.rs index 966e834..a28665f 100644 --- a/src/mutators/rectangle_mutator.rs +++ b/src/mutators/rectangle_mutator.rs @@ -18,8 +18,8 @@ impl RectangleMutator { let image_width = image.width(); let image_height = image.height(); - let x = random.get_random(0usize, image_width - 1); - let y = random.get_random(0usize, image_height - 1); + let x = random.get_random(0usize, image_width); + let y = random.get_random(0usize, image_height); let width = random.get_random(0usize, image_width - x) + 1; let height = random.get_random(0usize, image_height - y) + 1; diff --git a/src/mutators/triangle_mutator.rs b/src/mutators/triangle_mutator.rs index 3e59409..8c795bb 100644 --- a/src/mutators/triangle_mutator.rs +++ b/src/mutators/triangle_mutator.rs @@ -62,7 +62,7 @@ impl TriangleMutator { let mut xf = vertices[0].x as f64; let mut xt = vertices[0].x as f64 + dx_upper; - let mut y = vertices[0].y as isize; + let mut y = vertices[0].y; while y <= if vertices[2].y > image_height - 1 { image_height - 1