~jpl8/text-to-piet

416e2fda36df112cb0f5b80cb1e111a98c2bbd22 — jpl 1 year, 6 months ago 0737314
Clean up old comments and terminator placement logic.
2 files changed, 18 insertions(+), 27 deletions(-)

M src/instructions.rs
M src/main.rs
M src/instructions.rs => src/instructions.rs +0 -9
@@ 156,8 156,6 @@ pub fn expected_size(instructions: &Vec<Instructions>) -> Result<usize> {
            <= (spiral::Spiral2D::length(*side as u32) as usize)
    };

    let lower_bound_on_min_img_size = search_for_minimum_satisfier(eq_to_minimize, 1..usize::MAX);

    let min_img_size_range = match search_for_minimum_satisfier(eq_to_minimize, 1..usize::MAX) {
        Some(lower_bound) => {
            let upper_bound = lower_bound + std::cmp::min(num_pushes, num_corners(&lower_bound));


@@ 168,13 166,6 @@ pub fn expected_size(instructions: &Vec<Instructions>) -> Result<usize> {

    Ok(min_img_size_range?.0)

    // solving for side should work out to

    //4 + (((2 * n - 8) as f64).sqrt() as u32) + 1
    //2 + ((2 * n - 8) as f64).sqrt() as u32
    //3 + ((2 * n - 9) as f64).sqrt() as u32
    //10 + ((2.5 * (num_blocks as f64)) as f64).sqrt() as u32
    //2 + ((2 * num_blocks - 8) as f64).sqrt() as u32
    // ((2 * num_blocks + corner_turn_len * corner_turn_len - 6 * corner_turn_len + 2) as f64).sqrt()
    //     as u32
    //     + corner_turn_len as u32

M src/main.rs => src/main.rs +18 -18
@@ 5,7 5,7 @@ use clap::Parser;
use image::{Rgb, RgbImage};
use rand::seq::SliceRandom;

use anyhow::Result;
use anyhow::{bail, Context, Result};

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


@@ 46,7 46,8 @@ fn main() -> Result<()> {

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



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

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

    let mut final_pos = (0, 0);


@@ 221,33 222,32 @@ fn colors_to_img(colors: Vec<Color>, img_size: u32) -> RgbImage {
        img.put_pixel(x, y, Rgb(*picked_color));
    }

    let (mut x, mut y) = final_pos;
    let (x, y) = final_pos;

    // SAFE: There is always the starting color in colors
    let color = colors.last().unwrap();

    // Check if it is safe to add the terminator
    let terminator_region_corner = {
        let (square_x, square_y) = update_x_y_spiral(x, y, 2, &final_dir);
        update_x_y_spiral(square_x, square_y, 1, &final_dir.clockwise())
    };
    {
        let terminator_region_corner = {
            let (square_x, square_y) = update_x_y_spiral(x, y, 2, &final_dir);
            update_x_y_spiral(square_x, square_y, 1, &final_dir.clockwise())
        };

    let terminator_region = get_rect((5, 4), terminator_region_corner, &final_dir.clockwise());
        let terminator_region = get_rect((5, 4), terminator_region_corner, &final_dir.clockwise());

    let all_pixels_in_region_are_black = terminator_region
        .iter()
        .all(|&(x, y)| img.get_pixel(x, y) == &Rgb(Color::Black.to_bytes()));
        let all_pixels_in_region_are_black = terminator_region
            .iter()
            .all(|&(x, y)| img.get_pixel(x, y) == &Rgb(Color::Black.to_bytes()));

    // TODO: Add error handling with anyhow
    if !all_pixels_in_region_are_black {
        panic!("UNABLE TO ADD TERMINATOR!");
        if !all_pixels_in_region_are_black {
            bail!("Unable to add terminator to piet image due to insuficient space");
        }
    }

    // Add terminator
    add_terminator(&mut img, final_pos, color, &final_dir);

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

/// Creates a square that starts at the given position, clockwise