~jpl8/text-to-piet

1f7ad14c1c9f9057d531df092c6b20d1beec0aa0 — jpl 1 year, 7 months ago fd57bca
Add output file option to args
3 files changed, 21 insertions(+), 22 deletions(-)

A raven.png
M src/main.rs
A test.png
A raven.png => raven.png +0 -0
M src/main.rs => src/main.rs +21 -22
@@ 1,16 1,12 @@
use std::collections::HashSet;
use std::fs;

use rand::seq::SliceRandom;

use clap::Parser;
use image::{Rgb, RgbImage};
use rand::seq::SliceRandom;

use instructions::*;
use Instructions::*;

use crate::colors::Color;

use clap::Parser;
use colors::Color;
use instructions::{Instructions::*, *};

mod colors;
mod instructions;


@@ 21,13 17,17 @@ use spiral::*;
#[derive(Parser, Debug)]
#[clap(author, version, about, long_about=None)]
struct Args {
    /// Image name
    /// Input text file name
    #[clap()]
    input_file: String,

    /// Image size
    #[clap()]
    #[clap(short, long)]
    img_size: Option<u32>,

    /// Output image file name
    #[clap(short, long)]
    output: Option<String>,
}

fn main() {


@@ 36,19 36,17 @@ fn main() {
    let input = fs::read_to_string(args.input_file).expect("Failed to read input file");
    let instrs = text_to_instructions_prev(input);

    println!("{:?}", instrs);
    println!("num_instructions {:?}", instrs.len());
    //println!("{:?}", eval(&instrs));
    eprintln!("num_instructions: {:?}", instrs.len());

    let img_size = args.img_size.unwrap_or(expected_size(&instrs));
    println!("img_size: {:?}", img_size);
    println!("spiral_len: {:?}", Spiral2D::length(img_size));
    eprintln!("img_size: {:?}", img_size);
    eprintln!("spiral_len: {:?}", Spiral2D::length(img_size));

    let colors = instructions_to_blocks(&instrs, img_size);
    println!("img_size: {:?}", img_size);
    //println!("colors {:?}", colors);
    colors_to_img(colors, img_size, "test.png".to_string());
    println!("img_size: {:?}", img_size);
    eprintln!("img_size: {:?}", img_size);
    let img = colors_to_img(colors, img_size);
    img.save(args.output.unwrap_or("test.png".to_string()))
        .expect("Failed to write output image");
}

fn update_x_y_spiral(x: u32, y: u32, step: u32, dir: &Direction) -> (u32, u32) {


@@ 70,7 68,7 @@ fn instructions_to_blocks(instructions: &Vec<Instructions>, img_size: u32) -> Ve
    let mut direction = Direction::Right;

    for instr in instructions {
        //println!("remaining_space: {}", remaining_space);
        //eprintln!("remaining_space: {}", remaining_space);
        match instr {
            PUSH(n) => {
                // If there isn't enough room for the entire push instruction


@@ 152,7 150,7 @@ fn instructions_to_blocks(instructions: &Vec<Instructions>, img_size: u32) -> Ve
    colors
}

fn colors_to_img(colors: Vec<Color>, img_size: u32, img_name: String) {
fn colors_to_img(colors: Vec<Color>, img_size: u32) -> RgbImage {
    let mut img = RgbImage::new(img_size, img_size);

    let mut final_pos = (0, 0);


@@ 244,5 242,6 @@ fn colors_to_img(colors: Vec<Color>, img_size: u32, img_name: String) {
        img.put_pixel(x, y, Rgb(*picked_color));
    }

    img.save(img_name).expect("Failed to save image");
    //img.save(img_name).expect("Failed to save image");
    img
}

A test.png => test.png +0 -0