exiftool-rs 0.5.0

Read, write, and edit metadata in 93 file formats — a pure Rust reimplementation of ExifTool 13.53 with 100% tag name parity (194/194 test files)
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
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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
//! ICC Color Profile reader.
//!
//! Parses ICC profile header for color space and rendering intent info.

use crate::error::{Error, Result};
use crate::tag::{Tag, TagGroup, TagId};
use crate::value::Value;

pub fn read_icc(data: &[u8]) -> Result<Vec<Tag>> {
    if data.len() < 128 || &data[36..40] != b"acsp" {
        return Err(Error::InvalidData("not an ICC profile".into()));
    }

    let mut tags = Vec::new();

    let preferred_cmm = crate::encoding::decode_utf8_or_latin1(&data[4..8])
        .trim()
        .to_string();
    if !preferred_cmm.is_empty() && preferred_cmm != "\0\0\0\0" {
        tags.push(mk(
            "ProfileCMMType",
            "Profile CMM Type",
            Value::String(preferred_cmm),
        ));
    }

    let major = data[8];
    let minor = (data[9] >> 4) & 0x0F;
    let patch = data[9] & 0x0F;
    tags.push(mk(
        "ProfileVersion",
        "Profile Version",
        Value::String(format!("{}.{}.{}", major, minor, patch)),
    ));

    let device_class = crate::encoding::decode_utf8_or_latin1(&data[12..16]).to_string();
    let class_name = match device_class.trim() {
        "scnr" => "Input Device Profile",
        "mntr" => "Display Device Profile",
        "prtr" => "Output Device Profile",
        "link" => "DeviceLink Profile",
        "spac" => "ColorSpace Conversion Profile",
        "abst" => "Abstract Profile",
        "nmcl" => "Named Color Profile",
        _ => &device_class,
    };
    tags.push(mk(
        "ProfileClass",
        "Profile Class",
        Value::String(class_name.to_string()),
    ));

    let color_space = crate::encoding::decode_utf8_or_latin1(&data[16..20])
        .trim()
        .to_string();
    let cs_name = match color_space.as_str() {
        "XYZ" => "XYZ",
        "Lab" => "Lab",
        "Luv" => "Luv",
        "YCbr" => "YCbCr",
        "Yxy" => "Yxy",
        "RGB" => "RGB",
        "GRAY" => "Grayscale",
        "HSV" => "HSV",
        "HLS" => "HLS",
        "CMYK" => "CMYK",
        "CMY" => "CMY",
        _ => &color_space,
    };
    tags.push(mk(
        "ColorSpaceData",
        "Color Space",
        Value::String(cs_name.to_string()),
    ));

    let pcs = crate::encoding::decode_utf8_or_latin1(&data[20..24])
        .trim()
        .to_string();
    tags.push(mk(
        "ProfileConnectionSpace",
        "Connection Space",
        Value::String(pcs),
    ));

    // Creation date (bytes 24-35): year(2), month(2), day(2), hour(2), minute(2), second(2)
    let year = u16::from_be_bytes([data[24], data[25]]);
    let month = u16::from_be_bytes([data[26], data[27]]);
    let day = u16::from_be_bytes([data[28], data[29]]);
    let hour = u16::from_be_bytes([data[30], data[31]]);
    let min = u16::from_be_bytes([data[32], data[33]]);
    let sec = u16::from_be_bytes([data[34], data[35]]);
    tags.push(mk(
        "ProfileDateTime",
        "Profile Date/Time",
        Value::String(format!(
            "{:04}:{:02}:{:02} {:02}:{:02}:{:02}",
            year, month, day, hour, min, sec
        )),
    ));

    // Primary platform (bytes 40-43)
    let platform = crate::encoding::decode_utf8_or_latin1(&data[40..44])
        .trim()
        .to_string();
    let platform_name = match platform.as_str() {
        "APPL" => "Apple",
        "MSFT" => "Microsoft",
        "SGI " => "SGI",
        "SUNW" => "Sun Microsystems",
        _ => &platform,
    };
    if !platform_name.is_empty() {
        tags.push(mk(
            "PrimaryPlatform",
            "Primary Platform",
            Value::String(platform_name.to_string()),
        ));
    }

    // Rendering intent (byte 67)
    let intent = u32::from_be_bytes([data[64], data[65], data[66], data[67]]);
    let intent_name = match intent {
        0 => "Perceptual",
        1 => "Media-Relative Colorimetric",
        2 => "Saturation",
        3 => "ICC-Absolute Colorimetric",
        _ => "Unknown",
    };
    tags.push(mk(
        "RenderingIntent",
        "Rendering Intent",
        Value::String(intent_name.to_string()),
    ));

    // CMMFlags (bytes 44-47)
    let flags = u32::from_be_bytes([data[44], data[45], data[46], data[47]]);
    tags.push(mk(
        "CMMFlags",
        "CMM Flags",
        Value::String(format!("0x{:08X}", flags)),
    ));

    // DeviceAttributes (bytes 56-63)
    let attr = u64::from_be_bytes([
        data[56], data[57], data[58], data[59], data[60], data[61], data[62], data[63],
    ]);
    tags.push(mk(
        "DeviceAttributes",
        "Device Attributes",
        Value::String(format!("{}", attr)),
    ));

    // Device manufacturer (bytes 48-51) and model (52-55)
    let manufacturer = crate::encoding::decode_utf8_or_latin1(&data[48..52])
        .trim()
        .to_string();
    if !manufacturer.is_empty() && manufacturer.bytes().any(|b| b > 0x20) {
        tags.push(mk(
            "DeviceManufacturer",
            "Device Manufacturer",
            Value::String(manufacturer),
        ));
    }
    // DeviceModel: always emit (may be empty string), from ICC profile header bytes 52-55
    let dev_model_raw = &data[52..56];
    let dev_model = if dev_model_raw.iter().all(|&b| b == 0) {
        String::new()
    } else {
        crate::encoding::decode_utf8_or_latin1(dev_model_raw)
            .trim_end_matches('\0')
            .trim()
            .to_string()
    };
    tags.push(mk("DeviceModel", "Device Model", Value::String(dev_model)));

    // ProfileFileSignature (bytes 36-39, should be "acsp")
    tags.push(mk(
        "ProfileFileSignature",
        "Profile File Signature",
        Value::String("acsp".into()),
    ));

    // ConnectionSpaceIlluminant (bytes 68-79, XYZ)
    if data.len() >= 80 {
        let x = i32::from_be_bytes([data[68], data[69], data[70], data[71]]) as f64 / 65536.0;
        let y = i32::from_be_bytes([data[72], data[73], data[74], data[75]]) as f64 / 65536.0;
        let z = i32::from_be_bytes([data[76], data[77], data[78], data[79]]) as f64 / 65536.0;
        tags.push(mk(
            "ConnectionSpaceIlluminant",
            "Connection Space Illuminant",
            Value::String(format!("{:.6} {:.6} {:.6}", x, y, z)),
        ));
    }

    // ProfileCreator (bytes 80-83) - always emit (may be empty)
    if data.len() >= 84 {
        let raw = &data[80..84];
        let creator = if raw.iter().all(|&b| b == 0) {
            String::new()
        } else {
            crate::encoding::decode_utf8_or_latin1(raw)
                .trim_end_matches('\0')
                .trim()
                .to_string()
        };
        tags.push(mk(
            "ProfileCreator",
            "Profile Creator",
            Value::String(creator),
        ));
    }

    // ProfileID (bytes 84-99, MD5)
    if data.len() >= 100 {
        let id: String = data[84..100].iter().map(|b| format!("{:02x}", b)).collect();
        tags.push(mk("ProfileID", "Profile ID", Value::String(id)));
    }

    // Profile description tag - search in tag table
    if data.len() >= 132 {
        let tag_count = u32::from_be_bytes([data[128], data[129], data[130], data[131]]) as usize;
        let mut tpos = 132;
        for _ in 0..tag_count.min(100) {
            if tpos + 12 > data.len() {
                break;
            }
            let sig = &data[tpos..tpos + 4];
            let offset = u32::from_be_bytes([
                data[tpos + 4],
                data[tpos + 5],
                data[tpos + 6],
                data[tpos + 7],
            ]) as usize;
            let size = u32::from_be_bytes([
                data[tpos + 8],
                data[tpos + 9],
                data[tpos + 10],
                data[tpos + 11],
            ]) as usize;
            tpos += 12;

            if sig == b"desc" && offset + size <= data.len() && size > 12 {
                // 'desc' type: 4 bytes signature + 4 reserved + 4 bytes string length + string
                let d = &data[offset..offset + size];
                if d.len() >= 12 && &d[0..4] == b"desc" {
                    let str_len = u32::from_be_bytes([d[8], d[9], d[10], d[11]]) as usize;
                    if 12 + str_len <= d.len() {
                        let desc = crate::encoding::decode_utf8_or_latin1(&d[12..12 + str_len])
                            .trim_end_matches('\0')
                            .to_string();
                        if !desc.is_empty() {
                            tags.push(mk(
                                "ProfileDescription",
                                "Profile Description",
                                Value::String(desc),
                            ));
                        }
                    }
                }
            }

            if sig == b"cprt" && offset + size <= data.len() && size > 8 {
                let d = &data[offset..offset + size];
                if d.len() >= 8 && &d[0..4] == b"text" {
                    let text = crate::encoding::decode_utf8_or_latin1(&d[8..])
                        .trim_end_matches('\0')
                        .to_string();
                    if !text.is_empty() {
                        tags.push(mk(
                            "ProfileCopyright",
                            "Profile Copyright",
                            Value::String(text),
                        ));
                    }
                }
            }

            // Map ICC tag signatures to names (from Perl ICC_Profile.pm)
            if offset + size <= data.len() && size >= 8 {
                let d = &data[offset..offset + size];
                let tag_name = match sig {
                    b"rXYZ" => "RedMatrixColumn",
                    b"gXYZ" => "GreenMatrixColumn",
                    b"bXYZ" => "BlueMatrixColumn",
                    b"wtpt" => "MediaWhitePoint",
                    b"bkpt" => "MediaBlackPoint",
                    b"lumi" => "Luminance",
                    b"rTRC" => "RedTRC",
                    b"gTRC" => "GreenTRC",
                    b"bTRC" => "BlueTRC",
                    b"tech" => "Technology",
                    b"dmnd" => "DeviceMfgDesc",
                    b"dmdd" => "DeviceModelDesc",
                    b"vued" => "ViewingCondDesc",
                    b"view" => "ViewingConditions",
                    b"meas" => "MeasurementInfo",
                    b"chad" => "ChromaticAdaptation",
                    _ => "",
                };
                if !tag_name.is_empty() {
                    let type_sig = &d[0..4];
                    let value = match type_sig {
                        b"XYZ " if d.len() >= 20 => {
                            // XYZ type: 3 x s15Fixed16
                            let x = i32::from_be_bytes([d[8], d[9], d[10], d[11]]) as f64 / 65536.0;
                            let y =
                                i32::from_be_bytes([d[12], d[13], d[14], d[15]]) as f64 / 65536.0;
                            let z =
                                i32::from_be_bytes([d[16], d[17], d[18], d[19]]) as f64 / 65536.0;
                            format!("{:.6} {:.6} {:.6}", x, y, z)
                        }
                        b"curv" if d.len() >= 12 => {
                            let count = u32::from_be_bytes([d[8], d[9], d[10], d[11]]);
                            if count == 0 {
                                "Linear".into()
                            } else if count == 1 && d.len() >= 14 {
                                let gamma = u16::from_be_bytes([d[12], d[13]]) as f64 / 256.0;
                                format!("{:.1}", gamma)
                            } else {
                                format!("(Binary data {} entries)", count)
                            }
                        }
                        b"desc" if d.len() >= 12 => {
                            let len = u32::from_be_bytes([d[8], d[9], d[10], d[11]]) as usize;
                            if 12 + len <= d.len() {
                                crate::encoding::decode_utf8_or_latin1(&d[12..12 + len])
                                    .trim_end_matches('\0')
                                    .to_string()
                            } else {
                                String::new()
                            }
                        }
                        b"mluc" if d.len() >= 20 => {
                            // multiLocalizedUnicode
                            let rec_count = u32::from_be_bytes([d[8], d[9], d[10], d[11]]) as usize;
                            if rec_count > 0 && d.len() >= 20 {
                                let str_off =
                                    u32::from_be_bytes([d[20], d[21], d[22], d[23]]) as usize;
                                let str_len =
                                    u32::from_be_bytes([d[16], d[17], d[18], d[19]]) as usize;
                                if str_off + str_len <= d.len() {
                                    let units: Vec<u16> = d[str_off..str_off + str_len]
                                        .chunks_exact(2)
                                        .map(|c| u16::from_be_bytes([c[0], c[1]]))
                                        .collect();
                                    String::from_utf16_lossy(&units)
                                        .trim_end_matches('\0')
                                        .to_string()
                                } else {
                                    String::new()
                                }
                            } else {
                                String::new()
                            }
                        }
                        b"sig " if d.len() >= 12 => {
                            crate::encoding::decode_utf8_or_latin1(&d[8..12])
                                .trim()
                                .to_string()
                        }
                        b"meas" if d.len() >= 36 => {
                            // measurement type
                            let observer = match u32::from_be_bytes([d[8], d[9], d[10], d[11]]) {
                                1 => "CIE 1931",
                                2 => "CIE 1964",
                                _ => "Unknown",
                            };
                            tags.push(mk(
                                "MeasurementObserver",
                                "Measurement Observer",
                                Value::String(observer.into()),
                            ));
                            let geometry = match u32::from_be_bytes([d[24], d[25], d[26], d[27]]) {
                                1 => "0/45 or 45/0",
                                2 => "0/d or d/0",
                                _ => "Unknown",
                            };
                            tags.push(mk(
                                "MeasurementGeometry",
                                "Measurement Geometry",
                                Value::String(geometry.into()),
                            ));
                            let illum = match u32::from_be_bytes([d[32], d[33], d[34], d[35]]) {
                                1 => "D50",
                                2 => "D65",
                                3 => "D93",
                                4 => "F2",
                                5 => "D55",
                                6 => "A",
                                7 => "E",
                                8 => "F8",
                                _ => "Unknown",
                            };
                            tags.push(mk(
                                "MeasurementIlluminant",
                                "Measurement Illuminant",
                                Value::String(illum.into()),
                            ));
                            // Backing and flare
                            let backing_x =
                                i32::from_be_bytes([d[12], d[13], d[14], d[15]]) as f64 / 65536.0;
                            let backing_y =
                                i32::from_be_bytes([d[16], d[17], d[18], d[19]]) as f64 / 65536.0;
                            let backing_z =
                                i32::from_be_bytes([d[20], d[21], d[22], d[23]]) as f64 / 65536.0;
                            tags.push(mk(
                                "MeasurementBacking",
                                "Measurement Backing",
                                Value::String(format!(
                                    "{:.6} {:.6} {:.6}",
                                    backing_x, backing_y, backing_z
                                )),
                            ));
                            let flare =
                                u32::from_be_bytes([d[28], d[29], d[30], d[31]]) as f64 / 65536.0;
                            tags.push(mk(
                                "MeasurementFlare",
                                "Measurement Flare",
                                Value::String(format!("{:.4}%", flare * 100.0)),
                            ));
                            String::new() // sub-tags already pushed
                        }
                        b"view" if d.len() >= 28 => {
                            let x = i32::from_be_bytes([d[8], d[9], d[10], d[11]]) as f64 / 65536.0;
                            let y =
                                i32::from_be_bytes([d[12], d[13], d[14], d[15]]) as f64 / 65536.0;
                            let z =
                                i32::from_be_bytes([d[16], d[17], d[18], d[19]]) as f64 / 65536.0;
                            tags.push(mk(
                                "ViewingCondIlluminant",
                                "Viewing Cond Illuminant",
                                Value::String(format!("{:.5} {:.5} {:.5}", x, y, z)),
                            ));
                            let sx =
                                i32::from_be_bytes([d[20], d[21], d[22], d[23]]) as f64 / 65536.0;
                            let sy =
                                i32::from_be_bytes([d[24], d[25], d[26], d[27]]) as f64 / 65536.0;
                            let sz =
                                i32::from_be_bytes([d[28], d[29], d[30], d[31]]) as f64 / 65536.0;
                            tags.push(mk(
                                "ViewingCondSurround",
                                "Viewing Cond Surround",
                                Value::String(format!("{:.5} {:.5} {:.5}", sx, sy, sz)),
                            ));
                            if d.len() >= 36 {
                                let illum_type =
                                    match u32::from_be_bytes([d[32], d[33], d[34], d[35]]) {
                                        1 => "D50",
                                        2 => "D65",
                                        3 => "D93",
                                        4 => "F2",
                                        5 => "D55",
                                        6 => "A",
                                        7 => "E",
                                        8 => "F8",
                                        _ => "Unknown",
                                    };
                                tags.push(mk(
                                    "ViewingCondIlluminantType",
                                    "Viewing Cond Illuminant Type",
                                    Value::String(illum_type.into()),
                                ));
                            }
                            String::new()
                        }
                        _ => String::new(),
                    };
                    if !value.is_empty() {
                        tags.push(mk(tag_name, tag_name, Value::String(value)));
                    }
                }
            }
        }
    }

    Ok(tags)
}

fn mk(name: &str, description: &str, value: Value) -> Tag {
    let pv = value.to_display_string();
    Tag {
        id: TagId::Text(name.to_string()),
        name: name.to_string(),
        description: description.to_string(),
        group: TagGroup {
            family0: "ICC_Profile".into(),
            family1: "ICC_Profile".into(),
            family2: "Image".into(),
        },
        raw_value: value,
        print_value: pv,
        priority: 0,
    }
}

/// Parse ICC profile tags from raw profile data embedded in JPEG APP2.
pub fn parse_icc_tags(data: &[u8]) -> Vec<Tag> {
    read_icc(data).unwrap_or_default()
}