d84914ef1e5a
icc_legacy_test.rs
//! ICC profiles for the export colour spaces: a version 2.1 matrix/curve display profile, small
//! (about 1.2 KiB) and readable by every colour-managed viewer and editor.
//!
//! The colorants come from the space's primaries adapted to the D50 profile connection space
//! (see `ColorSpace::to_xyz_d50`). sRGB carries its transfer curve as a sampled table; the linear
//! spaces carry a gamma of 1. Written from the ICC.1:2001-04 specification; nothing is copied
//! from a colour-management library or from another profile.
use texturecraft_texel::{ColorSpace, srgb_to_linear};
/// Samples of the sRGB curve table.
const CURVE_POINTS: usize = 1024;
/// The D50 white of the profile connection space.
const D50_XYZ: [f64; 3] = [0.9642, 1.0, 0.8249];
fn s15f16(v: f64) -> [u8; 4] {
((v * 65536.0).round().clamp(f64::from(i32::MIN), f64::from(i32::MAX)) as i32).to_be_bytes()
}
fn xyz_tag(v: [f64; 3]) -> Vec<u8> {
let mut b = b"XYZ \0\0\0\0".to_vec();
for c in v {
b.extend_from_slice(&s15f16(c));
}
b
}
fn text_tag(text: &str) -> Vec<u8> {
let mut b = b"text\0\0\0\0".to_vec();
b.extend_from_slice(text.as_bytes());
b.push(0);
b
}
/// `textDescriptionType` with the ASCII description only.
fn desc_tag(text: &str) -> Vec<u8> {
let mut b = b"desc\0\0\0\0".to_vec();
b.extend_from_slice(&(text.len() as u32 + 1).to_be_bytes());
b.extend_from_slice(text.as_bytes());
b.push(0);
// No Unicode text and no Macintosh ScriptCode text: empty fields and the 67-byte filler.
b.extend_from_slice(&[0; 4 + 4 + 2 + 1 + 67]);
b
}
fn curve_tag(space: ColorSpace) -> Vec<u8> {
let mut b = b"curv\0\0\0\0".to_vec();
if space == ColorSpace::Srgb {
b.extend_from_slice(&(CURVE_POINTS as u32).to_be_bytes());
for i in 0..CURVE_POINTS {
let v = f64::from(srgb_to_linear(i as f32 / (CURVE_POINTS - 1) as f32));
b.extend_from_slice(&((v * 65535.0).round().clamp(0.0, 65535.0) as u16).to_be_bytes());
}
} else {
// One entry is a gamma, u8Fixed8: 1.0 is linear.
b.extend_from_slice(&1u32.to_be_bytes());
b.extend_from_slice(&0x0100u16.to_be_bytes());
}
b
}
fn description(space: ColorSpace) -> &'static str {
match space {
ColorSpace::Srgb => "sRGB (Rec.709 primaries, sRGB curve)",
ColorSpace::LinearRec709 => "Linear Rec.709 (sRGB primaries, D65)",
ColorSpace::AcesCg => "ACEScg (AP1 primaries, linear)",
ColorSpace::Aces2065 => "ACES2065-1 (AP0 primaries, linear)",
}
}
/// The ICC profile of `space`, or None if its primaries are degenerate (never for the built-in ones).
pub fn profile(space: ColorSpace) -> Option<Vec<u8>> {
let m = space.to_xyz_d50()?;
let column = |c: usize| [m[0][c], m[1][c], m[2][c]];
let curve = curve_tag(space);
// The green and blue curves are the red one's entry.
let tags: [(&[u8; 4], Vec<u8>); 9] = [
(b"desc", desc_tag(description(space))),
(b"cprt", text_tag("No copyright, generated by TextureCraft")),
(b"wtpt", xyz_tag(D50_XYZ)),
(b"rXYZ", xyz_tag(column(0))),
(b"gXYZ", xyz_tag(column(1))),
(b"bXYZ", xyz_tag(column(2))),
(b"rTRC", curve.clone()),
(b"gTRC", curve.clone()),
(b"bTRC", curve),
];
// Header (128) + tag count (4) + 12 per tag, then the tag data, each tag padded to 4 bytes.
let table_end = 128 + 4 + 12 * tags.len();
let mut offsets = Vec::new();
let mut data: Vec<u8> = Vec::new();
for (i, (_, bytes)) in tags.iter().enumerate() {
let shared = i >= 7;
let at = if shared { offsets.get(6).copied().unwrap_or(0) } else { table_end + data.len() };
offsets.push(at);
if !shared {
data.extend_from_slice(bytes);
while !data.len().is_multiple_of(4) {
data.push(0);
}
}
}
let total = table_end + data.len();
let mut out = vec![0u8; 128];
out.get_mut(0..4)?.copy_from_slice(&(total as u32).to_be_bytes());
out.get_mut(8..12)?.copy_from_slice(&[2, 0x10, 0, 0]); // version 2.1.0
out.get_mut(12..16)?.copy_from_slice(b"mntr");
out.get_mut(16..20)?.copy_from_slice(b"RGB ");
out.get_mut(20..24)?.copy_from_slice(b"XYZ ");
// A fixed date (2026-01-01 00:00:00), so a space always gives the same bytes.
out.get_mut(24..36)?.copy_from_slice(&[0x07, 0xEA, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0]);
out.get_mut(36..40)?.copy_from_slice(b"acsp");
for (k, v) in D50_XYZ.iter().enumerate() {
out.get_mut(68 + 4 * k..72 + 4 * k)?.copy_from_slice(&s15f16(*v));
}
out.extend_from_slice(&(tags.len() as u32).to_be_bytes());
for ((sig, bytes), at) in tags.iter().zip(&offsets) {
out.extend_from_slice(*sig);
out.extend_from_slice(&(*at as u32).to_be_bytes());
out.extend_from_slice(&(bytes.len() as u32).to_be_bytes());
}
out.extend_from_slice(&data);
Some(out)
}
No file is open. Choose one in the tree, or press Ctrl+O (⌘O on a Mac) to find one by name.
Syntax highlighting could not load. The unmodified read-only text remains available above.