2023-02-06 03:52:42 +00:00
|
|
|
use std::io::{Result, Write};
|
|
|
|
|
|
|
|
use nalgebra::Vector3;
|
|
|
|
|
|
|
|
/// A pixel color represented by a red, green, and blue value in the range 0-1.
|
|
|
|
pub type Color = Vector3<f64>;
|
|
|
|
|
|
|
|
/// A representation of an image
|
|
|
|
pub struct Image {
|
|
|
|
/// Width in pixels
|
2023-02-15 08:36:53 +00:00
|
|
|
pub width: usize,
|
2023-02-06 03:52:42 +00:00
|
|
|
|
|
|
|
/// Height in pixels
|
2023-02-15 08:36:53 +00:00
|
|
|
pub height: usize,
|
2023-02-06 03:52:42 +00:00
|
|
|
|
|
|
|
/// Pixel data in row-major form.
|
2023-02-15 08:36:53 +00:00
|
|
|
pub data: Vec<Color>,
|
2023-02-06 03:52:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Image {
|
|
|
|
/// Write the image in PPM format to a file.
|
|
|
|
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
|
|
|
|
assert_eq!(self.data.len(), self.width * self.height);
|
|
|
|
|
|
|
|
for pixel in self.data.iter() {
|
|
|
|
let pixel = pixel * 256.0;
|
|
|
|
let red = pixel.x as u8;
|
|
|
|
let green = pixel.y as u8;
|
|
|
|
let blue = pixel.z as u8;
|
|
|
|
let pixel = format!("{red} {green} {blue}\n");
|
|
|
|
w.write_all(pixel.as_bytes())?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|