malmal 0.5.1

Upload scanner for images, SVG, PDF and PostScript 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
500
501
502
503
504
505
506
507
508
509
//! Colours that are not RGB, turned into sRGB: inks under the profile that
//! says what they look like, and CIE L*a*b*.

use moxcms::{ColorProfile, DataColorSpace, Layout, TransformOptions};

/// Pixels converted at a time.
const BATCH: usize = 1 << 16;

/// The CMYK profile `icc` is, if it is one.
fn cmyk_profile(icc: Option<&[u8]>) -> Option<ColorProfile> {
    let profile = ColorProfile::new_from_slice(icc?).ok()?;
    (profile.color_space == DataColorSpace::Cmyk).then_some(profile)
}

/// Four inks a pixel, 0 for none, as 8-bit sRGB: through the profile when
/// there is one that applies, by arithmetic otherwise.
pub(super) fn inks(inks: &[u8], icc: Option<&[u8]>) -> Vec<u8> {
    let mut colours = vec![0u8; inks.len() / 4 * 3];
    if let Some(profile) = cmyk_profile(icc) {
        let transform = profile.create_transform_8bit(
            Layout::Rgba,
            &ColorProfile::new_srgb(),
            Layout::Rgb,
            TransformOptions::default(),
        );
        if let Ok(transform) = transform {
            let batches = inks.chunks(BATCH * 4).zip(colours.chunks_mut(BATCH * 3));
            if batches
                .into_iter()
                .all(|(inks, colours)| transform.transform(inks, colours).is_ok())
            {
                return colours;
            }
        }
    }
    for (ink, colour) in inks
        .as_chunks::<4>()
        .0
        .iter()
        .zip(colours.as_chunks_mut::<3>().0.iter_mut())
    {
        let white = 255 - ink[3] as u32;
        for (c, &i) in colour.iter_mut().zip(&ink[..3]) {
            *c = ((255 - i as u32) * white / 255) as u8;
        }
    }
    colours
}

/// [`inks`] for 16-bit samples.
pub(super) fn inks16(inks: &[u16], icc: Option<&[u8]>) -> Vec<u16> {
    let mut colours = vec![0u16; inks.len() / 4 * 3];
    if let Some(profile) = cmyk_profile(icc) {
        let transform = profile.create_transform_16bit(
            Layout::Rgba,
            &ColorProfile::new_srgb(),
            Layout::Rgb,
            TransformOptions::default(),
        );
        if let Ok(transform) = transform {
            let batches = inks.chunks(BATCH * 4).zip(colours.chunks_mut(BATCH * 3));
            if batches
                .into_iter()
                .all(|(inks, colours)| transform.transform(inks, colours).is_ok())
            {
                return colours;
            }
        }
    }
    for (ink, colour) in inks
        .as_chunks::<4>()
        .0
        .iter()
        .zip(colours.as_chunks_mut::<3>().0.iter_mut())
    {
        let white = 65535 - ink[3] as u64;
        for (c, &i) in colour.iter_mut().zip(&ink[..3]) {
            *c = ((65535 - i as u64) * white / 65535) as u16;
        }
    }
    colours
}

/// Colours of CIE L*a*b* relative to a white, turned into sRGB.
pub(super) struct Lab {
    white: [f32; 3],
    /// From XYZ relative to that white to linear sRGB.
    matrix: [[f32; 3]; 3],
}

fn product(a: &[[f32; 3]; 3], b: &[[f32; 3]; 3]) -> [[f32; 3]; 3] {
    let mut out = [[0f32; 3]; 3];
    for (i, row) in out.iter_mut().enumerate() {
        for (j, entry) in row.iter_mut().enumerate() {
            *entry = (0..3).map(|k| a[i][k] * b[k][j]).sum();
        }
    }
    out
}

fn apply(m: &[[f32; 3]; 3], v: [f32; 3]) -> [f32; 3] {
    [0, 1, 2].map(|i| m[i][0] * v[0] + m[i][1] * v[1] + m[i][2] * v[2])
}

impl Lab {
    /// D50, the white of ICC profiles and of most files that do not say.
    pub(super) const D50: (f32, f32) = (0.3457, 0.3585);

