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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
// SPDX-License-Identifier: Apache-2.0 OR MIT
// Copyright (c) 2021-2025, Harbers Bik LLC
//#![allow(dead_code, unused_imports)]
/*!
`cmx` is a Rust library for reading, writing, and constructing [ICC color profiles][icc-spec]
(versions 2.0–5.0). ICC profiles describe how color values produced by a device (camera,
display, printer) relate to a standard reference color space, making them essential for
accurate color reproduction across devices and applications.
[icc-spec]: https://www.color.org/specification/ICC.1-2022-05.pdf
# Quick Start
## Read a profile from disk
```no_run
use cmx::profile::Profile;
let profile = Profile::read("profile.icc")?;
println!("Color space : {:?}", profile.data_color_space());
println!("PCS : {:?}", profile.pcs());
println!("Version : {:?}", profile.version()?);
# Ok::<(), Box<dyn std::error::Error>>(())
```
## Dump a profile as TOML
The [`fmt::Display`](std::fmt::Display) implementation on every profile type serialises it
to TOML — the same output produced by the `cmx` CLI tool:
```no_run
use cmx::profile::Profile;
let profile = Profile::read("profile.icc")?;
println!("{profile}");
# Ok::<(), Box<dyn std::error::Error>>(())
```
The output looks like:
```toml
profile_size = 548
cmm = "Apple"
version = "4.0"
device_class = "Display"
color_space = "RGB"
pcs = "XYZ"
creation_datetime = "2015-10-14 13:08:56 UTC"
primary_platform = "Apple"
manufacturer = "APPL"
rendering_intent = "Perceptual"
pcs_illuminant = [0.9642, 1.0, 0.8249]
creator = "appl"
profile_id = "53410ea9facdd9fb57cc74868defc33f"
[desc]
ascii = "SMPTE RP 431-2-2007 DCI (P3)"
[wtpt]
xyz = [0.894592, 1.0, 0.954422]
[rTRC]
g = 2.60001
[chad]
matrix = [
[1.073822, 0.038803, -0.036896],
[0.055573, 0.963989, -0.014343],
[-0.004272, 0.005295, 0.862778]
]
```
## Build a profile from scratch
The consuming builder API sets tags one by one and computes the profile ID at the end:
```rust
use chrono::{DateTime, TimeZone};
use cmx::tag::tags::*;
use cmx::profile::DisplayProfile;
let display_p3_example = DisplayProfile::new()
// set creation date — current date/time is used if omitted
.with_creation_date(chrono::Utc.with_ymd_and_hms(2025, 8, 28, 0, 0, 0).unwrap())
.with_tag(ProfileDescriptionTag)
.as_text_description(|text| {
text.set_ascii("Display P3");
})
.with_tag(CopyrightTag)
.as_text(|text| {
text.set_text("CC0");
})
.with_tag(MediaWhitePointTag)
.as_xyz_array(|xyz| {
xyz.set([0.950455, 1.00000, 1.08905]);
})
.with_tag(RedMatrixColumnTag)
.as_xyz_array(|xyz| {
xyz.set([0.515121, 0.241196, -0.001053]);
})
.with_tag(GreenMatrixColumnTag)
.as_xyz_array(|xyz| {
xyz.set([0.291977, 0.692245, 0.041885]);
})
.with_tag(BlueMatrixColumnTag)
.as_xyz_array(|xyz| {
xyz.set([0.157104, 0.066574, 0.784073]);
})
.with_tag(RedTRCTag)
.as_parametric_curve(|para| {
para.set_parameters([2.39999, 0.94786, 0.05214, 0.07739, 0.04045]);
})
.with_tag(BlueTRCTag)
.as_parametric_curve(|para| {
para.set_parameters([2.39999, 0.94786, 0.05214, 0.07739, 0.04045]);
})
.with_tag(GreenTRCTag)
.as_parametric_curve(|para| {
para.set_parameters([2.39999, 0.94786, 0.05214, 0.07739, 0.04045]);
})
.with_tag(ChromaticAdaptationTag)
.as_sf15_fixed_16_array(|array| {
array.set([
1.047882, 0.022919, -0.050201,
0.029587, 0.990479, -0.017059,
-0.009232, 0.015076, 0.751678
]);
})
.with_profile_id(); // compute and embed the MD5 profile ID
// Serialise to bytes without touching the filesystem
let bytes = display_p3_example.to_bytes().unwrap();
assert_eq!(bytes.len(), 524);
```
## Modify an existing profile
Read a profile, change a tag, and write it back:
```no_run
use cmx::profile::Profile;
use cmx::tag::tags::CopyrightTag;
Profile::read("input.icc")?
.with_tag(CopyrightTag)
.as_text(|t| t.set_text("Copyright 2025 Acme Corp."))
.write("output.icc")?;
# Ok::<(), Box<dyn std::error::Error>>(())
```
# Modules
| Module | Contents |
|---|---|
| [`profile`] | `Profile` enum and per-device-class types (`DisplayProfile`, `InputProfile`, …) |
| [`tag`] | Tag signatures, tag data types, and the `TagSetter` builder |
| [`header`] | ICC 128-byte header fields and accessors |
| [`signatures`] | ICC 4-byte signature enums (`ColorSpace`, `DeviceClass`, `RenderingIntent`, …) |
| [`error`] | [`Error`] type returned by public API functions |
# ICC Profile Concepts
An ICC profile is a binary file with three sections:
1. **128-byte header** — fixed fields: device class, color space, PCS, version, creation date, etc.
2. **Tag table** — a list of `(signature, offset, size)` entries pointing into the data block.
3. **Tag data** — the actual payload for each tag (matrices, curves, look-up tables, text, …).
## Device Classes
The ICC specification defines eight device classes. This crate provides a dedicated type for each:
| Type | ICC class code | Typical use |
|---|---|---|
| [`profile::DisplayProfile`] | `mntr` | Monitors, projectors |
| [`profile::InputProfile`] | `scnr` | Cameras, scanners |
| [`profile::OutputProfile`] | `prtr` | Printers |
| [`profile::DeviceLinkProfile`] | `link` | Direct device-to-device transforms |
| [`profile::AbstractProfile`] | `abst` | Abstract color transforms |
| [`profile::ColorSpaceProfile`] | `spac` | Color space definitions |
| [`profile::NamedColorProfile`] | `nmcl` | Named color palettes |
| [`profile::SpectralProfile`] | `cenc` | Spectral data (ICC v5) |
All types wrap a [`profile::RawProfile`] which holds the raw binary data and
preserves unknown tags verbatim, guaranteeing lossless round-trips.
## Profile Connection Space (PCS)
Profiles connect device-specific color values to a common reference color space called the
**Profile Connection Space (PCS)**. Two PCS values are defined by the ICC specification:
- `XYZ` — CIE 1931 XYZ, used by most display and output profiles.
- `Lab` — CIELAB (L\*a\*b\*), used by some output and abstract profiles.
## Rendering Intents
The rendering intent controls how out-of-gamut colors are handled during color conversion.
Four intents are defined:
| Intent | Typical use |
|---|---|
| Perceptual | Photographic images — compresses the gamut smoothly |
| Relative Colorimetric | Graphics — clips and maps the source white point |
| Saturation | Business graphics — maximises saturation |
| Absolute Colorimetric | Proofing — preserves absolute colorimetric values |
## Tags
Tags are identified by a 4-byte signature (e.g. `rXYZ`, `rTRC`, `desc`). Each tag carries a
payload of a specific ICC type — an XYZ triplet, a tone-reproduction curve, a text string, etc.
All well-known tag signatures are re-exported from [`tag::tags`].
Tag types supported for reading and writing include:
| ICC type | Rust type | Common tags |
|---|---|---|
| `XYZ` | `XYZArrayData` | `rXYZ`, `gXYZ`, `bXYZ`, `wtpt` |
| `para` | `ParametricCurveData` | `rTRC`, `gTRC`, `bTRC` |
| `curv` | `CurveData` | `rTRC`, `gTRC`, `bTRC` |
| `mluc` | `MultiLocalizedUnicodeData` | `desc` |
| `desc` | `TextDescriptionData` | `desc` |
| `sf32` | `S15Fixed16ArrayData` | `chad` |
| `sig ` | `SignatureData` | `tech` |
| `text` | `TextData` | `cprt` |
| `mft1` | `Lut8Data` | `A2B0`, `B2A0` |
| `mft2` | `Lut16Data` | `A2B0`, `B2A0` |
Tags not yet parsed are stored as `RawData` and written back verbatim — no data is lost.
# Key Types
| Type | Description |
|---|---|
| [`S15Fixed16`] | ICC s15Fixed16 fixed-point number used in matrices and XYZ values |
| [`profile::Profile`] | Parsed profile, dispatched to one of eight device-class variants |
| [`profile::TagSetter`] | Consuming builder returned by `with_tag(…)` |
| [`tag::TagSignature`] | 4-byte tag identifier; 70+ known signatures plus `Unknown(u32)` |
| [`error::Error`] | Top-level error type |
# Lossless Round-trips
Any tag not recognised by this crate is preserved as raw bytes and written back verbatim.
Reading a profile and re-serialising it produces byte-identical output.
# CLI Tool
The `cmx` binary prints any ICC profile as TOML:
```bash
cargo install cmx
cmx profile.icc # print TOML to stdout
cmx profile.icc -o out.toml # write TOML to a file
```
# Installation
Add the library to your project:
```bash
cargo add cmx
```
Full API documentation is on [docs.rs/cmx](https://docs.rs/cmx).
# Roadmap
- [x] Parse full ICC profiles (versions 2.x–5.0)
- [x] Lossless round-trips — unknown tags preserved verbatim
- [x] Conversion to human-readable TOML format
- [x] Builder-style API for constructing ICC profiles
- [x] Support for the primary ICC tag types
- [ ] Read TOML color profiles and convert back to binary ICC
- [ ] Support all ICC tag types
- [ ] Spectral data and ICC v5 color management
*/
use Display;
pub use Error;
use Zero;
/// Rounds a floating-point value to the specified precision (decimal places).
/// Example: round_to_precision(1.23456, 2) -> 1.23
pub
/// Generic zero-check used by serde skip_serializing_if for many numeric fields.
pub
/// Treats the string as "empty" if it is empty or equals "none" (case-sensitive),
/// used to suppress serialization for some optional fields.
pub
/// Convert ICC s15Fixed16Number to f64 by dividing by 65536.0.
/// This is a signed 32-bit fixed-point with 16 fractional bits.
pub
/// Convert ICC u1.15 fixed number (u16) to f64.
/// Range is [0, ~1.99997]. We scale by (65535 / 32768) and round for compact output.
pub
/// Render bytes as uppercase hex grouped into 4-byte (8-hex) chunks separated by spaces.
/// Example: [0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC] -> "12345678 9abc"
/// This is used for displaying binary data in a human-readable format.
///
/// Example:
/// ```
/// let data = [0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC];
/// let formatted = cmx::format_hex_with_spaces(&data);
/// assert_eq!(formatted, "12345678 9abc");
/// ```
///
/// Note: The last chunk may be shorter than 8 characters if the data length is not a multiple of 4.
///
/// Parse a hex string with optional spaces into a byte vector.
/// Example: "12345678 9abc" -> [0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC]
/// This is used for converting human-readable hex strings back into binary data.
///
/// Example:
/// ```
/// let hex_str = "12345678 9abc";
/// let bytes = cmx::parse_hex_string(hex_str).unwrap();
/// assert_eq!(bytes, vec![0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC]);
/// ```
///
/// Notes:
/// * The input string can contain spaces and is case-insensitive.
/// * Non-hex characters and whitespace are ignored.
///
use ;
/// An ICC `s15Fixed16Number`: a signed 32-bit fixed-point value with 15 integer bits and
/// 16 fractional bits, stored in big-endian byte order.
///
/// The value `v` is encoded as `round(v × 65536)`. The representable range is roughly
/// `[−32768, 32767.99998]`. This type is used throughout ICC profiles for XYZ tristimulus
/// values, chromatic-adaptation matrices, and parametric curve parameters.
///
/// Conversion to and from `f64` is provided via the standard [`From`] trait. Values are
/// rounded to 5 decimal places on conversion to avoid floating-point noise.
;