cmx 0.1.0

Rust Spectral 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
428
429
430
431
432
// SPDX-License-Identifier: Apache-2.0 OR MIT
// Copyright (c) 2021-2025, Harbers Bik LLC
#![allow(unused)]

use crate::{
    profile::{ProfileTagRecord, RawProfile},
    tag::{
        self,
        tagdata::{CurveData, ParametricCurveData, TagData},
        Tag, TagSignature,
    },
};

/// Provides uniform access to the underlying [`RawProfile`] from any profile
/// wrapper type or from the [`Profile`](super::Profile) enum.
///
/// This trait is implemented for [`RawProfile`] itself, for every device-class
/// wrapper ([`DisplayProfile`](super::DisplayProfile), etc.), and for the
/// [`Profile`](super::Profile) enum, allowing the builder API to work generically
/// over all of them.
pub trait HasRawProfile {
    fn raw(&self) -> &RawProfile;
    fn raw_mut(&mut self) -> &mut RawProfile;
}

// Implement for RawProfile itself.
impl HasRawProfile for RawProfile {
    fn raw(&self) -> &RawProfile {
        self
    }
    fn raw_mut(&mut self) -> &mut RawProfile {
        self
    }
}

// Implement for all wrapper profiles.
impl HasRawProfile for super::InputProfile {
    fn raw(&self) -> &RawProfile {
        &self.0
    }
    fn raw_mut(&mut self) -> &mut RawProfile {
        &mut self.0
    }
}
impl HasRawProfile for super::DisplayProfile {
    fn raw(&self) -> &RawProfile {
        &self.0
    }
    fn raw_mut(&mut self) -> &mut RawProfile {
        &mut self.0
    }
}
impl HasRawProfile for super::OutputProfile {
    fn raw(&self) -> &RawProfile {
        &self.0
    }
    fn raw_mut(&mut self) -> &mut RawProfile {
        &mut self.0
    }
}
impl HasRawProfile for super::DeviceLinkProfile {
    fn raw(&self) -> &RawProfile {
        &self.0
    }
    fn raw_mut(&mut self) -> &mut RawProfile {
        &mut self.0
    }
}
impl HasRawProfile for super::AbstractProfile {
    fn raw(&self) -> &RawProfile {
        &self.0
    }
    fn raw_mut(&mut self) -> &mut RawProfile {
        &mut self.0
    }
}
impl HasRawProfile for super::ColorSpaceProfile {
    fn raw(&self) -> &RawProfile {
        &self.0
    }
    fn raw_mut(&mut self) -> &mut RawProfile {
        &mut self.0
    }
}
impl HasRawProfile for super::NamedColorProfile {
    fn raw(&self) -> &RawProfile {
        &self.0
    }
    fn raw_mut(&mut self) -> &mut RawProfile {
        &mut self.0
    }
}
impl HasRawProfile for super::SpectralProfile {
    fn raw(&self) -> &RawProfile {
        &self.0
    }
    fn raw_mut(&mut self) -> &mut RawProfile {
        &mut self.0
    }
}

// Implement for the enum Profile.
impl HasRawProfile for super::Profile {
    fn raw(&self) -> &RawProfile {
        self.as_raw_profile()
    }
    fn raw_mut(&mut self) -> &mut RawProfile {
        super::Profile::as_raw_profile_mut(self)
    }
}

/// Marker trait for tag signatures whose data is stored as a
/// `MultiLocalizedUnicodeData` (ICC type `mluc`).
///
/// Implemented for: `CopyrightTag`, `ProfileDescriptionTag`,
/// `DeviceMfgDescTag`, `DeviceModelDescTag`, `ViewingCondDescTag`.
pub trait IsMultiLocalizedUnicodeTag {}
impl IsMultiLocalizedUnicodeTag for crate::tag::tags::CopyrightTag {}
impl IsMultiLocalizedUnicodeTag for crate::tag::tags::ProfileDescriptionTag {}
impl IsMultiLocalizedUnicodeTag for crate::tag::tags::DeviceMfgDescTag {}
impl IsMultiLocalizedUnicodeTag for crate::tag::tags::DeviceModelDescTag {}
impl IsMultiLocalizedUnicodeTag for crate::tag::tags::ViewingCondDescTag {}
#[cfg(feature = "v5")]
impl IsMultiLocalizedUnicodeTag for crate::tag::tags::ScreeningDescTag {}