    /// Relative to the white of chromaticity `(x, y)`.
    pub(super) fn new((x, y): (f32, f32)) -> Self {
        let white = [x / y, 1.0, (1.0 - x - y) / y];
        const BRADFORD: [[f32; 3]; 3] = [
            [0.8951, 0.2664, -0.1614],
            [-0.7502, 1.7135, 0.0367],
            [0.0389, -0.0685, 1.0296],
        ];
        const BRADFORD_INVERSE: [[f32; 3]; 3] = [
            [0.986_993, -0.147_054, 0.159_963],
            [0.432_305, 0.518_360, 0.049_291],
            [-0.008_529, 0.040_043, 0.968_487],
        ];
        const D65: [f32; 3] = [0.950_47, 1.0, 1.088_83];
        const SRGB: [[f32; 3]; 3] = [
            [3.240_454, -1.537_139, -0.498_531],
            [-0.969_266, 1.876_011, 0.041_556],
            [0.055_643, -0.204_026, 1.057_225],
        ];
        let (source, target) = (apply(&BRADFORD, white), apply(&BRADFORD, D65));
        let scale = [0, 1, 2].map(|i| {
            let mut row = [0f32; 3];
            row[i] = target[i] / source[i];
            row
        });
        let adapt = product(&BRADFORD_INVERSE, &product(&scale, &BRADFORD));
        Self {
            white,
            matrix: product(&SRGB, &adapt),
        }
    }

    /// L* from 0 to 100, as sRGB from 0 to 1.
    pub(super) fn srgb(&self, l: f32, a: f32, b: f32) -> [f32; 3] {
        let fy = (l + 16.0) / 116.0;
        let (fx, fz) = (fy + a / 500.0, fy - b / 200.0);
        let inverse = |t: f32| {
            const DELTA: f32 = 6.0 / 29.0;
            if t > DELTA {
                t * t * t
            } else {
                3.0 * DELTA * DELTA * (t - 4.0 / 29.0)
            }
        };
        let xyz = [
            self.white[0] * inverse(fx),
            self.white[1] * inverse(fy),
            self.white[2] * inverse(fz),
        ];
        apply(&self.matrix, xyz).map(|c| {
            let c = c.clamp(0.0, 1.0);
            if c <= 0.003_130_8 {
                12.92 * c
            } else {
                1.055 * c.powf(1.0 / 2.4) - 0.055
            }
        })
    }
}

/// Tags of a profile that say what colours its inks make, kept as they
/// are; every other tag is left out.
const COLOUR_TAGS: [&[u8; 4]; 10] = [
    b"A2B0", b"A2B1", b"A2B2", b"B2A0", b"B2A1", b"B2A2", b"wtpt", b"bkpt", b"chad", b"gamt",
];

/// Tags of a profile of a screen's colours that say what they are.
const RGB_TAGS: [&[u8; 4]; 16] = [
    b"rXYZ", b"gXYZ", b"bXYZ", b"rTRC", b"gTRC", b"bTRC", b"A2B0", b"A2B1", b"A2B2", b"B2A0",
    b"B2A1", b"B2A2", b"wtpt", b"bkpt", b"chad", b"cicp",
];

/// A grid of every mix of 17 amounts of each of `channels` samples, 16
/// bits each.
fn grid(channels: u32) -> Vec<u16> {
    (0..17u32.pow(channels))
        .flat_map(|i| {
            (0..channels)
                .rev()
                .map(move |c| (i / 17u32.pow(c) % 17 * 65535 / 16) as u16)
        })
        .collect()
}

/// The colours `profile` gives each pixel of `grid`, in sRGB.
fn through(profile: &ColorProfile, grid: &[u16], layout: Layout) -> Option<Vec<u16>> {
    let transform = profile
        .create_transform_16bit(
            layout,
            &ColorProfile::new_srgb(),
            Layout::Rgb,
            TransformOptions::default(),
        )
        .ok()?;
    let mut colours = vec![0u16; grid.len() / layout.channels() * 3];
    transform.transform(grid, &mut colours).ok()?;
    Some(colours)
}

/// A CMYK profile written anew: its header, the tags that describe its
/// colours byte for byte, and a description and copyright of its own.
/// Nothing else of the file is kept: no text, no private tags, nothing
/// between or after the tags. `None` unless the new profile makes the
/// same colours as the old of every mix of a grid of ink amounts.
pub(super) fn rewritten_cmyk(icc: &[u8]) -> Option<Vec<u8>> {
    let original = cmyk_profile(Some(icc))?;
    let out = rewritten(icc, &COLOUR_TAGS, "CMYK")?;
    let again = cmyk_profile(Some(&out))?;
    let grid = grid(4);
    (through(&original, &grid, Layout::Rgba)? == through(&again, &grid, Layout::Rgba)?)
        .then_some(out)
}

