use cmx::{profile::DisplayProfile, tag::RenderingIntent};
use image::{codecs::png::PngEncoder, ExtendedColorType, ImageEncoder, Rgb, RgbImage};
pub fn main() -> Result<(), Box<dyn std::error::Error>> {
let width = 300;
let height = 100;
let mut image = RgbImage::new(width, height);
for (x, _y, pixel) in image.enumerate_pixels_mut() {
if x < 100 {
*pixel = Rgb([255, 0, 0]); } else if x < 200 {
*pixel = Rgb([0, 255, 0]); } else {
*pixel = Rgb([0, 0, 255]); }
}
image.save("examples/srgb_test.png")?;
println!("Saved examples/srgb_test.png without color profile");
let srgb_profile = DisplayProfile::cmx_srgb(RenderingIntent::RelativeColorimetric);
let mut srgb_png_data = Vec::new();
let mut encoder = PngEncoder::new(&mut srgb_png_data);
encoder.set_icc_profile(srgb_profile.to_bytes()?)?;
encoder.write_image(image.as_raw(), width, height, ExtendedColorType::Rgb8)?;
std::fs::write("examples/srgb_profile_test.png", &srgb_png_data)?;
println!("Saved examples/srgb_profile_test.png with sRGB color profile");
let display_p3_profile = DisplayProfile::cmx_display_p3(RenderingIntent::RelativeColorimetric);
display_p3_profile
.clone()
.write("examples/display_p3.icc")?;
let mut display_p3_png_data = Vec::new();
let mut encoder = PngEncoder::new(&mut display_p3_png_data);
encoder.set_icc_profile(display_p3_profile.to_bytes()?)?;
encoder.write_image(image.as_raw(), width, height, ExtendedColorType::Rgb8)?;
std::fs::write("examples/display_p3_test.png", &display_p3_png_data)?;
println!("Saved examples/display_p3_test.png with Display P3 color profile");
let adobe_rgb = DisplayProfile::cmx_adobe_rgb(RenderingIntent::RelativeColorimetric);
adobe_rgb.clone().write("examples/display_p3.icc")?;
let mut adobe_rgb_png_data = Vec::new();
let mut encoder = PngEncoder::new(&mut adobe_rgb_png_data);
encoder.set_icc_profile(adobe_rgb.to_bytes()?)?;
encoder.write_image(image.as_raw(), width, height, ExtendedColorType::Rgb8)?;
std::fs::write("examples/adobe_rgb_test.png", &adobe_rgb_png_data)?;
println!("Saved examples/adobe_rgb_test.png with Adobe RGB color profile");
Ok(())
}