Skip to main content

rust_motion_cms/
lib.rs

1//! Photocraft colour management: a pure-Rust ICC v2/v4 colour management module.
2//!
3//! * [`Profile`] parses ICC profiles (matrix/TRC RGB, gray TRC, `mft1`/`mft2`/`mAB `/`mBA `
4//!   LUT profiles incl. CMYK↔Lab, `curv`/`para` curves) and writes ICC v4 profiles.
5//! * [`Transform`] connects two profiles through the PCS (XYZ/Lab D50) with a rendering
6//!   [`Intent`] and optional black point compensation, and converts 8/16-bit and float pixel
7//!   buffers through precomputed tables (tetrahedral 3D/4D interpolation, rayon on native).
8//! * [`builtin`] ships sRGB, Display P3, Adobe RGB (1998)-compatible, ProPhoto-compatible,
9//!   linear sRGB, Rec. 2020, Gray 2.2, sGray, Lab D50 and a synthetic coated CMYK profile.
10//! * [`Lut3d`] samples a display transform into a 3D texture for the GPU canvas.
11//!
12//! Implemented from the public ICC.1:2010 (v4.3) and ICC.1:2001-04 (v2) specifications,
13//! CIE 15, and Adobe's published black point compensation paper. This crate depends on no
14//! other workspace crate.
15#![forbid(unsafe_code)]
16#![cfg_attr(
17    not(test),
18    deny(
19        clippy::unwrap_used,
20        clippy::expect_used,
21        clippy::panic,
22        clippy::unimplemented,
23        clippy::todo,
24        clippy::unreachable
25    )
26)]
27
28pub mod builtin;
29pub mod clut;
30pub mod curve;
31pub mod gamut;
32pub mod lut3d;
33pub mod lutfile;
34pub mod math;
35pub mod pipeline;
36pub mod profile;
37pub mod synth;
38pub mod transform;
39mod write;
40
41pub use builtin::Builtin;
42pub use clut::Clut;
43pub use curve::Curve;
44pub use gamut::GamutCheck;
45pub use lut3d::Lut3d;
46pub use profile::{ColorSpace, Pcs, Profile, ProfileClass};
47pub use transform::{black_point, cached, SampleKind, Transform, TransformOptions};
48
49/// ICC rendering intent.
50#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Default)]
51pub enum Intent {
52    Perceptual = 0,
53    #[default]
54    RelativeColorimetric = 1,
55    Saturation = 2,
56    AbsoluteColorimetric = 3,
57}
58
59impl Intent {
60    pub const ALL: [Intent; 4] = [
61        Intent::Perceptual,
62        Intent::RelativeColorimetric,
63        Intent::Saturation,
64        Intent::AbsoluteColorimetric,
65    ];
66
67    pub fn from_u32(v: u32) -> Option<Intent> {
68        Some(match v {
69            0 => Intent::Perceptual,
70            1 => Intent::RelativeColorimetric,
71            2 => Intent::Saturation,
72            3 => Intent::AbsoluteColorimetric,
73            _ => return None,
74        })
75    }
76
77    /// Parses `perceptual`, `relative`/`relativeColorimetric`, `saturation`,
78    /// `absolute`/`absoluteColorimetric` (case-insensitive).
79    pub fn parse(s: &str) -> Option<Intent> {
80        let n: String = s
81            .to_ascii_lowercase()
82            .chars()
83            .filter(|c| c.is_ascii_alphanumeric())
84            .collect();
85        Some(match n.as_str() {
86            "perceptual" | "0" => Intent::Perceptual,
87            "relative" | "relativecolorimetric" | "colorimetric" | "1" => {
88                Intent::RelativeColorimetric
89            }
90            "saturation" | "2" => Intent::Saturation,
91            "absolute" | "absolutecolorimetric" | "3" => Intent::AbsoluteColorimetric,
92            _ => return None,
93        })
94    }
95
96    pub fn id(self) -> &'static str {
97        match self {
98            Intent::Perceptual => "perceptual",
99            Intent::RelativeColorimetric => "relative",
100            Intent::Saturation => "saturation",
101            Intent::AbsoluteColorimetric => "absolute",
102        }
103    }
104}
105
106/// Errors from parsing profiles or building transforms.
107#[derive(Clone, Debug, PartialEq, Eq)]
108pub enum CmsError {
109    Truncated,
110    /// Missing `acsp` signature: not an ICC profile.
111    BadSignature,
112    Invalid(String),
113    Unsupported(String),
114}
115
116impl std::fmt::Display for CmsError {
117    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
118        match self {
119            CmsError::Truncated => write!(f, "ICC profile is truncated"),
120            CmsError::BadSignature => write!(f, "not an ICC profile (missing 'acsp' signature)"),
121            CmsError::Invalid(m) => write!(f, "invalid ICC profile: {m}"),
122            CmsError::Unsupported(m) => write!(f, "unsupported ICC feature: {m}"),
123        }
124    }
125}
126
127impl std::error::Error for CmsError {}