write_icc.rs

Le fichier Rust de l'exemple « Le profil ICC embarqué ». Le profil ICC que la bibliothèque embarque dans un fichier PDF/A.

Rust 35 lignes

À quoi sert cet exemple

Cette entrée est un fichier à télécharger, pas une page à regarder. Un fichier destiné à l'archivage est obligé de contenir un profil de couleur décrivant l'appareil pour lequel ses couleurs ont été choisies, et la réponse habituelle consiste à livrer celui de quelqu'un d'autre : une centaine de kilooctets ou plus, avec une licence attachée, recopiés dans chacun de vos documents.

Ce que montre cet exemple

 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
//! Writes the ICC profile the library embeds in a PDF/A file.
//!
//! Our own tests can only say the bytes look right to us. Whether they are an
//! ICC profile — and whether that profile is really sRGB rather than something
//! wearing its name — is a question for a colour engine, which is what
//! `scripts/check_icc_profile.py` uses this for.
//!
//! Usage: `cargo run --example write_icc -- tmp/srgb.icc`

use std::env;
use std::fs;
use std::path::Path;

#[path = "shared/failure.rs"]
mod failure;

fn main() -> std::process::ExitCode {
    failure::reported(run())
}

fn run() -> Result<(), Box<dyn std::error::Error>> {
    let out = env::args()
        .nth(1)
        .unwrap_or_else(|| "tmp/srgb.icc".to_owned());

    let profile = hqf_pdf::color::icc::srgb();

    if let Some(parent) = Path::new(&out).parent() {
        fs::create_dir_all(parent)?;
    }
    fs::write(&out, &profile)?;

    println!("wrote {out}: {} bytes", profile.len());
    Ok(())
}