rework image
This commit is contained in:
parent
76eca72699
commit
8d8515bb8a
26
src/image.rs
26
src/image.rs
|
@ -1,22 +1,26 @@
|
|||
#[derive(Debug, Eq, PartialEq)]
|
||||
pub struct Image {
|
||||
pub pixels: Vec<u32>, // 64 for SD, 256 for HD
|
||||
pub pixels: Vec<u8>, // 64 for SD, 256 for HD
|
||||
}
|
||||
|
||||
impl From<String> 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<u32> = pixels
|
||||
.iter()
|
||||
.map(|&pixel|
|
||||
pixel.parse::<u32>().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<u8> = 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 }
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue