rcms 0.1.0

ICC color management library
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
//! Profile linking.

use crate::black_point::{detect_black_point, detect_dest_black_point};
use crate::color::{CxyY, Cxyz, D50};
use crate::pipeline::{Pipeline, PipelineError, PipelineStage};
use crate::profile::{ColorSpace, IccProfile, Intent, ProfileClass};
use crate::util::{lcms_mat3_eval, lcms_mat3_per};
use cgmath::prelude::*;
use cgmath::{Matrix3, Vector3};
use std::fmt;

/// Linking errors.
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub enum LinkError {
    /// A pipeline error.
    Pipeline(PipelineError),
    /// Incompatible color spaces at a given index.
    IncompatibleSpaces(usize, ColorSpace, ColorSpace),
    /// Failure computing absolute intent at a given index.
    AbsoluteIntentError(usize),
    /// Missing device link LUT at a given index.
    NoDeviceLinkLut(usize),
    /// Missing input LUT at a given index.
    NoInputLut(usize),
    /// Missing output LUT at a given index.
    NoOutputLut(usize),
}

impl From<PipelineError> for LinkError {
    fn from(this: PipelineError) -> LinkError {
        LinkError::Pipeline(this)
    }
}

impl fmt::Display for LinkError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            LinkError::Pipeline(err) => write!(f, "{}", err),
            LinkError::IncompatibleSpaces(i, a, b) => write!(
                f,
                "incompatible color spaces at index {}: converting from {} to {}",
                i, a, b
            ),
            LinkError::AbsoluteIntentError(i) => {
                write!(f, "absolute intent error for profile at index {}", i)
            }
            LinkError::NoDeviceLinkLut(i) => {
                write!(f, "missing device link LUT for profile at index {}", i)
            }
            LinkError::NoInputLut(i) => write!(f, "missing input LUT for profile at index {}", i),
            LinkError::NoOutputLut(i) => write!(f, "missing output LUT for profile at index {}", i),
        }
    }
}

impl ColorSpace {
    /// Returns true if this color space is compatible with the other for linking.
    ///
    /// Commutative.
    pub fn is_compatible_with(self, other: ColorSpace) -> bool {
        if self == other {
            // they're the same
            true
        } else if self == ColorSpace::S4Color && other == ColorSpace::CMYK
            || self == ColorSpace::CMYK && other == ColorSpace::S4Color
        {
            // mch4 substitution of cmyk
            true
        } else if self == ColorSpace::XYZ && other == ColorSpace::Lab
            || self == ColorSpace::Lab && other == ColorSpace::XYZ
        {
            // xyz/lab
            true
        } else {
            false
        }
    }
}

/// Links profiles.
///
/// # Parameters
/// - `profiles`: a list of ICC profiles. This will most commonly be just two profiles (an input and
///   an output profile) but more can be used in-between to link more finicky profiles.
/// - `intents`: rendering intents for each profile.
/// - `black_point_compensation`: set to true to use black point compensation for the conversion to
///   the profile at the given index. Will be overridden automatically in certain cases.
/// - `adaptation_states`: for absolute colorimetric intents: a float value from 0 to 1 indicating
///   how adapted the observer is.
///
/// # Panics
/// - if parameters are not all the same length
pub fn link(
    profiles: &[&IccProfile],
    intents: &[Intent],
    black_point_compensation: &[bool],
    adaptation_states: &[f64],
) -> Result<Pipeline, LinkError> {
    let mut pipeline = Pipeline::new();

    let mut current_cs = profiles[0].color_space;

    for i in 0..profiles.len() {
        let profile = &profiles[i];
        let intent = intents[i];

        let mut bpc = black_point_compensation[i];

        // Check if black point is really needed or allowed. Note that
        // following Adobe's document:
        // BPC does not apply to devicelink profiles, nor to abs colorimetric,
        // and applies always on V4 perceptual and saturation.
        if intent == Intent::AbsoluteColorimetric {
            bpc = false;
        }

        if intent == Intent::Perceptual || intent == Intent::Saturation {
            if profile.version() >= (4, 0) {
                bpc = true;
            }
        }

        let is_device_link = profile.device_class == ProfileClass::Link.into()
            || profile.device_class == ProfileClass::Abstract.into();

        let use_as_input = if i == 0 && !is_device_link {
            // First profile is used as input unless devicelink or abstract
            true
        } else {
            // Else use profile in the input direction if current space is not PCS
            current_cs != ColorSpace::XYZ && current_cs != ColorSpace::Lab
        };

        let (cs_in, cs_out) = if use_as_input || is_device_link {
            (profile.color_space, profile.pcs)
        } else {
            (profile.pcs, profile.color_space)
        };

        if !cs_in.is_compatible_with(current_cs) {
            return Err(LinkError::IncompatibleSpaces(i, cs_in, current_cs));
        }

        if is_device_link
            || (profile.device_class == ProfileClass::NamedColor && profiles.len() == 1)
        {
            let mut profile_lut = match profile.device_link_lut(intent) {
                Some(lut) => lut,
                None => return Err(LinkError::NoDeviceLinkLut(i)),
            };

            let (m, off) = if profile.device_class == ProfileClass::Abstract && i > 0 {
                compute_conversion(i as u32, profiles, intent, bpc, adaptation_states[i])?
            } else {
                (Matrix3::identity(), Vector3::zero())
            };

            add_conversion(&mut pipeline, i, current_cs, cs_in, m, off)?;
            pipeline.append(&mut profile_lut)?;
        } else if use_as_input {
            let mut profile_lut = match profile.input_lut(intent) {
                Some(lut) => lut,
                None => return Err(LinkError::NoInputLut(i)),
            };
            pipeline.append(&mut profile_lut)?;
        } else {
            let mut profile_lut = match profile.output_lut(intent) {
                Some(lut) => lut,
                None => return Err(LinkError::NoOutputLut(i)),
            };

            let (m, off) =
                compute_conversion(i as u32, profiles, intent, bpc, adaptation_states[i])?;
            add_conversion(&mut pipeline, i, current_cs, cs_in, m, off)?;
            pipeline.append(&mut profile_lut)?;
        }

        current_cs = cs_out;
    }

    Ok(pipeline)
}

