~jpl8/piet_interpreter

cc045d8bd2804702afa9e2599e7fca9e406bd0cf — jpl 2 years ago 58c98b1
add codel_size arg
3 files changed, 52 insertions(+), 4 deletions(-)

M src/interpreter/mod.rs
M src/main.rs
M src/translator.rs
M src/interpreter/mod.rs => src/interpreter/mod.rs +7 -0
@@ 24,6 24,13 @@ impl Interpreter {
        Ok(Interpreter::from_img(img))
    }

    pub fn with_codel_size<P>(path: P, codel_size: u32) -> io::Result<Self>
    where P: AsRef<Path>
    {
        let img = translator::with_codel_size(path, codel_size)?;
        Ok(Interpreter::from_img(img))
    }

    pub fn from_img(img: PietImage) -> Self { 
        Self {
            img, 

M src/main.rs => src/main.rs +14 -3
@@ 13,6 13,7 @@ fn main() {
        .arg(
            Arg::with_name("input")
            .help("Input image")
            .value_name("FILE")
            .index(1)
            .required(true)
        )


@@ 23,9 24,11 @@ fn main() {
            .help("Enable debug information")
        )
        .arg(
            Arg::with_name("codel_size")
            Arg::with_name("codel-size")
            .short("c")
            .long("codel_size")
            .long("codel-size")
            .value_name("CODEL-SIZE")
            .takes_value(true)
            .help("Codel size to interpret")
        )
        .get_matches();


@@ 33,7 36,15 @@ fn main() {

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

        let mut interpreter = Interpreter::new(&input)
        let mut interpreter = if matches.is_present("codel-size") {
            let codel_size: u32 = matches.value_of("codel-size").unwrap()
                .parse().expect("Invalid codel size");

            Interpreter::with_codel_size(&input, codel_size)
        }
        else {
            Interpreter::new(&input)
        }
                .expect(format!("Error opening file {}!", input).as_str());

        interpreter.set_debug( matches.is_present("debug") );

M src/translator.rs => src/translator.rs +31 -1
@@ 35,7 35,37 @@ where P: AsRef<Path>
    return Ok(PietImage::new(dimensions, upscaled_codels));
    

} 
}

pub fn with_codel_size<P>(path: P, codel_size: u32) -> io::Result<PietImage>
where P: AsRef<Path>
{
    let img_read = ImageReader::open(path)?;

    let img = img_read.decode().expect("Failed to decode! Image type unsupported!");
    let rgb_img = img.to_rgb8();


    let codels: Vec<Vec<Codel>> = rgb_img.rows().map(|row| row.into_iter()
                                    .map(|Rgb(bytes)| Codel::from_bytes(*bytes))
                                    .collect()
                                   ).collect();

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


    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();


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


}

pub fn codel_size(codels: &Vec<Vec<Codel>>) -> u32 {
    let mut min_width = usize::MAX;
    for row in codels {