use colorspace::*;
fn main() {
let (px_in_u8, width, height) = read_png("resources/marcie_sRGB.png");
let px_srgb: Vec<_> = px_in_u8
.chunks_exact(3)
.map(|p| {
rgbf32(
p[0] as f32 / 255.0,
p[1] as f32 / 255.0,
p[2] as f32 / 255.0,
)
})
.collect();
let srgb = &color_space_rgb::model_f32::SRGB;
let adobe_rgb = &color_space_rgb::model_f32::ADOBE_RGB_1998;
let alexa_wide = &color_space_rgb::model_f32::ALEXA_WIDE_GAMUT;
let dci_p3 = &color_space_rgb::model_f32::DCI_P3;
let mut px_adobe_rgb_u8 = vec![rgbu8(0, 0, 0); px_srgb.len()];
let mut px_alexa_wide_u8 = vec![rgbu8(0, 0, 0); px_srgb.len()];
let mut px_dci_p3_u8 = vec![rgbu8(0, 0, 0); px_srgb.len()];
rgb_to_rgb(srgb, adobe_rgb, &px_srgb, &mut px_adobe_rgb_u8);
rgb_to_rgb(srgb, alexa_wide, &px_srgb, &mut px_alexa_wide_u8);
rgb_to_rgb(srgb, dci_p3, &px_srgb, &mut px_dci_p3_u8);
write_png(
"marcie_AdobeRGB.png",
width,
height,
rgbu8_slice_as_u8(&px_adobe_rgb_u8),
);
write_png(
"marcie_AlexaWide.png",
width,
height,
rgbu8_slice_as_u8(&px_alexa_wide_u8),
);
write_png(
"marcie_DCI-P3.png",
width,
height,
rgbu8_slice_as_u8(&px_dci_p3_u8),
);
}
use std::fs::File;
fn read_png(filename: &str) -> (Vec<u8>, u32, u32) {
let decoder = png::Decoder::new(File::open(filename).unwrap());
let (info, mut reader) = decoder.read_info().unwrap();
let (width, height) = reader.info().size();
let mut px_in_u8 = vec![0; info.buffer_size()];
reader.next_frame(&mut px_in_u8).unwrap();
(px_in_u8, width, height)
}
fn write_png(filename: &str, width: u32, height: u32, pixels: &[u8]) {
use std::io::BufWriter;
let file = File::create(filename).unwrap();
let ref mut w = BufWriter::new(file);
let mut encoder = png::Encoder::new(w, width, height);
encoder.set_color(png::ColorType::RGB);
encoder.set_depth(png::BitDepth::Eight);
let mut writer = encoder.write_header().unwrap();
writer.write_image_data(pixels).unwrap();
}
pub fn rgbu8_slice_as_u8(slice: &[RGBu8]) -> &[u8] {
unsafe {
std::slice::from_raw_parts(slice.as_ptr() as *const u8, slice.len() * 3)
}
}