diff --git a/modraw.png b/modraw.png index 985607b..0de478c 100644 Binary files a/modraw.png and b/modraw.png differ diff --git a/src/main.rs b/src/main.rs index 95806d7..f2a48d0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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, /// 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, Vec> = ImageBuffer::new(args.width, args.width); + let mut img: ImageBuffer, Vec> = 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); } } }