/// An RGB profile written anew as [`rewritten_cmyk`] writes one of inks,
/// from the tags that say what its colours are. `None` unless the new
/// profile makes the same colours as the old of every mix of a grid of
/// amounts of red, green and blue.
pub(super) fn rewritten_rgb(icc: &[u8]) -> Option<Vec<u8>> {
    let rgb = |icc: &[u8]| {
        ColorProfile::new_from_slice(icc)
            .ok()
            .filter(|p| p.color_space == DataColorSpace::Rgb)
    };
    let original = rgb(icc)?;
    let out = rewritten(icc, &RGB_TAGS, "RGB")?;
    let again = rgb(&out)?;
    let grid = grid(3);
    (through(&original, &grid, Layout::Rgb)? == through(&again, &grid, Layout::Rgb)?).then_some(out)
}

/// Tags of a profile of shades of grey that say what they are.
const GREY_TAGS: [&[u8; 4]; 5] = [b"kTRC", b"wtpt", b"bkpt", b"chad", b"cicp"];

/// A grey profile written anew as [`rewritten_rgb`] writes one of colours.
/// `None` unless the new profile makes the same colour as the old of every
/// one of 257 shades.
pub(super) fn rewritten_grey(icc: &[u8]) -> Option<Vec<u8>> {
    let grey = |icc: &[u8]| {
        ColorProfile::new_from_slice(icc)
            .ok()
            .filter(|p| p.color_space == DataColorSpace::Gray)
    };
    let original = grey(icc)?;
    let out = rewritten(icc, &GREY_TAGS, "Grey")?;
    let again = grey(&out)?;
    let shades: Vec<u16> = (0..=256u32).map(|i| (i * 65535 / 256) as u16).collect();
    (through(&original, &shades, Layout::Gray)? == through(&again, &shades, Layout::Gray)?)
        .then_some(out)
}

/// A profile of colours or of grey written anew, for pixels of `channels`
/// samples (1 or 3, alpha aside) that it is the right kind for.
pub(super) fn rewritten_screen(icc: &[u8], channels: usize) -> Option<Vec<u8>> {
    match channels {
        1 => rewritten_grey(icc),
        3 => rewritten_rgb(icc),
        _ => None,
    }
}

/// Whether `icc` says it is a profile of grey.
pub(super) fn is_grey(icc: &[u8]) -> bool {
    icc.get(16..20) == Some(b"GRAY")
}

