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
//! ICC color profile parsing and management.
//!
//! Provides types for representing ICC color profile headers, full profiles,
//! and a registry for managing multiple profiles.
#![allow(dead_code)]
// ── ColorSpaceSignature ───────────────────────────────────────────────────────
/// ICC color space signature identifying the color model.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ColorSpaceSignature {
/// RGB (red, green, blue) color space.
Rgb,
/// CMYK (cyan, magenta, yellow, key/black) color space.
Cmyk,
/// Grayscale color space.
Gray,
/// CIE L*a*b* color space.
Lab,
/// CIE XYZ color space.
Xyz,
/// CIE L*u*v* color space.
Luv,
/// YUV color space.
Yuv,
/// YCbCr color space.
Ycbcr,
}
impl ColorSpaceSignature {
/// Returns the number of channels for this color space.
#[must_use]
pub fn channel_count(&self) -> u8 {
match self {
Self::Rgb | Self::Lab | Self::Xyz | Self::Luv | Self::Yuv | Self::Ycbcr => 3,
Self::Cmyk => 4,
Self::Gray => 1,
}
}
}
// ── ProfileClass ──────────────────────────────────────────────────────────────
/// ICC profile class, describing the type of device or transformation.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ProfileClass {
/// Input device profile (scanner, camera).
Input,
/// Display device profile (monitor).
Display,
/// Output device profile (printer, projector).
Output,
/// Device-link profile (chained device transforms).
DeviceLink,
/// Abstract profile (general color transformation).
Abstract,
/// Named color profile.
NamedColor,
}
impl ProfileClass {
/// Returns `true` if this is a real device profile (Input, Display, or Output).
#[must_use]
pub fn is_device_profile(&self) -> bool {
matches!(self, Self::Input | Self::Display | Self::Output)
}
}
// ── IccProfileHeader ──────────────────────────────────────────────────────────
/// ICC profile header fields (subset of the full 128-byte ICC header).
#[derive(Debug, Clone)]
pub struct IccProfileHeader {
/// Total profile size in bytes.
pub profile_size: u32,
/// Device color space signature.
pub color_space: ColorSpaceSignature,
/// Profile class (device type).
pub profile_class: ProfileClass,
/// Profile Connection Space (PCS) color space.
pub pcs: ColorSpaceSignature,
/// Rendering intent (0=Perceptual, 1=RelativeColorimetric, 2=Saturation, 3=AbsoluteColorimetric).
pub rendering_intent: u8,
/// Profile creator signature (4 ASCII bytes, e.g. b"ADBE").
pub creator: [u8; 4],
}
impl IccProfileHeader {
/// Returns `true` if the Profile Connection Space (PCS) is L*a*b* or XYZ,
/// which are the only valid PCS values in ICC specs.
#[must_use]
pub fn is_valid_pcs(&self) -> bool {
matches!(
self.pcs,
ColorSpaceSignature::Lab | ColorSpaceSignature::Xyz
)
}
}
// ── IccProfile ────────────────────────────────────────────────────────────────
/// A complete ICC color profile with header and textual metadata.
#[derive(Debug, Clone)]
pub struct IccProfile {
/// The parsed profile header.
pub header: IccProfileHeader,
/// Human-readable profile description.
pub description: String,
/// Copyright string embedded in the profile.
pub copyright: String,
}
impl IccProfile {
/// Returns `true` if the device color space of this profile is RGB.
#[must_use]
pub fn is_rgb(&self) -> bool {
self.header.color_space == ColorSpaceSignature::Rgb
}
/// Returns `true` if this is a display (monitor) profile.
#[must_use]
pub fn is_display_profile(&self) -> bool {
self.header.profile_class == ProfileClass::Display
}
}
// ── ProfileRegistry ───────────────────────────────────────────────────────────
/// A registry that stores and queries a collection of [`IccProfile`]s.
#[derive(Debug, Default)]
pub struct ProfileRegistry {
profiles: Vec<IccProfile>,
}
impl ProfileRegistry {
/// Create an empty registry.
#[must_use]
pub fn new() -> Self {
Self::default()
}
/// Add a profile to the registry.
pub fn add(&mut self, profile: IccProfile) {
self.profiles.push(profile);
}
/// Find profiles whose description contains `query` (case-insensitive).
#[must_use]
pub fn find_by_description(&self, query: &str) -> Vec<&IccProfile> {
let q = query.to_lowercase();
self.profiles
.iter()
.filter(|p| p.description.to_lowercase().contains(&q))
.collect()
}
/// Return all RGB profiles in the registry.
#[must_use]
pub fn rgb_profiles(&self) -> Vec<&IccProfile> {
self.profiles.iter().filter(|p| p.is_rgb()).collect()
}
/// Return the total number of profiles in the registry.
#[must_use]
pub fn count(&self) -> usize {
self.profiles.len()
}
}
// ── tests ─────────────────────────────────────────────────────────────────────
#[cfg(test)]
mod tests {
use super::*;
fn sample_header(
cs: ColorSpaceSignature,
cls: ProfileClass,
pcs: ColorSpaceSignature,
) -> IccProfileHeader {
IccProfileHeader {
profile_size: 1024,
color_space: cs,
profile_class: cls,
pcs,
rendering_intent: 0,
creator: *b"TEST",
}
}
fn rgb_display_profile(desc: &str) -> IccProfile {
IccProfile {
header: sample_header(
ColorSpaceSignature::Rgb,
ProfileClass::Display,
ColorSpaceSignature::Lab,
),
description: desc.to_string(),
copyright: "Test Copyright".to_string(),
}
}
// ── ColorSpaceSignature ────────────────────────────────────────────────────
#[test]
fn test_rgb_channel_count() {
assert_eq!(ColorSpaceSignature::Rgb.channel_count(), 3);
}
#[test]
fn test_cmyk_channel_count() {
assert_eq!(ColorSpaceSignature::Cmyk.channel_count(), 4);
}
#[test]
fn test_gray_channel_count() {
assert_eq!(ColorSpaceSignature::Gray.channel_count(), 1);
}
#[test]
fn test_lab_channel_count() {
assert_eq!(ColorSpaceSignature::Lab.channel_count(), 3);
}
#[test]
fn test_xyz_channel_count() {
assert_eq!(ColorSpaceSignature::Xyz.channel_count(), 3);
}
// ── ProfileClass ───────────────────────────────────────────────────────────
#[test]
fn test_input_is_device_profile() {
assert!(ProfileClass::Input.is_device_profile());
}
#[test]
fn test_display_is_device_profile() {
assert!(ProfileClass::Display.is_device_profile());
}
#[test]
fn test_output_is_device_profile() {
assert!(ProfileClass::Output.is_device_profile());
}
#[test]
fn test_devicelink_not_device_profile() {
assert!(!ProfileClass::DeviceLink.is_device_profile());
}
#[test]
fn test_abstract_not_device_profile() {
assert!(!ProfileClass::Abstract.is_device_profile());
}
#[test]
fn test_namedcolor_not_device_profile() {
assert!(!ProfileClass::NamedColor.is_device_profile());
}
// ── IccProfileHeader ───────────────────────────────────────────────────────
#[test]
fn test_valid_pcs_lab() {
let h = sample_header(
ColorSpaceSignature::Rgb,
ProfileClass::Display,
ColorSpaceSignature::Lab,
);
assert!(h.is_valid_pcs());
}
#[test]
fn test_valid_pcs_xyz() {
let h = sample_header(
ColorSpaceSignature::Rgb,
ProfileClass::Display,
ColorSpaceSignature::Xyz,
);
assert!(h.is_valid_pcs());
}
#[test]
fn test_invalid_pcs_rgb() {
let h = sample_header(
ColorSpaceSignature::Rgb,
ProfileClass::Display,
ColorSpaceSignature::Rgb,
);
assert!(!h.is_valid_pcs());
}
// ── IccProfile ─────────────────────────────────────────────────────────────
#[test]
fn test_is_rgb_true() {
let p = rgb_display_profile("sRGB");
assert!(p.is_rgb());
}
#[test]
fn test_is_rgb_false_for_gray() {
let p = IccProfile {
header: sample_header(
ColorSpaceSignature::Gray,
ProfileClass::Input,
ColorSpaceSignature::Lab,
),
description: "Gray profile".to_string(),
copyright: "".to_string(),
};
assert!(!p.is_rgb());
}
#[test]
fn test_is_display_profile_true() {
let p = rgb_display_profile("sRGB IEC61966-2.1");
assert!(p.is_display_profile());
}
#[test]
fn test_is_display_profile_false_for_input() {
let p = IccProfile {
header: sample_header(
ColorSpaceSignature::Rgb,
ProfileClass::Input,
ColorSpaceSignature::Lab,
),
description: "Camera profile".to_string(),
copyright: "".to_string(),
};
assert!(!p.is_display_profile());
}
// ── ProfileRegistry ────────────────────────────────────────────────────────
#[test]
fn test_registry_count_empty() {
let reg = ProfileRegistry::new();
assert_eq!(reg.count(), 0);
}
#[test]
fn test_registry_add_and_count() {
let mut reg = ProfileRegistry::new();
reg.add(rgb_display_profile("sRGB"));
reg.add(rgb_display_profile("AdobeRGB"));
assert_eq!(reg.count(), 2);
}
#[test]
fn test_registry_find_by_description() {
let mut reg = ProfileRegistry::new();
reg.add(rgb_display_profile("sRGB IEC61966-2.1"));
reg.add(rgb_display_profile("AdobeRGB (1998)"));
let found = reg.find_by_description("adobe");
assert_eq!(found.len(), 1);
assert!(found[0].description.contains("Adobe"));
}
#[test]
fn test_registry_find_by_description_no_match() {
let mut reg = ProfileRegistry::new();
reg.add(rgb_display_profile("sRGB"));
let found = reg.find_by_description("ProPhoto");
assert!(found.is_empty());
}
#[test]
fn test_registry_rgb_profiles() {
let mut reg = ProfileRegistry::new();
reg.add(rgb_display_profile("sRGB"));
// Add a CMYK profile
reg.add(IccProfile {
header: sample_header(
ColorSpaceSignature::Cmyk,
ProfileClass::Output,
ColorSpaceSignature::Lab,
),
description: "US Web Coated".to_string(),
copyright: "".to_string(),
});
let rgb = reg.rgb_profiles();
assert_eq!(rgb.len(), 1);
assert_eq!(rgb[0].description, "sRGB");
}
}