/// Marker trait for tone-reproduction-curve tags whose data is stored as a
/// `CurveData` (ICC type `curv`): `RedTRCTag`, `GreenTRCTag`,
/// `BlueTRCTag`, `GrayTRCTag`.
pub trait IsCurveTag {}
impl IsCurveTag for crate::tag::tags::BlueTRCTag {}
impl IsCurveTag for crate::tag::tags::GreenTRCTag {}
impl IsCurveTag for crate::tag::tags::RedTRCTag {}
impl IsCurveTag for crate::tag::tags::GrayTRCTag {}

/// Marker trait for tone-reproduction-curve tags whose data is stored as a
/// `ParametricCurveData` (ICC type `para`): `RedTRCTag`, `GreenTRCTag`,
/// `BlueTRCTag`, `GrayTRCTag`.
pub trait IsParametricCurveTag {}
impl IsParametricCurveTag for crate::tag::tags::BlueTRCTag {}
impl IsParametricCurveTag for crate::tag::tags::GreenTRCTag {}
impl IsParametricCurveTag for crate::tag::tags::RedTRCTag {}
impl IsParametricCurveTag for crate::tag::tags::GrayTRCTag {}

/// Marker trait for tags whose data is stored as a `SignatureData`
/// (ICC type `sig `): `TechnologyTag`,
/// `ColorimetricIntentImageStateTag`, `PerceptualRenderingIntentGamutTag`.
pub trait IsSignatureTag {}
impl IsSignatureTag for crate::tag::tags::ColorimetricIntentImageStateTag {}
impl IsSignatureTag for crate::tag::tags::TechnologyTag {}
#[cfg(feature = "v5")]
impl IsSignatureTag for crate::tag::tags::SaturationRenderingIntentGamutTag {}
impl IsSignatureTag for crate::tag::tags::PerceptualRenderingIntentGamutTag {}

/// Marker trait for tags whose data is stored as an `XYZArrayData`
/// (ICC type `XYZ `): matrix column tags, luminance, white-point, black-point.
pub trait IsXYZArrayTag {}
impl IsXYZArrayTag for crate::tag::tags::RedMatrixColumnTag {}
impl IsXYZArrayTag for crate::tag::tags::GreenMatrixColumnTag {}
impl IsXYZArrayTag for crate::tag::tags::BlueMatrixColumnTag {}
impl IsXYZArrayTag for crate::tag::tags::LuminanceTag {}
impl IsXYZArrayTag for crate::tag::tags::MediaWhitePointTag {}
impl IsXYZArrayTag for crate::tag::tags::MediaBlackPointTag {}

/// Marker trait for tags whose data is stored as a `TextDescriptionData`
/// (legacy ICC v2 type `desc`).
pub trait IsTextDescriptionTag {}
impl IsTextDescriptionTag for crate::tag::tags::ProfileDescriptionTag {}
impl IsTextDescriptionTag for crate::tag::tags::CopyrightTag {}
impl IsTextDescriptionTag for crate::tag::tags::DeviceMfgDescTag {}
impl IsTextDescriptionTag for crate::tag::tags::DeviceModelDescTag {}
#[cfg(feature = "v5")]
impl IsTextDescriptionTag for crate::tag::tags::ScreeningDescTag {}
impl IsTextDescriptionTag for crate::tag::tags::ViewingCondDescTag {}

/// Marker trait for tags whose data is stored as plain ASCII `TextData`
/// (ICC type `text`): `CopyrightTag`, `CharTargetTag`.
pub trait IsTextTag {}
impl IsTextTag for crate::tag::tags::CopyrightTag {}
impl IsTextTag for crate::tag::tags::CharTargetTag {}

/// Marker trait for tags whose data is stored as an `S15Fixed16ArrayData`
/// (ICC type `sf32`): `ChromaticAdaptationTag`.
pub trait IsS15Fixed16ArrayTag {}
impl IsS15Fixed16ArrayTag for crate::tag::tags::ChromaticAdaptationTag {}

/// Marker trait for tags whose data is stored as `Lut8Data` (ICC type `mft1`).
pub trait IsLut8DataTag {}
/// Marker trait for tags whose data is stored as `Lut16Data` (ICC type `mft2`).
pub trait IsLut16DataTag {}
/// Marker trait for tags whose data uses the `LutAtoB` format (ICC type `mAB `).
pub trait IsLutAtoBDataTag {}
/// Marker trait for tags whose data uses the `LutBtoA` format (ICC type `mBA `).
pub trait IsLutBtoADataTag {}

/// Marker trait that allows any tag to be set using raw bytes via
/// [`TagSetter::as_raw`].  Implemented for all tag signatures.
pub trait IsRawTag {}

