diff --git a/Cargo.lock b/Cargo.lock index d63a2ff2..c6801d15 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -755,6 +755,15 @@ dependencies = [ "libc", ] +[[package]] +name = "craft-cms" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=6c09292724ec8dbe32950cb5042cfaa2a614fb6c#6c09292724ec8dbe32950cb5042cfaa2a614fb6c" +dependencies = [ + "md-5", + "rayon", +] + [[package]] name = "craft-dialogs" version = "0.1.0" @@ -2499,6 +2508,16 @@ version = "0.3.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4facc753ae494aeb6e3c22f839b158aebd4f9270f55cd3c79906c45476c47ab4" +[[package]] +name = "md-5" +version = "0.10.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d89e7ee0cfbedfc4da3340218492196241d89eefb6dab27de5df917a6d2e78cf" +dependencies = [ + "cfg-if", + "digest", +] + [[package]] name = "memchr" version = "2.8.3" @@ -4414,6 +4433,7 @@ name = "texturecraft-formats" version = "0.6.1" dependencies = [ "base64 0.22.1", + "craft-cms", "craft-materialx", "craft-mcmat", "craft-mesh", diff --git a/crates/formats/Cargo.toml b/crates/formats/Cargo.toml index 756b2a78..795e066e 100644 --- a/crates/formats/Cargo.toml +++ b/crates/formats/Cargo.toml @@ -9,6 +9,8 @@ license.workspace = true workspace = true [dependencies] +# ICC v2 serialization prerequisite: craft-libs#1735; app colour policy stays local. +craft-cms-icc-v2 = { package = "craft-cms", version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "6c09292724ec8dbe32950cb5042cfaa2a614fb6c" } craft-materialx = { workspace = true } craft-mesh = { workspace = true } craft-mcmat = { workspace = true } diff --git a/crates/formats/src/icc.rs b/crates/formats/src/icc.rs index 9323f561..f0f16a84 100644 --- a/crates/formats/src/icc.rs +++ b/crates/formats/src/icc.rs @@ -1,63 +1,7 @@ -//! 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. - +//! ICC v2.1 export policy; byte serialization is shared with craft-cms. +use craft_cms_icc_v2::write_v2::{MatrixProfile, ToneCurve, matrix_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 { - 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 { - 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 { - 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 { - 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 { @@ -68,64 +12,41 @@ fn description(space: ColorSpace) -> &'static str { } } -/// The ICC profile of `space`, or None if its primaries are degenerate (never for the built-in ones). +/// The ICC profile of `space`, or None if its primaries are degenerate. pub fn profile(space: ColorSpace) -> Option> { - 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); 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 = 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) + let matrix = space.to_xyz_d50()?; + let samples: Vec = if space == ColorSpace::Srgb { + (0..CURVE_POINTS) + .map(|i| { + let v = f64::from(srgb_to_linear(i as f32 / (CURVE_POINTS - 1) as f32)); + (v * 65535.0).round().clamp(0.0, 65535.0) as u16 + }) + .collect() + } else { + Vec::new() + }; + matrix_profile(&MatrixProfile { + matrix, + curve: if samples.is_empty() { ToneCurve::Linear } else { ToneCurve::Samples(&samples) }, + description: description(space), + copyright: "No copyright, generated by TextureCraft", + date: [2026, 1, 1, 0, 0, 0], + }) } +#[cfg(test)] +#[path = "icc_legacy_test.rs"] +mod legacy; + #[cfg(test)] mod tests { use super::*; + #[test] + fn all_spaces_match_legacy_wire_bytes() { + for space in ColorSpace::ALL { + assert_eq!(profile(space), legacy::profile(space), "{space:?}"); + } + } fn u32_at(b: &[u8], i: usize) -> u32 { u32::from_be_bytes([b[i], b[i + 1], b[i + 2], b[i + 3]]) diff --git a/crates/formats/src/icc_legacy_test.rs b/crates/formats/src/icc_legacy_test.rs new file mode 100644 index 00000000..47b4e600 --- /dev/null +++ b/crates/formats/src/icc_legacy_test.rs @@ -0,0 +1,124 @@ +//! 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 { + 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 { + 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 { + 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 { + 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> { + 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); 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 = 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) +}