1use 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
19const IMAGE_EXTENSIONS: [&str; 5] = ["png", "ppm", "pam", "raw", "rgba"];
21
22#[derive(Debug, Clone)]
24pub struct Options {
25 pub mode: EncodeMode,
27 pub metadata: Selection,
29 pub optimize: bool,
31 pub recursive: bool,
33 pub output_dir: Option<PathBuf>,
35}
36
37#[derive(Debug, Default)]
39pub struct Outcome {
40 pub converted: usize,
42 pub failed: usize,
44 pub total_in: u64,
46 pub total_out: u64,
48 pub lines: Vec<(bool, String)>,
50}
51
52pub 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 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
120fn 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
135fn 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
174fn 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}