//! Portable PixMap format images.

use crate::durandal::{err::*, image::*};
use std::io;

/// Writes a PPM file from an image.
///
/// # Errors
///
/// Errors if `out` cannot be written to.
pub fn write_ppm(out: &mut impl io::Write, im: &impl Image) -> ResultS<()>
{
   write!(out, "P3\n{} {}\n{}\n", im.w(), im.h(), u16::max_value())?;

   for y in 0..im.h() {
      for x in 0..im.w() {
         let cr = im.index(x, y);
         write!(out, "{} {} {} ", cr.r(), cr.g(), cr.b())?;
      }

      out.write_all(b"\n")?;
   }

   Ok(())
}

// EOF
