diff --git a/src/image.rs b/src/image.rs index 77bf829..a46e63e 100644 --- a/src/image.rs +++ b/src/image.rs @@ -1,22 +1,26 @@ #[derive(Debug, Eq, PartialEq)] pub struct Image { - pub pixels: Vec, // 64 for SD, 256 for HD + pub pixels: Vec, // 64 for SD, 256 for HD } impl From for Image { #[inline] fn from(string: String) -> Image { - let string = string.replace("\n", ""); + print!("image: \n{}", string); let string = string.replace("NaN", "0"); - let pixels: Vec<&str> = string.split("").collect(); - // the above seems to add an extra "" at the start and end of the vec, so strip them below - let pixels = &pixels[1..(pixels.len() - 1)]; - let pixels: Vec = pixels - .iter() - .map(|&pixel| - pixel.parse::().expect(&format!("Bad pixel in image: {}\n", pixel)) - ) - .collect(); + let string = string.trim(); + let lines: Vec<&str> = string.lines().collect(); + let dimension = lines.len(); + let mut pixels: Vec = Vec::new(); + + for line in lines { + let line = &line[..dimension]; + println!("line: {}", line); + for char in line.chars().into_iter() { + println!("Char: {} value: {}", char, match char {'1' => 1, _ => 0}); + pixels.push(match char {'1' => 1, _ => 0}); + } + } Image { pixels } }