/// An intermediate builder returned by [`with_tag`](super::Profile::with_tag) that
/// provides type-safe methods for setting the data of a specific ICC tag.
///
/// `TagSetter<P, S>` is generic over:
/// - `P` — the profile type being built (e.g. [`DisplayProfile`](super::DisplayProfile),
///   [`Profile`](super::Profile), or [`RawProfile`]).
/// - `S` — the tag signature type (one of the zero-sized structs in
///   [`cmx::tag::tags`](crate::tag::tags)).
///
/// Each `as_*` method is gated by a marker trait (e.g. `IsCurveTag`) that is
/// only implemented for the tag signatures where that data format is valid.
/// Attempting to call `as_curve()` on a tag signature that does not implement
/// `IsCurveTag` is a **compile-time error**, not a runtime one.
///
/// Calling an `as_*` method consumes the `TagSetter` and returns the profile `P`,
/// allowing builder chains:
///
/// ```rust
/// use cmx::profile::DisplayProfile;
/// use cmx::tag::tags::{RedTRCTag, GreenTRCTag};
///
/// let profile = DisplayProfile::new()
///     .with_tag(RedTRCTag)
///     .as_curve(|c| c.set_gamma(2.2))
///     .with_tag(GreenTRCTag)
///     .as_curve(|c| c.set_gamma(2.2));
/// ```
pub struct TagSetter<P: HasRawProfile, S> {
    profile: P,
    tag: S,
}

