csci5607/assignment-0/src/ppm.rs

27 lines
581 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 {
2023-01-21 08:22:22 +00:00
pub(crate) width: usize,
pub(crate) height: usize,
pub(crate) data: Vec<Pixel>,
2023-01-21 08:03:11 +00:00
}
impl Ppm {
2023-01-21 08:22:22 +00:00
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())?;
2023-01-21 08:03:11 +00:00
2023-01-21 08:22:22 +00:00
// 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())?;
2023-01-21 08:03:11 +00:00
}
2023-01-21 08:22:22 +00:00
Ok(())
}
2023-01-21 08:03:11 +00:00
}