add inherit_last option + pix4 option
All checks were successful
CI / Rust project (push) Successful in 1m7s

This commit is contained in:
fabolous005 2023-11-27 23:28:40 +01:00
parent b345147719
commit 89ad59a852
2 changed files with 42 additions and 12 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.1 MiB

After

Width:  |  Height:  |  Size: 144 KiB

View File

@ -7,11 +7,11 @@ use rand::Rng;
#[derive(Parser, Debug)]
struct Args {
/// width and height of the image
#[arg(short, long, default_value_t = 2000)]
width: u32,
#[arg(short, long)]
width: Option<u32>,
/// The multiplicator
#[arg(short, default_value_t = 96)]
#[arg(short, default_value_t = 36)]
modulo: u32,
/// Set verbose mode
@ -21,27 +21,57 @@ struct Args {
/// Optional 4x pixel mode
#[arg(short, long, default_value_t = false)]
pix4: bool,
/// Inherit color from last pixel
#[arg(short, long, default_value_t = false)]
inherit_last: bool,
}
fn main() {
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.unwrap_or(args.modulo * 10), args.width.unwrap_or(args.modulo * 10));
let mut colors: Vec<[u8; 3]> = vec![];
if args.pix4 {
for x in 0..(img.width() / 2) {
for y in 0..(img.height() / 2) {
let result: usize = (x * y % args.modulo).try_into().unwrap();
let mut color;
if colors.get(result).is_some() {
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]));
color = Rgb(colors[result]);
if args.inherit_last {
let color_before;
// TODO: highly review this
// WARN: not usable yet
if x == 0 {
if y == 0 {
color_before = &Rgb([0,0,0]);
// } else if y == img.width() {
// color_before = img.get_pixel(img.width(), y - 2);
} else {
color_before = img.get_pixel(x, y - 1);
}
} else if x == 1 {
color_before = img.get_pixel(x - 1, y);
} else {
color_before = img.get_pixel(x - 2, y);
}
color = Rgb([
color[0] / 2 + color_before[0] / 2,
color[1] / 2 + color_before[1] / 2,
color[2] / 2 + color_before[2] / 2,
])
}
img.put_pixel(2 * x, 2 * y, color);
img.put_pixel(2 * x + 1, 2 * y, color);
img.put_pixel(2 * x, 2 * y + 1, color);
img.put_pixel(2 * x + 1, 2 * y + 1, color);
} 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]));
color = Rgb(colors[result]);
img.put_pixel(2 * x, 2 * y, color);
img.put_pixel(2 * x + 1, 2 * y, color);
img.put_pixel(2 * x, 2 * y + 1, color);
img.put_pixel(2 * x + 1, 2 * y + 1, color);
}
}
}