Skip to main content

jxl_color/
cms.rs

1use crate::RenderingIntent;
2
3/// Color management system that handles ICCv4 profiles.
4pub trait ColorManagementSystem {
5    /// Prepares color transformation between two ICC profiles.
6    ///
7    /// # Errors
8    /// This function will return an error if the internal CMS implementation returned an error.
9    fn prepare_transform(
10        &self,
11        from_icc: &[u8],
12        to_icc: &[u8],
13        intent: RenderingIntent,
14    ) -> Result<Box<dyn PreparedTransform>, Box<dyn std::error::Error + Send + Sync + 'static>>;
15
16    /// Returns whether the CMS supports linear transfer function.
17    ///
18    /// This method will return `false` if it doesn't support (or it lacks precision to handle)
19    /// linear transfer function.
20    fn supports_linear_tf(&self) -> bool {
21        true
22    }
23}
24
25/// Prepared transformation created by [`ColorManagementSystem`].
26///
27/// Prepared transformations may be cached by jxl-oxide internally.
28pub trait PreparedTransform: Send + Sync {
29    /// The number of expected input channels.
30    fn num_input_channels(&self) -> usize;
31
32    /// The number of expected output channels.
33    fn num_output_channels(&self) -> usize;
34
35    /// Performs prepared color transformation.
36    ///
37    /// # Errors
38    /// This function will return an error if the internal CMS implementation returned an error.
39    fn transform(
40        &self,
41        channels: &mut [&mut [f32]],
42    ) -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>>;
43}
44
45/// "Null" color management system that fails on every operation.
46#[derive(Debug, Copy, Clone)]
47pub struct NullCms;
48impl ColorManagementSystem for NullCms {
49    fn prepare_transform(
50        &self,
51        _: &[u8],
52        _: &[u8],
53        _: RenderingIntent,
54    ) -> Result<Box<dyn PreparedTransform>, Box<dyn std::error::Error + Send + Sync + 'static>>
55    {
56        Err(Box::new(crate::Error::CmsNotAvailable))
57    }
58}