fix little bug + add pix4 option
All checks were successful
CI / Rust project (push) Successful in -2m56s

This commit is contained in:
fabolous005 2023-11-27 18:12:48 +01:00
parent dcde63552a
commit b345147719
2 changed files with 34 additions and 9 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 11 MiB

After

Width:  |  Height:  |  Size: 1.1 MiB

View File

@ -3,6 +3,7 @@ use image::Rgb;
use clap::Parser; use clap::Parser;
use rand::Rng; use rand::Rng;
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
struct Args { struct Args {
/// width and height of the image /// width and height of the image
@ -15,20 +16,45 @@ struct Args {
/// Set verbose mode /// Set verbose mode
#[arg(short, long, default_value_t = false)] #[arg(short, long, default_value_t = false)]
verbose: bool verbose: bool,
/// Optional 4x pixel mode
#[arg(short, long, default_value_t = false)]
pix4: bool,
} }
fn main() { fn main() {
let args = Args::parse(); let args = Args::parse();
let mut img: ImageBuffer<Rgb<u8>, Vec<u8>> = ImageBuffer::new(args.width, args.width); let mut img: ImageBuffer<Rgb<u8>, Vec<u8>> = ImageBuffer::new(args.width, args.width);
let mut colors: Vec<[u8; 3]> = vec![]; let mut colors: Vec<[u8; 3]> = vec![];
for x in 0..img.width() { if args.pix4 {
for y in 0..img.height() { for x in 0..(img.width() / 2) {
let result: usize = (x * y % args.modulo).try_into().unwrap(); for y in 0..(img.height() / 2) {
if colors.get(result).is_some() { let result: usize = (x * y % args.modulo).try_into().unwrap();
img.put_pixel(x, y, Rgb(colors[(x * y % args.modulo) as usize])); if colors.get(result).is_some() {
} else { img.put_pixel(2 * x, 2 * y, Rgb(colors[(x * y % args.modulo) as usize]));
colors.insert(result, [generate_random(0, 255), generate_random(0, 255), generate_random(0, 255)]); img.put_pixel(2 * x + 1, 2 * y, Rgb(colors[(x * y % args.modulo) as usize]));
img.put_pixel(2 * x, 2 * y + 1, Rgb(colors[(x * y % args.modulo) as usize]));
img.put_pixel(2 * x + 1, 2 * y + 1, Rgb(colors[(x * y % args.modulo) as usize]));
} else {
colors.insert(result, [generate_random(0, 255), generate_random(0, 255), generate_random(0, 255)]);
img.put_pixel(2 * x, 2 * y, Rgb(colors[(x * y % args.modulo) as usize]));
img.put_pixel(2 * x + 1, 2 * y, Rgb(colors[(x * y % args.modulo) as usize]));
img.put_pixel(2 * x, 2 * y + 1, Rgb(colors[(x * y % args.modulo) as usize]));
img.put_pixel(2 * x + 1, 2 * y + 1, Rgb(colors[(x * y % args.modulo) as usize]));
}
}
}
} else {
for x in 0..img.width() {
for y in 0..img.height() {
let result: usize = (x * y % args.modulo).try_into().unwrap();
if colors.get(result).is_some() {
img.put_pixel(x, y, Rgb(colors[(x * y % args.modulo) as usize]));
} else {
colors.insert(result, [generate_random(0, 255), generate_random(0, 255), generate_random(0, 255)]);
img.put_pixel(x, y, Rgb(colors[(x * y % args.modulo) as usize]));
}
} }
} }
} }
@ -41,7 +67,6 @@ fn main() {
fn generate_random(start: u8, end: u8) -> u8 { fn generate_random(start: u8, end: u8) -> u8 {
let mut rng = rand::thread_rng(); let mut rng = rand::thread_rng();
// rng.gen_range(std::char::from_u32(start).unwrap()..=std::char::from_u32(end).unwrap())
rng.gen_range(start..=end) rng.gen_range(start..=end)
} }