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.

Thumbnail probe: ways to keep thumbnails on disk

gelenmedyab97ccea23b43
diff --git a/Cargo.lock b/Cargo.lock
index 874e865c..3adfc31b 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -6409,6 +6409,7 @@ dependencies = [
"velopack",
"windows 0.62.2",
"windows-registry 0.6.1",
+ "zstd",
]
[[package]]
diff --git a/crates/rf-app/Cargo.toml b/crates/rf-app/Cargo.toml
index 1f1127dc..824b38d2 100644
--- a/crates/rf-app/Cargo.toml
+++ b/crates/rf-app/Cargo.toml
@@ -59,6 +59,7 @@ unicode-segmentation = "1"
png = "0.18"
flate2.workspace = true
memchr = "2"
+zstd.workspace = true
[build-dependencies]
embed-resource = "3"
diff --git a/crates/rf-app/examples/thumb_probe/main.rs b/crates/rf-app/examples/thumb_probe/main.rs
index 54f7c771..8ce0e2ab 100644
--- a/crates/rf-app/examples/thumb_probe/main.rs
+++ b/crates/rf-app/examples/thumb_probe/main.rs
@@ -15,12 +15,15 @@
//! - `dc-1`, `dc-16`, `dc-skip`: a JPEG from its blocks' averages alone (dcjpeg.rs), on 1 or 16 threads, and skipping the
//! restart intervals the thumbnail needs no row from.
//! Env: PROBE_ONLY=jpeg|png|raw|video|psd, PROBE_MIN_MB, PROBE_BATCH=<workers> (a whole folder, Windows against ours),
-//! PROBE_PHASES (where the time goes), PROBE_DEBUG (+ PROBE_SAVE=<dir>: the DC thumbnail beside Windows' codec's).
+//! PROBE_CACHE (ways to keep made thumbnails on disk: size, store and read time), PROBE_PHASES (where the time goes), PROBE_DEBUG (+ PROBE_SAVE=<dir>: the DC thumbnail beside Windows' codec's).
//! Windows' thumbnail cache is only read and added to (as by any Explorer visit), never deleted.
#[cfg(windows)]
mod dcjpeg;
+#[cfg(windows)]
+use image::ImageEncoder;
+
#[cfg(windows)]
fn main() {
let args: Vec<String> = std::env::args().skip(1).collect();
@@ -56,6 +59,10 @@ fn probe_folder(dir: &std::path::Path, size: u32, per: usize) {
.collect();
files.sort();
files.truncate(per);
+ if std::env::var("PROBE_CACHE").is_ok() {
+ cache_bench(&files, size);
+ return;
+ }
let mut times: Vec<Vec<f64>> = vec![Vec::new(); KINDS.len()];
let mut hits = 0usize;
let mut fails = vec![0usize; KINDS.len()];
@@ -568,6 +575,110 @@ fn inflate_only(p: &std::path::Path) -> bool {
d.decompress_vec(&z, &mut out, flate2::FlushDecompress::Finish).is_ok() && out.len() == raw
}
+/// How the made thumbnails could be kept on disk: for each way of storing them, the bytes each takes, and the time to
+/// store and to read back one (to BGRA pixels again).
+#[cfg(windows)]
+fn cache_bench(files: &[std::path::PathBuf], size: u32) {
+ use std::time::Instant;
+ // the thumbnails: Windows' codec at the size, BGRA (straight alpha)
+ let thumbs: Vec<(u32, u32, Vec<u8>, bool)> = files
+ .iter()
+ .filter(|p| matches!(kind_of(p), Some(Kind::Jpeg | Kind::Png | Kind::Raw)))
+ .filter_map(|p| {
+ let (w, h) = wic_size(p)?;
+ let k = size as f64 / w.max(h) as f64;
+ let (tw, th) = (((w as f64 * k).round() as u32).max(1), ((h as f64 * k).round() as u32).max(1));
+ let px = wic_exact(p, tw, th)?;
+ let alpha = px.chunks_exact(4).any(|b| b[3] != 255);
+ Some((tw, th, px, alpha))
+ })
+ .collect();
+ let n = thumbs.len();
+ let alpha_n = thumbs.iter().filter(|t| t.3).count();
+ println!("\n{} thumbnails of {size}px ({alpha_n} with transparency)", n);
+ println!(" {:<16} {:>9} {:>10} {:>10}", "kept as", "avg KB", "store ms", "read ms");
+ type Enc = fn(u32, u32, &[u8]) -> Vec<u8>;
+ type Dec = fn(&[u8], u32, u32) -> Option<Vec<u8>>;
+ let ways: Vec<(&str, Enc, Dec)> = vec![
+ ("raw BGRA", |_, _, px| px.to_vec(), |b, _, _| Some(b.to_vec())),
+ ("raw + zstd 1", |_, _, px| zstd::bulk::compress(px, 1).unwrap(), |b, w, h| zstd::bulk::decompress(b, (w * h * 4) as usize).ok()),
+ ("raw + zstd 3", |_, _, px| zstd::bulk::compress(px, 3).unwrap(), |b, w, h| zstd::bulk::decompress(b, (w * h * 4) as usize).ok()),
+ ("QOI", |w, h, px| {
+ let mut out = Vec::new();
+ image::codecs::qoi::QoiEncoder::new(&mut out).write_image(&to_rgba(px), w, h, image::ExtendedColorType::Rgba8).unwrap();
+ out
+ }, |b, _, _| image::load_from_memory_with_format(b, image::ImageFormat::Qoi).ok().map(|i| i.to_rgba8().into_raw())),
+ ("PNG fast", |w, h, px| {
+ let mut out = Vec::new();
+ image::codecs::png::PngEncoder::new_with_quality(&mut out, image::codecs::png::CompressionType::Fast, image::codecs::png::FilterType::Adaptive)
+ .write_image(&to_rgba(px), w, h, image::ExtendedColorType::Rgba8)
+ .unwrap();
+ out
+ }, |b, _, _| image::load_from_memory_with_format(b, image::ImageFormat::Png).ok().map(|i| i.to_rgba8().into_raw())),
+ ("WebP lossless", |w, h, px| {
+ let mut out = Vec::new();
+ image::codecs::webp::WebPEncoder::new_lossless(&mut out).write_image(&to_rgba(px), w, h, image::ExtendedColorType::Rgba8).unwrap();
+ out
+ }, |b, _, _| image::load_from_memory_with_format(b, image::ImageFormat::WebP).ok().map(|i| i.to_rgba8().into_raw())),
+ ("JPEG q85", |w, h, px| {
+ let mut out = Vec::new();
+ image::codecs::jpeg::JpegEncoder::new_with_quality(&mut out, 85).write_image(&to_rgb(px), w, h, image::ExtendedColorType::Rgb8).unwrap();
+ out
+ }, |b, _, _| image::load_from_memory_with_format(b, image::ImageFormat::Jpeg).ok().map(|i| i.to_rgba8().into_raw())),
+ ("JPEG q75", |w, h, px| {
+ let mut out = Vec::new();
+ image::codecs::jpeg::JpegEncoder::new_with_quality(&mut out, 75).write_image(&to_rgb(px), w, h, image::ExtendedColorType::Rgb8).unwrap();
+ out
+ }, |b, _, _| image::load_from_memory_with_format(b, image::ImageFormat::Jpeg).ok().map(|i| i.to_rgba8().into_raw())),
+ ];
+ for (name, enc, dec) in ways {
+ let mut bytes = 0usize;
+ let mut kept = Vec::new();
+ let t = Instant::now();
+ for (w, h, px, _) in &thumbs {
+ let e = enc(*w, *h, px);
+ bytes += e.len();
+ kept.push(e);
+ }
+ let store = t.elapsed().as_secs_f64() * 1000.0 / n as f64;
+ let t = Instant::now();
+ let mut ok = 0;
+ for ((w, h, _, _), e) in thumbs.iter().zip(&kept) {
+ ok += dec(e, *w, *h).is_some() as usize;
+ }
+ let read = t.elapsed().as_secs_f64() * 1000.0 / n as f64;
+ println!(" {:<16} {:>9.1} {:>10.2} {:>10.3}{}", name, bytes as f64 / n as f64 / 1024.0, store, read, if ok == n { "" } else { " (failed some)" });
+ }
+}
+
+#[cfg(windows)]
+fn to_rgba(px: &[u8]) -> Vec<u8> {
+ px.chunks_exact(4).flat_map(|b| [b[2], b[1], b[0], b[3]]).collect()
+}
+
+#[cfg(windows)]
+fn to_rgb(px: &[u8]) -> Vec<u8> {
+ px.chunks_exact(4).flat_map(|b| [b[2], b[1], b[0]]).collect()
+}
+
+/// A picture's size as Windows' codec reads it.
+#[cfg(windows)]
+fn wic_size(p: &std::path::Path) -> Option<(u32, u32)> {
+ use windows::Win32::Foundation::GENERIC_READ;
+ use windows::Win32::Graphics::Imaging::*;
+ use windows::Win32::System::Com::{CLSCTX_INPROC_SERVER, CoCreateInstance};
+ use windows::core::PCWSTR;
+ let wz = p.as_os_str().encode_wide_z();
+ unsafe {
+ let f: IWICImagingFactory = CoCreateInstance(&CLSID_WICImagingFactory, None, CLSCTX_INPROC_SERVER).ok()?;
+ let dec = f.CreateDecoderFromFilename(PCWSTR(wz.as_ptr()), None, GENERIC_READ, WICDecodeMetadataCacheOnDemand).ok()?;
+ let frame = dec.GetFrame(0).ok()?;
+ let (mut w, mut h) = (0u32, 0u32);
+ frame.GetSize(&mut w, &mut h).ok()?;
+ Some((w, h))
+ }
+}
+
/// rustdecoder's first frame made a thumbnail on the GPU (converted, averaged down by mipmaps), only that read back. The
/// converter (its shaders compiled once) is kept per thread, as the app would keep one.
#[cfg(windows)]