write_icc.rs

Die Rust-Datei des Beispiels „Das eingebettete ICC-Profil“. Das ICC-Profil, das die Bibliothek in eine PDF/A-Datei einbettet.

Rust 35 Zeilen

Wofür dieses Beispiel da ist

Dieser Eintrag ist ein Download, keine Seite zum Ansehen. Eine zur Archivierung bestimmte Datei muss ein Farbprofil tragen, das das Gerät beschreibt, für das ihre Farben bestimmt sind, und die übliche Antwort ist, das eines anderen mitzuliefern: hundert Kilobyte oder mehr, mit einer angehängten Lizenz, in jedes Dokument kopiert, das Sie je erzeugen.

Was dieses Beispiel zeigt

 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(())
}