Skip to main content

webpkit_cli/
bulk.rs

1//! Parallel bulk / directory conversion for `webp convert`.
2//!
3//! Files (and directories, optionally recursive) are encoded to WebP in
4//! parallel with rayon. `--optimize` tries every effort level per file and
5//! keeps the smallest output.
6
7use std::path::{Path, PathBuf};
8
9use rayon::prelude::*;
10use webpkit::lossless::{Effort, Image, Metadata};
11
12use crate::{
13    codec::{self, EncodeMode},
14    error::CliError,
15    format::{self, InputFormat},
16    metadata::Selection,
17};
18
19/// Extensions treated as encodable image inputs when scanning directories.
20const IMAGE_EXTENSIONS: [&str; 5] = ["png", "ppm", "pam", "raw", "rgba"];
21
22/// Options for a bulk conversion run.
23#[derive(Debug, Clone)]
24pub struct Options {
25    /// The codec and knobs (ignored for the lossless effort search under `optimize`).
26    pub mode: EncodeMode,
27    /// Which metadata to carry through.
28    pub metadata: Selection,
29    /// Try every lossless effort level and keep the smallest output.
30    pub optimize: bool,
31    /// Recurse into subdirectories.
32    pub recursive: bool,
33    /// Output directory; when absent, each output sits beside its input.
34    pub output_dir: Option<PathBuf>,
35}
36
37/// One result line and the aggregate totals from a bulk run.
38#[derive(Debug, Default)]
39pub struct Outcome {
40    /// Number of files converted successfully.
41    pub converted: usize,
42    /// Number of files that failed.
43    pub failed: usize,
44    /// Total input bytes across successful files.
45    pub total_in: u64,
46    /// Total output bytes across successful files.
47    pub total_out: u64,
48    /// Per-file report lines paired with whether that file succeeded.
49    pub lines: Vec<(bool, String)>,
50}
51
52/// Convert every input file (expanding directories) to WebP.
53///
54/// # Errors
55///
56/// [`CliError`] only for a failure that prevents the whole run (e.g. a
57/// directory that cannot be read); per-file failures are recorded in the
58/// returned [`Outcome`].
59pub fn convert(inputs: &[PathBuf], options: &Options) -> Result<Outcome, CliError> {
60    let files = collect_files(inputs, options.recursive)?;
61    let results: Vec<Result<Stat, String>> = files
62        .par_iter()
63        .map(|path| convert_one(path, options).map_err(|err| format!("{}: {err}", path.display())))
64        .collect();
65
66    let mut outcome = Outcome::default();
67    for result in results {
68        match result {
69            Ok(stat) => {
70                outcome.converted += 1;
71                outcome.total_in += stat.in_len;
72                outcome.total_out += stat.out_len;
73                outcome.lines.push((
74                    true,
75                    format!("{} ({} bytes)", stat.output.display(), stat.out_len),
76                ));
77            },
78            Err(message) => {
79                outcome.failed += 1;
80                outcome.lines.push((false, message));
81            },
82        }
83    }
84    Ok(outcome)
85}
86
87struct Stat {
88    output: PathBuf,
89    in_len: u64,
90    out_len: u64,
91}
92
93fn convert_one(path: &Path, options: &Options) -> Result<Stat, CliError> {
94    let bytes = std::fs::read(path)
95        .map_err(|err| CliError::read_input(path.display().to_string(), &err))?;
96    let format = InputFormat::resolve(None, extension_of(path).as_deref(), &bytes);
97    if format == InputFormat::Raw {
98        return Err(CliError::Format(
99            "raw input needs explicit dimensions; use `webp encode`".to_owned(),
100        ));
101    }
102    let image = format::read_image(&bytes, format, None)?;
103    let metadata = options.metadata.apply(image.metadata());
104    // `--optimize` searches the three lossless effort levels; a lossy request keeps
105    // its own single (quality, effort) rather than an effort sweep.
106    let webp = match (options.optimize, options.mode) {
107        (true, EncodeMode::Lossless(_)) => encode_smallest(&image, &metadata)?,
108        (_, mode) => codec::encode(&image, mode, metadata)?,
109    };
110    let output = output_path(path, options.output_dir.as_deref());
111    std::fs::write(&output, &webp)
112        .map_err(|err| CliError::write_output(output.display().to_string(), &err))?;
113    Ok(Stat {
114        output,
115        in_len: bytes.len() as u64,
116        out_len: webp.len() as u64,
117    })
118}
119
120/// Encode with every lossless effort level and return the smallest output.
121fn encode_smallest(image: &Image, metadata: &Metadata) -> Result<Vec<u8>, CliError> {
122    let mut smallest: Option<Vec<u8>> = None;
123    for method in [Effort::Fast, Effort::Balanced, Effort::Best] {
124        let candidate = codec::encode(image, EncodeMode::Lossless(method), metadata.clone())?;
125        if smallest
126            .as_ref()
127            .is_none_or(|best| candidate.len() < best.len())
128        {
129            smallest = Some(candidate);
130        }
131    }
132    smallest.ok_or_else(|| CliError::Format("no encoder produced output".to_owned()))
133}
134
135/// Expand `inputs` (files and directories) into a flat list of files.
136fn collect_files(inputs: &[PathBuf], recursive: bool) -> Result<Vec<PathBuf>, CliError> {
137    let mut files = Vec::new();
138    for input in inputs {
139        if input.is_dir() {
140            walk(input, recursive, &mut files)?;
141        } else {
142            files.push(input.clone());
143        }
144    }
145    Ok(files)
146}
147
148fn walk(dir: &Path, recursive: bool, out: &mut Vec<PathBuf>) -> Result<(), CliError> {
149    let entries = std::fs::read_dir(dir)
150        .map_err(|err| CliError::read_input(dir.display().to_string(), &err))?;
151    for entry in entries {
152        let entry = entry.map_err(|err| CliError::read_input(dir.display().to_string(), &err))?;
153        let path = entry.path();
154        if path.is_dir() {
155            if recursive {
156                walk(&path, recursive, out)?;
157            }
158        } else if is_image(&path) {
159            out.push(path);
160        }
161    }
162    Ok(())
163}
164
165fn is_image(path: &Path) -> bool {
166    extension_of(path).is_some_and(|ext| IMAGE_EXTENSIONS.contains(&ext.as_str()))
167}
168
169fn extension_of(path: &Path) -> Option<String> {
170    path.extension()
171        .map(|ext| ext.to_string_lossy().to_ascii_lowercase())
172}
173
174/// The `.webp` output path: `<stem>.webp` in the output dir, else beside input.
175fn output_path(input: &Path, output_dir: Option<&Path>) -> PathBuf {
176    let stem = input.file_stem().unwrap_or(input.as_os_str());
177    let dir = output_dir
178        .map(Path::to_path_buf)
179        .or_else(|| input.parent().map(Path::to_path_buf))
180        .unwrap_or_default();
181    dir.join(stem).with_extension("webp")
182}