diff --git a/Cargo.lock b/Cargo.lock index 0daa69e0..3802eb9e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1009,6 +1009,15 @@ dependencies = [ "web-sys", ] +[[package]] +name = "craft-png" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=c42c34bfa127b095fb0a8d5ccaba0c168c254062#c42c34bfa127b095fb0a8d5ccaba0c168c254062" +dependencies = [ + "png", + "thiserror 2.0.21", +] + [[package]] name = "craft-quit" version = "0.1.0" @@ -3002,6 +3011,7 @@ dependencies = [ "craft-dialogs", "craft-keymap", "craft-platform", + "craft-png", "craft-quit", "craft-ui", "craft-update", diff --git a/Cargo.toml b/Cargo.toml index d01020b4..39486918 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -109,6 +109,7 @@ craft-window = { git = "https://github.com/storytold/craft-libs", rev = "453b0da craft-update = { git = "https://github.com/storytold/craft-libs", rev = "453b0da782ccef39be9a6dac8d27c80b553ef1cc", features = ["egui"] } # Atomic persistence, at the suite's current revision craft-atomic = { git = "https://github.com/storytold/craft-libs", rev = "453b0da782ccef39be9a6dac8d27c80b553ef1cc" } +craft-png = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "c42c34bfa127b095fb0a8d5ccaba0c168c254062" } # Parse the optional shared font catalog at build time; the font repository is not a dependency craft-fonts-build = { git = "https://github.com/storytold/craft-libs", rev = "453b0da782ccef39be9a6dac8d27c80b553ef1cc" } # Float image planes and texture sampling for node cooking: storytold/craft-libs, MIT OR Apache-2.0 diff --git a/crates/ui-egui/Cargo.toml b/crates/ui-egui/Cargo.toml index 9750f558..73080dfc 100644 --- a/crates/ui-egui/Cargo.toml +++ b/crates/ui-egui/Cargo.toml @@ -25,6 +25,7 @@ zip = { workspace = true } [target.'cfg(not(target_arch = "wasm32"))'.dependencies] craft-atomic = { workspace = true } +craft-png = { workspace = true } sha2 = { workspace = true } sys-locale = "0.3" craft-update = { workspace = true } diff --git a/crates/ui-egui/src/preview_disk_cache.rs b/crates/ui-egui/src/preview_disk_cache.rs index e0830aa8..7778c7b7 100644 --- a/crates/ui-egui/src/preview_disk_cache.rs +++ b/crates/ui-egui/src/preview_disk_cache.rs @@ -7,7 +7,7 @@ use std::sync::OnceLock; use std::sync::atomic::{AtomicBool, Ordering}; use std::time::{Duration, SystemTime}; -use image::{ImageDecoder, ImageEncoder}; +use image::ImageDecoder; use sha2::{Digest, Sha256}; use super::preview_cache_settings::{PreviewCacheLimits, PreviewCacheSettings, PreviewCacheState}; @@ -128,7 +128,7 @@ impl PreviewDiskCache { } let mut bytes = Vec::new(); - image::codecs::png::PngEncoder::new(&mut bytes).write_image(pixels, side, side, image::ExtendedColorType::Rgba8).map_err(io::Error::other)?; + craft_png::write_8bit(&mut bytes, side, side, craft_png::Layout::Rgba, pixels).map_err(io::Error::other)?; if bytes.len() as u64 > MAX_FILE_BYTES { return Ok(()); } diff --git a/crates/ui-egui/src/preview_disk_cache.test.rs b/crates/ui-egui/src/preview_disk_cache.test.rs index 269906ee..fbfbc5ef 100644 --- a/crates/ui-egui/src/preview_disk_cache.test.rs +++ b/crates/ui-egui/src/preview_disk_cache.test.rs @@ -192,6 +192,28 @@ fn cache_budget_evicts_old_files_and_preserves_unrelated_files() { assert!(!oversized.exists()); } +#[test] +fn encoded_preview_keeps_legacy_bytes_and_budget_boundary() { + let _serial = TESTS.lock().unwrap_or_else(std::sync::PoisonError::into_inner); + let root = tempfile::tempdir().unwrap(); + let cache = cache(root.path()); + let side = MAX_SIDE; + let pixels: Vec = (0..side * side * 4).map(|i| (i * 17 + i / 31) as u8).collect(); + let mut legacy = Vec::new(); + image::ImageEncoder::write_image(image::codecs::png::PngEncoder::new(&mut legacy), &pixels, side, side, image::ExtendedColorType::Rgba8).unwrap(); + let bytes = legacy.len() as u64; + let path = cache.path([1; 32], side); + + cache.settings.update(PreviewCacheLimits { budget_bytes: bytes - 1, max_entries: 1 }, false); + cache.write(&path, side, &pixels).unwrap(); + assert!(!path.exists()); + + cache.settings.update(PreviewCacheLimits { budget_bytes: bytes, max_entries: 1 }, false); + cache.write(&path, side, &pixels).unwrap(); + assert_eq!(fs::read(&path).unwrap(), legacy); + assert_eq!(cache.read(&path, side), Some(pixels)); +} + #[test] fn unavailable_or_busy_cache_still_returns_rendered_pixels() { let _serial = TESTS.lock().unwrap_or_else(std::sync::PoisonError::into_inner);