Skip to content

A fast file manager for Windows and macOS that never freezes: tabs, dual panes, archives as folders, Quick Look-style previews. A File Explorer and Finder alternative written in Rust.

3.9 KB
Raw Blame
//! The thumbnail from a camera file's own preview (`rf_win::embedded_thumb`) against the shell's, on the files of a real folder:
//! `cargo run --release -p rf-win --example embedded_probe -- <size px> <files> <folder> [folder to save pictures in]`
//!
//! For each camera JPEG or RAW file: the time of each way and the size of the picture each gives, and whether the two are the
//! same way up (their proportions agree). With a folder to save in, the first few pairs are written side by side as PNG files
//! (ours at the left, the shell's at the right) for a look at colours and direction. Windows' thumbnail cache is only read and
//! added to, as by any visit of the folder.

use std::path::{Path, PathBuf};
use std::time::Instant;

use rf_win::thumbs::{Bgra, Kind, get_image};

fn rgba(b: &Bgra) -> image::RgbaImage {
    let mut px = b.data.clone();
    for p in px.chunks_exact_mut(4) {
        p.swap(0, 2);
    }
    image::RgbaImage::from_raw(b.width, b.height, px).unwrap()
}

fn main() {
    let args: Vec<String> = std::env::args().skip(1).collect();
    if args.len() < 3 {
        eprintln!("usage: embedded_probe <size px> <files> <folder> [save folder]");
        std::process::exit(2);
    }
    let size: u32 = args[0].parse().expect("size");
    let count: usize = args[1].parse().expect("files");
    let save: Option<PathBuf> = args.get(3).map(PathBuf::from);
    rf_win::shell::ensure_com();
    // (the shell takes no forward slashes in a path)
    let folder = args[2].replace('/', "\\");
    let mut files: Vec<PathBuf> = std::fs::read_dir(&folder).expect("folder").flatten().map(|e| e.path()).filter(|p| rf_win::embedded_thumb::handles(p)).collect();
    files.sort();
    files.truncate(count);
    let (mut ours_ms, mut shell_ms, mut made, mut none, mut turned) = (0.0f64, 0.0f64, 0usize, 0usize, 0usize);
    for (i, p) in files.iter().enumerate() {
        let name = p.file_name().unwrap().to_string_lossy().into_owned();
        let t = Instant::now();
        let ours = rf_win::embedded_thumb::thumbnail(p, size);
        let o_ms = t.elapsed().as_secs_f64() * 1000.0;
        // the shell's own, with our way switched off for the call
        rf_win::embedded_thumb::set_enabled(false);
        let t = Instant::now();
        let shell = get_image(Path::new(p), size, Kind::ThumbnailOnly).ok();
        let s_ms = t.elapsed().as_secs_f64() * 1000.0;
        rf_win::embedded_thumb::set_enabled(true);
        let dims = |b: &Option<Bgra>| b.as_ref().map_or("none".to_string(), |b| format!("{}x{}", b.width, b.height));
        let same_way = match (&ours, &shell) {
            (Some(a), Some(b)) => (a.width >= a.height) == (b.width >= b.height),
            _ => true,
        };
        println!("{name:<28} ours {o_ms:6.1} ms {:<9} shell {s_ms:7.1} ms {:<9}{}", dims(&ours), dims(&shell), if same_way { "" } else { "  TURNED DIFFERENTLY" });
        match &ours {
            Some(_) => {
                made += 1;
                ours_ms += o_ms;
                shell_ms += s_ms;
            }
            None => none += 1,
        }
        if !same_way {
            turned += 1;
        }
        if let (Some(dir), Some(a), Some(b), true) = (&save, &ours, &shell, i < 6) {
            let (a, b) = (rgba(a), rgba(b));
            let mut both = image::RgbaImage::from_pixel(a.width() + b.width() + 8, a.height().max(b.height()), image::Rgba([40, 40, 40, 255]));
            image::imageops::overlay(&mut both, &a, 0, 0);
            image::imageops::overlay(&mut both, &b, a.width() as i64 + 8, 0);
            let _ = std::fs::create_dir_all(dir);
            let _ = both.save(dir.join(format!("pair-{i}.png")));
        }
    }
    println!("\n{} files: {made} from their own preview, {none} left to the shell, {turned} turned differently", files.len());
    if made > 0 {
        println!("where ours was used: ours {:.1} ms a file, the shell {:.1} ms a file ({:.0} times)", ours_ms / made as f64, shell_ms / made as f64, shell_ms / ours_ms.max(0.001));
    }
}