~jpl8/piet_interpreter

5e08ebea91d21060103dc6fe08dbac235a2eed8e — jpl 2 years ago 91fc2a2
fixed upscale
M .gitignore => .gitignore +1 -0
@@ 1,2 1,3 @@
/target
/spec
out

M Cargo.lock => Cargo.lock +86 -1
@@ 15,6 15,26 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aae1277d39aeec15cb388266ecc24b11c80469deae6067e17a1a7aa9e5c1f234"

[[package]]
name = "ansi_term"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b"
dependencies = [
 "winapi",
]

[[package]]
name = "atty"
version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
dependencies = [
 "hermit-abi",
 "libc",
 "winapi",
]

[[package]]
name = "autocfg"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"


@@ 45,6 65,21 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"

[[package]]
name = "clap"
version = "2.33.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "37e58ac78573c40708d45522f0d80fa2f01cc4f9b4e2bf749807255454312002"
dependencies = [
 "ansi_term",
 "atty",
 "bitflags",
 "strsim",
 "textwrap",
 "unicode-width",
 "vec_map",
]

[[package]]
name = "color_quant"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"


@@ 259,8 294,9 @@ dependencies = [

[[package]]
name = "piet"
version = "0.1.0"
version = "0.2.0"
dependencies = [
 "clap",
 "image",
]



@@ 314,6 350,21 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd"

[[package]]
name = "strsim"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a"

[[package]]
name = "textwrap"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060"
dependencies = [
 "unicode-width",
]

[[package]]
name = "tiff"
version = "0.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"


@@ 325,7 376,41 @@ dependencies = [
]

[[package]]
name = "unicode-width"
version = "0.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9337591893a19b88d8d87f2cec1e73fad5cdfd10e5a6f349f498ad6ea2ffb1e3"

[[package]]
name = "vec_map"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191"

[[package]]
name = "weezl"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d8b77fdfd5a253be4ab714e4ffa3c49caf146b4de743e97510c0656cf90f1e8e"

[[package]]
name = "winapi"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
dependencies = [
 "winapi-i686-pc-windows-gnu",
 "winapi-x86_64-pc-windows-gnu",
]

[[package]]
name = "winapi-i686-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"

[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"

M Cargo.toml => Cargo.toml +1 -0
@@ 6,4 6,5 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
clap = "2.33.3"
image = "0.23.14"

A images/Piet_hello2_big.png => images/Piet_hello2_big.png +0 -0
A images/adder_big.png => images/adder_big.png +0 -0
A images/erat2.png => images/erat2.png +0 -0
A images/hi.png => images/hi.png +0 -0
A images/primetest.png => images/primetest.png +0 -0
A images/test_upscale.png => images/test_upscale.png +0 -0
M src/color.rs => src/color.rs +30 -0
@@ 1,3 1,5 @@
use std::fmt;

#[allow(dead_code)]
#[derive(PartialEq, Eq, Copy, Clone, Debug)]
pub enum Color {


@@ 7,6 9,34 @@ pub enum Color {
    Unknown,
}

impl fmt::Display for Color {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Color::LightRed     => write!(f, "Light Red"),
            Color::LightYellow  => write!(f, "Light Yello"),
            Color::LightGreen   => write!(f, "Light Green"),
            Color::LightCyan    => write!(f, "Light Cyan"),
            Color::LightBlue    => write!(f, "Light Blue"),
            Color::LightMagenta => write!(f, "Light Magenta"),
            Color::Red          => write!(f, "Red"),
            Color::Yellow       => write!(f, "Yellow"),
            Color::Green        => write!(f, "Green"),
            Color::Cyan         => write!(f, "Cyan"),
            Color::Blue         => write!(f, "Blue"),
            Color::Magenta      => write!(f, "Magenta"),
            Color::White        => write!(f, "White"),
            Color::Black        => write!(f, "Black"),
            Color::DarkRed      => write!(f, "Dark Red"),
            Color::DarkYellow   => write!(f, "Dark Yellow"),
            Color::DarkGreen    => write!(f, "Dark Green"),
            Color::DarkCyan     => write!(f, "Dark Cyan"),
            Color::DarkBlue     => write!(f, "Dark Blue"),
            Color::DarkMagenta  => write!(f, "Dark Magenta"),
            Color::Unknown      => write!(f, "Unknown"),
        }
    }
}

impl Color {
    pub fn to_color(color: [u8; 3]) -> Self {
        if color == [0xFF, 0xC0, 0xC0] { return Color::LightRed;     }

M src/interpreter/mod.rs => src/interpreter/mod.rs +7 -23
@@ 11,6 11,7 @@ pub struct Interpreter {
    img: PietImage,
    head: Head,
    stack: Vec<isize>,
    debug: bool,
}

#[allow(dead_code)]


@@ 28,11 29,16 @@ impl Interpreter {
            img, 
            head: Head::default(),
            stack: vec!(),
            debug: false,
        }
    }

    pub fn head(&self) -> &Head { &self.head }
    pub fn head_as_mut(&mut self) -> &mut Head { &mut self.head }
    
    pub fn debug(&self) -> bool { self.debug }
    pub fn set_debug(&mut self, debug: bool) { self.debug = debug }

    pub fn img(&self) -> &PietImage { &self.img }
    pub fn stack(&self) -> &Vec<isize> { &self.stack }
    pub fn stack_as_mut(&mut self) -> &mut Vec<isize> { &mut self.stack }


@@ 55,30 61,8 @@ impl Interpreter {

    }

    pub fn debug(&mut self) -> io::Result<()> {

       loop {
            let curr = self.head.pos();
            if let Some(next) = self.move_next() {

                let curr_codel = self.img().get(curr.0, curr.1);
                let next_codel = self.img().get(next.0, next.1);

                println!("Stack: {:?}", self.stack);
                println!("In: {:?}, Next: {:?}, Op: {:?}", curr, next, rules::get_op(&curr_codel, &next_codel)); 
                println!("============");

                rules::do_op(self, &curr_codel, &next_codel)?;

            }

            else { return Ok(()) }

       }

    }
 
    fn move_next(&mut self) -> Option<(u32, u32)> {
   fn move_next(&mut self) -> Option<(u32, u32)> {
        let curr_codel = self.head.pos;
        let block = &self.img.get_block(curr_codel.0, curr_codel.1);
       

M src/interpreter/rules.rs => src/interpreter/rules.rs +23 -8
@@ 60,26 60,41 @@ pub fn do_op(interpreter: &mut Interpreter, curr: &Codel, next: &Codel) -> io::R
        match op {

            Op::Push => {
                

                let block_size = curr.block_size().expect("Block size set to None!") as isize;
                interpreter.stack_as_mut().push(block_size);

                if interpreter.debug {
                    eprintln!("Executed: Push({})", block_size);
                }

                Ok(())
            }

            Op::Pop => {
                interpreter.stack_as_mut().pop();
                let popped = interpreter.stack_as_mut().pop();
                if interpreter.debug {
                    if let Some(v) = popped {
                        eprintln!("Executed: Pop -> {}", v);
                    }
                    else {
                        eprintln!("Executed: Pop, but stack was empty");
                    }
                }
                Ok(())
            }

            Op::Add => {
                let first_pop  = interpreter.stack_as_mut().pop();
                let second_pop = interpreter.stack_as_mut().pop();
                if let (Some(a), Some(b)) = (first_pop, second_pop) {
                    interpreter.stack_as_mut().push(a + b);
                    Ok(())
                if interpreter.stack.len() >= 2 {
                    let first_pop  = interpreter.stack_as_mut().pop().unwrap();
                    let second_pop = interpreter.stack_as_mut().pop().unwrap();
                    interpreter.stack_as_mut().push(second_pop + first_pop);
                }
                else { 
                    Err(Error::new(ErrorKind::InvalidInput, "Not enough values on stack!"))
                else {
                    eprintln!("Not enough values on the stack! Were {}, needed 2", interpreter.stack.len());
                }
                Ok(())
            }

            Op::Sub => {

M src/main.rs => src/main.rs +25 -13
@@ 1,29 1,41 @@
use clap::{App, Arg};


use piet::interpreter::{Interpreter};

use std::env;


fn main() {

    let args: Vec<String> = env::args().collect();
    let matches = App::new("piet")
        .version("0.2.0")
        .about("A somewhat functioning piet interpreter")
        .arg(
            Arg::with_name("input")
            .help("Input image")
            .index(1)
            .required(true)
        )
        .arg(
            Arg::with_name("debug")
            .short("d")
            .long("debug")
            .help("Enable debug information")
        )
        .get_matches();

    if args.len() < 2 { 
        panic!("No image has been submited!");
    }

    if let Some(input) = matches.value_of("input") {

    let image = &args[1];
    let debug = if args.len() >= 3 { &args[2] } else { "" };
        let mut interpreter = Interpreter::new(&input)
                .expect(format!("Error opening file {}!", input).as_str());

    let mut interpreter = Interpreter::new(&image)
            .expect(format!("Error opening file {}!", image).as_str());
        interpreter.set_debug( matches.is_present("debug") );

    if debug == "--debug" {
        interpreter.debug().expect("Execution Failed!");
    }
    else {
        interpreter.exec().expect("Execution Failed!");
    }


}



M src/piet_img.rs => src/piet_img.rs +12 -0
@@ 1,4 1,5 @@
use std::collections::HashSet;
use std::fmt;

use crate::color::Color;



@@ 9,6 10,17 @@ pub struct Codel {
    block_size: Option<u32>,
}

impl fmt::Display for Codel {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if let Some(v) = self.block_size {
            write!(f, "Color: {}, Block Size: {}", self.color, v)
        }
        else {
            write!(f, "Color: {}", self.color)
        }
    }
}

impl Codel {
    pub fn from_color(color: Color) -> Self {
        Self {

M src/translator.rs => src/translator.rs +6 -9
@@ 27,21 27,18 @@ where P: AsRef<Path>
        }

        let codel_size = codel_size(&codels);

        let dimensions = (rgb_img.dimensions().0 / codel_size,
                         rgb_img.dimensions().1 / codel_size);


        let mut upscaled_codels = vec!();
        let row_len = codels[0].len();
        let col_len = codels.len();
        eprintln!("dimensions: {:?}", dimensions);

        for row in (0..row_len).step_by(codel_size as usize) {
            upscaled_codels.push(vec!());
            for col in (0..col_len).step_by(codel_size as usize) {
                upscaled_codels[row].push(codels[row][col]);
        let upscaled_codels = codels.into_iter().step_by((codel_size) as usize)
            .map(|row| row.into_iter().step_by((codel_size) as usize).collect())
            .collect();

            }
        }
        eprintln!("upscaled_codels: {:#?}", upscaled_codels);

        return Ok(PietImage::new(dimensions, upscaled_codels));
    }