impl<P, S> TagSetter<P, S>
where
    P: HasRawProfile,
    S: Into<TagSignature> + Copy,
{
    pub fn new(profile: P, tag: S) -> Self {
        Self { profile, tag }
    }

    /// Sets the tag's data as a `CurveData`.
    /// This method is only available if the signature implements `IsCurveTag`.
    pub fn as_curve<F>(mut self, configure: F) -> P
    where
        S: IsCurveTag,
        F: FnOnce(&mut CurveData),
    {
        let curve = self.profile.raw_mut().ensure_curve_mut(self.tag.into());
        configure(curve);
        self.profile
    }

    /// Set the tag's data as a `ParametricCurveData` (ICC type `para`).
    ///
    /// Available for TRC tags: `RedTRCTag`, `GreenTRCTag`, `BlueTRCTag`,
    /// `GrayTRCTag`.  Use [`ParametricCurveData::set_parameters`] for a
    /// fixed-size array or [`ParametricCurveData::set_parameters_slice`] for a
    /// dynamically-sized slice (the slice variant validates the parameter count
    /// and returns a `Result`).
    pub fn as_parametric_curve<F>(mut self, configure: F) -> P
    where
        S: IsParametricCurveTag,
        F: FnOnce(&mut ParametricCurveData),
    {
        let para_curve = self
            .profile
            .raw_mut()
            .ensure_parametric_curve_mut(self.tag.into());
        configure(para_curve);
        self.profile
    }

    /// Set the tag's data as a `SignatureData` (ICC type `sig `).
    ///
    /// Available for: `TechnologyTag`, `ColorimetricIntentImageStateTag`,
    /// `PerceptualRenderingIntentGamutTag`.
    pub fn as_signature<F>(mut self, configure: F) -> P
    where
        S: IsSignatureTag,
        F: FnOnce(&mut crate::tag::tagdata::SignatureData),
    {
        let signature = self.profile.raw_mut().ensure_signature_mut(self.tag.into());
        configure(signature);
        self.profile
    }

    /// Set the tag's data as an `XYZArrayData` (ICC type `XYZ `).
    ///
    /// Available for: `RedMatrixColumnTag`, `GreenMatrixColumnTag`,
    /// `BlueMatrixColumnTag`, `LuminanceTag`, `MediaWhitePointTag`,
    /// `MediaBlackPointTag`.
    pub fn as_xyz_array<F>(mut self, configure: F) -> P
    where
        S: IsXYZArrayTag,
        F: FnOnce(&mut crate::tag::tagdata::XYZArrayData),
    {
        let xyz = self.profile.raw_mut().ensure_xyz_array_mut(self.tag.into());
        configure(xyz);
        self.profile
    }

    /// Set the tag's data as a `TextDescriptionData` (legacy ICC v2 type `desc`).
    ///
    /// Available for: `ProfileDescriptionTag`, `CopyrightTag`,
    /// `DeviceMfgDescTag`, `DeviceModelDescTag`, `ViewingCondDescTag`.
    ///
    /// For new ICC v4 profiles prefer [`as_multi_localized_unicode`](Self::as_multi_localized_unicode),
    /// which supports multiple languages.  `TextDescriptionData` is provided for
    /// compatibility with tools that require v2-style description tags.
    pub fn as_text_description<F>(mut self, configure: F) -> P
    where
        S: IsTextDescriptionTag,
        F: FnOnce(&mut crate::tag::tagdata::TextDescriptionData),
    {
        let text_description = self
            .profile
            .raw_mut()
            .ensure_text_description_mut(self.tag.into());
        configure(text_description);
        self.profile
    }

    /// Set the tag's data as plain ASCII `TextData` (ICC type `text`).
    ///
    /// Available for: `CopyrightTag`, `CharTargetTag`.
    pub fn as_text<F>(mut self, configure: F) -> P
    where
        S: IsTextTag,
        F: FnOnce(&mut crate::tag::tagdata::TextData),
    {
        let text = self.profile.raw_mut().ensure_text_mut(self.tag.into());
        configure(text);
        self.profile
    }

    /// Set the tag's data as an `S15Fixed16ArrayData` (ICC type `sf32`).
    ///
    /// Available for: `ChromaticAdaptationTag` (a 3×3 matrix stored as nine
    /// s15Fixed16 values in row-major order).
    pub fn as_sf15_fixed_16_array<F>(mut self, configure: F) -> P
    where
        S: IsS15Fixed16ArrayTag,
        F: FnOnce(&mut crate::tag::tagdata::S15Fixed16ArrayData),
    {
        let array = self
            .profile
            .raw_mut()
            .ensure_s15_fixed_16_array_mut(self.tag.into());
        configure(array);
        self.profile
    }

    /// Sets the tag's data as raw bytes.
    /// This is used for non-ICC or manufacturer private tags, with unknown data formats, It is the
    /// caller's responsibility to ensure the data is valid for the intended use.
    /// It can also be used to set the raw data for known tags, but this is not recommended,
    /// as it bypasses the type safety provided by the other methods.
    pub fn as_raw<F>(mut self, configure: F) -> P
    where
        F: FnOnce(&mut crate::tag::tagdata::RawData),
    {
        let sig: TagSignature = self.tag.into();
        let raw = self.profile.raw_mut().ensure_raw_mut(self.tag.into());

        // If the tag has no data yet (is a new tag), initialize it with the tag signature and
        // reserved bytes.  This method is intended for use with unknown tags.  If a new tag is
        // being created using this method, it will use the tag signature also as the type
        // signature.  If the tag already exists, it may already have data, in which case we do not
        // overwrite it.
        if raw.0.is_empty() {
            // Initialize with the tag signature and reserved bytes.
            let mut initial_data = Vec::with_capacity(8);
            initial_data.extend_from_slice(&sig.to_u32().to_be_bytes());
            initial_data.extend_from_slice(&[0u8; 4]); // Reserved bytes
            raw.0 = initial_data;
        }
        configure(raw);
        self.profile
    }

    /// Set the tag's data as a `MultiLocalizedUnicodeData` (ICC type `mluc`).
    ///
    /// Available for: `CopyrightTag`, `ProfileDescriptionTag`,
    /// `DeviceMfgDescTag`, `DeviceModelDescTag`, `ViewingCondDescTag`.
    ///
    /// Use [`MultiLocalizedUnicodeData::insert`](crate::tag::tagdata::MultiLocalizedUnicodeData::insert)
    /// inside the closure to add one or more locale/string pairs.
    pub fn as_multi_localized_unicode<F>(mut self, configure: F) -> P
    where
        S: IsMultiLocalizedUnicodeTag,
        F: FnOnce(&mut crate::tag::tagdata::MultiLocalizedUnicodeData),
    {
        let mlu = self
            .profile
            .raw_mut()
            .ensure_multi_localized_unicode_mut(self.tag.into());
        configure(mlu);
        self.profile
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::profile::InputProfile;
    use crate::tag::tags::*;
    #[test]
    fn test_tag_setter() -> Result<(), Box<dyn std::error::Error>> {
        let mut profile = InputProfile::new();

        profile
            .with_tag(RedTRCTag)
            .as_curve(|curve| {
                curve.set_gamma(2.2);
            })
            .with_tag(GreenTRCTag)
            .as_curve(|curve| {
                curve.set_gamma(2.2);
            })
            .with_tag(BlueTRCTag)
            .as_curve(|curve| {
                curve.set_gamma(2.2);
            })
            .with_tag(GrayTRCTag)
            .as_parametric_curve(|para_curve| para_curve.set_parameters([0.5]))
            .with_tag(TechnologyTag)
            .as_signature(|signature| {
                signature.set_signature("fscn");
            });

        Ok(())
    }
}