csci5607/assignment-0/src/ppm.rs

27 lines
637 B
Rust
Raw Normal View History

2023-01-21 08:03:11 +00:00
use std::io::{Result, Write};
pub struct Pixel(pub u8, pub u8, pub u8);
pub struct Ppm {
pub(crate) width: usize,
pub(crate) height: usize,
pub(crate) data: Vec<Pixel>,
}
impl Ppm {
pub fn write(&self, mut w: impl Write) -> Result<()> {
// Header
let header = format!("P3 {} {} 255\n", self.width, self.height);
w.write_all(header.as_bytes())?;
// Pixel data
for pixel in self.data.iter() {
let Pixel(red, green, blue) = pixel;
let pixel = format!("{red} {green} {blue}\n");
w.write_all(pixel.as_bytes())?;
}
Ok(())
}
}