use std::path::Path;
use image::save_buffer_with_format;
use image::ColorType;
use image::ImageFormat;
use log::error;
use crate::clamp;
use crate::core::geometry::Bounds2i;
use crate::core::geometry::Point2i;
use crate::gamma_correct;
use crate::Float;
fn to_byte(v: Float) -> u8 {
clamp(255. * gamma_correct(v) + 0.5, 0., 255.) as u8
}
pub fn write_image(
name: &str,
rgb: &Vec<Float>,
output_bounds: Bounds2i,
_total_resolution: Point2i,
) {
let resolution = output_bounds.diagonal();
match Path::new(name)
.extension()
.expect("file has no extension")
.to_str()
.expect("filename not ascii")
.to_ascii_lowercase()
.as_str()
{
"png" => {
let rgb8: Vec<u8> = rgb.iter().map(|f| to_byte(*f)).collect();
if let Err(err) = save_buffer_with_format(
name,
&rgb8,
resolution.x as u32,
resolution.y as u32,
ColorType::RGB(8),
ImageFormat::PNG,
) {
error!("Failed to write PNG To {}: {}", name, err);
}
}
"exr" => unimplemented!("writing .exr files is not implemented"),
"tga" => unimplemented!("writing .tga files is not implemented"),
"pfm" => unimplemented!("writing .pfm files is not implemented"),
ext @ _ => error!("unknown file extension {}", ext),
}
}