/// Computes the conversion layer
fn compute_conversion(
    i: u32,
    profiles: &[&IccProfile],
    intent: Intent,
    bpc: bool,
    adaptation_state: f64,
) -> Result<(Matrix3<f64>, Vector3<f64>), LinkError> {
    let i = i as usize;
    if intent == Intent::AbsoluteColorimetric {
        let prev_profile = &profiles[i - 1];
        let profile = &profiles[i];

        let wp_in = prev_profile.media_white_point();
        let cam_in = prev_profile.adaptation_matrix();
        let wp_out = profile.media_white_point();
        let cam_out = profile.adaptation_matrix();

        Ok((
            compute_absolute_intent(adaptation_state, wp_in, cam_in, wp_out, cam_out, i)?,
            Vector3::zero(),
        ))
    } else if bpc {
        let bp_in = detect_black_point(&profiles[i - 1], intent, 0);
        let bp_out = detect_dest_black_point(&profiles[i], intent, 0);

        // If black points are equal, then do nothing
        if bp_in != bp_out {
            let (m, off) = compute_black_point_compensation(bp_in, bp_out);
            // Offset should be adjusted because the encoding. We encode XYZ normalized to
            // 0..1.0.
            // To do that, we divide by MAX_ENCODEABLE_XYZ. The conversion stage goes XYZ -> XYZ
            // so we have first to convert from encoded to XYZ and then convert back to encoded.
            // y = Mx + Off
            // x = x'c
            // y = M x'c + Off
            // y = y'c; y' = y / c
            // y' = (Mx'c + Off) /c = Mx' + (Off / c)
            Ok((m, off / Cxyz::MAX_ENCODABLE))
        } else {
            Ok((Matrix3::identity(), Vector3::zero()))
        }
    } else {
        Ok((Matrix3::identity(), Vector3::zero()))
    }
}

/// Returns if m should be applied
fn is_empty_layer(m: Matrix3<f64>, off: Vector3<f64>) -> bool {
    let ident = Matrix3::<f64>::identity();
    let mut diff = 0.;

    for i in 0..9 {
        diff += (m[i / 3][i % 3] - ident[i / 3][i % 3]).abs();
    }

    for i in 0..3 {
        diff += off[i].abs();
    }

    diff < 0.002
}

fn add_conversion(
    result: &mut Pipeline,
    i: usize,
    in_pcs: ColorSpace,
    out_pcs: ColorSpace,
    mat: Matrix3<f64>,
    off: Vector3<f64>,
) -> Result<(), LinkError> {
    // Handle PCS mismatches. A specialized stage is added to the LUT in such case
    match (in_pcs, out_pcs) {
        (ColorSpace::XYZ, ColorSpace::XYZ) => {
            if !is_empty_layer(mat, off) {
                result.append_stage(PipelineStage::new_matrix3(mat, Some(off)))?;
            }
        }
        (ColorSpace::XYZ, ColorSpace::Lab) => {
            if !is_empty_layer(mat, off) {
                result.append_stage(PipelineStage::new_matrix3(mat, Some(off)))?;
            }
            result.append_stage(PipelineStage::new_xyz_to_lab())?;
        }
        (ColorSpace::Lab, ColorSpace::XYZ) => {
            result.append_stage(PipelineStage::new_lab_to_xyz())?;
            if !is_empty_layer(mat, off) {
                result.append_stage(PipelineStage::new_matrix3(mat, Some(off)))?;
            }
        }
        (ColorSpace::Lab, ColorSpace::Lab) => {
            if !is_empty_layer(mat, off) {
                result.append_stage(PipelineStage::new_lab_to_xyz())?;
                result.append_stage(PipelineStage::new_matrix3(mat, Some(off)))?;
                result.append_stage(PipelineStage::new_xyz_to_lab())?;
            }
        }
        _ => {
            // On colorspaces other than PCS, check for same space
            if in_pcs != out_pcs {
                return Err(LinkError::IncompatibleSpaces(i, in_pcs, out_pcs));
            }
        }
    }

    Ok(())
}

