27 lines
637 B
Rust
27 lines
637 B
Rust
|
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(())
|
||
|
}
|
||
|
}
|