Skip to main content

resopt/
analysis.rs

1use crate::{
2    ImageDifference, ImageInfo, Policy, Resource, ResourceInventory,
3    filesystem::{contained_file, hash, write_new},
4    image_backend, optimizer,
5    resources::{bounded_read, inventory},
6};
7use anyhow::{Context, Result, ensure};
8use rayon::prelude::*;
9use serde::{Deserialize, Serialize};
10use std::{
11    collections::BTreeMap,
12    fs,
13    path::{Path, PathBuf},
14    sync::atomic::{AtomicUsize, Ordering},
15};
16
17#[derive(Debug, Clone, Serialize, Deserialize)]
18#[serde(default, deny_unknown_fields)]
19pub struct AnalysisOptions {
20    pub qualities: Vec<u8>,
21    pub jobs: usize,
22    /// All sizes are included by default, unlike the legacy lossless plan.
23    pub min_input_bytes: u64,
24    pub min_savings_bytes: u64,
25    pub probe_only: bool,
26    /// Lossy HEIC may quantize alpha. 0 requires exact alpha samples.
27    pub max_alpha_error: f32,
28}
29impl Default for AnalysisOptions {
30    fn default() -> Self {
31        Self {
32            qualities: vec![75, 85, 95],
33            jobs: 2,
34            min_input_bytes: 0,
35            min_savings_bytes: 1,
36            probe_only: false,
37            max_alpha_error: 1.0 / 255.0 + 0.000001,
38        }
39    }
40}
41impl AnalysisOptions {
42    fn validate(&self) -> Result<()> {
43        ensure!(
44            self.max_alpha_error.is_finite() && (0.0..=1.0).contains(&self.max_alpha_error),
45            "max_alpha_error must be 0..=1"
46        );
47        ensure!((1..=8).contains(&self.jobs), "jobs must be 1..=8");
48        ensure!(
49            !self.qualities.is_empty()
50                && self.qualities.len() <= 8
51                && self.qualities.iter().all(|q| (1..=100).contains(q)),
52            "qualities must contain 1..=8 values in 1..=100"
53        );
54        let mut qualities = self.qualities.clone();
55        qualities.sort_unstable();
56        qualities.dedup();
57        ensure!(
58            qualities.len() == self.qualities.len(),
59            "duplicate quality values"
60        );
61        Ok(())
62    }
63}
64
65#[derive(Debug, Clone, Serialize, Deserialize)]
66pub struct ImageCandidate {
67    pub format: String,
68    pub quality: Option<u8>,
69    pub lossy: bool,
70    pub bytes: u64,
71    pub savings_bytes: u64,
72    pub valid: bool,
73    pub rejection: Option<String>,
74    pub difference: Option<ImageDifference>,
75    pub artifact: Option<PathBuf>,
76    pub preview: Option<PathBuf>,
77}
78
79#[derive(Debug, Clone, Serialize, Deserialize)]
80pub struct ResourceAnalysis {
81    pub resource: Resource,
82    pub sha256: Option<String>,
83    pub image: Option<ImageInfo>,
84    pub status: String,
85    pub issues: Vec<String>,
86    pub candidates: Vec<ImageCandidate>,
87    /// A size winner, not an assertion of acceptable visual quality.
88    pub smallest_candidate: Option<usize>,
89    pub original_preview: Option<PathBuf>,
90    pub original_artifact: Option<PathBuf>,
91}
92
93#[derive(Debug, Serialize, Deserialize)]
94pub struct AnalysisReport {
95    pub schema_version: u32,
96    pub root: PathBuf,
97    pub backend: String,
98    pub options: AnalysisOptions,
99    pub inventory: ResourceInventory,
100    pub resources: Vec<ResourceAnalysis>,
101    pub status_counts: BTreeMap<String, usize>,
102    pub potential_source_bytes_saved: u64,
103}
104
105/// Read-only analysis of all inventoried resources, with lossy candidates staged
106/// solely for review. This report is deliberately not an executable apply plan.
107pub fn analyze(
108    root: impl AsRef<Path>,
109    out: impl AsRef<Path>,
110    options: AnalysisOptions,
111) -> Result<AnalysisReport> {
112    analyze_with_progress(root, out, options, |_, _| {})
113}
114
115pub fn analyze_with_progress(
116    root: impl AsRef<Path>,
117    out: impl AsRef<Path>,
118    options: AnalysisOptions,
119    progress: impl Fn(usize, usize) + Sync,
120) -> Result<AnalysisReport> {
121    options.validate()?;
122    if !options.probe_only {
123        image_backend::check_encoders()?;
124    }
125    let inventory = inventory(root)?;
126    let out = out.as_ref();
127    let parent = fs::canonicalize(
128        out.parent()
129            .filter(|p| !p.as_os_str().is_empty())
130            .unwrap_or(Path::new(".")),
131    )?;
132    let out = parent.join(out.file_name().context("output directory has no name")?);
133    ensure!(
134        !out.starts_with(&inventory.root),
135        "analysis output must be outside the scanned project"
136    );
137    fs::create_dir(&out).context("analysis output must be a new directory")?;
138    fs::create_dir(out.join("candidates"))?;
139    fs::create_dir(out.join("previews"))?;
140    fs::create_dir(out.join("originals"))?;
141    let pool = rayon::ThreadPoolBuilder::new()
142        .num_threads(options.jobs)
143        .build()?;
144    let complete = AtomicUsize::new(0);
145    let resources = pool.install(|| {
146        inventory
147            .assets
148            .par_iter()
149            .enumerate()
150            .map(|(index, resource)| {
151                #[cfg(target_os = "macos")]
152                let result = objc2::rc::autoreleasepool(|_| {
153                    analyze_resource(resource, index, &inventory.root, &out, &options)
154                });
155                #[cfg(not(target_os = "macos"))]
156                let result = analyze_resource(resource, index, &inventory.root, &out, &options);
157                let done = complete.fetch_add(1, Ordering::Relaxed) + 1;
158                progress(done, inventory.assets.len());
159                result
160            })
161            .collect::<Vec<_>>()
162    });
163    let mut status_counts = BTreeMap::new();
164    let mut savings = 0;
165    for resource in &resources {
166        *status_counts.entry(resource.status.clone()).or_insert(0) += 1;
167        if let Some(index) = resource.smallest_candidate {
168            savings += resource.candidates[index].savings_bytes;
169        }
170    }
171    let report = AnalysisReport {
172        schema_version: 1,
173        root: inventory.root.clone(),
174        backend: if image_backend::image_backend_available() {
175            "Apple ImageIO + CoreGraphics sRGB float comparison"
176        } else {
177            "ImageIO unavailable on this platform"
178        }
179        .into(),
180        options,
181        inventory,
182        resources,
183        status_counts,
184        potential_source_bytes_saved: savings,
185    };
186    write_new(
187        &out.join("analysis.json"),
188        &serde_json::to_vec_pretty(&report)?,
189    )?;
190    write_new(
191        &out.join("report.html"),
192        crate::report::render_html(&report)?.as_bytes(),
193    )?;
194    Ok(report)
195}
196
197fn analyze_resource(
198    resource: &Resource,
199    index: usize,
200    root: &Path,
201    out: &Path,
202    options: &AnalysisOptions,
203) -> ResourceAnalysis {
204    let mut result = ResourceAnalysis {
205        resource: resource.clone(),
206        sha256: None,
207        image: None,
208        status: "inventory_only".into(),
209        issues: vec![],
210        candidates: vec![],
211        smallest_candidate: None,
212        original_preview: None,
213        original_artifact: None,
214    };
215    if resource.kind != "image" {
216        result.issues.push(format!(
217            "{}_optimization_backend_not_implemented",
218            resource.kind
219        ));
220        return result;
221    }
222    let attempt = (|| -> Result<()> {
223        let path = contained_file(root, &resource.path)?;
224        let original = bounded_read(&path)?;
225        result.sha256 = Some(hash(&original));
226        let decoded = image_backend::decode(&original)?;
227        result.image = Some(decoded.info.clone());
228        result.status = "inspected".into();
229        if decoded.info.frames != 1 {
230            result.issues.push("multiple_frames_not_transcoded".into());
231            return Ok(());
232        }
233        if let Some(reason) = &resource.conversion_exclusion {
234            result.issues.push(reason.clone());
235            return Ok(());
236        }
237        if options.probe_only {
238            return Ok(());
239        }
240        if original.len() < options.min_input_bytes as usize {
241            result.issues.push("below_explicit_input_threshold".into());
242            return Ok(());
243        }
244        let targets = if decoded.info.has_transparent_pixels {
245            vec!["heic"]
246        } else {
247            vec!["jpeg", "heic"]
248        };
249        let mut trials: Vec<(&str, Option<u8>)> = targets
250            .iter()
251            .flat_map(|format| {
252                options
253                    .qualities
254                    .iter()
255                    .map(move |quality| (*format, Some(*quality)))
256            })
257            .collect();
258        if resource.format == "png" {
259            trials.insert(0, ("png", None));
260        }
261        for (format, quality) in trials {
262            let mut trial = ImageCandidate {
263                format: format.into(),
264                quality,
265                lossy: quality.is_some(),
266                bytes: 0,
267                savings_bytes: 0,
268                valid: false,
269                rejection: None,
270                difference: None,
271                artifact: None,
272                preview: None,
273            };
274            let encoded = match quality {
275                Some(quality) => image_backend::encode(&original, format, quality),
276                None => optimizer::optimize(&original, &Policy::default()),
277            };
278            let checked = (|| -> Result<()> {
279                let bytes = encoded?;
280                trial.bytes = bytes.len() as u64;
281                trial.savings_bytes = (original.len() as u64).saturating_sub(trial.bytes);
282                let candidate = image_backend::decode(&bytes)?;
283                let difference = image_backend::compare(&decoded, &candidate)?;
284                trial.difference = Some(difference.clone());
285                ensure!(
286                    difference.max_alpha_error <= options.max_alpha_error,
287                    "alpha_error_exceeds_policy"
288                );
289                ensure!(
290                    decoded.info.has_transparent_pixels == candidate.info.has_transparent_pixels,
291                    "transparency_presence_changed"
292                );
293                trial.valid = true;
294                if trial.savings_bytes >= options.min_savings_bytes
295                    && trial.bytes < original.len() as u64
296                {
297                    let stem = format!("{index}-{format}-{}", quality.unwrap_or(0));
298                    let artifact = PathBuf::from(format!("candidates/{stem}.{format}"));
299                    let preview = PathBuf::from(format!("previews/{stem}.png"));
300                    write_new(&out.join(&artifact), &bytes)?;
301                    write_new(&out.join(&preview), &image_backend::preview(&candidate)?)?;
302                    trial.artifact = Some(artifact);
303                    trial.preview = Some(preview);
304                }
305                Ok(())
306            })();
307            if let Err(error) = checked {
308                trial.valid = false;
309                trial.rejection = Some(format!("{error:#}"));
310            }
311            result.candidates.push(trial);
312        }
313        result.smallest_candidate = result
314            .candidates
315            .iter()
316            .enumerate()
317            .filter(|(_, candidate)| candidate.valid && candidate.artifact.is_some())
318            .min_by_key(|(_, candidate)| candidate.bytes)
319            .map(|(index, _)| index);
320        if result.smallest_candidate.is_some() {
321            let preview = PathBuf::from(format!("previews/{index}-original.png"));
322            write_new(&out.join(&preview), &image_backend::preview(&decoded)?)?;
323            result.original_preview = Some(preview);
324            let artifact = PathBuf::from(format!("originals/{index}.{}", resource.format));
325            write_new(&out.join(&artifact), &original)?;
326            result.original_artifact = Some(artifact);
327            result.status = "candidates_available".into();
328        }
329        // Report a moving source instead of presenting stale candidate estimates.
330        ensure!(
331            hash(&bounded_read(&path)?)
332                == *result.sha256.as_ref().context("source hash missing")?,
333            "source_changed_during_analysis"
334        );
335        Ok(())
336    })();
337    if let Err(error) = attempt {
338        result.status = "failed".into();
339        result.issues.push(format!("{error:#}"));
340        result.smallest_candidate = None;
341    }
342    result
343}