/// Black point compensation. Implemented as a linear scaling in XYZ. Black points
/// should come relative to the white point. Fills a matrix/offset element m
/// which is organized as a 4x4 matrix.
fn compute_black_point_compensation(
    black_point_in: Cxyz,
    black_point_out: Cxyz,
) -> (Matrix3<f64>, Vector3<f64>) {
    // Now we need to compute a matrix plus an offset m such that
    // [m]*bpin + off = bpout
    // [m]*D50  + off = D50
    //
    // This is a linear scaling in the form ax+b, where
    // a = (bpout - D50) / (bpin - D50)
    // b = - D50* (bpout - bpin) / (bpin - D50)
    let tx = black_point_in.x - D50.x;
    let ty = black_point_in.y - D50.y;
    let tz = black_point_in.z - D50.z;

    let ax = (black_point_out.x - D50.x) / tx;
    let ay = (black_point_out.y - D50.y) / ty;
    let az = (black_point_out.z - D50.z) / tz;

    let bx = -D50.x * (black_point_out.x - black_point_in.x) / tx;
    let by = -D50.y * (black_point_out.y - black_point_in.y) / ty;
    let bz = -D50.z * (black_point_out.z - black_point_in.z) / tz;

    (
        Matrix3::from_diagonal((ax, ay, az).into()),
        Vector3::new(bx, by, bz),
    )
}

/// Join scalings to obtain relative input to absolute and then to relative output.
/// Result is stored in a 3x3 matrix
fn compute_absolute_intent(
    adaptation_state: f64,
    wp_in: Cxyz,
    cam_in: Matrix3<f64>,
    wp_out: Cxyz,
    cam_out: Matrix3<f64>,
    i: usize,
) -> Result<Matrix3<f64>, LinkError> {
    if adaptation_state == 1. {
        // Observer is fully adapted. Keep chromatic adaptation.
        // That is the standard V4 behaviour
        Ok(Matrix3::from_diagonal(
            (wp_in.x / wp_out.x, wp_in.y / wp_out.y, wp_in.z / wp_out.z).into(),
        ))
    } else {
        // Incomplete adaptation. This is an advanced feature.
        let scale = Matrix3::from_diagonal(
            (wp_in.x / wp_out.x, wp_in.y / wp_out.y, wp_in.z / wp_out.z).into(),
        );

        if adaptation_state == 0. {
            let m2 = lcms_mat3_per(cam_out, scale);
            // m2 holds CHAD from output white to D50 times abs. col. scaling

            // Observer is not adapted, undo the chromatic adaptation
            // let m = mat3_per(m2, cam_out);

            let cam_in_inv = match cam_in.invert() {
                Some(m) => m,
                None => return Err(LinkError::AbsoluteIntentError(i)),
            };

            Ok(lcms_mat3_per(m2, cam_in_inv))
        } else {
            let m2 = match cam_in.invert() {
                Some(m) => m,
                None => return Err(LinkError::AbsoluteIntentError(i)),
            };

            let m3 = lcms_mat3_per(m2, scale);

            // m3 holds CHAD from input white to D50 times abs. col. scaling
            let temp_src = match chad_to_temp(cam_in) {
                Some(v) => v,
                None => return Err(LinkError::AbsoluteIntentError(i)),
            };
            let temp_dest = match chad_to_temp(cam_out) {
                Some(v) => v,
                None => return Err(LinkError::AbsoluteIntentError(i)),
            };

            if temp_src < 0. || temp_dest < 0. {
                // something went wrong
                return Err(LinkError::AbsoluteIntentError(i));
            }

            if scale.is_identity() && (temp_src - temp_dest).abs() < 0.01 {
                return Ok(Matrix3::identity());
            }

            let temp = (1. - adaptation_state) * temp_dest + adaptation_state * temp_src;

            let mixed_chad = match temp_to_chad(temp) {
                Some(m) => m,
                None => return Err(LinkError::AbsoluteIntentError(i)),
            };

            Ok(lcms_mat3_per(m3, mixed_chad))
        }
    }
}

/// Approximate a blackbody illuminant based on CHAD information
fn chad_to_temp(chad: Matrix3<f64>) -> Option<f64> {
    let inverse = match chad.invert() {
        Some(inverse) => inverse,
        None => return None,
    };

    let d50_xyz = D50;
    let s = Vector3::new(d50_xyz.x, d50_xyz.y, d50_xyz.z);
    let d = lcms_mat3_eval(inverse, s);

    let dest: CxyY = Cxyz {
        x: d.x,
        y: d.y,
        z: d.z,
    }
    .into();

    dest.to_temp()
}

/// Compute a CHAD based on a given temperature
fn temp_to_chad(temp: f64) -> Option<Matrix3<f64>> {
    let chromaticity_of_white: Cxyz = match CxyY::from_temp(temp) {
        Some(c) => c.into(),
        None => return None,
    };
    chromaticity_of_white.adaptation_matrix(D50, None)
}