use super::icc_profile;
use image::{ExtendedColorType, ImageEncoder, codecs::png::PngEncoder};
use indicatrix::color::ColorSpace;
pub use indicatrix::renderer::tonemap::tonemap_accumulation;
use std::{io::BufWriter, path::Path};
pub(super) fn save_png(
path: &Path,
width: u32,
height: u32,
rgba: &[u8],
color_space: ColorSpace,
) -> Result<(), String> {
if color_space == ColorSpace::Srgb {
return image::RgbaImage::from_raw(width, height, rgba.to_vec()).map_or_else(
|| Err("Internal error: pixel buffer size did not match dimensions.".to_string()),
|img| img.save(path).map_err(|e| e.to_string()),
);
}
let file = std::fs::File::create(path).map_err(|e| e.to_string())?;
let mut encoder = PngEncoder::new(BufWriter::new(file));
encoder
.set_icc_profile(icc_profile::build(color_space))
.map_err(|e| e.to_string())?;
encoder
.write_image(rgba, width, height, ExtendedColorType::Rgba8)
.map_err(|e| e.to_string())
}