~jpl8/text-to-piet

5919a2cfc4224f7be0e0d3ea69c62ba144393eac — jpl 1 year, 3 months ago ee044d1
Fix terminator not being added
3 files changed, 25 insertions(+), 35 deletions(-)

M src/main.rs
M src/piet_image.rs
D test.png
M src/main.rs => src/main.rs +0 -1
@@ 1,4 1,3 @@
use std::fmt::format;
use std::fs;

use clap::Parser;

M src/piet_image.rs => src/piet_image.rs +25 -34
@@ 12,6 12,7 @@ use crate::spiral::{Direction, Spiral2D};
use crate::utils;

// TODO: Make img an Option<RgbImage>
#[derive(Debug)]
pub struct PietImager {
    img_size: Option<u32>,
    img: Option<RgbImage>,


@@ 247,8 248,8 @@ impl PietImager {
        // Check if it is safe to add the terminator
        {
            let terminator_region_corner = {
                let (square_x, square_y) = self.update_x_y(x, y, 2, &final_dir);
                self.update_x_y(square_x, square_y, 1, &final_dir.clockwise())
                let (square_x, square_y) = Self::update_x_y(x, y, 2, &final_dir);
                Self::update_x_y(square_x, square_y, 1, &final_dir.clockwise())
            };

            let terminator_region =


@@ 263,7 264,7 @@ impl PietImager {
            }
        }
        // Add terminator
        self.add_terminator(final_pos, color, &final_dir);
        Self::add_terminator(&mut img, final_pos, color, &final_dir);
        self.img = Some(img.clone());
        Ok(img.clone())
    }


@@ 273,50 274,40 @@ impl PietImager {
    fn get_rect(&self, sides: (u32, u32), start: (u32, u32), dir: &Direction) -> Vec<(u32, u32)> {
        let mut blocks = vec![];
        for i in 0..sides.0 {
            let (x, y) = self.update_x_y(start.0, start.1, i, &dir.clockwise());
            let (x, y) = Self::update_x_y(start.0, start.1, i, &dir.clockwise());
            for j in 0..sides.1 {
                blocks.push(self.update_x_y(x, y, j, dir));
                blocks.push(Self::update_x_y(x, y, j, dir));
            }
        }
        blocks
    }

    fn add_terminator(&mut self, pos: (u32, u32), color: &Color, dir: &Direction) {
    fn add_terminator(img: &mut RgbImage, pos: (u32, u32), color: &Color, dir: &Direction) {
        let (mut x, mut y) = pos;
        self.img
            .as_mut()
            .map(|s| s.put_pixel(x, y, Rgb(color.to_bytes())));

        eprintln!("DIRECTION: {:?}", dir);
        img.put_pixel(x, y, Rgb(color.to_bytes()));
        eprintln!("POS: {:?}", (x, y));
        let color = colors::instruction_to_color(PUSH(1), *color);
        (x, y) = self.update_x_y(x, y, 1, &dir.clockwise());
        self.img
            .as_mut()
            .map(|s| s.put_pixel(x, y, Rgb(color.to_bytes())));
        (x, y) = Self::update_x_y(x, y, 1, &dir.clockwise());
        img.put_pixel(x, y, Rgb(color.to_bytes()));

        let color = colors::instruction_to_color(PUSH(1), color);
        (x, y) = self.update_x_y(x, y, 1, &dir.clockwise());
        self.img
            .as_mut()
            .map(|s| s.put_pixel(x, y, Rgb(color.to_bytes())));

        (x, y) = self.update_x_y(x, y, 1, &dir);
        self.img
            .as_mut()
            .map(|s| s.put_pixel(x, y, Rgb(color.to_bytes())));

        (x, y) = self.update_x_y(x, y, 2, &dir.clockwise().clockwise());
        self.img
            .as_mut()
            .map(|s| s.put_pixel(x, y, Rgb(color.to_bytes())));

        (x, y) = self.update_x_y(x, y, 1, &dir);
        (x, y) = self.update_x_y(x, y, 1, &dir.clockwise());
        self.img
            .as_mut()
            .map(|s| s.put_pixel(x, y, Rgb(color.to_bytes())));
        (x, y) = Self::update_x_y(x, y, 1, &dir.clockwise());
        img.put_pixel(x, y, Rgb(color.to_bytes()));

        (x, y) = Self::update_x_y(x, y, 1, &dir);
        img.put_pixel(x, y, Rgb(color.to_bytes()));

        (x, y) = Self::update_x_y(x, y, 2, &dir.clockwise().clockwise());
        img.put_pixel(x, y, Rgb(color.to_bytes()));

        (x, y) = Self::update_x_y(x, y, 1, &dir);
        (x, y) = Self::update_x_y(x, y, 1, &dir.clockwise());
        img.put_pixel(x, y, Rgb(color.to_bytes()));
    }

    fn update_x_y(&self, x: u32, y: u32, step: u32, dir: &Direction) -> (u32, u32) {
    fn update_x_y(x: u32, y: u32, step: u32, dir: &Direction) -> (u32, u32) {
        match dir {
            Direction::Up => (x, y - step),
            Direction::Down => (x, y + step),

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