/// The header of profile `icc`, the tags of it named in `tags` byte for
/// byte, and a description `name` and a copyright of its own.
fn rewritten(icc: &[u8], tags_kept: &[&[u8; 4]], name: &str) -> Option<Vec<u8>> {
    let be32 = |at: usize| Some(u32::from_be_bytes(icc.get(at..at + 4)?.try_into().ok()?));
    let count = be32(128)? as usize;
    if count > 256 {
        return None;
    }
    // The data of each tag kept, once however many tags share it.
    let mut kept: Vec<([u8; 4], usize, usize)> = Vec::new();
    for index in 0..count {
        let at = 132 + index * 12;
        let signature: [u8; 4] = icc.get(at..at + 4)?.try_into().ok()?;
        if !tags_kept.contains(&&signature) {
            continue;
        }
        let (offset, size) = (be32(at + 4)? as usize, be32(at + 8)? as usize);
        icc.get(offset..offset.checked_add(size)?)?;
        kept.push((signature, offset, size));
    }
    let text = |kind: &[u8; 4], words: &str| -> Vec<u8> {
        let mut tag = kind.to_vec();
        tag.extend_from_slice(&[0; 4]);
        match kind {
            // Version 2: ASCII, then empty Unicode and ScriptCode parts.
            b"desc" => {
                tag.extend_from_slice(&(words.len() as u32 + 1).to_be_bytes());
                tag.extend_from_slice(words.as_bytes());
                tag.push(0);
                tag.extend_from_slice(&[0; 4 + 4 + 2 + 1 + 67]);
            }
            _ => {
                tag.extend_from_slice(words.as_bytes());
                tag.push(0);
            }
        }
        tag
    };
    let version = icc.get(8).copied()?;
    let (description, copyright) = if version >= 4 {
        // Version 4 has one kind of text, in records of UTF-16.
        let unicode = |words: &str| {
            let mut tag = b"mluc\0\0\0\0".to_vec();
            tag.extend_from_slice(&1u32.to_be_bytes());
            tag.extend_from_slice(&12u32.to_be_bytes());
            tag.extend_from_slice(b"enUS");
            let utf16: Vec<u8> = words.encode_utf16().flat_map(u16::to_be_bytes).collect();
            tag.extend_from_slice(&(utf16.len() as u32).to_be_bytes());
            tag.extend_from_slice(&28u32.to_be_bytes());
            tag.extend_from_slice(&utf16);
            tag
        };
        (unicode(name), unicode("No copyright"))
    } else {
        (text(b"desc", name), text(b"text", "No copyright"))
    };

    let mut blocks: Vec<(Vec<[u8; 4]>, Vec<u8>)> = Vec::new();
    for (signature, offset, size) in &kept {
        let data = &icc[*offset..offset + size];
        match blocks.iter_mut().find(|(_, d)| d == data) {
            Some((signatures, _)) => signatures.push(*signature),
            None => blocks.push((vec![*signature], data.to_vec())),
        }
    }
    blocks.push((vec![*b"desc"], description));
    blocks.push((vec![*b"cprt"], copyright));
    let tags: usize = blocks.iter().map(|(s, _)| s.len()).sum();
    let mut out = icc.get(..128)?.to_vec();
    // Who made the profile, for what device, and its checksum, which
    // would no longer hold, are not kept.
    for range in [4..8, 40..44, 48..56, 56..64, 80..84, 84..100, 100..128] {
        out[range].fill(0);
    }
    out.extend_from_slice(&(tags as u32).to_be_bytes());
    let mut offset = 128 + 4 + tags * 12;
    let mut table = Vec::new();
    let mut body = Vec::new();
    for (signatures, data) in &blocks {
        for signature in signatures {
            table.extend_from_slice(signature);
            table.extend_from_slice(&(offset as u32).to_be_bytes());
            table.extend_from_slice(&(data.len() as u32).to_be_bytes());
        }
        body.extend_from_slice(data);
        // Each tag starts on four bytes.
        while body.len() % 4 != 0 {
            body.push(0);
        }
        offset = 128 + 4 + tags * 12 + body.len();
    }
    out.extend_from_slice(&table);
    out.extend_from_slice(&body);
    let size = out.len() as u32;
    out[..4].copy_from_slice(&size.to_be_bytes());

    Some(out)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn lab_white_black_and_red() {
        let close = |a: [f32; 3], b: [f32; 3]| a.iter().zip(b).all(|(x, y)| (x - y).abs() < 0.01);
        let d50 = Lab::new(Lab::D50);
        assert!(close(d50.srgb(100.0, 0.0, 0.0), [1.0, 1.0, 1.0]));
        assert!(close(d50.srgb(0.0, 0.0, 0.0), [0.0, 0.0, 0.0]));
        // sRGB red is L* 54.29, a* 80.80, b* 69.89 under D50, and
        // 53.24, 80.09, 67.20 under D65.
        assert!(close(d50.srgb(54.29, 80.80, 69.89), [1.0, 0.0, 0.0]));
        let d65 = Lab::new((0.3127, 0.3290));
        assert!(close(d65.srgb(53.24, 80.09, 67.20), [1.0, 0.0, 0.0]));
    }

    #[test]
    fn inks_without_a_profile_are_mixed_by_arithmetic() {
        assert_eq!(
            inks(&[0, 0, 0, 0, 255, 0, 0, 0, 0, 0, 0, 255], None),
            [255, 255, 255, 0, 255, 255, 0, 0, 0]
        );
    }

    /// Where the profiles of Ghostscript are, if it is installed.
    fn ghostscript_profiles() -> Option<std::path::PathBuf> {
        let mut places = vec![
            std::path::PathBuf::from("/usr/share/ghostscript/iccprofiles"),
            std::path::PathBuf::from("/usr/share/color/icc/ghostscript"),
        ];
        if let Ok(versions) = std::fs::read_dir("/usr/share/ghostscript") {
            places.extend(versions.flatten().map(|v| v.path().join("iccprofiles")));
        }
        places.into_iter().find(|p| p.join("sgray.icc").is_file())
    }

    /// The colours of `samples` (lines of numbers, one per channel, 0 to
    /// 255) through `icc` by Little CMS, in Lab; `None` without `transicc`.
    fn lcms_lab(icc: &[u8], samples: &str, intent: u8) -> Option<Vec<[f64; 3]>> {
        use std::io::Write;
        let dir = std::env::temp_dir().join(format!("malmal-lcms-{}", std::process::id()));
        std::fs::create_dir_all(&dir).ok()?;
        let path = dir.join(format!("{:016x}.icc", {
            use std::hash::{Hash, Hasher};
            let mut h = std::collections::hash_map::DefaultHasher::new();
            icc.hash(&mut h);
            h.finish()
        }));
        std::fs::write(&path, icc).ok()?;
        let mut child = std::process::Command::new("transicc")
            .args(["-n", "-t", &intent.to_string(), "-o", "*Lab", "-i"])
            .arg(&path)
            .stdin(std::process::Stdio::piped())
            .stdout(std::process::Stdio::piped())
            .stderr(std::process::Stdio::null())
            .spawn()
            .ok()?;
        child.stdin.take()?.write_all(samples.as_bytes()).ok()?;
        let out = child.wait_with_output().ok()?;
        let _ = std::fs::remove_file(&path);
        assert!(out.status.success(), "transicc failed");
        let lab = String::from_utf8(out.stdout)
            .unwrap()
            .lines()
            .filter(|l| !l.trim().is_empty())
            .map(|l| {
                let v: Vec<f64> = l.split_whitespace().map(|x| x.parse().unwrap()).collect();
                [v[0], v[1], v[2]]
            })
            .collect();
        Some(lab)
    }

    /// Profiles made by others than moxcms (those of Ghostscript) are
    /// rewritten, and Little CMS, which did not make them either, finds the
    /// new one gives every colour the old one does.
    #[test]
    fn profiles_of_ghostscript_are_rewritten_to_the_same_colours() {
        let required = std::env::var_os("MALMAL_REQUIRE_PROFILES").is_some();
        let Some(dir) = ghostscript_profiles() else {
            assert!(!required, "no profiles of Ghostscript");
            return;
        };
        let shades: String = (0..=256)
            .map(|i| format!("{}\n", f64::from(i) * 255.0 / 256.0))
            .collect();
        let steps = [0, 37, 64, 100, 128, 160, 200, 230, 255];
        let mut grid = String::new();
        for r in steps {
            for g in steps {
                for b in steps {
                    grid += &format!("{r} {g} {b}\n");
                }
            }
        }
        let mut checked_by_lcms = 0;
        for (name, grey) in [
            ("default_gray.icc", true),
            ("ps_gray.icc", true),
            ("sgray.icc", true),
            ("a98.icc", false),
            ("default_rgb.icc", false),
            ("esrgb.icc", false),
            ("ps_rgb.icc", false),
            ("rommrgb.icc", false),
            ("scrgb.icc", false),
            ("srgb.icc", false),
        ] {
            let Ok(icc) = std::fs::read(dir.join(name)) else {
                continue;
            };
            assert_eq!(is_grey(&icc), grey, "{name}");
            let new = rewritten_screen(&icc, if grey { 1 } else { 3 })
                .unwrap_or_else(|| panic!("{name} is not rewritten"));
            assert_eq!(is_grey(&new), grey, "{name}");
            let samples = if grey { &shades } else { &grid };
            for intent in [0, 1, 3] {
                let Some(before) = lcms_lab(&icc, samples, intent) else {
                    continue;
                };
                let after = lcms_lab(&new, samples, intent).unwrap();
                assert_eq!(before.len(), samples.lines().count(), "{name}");
                for (b, a) in before.iter().zip(&after) {
                    for (x, y) in b.iter().zip(a) {
                        assert!((x - y).abs() < 0.01, "{name}, intent {intent}: {b:?} {a:?}");
                    }
                }
                checked_by_lcms += 1;
            }
        }
        assert!(
            !required || checked_by_lcms == 30,
            "transicc checked {checked_by_lcms}"
        );
    }
}