19 lines
695 B
Rust
19 lines
695 B
Rust
use std::{fs::File, io::{Error, Write}};
|
|
|
|
pub trait Output {
|
|
fn write(filename: &str, pixels: &Vec<u8>, width: usize, height: usize) -> Result<File, Error>;
|
|
}
|
|
|
|
pub struct P3 {}
|
|
impl Output for P3 {
|
|
fn write(filename: &str, pixels: &Vec<u8>, width: usize, height: usize) -> Result<File, Error> {
|
|
let mut file = File::create(filename)?;
|
|
file.write(format!("P3\n{} {}\n255\n", width, height).as_bytes())?;
|
|
let lines: Result<Vec<usize>, Error> = pixels
|
|
.chunks(3)
|
|
.map(|chunk| format!("{} {} {}\n", chunk[0], chunk[1], chunk[2]))
|
|
.map(|line| file.write(line.as_bytes()))
|
|
.collect();
|
|
lines.map(|_v| file)
|
|
}
|
|
} |