Files
2026-11-29 17:43:31 +01:00

201 lines
8.7 KiB
Rust

#[cfg(feature = "denoise")]
use super::builtin::{builtin_marker, forget_builtin_card};
use super::gpu::GPU_SETUP_MARKER;
use super::model::RunOn;
use super::{DenoiseModelPin, DenoiserManifest, Deserialize, Duration, Path, PathBuf, Serialize, Value};
/// Extension of a product file.
pub const EXT: &str = "lcdn";
/// Where the products of a library are kept, inside the library's folder (safe to delete, like `thumbs/`).
pub const DIR: &str = "denoise";
/// What the cache may take up unless the user says otherwise (GB). A product is about as large as the raw file's
/// decoded samples, compressed: ~100 MB for 24 megapixels.
pub const DEFAULT_CACHE_GB: u32 = 20;
/// How long a look at the model settings is trusted.
pub(super) const SETTINGS_TTL: Duration = Duration::from_secs(2);
// ---------------------------------------------------------------------------------------------------------------------
// settings and installed models
/// `settings.json` in the models folder.
#[derive(Clone, Default, Serialize, Deserialize)]
#[serde(default, rename_all = "camelCase")]
pub(crate) struct Settings {
/// Default for new assignments. A missing model or no choice uses the built-in denoiser.
pub model: Option<String>,
/// The old global choice, preserved for libraries opened after another library changes the default.
#[serde(skip_serializing_if = "Option::is_none")]
pub legacy_model: Option<DenoiseModelPin>,
/// Photos with a Denoise amount above 0 that are being looked at (active, selected) get their picture made without
/// being asked (`None`: yes).
pub auto: Option<bool>,
/// Cache size limit (GB; `None`: [`DEFAULT_CACHE_GB`]).
pub cache_gb: Option<u32>,
/// Most tiles run at once (`None`: by how hard the pump says to work).
pub threads: Option<u32>,
/// Where the model runs: `auto`, `gpu` or `cpu` (`None` or anything else: `auto`; see [`RunOn`]).
pub run_on: Option<String>,
/// What came before `runOn`: `false` was the processor only.
pub gpu: Option<bool>,
}
impl Settings {
pub fn auto(&self) -> bool {
self.auto.unwrap_or(true)
}
pub fn run_on(&self) -> RunOn {
match self.run_on.as_deref().and_then(RunOn::parse) {
Some(r) => r,
None if self.gpu == Some(false) => RunOn::Cpu,
None => RunOn::Auto,
}
}
pub fn cache_bytes(&self) -> u64 {
u64::from(self.cache_gb.unwrap_or(DEFAULT_CACHE_GB).clamp(1, 10_000)) << 30
}
}
pub(crate) fn read_capped(path: &Path, max: usize) -> Option<Vec<u8>> {
use std::io::Read;
let mut buf = Vec::new();
std::fs::File::open(path).ok()?.take(max as u64 + 1).read_to_end(&mut buf).ok()?;
(buf.len() <= max).then_some(buf)
}
fn read_settings_checked(dir: &Path) -> Result<Settings, String> {
use std::io::{ErrorKind, Read};
const MAX_BYTES: usize = 4096;
let path = dir.join("settings.json");
let file = match std::fs::File::open(&path) {
Err(e) if e.kind() == ErrorKind::NotFound => return Ok(Settings::default()),
other => other.map_err(|e| format!("could not open {}: {e}", path.display()))?,
};
let mut bytes = Vec::new();
file.take(MAX_BYTES as u64 + 1).read_to_end(&mut bytes).map_err(|e| format!("could not read {}: {e}", path.display()))?;
if bytes.len() > MAX_BYTES {
return Err(format!("{} exceeds the denoise settings limit of {MAX_BYTES} bytes", path.display()));
}
serde_json::from_slice(&bytes).map_err(|e| format!("could not read {}: {e}", path.display()))
}
pub(crate) fn read_settings(dir: &Path) -> Settings {
read_settings_checked(dir).unwrap_or_default()
}
pub(super) fn builtin_pin() -> DenoiseModelPin {
let m = lightcraft_denoise::builtin::manifest();
DenoiseModelPin { id: m.id, version: m.version, sha256: None }
}
pub(super) fn configured_pin(dir: &Path, settings: &Settings) -> Result<DenoiseModelPin, String> {
if let Some(id) = &settings.model
&& let Some(model) = installed_models(dir).into_iter().find(|model| model.manifest.id == *id)
{
return model.pin(None);
}
// The pre-pin engine also used its built-in denoiser if the chosen model had gone away.
Ok(builtin_pin())
}
/// Capture the pre-feature model before migrating a library or changing the installed models/default.
pub(crate) fn ensure_legacy_settings(dir: &Path) -> Result<Settings, String> {
let mut settings = read_settings_checked(dir).map_err(|e| {
format!(
"{e}. Check read permissions or restore this file from a backup, then retry. \
To reset denoise settings, rename only settings.json and retry. \
Older edits without a saved model will then use the built-in denoiser."
)
})?;
if settings.legacy_model.is_none() {
settings.legacy_model = Some(configured_pin(dir, &settings)?);
write_settings(dir, &settings)?;
}
Ok(settings)
}
pub(crate) fn capture_legacy_pin(dir: Option<&Path>) -> Result<DenoiseModelPin, String> {
let Some(dir) = dir else { return Ok(builtin_pin()) };
ensure_legacy_settings(dir)?.legacy_model.ok_or_else(|| "could not preserve the legacy denoise model".into())
}
/// Write a file next to its final name and rename it into place.
pub(crate) fn write_atomic(path: &Path, bytes: &[u8]) -> Result<(), String> {
let part = path.with_extension("part");
std::fs::write(&part, bytes).map_err(|e| format!("could not write {}: {e}", path.display()))?;
std::fs::rename(&part, path).map_err(|e| {
let _ = std::fs::remove_file(&part);
format!("could not write {}: {e}", path.display())
})
}
pub(crate) fn write_settings(dir: &Path, st: &Settings) -> Result<(), String> {
std::fs::create_dir_all(dir).map_err(|e| format!("could not create the denoise models folder: {e}"))?;
write_atomic(&dir.join("settings.json"), &serde_json::to_vec_pretty(st).map_err(|e| e.to_string())?)
}
/// An installed model folder: `<id>/model.onnx`, `denoise-model.json`, `installed.json`.
pub(crate) struct Installed {
pub manifest: DenoiserManifest,
pub onnx: PathBuf,
/// What was recorded when it was installed (terms accepted, hash of the file, self-test).
pub accepted: Value,
}
impl Installed {
fn recorded_hash(&self) -> Option<&str> {
self.manifest.sha256.as_deref().or_else(|| self.accepted.get("sha256").and_then(Value::as_str))
}
/// The installer records the hash even for custom manifests that do not name one.
/// A manually provisioned folder may only have the weights and their manifest.
pub(super) fn pin(&self, previous_hash: Option<&str>) -> Result<DenoiseModelPin, String> {
let sha = match self.recorded_hash().or(previous_hash) {
Some(hash) => hash.to_owned(),
None => lightcraft_denoise::hash::sha256_file(&self.onnx).map_err(|e| format!("{}: {e}", self.onnx.display()))?,
};
Ok(DenoiseModelPin { id: self.manifest.id.clone(), version: self.manifest.version.clone(), sha256: Some(sha) })
}
}
/// The models in the folder. A folder that is not a valid model (wrong name, no model file, a manifest that does not
/// validate or does not match its folder) is ignored, never trusted.
pub(crate) fn installed_models(dir: &Path) -> Vec<Installed> {
let Ok(entries) = std::fs::read_dir(dir) else { return Vec::new() };
let mut out = Vec::new();
for e in entries.flatten() {
let p = e.path();
let Some(name) = p.file_name().and_then(|n| n.to_str()).map(str::to_string) else { continue };
let onnx = p.join("model.onnx");
if name == lightcraft_denoise::builtin::ID || !lightcraft_denoise::licence::valid_id(&name) || !p.is_dir() || !onnx.is_file() {
continue;
}
let Some(m) = read_capped(&p.join("denoise-model.json"), lightcraft_denoise::manifest::MAX_MANIFEST_BYTES)
.and_then(|b| lightcraft_denoise::manifest::parse(&b).ok())
else {
continue;
};
if m.id != name {
continue;
}
let accepted = read_capped(&p.join("installed.json"), 8192).and_then(|b| serde_json::from_slice(&b).ok()).unwrap_or(Value::Null);
out.push(Installed { manifest: m, onnx, accepted });
}
out.sort_by(|a, b| a.manifest.id.cmp(&b.manifest.id));
out
}
/// Forget that setting the graphics card up for a model closed LightCraft before (the markers [`set_up_card`] leaves):
/// the user chose where denoise runs, so the card may be tried again.
pub(crate) fn forget_gpu_set_ups(dir: &Path) {
for i in installed_models(dir) {
let _ = std::fs::remove_file(i.onnx.with_file_name(GPU_SETUP_MARKER));
}
#[cfg(feature = "denoise")]
forget_builtin_card(&builtin_marker(&dir.join(lightcraft_denoise::builtin::ID)));
}