Merge upstream/main into feat/issue-2270-web-host-bridge

Conflicts: web.rs (upstream moved drop import and the benchmark into ui(), added the page title and
write_new; the bridge keeps run_host in update and reports dirty after save), docs/web.md (both
bullets kept), docs/whats-new.md (the entry moves to 2026-10-26).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01697nraCtyEpry7BiGLJjqv
This commit is contained in:
francoisgnandClaude Opus 5.5 committed 2026-10-26 04:32:49 +00:00
commit 6655b70ff1
430 files changed
+42344 -5007

No files matched your search

+69
View File
@@ -0,0 +1,69 @@
<!--
Title: start with the checklist id or the issue, as the commits do:
`LR-IMP-FORMATS: folder import finds Sony SR2 and SRF files`, or `Issue #123: …` when there is no checklist row.
One change per pull request. Unrelated fixes and clean-ups (clippy, formatting, a second feature) go in their own:
small ones land fast, bundles get sent back to be split. The rules are in AGENTS.md.
Delete the sections and lines that don't apply, and these comments. A translation or a one-line fix needs only the
first three sections.
-->
Fixes #
<!-- `Part of #123` when this does only part of the issue. Built on another open pull request? Say so here
(`Stacked on #123`) and name the commits that are this one's own. -->
## Problem
<!-- What is wrong or missing, as a person using the app meets it. For a bug: the cause. -->
## Change
<!-- What you changed and where. -->
**Not in this PR:** <!-- what you left out on purpose, and known limits -->
## How it was checked
- [ ] `cargo xtask ci` passes on this branch, rebased on current `main`
- [ ] A test that fails without this change (for a crash fix: a regression test that panicked before it)
- [ ] User-visible change: ran the app and looked at the result
<!-- Numbers, when it changes rendering, decoding or speed: before and after, on more than one photo or camera
(ΔE against the reference, `cargo xtask bench`, `/usr/bin/time`, memory). Name the files or corpus entries used. -->
## Screenshots
<!-- For a visible change: before and after, labelled. Never a screenshot of an Adobe app. -->
## Merging
<!-- Pull requests land in batches, merged together. This is what the person merging can't see in the diff. -->
- **Overlap:** <!-- open pull requests that fix the same issue or change the same functions, by number, and how
this one differs. Search the open pull requests for the issue number and checklist id first. "none found" is fine. -->
- **Existing edits and libraries:** <!-- does any photo already edited render differently, or any saved setting,
preset, sort order or default change? Then it goes behind a new process version (docs/process-versions.md) or is a
setting that is off by default, with a line in docs/whats-new.md. "unchanged" is fine. -->
- **Shared numbers taken:** <!-- catalog format `VERSION` (crates/catalog/src/journal.rs), `RENDER_CACHE_VERSION`,
`LOOK_VERSION`, process version: the number this takes. If another pull request lands with it first, take one
past main's when you rebase. -->
- **Where it came from:** <!-- for a decoder, camera or lens data, or fitted constants: the public spec, your own
measurements, or observed output, and the files measured. Never GPL / LGPL code (dcraw, LibRaw, rawspeed, darktable,
RawTherapee, lensfun …), text derived from it, or Adobe profiles. -->
- **New dependencies, `unsafe`, build scripts, network access, spawned processes, deleted files, CI or `xtask` gate
changes:** <!-- each with its reason, or "none". Dependencies are pure Rust and use `workspace = true`; actions are
pinned by SHA; downloads are checked against a sha256; nothing leaves the machine unless the person asked. -->
## Checklist
- [ ] No new panic path in non-test code: no `unwrap` / `expect` / `panic!`, no indexing or unchecked arithmetic on a
number from a file, a person or an agent (`get`, `checked_mul`, NaN and char boundaries handled)
- [ ] New text on screen goes through `tr()` and has its key in `crates/ui-egui/locales/*.json`; a new shortcut is
free in `docs/library-shortcuts.md`
- [ ] The feature's rows in `docs/parity-checklist.md` and its entry in `docs/gaps.md` are updated, honestly (🟡 when
it is partial), in the same commit
- [ ] New code has a caller: no command, module or setting that nothing reaches yet
- [ ] Every new image, icon or other asset has its entry in `assets/ATTRIBUTION.md`; no font or media files added
- [ ] Ready to merge (otherwise open it as a draft)
+32
View File
@@ -0,0 +1,32 @@
name: CI
# Quality gates for every push to main and every pull request (`cargo xtask ci`), through the
# suite's shared gate workflow in craft-common-actions.
on:
push:
branches: [main]
pull_request:
workflow_dispatch:
permissions:
contents: read
# A newer push to a PR branch cancels its older run; every push to main runs to the end.
concurrency:
group: ci-${{ github.event_name == 'pull_request' && github.ref || github.run_id }}
cancel-in-progress: ${{ github.event_name == 'pull_request' }}
jobs:
gate:
# Tracks @main so suite-wide CI fixes reach every app without a PR in each repository.
uses: storytold/craft-common-actions/.github/workflows/gate.yml@main
with:
# Same craft-fonts commit as release.yml.
craft-fonts-ref: b076417b5880bd4154bd22f00695b1d28c6bea83
timeout-minutes: 90
# xtask ci runs cargo test: the demo scene cache is per process, so nextest is slower here.
tools: ""
packages: >-
pkg-config libxkbcommon-dev libwayland-dev libx11-dev libxcb1-dev libxcursor-dev
libxrandr-dev libxi-dev libfontconfig1-dev libfreetype-dev mesa-vulkan-drivers
libgl1-mesa-dev libdbus-1-dev
+9 -14
View File
@@ -1,23 +1,18 @@
# FreeBSD build and package check in a QEMU VM.
name: FreeBSD
# The VM build is slow, so it runs nightly on main and on pushes to `release`, not on every pull
# request: a drifted dependency still shows up within a day, and always before a release ships.
# PRs that change the FreeBSD workflow or packaging itself still run it.
on:
push:
branches: [main]
paths:
- ".github/workflows/freebsd.yml"
- "packaging/freebsd/**"
- "Cargo.toml"
- "Cargo.lock"
- "crates/**"
branches: [release]
pull_request:
branches: [main]
paths:
- ".github/workflows/freebsd.yml"
- "packaging/freebsd/**"
- "Cargo.toml"
- "Cargo.lock"
- "crates/**"
- .github/workflows/freebsd.yml
- packaging/freebsd/**
schedule:
- cron: "7 4 * * *"
workflow_dispatch:
concurrency:
@@ -33,4 +28,4 @@ jobs:
uses: storytold/craft-common-actions/.github/workflows/freebsd.yml@main
with:
require-craft-fonts: true
craft-fonts-ref: a71c20e30af613287c504f7e685ca8fad77752bc
craft-fonts-ref: b076417b5880bd4154bd22f00695b1d28c6bea83
+38
View File
@@ -18,6 +18,44 @@ concurrency:
cancel-in-progress: true
jobs:
git-sources:
name: Git dependencies come from storytold
runs-on: ubuntu-latest
timeout-minutes: 5
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
- name: Git dependencies come from storytold
run: |
set -euo pipefail
test -f Cargo.lock || { echo "Cargo.lock is missing"; exit 1; }
# Source replacement, patches and path overrides in cargo config never show up
# in Cargo.lock. Parsed rather than grepped: TOML has many spellings of one key.
# Cargo still reads the legacy .cargo/config.
python3 - <<'PY'
import os, sys, tomllib
for path in (".cargo/config", ".cargo/config.toml"):
if not os.path.exists(path):
continue
config = tomllib.load(open(path, "rb"))
replaced = [name for name, source in config.get("source", {}).items()
if isinstance(source, dict) and "replace-with" in source]
if replaced or "patch" in config or "paths" in config:
print(f"{path} replaces, patches or overrides a source")
sys.exit(1)
PY
# Fails when Cargo.lock is out of date with the manifests.
cargo metadata --locked --format-version 1 > /dev/null
python3 - <<'PY'
import sys, tomllib
lock = tomllib.load(open("Cargo.lock", "rb"))
bad = sorted({p["source"] for p in lock.get("package", [])
if p.get("source", "").startswith("git+")
and not p["source"].startswith("git+https://github.com/storytold/")})
if bad:
print("Git dependencies outside github.com/storytold:", *bad, sep="\n")
sys.exit(1)
PY
nix:
# Intentionally tracked at @main (not SHA-pinned) so org-wide CI improvements, security fixes, and build optimizations propagate without PR churn across all craftapp repos.
uses: storytold/craft-common-actions/.github/workflows/nix.yml@main
+41 -1
View File
@@ -20,13 +20,53 @@ concurrency:
cancel-in-progress: false
jobs:
git-sources:
name: Git dependencies come from storytold
runs-on: ubuntu-latest
permissions:
contents: read
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
- name: Git dependencies come from storytold
run: |
set -euo pipefail
test -f Cargo.lock || { echo "Cargo.lock is missing"; exit 1; }
# Source replacement, patches and path overrides in cargo config never show up
# in Cargo.lock. Parsed rather than grepped: TOML has many spellings of one key.
# Cargo still reads the legacy .cargo/config.
python3 - <<'PY'
import os, sys, tomllib
for path in (".cargo/config", ".cargo/config.toml"):
if not os.path.exists(path):
continue
config = tomllib.load(open(path, "rb"))
replaced = [name for name, source in config.get("source", {}).items()
if isinstance(source, dict) and "replace-with" in source]
if replaced or "patch" in config or "paths" in config:
print(f"{path} replaces, patches or overrides a source")
sys.exit(1)
PY
# Fails when Cargo.lock is out of date with the manifests.
cargo metadata --locked --format-version 1 > /dev/null
python3 - <<'PY'
import sys, tomllib
lock = tomllib.load(open("Cargo.lock", "rb"))
bad = sorted({p["source"] for p in lock.get("package", [])
if p.get("source", "").startswith("git+")
and not p["source"].startswith("git+https://github.com/storytold/")})
if bad:
print("Git dependencies outside github.com/storytold:", *bad, sep="\n")
sys.exit(1)
PY
release:
needs: git-sources
# Intentionally tracked at @main (not SHA-pinned) so org-wide CI improvements, security fixes, and build optimizations propagate without PR churn across all craftapp repos.
uses: storytold/craft-common-actions/.github/workflows/release.yml@main
with:
app-name: lightcraft
app-title: LightCraft
craft-fonts-ref: a71c20e30af613287c504f7e685ca8fad77752bc
craft-fonts-ref: b076417b5880bd4154bd22f00695b1d28c6bea83
web-craft-fonts: true
version: ${{ inputs.version }}
secrets:
+2 -1
View File
@@ -11,7 +11,8 @@ permissions:
concurrency:
group: sonarcloud-${{ github.ref }}
cancel-in-progress: true
# A push to main is the run that publishes coverage; superseded runs there would never finish.
cancel-in-progress: ${{ github.ref != 'refs/heads/main' }}
jobs:
sonarcloud:
+7 -13
View File
@@ -5,23 +5,17 @@
# on GitHub Actions' native windows-11-arm runner.
name: Windows ARM64
# Nightly on main and on pushes to `release` (dependency and runner-image drift shows up within a
# day, and always before a release ships), and on PRs that change the Windows packaging itself.
on:
schedule:
- cron: "3 5 * * *"
push:
branches: [main]
paths:
- ".github/workflows/windows-arm64.yml"
- "packaging/windows/**"
- "Cargo.toml"
- "Cargo.lock"
- "crates/**"
branches: [release]
pull_request:
branches: [main]
paths:
- ".github/workflows/windows-arm64.yml"
- "packaging/windows/**"
- "Cargo.toml"
- "Cargo.lock"
- "crates/**"
- .github/workflows/windows-arm64.yml
- packaging/windows/**
workflow_dispatch:
concurrency:
+4 -1
View File
@@ -68,7 +68,10 @@ of it. Full standard: `../craftrules/standards/never-crash.md`
`Cancel::No` when the job can't stop. No progress window, panel or progress toast of its own; its tests assert the row.
- **Resolution independence:** settings use normalized image coordinates and relative radii; previews and exports must match.
- **Time goes through `chrono`** (workspace dependency). Never hand-roll UTC/calendar conversion (days-to-date algorithms, leap-year math); a crate owns only its output format string. The one exception is range extension: past the years chrono can represent, shift the date by whole 400-year Gregorian cycles (146,097 days, after which the calendar repeats), let chrono compute the month and day, and add the cycles back to the year (splitting seconds into hours, minutes and seconds is plain arithmetic).
- **Quality gates** before every commit: `cargo xtask ci` (fmt, clippy -D warnings, tests, layers, assets, wasm).
- **Quality gates** before every commit: `cargo xtask ci --quick` (fmt, clippy -D warnings, the changed packages'
tests, layers, assets, and heif / parity / wasm when touched). GitHub runs the full `cargo xtask ci` on every PR
(`.github/workflows/ci.yml`); run the full one locally before merging branches (below) or when `--quick` can't see
the risk. One `ci` runs at a time per machine; others wait their turn (`xtask/src/ci_lock.rs`).
- **Commits:** one task id per commit (`M2.3: local Laplacian highlights/shadows`). Only green states. End messages with the attribution line required by the environment.
## Assets: icons, images, fonts (ABSOLUTE RULE — never violate)
Generated
+808 -310
View File
File diff suppressed because it is too large. Load diff
+33 -9
View File
@@ -3,15 +3,16 @@ resolver = "3"
members = ["crates/*", "apps/*", "xtask"]
[workspace.package]
version = "0.11.0"
version = "0.14.0"
edition = "2024"
license = "MIT OR Apache-2.0"
rust-version = "1.90"
rust-version = "1.95"
repository = "https://github.com/storytold/lightcraft"
[workspace.dependencies]
craft-ui = { git = "https://github.com/storytold/craft-libs", rev = "5c874d51eeb3de70e735c8ce87a59a7dedd47675", features = ["serde"] }
craft-ui = { git = "https://github.com/storytold/craft-libs", rev = "25da1314d443171b4adeaad2cd9454b273f35c6f", features = ["serde"] }
craft-docpath = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "82a24b006eefa036b43c09a67fb9a3fbd17ffe63" }
craft-update = { git = "https://github.com/storytold/craft-libs", version = "=0.1.0", rev = "495e5971bc024aa96e6ed318cb6e7cd3a0103184", features = ["egui"] }
# internal
lightcraft-denoise-core = { path = "crates/denoise-core" }
lightcraft-denoise = { path = "crates/denoise", default-features = false }
@@ -60,8 +61,8 @@ libc = "0.2" # dev-only: process CPU time in benches
dhat = "0.3" # opt-in heap profiling (lightcraft-cli --features dhat-heap) and heap tests; never in product builds
# formats
quick-xml = "0.38"
miniz_oxide = "0.8"
quick-xml = "0.41"
miniz_oxide = "0.9"
# codecs
image = { version = "0.25", default-features = false, features = ["png", "jpeg", "tiff", "webp"] }
@@ -91,10 +92,13 @@ wgpu = { version = "30", default-features = false, features = ["std", "parking_l
pollster = "1"
# ui
egui = "0.36"
egui_extras = { version = "0.36", default-features = false }
eframe = { version = "0.36", default-features = false }
egui_kittest = "0.36"
# 0.37: `default_fonts` is no longer an egui default and the monochrome emoji faces moved behind
# `monochrome_emoji_fonts`. Every resolution root (native, CLI/headless, kittest, wasm) needs both:
# kittest's own dev-dependencies do not propagate to its consumers.
egui = { version = "0.37", default-features = false, features = ["default_fonts", "monochrome_emoji_fonts"] }
egui_extras = { version = "0.37", default-features = false }
eframe = { version = "0.37", default-features = false }
egui_kittest = "0.37"
rfd = "0.17"
muda = { version = "0.21", default-features = false } # native menu bar (macOS: objc2/AppKit, pure Rust; no GTK)
# macOS: the profile of the screen showing the window (safe AppKit bindings; muda and wgpu use the same)
@@ -125,9 +129,11 @@ excessive_precision = "allow"
[profile.dev.package."*"]
opt-level = 2
debug = false
[profile.dev]
opt-level = 1
debug = "line-tables-only"
[profile.release]
debug = "line-tables-only"
@@ -167,3 +173,21 @@ jpeg-decoder = { opt-level = 3 }
jpeg-encoder = { opt-level = 3 }
png = { opt-level = 3 }
miniz_oxide = { opt-level = 3 }
# Keep the complete egui family and native event bridge on reviewed fork commits.
[patch.crates-io]
ecolor = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" }
emath = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" }
epaint = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" }
epaint_default_fonts = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" }
egui = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" }
egui-winit = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" }
egui-wgpu = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" }
egui_glow = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" }
egui_extras = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" }
egui_kittest = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" }
egui_system_font_provider = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" }
eframe = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" }
egui_demo_lib = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" }
egui_inspection = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" }
winit = { git = "https://github.com/storytold/winit.git", rev = "a517b3846ee8457d84ee4975110bfda6797c44dc" }
+279 -6
View File
@@ -12,7 +12,9 @@ Third-party material
This product bundles third-party material under its own licenses, listed below. The per-file
list (author, source, license) is in assets/ATTRIBUTION.md.
Inter (assets/fonts/Inter-*.ttf) — SIL Open Font License 1.1. See assets/fonts/OFL-Inter.txt.
Inter (assets/fonts/Inter-*.ttf) — SIL Open Font License 1.1. The licence text is
assets/fonts/OFL-Inter.txt in the source tree; release packages ship it beside NOTICE as
OFL-Inter.txt (Windows installers include it in FONT-LICENCES.txt).
Showcase photographs by Ansel Adams, Dorothea Lange and NASA (docs/images/) — public domain. See assets/ATTRIBUTION.md.
crates/raw/src/spectral_table.rs (lightcraft-raw) holds camera colour matrices derived from the
@@ -50,11 +52,15 @@ maintainer decision. The ONNX reader and CPU/GPU executor are our Rust implement
no darktable, RawSpeed, LibRaw or other GPL decoder/runtime code is included.
Builds made with craft-fonts (the optional CRAFT_FONTS_DIR build input; all official releases)
also embed the fonts listed in https://github.com/storytold/craft-fonts/blob/main/ATTRIBUTION.md
(BIZ UDPGothic, BIZ UDMincho, Shippori Mincho, Noto Sans CJK SC, Noto Sans Arabic and Noto Sans
Thai), under the SIL Open Font License 1.1. The web build embeds BIZ UDPGothic Regular and Noto
Sans Thai Regular, and loads Noto Sans CJK SC separately. Release packages include each font's
licence as OFL-<family>.txt; Windows installers include their texts in FONT-LICENCES.txt.
also embed the default fonts listed in
https://github.com/storytold/craft-fonts/blob/b076417b5880bd4154bd22f00695b1d28c6bea83/ATTRIBUTION.md (BIZ UDPGothic, BIZ UDMincho,
Shippori Mincho, Noto Sans CJK SC, Noto Sans Arabic, Noto Sans Thai, Noto Sans Georgian,
NanumGothic, Noto Sans Symbols, Noto Sans Symbols 2, Noto Sans Hebrew and
Noto Sans Devanagari), under the SIL Open Font License 1.1. The opt-in LXGW WenKai Lite face
(fonts/simplified-chinese-manifest.txt in craft-fonts) is not embedded. The web build embeds
BIZ UDPGothic Regular, Noto Sans Thai Regular and Noto Sans Georgian Regular, and loads Noto Sans
CJK SC separately. Release packages include each font's licence as OFL-<family>.txt; Windows
installers include their texts in FONT-LICENCES.txt.
Place names for reverse geocoding (Photo > Fill Location from GPS) are not part of LightCraft and
are never distributed with it. On the user's request (Photo > Download Place Names) the app
@@ -62,3 +68,270 @@ downloads cities15000.zip, admin1CodesASCII.txt and countryInfo.txt from GeoName
(https://download.geonames.org/export/dump/) into the settings folder. GeoNames data is licensed
under Creative Commons Attribution 4.0 (https://creativecommons.org/licenses/by/4.0/);
attribution: "GeoNames, geonames.org". The lookup code (crates/geocode) is our own.
epaint_default_fonts, the egui default-font crate LightCraft builds with, also bundles Hack Regular
and Ubuntu Light, and Noto Emoji Regular when egui's `monochrome_emoji_fonts` feature is on (the
LightCraft workspace enables both `default_fonts` and `monochrome_emoji_fonts` on egui 0.37). The
LightCraft binaries embed these faces unmodified, registered through egui's default font definitions.
Source for all of them (the same pin as the icon fonts below):
https://github.com/storytold/egui/tree/6d518fc799681571963842f95b55a6b68431b15a/crates/epaint_default_fonts/fonts
Hack Regular (Hack-Regular.ttf) is Copyright (c) 2018 Source Foundry Authors and
Copyright (c) 2003 by Bitstream, Inc. All Rights Reserved. It is licensed under the MIT License, and
the Bitstream Vera parts under the Bitstream Vera License with Reserved Font Names "Bitstream" and
"Vera": the font as a whole is MIT AND Bitstream Vera License. The work in the DejaVu project it
derives from was committed to the public domain.
The following text is reproduced verbatim from Hack-Regular.txt:
The work in the Hack project is Copyright 2018 Source Foundry Authors and licensed under the MIT License
The work in the DejaVu project was committed to the public domain.
Bitstream Vera Sans Mono Copyright 2003 Bitstream Inc. and licensed under the Bitstream Vera License with Reserved Font Names "Bitstream" and "Vera"
MIT License
Copyright (c) 2018 Source Foundry Authors
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
BITSTREAM VERA LICENSE
Copyright (c) 2003 by Bitstream, Inc. All Rights Reserved. Bitstream Vera is a trademark of Bitstream, Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy of the fonts accompanying this license ("Fonts") and associated documentation files (the "Font Software"), to reproduce and distribute the Font Software, including without limitation the rights to use, copy, merge, publish, distribute, and/or sell copies of the Font Software, and to permit persons to whom the Font Software is furnished to do so, subject to the following conditions:
The above copyright and trademark notices and this permission notice shall be included in all copies of one or more of the Font Software typefaces.
The Font Software may be modified, altered, or added to, and in particular the designs of glyphs or characters in the Fonts may be modified and additional glyphs or characters may be added to the Fonts, only if the fonts are renamed to names not containing either the words "Bitstream" or the word "Vera".
This License becomes null and void to the extent applicable to Fonts or Font Software that has been modified and is distributed under the "Bitstream Vera" names.
The Font Software may be sold as part of a larger software package but no copy of one or more of the Font Software typefaces may be sold by itself.
THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL BITSTREAM OR THE GNOME FOUNDATION BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM OTHER DEALINGS IN THE FONT SOFTWARE.
Except as contained in this notice, the names of Gnome, the Gnome Foundation, and Bitstream Inc., shall not be used in advertising or otherwise to promote the sale, use or other dealings in this Font Software without prior written authorization from the Gnome Foundation or Bitstream Inc., respectively. For further information, contact: fonts at gnome dot org.
Ubuntu Light (Ubuntu-Light.ttf) is Copyright 2011 Canonical Ltd., was designed by Dalton Maag Ltd,
and is licensed under the Ubuntu Font Licence 1.0 (not the SIL Open Font License).
The following text is reproduced verbatim from UFL.txt; that file carries no copyright line, so the
copyright notice above is taken from the font's own name table:
-------------------------------
UBUNTU FONT LICENCE Version 1.0
-------------------------------
PREAMBLE
This licence allows the licensed fonts to be used, studied, modified and
redistributed freely. The fonts, including any derivative works, can be
bundled, embedded, and redistributed provided the terms of this licence
are met. The fonts and derivatives, however, cannot be released under
any other licence. The requirement for fonts to remain under this
licence does not require any document created using the fonts or their
derivatives to be published under this licence, as long as the primary
purpose of the document is not to be a vehicle for the distribution of
the fonts.
DEFINITIONS
"Font Software" refers to the set of files released by the Copyright
Holder(s) under this licence and clearly marked as such. This may
include source files, build scripts and documentation.
"Original Version" refers to the collection of Font Software components
as received under this licence.
"Modified Version" refers to any derivative made by adding to, deleting,
or substituting -- in part or in whole -- any of the components of the
Original Version, by changing formats or by porting the Font Software to
a new environment.
"Copyright Holder(s)" refers to all individuals and companies who have a
copyright ownership of the Font Software.
"Substantially Changed" refers to Modified Versions which can be easily
identified as dissimilar to the Font Software by users of the Font
Software comparing the Original Version with the Modified Version.
To "Propagate" a work means to do anything with it that, without
permission, would make you directly or secondarily liable for
infringement under applicable copyright law, except executing it on a
computer or modifying a private copy. Propagation includes copying,
distribution (with or without modification and with or without charging
a redistribution fee), making available to the public, and in some
countries other activities as well.
PERMISSION & CONDITIONS
This licence does not grant any rights under trademark law and all such
rights are reserved.
Permission is hereby granted, free of charge, to any person obtaining a
copy of the Font Software, to propagate the Font Software, subject to
the below conditions:
1) Each copy of the Font Software must contain the above copyright
notice and this licence. These can be included either as stand-alone
text files, human-readable headers or in the appropriate machine-
readable metadata fields within text or binary files as long as those
fields can be easily viewed by the user.
2) The font name complies with the following:
(a) The Original Version must retain its name, unmodified.
(b) Modified Versions which are Substantially Changed must be renamed to
avoid use of the name of the Original Version or similar names entirely.
(c) Modified Versions which are not Substantially Changed must be
renamed to both (i) retain the name of the Original Version and (ii) add
additional naming elements to distinguish the Modified Version from the
Original Version. The name of such Modified Versions must be the name of
the Original Version, with "derivative X" where X represents the name of
the new work, appended to that name.
3) The name(s) of the Copyright Holder(s) and any contributor to the
Font Software shall not be used to promote, endorse or advertise any
Modified Version, except (i) as required by this licence, (ii) to
acknowledge the contribution(s) of the Copyright Holder(s) or (iii) with
their explicit written permission.
4) The Font Software, modified or unmodified, in part or in whole, must
be distributed entirely under this licence, and must not be distributed
under any other licence. The requirement for fonts to remain under this
licence does not affect any document created using the Font Software,
except any version of the Font Software extracted from a document
created using the Font Software may only be distributed under this
licence.
TERMINATION
This licence becomes null and void if any of the above conditions are
not met.
DISCLAIMER
THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF
COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE
COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM OTHER
DEALINGS IN THE FONT SOFTWARE.
Noto Emoji Regular (NotoEmoji-Regular.ttf) is Copyright 2013 Google Inc. All Rights Reserved. It was
designed by the Monotype Design Team and is licensed under the SIL Open Font License 1.1.
The following text is reproduced verbatim from OFL.txt; that file carries no copyright line, so the
copyright notice above is taken from the font's own name table:
This Font Software is licensed under the SIL Open Font License,
Version 1.1.
This license is copied below, and is also available with a FAQ at:
http://scripts.sil.org/OFL
-----------------------------------------------------------
SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007
-----------------------------------------------------------
PREAMBLE
The goals of the Open Font License (OFL) are to stimulate worldwide
development of collaborative font projects, to support the font
creation efforts of academic and linguistic communities, and to
provide a free and open framework in which fonts may be shared and
improved in partnership with others.
The OFL allows the licensed fonts to be used, studied, modified and
redistributed freely as long as they are not sold by themselves. The
fonts, including any derivative works, can be bundled, embedded,
redistributed and/or sold with any software provided that any reserved
names are not used by derivative works. The fonts and derivatives,
however, cannot be released under any other type of license. The
requirement for fonts to remain under this license does not apply to
any document created using the fonts or their derivatives.
DEFINITIONS
"Font Software" refers to the set of files released by the Copyright
Holder(s) under this license and clearly marked as such. This may
include source files, build scripts and documentation.
"Reserved Font Name" refers to any names specified as such after the
copyright statement(s).
"Original Version" refers to the collection of Font Software
components as distributed by the Copyright Holder(s).
"Modified Version" refers to any derivative made by adding to,
deleting, or substituting -- in part or in whole -- any of the
components of the Original Version, by changing formats or by porting
the Font Software to a new environment.
"Author" refers to any designer, engineer, programmer, technical
writer or other person who contributed to the Font Software.
PERMISSION & CONDITIONS
Permission is hereby granted, free of charge, to any person obtaining
a copy of the Font Software, to use, study, copy, merge, embed,
modify, redistribute, and sell modified and unmodified copies of the
Font Software, subject to the following conditions:
1) Neither the Font Software nor any of its individual components, in
Original or Modified Versions, may be sold by itself.
2) Original or Modified Versions of the Font Software may be bundled,
redistributed and/or sold with any software, provided that each copy
contains the above copyright notice and this license. These can be
included either as stand-alone text files, human-readable headers or
in the appropriate machine-readable metadata fields within text or
binary files as long as those fields can be easily viewed by the user.
3) No Modified Version of the Font Software may use the Reserved Font
Name(s) unless explicit written permission is granted by the
corresponding Copyright Holder. This restriction only applies to the
primary font name as presented to the users.
4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font
Software shall not be used to promote, endorse or advertise any
Modified Version, except to acknowledge the contribution(s) of the
Copyright Holder(s) and the Author(s) or with their explicit written
permission.
5) The Font Software, modified or unmodified, in part or in whole,
must be distributed entirely under this license, and must not be
distributed under any other license. The requirement for fonts to
remain under this license does not apply to any document created using
the Font Software.
TERMINATION
This license becomes null and void if any of the above conditions are
not met.
DISCLAIMER
THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE
COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM
OTHER DEALINGS IN THE FONT SOFTWARE.
The UI's egui-icons.ttf subset and emoji-icon-font.ttf fallback are provided by epaint_default_fonts
and registered through egui's default font definitions. They derive from emoji-icon-font by
John Slegers, under the MIT License. The subset and full face share the following upstream notice.
Source: https://github.com/storytold/egui/tree/6d518fc799681571963842f95b55a6b68431b15a/crates/epaint_default_fonts/fonts
Original: https://github.com/jslegers/emoji-icon-font
The following text is reproduced verbatim from emoji-icon-font-mit-license.txt:
MIT License
Copyright (c) 2014 John Slegers
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+20 -5
View File
@@ -8,6 +8,10 @@
</p>
<p align="center">
<img src="assets/app-icon/hicolor/256x256/apps/ai.storyteller.lightcraft.png" width="128" height="128" alt="LightCraft icon: an engraved lynx on an amber tile">
</p>
<h1 align="center">LightCraft</h1>
<h3 align="center">Your photos. Your pixels. Your machine.</h3>
@@ -344,7 +348,7 @@ LightCraft is young and moving fast: **alpha**. **Where we honestly stand** (det
casual use.** It's great for JPEG/DNG and most Sony / Panasonic /
Fujifilm / Nikon / older-Canon raws on one machine; see [docs/raw-parity.md](docs/raw-parity.md) for your camera.
- **The biggest gaps:**
- **camera colour calibration:** Sony, Nikon, Panasonic, Fujifilm and Canon CR3 raws have guarded estimates from their camera JPEGs, with built-in ILCA-99M2, ILCE-7CR, ILCE-7M4, X-H2S, X-T4, Nikon Z 6II, Z 8 and D5300 profiles; measured calibration is missing, and other raws or rejected fits retain a neutral matrix;
- **camera colour calibration:** Sony, Nikon, Panasonic, Fujifilm and Canon CR3 raws have guarded estimates from their camera JPEGs, with built-in ILCA-99M2, ILCE-7CR, ILCE-7M4, ILCE-7RM5, X-H2S, X-T4, Nikon Z 6II, Z 8 and D5300 profiles; measured calibration is missing, and other raws or rejected fits retain a neutral matrix;
- **compressed Olympus raws and unsupported CR3 variants:** these use embedded JPEG previews when present. Fujifilm lossless/lossy compressed RAF now decodes sensor data; [verification and existing-library reload instructions](docs/raf-compression.md);
- **AI quality and model setup:** Subject and Sky selection are classical heuristics; Object/Describe use an
optional SAM 3 model. AI Denoise supports Bayer RAW with separately installed weights, while X-Trans and
@@ -401,15 +405,26 @@ cargo run --release -p lightcraft -- --control 7980 # with the automation ch
cargo xtask web --serve # the same app in the browser: http://127.0.0.1:8080/
cargo run --release -p lightcraft-cli -- render photo.jpg -o out.jpg --set light.exposure=0.5
cargo xtask ci # fmt, clippy, HEIF, tests, parity, layers, assets, WASM
cargo xtask ci --quick # only what your branch's changes can break (minutes)
```
`cargo xtask ci --quick` compares with the merge base of `origin/main` (`--base REF` for another)
and runs fmt, clippy, the tests of the changed packages, layers and assets, plus HEIF, parity and
the wasm check when the changes touch them; a change to `Cargo.toml`, `Cargo.lock` or the lint
config tests everything. GitHub runs the full `cargo xtask ci` on every pull request
(`.github/workflows/ci.yml`). Only one `ci` runs at a time per machine: a second one, from another
worktree say, waits for the first and names it, since several at once take longer in total than in
turn. `LIGHTCRAFT_CI_SLOTS=2` allows two; `LIGHTCRAFT_CI_LOCK=off` skips the lock.
CI defaults to line-table debug information and one build job per 1.5 GB of RAM
available when it starts (at most one per CPU; 4 if memory can't be read), so an
8 GB machine with 6 GB free builds with 4, and a busy machine with little free
builds with fewer. Test threads follow, capped at 4. Full-debuginfo linkers, one
per CPU, otherwise exhaust RAM before any test runs. GPU coverage is unchanged. Explicit
`CARGO_PROFILE_DEV_DEBUG`, `CARGO_BUILD_JOBS` and `RUST_TEST_THREADS` settings
override these defaults. Ordinary development commands keep their own settings.
builds with fewer. Test threads follow, capped at 4.
Full-debuginfo linkers, one per CPU, otherwise exhaust RAM before any test runs. GPU coverage is
unchanged. Explicit `CARGO_PROFILE_DEV_DEBUG`, `CARGO_BUILD_JOBS`, `RUST_TEST_THREADS` and
`NEXTEST_TEST_THREADS` settings override these defaults. The tests run under
`cargo test`; `LIGHTCRAFT_NEXTEST=1` runs them under [cargo-nextest](https://nexte.st) instead (one
process per test, slower here because each regenerates the demo scenes). Ordinary development commands keep their own settings.
**Release builds on memory-constrained machines:** [#1196](https://github.com/storytold/lightcraft/issues/1196)
reports Arch/AUR release-link OOM kills on a 16 GB machine when the desktop and CLI build concurrently
+6 -6
View File
@@ -298,18 +298,18 @@ lightcraft --control 7980 ~/Pictures/trip
## Feature status
LightCraft 很年轻,而且进展很快。**我们如实所处的位置**(细节见[路线图](ROADMAP.md#where-we-stand)):
LightCraft 很年轻,而且进展很快。**我们如实所处的位置**(细节见[路线图](ROADMAP.md)):
- **按功能数量算,我们达到了约 79%**(核心功能 98%),逐行记录在
[docs/parity.md](docs/parity.md)。
[docs/parity-checklist.md](docs/parity-checklist.md)。
- **作为日常使用的工具,我们更接近 60–70%。** 在单台机器上处理 JPEG/DNG 以及大多数尼康/索尼/
较老佳能的 raw 都很出色。
- **最大的缺口:**
- **相机色彩校准:** 索尼、尼康、松下、富士和佳能 CR3 的 raw 会从各自的相机 JPEG 得到受保护的估计值,内置 ILCA-99M2、ILCE-7CR、ILCE-7M4、X-H2S、X-T4、尼康 Z 6II、Z 8 和 D5300 配置文件;实测校准仍然缺失,其他 raw 或被拒绝的拟合会保留中性矩阵;
- **相机色彩校准:** 索尼、尼康、松下、富士和佳能 CR3 的 raw 会从各自的相机 JPEG 得到受保护的估计值,内置 ILCA-99M2、ILCE-7CR、ILCE-7M4、ILCE-7RM5、X-H2S、X-T4、尼康 Z 6II、Z 8 和 D5300 配置文件;实测校准仍然缺失,其他 raw 或被拒绝的拟合会保留中性矩阵;
- **压缩的奥林巴斯 raw 和不受支持的 CR3 变体:** 这些在有内嵌 JPEG 预览时使用预览。富士无损/有损压缩 RAF 现在能解码传感器数据;[验证与已有图库重新加载说明](docs/raf-compression.md);
- **AI 蒙版与降噪:** 主体和天空选择目前是经典启发式算法;
- **屏幕上的 HDR 显示、视频,以及经典版式中的打印/画册/地图模块。**
- **接下来做什么:** 见[我们要去往哪里](ROADMAP.md#where-were-going)。
- **接下来做什么:** 见[我们要去往哪里](ROADMAP.md#upcoming)。
| 领域 | 状态 |
|---|---|
@@ -333,7 +333,7 @@ LightCraft 很年轻,而且进展很快。**我们如实所处的位置**(
| 镜头校正(畸变、暗角、自动 + 手动色差、去边、DNG 文件内嵌的镜头校正以及松下/徕卡 RW2/RWL 畸变数据)、几何(变换、限制裁剪)、垂直校正(自动/水平/垂直/完整/引导式) | ✅ · 相机镜头配置文件(我们自己的)⬜ |
| 照片合并:HDR(自动对齐、去重影)、全景(球面/圆柱/透视、边界变形、自动裁剪)、HDR 全景 → DNG | ✅ |
| GPU 管线(wgpu 计算,与 CPU 一致到 1/255 以内)、设备限制/出错时回退到 CPU | ✅ · 浏览器中的 WebGPU 🚧 |
| AI:分割蒙版、AI 降噪、超分辨率、人脸;视频 | ⬜(见[路线图](ROADMAP.md#where-were-going)) |
| AI:分割蒙版、AI 降噪、超分辨率、人脸;视频 | ⬜(见[路线图](ROADMAP.md#upcoming)) |
| 网页版(通过 WASM 在浏览器中使用同一界面):图库持久化在 OPFS/IndexedDB、Web Worker 渲染、导出下载 | ✅ · WebGPU、Safari/Firefox 测试 🚧 |
<sub>✅ 现已可用 · 🚧 进行中 · ⬜ 尚未开始</sub>
@@ -357,7 +357,7 @@ CI 默认使用行表调试信息,并按启动时可用内存每 1.5 GB 开一
(每个 CPU 最多一个;读不到内存时为 4 个),所以一台 8 GB、空闲 6 GB 的机器会用 4 个任务构建,
而一台空闲内存很少的繁忙机器会用更少。测试线程数随之调整,上限为 4。否则,每个 CPU 一个的
完整调试信息链接器会在任何测试运行之前就耗尽内存。GPU 覆盖范围不变。显式设置
`CARGO_PROFILE_DEV_DEBUG`、`CARGO_BUILD_JOBS` 和 `RUST_TEST_THREADS` 会覆盖这些默认值。
`CARGO_PROFILE_DEV_DEBUG`、`CARGO_BUILD_JOBS`、`RUST_TEST_THREADS` 和 `NEXTEST_TEST_THREADS` 会覆盖这些默认值。
日常开发命令保持它们各自的设置。
**内存受限机器上的 release 构建:** [#1196](https://github.com/storytold/lightcraft/issues/1196)
+2 -2
View File
@@ -320,7 +320,7 @@ LightCraft 還很年輕,而且進展飛快:**Alpha 階段**。**老實說,
在單機上處理 JPEG/DNG,以及大多數 Sony/Panasonic/
Fujifilm/Nikon/較舊款 Canon 的 raw 都很出色;各機型詳情見 [docs/raw-parity.md](docs/raw-parity.md)。
- **最大的缺口:**
- **相機色彩校正:** Sony、Nikon、Panasonic、Fujifilm 和 Canon CR3 的 raw 會從各自的相機 JPEG 得到受保護的估計值,內建 ILCA-99M2、ILCE-7CR、ILCE-7M4、X-H2S、X-T4、Nikon Z 6II、Z 8 和 D5300 描述檔;仍缺少實測校正,其他 raw 或被拒絕的擬合會保留中性矩陣;
- **相機色彩校正:** Sony、Nikon、Panasonic、Fujifilm 和 Canon CR3 的 raw 會從各自的相機 JPEG 得到受保護的估計值,內建 ILCA-99M2、ILCE-7CR、ILCE-7M4、ILCE-7RM5、X-H2S、X-T4、Nikon Z 6II、Z 8 和 D5300 描述檔;仍缺少實測校正,其他 raw 或被拒絕的擬合會保留中性矩陣;
- **壓縮的 Olympus raw 與不受支援的 CR3 變體:** 這些在有內嵌 JPEG 預覽時會改用預覽畫面。Fujifilm 無損/有損壓縮 RAF 現在能解碼感光元件資料;[驗證與現有照片庫重新載入說明](docs/raf-compression.md);
- **AI 遮色片與降噪:** 主體與天空選取目前是傳統啟發式演算法;
- **網路照片庫:** 預覽只讀取 raw 的標頭 — 或者,對於 CR3 或預覽標籤位於其後的 TIFF raw,只讀取稀疏規劃器指定的範圍 — 以及其內嵌的 JPEG(56.5 MB → 5.9 MB,10 秒 → 1 秒,測得於測試用的網路共用資料夾),但*顯影後*的畫面仍需要透過網路傳輸感光元件資料;詳見 [docs/raw-preview-reads.md](docs/raw-preview-reads.md);
@@ -375,7 +375,7 @@ CI 預設使用行表除錯資訊,並依啟動時可用記憶體每 1.5 GB 開
(每個 CPU 最多一個;讀不到記憶體時為 4 個),所以一台 8 GB、剩餘 6 GB 可用的機器會用 4 個任務建置,
而一台忙碌、剩餘記憶體很少的機器會用更少。測試執行緒數隨之調整,上限為 4。若不限制,每個 CPU 各跑
一個帶完整除錯資訊的連結器,測試還沒開始記憶體就會耗盡。GPU 測試的涵蓋範圍不受影響。明確設定
`CARGO_PROFILE_DEV_DEBUG`、`CARGO_BUILD_JOBS` 與 `RUST_TEST_THREADS` 會覆寫這些預設值。
`CARGO_PROFILE_DEV_DEBUG`、`CARGO_BUILD_JOBS`、`RUST_TEST_THREADS` 與 `NEXTEST_TEST_THREADS` 會覆寫這些預設值。
日常開發指令保留各自的設定。
**記憶體受限機器上的 release 建置:** [#1196](https://github.com/storytold/lightcraft/issues/1196)
+9 -3
View File
@@ -2,7 +2,7 @@
**Stage: alpha** · next: beta, ~14 points and ~340–600 h away
> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-16 · **Change:** minor (Minolta MRW decoded, 6 bodies verified; RAW: 83% of Adobe's models decode, 38% verified after the camera-coverage sweep #1327; German catalog 91% → 97%: interface strings and What's New lines translated) · **Target:** Adobe Lightroom Classic 15.5 / Lightroom 9.6
> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-22 · **Change:** minor (Print: Japanese photo and postcard paper sizes — L判, 2L判, KG判, はがき, 往復はがき, 六切, 四切, 名刺; Minolta MRW decoded, 6 bodies verified; RAW: 83% of Adobe's models decode, 38% verified after the camera-coverage sweep #1327; German catalog 91% → 97%: interface strings and What's New lines translated) · **Target:** Adobe Lightroom Classic 15.5 / Lightroom 9.6
One-page summary of how close LightCraft is to Adobe Lightroom. Agents: read this, then pick work
from [docs/gaps.md](docs/gaps.md). The full assessment, method and evidence are in
@@ -114,6 +114,11 @@ Ranked toward beta; detail in [docs/roadmap.md](docs/roadmap.md), gaps in [docs/
## Progress log
- 2026-10-22: Print page setup and PDF output gain the Japanese photo and postcard papers — L判
(89 × 127 mm), 2L判 (127 × 178), KG判 (102 × 152), はがき (100 × 148), 往復はがき (148 × 200),
六切 (203 × 254), 四切 (254 × 305) and 名刺 (55 × 91) — at their exact metric sizes. The Print dialog
and `print.set` take their customary names, and names are matched case-, space- and
punctuation-insensitively (`L判`, `l`, `L-size` are one paper).
- 2026-10-14: Camera-coverage sweep verifies 30 more models on 40 CC0 files, with file and sensor
hashes pinned (`docs/raw/coverage.py`: 515 → 545 verified). Eleven candidates excluded after rechecking
identity, provenance and camera-image agreement. Phone and drone DNG tone differences remain tracked in #1277.
@@ -121,7 +126,7 @@ Ranked toward beta; detail in [docs/roadmap.md](docs/roadmap.md), gaps in [docs/
RGGB and GBRG, as-shot WB from `WBG`); DiMAGE 7, 7Hi, A1, A2, A200 and Dynax / Maxxum 7D verified on
CC0 files. 551 models verified, 201 preview-only; headline numbers unchanged.
- 2026-10-14: 97 Canon CR2, Pentax and Ricoh models verified, one CC0 raw.pixls.us file each pinned and
checked against the camera JPEG (`crates/raw/tests/canon_pentax_corpus.rs`). 334 models verified, ~50% of
checked against the camera JPEG (`crates/raw/tests/it/canon_pentax_corpus.rs`). 334 models verified, ~50% of
photographers on a verified body; headline numbers unchanged.
- 2026-10-12: Mamiya MEF (ZD) decodes from sensor data and is imported; verified on a CC0 file. 241 models
verified, 35 unsupported; headline numbers unchanged.
@@ -135,7 +140,7 @@ Ranked toward beta; detail in [docs/roadmap.md](docs/roadmap.md), gaps in [docs/
decode. The EOS R6 Mark III gets its as-shot white balance (`ColorData` 66). 259 models verified; RAW
decoding 77% → 83%; mainstream ~49% → ~51%, essentials ~64% → ~65%, full ~61%.
- 2026-10-12: 117 Nikon and Fujifilm bodies verified, one CC0 raw.pixls.us file each pinned and checked
against the camera JPEG (`crates/raw/tests/body_corpus.rs`); the sweep found COOLPIX 5700 / 8400 / 8800
against the camera JPEG (`crates/raw/tests/it/body_corpus.rs`); the sweep found COOLPIX 5700 / 8400 / 8800
decoding wrongly. 354 models verified, ~58% of photographers on a verified body; headline numbers unchanged.
- 2026-10-11: Canon EOS R5 Mark II C-RAW decodes from sensor data (57% of its files opened as a
preview): the CRX QP map's first-symbol literal and limited-length escape; four CC0 R5 Mark II files
@@ -186,6 +191,7 @@ Ranked toward beta; detail in [docs/roadmap.md](docs/roadmap.md), gaps in [docs/
| Date | Change | Summary |
|---|---|---|
| 2026-10-22 | minor | Print: Japanese photo and postcard paper sizes (L判, 2L判, KG判, はがき, 往復はがき, 六切, 四切, 名刺) in the page setup, Print dialog and PDF output; headline numbers unchanged |
| 2026-10-19 | minor | Indonesian interface (`id`): every plain and formats message, plus 69 command labels, locale, menu command and tests; What's New left in English; Localization ~55% → ~64% (mean of the 12 key languages), 8 of 12 present |
| 2026-10-12 | minor | Italian interface (`it`, #1192): 2,201 of 2,398 messages, locale, menu command and tests; headline numbers unchanged |
| 2026-10-13 | minor | Sony ILCA-99M2 camera colour profile in the log; numbers unchanged |
+6 -1
View File
@@ -819,6 +819,7 @@ fn snapshot(args: &[String]) -> Result<(), String> {
let services = lightcraft_ui_egui::Services {
write_shared: Some(std::sync::Arc::new(lightcraft_engine::export::write_file)),
write: Some(Box::new(lightcraft_engine::export::write_file)),
write_new: Some(Box::new(lightcraft_engine::export::write_file_new)),
png: Some(Box::new(|img: &lightcraft_raster::Rgba8| {
lightcraft_codecs::encode_png(&lightcraft_codecs::EncodeImage::rgba8(img), &lightcraft_codecs::EncodeMeta::default()).unwrap_or_default()
})),
@@ -935,7 +936,11 @@ fn snapshot(args: &[String]) -> Result<(), String> {
fn commands(args: &[String]) -> Result<(), String> {
let mut h = Headless::demo();
let cmds = h.call("engine.commands", json!({}))?;
let mut cmds = h.call("engine.commands", json!({}))?;
// Listed by id so each family (`photo.*`, `mask.*`, …) reads together; the registry keeps its own order.
if let Some(list) = cmds.as_array_mut() {
list.sort_by(|a, b| a["id"].as_str().unwrap_or("").cmp(b["id"].as_str().unwrap_or("")));
}
let mut out = std::io::stdout().lock();
if args.iter().any(|a| a == "--json") {
writeln!(out, "{}", serde_json::to_string_pretty(&cmds).unwrap_or_default()).map_err(|e| e.to_string())?;
@@ -286,6 +286,38 @@ fn commands_subcommand_lists_registry() {
assert!(String::from_utf8_lossy(&o.stdout).contains("light.exposure"));
}
#[test]
fn commands_subcommand_lists_commands_sorted_by_id() {
let o = Command::new(BIN).args(["commands", "--json"]).output().unwrap();
assert!(o.status.success());
let v: Value = serde_json::from_slice(&o.stdout).unwrap();
let cmds = v.as_array().unwrap();
for c in cmds {
for field in ["id", "label", "menu", "shortcut", "params", "enabled"] {
assert!(c.get(field).is_some(), "every command should keep its {field}: {c}");
}
}
let ids: Vec<&str> = cmds.iter().filter_map(|c| c["id"].as_str()).collect();
assert_eq!(ids.len(), cmds.len(), "every command should have a string id");
let unsorted = ids.windows(2).find(|w| w[0] >= w[1]);
assert!(unsorted.is_none(), "--json should list commands sorted by id, but {unsorted:?} are out of order");
let o = Command::new(BIN).arg("commands").output().unwrap();
assert!(o.status.success());
let text = String::from_utf8_lossy(&o.stdout);
let lines: Vec<&str> = text.lines().collect();
assert_eq!(lines.len(), 2 * ids.len(), "each command should take an id line and a params line");
let listed: Vec<&str> = lines.iter().step_by(2).filter_map(|l| l.split_whitespace().next()).collect();
assert_eq!(listed, ids, "the text output should list the same commands in the same order as --json");
assert!(lines.iter().skip(1).step_by(2).all(|l| l.trim_start().starts_with("params:")), "each id line should be followed by its params line");
for (line, c) in lines.iter().step_by(2).zip(cmds) {
assert!(line.contains(c["label"].as_str().unwrap()), "{line:?} should show the label of {c}");
if let Some(sc) = c["shortcut"].as_str() {
assert!(line.contains(&format!("[{sc}]")), "{line:?} should show the shortcut of {c}");
}
}
}
#[test]
fn snapshot_subcommand_renders_the_ui_headlessly() {
let script = tmp("snap.jsonl");
+4
View File
@@ -0,0 +1,4 @@
//! The integration tests, one module per file, linked into one test binary.
mod cli;
mod mcp_shutdown;
+48
View File
@@ -0,0 +1,48 @@
//! The page's automation entry point, `lightcraft.command(id, params)` (see `web.rs`): what the
//! params text means. Plain Rust, so it is tested natively.
use serde_json::Value;
/// The command parameters in `text`: a JSON object, or `{}` when the text is empty or blank (or
/// `null`). Anything else is an error, never `{}`: a command run without its params acts on the
/// selection or with defaults, so a typo such as `{ids: [3]}` must not delete or reset the
/// selected photos. The control channel and MCP refuse such params the same way.
pub fn params(text: &str) -> Result<Value, String> {
let text = text.trim();
if text.is_empty() {
return Ok(Value::Object(Default::default()));
}
match serde_json::from_str::<Value>(text) {
Ok(Value::Null) => Ok(Value::Object(Default::default())),
Ok(v @ Value::Object(_)) => Ok(v),
Ok(_) => Err("params must be a JSON object, e.g. {\"ids\": [3]}".into()),
Err(e) => Err(format!("params are not valid JSON ({e}); nothing was run")),
}
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
#[test]
fn blank_or_null_params_are_an_empty_object() {
for text in ["", " ", "\n", "null", " null "] {
assert_eq!(params(text), Ok(json!({})), "{text:?}");
}
}
#[test]
fn an_object_is_passed_through() {
assert_eq!(params(r#"{"ids": [3], "rating": 5}"#), Ok(json!({"ids": [3], "rating": 5})));
}
/// Before, any of these ran the command with `{}`: `photo.delete` then removed the selection.
#[test]
fn invalid_json_or_a_non_object_is_refused() {
for text in ["{ids: [3]}", "{\"ids\": [3]", "ids=3", "[3]", "3", "\"{}\"", "true"] {
assert!(params(text).is_err(), "{text:?} must not run the command");
}
assert!(params("{ids: [3]}").unwrap_err().contains("nothing was run"));
}
}
+6 -3
View File
@@ -39,7 +39,7 @@ pub struct Bench {
}
impl Bench {
/// `start_ms`: the page's time origin in the same clock as [`lightcraft_ui_egui::now_ms`].
/// `start_ms`: an origin on the same monotonic clock as [`lightcraft_ui_egui::monotonic_ms`].
pub fn new(start_ms: f64) -> Self {
Bench {
phase: Phase::Idle,
@@ -54,13 +54,16 @@ impl Bench {
}
}
/// The render key of the main preview texture, or `None` while that slot holds no texture.
/// A changed key means the benchmark's requested main-image render has arrived.
fn main_key(app: &LightcraftApp) -> Option<u64> {
app.renderer.textures.get(&Slot::Main).map(|t| t.key)
}
/// Call once per frame after `LightcraftApp::logic`. Returns the report when finished.
/// Call once per root UI pass after `LightcraftApp::ui` (a UI-phase step, never a hidden tick).
/// Returns the report when finished.
pub fn step(&mut self, app: &mut LightcraftApp) -> Option<Value> {
let now = lightcraft_ui_egui::now_ms();
let now = lightcraft_ui_egui::monotonic_ms();
self.frames += 1;
if self.first_frame_ms.is_none() && self.frames > 1 {
self.first_frame_ms = Some(now - self.start_ms);
+1
View File
@@ -18,6 +18,7 @@
#![forbid(unsafe_code)]
#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)]
pub mod automation;
pub mod backup;
pub mod bench;
pub mod files;
+94 -32
View File
@@ -24,7 +24,11 @@ use crate::store::{Originals, content_hash, download_name, storage_key};
use crate::wire::ThumbIndex;
use crate::workers::{THUMB_INDEX, Workers};
const ACCEPT: &str = ".jpg,.jpeg,.png,.tif,.tiff,.webp,.dng,.cr2,.nef,.arw,.psd,.jxl,.gif,.bmp";
/// The web picker's accept list, derived from the engine's import extensions (both
/// cases) so no format the engine can import is silently filtered out of the dialog.
fn accept_list() -> String {
lightcraft_engine::import::accept_list()
}
/// After a failed save, wait this long before trying again (ms).
const SAVE_RETRY_MS: f64 = 2000.0;
@@ -127,7 +131,7 @@ fn open_picker(originals: Originals, backend: Option<Backend>, ctx: egui::Contex
let Ok(input) = doc.create_element("input").map(|e| e.unchecked_into::<web_sys::HtmlInputElement>()) else { return };
input.set_type("file");
input.set_multiple(true);
input.set_accept(ACCEPT);
input.set_accept(&accept_list());
let inp = input.clone();
let on_change = Closure::<dyn FnMut()>::new(move || {
if let Some(files) = inp.files() {
@@ -168,6 +172,9 @@ fn download(path: &str, bytes: &[u8]) -> Result<(), String> {
Ok(())
}
/// Build the browser host's `Services`: library backup/restore through browser storage and a
/// download, asynchronous file picking, downloads for writes, and PNG encoding. Desktop-only
/// services (native pickers, reveal, model folders) stay unset.
fn services(
originals: Originals,
backend: Option<Backend>,
@@ -249,7 +256,12 @@ fn services(
save_curve_preset_file: None,
pick_slideshow_templates: None,
save_slideshow_template: None,
pick_print_templates: None,
save_print_template: None,
write: Some(write),
// the browser download path has no meaningful create-new file write; the control channel
// (and therefore screenshot paths) is not available on the web
write_new: None,
// downloads happen on the main thread: exports run in the foreground on the web
write_shared: None,
png: Some(Box::new(|img: &lightcraft_raster::Rgba8| {
@@ -292,13 +304,14 @@ async fn flush(files: Files, backend: Backend, dir: String, flushing: Rc<Cell<bo
log::error!("saving {} failed: {e}", op.name());
files.failed(&ops[i..]);
files.set_error(Some(e));
failed_at.set(perf_now());
failed_at.set(lightcraft_ui_egui::monotonic_ms());
break 'outer;
}
}
}
flushing.set(false);
// the next frame reports the library as saved (the page framing LightCraft hears `dirty: false`)
// the page title drops its unsaved • now, and the page framing LightCraft hears `dirty: false`,
// even when nothing else wakes the page
request_repaint();
}
@@ -431,12 +444,17 @@ struct WebApp {
lib_dir: String,
bench: Option<Bench>,
first_frame_logged: bool,
/// The page title last set (`document.title`), so it is written only when it changes.
title: String,
last_save: f64,
ui_written: Vec<u8>,
bridge: Option<crate::host::Bridge>,
}
impl WebApp {
/// Construct the web app from the boot options: install originals and open the library from
/// browser storage (a read failure starts an empty session and a blocking `LibraryProblem`),
/// load the stored UI state, start render workers and the optional benchmark.
fn new(cc: &eframe::CreationContext<'_>, boot: Boot, chinese_font: Vec<u8>) -> Self {
let Boot { opts, backend, files, index, lib_dir, bridge, load_error } = boot;
let originals = Originals::default();
@@ -525,7 +543,7 @@ impl WebApp {
log::warn!("lightcraft: the page framing LightCraft could not be told the app is ready");
}
}
let origin = lightcraft_ui_egui::now_ms() - perf_now();
let origin = lightcraft_ui_egui::monotonic_ms();
WebApp {
app,
originals,
@@ -538,6 +556,7 @@ impl WebApp {
lib_dir,
bench: opts.bench.then(|| Bench::new(origin)),
first_frame_logged: false,
title: String::new(),
last_save: 0.0,
ui_written,
bridge,
@@ -583,24 +602,41 @@ impl WebApp {
self.files.is_dirty() || self.flushing.get() || self.app.session.unsaved().is_some()
}
/// Consume the drops the raw-input hook queued once (never re-read `raw.dropped_files` in a
/// hidden logic tick), and add originals whose bytes have arrived.
fn import_dropped(&mut self, ctx: &egui::Context) {
let dropped = ctx.input(|i| i.raw.dropped_files.clone());
let dropped = self.app.take_dropped_files();
// a dialog is on screen (the card said so while the files hovered): nothing is read or imported
// (files read from an earlier drop still finish below)
if !dropped.is_empty() && self.app.ui.dialog.is_some() {
self.app.toast(ctx, lightcraft_ui_egui::i18n::tr("Close the open dialog, then drop the files again"));
return;
}
for f in dropped {
let (originals, backend, ctx) = (self.originals.clone(), self.backend.clone(), ctx.clone());
wasm_bindgen_futures::spawn_local(async move {
let name = f.path().to_string_lossy().to_string();
match f.bytes_async().await {
let name = egui::DroppedFile::path(f.as_ref()).to_string_lossy().to_string();
match egui::DroppedFile::bytes_async(f.as_ref()).await {
Ok(bytes) => store_file(originals, backend, name, bytes, ctx).await,
Err(e) => log::warn!("reading dropped {name}: {e}"),
Err(e) => notice(lightcraft_ui_egui::i18n::dropped_file_unreadable(&name, &e.to_string())),
}
});
}
let paths = self.originals.take_pending();
if !paths.is_empty() {
let n = paths.len();
match self.app.run("library.import", json!({"paths": paths})) {
Ok(_) => self.app.toast(ctx, format!("Added {n} photo{}", if n == 1 { "" } else { "s" })),
Err(e) => log::warn!("import: {e}"),
// what the import did, not what was submitted: duplicates and unsupported files add nothing,
// a drop that only finds duplicates says nothing (as the desktop import's toast does when it
// shows where they are), and one whose files could not be read says so
Ok(report) => {
if let Some(notice) = lightcraft_ui_egui::i18n::dropped_photos_outcome(&report) {
self.app.toast(ctx, notice);
}
}
Err(e) => {
log::warn!("import: {e}");
self.app.toast(ctx, lightcraft_ui_egui::i18n::dropped_import_failed(&e));
}
}
}
}
@@ -640,7 +676,7 @@ impl WebApp {
if self.frozen.get() {
return;
}
let now = perf_now();
let now = lightcraft_ui_egui::monotonic_ms();
if now - self.last_save >= SAVE_EVERY_MS {
self.last_save = now;
self.app.session.save_view();
@@ -716,8 +752,11 @@ impl WebApp {
fn run_inbox(&mut self) {
let inbox = INBOX.with(|q| std::mem::take(&mut *q.borrow_mut()));
for (id, params, (resolve, reject)) in inbox {
let params: serde_json::Value = serde_json::from_str(&params).unwrap_or(json!({}));
let r = if id == "web.stats" { Ok(self.web_stats()) } else { self.app.run(&id, params) };
let r = match crate::automation::params(&params) {
Err(e) => Err(format!("{id}: {e}")),
Ok(_) if id == "web.stats" => Ok(self.web_stats()),
Ok(params) => self.app.run(&id, params),
};
let _ = match r {
Ok(v) => resolve.call1(&JsValue::NULL, &v.to_string().into()),
Err(e) => reject.call1(&JsValue::NULL, &e.into()),
@@ -727,14 +766,15 @@ impl WebApp {
}
impl eframe::App for WebApp {
/// eframe's non-drawing pass for the browser host: drain safety notices and queued inbox work,
/// load originals the UI requested, apply the cache budget, run the app's logic, then save.
fn logic(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
for text in safety::take_notices() {
let now = ctx.input(|i| i.time);
let now = self.app.tick_now();
self.app.ui.toast = Some((text, now + NOTICE_SECS, None));
}
self.run_inbox();
self.run_host(ctx);
self.import_dropped(ctx);
self.load_originals(ctx);
if let Some(w) = &self.workers {
// `library.preferences ▸ cacheMb` is read by the session; the worker index prunes on
@@ -746,27 +786,49 @@ impl eframe::App for WebApp {
if let Some(b) = &self.bridge {
b.report_dirty(self.dirty_for_host());
}
if let Some(b) = self.bench.as_mut()
&& let Some(report) = b.step(&mut self.app)
{
log::info!("lightcraft-bench {report}");
if let Some((alive, ready, remote, inline)) = self.workers.as_ref().map(Workers::stats) {
log::info!("lightcraft-workers {{\"alive\":{alive},\"ready\":{ready},\"remote_jobs\":{remote},\"inline_jobs\":{inline}}}");
}
set_status(&format!("bench: {report}"));
self.bench = None;
}
if self.bench.is_some() {
ctx.request_repaint();
}
}
fn raw_input_hook(&mut self, _ctx: &egui::Context, raw: &mut egui::RawInput) {
self.app.raw_input_hook(raw);
/// Pass the browser's raw input to the app hook before egui processes it, so prompt barriers
/// and synthetic or dropped input are applied consistently with the desktop host.
fn raw_input_hook(&mut self, ctx: &egui::Context, raw: &mut egui::RawInput) {
self.app.raw_input_hook(ctx, raw);
}
/// eframe's UI pass: import queued drops unless a prompt blocks input, draw the app, update the
/// page title, and advance the benchmark once per root pass.
fn ui(&mut self, ui: &mut egui::Ui, _frame: &mut eframe::Frame) {
// Drops are the UI phase's: a hidden `logic`/`run_logic` tick must not import them twice.
// A prompt owns the library too: keep taking the queue only after it is gone, so a drop
// during a notice cannot add photos behind it.
let ctx = ui.ctx().clone();
if !lightcraft_ui_egui::panels::notices::blocks_input(&self.app) {
self.import_dropped(&ctx);
}
self.app.ui(ui);
// eframe's web runner ignores `ViewportCommand::Title`: name the open photo in the page title
let title = self.app.page_title(self.files.is_dirty() || self.flushing.get());
if title != self.title
&& let Some(doc) = window().and_then(|w| w.document())
{
doc.set_title(&title);
self.title = title;
}
// The benchmark is a UI-phase step: a second layout pass or a hidden tick must not count a frame.
if ui.ctx().current_pass_index() == 0 {
if let Some(b) = self.bench.as_mut()
&& let Some(report) = b.step(&mut self.app)
{
log::info!("lightcraft-bench {report}");
if let Some((alive, ready, remote, inline)) = self.workers.as_ref().map(Workers::stats) {
log::info!("lightcraft-workers {{\"alive\":{alive},\"ready\":{ready},\"remote_jobs\":{remote},\"inline_jobs\":{inline}}}");
}
set_status(&format!("bench: {report}"));
self.bench = None;
}
if self.bench.is_some() {
ctx.request_repaint();
}
}
if !self.first_frame_logged && !self.app.widgets.is_empty() {
self.first_frame_logged = true;
log::info!("lightcraft first UI frame at {:.0} ms after navigation start", perf_now());
+1 -1
View File
@@ -522,7 +522,7 @@ mod tests {
}
};
frame(&mut watch, &mut index); // an empty filtered view: no render request yet
cache.clear(); // File ▸ Clear Preview Cache
cache.clear(); // File ▸ Discard Preview Cache
frame(&mut watch, &mut index);
// the filter is removed: the first thumbnail request (`Workers::try_start`)
gone.extend(watch.observe(&cache, cache.generation(), &mut index));
+6 -1
View File
@@ -23,7 +23,9 @@ lightcraft-sysmem = { workspace = true }
lightcraft-ui-egui = { workspace = true }
lightcraft-codecs = { workspace = true }
lightcraft-raster = { workspace = true }
eframe = { workspace = true, features = ["default_fonts", "wgpu", "glow", "accesskit", "wayland", "x11"] }
# `clipboard` is a separate eframe feature in 0.37: without it egui-winit simulates an in-process
# clipboard and cut/copy/paste, RequestPaste and the Wayland PRIMARY path are unavailable.
eframe = { workspace = true, features = ["default_fonts", "clipboard", "wgpu", "glow", "accesskit", "wayland", "x11"] }
egui = { workspace = true }
serde_json = { workspace = true }
chrono = { workspace = true }
@@ -53,3 +55,6 @@ objc2-foundation = { workspace = true }
[dev-dependencies]
# tests record why they skip (`cargo xtask ci` lists the skips)
lightcraft-testkit = { workspace = true }
[target.'cfg(windows)'.dependencies]
craft-update = { workspace = true }
+27 -11
View File
@@ -240,11 +240,17 @@ mod tests {
}
impl Connection {
fn new(open: &Arc<AtomicUsize>, idle: Duration) -> Self {
/// `before` acts on the client before the worker starts reading, so what it sends is
/// already there however late this thread gets scheduled.
fn new(open: &Arc<AtomicUsize>, idle: Duration, before: impl FnOnce(&mut TcpStream)) -> Self {
let listener = TcpListener::bind(("127.0.0.1", 0)).unwrap();
let client = TcpStream::connect(listener.local_addr().unwrap()).unwrap();
let mut client = TcpStream::connect(listener.local_addr().unwrap()).unwrap();
client.set_read_timeout(Some(Duration::from_secs(3))).unwrap();
let (stream, _) = listener.accept().unwrap();
// Small test messages must not wait for Nagle's algorithm or a delayed ACK.
client.set_nodelay(true).unwrap();
stream.set_nodelay(true).unwrap();
before(&mut client);
let (tx, requests) = channel();
let worker = spawn_connection(stream, tx, egui::Context::default(), Arc::clone(open), idle).unwrap();
Self { client: BufReader::new(client), requests, worker: Some(worker) }
@@ -271,9 +277,8 @@ mod tests {
fn idle_connections_release_their_slots_without_dispatching() {
for input in ["", "{\"method\":\"engine.execute\""] {
let open = Arc::new(AtomicUsize::new(0));
let mut connection = Connection::new(&open, Duration::from_millis(100));
let mut connection = Connection::new(&open, Duration::from_millis(100), |c| c.write_all(input.as_bytes()).unwrap());
assert_eq!(open.load(Ordering::SeqCst), 1);
connection.client.get_mut().write_all(input.as_bytes()).unwrap();
let mut reply = String::new();
let closed = connection.client.read_to_string(&mut reply);
assert!(closed.is_ok(), "idle connection stayed open: {closed:?}");
@@ -285,8 +290,7 @@ mod tests {
assert!(reply["error"].as_str().unwrap().contains("idle read timed out"));
// A subsequent connection is admitted through the same accounting path.
let mut next = Connection::new(&open, Duration::from_secs(1));
next.client.get_mut().shutdown(std::net::Shutdown::Write).unwrap();
let mut next = Connection::new(&open, Duration::from_secs(1), |c| c.shutdown(std::net::Shutdown::Write).unwrap());
let mut eof = String::new();
next.client.read_to_string(&mut eof).unwrap();
next.join();
@@ -298,21 +302,32 @@ mod tests {
#[test]
fn persistent_requests_and_slow_replies_outlive_the_idle_read_timeout() {
let open = Arc::new(AtomicUsize::new(0));
let mut connection = Connection::new(&open, Duration::from_millis(100));
let request = |id: u64| format!("{}\n", json!({"id": id, "method": "engine.commands"}));
// Each request reaches the socket before the worker reads it (the next one while the worker
// waits for the reply), so only the reply waits outlast the idle timeout, however slowly
// this thread runs.
let mut connection = Connection::new(&open, Duration::from_millis(100), |c| c.write_all(request(1).as_bytes()).unwrap());
for id in [1, 2] {
writeln!(connection.client.get_mut(), "{}", json!({"id": id, "method": "engine.commands"})).unwrap();
let req = connection.requests.recv_timeout(Duration::from_secs(3)).unwrap();
let req = connection.requests.recv_timeout(Duration::from_secs(10)).unwrap();
assert_eq!(req.method, "engine.commands");
assert!(req.take());
if id == 1 {
connection.client.get_mut().write_all(request(2).as_bytes()).unwrap();
} else {
connection.client.get_mut().shutdown(std::net::Shutdown::Write).unwrap();
}
// A dispatched command waits for its reply, not for socket input.
std::thread::sleep(Duration::from_millis(250));
req.reply.send(json!({"ok": true, "result": id})).unwrap();
if id == 2 {
// The worker must reach EOF even before the client reads its final reply.
connection.join();
}
let mut reply = String::new();
connection.client.read_line(&mut reply).unwrap();
let reply: Value = serde_json::from_str(&reply).unwrap();
assert_eq!(reply, json!({"id": id, "ok": true, "result": id}));
}
connection.client.get_mut().shutdown(std::net::Shutdown::Write).unwrap();
let mut eof = String::new();
connection.client.read_to_string(&mut eof).unwrap();
connection.join();
@@ -327,7 +342,8 @@ mod tests {
let (req, rrx) = ControlRequest::new("ui.inspect", json!({}));
let t0 = std::time::Instant::now();
let r = await_reply(&rrx, &req.state, || 7, || {}, Duration::from_millis(50), Duration::from_secs(10));
assert!(t0.elapsed() < Duration::from_secs(2));
// the frame window (50 ms), not the 10 s timeout; the margin is for a slow machine
assert!(t0.elapsed() < Duration::from_secs(5), "{:?}", t0.elapsed());
assert_eq!(r["ok"], false);
assert!(r["error"].as_str().unwrap().contains("not visible"), "{r}");
assert!(!req.take(), "the app does not run it once it shows up");
+134 -23
View File
@@ -31,22 +31,14 @@ pub fn check(app: &str) -> Result<(), String> {
/// Open `path` in `app` (see [`check`]). Spawned, never waited for: the editor runs alongside.
pub fn open_with(path: &str, app: &str) -> Result<(), String> {
let mut c = match launcher(app)? {
Launcher::Default if cfg!(target_os = "macos") => {
let mut c = Command::new("open");
c.arg(path);
c
}
Launcher::Default if cfg!(target_os = "macos") => return run_open(&[path]),
Launcher::Default if cfg!(target_os = "windows") => windows_default_command(path),
Launcher::Default => {
let mut c = Command::new("xdg-open");
c.arg(path);
c
}
Launcher::MacApp(app) => {
let mut c = Command::new("open");
c.args(["-a", &app, path]);
c
}
Launcher::MacApp(app) => return run_open(&["-a", &app, path]),
Launcher::Program(p) => {
let mut c = Command::new(p);
c.arg(path);
@@ -61,6 +53,22 @@ pub fn open_with(path: &str, app: &str) -> Result<(), String> {
c.spawn().map(|_| ()).map_err(|e| e.to_string())
}
/// macOS: run `open` and wait for it (it returns as soon as LaunchServices has handed the file
/// over), so an application it can't find or start is reported instead of silently doing nothing.
fn run_open(args: &[&str]) -> Result<(), String> {
let out = Command::new("open").args(args).output().map_err(|e| e.to_string())?;
open_result(out.status.success(), &String::from_utf8_lossy(&out.stderr))
}
/// What `open` said, as a result: its own message when it failed.
fn open_result(ok: bool, stderr: &str) -> Result<(), String> {
if ok {
return Ok(());
}
let msg = stderr.lines().map(str::trim).find(|l| !l.is_empty()).unwrap_or("the application couldn't be started");
Err(msg.to_string())
}
/// Open every file in `paths` with one start of `app`, as a file manager hands over a selection:
/// an editor that keeps a single window (DxO PhotoLab) loses the files that arrive while it is
/// still starting when it is started once per file. The system default opens them one by one
@@ -119,10 +127,15 @@ fn launcher(app: &str) -> Result<Launcher, String> {
fn find_photocraft() -> Option<Launcher> {
let env = |k: &str| std::env::var_os(k).map(|v| v.to_string_lossy().to_string());
if cfg!(target_os = "macos") {
// LaunchServices finds the app wherever it is; these are only the usual places
// the usual places, then wherever Spotlight knows it (by bundle id or bundle name)
let home = env("HOME").map(|h| Path::new(&h).join("Applications/PhotoCraft.app"));
let found = [Some(PathBuf::from("/Applications/PhotoCraft.app")), home].into_iter().flatten().find(|p| p.is_dir());
return Some(Launcher::MacApp(found.map_or_else(|| PHOTOCRAFT.to_string(), |p| p.to_string_lossy().to_string())));
let usual = [Some(PathBuf::from("/Applications/PhotoCraft.app")), home].into_iter().flatten().find(|p| p.is_dir());
let spotlight = || {
let query = format!("kMDItemCFBundleIdentifier == '{PHOTOCRAFT_ID}' || kMDItemFSName == 'PhotoCraft.app'");
let out = Command::new("mdfind").arg(query).output().ok().filter(|o| o.status.success())?;
mdfind_app(&String::from_utf8_lossy(&out.stdout))
};
return usual.or_else(spotlight).map(|p| Launcher::MacApp(p.to_string_lossy().to_string()));
}
if cfg!(target_os = "windows") {
let install_dir = reg_query(r"HKLM\Software\PhotoCraft", Some("InstallDir"));
@@ -130,16 +143,34 @@ fn find_photocraft() -> Option<Launcher> {
return windows_candidates(install_dir, app_path, env).into_iter().find(|p| p.is_file()).map(Launcher::Program);
}
// Linux, FreeBSD: on the PATH (.deb, .rpm, tarball), a desktop entry (AppImage, which
// registers one on first run), else the Flatpak
let on_path =
env("PATH").into_iter().flat_map(|p| std::env::split_paths(&p).collect::<Vec<_>>()).map(|d| d.join("photocraft")).find(|p| p.is_file());
// registers one on first run; its Exec a path or a name on the PATH), an AppImage where people
// keep them, else the Flatpak
let path_dirs: Vec<PathBuf> = env("PATH").into_iter().flat_map(|p| std::env::split_paths(&p).collect::<Vec<_>>()).collect();
let on_path = |name: &Path| resolve_program(name, &path_dirs);
let from_entry = || {
desktop_entry_dirs(env).into_iter().find_map(|d| {
let text = std::fs::read_to_string(d.join(format!("{PHOTOCRAFT_ID}.desktop"))).ok()?;
desktop_exec(&text).filter(|p| p.is_file())
desktop_exec(&text)
// a wrapper (`Exec=env VAR=1 /opt/PhotoCraft/photocraft %F`) would be started with the photo only:
// skip it and look further
.filter(|p| p.file_name().is_some_and(|n| n.to_string_lossy().to_lowercase().contains("photocraft")))
.and_then(|p| on_path(&p))
})
};
if let Some(p) = on_path.or_else(from_entry) {
let appimage = || {
let home = env("HOME")?;
let dirs = [Path::new(&home).join("Applications"), Path::new(&home).join(".local/bin")];
// both folders, so the newest version wins whichever it is in
let mut found: Vec<PathBuf> = dirs
.iter()
.filter_map(|d| std::fs::read_dir(d).ok())
.flat_map(|rd| rd.flatten().map(|e| e.path()).filter(|p| is_photocraft_appimage(p) && p.is_file()).collect::<Vec<_>>())
.collect();
// the newest version by name (1.10 after 1.9)
found.sort_by_key(|p| version_key(&p.file_name().map(|n| n.to_string_lossy().to_lowercase()).unwrap_or_default()));
found.pop()
};
if let Some(p) = on_path(Path::new("photocraft")).or_else(from_entry).or_else(appimage) {
return Some(Launcher::Program(p));
}
let flatpak = Command::new("flatpak").args(["info", PHOTOCRAFT_ID]).output().is_ok_and(|o| o.status.success());
@@ -205,10 +236,44 @@ fn desktop_exec(entry: &str) -> Option<PathBuf> {
Some(rest) => rest.split('"').next()?,
None => exec.split_whitespace().next()?,
};
// a bare name (`photocraft %F`) was already looked for on the PATH. Desktop entries are a
// freedesktop (POSIX) format: absolute means a leading `/` on every platform the parser runs on
// (`Path::is_absolute` wants a drive letter on Windows, where the tests run too)
program.starts_with('/').then(|| PathBuf::from(program))
// a path, or a bare name (`photocraft %F`) the caller looks for on the PATH
(!program.is_empty()).then(|| PathBuf::from(program))
}
/// A program from a desktop entry or the PATH: a path as it is (if it is a file), a bare name
/// looked for in `dirs`. A leading `/` counts as absolute on every platform (desktop entries are
/// a POSIX format; `Path::is_absolute` wants a drive letter on Windows, where the tests run too).
fn resolve_program(name: &Path, dirs: &[PathBuf]) -> Option<PathBuf> {
if name.is_absolute() || name.starts_with("/") {
return Some(name.to_path_buf()).filter(|p| p.is_file());
}
dirs.iter().map(|d| d.join(name)).find(|p| p.is_file())
}
/// A sort key that orders the digit runs in a name by value (`1.9` before `1.10`).
fn version_key(name: &str) -> Vec<(u64, String)> {
let mut out = Vec::new();
let mut rest = name;
while !rest.is_empty() {
let digits = rest.len() - rest.trim_start_matches(|c: char| c.is_ascii_digit()).len();
let (num, tail) = rest.split_at(digits);
let text_len = tail.find(|c: char| c.is_ascii_digit()).unwrap_or(tail.len());
let (text, next) = tail.split_at(text_len);
out.push((num.parse().unwrap_or(u64::MAX), text.to_string()));
rest = next;
}
out
}
/// An AppImage of PhotoCraft by its file name (`PhotoCraft-1.2-x86_64.AppImage`, any case).
fn is_photocraft_appimage(p: &Path) -> bool {
let name = p.file_name().map(|n| n.to_string_lossy().to_lowercase()).unwrap_or_default();
name.contains("photocraft") && name.ends_with(".appimage")
}
/// The first application bundle in `mdfind` output (one path per line).
fn mdfind_app(output: &str) -> Option<PathBuf> {
output.lines().map(str::trim).find(|l| l.ends_with(".app")).map(PathBuf::from)
}
#[cfg(test)]
@@ -275,13 +340,59 @@ mod tests {
let appimage = "[Desktop Entry]\nName=PhotoCraft\nExec=\"/home/simon/Apps/photocraft-1.2-linux-x86_64.AppImage\" %F\n";
assert_eq!(desktop_exec(appimage), Some(PathBuf::from("/home/simon/Apps/photocraft-1.2-linux-x86_64.AppImage")));
assert_eq!(desktop_exec("Exec=/opt/photocraft/photocraft %F\n"), Some(PathBuf::from("/opt/photocraft/photocraft")));
assert_eq!(desktop_exec("Exec=photocraft %F\nTryExec=photocraft\n"), None, "a bare name is a PATH lookup");
assert_eq!(desktop_exec("Exec=photocraft %F\nTryExec=photocraft\n"), Some(PathBuf::from("photocraft")), "a bare name, for a PATH lookup");
assert_eq!(desktop_exec("[Desktop Entry]\n"), None);
let dirs = desktop_entry_dirs(|k| (k == "HOME").then(|| "/home/simon".to_string()));
assert_eq!(dirs[0], PathBuf::from("/home/simon/.local/share/applications"));
assert!(dirs.contains(&PathBuf::from("/usr/share/applications")));
}
#[test]
fn a_bare_exec_name_is_found_on_the_path() {
let a = std::env::temp_dir().join(format!("lc-editors-a-{}", std::process::id()));
let b = std::env::temp_dir().join(format!("lc-editors-b-{}", std::process::id()));
std::fs::create_dir_all(&a).unwrap();
std::fs::create_dir_all(&b).unwrap();
std::fs::write(b.join("photocraft"), b"").unwrap();
let dirs = vec![a.clone(), b.clone()];
assert_eq!(resolve_program(Path::new("photocraft"), &dirs), Some(b.join("photocraft")));
assert_eq!(resolve_program(Path::new("nothere"), &dirs), None);
let abs = b.join("photocraft");
assert_eq!(resolve_program(&abs, &[]), Some(abs.clone()), "a path needs no PATH");
assert_eq!(resolve_program(&b.join("missing"), &dirs), None);
let _ = std::fs::remove_dir_all(&a);
let _ = std::fs::remove_dir_all(&b);
}
#[test]
fn appimage_versions_sort_numerically() {
let mut names = ["photocraft-1.10-x86_64.appimage", "photocraft-1.9-x86_64.appimage", "photocraft-1.2-x86_64.appimage"];
names.sort_by_key(|n| version_key(n));
assert_eq!(names[2], "photocraft-1.10-x86_64.appimage");
assert_eq!(names[0], "photocraft-1.2-x86_64.appimage");
}
#[test]
fn a_failed_open_is_an_error() {
assert!(open_result(true, "").is_ok());
assert!(open_result(false, "Unable to find application\n").is_err());
assert!(open_result(false, "").is_err(), "even when it says nothing");
}
#[test]
fn odd_detection_input_does_not_panic() {
for text in ["", "Exec=", "Exec=\"", "Exec=\"\"", "\u{0}\u{fffd}", "Exec= %F", "[Desktop Entry]\nExec=\u{e9}\u{301}"] {
let _ = desktop_exec(text);
let _ = mdfind_app(text);
let _ = reg_value(text);
}
for p in ["", "/", ".AppImage", "\u{e9}.appimage", "a/../b"] {
let _ = is_photocraft_appimage(Path::new(p));
}
let _ = desktop_entry_dirs(|_| None);
let _ = windows_candidates(None, Some("\"".into()), |_| None);
}
#[test]
fn other_editors_are_used_as_named() {
assert_eq!(launcher("").unwrap(), Launcher::Default);
+103 -24
View File
@@ -3,8 +3,10 @@
//! Usage: `lightcraft [--library DIR | --memory] [--no-demo] [--control <port>] [files or folders…]`
//!
//! The library (catalog, presets, thumbnail cache) lives in `--library DIR`, else
//! `$LIGHTCRAFT_LIBRARY`, else the library last opened with Settings → Open Library…, else
//! `~/Pictures/LightCraft Library`; a new library starts with the
//! `$LIGHTCRAFT_LIBRARY`, else — with Settings ▸ General ▸ At launch set to *Ask me*, or Alt/Option
//! held as the window appears — the one chosen in a list of the recent libraries, else the library
//! last opened, else `~/Pictures/LightCraft Library`
//! ([`lightcraft_ui_egui::panels::library_problem::launch_plan`]); a new library starts with the
//! procedural demo photos unless `--no-demo` or files are given. Files and folders on the command
//! line are imported (duplicates are skipped). `--memory` runs an in-memory session that writes
//! nothing (demo photos unless files are given; used by the README showcase scripts). A library
@@ -39,7 +41,7 @@ mod window_gpu;
use dialog_filter::FileDialogExt as _;
use lightcraft_engine::Session;
use lightcraft_ui_egui::panels::library_problem::LibraryProblem;
use lightcraft_ui_egui::panels::library_problem::{Launch, LibraryProblem};
use lightcraft_ui_egui::{LightcraftApp, Services, UiState};
/// Reverse-DNS app id: Wayland app id, `.desktop` file and hicolor icon name.
@@ -73,8 +75,10 @@ impl eframe::App for App {
self.0.logic(ctx);
self.1.tick(&mut self.0, ctx);
}
fn raw_input_hook(&mut self, _ctx: &egui::Context, raw: &mut egui::RawInput) {
self.0.raw_input_hook(raw);
/// Forwards the host's raw-input pass to the app's hook before egui processes it, keeping
/// modal/prompt input barriers and queued synthetic or dropped input ahead of the panels.
fn raw_input_hook(&mut self, ctx: &egui::Context, raw: &mut egui::RawInput) {
self.0.raw_input_hook(ctx, raw);
}
fn ui(&mut self, ui: &mut egui::Ui, frame: &mut eframe::Frame) {
self.0.perf.host_frame_ms = frame.info().cpu_usage.map(|s| f64::from(s) * 1000.0);
@@ -363,7 +367,7 @@ struct PrefsWriter {
impl PrefsWriter {
fn save(&mut self, app: &LightcraftApp) -> Result<(), String> {
self.save_ui(&app.ui, app.library_problem.is_some())
self.save_ui(&app.ui, app.library_problem.as_ref().is_some_and(LibraryProblem::saves_nothing))
}
/// Write `ui` if it changed since the last write; `temporary` (the library couldn't be
@@ -402,15 +406,26 @@ impl PrefsWriter {
}
}
/// Per-frame host tick: records the window's placement while visible, asks for a repaint when
/// a remembered size is due, and saves settings/library at most every three seconds or
/// immediately after the library path changes; a failed save is reported once and retried.
fn tick(&mut self, app: &mut LightcraftApp, ctx: &egui::Context) {
// (asked for before the input is read: the context is not asked from inside its own lock)
let zoom = ctx.zoom_factor();
let (now, seen) = ctx.input(|i| (i.time, lightcraft_ui_egui::window_place::Seen::of(i.viewport(), i.content_rect(), zoom)));
// The monotonic app clock: `i.time` stands still while the window is hidden.
let now = app.tick_now();
let (hidden, seen) = ctx.input(|i| {
let hidden = i.viewport().visible() == Some(false);
(hidden, lightcraft_ui_egui::window_place::Seen::of(i.viewport(), i.content_rect(), zoom))
});
// how the window is, to open like that next time (written with the settings below). What
// the settings hold is checked first: a script may have set it (`ui.set`)
// the settings hold is checked first: a script may have set it (`ui.set`). A hidden
// `run_logic` pass has no fresh content rect: keep the last known placement.
let remembered = app.ui.window.and_then(lightcraft_ui_egui::window_place::WindowPlace::checked);
let at = self.window_since.get_or_insert_with(std::time::Instant::now).elapsed().as_secs_f64();
app.ui.window = self.window.note(remembered, &seen, at);
if !hidden {
app.ui.window = self.window.note(remembered, &seen, at);
}
if let Some(wait) = self.window.waits(at) {
// (a size is only remembered once the window has rested at it: look again then, though
// nothing else asks for a frame)
@@ -704,9 +719,28 @@ fn services(cc: &eframe::CreationContext<'_>, log_file: Option<&std::path::Path>
.save_file()
.map(|p| p.to_string_lossy().to_string())
})),
pick_print_templates: Some(Box::new(|| {
file_dialog()
.set_title(lightcraft_ui_egui::i18n::tr("Import Print Templates"))
.add_filter_nocase(lightcraft_ui_egui::i18n::tr("Print Templates"), &[lightcraft_engine::print::template::FILE_EXT])
.pick_files()
.unwrap_or_default()
.into_iter()
.map(|p| p.to_string_lossy().to_string())
.collect()
})),
save_print_template: Some(Box::new(|name: &str| {
file_dialog()
.set_title(lightcraft_ui_egui::i18n::tr("Export Print Template"))
.add_filter_nocase(lightcraft_ui_egui::i18n::tr("Print Templates"), &[lightcraft_engine::print::template::FILE_EXT])
.set_file_name(name)
.save_file()
.map(|p| p.to_string_lossy().to_string())
})),
// atomic (temp file + sync + rename): a failed write never leaves a truncated file
write_shared: Some(std::sync::Arc::new(lightcraft_engine::export::write_file)),
write: Some(Box::new(lightcraft_engine::export::write_file)),
write_new: Some(Box::new(lightcraft_engine::export::write_file_new)),
png: Some(Box::new(|img: &lightcraft_raster::Rgba8| {
lightcraft_codecs::encode_png(&lightcraft_codecs::EncodeImage::rgba8(img), &lightcraft_codecs::EncodeMeta::default()).unwrap_or_default()
})),
@@ -718,9 +752,13 @@ fn services(cc: &eframe::CreationContext<'_>, log_file: Option<&std::path::Path>
}
/// The library session with the folders face models (`<config>/models`) and slideshow templates
/// (`<config>/slideshow-templates`) are kept in.
fn open_session(in_memory: bool, dir: Option<std::path::PathBuf>, seed_demo: bool) -> (Session, Option<LibraryProblem>) {
let (mut s, problem) = open_library_session(in_memory, dir, seed_demo);
/// (`<config>/slideshow-templates`) are kept in. With [`Launch::Ask`] no library is opened yet:
/// the window lists the recent ones.
fn open_session(in_memory: bool, launch: &Launch, seed_demo: bool) -> (Session, Option<LibraryProblem>) {
let (mut s, problem) = match launch {
Launch::Ask => (Session::new().with_fs().with_default_denoise_models().with_system_clock(), None),
Launch::Open { dir, .. } => open_library_session(in_memory, dir.clone(), seed_demo),
};
s.face_models_dir = lightcraft_engine::config::default_face_models_dir();
s.slide_templates_dir = lightcraft_engine::config::config_dir().map(|d| d.join(lightcraft_engine::slide_template::DIR_NAME));
// Control requests are answered on the UI thread: Add to Apple Photos must not wait there
@@ -769,7 +807,9 @@ lightcraft — photo library + raw developer
USAGE: lightcraft [OPTIONS] [FILES/FOLDERS…] (files and folders are imported)
OPTIONS:
--library DIR open (or create) this library (default: ~/Pictures/LightCraft Library; env LIGHTCRAFT_LIBRARY)
--library DIR open (or create) this library (env LIGHTCRAFT_LIBRARY; default: the last library opened, or
a list of the recent ones with Settings > General > At launch: Ask me or Alt/Option held,
else ~/Pictures/LightCraft Library)
--no-demo don't seed a new library with the procedural demo photos
--memory throwaway in-memory session (alias --demo); nothing is saved
--control PORT serve the JSON-lines control channel on 127.0.0.1:PORT (env LIGHTCRAFT_CONTROL_PORT;
@@ -841,6 +881,11 @@ fn panic_log_path_with(env: impl Fn(&str) -> Option<std::ffi::OsString>, temp: s
}
fn main() -> eframe::Result {
// Windows: the update helper is a copy of this program started with `--finish-update`.
#[cfg(windows)]
if let Some(result) = craft_update::self_update::finish(&craft_update::suite::LIGHTCRAFT, &std::env::args_os().skip(1).collect::<Vec<_>>()) {
std::process::exit(i32::from(result.is_err()));
}
// First, so every start-up record is kept for the log file (`logging`).
let logger = logging::install();
lightcraft_engine::guard::install_hook(panic_log_path_with(|name| std::env::var_os(name), std::env::temp_dir()));
@@ -900,15 +945,15 @@ fn main() -> eframe::Result {
if let Some(ui) = &prefs {
lightcraft_ui_egui::i18n::set_language(ui.language);
}
// --library, else the library last opened from Settings, else the default location
let library_dir = library_dir.or_else(|| {
prefs
.as_ref()
.map(|u| u.settings.library_path.clone())
.filter(|p| !p.is_empty() && std::env::var_os("LIGHTCRAFT_LIBRARY").is_none())
.map(Into::into)
});
let library_dir = library_dir.or_else(lightcraft_engine::library::default_dir);
// --library, else LIGHTCRAFT_LIBRARY, else the list of recent libraries if Settings say Ask
// me, else the library last opened from Settings, else the default location
let launch = lightcraft_ui_egui::panels::library_problem::launch_plan(
in_memory,
library_dir,
std::env::var_os("LIGHTCRAFT_LIBRARY").filter(|p| !p.is_empty()).map(Into::into),
prefs.as_ref().map(|u| &u.settings),
lightcraft_engine::library::default_dir(),
);
// GPU compute: off if the preference says so, or if the last launch died creating the device
// (issue #136) — before anything can create it
let gpu_crash = gpu_crash_check(in_memory).or_else(|| gpu_lost_check_at(gpu_lost_marker_path().as_deref(), in_memory));
@@ -961,7 +1006,7 @@ fn main() -> eframe::Result {
// the window's renderer is up (eframe calls this after creating it)
win_files_app.started(window_renderer);
came_up_app.store(true, std::sync::atomic::Ordering::Relaxed);
let (mut session, problem) = open_session(in_memory, library_dir, seed_demo && files.is_empty());
let (mut session, mut problem) = open_session(in_memory, &launch, seed_demo && files.is_empty());
// AI masks: the SAM 3 checkpoint (facebook/sam3) in <config>/models/sam3, or LIGHTCRAFT_SAM3_DIR
// (never required: without it, AI masks offer to download it; see docs/ai-masks.md)
// and the user's own download locations, one base URL per line (LIGHTCRAFT_SAM3_MIRRORS too)
@@ -969,9 +1014,15 @@ fn main() -> eframe::Result {
// HDR display: SDR unless the surface, platform and display all allow HDR
hdr_display::report(cc.wgpu_render_state.as_ref().map(|r| r.target_format));
let mut app = LightcraftApp::new(session, services(cc, app_log_file.as_deref()));
app.set_startup_update_check(true);
if let Some(ui) = prefs {
app.ui = ui;
}
// the recent libraries to choose from (issue #1290), checked on a worker once shown
if launch == Launch::Ask {
problem = Some(LibraryProblem::choose_later(&app.ui.settings));
}
app.alt_asks_for_library = matches!(launch, Launch::Open { alt_asks: true, .. });
lightcraft_ui_egui::i18n::set_language(app.ui.language);
app.integrated_titlebar = cfg!(target_os = "macos");
app.notices.extend(prefs_warning);
@@ -1145,12 +1196,40 @@ mod tests {
/// adds to Apple Photos on a worker (headless sessions wait instead).
#[test]
fn the_desktop_session_adds_to_apple_photos_in_the_background() {
let (s, problem) = open_session(true, None, false);
let (s, problem) = open_session(true, &Launch::Open { dir: None, alt_asks: false }, false);
assert!(problem.is_none());
assert!(s.apple_photos_background);
assert!(!lightcraft_engine::Session::new().apple_photos_background);
}
/// Issue #1290: with Settings ▸ At launch ▸ Ask me nothing is opened (nor seeded with demo
/// photos) before the user chooses, and the settings are still saved while the list shows (a
/// removed entry is kept), unlike the failed-library window's temporary session.
#[test]
fn asking_at_launch_opens_nothing_and_still_saves_the_settings() {
let (s, problem) = open_session(false, &Launch::Ask, true);
assert!(problem.is_none() && s.library.is_none() && s.catalog.is_empty());
assert!(s.apple_photos_background, "the same desktop session as an opened one");
let d = dir("writer-ask");
let path = d.join("ui.json");
let mut app = LightcraftApp::new(s, Services::default());
app.ui.settings.recent_libraries = vec!["/gone/a".into(), "/gone/b".into()];
app.library_problem = Some(LibraryProblem::choose_later(&app.ui.settings));
let mut w = PrefsWriter { path: Some(path.clone()), ..Default::default() };
app.ui.settings.recent_libraries.remove(0);
w.save(&app).unwrap();
let saved: UiState = serde_json::from_slice(&std::fs::read(&path).unwrap()).unwrap();
assert_eq!(saved.settings.recent_libraries, ["/gone/b"]);
// the failed-library window still writes nothing
app.library_problem = Some(LibraryProblem::new("/gone/b", "not there"));
app.ui.settings.recent_libraries.clear();
w.save(&app).unwrap();
let saved: UiState = serde_json::from_slice(&std::fs::read(&path).unwrap()).unwrap();
assert_eq!(saved.settings.recent_libraries, ["/gone/b"]);
let _ = std::fs::remove_dir_all(&d);
}
/// Issue #103: a damaged ui.json (which holds the library location) is kept aside and
/// reported, not silently replaced by the defaults.
#[test]
+303 -88
View File
@@ -11,12 +11,19 @@
//! - while a text field has keyboard focus, accelerators without ⌘ (`G`, `1`, `Delete`…) and the
//! text-editing ones (⌘A/⌘C/⌘V/⌘X/⌘Z) are removed, so typing and text editing work; they come
//! back when the field loses focus.
//!
//! Menu events never pass through the egui shortcut handler. [`NativeMenu::update`] therefore runs
//! every queued command through [`lightcraft_ui_egui::menubar::run_native_command`]: while the
//! blocking library window or a quit/settings notice owns input it drops the command (Settings…
//! and About included), and only Quit stays available for the host's existing confirmation.
use std::collections::{HashMap, HashSet};
use std::sync::mpsc::{Receiver, channel};
use lightcraft_ui_egui::LightcraftApp;
use lightcraft_ui_egui::menubar::{MenuNode, menu_bar};
use lightcraft_ui_egui::menubar::{
MenuNode, menu_bar, native_item_presentation, native_menu_delivers as menu_delivers, native_menu_structure as structure_of,
};
use muda::accelerator::{Accelerator, Code, Modifiers};
use muda::{CheckMenuItem, Menu, MenuEvent, MenuItem, PredefinedMenuItem, Submenu};
use serde_json::Value;
@@ -39,18 +46,140 @@ enum Handle {
struct Item {
handle: Handle,
/// The submenu the item sits in, to swap the item when its accelerator has to go (macOS).
parent: Submenu,
cmd: String,
params: Value,
shortcut: Option<String>,
/// The accelerator the item's shortcut maps to.
accel: Option<Accelerator>,
/// The accelerator the native item has now: `accel`, or `None` while it yields.
current: Option<Accelerator>,
label: String,
enabled: bool,
checked: Option<bool>,
/// Whether the item may be replaced to drop its key. Not in the Help menu: a swap there left
/// the old row and added a second one (muda 0.21, macOS 27), so those keys stay.
swappable: bool,
}
impl Item {
/// The row's text as installed: `label` is already what the row shows (translated, with a
/// borrowed key's hint, `native_item_presentation`).
fn native_label(&self) -> String {
self.label.replace('&', "&&")
}
/// Puts `want` on the native item. muda's `set_accelerator(None)` does nothing on macOS (the
/// old key equivalent stays and the menu still takes the key), so there the item is replaced
/// by an identical one without an accelerator, at the same position and with the same id,
/// text, enabled and checked state. Only done when the accelerator changes (and again while a
/// native call keeps failing), never per frame otherwise.
fn set_accelerator(&mut self, want: Option<Accelerator>) {
if self.current == want {
return;
}
if cfg!(target_os = "macos") && want.is_none() && self.swappable {
// `current` follows the native item: it changes only when the swap went through, so a
// failed swap (the old item, key and all, is still there) is tried again
let done = self.replace_without_accelerator();
self.current = settled(self.current, want, done);
return;
}
let done = match &self.handle {
Handle::Plain(h) => h.set_accelerator(want),
Handle::Check(h) => h.set_accelerator(want),
}
.is_ok();
self.current = settled(self.current, want, done);
}
/// Replaces the native item by one without an accelerator. `self.handle` changes only when the
/// new item is attached; on failure the old item is still the one in the submenu.
fn replace_without_accelerator(&mut self) -> bool {
let (id, old): (_, &dyn muda::IsMenuItem) = match &self.handle {
Handle::Plain(h) => (h.id().clone(), h),
Handle::Check(h) => (h.id().clone(), h),
};
let Some(position) = self.parent.items().iter().position(|k| *k.id() == id) else { return false };
let new = match &self.handle {
Handle::Plain(_) => Handle::Plain(MenuItem::with_id(id, self.native_label(), self.enabled, None)),
Handle::Check(h) => Handle::Check(CheckMenuItem::with_id(id, self.native_label(), self.enabled, h.is_checked(), None)),
};
let new_ref: &dyn muda::IsMenuItem = match &new {
Handle::Plain(h) => h,
Handle::Check(h) => h,
};
if !swap_item(&self.parent, old, new_ref, position) {
return false;
}
self.handle = new;
true
}
}
/// The accelerator the native item has after trying to put `want` on it: `want` when the native
/// call worked, else what it had, so the caller sees the mismatch and tries again.
fn settled(had: Option<Accelerator>, want: Option<Accelerator>, native_call_worked: bool) -> Option<Accelerator> {
if native_call_worked { want } else { had }
}
/// Whether the per-frame accelerator sync has to run: the focus or recording state changed, or an
/// earlier sync left an item whose native accelerator is not the wanted one (a failed swap).
fn sync_needed(focus: bool, capturing: bool, synced_focus: bool, synced_capturing: bool, pending: bool) -> bool {
focus != synced_focus || capturing != synced_capturing || pending
}
/// The three submenu calls a swap needs, so the failure paths can be tested without a main-thread
/// `muda::Menu`.
trait SwapTarget {
type Row: ?Sized;
fn remove(&self, row: &Self::Row) -> bool;
fn insert(&self, row: &Self::Row, position: usize) -> bool;
fn append(&self, row: &Self::Row) -> bool;
}
impl SwapTarget for Submenu {
type Row = dyn muda::IsMenuItem;
fn remove(&self, row: &Self::Row) -> bool {
Submenu::remove(self, row).is_ok()
}
fn insert(&self, row: &Self::Row, position: usize) -> bool {
Submenu::insert(self, row, position).is_ok()
}
fn append(&self, row: &Self::Row) -> bool {
Submenu::append(self, row).is_ok()
}
}
/// Puts `new` where `old` is. Returns whether `new` is attached; when it is not, `old` is back in
/// the menu (at its position, else at the end), so the caller keeps `old` as its handle.
fn swap_item<T: SwapTarget + ?Sized>(target: &T, old: &T::Row, new: &T::Row, position: usize) -> bool {
if !target.remove(old) {
return false;
}
// (an item at the end beats a missing one)
if target.insert(new, position) || target.append(new) {
return true;
}
let _ = target.insert(old, position) || target.append(old);
false
}
/// The accelerator a native item has: none while a shortcut is being recorded, and none for the
/// keys a focused text field needs.
fn wanted_accelerator(accel: Option<Accelerator>, sc: &str, text_focus: bool, capturing: bool) -> Option<Accelerator> {
if capturing || (text_focus && yields_to_text(sc)) { None } else { accel }
}
pub struct NativeMenu {
menu: Menu,
items: HashMap<String, Item>,
/// Settings… and Quit in the application submenu: ordinary items, so they give up ⌘, and ⌘Q
/// to the shortcut recorder like the rest. (The system's own items there and in File/Window,
/// such as Hide ⌘H, Close Window ⌘W and Minimize ⌘M, are muda predefined items whose key
/// cannot be changed or swapped.)
app_items: Vec<Item>,
rx: Receiver<String>,
/// Structure of the installed menu (ids, kinds, submenu names); a change rebuilds it.
structure: String,
@@ -58,6 +187,8 @@ pub struct NativeMenu {
/// The keyboard shortcuts editor is recording a key: every accelerator is off so the key
/// press reaches it.
capturing: bool,
/// An item's native accelerator is not the wanted one (its swap or accelerator call failed): sync again next frame.
accelerators_pending: bool,
}
/// `Cmd+Shift+Z` → a muda accelerator (`None` for keys we leave to egui, e.g. Escape).
@@ -154,13 +285,6 @@ fn yields_to_text(sc: &str) -> bool {
!sc.contains("Cmd") && !sc.contains("Ctrl") || TEXT_EDIT.contains(&sc)
}
/// Whether AppKit hands this shortcut to the menu bar: only key presses with ⌘ or ⌃, and function
/// keys. A plain `C` or `⇧P` never reaches it (the window's view takes the key), so egui has to run
/// those even though their menu item shows the key.
fn menu_delivers(sc: &str) -> bool {
sc.split('+').any(|part| matches!(part, "Cmd" | "Ctrl") || part.strip_prefix('F').is_some_and(|n| n.parse::<u8>().is_ok()))
}
/// The shortcuts the menu bar runs itself, for `LightcraftApp::native_shortcuts`.
fn owned_by_menu<'a>(installed: impl Iterator<Item = &'a str>, text_focus: bool) -> HashSet<String> {
installed.filter(|sc| menu_delivers(sc) && !(text_focus && yields_to_text(sc))).map(str::to_string).collect()
@@ -171,34 +295,6 @@ fn label_text(s: &str) -> String {
lightcraft_ui_egui::i18n::tr(s).replace('&', "&&")
}
fn structure_of(bar: &[(String, Vec<MenuNode>)]) -> String {
fn walk(n: &[MenuNode], out: &mut String) {
for x in n {
match x {
MenuNode::Item { id, params, checked, shortcut, .. } => {
out.push_str(&MenuNode::key(id, params));
out.push(if checked.is_some() { 'c' } else { 'i' });
out.push_str(shortcut.as_deref().unwrap_or(""));
}
MenuNode::Separator => out.push('-'),
MenuNode::Submenu { label, children } => {
out.push('[');
out.push_str(label);
walk(children, out);
out.push(']');
}
}
out.push(';');
}
}
let mut s = format!("{};", lightcraft_ui_egui::i18n::language().code());
for (t, items) in bar {
s.push_str(t);
walk(items, &mut s);
}
s
}
/// Drop a trailing separator (left behind when an item moved to the app menu).
fn tidy_separators(mut nodes: Vec<MenuNode>) -> Vec<MenuNode> {
while matches!(nodes.last(), Some(MenuNode::Separator)) {
@@ -216,17 +312,28 @@ impl NativeMenu {
let _ = tx.send(e.id.0);
repaint.request_repaint();
}));
let mut m = NativeMenu { menu: Menu::new(), items: HashMap::new(), rx, structure: String::new(), text_focus: false, capturing: false };
let mut m = NativeMenu {
menu: Menu::new(),
items: HashMap::new(),
app_items: Vec::new(),
rx,
structure: String::new(),
text_focus: false,
capturing: false,
accelerators_pending: false,
};
m.rebuild(app);
app.native_menu = true;
m
}
/// Recreate native handles and shortcut ownership from the current shared menu model.
fn rebuild(&mut self, app: &mut LightcraftApp) {
let bar = menu_bar(app);
self.structure = structure_of(&bar);
self.structure = structure_of(&bar, &app.ui.settings.keymap);
self.menu = Menu::new();
self.items.clear();
self.app_items.clear();
// the application menu
let app_menu = Submenu::new("LightCraft", true);
@@ -246,6 +353,22 @@ impl NativeMenu {
&PredefinedMenuItem::separator(),
&quit,
]);
for (handle, cmd, shortcut, text) in [(&settings, SETTINGS, SETTINGS_KEY, "Settings…"), (&quit, QUIT, "Cmd+Q", "Quit LightCraft")] {
let accel = accelerator(shortcut);
self.app_items.push(Item {
handle: Handle::Plain(handle.clone()),
parent: app_menu.clone(),
swappable: true,
cmd: cmd.to_string(),
params: Value::Null,
shortcut: Some(shortcut.to_string()),
accel,
current: accel,
label: lightcraft_ui_egui::menubar::display_item_label(cmd, &Value::Null, text).to_string(),
enabled: true,
checked: None,
});
}
let _ = self.menu.append(&app_menu);
for (title, nodes) in &bar {
@@ -264,7 +387,9 @@ impl NativeMenu {
.cloned()
.collect();
let nodes = tidy_separators(nodes);
self.append_nodes(&sub, &nodes);
// Swapping an item in the Help menu leaves the old row in place and adds a second one
// (measured on macOS 27 with muda 0.21): the Help menu is not addressed row for row.
self.append_nodes(&sub, &nodes, &app.ui.settings.keymap, title != "Help");
if title == "File" {
// ⌘W, the system's own item
let _ = sub.append_items(&[
@@ -287,10 +412,13 @@ impl NativeMenu {
self.menu.init_for_nsapp();
self.text_focus = false;
self.capturing = false;
self.accelerators_pending = false;
self.publish_shortcuts(app);
}
fn append_nodes(&mut self, sub: &Submenu, nodes: &[MenuNode]) {
/// Install translated rows with their effective accelerators, preserving hint-only keys.
/// `swappable`: whether its items may be replaced to drop their key (see `Item::swappable`).
fn append_nodes(&mut self, sub: &Submenu, nodes: &[MenuNode], keymap: &lightcraft_ui_egui::shortcuts::Keymap, swappable: bool) {
for n in nodes {
match n {
MenuNode::Separator => {
@@ -298,7 +426,7 @@ impl NativeMenu {
}
MenuNode::Submenu { label, children } => {
let s = Submenu::new(label_text(label), true);
self.append_nodes(&s, children);
self.append_nodes(&s, children, keymap, swappable);
let _ = sub.append(&s);
}
MenuNode::Item { id, params, label, shortcut, enabled, checked } => {
@@ -306,26 +434,16 @@ impl NativeMenu {
if self.items.contains_key(&key) {
continue; // muda ids must be unique
}
let accel = shortcut.as_deref().filter(|s| !CONTEXTUAL.contains(s)).and_then(accelerator);
let (native_label, native_key) = native_item_presentation(keymap, id, params, label, shortcut.as_deref());
let accel = native_key.filter(|s| !CONTEXTUAL.contains(s)).and_then(accelerator);
let handle = match checked {
Some(c) => {
let it = CheckMenuItem::with_id(
key.clone(),
lightcraft_ui_egui::menubar::display_item_label(id, params, label).replace('&', "&&"),
*enabled,
*c,
accel,
);
let it = CheckMenuItem::with_id(key.clone(), native_label.replace('&', "&&"), *enabled, *c, accel);
let _ = sub.append(&it);
Handle::Check(it)
}
None => {
let it = MenuItem::with_id(
key.clone(),
lightcraft_ui_egui::menubar::display_item_label(id, params, label).replace('&', "&&"),
*enabled,
accel,
);
let it = MenuItem::with_id(key.clone(), native_label.replace('&', "&&"), *enabled, accel);
let _ = sub.append(&it);
Handle::Plain(it)
}
@@ -334,11 +452,14 @@ impl NativeMenu {
key,
Item {
handle,
parent: sub.clone(),
swappable,
cmd: id.clone(),
params: params.clone(),
shortcut: shortcut.clone(),
accel,
label: label.clone(),
current: accel,
label: native_label,
enabled: *enabled,
checked: *checked,
},
@@ -364,36 +485,43 @@ impl NativeMenu {
lightcraft_ui_egui::i18n::set_language(app.ui.language);
let mut quit = false;
while let Ok(key) = self.rx.try_recv() {
if key == QUIT {
// Resolve the native id to its command, then let the shared policy decide. Native menu
// events never reach `shortcuts::handle`, so this is the one guard for AppKit's
// accelerators and clicks: while the blocking library window or a quit/settings notice
// owns input, a queued command is dropped (Quit stays for the existing confirmation).
let (cmd, params) = if key == QUIT {
(key, serde_json::json!({}))
} else if key == "app.about" || key == SETTINGS {
(key, serde_json::json!({}))
} else if let Some(it) = self.items.get(&key) {
(it.cmd.clone(), it.params.clone())
} else {
continue;
};
if lightcraft_ui_egui::menubar::run_native_command(app, &cmd, params) == lightcraft_ui_egui::menubar::NativeCommand::Quit {
quit = true;
continue;
}
if key == "app.about" || key == SETTINGS {
let _ = app.run(&key, serde_json::json!({}));
continue;
}
if let Some(it) = self.items.get(&key) {
let (cmd, params) = (it.cmd.clone(), it.params.clone());
let _ = lightcraft_ui_egui::menubar::run_item(app, &cmd, params);
}
}
let bar = menu_bar(app);
if structure_of(&bar) != self.structure {
if structure_of(&bar, &app.ui.settings.keymap) != self.structure {
// (no early return: a rebuild installs every accelerator and resets the state they were synced
// to, so the sync below runs in this same frame and no key reaches the menu in between)
self.rebuild(app);
return quit;
}
fn walk(nodes: &[MenuNode], items: &mut HashMap<String, Item>) {
/// Synchronize live labels and states; accelerator ownership changes already triggered a rebuild.
fn walk(nodes: &[MenuNode], items: &mut HashMap<String, Item>, keymap: &lightcraft_ui_egui::shortcuts::Keymap) {
for n in nodes {
match n {
MenuNode::Submenu { children, .. } => walk(children, items),
MenuNode::Item { id, params, label, enabled, checked, .. } => {
MenuNode::Submenu { children, .. } => walk(children, items, keymap),
MenuNode::Item { id, params, label, shortcut, enabled, checked } => {
let Some(it) = items.get_mut(&MenuNode::key(id, params)) else { continue };
if it.label != *label {
let (native_label, _) = native_item_presentation(keymap, id, params, label, shortcut.as_deref());
if it.label != native_label {
match &it.handle {
Handle::Plain(h) => h.set_text(lightcraft_ui_egui::menubar::display_item_label(id, params, label).replace('&', "&&")),
Handle::Check(h) => h.set_text(lightcraft_ui_egui::menubar::display_item_label(id, params, label).replace('&', "&&")),
Handle::Plain(h) => h.set_text(native_label.replace('&', "&&")),
Handle::Check(h) => h.set_text(native_label.replace('&', "&&")),
}
it.label = label.clone();
it.label = native_label;
}
if it.enabled != *enabled {
match &it.handle {
@@ -414,24 +542,24 @@ impl NativeMenu {
}
}
}
walk(&bar.iter().flat_map(|(_, v)| v.clone()).collect::<Vec<_>>(), &mut self.items);
walk(&bar.iter().flat_map(|(_, v)| v.clone()).collect::<Vec<_>>(), &mut self.items, &app.ui.settings.keymap);
let focus = ctx.egui_wants_keyboard_input();
let capturing = app.recording_shortcut.is_some();
if focus != self.text_focus || capturing != self.capturing {
if sync_needed(focus, capturing, self.text_focus, self.capturing, self.accelerators_pending) {
self.text_focus = focus;
self.capturing = capturing;
for it in self.items.values() {
let mut pending = false;
for it in self.items.values_mut().chain(self.app_items.iter_mut()) {
let Some(sc) = it.shortcut.as_deref() else { continue };
if it.accel.is_none() {
continue;
}
let accel = if capturing || (focus && yields_to_text(sc)) { None } else { it.accel };
let _ = match &it.handle {
Handle::Plain(h) => h.set_accelerator(accel),
Handle::Check(h) => h.set_accelerator(accel),
};
let want = wanted_accelerator(it.accel, sc, focus, capturing);
it.set_accelerator(want);
pending |= it.current != want;
}
self.accelerators_pending = pending;
self.publish_shortcuts(app);
}
quit
@@ -454,15 +582,102 @@ mod tests {
let mut seen = std::collections::BTreeSet::new();
for language in Locale::ALL {
set_language(*language);
assert!(seen.insert(structure_of(&bar)), "{language:?} shares another language's menu structure key");
assert!(seen.insert(structure_of(&bar, &app.ui.settings.keymap)), "{language:?} shares another language's menu structure key");
}
set_language(Locale::ZhHant);
assert_eq!(label_text("File"), "檔案");
assert_eq!(label_text("Settings…"), "設定…");
assert_eq!(label_text("Quit LightCraft"), "結束 LightCraft");
set_language(Locale::En);
}
#[test]
fn accelerators_yield_to_text_focus_and_shortcut_recording() {
let g = accelerator("G");
let cmd_s = accelerator("Cmd+S");
assert_eq!(wanted_accelerator(g, "G", false, false), g);
assert_eq!(wanted_accelerator(g, "G", true, false), None, "typing a G in a field");
assert_eq!(wanted_accelerator(cmd_s, "Cmd+S", true, false), cmd_s, "⌘S still saves while typing");
assert_eq!(wanted_accelerator(cmd_s, "Cmd+S", false, true), None, "the editor records the key");
assert_eq!(wanted_accelerator(cmd_s, "Cmd+S", true, true), None, "recording wins over typing");
// the application menu's Settings… and Quit are tracked like the other items
for sc in [SETTINGS_KEY, "Cmd+Q"] {
let a = accelerator(sc);
assert!(a.is_some());
assert_eq!(wanted_accelerator(a, sc, true, false), a, "{sc} stays while typing");
assert_eq!(wanted_accelerator(a, sc, false, true), None, "{sc} goes while recording");
}
assert_eq!(wanted_accelerator(None, "Escape", false, false), None, "no accelerator stays none");
}
/// A submenu of numbers whose calls can be made to fail, to run `swap_item`'s failure paths.
/// Row 2 is the old item and row 9 its replacement.
#[derive(Default)]
struct Fake {
rows: std::cell::RefCell<Vec<u32>>,
fail_remove: bool,
/// The new item (9) cannot be inserted / appended.
reject_new_insert: bool,
reject_new_append: bool,
/// The old item (2) cannot be inserted back.
reject_old_insert: bool,
}
impl SwapTarget for Fake {
type Row = u32;
fn remove(&self, row: &u32) -> bool {
if self.fail_remove {
return false;
}
self.rows.borrow_mut().retain(|x| x != row);
true
}
fn insert(&self, row: &u32, position: usize) -> bool {
if (self.reject_new_insert && *row == 9) || (self.reject_old_insert && *row == 2) {
return false;
}
self.rows.borrow_mut().insert(position, *row);
true
}
fn append(&self, row: &u32) -> bool {
if self.reject_new_append && *row == 9 {
return false;
}
self.rows.borrow_mut().push(*row);
true
}
}
fn swap(fake: Fake) -> (bool, Vec<u32>) {
*fake.rows.borrow_mut() = vec![1, 2, 3];
let done = swap_item(&fake, &2, &9, 1);
(done, fake.rows.into_inner())
}
#[test]
fn a_swap_changes_the_item_only_when_the_new_one_is_attached() {
assert_eq!(swap(Fake::default()), (true, vec![1, 9, 3]), "in place");
assert_eq!(swap(Fake { fail_remove: true, ..Default::default() }), (false, vec![1, 2, 3]), "remove fails: the old item stays");
assert_eq!(swap(Fake { reject_new_insert: true, ..Default::default() }), (true, vec![1, 3, 9]), "insert fails: the new item goes last");
assert_eq!(
swap(Fake { reject_new_insert: true, reject_new_append: true, ..Default::default() }),
(false, vec![1, 2, 3]),
"neither works: the old item is back where it was"
);
assert_eq!(
swap(Fake { reject_new_insert: true, reject_new_append: true, reject_old_insert: true, ..Default::default() }),
(false, vec![1, 3, 2]),
"the old item cannot go back in place either: it is appended"
);
}
#[test]
fn a_failed_native_call_is_retried_while_the_state_holds() {
let g = accelerator("G");
assert_eq!(settled(g, None, true), None, "the swap worked");
assert_eq!(settled(g, None, false), g, "it failed: the native item still has its key");
assert_eq!(settled(None, g, false), None, "restoring a key failed");
assert!(!sync_needed(true, false, true, false, false), "nothing changed");
assert!(sync_needed(true, false, false, false, false), "focus changed");
assert!(sync_needed(false, true, false, false, false), "recording changed");
assert!(sync_needed(true, false, true, false, true), "a swap failed last time: try again with the state unchanged");
}
#[test]
fn shortcuts_map_to_accelerators() {
assert_eq!(accelerator("Cmd+Shift+Z"), Some(Accelerator::new(Modifiers::META | Modifiers::SHIFT, Code::KeyZ)));
+34 -13
View File
@@ -93,7 +93,6 @@ pub fn choose(files: &Files) -> Chosen {
let compiled = wgpu::Instance::enabled_backend_features();
let os = std::env::consts::OS;
let order = backend::window_renderer_order(forced, remembered, failed, os, compiled, true);
let mut notice = failed.map(|f| format!("The window could not start with its {} renderer last time.", f.name()));
let mut problems = Vec::new();
let chosen = if cfg!(windows) {
pick_probed(&order, &mut problems)
@@ -104,18 +103,7 @@ pub fn choose(files: &Files) -> Chosen {
Some((WindowRenderer::Wgpu(backend::window_backends()), None))
};
let (which, existing) = chosen.unwrap_or((WindowRenderer::Glow, None));
if !problems.is_empty() {
notice = Some(format!("{} {}", notice.unwrap_or_default(), problems.join(" ")).trim().to_string());
}
let notice = notice.map(|n| {
format!(
"{n} {app} now draws its window with {}. To choose another, start {app} with {var}=dx12, vulkan, gl or glow \
(see docs/gpu-pipeline.md → Troubleshooting).",
which.name(),
app = "LightCraft",
var = "LIGHTCRAFT_GPU_BACKEND",
)
});
let notice = user_notice(failed, forced, &problems, which);
log::info!("window: renderer {} (order {:?})", which.name(), order.iter().map(|r| r.name()).collect::<Vec<_>>());
let mut wgpu_options = base_options();
let renderer = match (which, existing) {
@@ -134,6 +122,25 @@ pub fn choose(files: &Files) -> Chosen {
Chosen { renderer, wgpu_options, which, notice }
}
/// What to tell the user about the window's renderer. A renderer that failed its probe is skipped
/// silently (it is logged, and the one that worked is remembered): the app works, so a pop-up with
/// wgpu's error only alarms (#1624). Shown: the last start died, or the forced renderer failed.
fn user_notice(failed: Option<WindowRenderer>, forced: Option<WindowRenderer>, problems: &[String], which: WindowRenderer) -> Option<String> {
let mut notice = failed.map(|f| format!("The window could not start with its {} renderer last time.", f.name()));
if forced.is_some() && !problems.is_empty() {
notice = Some(format!("{} {}", notice.unwrap_or_default(), problems.join(" ")).trim().to_string());
}
notice.map(|n| {
format!(
"{n} {app} now draws its window with {}. To choose another, start {app} with {var}=dx12, vulkan, gl or glow \
(see docs/gpu-pipeline.md → Troubleshooting).",
which.name(),
app = "LightCraft",
var = "LIGHTCRAFT_GPU_BACKEND",
)
})
}
/// eframe's defaults with the app's backends and DX12 shader compiler (issues #136, #471).
pub fn base_options() -> WgpuConfiguration {
let mut c = WgpuConfiguration::default();
@@ -269,6 +276,20 @@ mod tests {
let _ = std::fs::remove_dir_all(&d);
}
/// #1624: a renderer skipped by the probe (DX12 → Vulkan) is no pop-up; a died start or a
/// failed forced renderer still is.
#[test]
fn probe_fallback_is_silent() {
let (dx, vk) = (WindowRenderer::Wgpu(Backends::DX12), WindowRenderer::Wgpu(Backends::VULKAN));
let problems = vec!["The dx12 renderer failed (Validation Error).".to_string()];
assert_eq!(user_notice(None, None, &problems, vk), None);
assert_eq!(user_notice(None, None, &[], vk), None);
let forced = user_notice(None, Some(dx), &problems, vk).unwrap();
assert!(forced.starts_with("The dx12 renderer failed") && forced.contains("now draws its window with vulkan"), "{forced}");
let died = user_notice(Some(dx), None, &problems, vk).unwrap();
assert!(died.contains("last time") && !died.contains("Validation Error"), "{died}");
}
/// The crash report: a pipeline that fails validation must come back as an error, never as
/// wgpu's default panic. Needs an adapter; skipped where there is none (CI without a GPU).
#[test]
+9 -1
View File
@@ -8,6 +8,10 @@ covers a whole directory), or if a licence file referenced in the Licence column
| Path | Asset | Creator | Source | Licence | Added | Modifications |
|---|---|---|---|---|---|---|
| `epaint_default_fonts::EGUI_ICONS`, `epaint_default_fonts::EMOJI_ICON` (dependency font inputs) | egui icon subset and monochrome fallback | John Slegers | [egui 6d518fc](https://github.com/storytold/egui/tree/6d518fc799681571963842f95b55a6b68431b15a/crates/epaint_default_fonts/fonts), [emoji-icon-font](https://github.com/jslegers/emoji-icon-font) | MIT (Copyright (c) 2014 John Slegers; full upstream text in `NOTICE`) | 2026-10-24 (notice audit) | egui-icons.ttf is the five-glyph subset; emoji-icon-font.ttf is the full fallback. Registered by production default font definitions; dependency assets unmodified here, no font file copied into this repository. |
| `epaint_default_fonts::HACK_REGULAR` (dependency font input) | Hack Regular (the monospace default) | Source Foundry Authors (built on the DejaVu project, which was committed to the public domain) and Bitstream Inc. | [egui 6d518fc](https://github.com/storytold/egui/tree/6d518fc799681571963842f95b55a6b68431b15a/crates/epaint_default_fonts/fonts) | MIT AND Bitstream Vera License (Copyright (c) 2018 Source Foundry Authors; Copyright (c) 2003 by Bitstream, Inc. All Rights Reserved.; full upstream texts in `NOTICE`) | 2026-10-25 (notice audit) | unmodified egui default font embedded in the binary and registered by the production default font definitions (egui's `default_fonts`); no font file copied into this repository |
| `epaint_default_fonts::UBUNTU_LIGHT` (dependency font input) | Ubuntu Light (the proportional default) | Canonical Ltd.; designed by Dalton Maag Ltd | [egui 6d518fc](https://github.com/storytold/egui/tree/6d518fc799681571963842f95b55a6b68431b15a/crates/epaint_default_fonts/fonts) | Ubuntu Font Licence 1.0, not the SIL Open Font License (Copyright 2011 Canonical Ltd.; full upstream text in `NOTICE`) | 2026-10-25 (notice audit) | unmodified egui default font embedded in the binary and registered by the production default font definitions (egui's `default_fonts`); no font file copied into this repository |
| `epaint_default_fonts::NOTO_EMOJI_REGULAR` (dependency font input) | Noto Emoji Regular | Google Inc.; designed by the Monotype Design Team | [egui 6d518fc](https://github.com/storytold/egui/tree/6d518fc799681571963842f95b55a6b68431b15a/crates/epaint_default_fonts/fonts) | SIL Open Font License 1.1 (Copyright 2013 Google Inc. All Rights Reserved.; full upstream text in `NOTICE`) | 2026-10-25 (notice audit) | unmodified egui default font embedded in the binary and registered by the production default font definitions (egui's `default_fonts` and `monochrome_emoji_fonts`); no font file copied into this repository |
| `assets/fonts/Inter-*.ttf` (Regular, Medium, SemiBold) | Inter | Rasmus Andersson | https://github.com/rsms/inter | SIL Open Font License 1.1 (`assets/fonts/OFL-Inter.txt`) | 2026-09-30 | none |
| `assets/app-icon/` (`lightcraft.svg` and the PNG/ICNS/ICO/SVG files derived from it) | LightCraft app icon (lynx) | LightCraft project owner (drawn in ArtCraft) | original work (vectorised from the owner's drawing; see `assets/app-icon/README.md`) | MIT OR Apache-2.0 (`assets/app-icon/LICENSE.txt`) | 2026-10-02 | vectorised to SVG; rendered to PNG/ICNS/ICO by `packaging/icons.sh` |
| `assets/camera-profiles/*.json` | Camera colour profiles (matrix + hue/saturation/value table), built into LightCraft | ILCE-7M4: Harald Hoyer | original work: fitted by `lightcraft-cli calibrate` to the contributor's own raw photos and their in-camera JPEGs (597 ILCE-7M4 photos, 2026); aggregate colour statistics only, no image content; no Adobe or third-party profile data | MIT OR Apache-2.0 | 2026-10-07 | none |
@@ -18,6 +22,7 @@ covers a whole directory), or if a licence file referenced in the Licence column
| `assets/camera-profiles/NIKON_Z_6_2.json` | Nikon Z 6II camera colour profile (matrix + hue/saturation/value table), built into LightCraft | Michael Anderson (@endthestart) | original work: fitted by `lightcraft-cli calibrate` to 296 usable Z 6II NEFs of my own (of 299, 2024 to 2026, Standard and Auto picture controls; NIKKOR Z 20mm f/1.8 S, Z 24-70mm f/4 S and AF-S 70-200mm f/2.8G on the FTZ) and their in-camera JPEGs; aggregate colour statistics only, no image content; no Adobe or third-party profile data | MIT OR Apache-2.0 | 2026-10-13 | none |
| `assets/camera-profiles/NIKON_Z_8.json` | Nikon Z 8 camera colour profile (matrix + hue/saturation/value table), built into LightCraft | Michael Anderson (@endthestart) | original work: fitted by `lightcraft-cli calibrate` to 261 usable Z 8 NEFs of my own (of 278, 2023 and 2026, nearly all Standard picture control, lossless 14-bit; NIKKOR Z 24-120mm f/4 S and AF-S 70-200mm f/2.8G on the FTZ) and their in-camera JPEGs; aggregate colour statistics only, no image content; no Adobe or third-party profile data | MIT OR Apache-2.0 | 2026-10-13 | none |
| `assets/camera-profiles/NIKON_D5300.json` | Nikon D5300 camera colour profile (matrix + hue/saturation/value table), built into LightCraft | Michael Anderson (@endthestart) | original work: fitted by `lightcraft-cli calibrate` to 264 usable D5300 NEFs of my own (of 273, 2014 to 2017, mostly Standard picture control, lossy 12-bit, mostly the AF-S DX 35mm f/1.8G) and their in-camera JPEGs; aggregate colour statistics only, no image content; no Adobe or third-party profile data | MIT OR Apache-2.0 | 2026-10-13 | none |
| `assets/camera-profiles/ILCE-7RM5.json` | Sony ILCE-7RM5 camera colour profile (matrix + hue/saturation/value table), built into LightCraft | Yule (Brian) Wang (@peaperfish) | original work: fitted by `lightcraft-cli calibrate` to 220 usable ILCE-7RM5 ARWs of my own (of 241, 2025 to 2026, Standard creative style; Sigma 24-70mm F2.8 DG DN II Art and Sony FE 200-600mm F5.6-6.3 G OSS) and their in-camera JPEGs; aggregate colour statistics only, no image content; no Adobe or third-party profile data | MIT OR Apache-2.0 | 2026-10-26 | none |
| `crates/raw/src/spectral_table.rs` | Camera colour matrices (DNG ColorMatrix1/2 and ForwardMatrix1/2 for Standard Illuminant A and D65) for 52 camera models, and the model alias list | Derived by LightCraft contributors from rawtoaces-data (Academy Software Foundation): camera spectral sensitivities measured by Weta Digital (44 cameras), scitech (7) and Jonathon Irons at the National Physical Laboratory (Nikon D5100); training reflectances; CIE 1931 colour-matching functions (CIE, CC-BY-4.0, as shipped in rawtoaces-data) | https://github.com/AcademySoftwareFoundation/rawtoaces-data, tag v1.1.0, commit 5cfa53707b053594b2d0d48e8019fb56e6582d6c | Apache-2.0 (`LICENSE-APACHE`); the colour-matching functions CC-BY-4.0 | 2026-10-08 | generated by `tools/spectral_camera_table.py`: a 3×3 matrix per illuminant fitted to the spectral data (no measurement copied verbatim); aliases copied from `data/aliases.json`; no Adobe or third-party camera matrix |
| `assets/camera-tone/sony-dro-off.json` | Sony global tone curve with the Dynamic Range Optimizer off (32 knots), built into LightCraft for ARWs shot with DRO on | LightCraft contributors; source photographs by raw.pixls.us contributors | original work: tone curve quantile-matched over the pooled pixel pairs of 12 CC0 raw.pixls.us ARWs (DRO off, Standard creative style; ILCE-3500, -5000, -6600, -7CR, -7M3 ×3, -7S, -9M2, ILCA-77M2, SLT-A57, NEX-F3) and their embedded camera JPEGs; the file lists each source URL and SHA-256 and how the curve was derived (see also `docs/camera-preview-colour.md`); aggregate tone statistics only, no image content; no Adobe or third-party profile data | MIT OR Apache-2.0 (sources CC0) | 2026-10-09 | none |
| `crates/scenes/` (generated images) | Procedural demo photographs | LightCraft contributors | original work (generated at runtime, no source imagery) | MIT OR Apache-2.0 | 2026-09-30 | n/a |
@@ -29,7 +34,7 @@ covers a whole directory), or if a licence file referenced in the Licence column
| `docs/images/*-blue-marble.jpg`, `docs/images/grid-pd.jpg` | "The Blue Marble" (AS17-148-22727) | NASA, Apollo 17 crew (1972) | https://commons.wikimedia.org/wiki/File:The_Earth_seen_from_Apollo_17.jpg | Public domain (NASA) | 2026-09-30 | developed in LightCraft; shown inside app screenshots |
| `docs/images/*.jpg` (UI) | LightCraft application screenshots | LightCraft contributors | original work (captured with `docs/showcase/`) | MIT OR Apache-2.0 | 2026-09-30 | n/a |
| `docs/brand/` (artcraft-logo and artcraft-mark, SVG and PNG) | ArtCraft wordmark and mark | ArtCraft Team | original work (https://getartcraft.com/) | ArtCraft trademark, see `docs/brand/LICENSE-brand.txt` (not open source) | 2026-10-01 | none |
| craft-fonts: optional build input, not files in this repository | BIZ UDPGothic (Regular, Bold), BIZ UDMincho Regular, Shippori Mincho Regular, Noto Sans CJK SC Regular, Noto Sans Arabic Regular, Noto Sans Thai Regular | The Noto Project Authors (Arabic and Thai); Morisawa Inc. / The BIZ UDGothic and BIZ UDMincho Project Authors; The Shippori Mincho Project Authors; Google and Adobe (Noto Sans CJK, maintainer-approved exception; see AGENTS.md) (per craft-fonts' attribution) | https://github.com/storytold/craft-fonts/blob/main/ATTRIBUTION.md | SIL Open Font License 1.1 (each font's OFL.txt in craft-fonts; release packages ship it as OFL-<family>.txt) | 2026-10-06 | none; native builds made with `CRAFT_FONTS_DIR` embed the manifest fonts (all official releases); the web build embeds BIZ UDPGothic Regular and Noto Sans Thai Regular and ships Noto Sans CJK SC Regular as a separate font file |
| craft-fonts: optional build input, not files in this repository | BIZ UDPGothic (Regular, Bold), BIZ UDMincho Regular, Shippori Mincho Regular, Noto Sans CJK SC Regular, Noto Sans Arabic Regular, Noto Sans Thai Regular, Noto Sans Georgian Regular, NanumGothic Regular, Noto Sans Symbols Regular, Noto Sans Symbols 2 Regular, Noto Sans Hebrew Regular, Noto Sans Devanagari Regular | The Noto Project Authors (Arabic, Thai, Georgian, Symbols, Symbols 2, Hebrew and Devanagari); Morisawa Inc. / The BIZ UDGothic and BIZ UDMincho Project Authors; The Shippori Mincho Project Authors; NHN Corporation and Sandoll Communications (NanumGothic); Google and Adobe (Noto Sans CJK, maintainer-approved exception; see AGENTS.md) (per craft-fonts' attribution) | https://github.com/storytold/craft-fonts/blob/b076417b5880bd4154bd22f00695b1d28c6bea83/ATTRIBUTION.md | SIL Open Font License 1.1 (each font's OFL.txt in craft-fonts; release packages ship it as OFL-<family>.txt) | 2026-10-06 | none; native builds made with `CRAFT_FONTS_DIR` embed the default `fonts/manifest.txt` set (all official releases; the opt-in LXGW WenKai Lite face in `fonts/simplified-chinese-manifest.txt` is not embedded); the web build embeds BIZ UDPGothic Regular, Noto Sans Thai Regular and Noto Sans Georgian Regular and ships Noto Sans CJK SC Regular as a separate font file |
| `crates/ui-egui/locales/uk.json`, `crates/ui-egui/locales/uk-formats.json` | Ukrainian interface catalogs | @dmatviichuk with Codex; PhotoCraft, FilmCraft and VectorCraft translation contributors for reused labels | original translations from English source; 337 initial English-key matches reviewed from [PhotoCraft 69c41f3](https://github.com/storytold/photocraft/blob/69c41f3523e147eb215da8c7b4c29b18b35d1c63/crates/ui-egui/src/i18n/uk.tsv), [FilmCraft 4854c27](https://github.com/storytold/filmcraft/blob/4854c277536fe8118257362304b6a7d82d8e704b/crates/ui-egui/src/i18n/uk.tsv), [VectorCraft 4b44265](https://github.com/storytold/vectorcraft/blob/4b44265965fdd6ffedfd6182a892c1a6c360d344/crates/ui-egui/src/i18n/uk.tsv) | MIT OR Apache-2.0 (`LICENSE-MIT`, `LICENSE-APACHE`, also the source repositories' licences) | 2026-10-09 | adapted to LightCraft photo-development context, completed catalogs, reviewed spelling and placeholders |
| `crates/ui-egui/locales/cs.json`, `crates/ui-egui/locales/cs-formats.json` | Czech interface catalogs | @Losminek with AI assistance (Codex) | original work: translations of English source phrases, reviewed against the app UI conventions; no external translation material | MIT OR Apache-2.0 (`LICENSE-MIT`, `LICENSE-APACHE`) | 2026-10-11 | catalog coverage, Czech dates and count-neutral wording; independent scope/terminology review; native-speaker proofreading pending |
| `crates/ui-egui/locales/it.json`, `crates/ui-egui/locales/it-formats.json` | Italian interface catalogs | @GabrielePecchioli with Claude Code; PhotoCraft, VectorCraft, PdfCraft and WordCraft translation contributors for reused labels | original translations from English source; 410 initial English-key matches reviewed from [PhotoCraft da3da09](https://github.com/storytold/photocraft/blob/da3da0942c14c1159f40c652afcf33584332c733/crates/ui-egui/src/i18n/it.tsv), [VectorCraft 5432821](https://github.com/storytold/vectorcraft/blob/543282148007bcb5633c493a5374cd26b0339c8d/crates/ui-egui/src/i18n/it.tsv), [PdfCraft e8e275c](https://github.com/storytold/pdfcraft/blob/e8e275cbd40ccd0dc6b56f82b16a575571043702/crates/ui-egui/src/i18n/it.tsv) and the Italian WordCraft catalog ([storytold/wordcraft#598](https://github.com/storytold/wordcraft/pull/598)); 46 of them retranslated where the sibling's meaning didn't fit a photo app | MIT OR Apache-2.0 (`LICENSE-MIT`, `LICENSE-APACHE`, also the source repositories' licences) | 2026-10-12 | adapted to LightCraft photo-development context, count-neutral labels for Italian plural agreement, reviewed placeholders |
@@ -40,6 +45,9 @@ covers a whole directory), or if a licence file referenced in the Licence column
| `crates/ui-egui/locales/vi.json`, `crates/ui-egui/locales/vi-formats.json` | Vietnamese interface catalogs | @duydev (DUY Tran Nhat) with AI assistance (Cursor) | original work: translations of English source phrases; no external translation material | MIT OR Apache-2.0 (`LICENSE-MIT`, `LICENSE-APACHE`) | 2026-10-13 | plural-neutral counts, Vietnamese dates and weekdays, reviewed placeholders |
Notes
- egui's default fonts (Hack, Ubuntu Light, Noto Emoji, egui-icons and emoji-icon-font) come from the
`epaint_default_fonts` dependency, not from files in this repository; their creators, licences and the full
upstream texts are recorded in the rows above and in `NOTICE`.
- Fonts authored or published by Adobe (Source Sans/Serif/Code, Source Han, …) are not used, even though some are
OFL-licensed: the asset rule excludes anything from Adobe. Source Sans 3 was removed on 2026-09-30 and replaced by Inter.
- CJK fonts (BIZ UDPGothic, BIZ UDMincho, Shippori Mincho, Noto Sans CJK SC) are not in this repository. They live in
File diff suppressed because it is too large. Load diff
+10
View File
@@ -38,6 +38,9 @@ sync_github_name = true
[people.Losminek]
sync_github_name = true
[people.nicolasfalesy]
sync_github_name = true
[people.FooVoo]
real_name = "Aleksandr Batyrev"
sync_github_name = true
@@ -58,9 +61,16 @@ sync_github_name = false
real_name = "Ege Gursel"
sync_github_name = true
[people.valenfys]
sync_github_name = true
[people.mahmatias]
display_name = "Márcio Matias de Andrade"
sync_github_name = true
[people.ronibalthazar]
sync_github_name = true
[people.dotCooCoo]
real_name = "Robert Lee"
sync_github_name = true
+107 -4
View File
@@ -43,19 +43,63 @@ pub fn weekday(iso: &str) -> Option<&'static str> {
/// Shift an ISO time by `secs`, keeping anything after the seconds (fractions, zone) as it was.
/// A date without a time gets one. `None` if it doesn't parse, or if the result leaves years
/// 0000–9999 (an ISO date here has four year digits, so it couldn't be read back); `secs` may be
/// anything, it never overflows.
/// anything, it never overflows. Stricter than [`iso_seconds`], which reads what it can for sorting:
/// a time cut short, seconds that aren't digits or anything after the zone is `None` here, not
/// written back as a clean-looking time.
pub fn shift_iso(iso: &str, secs: i64) -> Option<String> {
let iso = iso.trim();
let tail = iso.get(tail_start(iso)?..)?;
let t = iso_seconds(iso)?.checked_add(secs)?;
let (first, last) = (iso_seconds("0000-01-01T00:00:00")?, iso_seconds("9999-12-31T23:59:59")?);
if !(first..=last).contains(&t) {
return None;
}
let iso = iso.trim();
// the seconds are optional (`10:00+02:00`): without them the tail starts right after the minutes
let tail = iso.get(if iso.get(16..17) == Some(":") { 19 } else { 16 }..).unwrap_or("");
Some(format!("{}{tail}", civil(t)))
}
/// Where the part of an ISO time that [`shift_iso`] keeps as it was (fraction, zone) starts, or
/// `None` unless `iso` is a date alone (`2026-01-01`) or a date and `HH:MM`, optional `:SS` with
/// an optional fraction (`.5`), and an optional zone (`Z`, `+02:00`, `-0500`, `+02`), and nothing after.
fn tail_start(iso: &str) -> Option<usize> {
let b = iso.as_bytes();
let digits = |x: &[u8]| !x.is_empty() && x.iter().all(u8::is_ascii_digit);
let digits_at = |a: usize, z: usize| b.get(a..z).is_some_and(digits);
let at = |i: usize, c: u8| b.get(i) == Some(&c);
if !(digits_at(0, 4) && at(4, b'-') && digits_at(5, 7) && at(7, b'-') && digits_at(8, 10)) {
return None;
}
if b.len() == 10 {
return Some(10);
}
if !((at(10, b'T') || at(10, b' ')) && digits_at(11, 13) && at(13, b':') && digits_at(14, 16)) {
return None;
}
// the seconds are optional (`10:00+02:00`): without them the tail starts right after the minutes
let start = if !at(16, b':') {
16
} else if digits_at(17, 19) {
19
} else {
return None;
};
let mut zone = b.get(start..)?;
// a fraction only after the seconds: `10:00.5` is half a minute, not half a second
if let ([b'.', rest @ ..], 19) = (zone, start) {
let n = rest.iter().take_while(|c| c.is_ascii_digit()).count();
zone = rest.get(n..)?;
if n == 0 {
return None;
}
}
let ok = match zone {
[] | [b'Z'] => true,
[b'+' | b'-', h1, h2, b':', m1, m2] => digits(&[*h1, *h2, *m1, *m2]),
[b'+' | b'-', hm @ ..] => matches!(hm.len(), 2 | 4) && digits(hm),
_ => false,
};
ok.then_some(start)
}
/// Normalize user input to `YYYY-MM-DDTHH:MM:SS` (accepts a space instead of `T`, missing
/// seconds or time). `None` if it isn't a valid date.
pub fn normalize_iso(s: &str) -> Option<String> {
@@ -260,6 +304,65 @@ mod tests {
assert_eq!(iso_seconds("2026-01-01T10:00+02:00"), iso_seconds("2026-01-01T10:00"));
}
#[test]
fn shift_refuses_a_time_it_cant_fully_read() {
for bad in [
"2026-01-01T10:0",
"2026-01-01T10",
"2026-01-01T",
"2026-01-01T10:00:xx",
"2026-01-01T10:00:0",
"2026-01-01T10:00:00+",
"2026-01-01T10:00:00+2",
"2026-01-01T10:00:00+02:0",
"2026-01-01T10:00:00.",
"2026-01-01T10:00:00.5.5",
"2026-01-01T10:00.5",
"2026-01-01T10:00:00ZZ",
"2026-01-01T10:00:00 and more",
"2026-01-01T10:00 and more",
"2026-01-01x10:00:00",
"2026/01/01T10:00:00",
] {
assert_eq!(shift_iso(bad, 3600), None, "{bad} doesn't fully read, so it must not be written back");
}
// multi-byte characters where the time, the seconds and the zone start never panic
for at in [11, 16, 18, 21] {
let mut s = String::from("2026-01-01T10:00:00+02:00");
s.replace_range(at..at + 1, "é");
assert_eq!(shift_iso(&s, 3600), None, "{s}");
assert_eq!(shift_iso(s.get(..at + 2).unwrap(), 3600), None, "{s}");
}
}
#[test]
fn shift_keeps_a_fraction_and_zone_and_reads_back() {
for (from, to) in [
("2026-01-01", "2026-01-01T01:00:00"),
(" 2026-01-01T10:00 ", "2026-01-01T11:00:00"),
("2026-01-01 10:00:30", "2026-01-01T11:00:30"),
("2026-01-01T10:00:30.5", "2026-01-01T11:00:30.5"),
("2026-01-01T10:00:30Z", "2026-01-01T11:00:30Z"),
("2026-01-01T10:00:30+02:00", "2026-01-01T11:00:30+02:00"),
("2026-01-01T10:00:30-0500", "2026-01-01T11:00:30-0500"),
("2026-01-01T10:00:30+02", "2026-01-01T11:00:30+02"),
("2026-01-01T10:00:30.125+02:00", "2026-01-01T11:00:30.125+02:00"),
] {
assert_eq!(shift_iso(from, 3600).as_deref(), Some(to), "{from}");
assert_eq!(shift_iso(to, 0).as_deref(), Some(to), "{to} reads back");
}
}
/// Sorting, grouping and date rules read with `iso_seconds`, which stays lenient: only the
/// write path refuses what it can't fully read.
#[test]
fn reading_stays_lenient_for_sorting() {
let at_ten = iso_seconds("2026-01-01T10:00:00");
assert_eq!(iso_seconds("2026-01-01T10:00:00 and more"), at_ten);
assert_eq!(iso_seconds("2026-01-01T10:00:xx"), at_ten);
assert_eq!(iso_seconds("2026-01-01T10:0"), iso_seconds("2026-01-01"));
}
#[test]
fn impossible_times_are_rejected_not_rolled_over() {
for bad in ["2026-04-01T10:75:00", "2026-04-01 10:00:75", "2026-04-01T10:60", "2026-04-01T24:00:00", "2026-04-01T10:59:60"] {
+19 -2
View File
@@ -207,14 +207,31 @@ impl Catalog {
/// folder that is no longer there and are dropped (undo restores them). Empty when there is
/// nothing to carry or drop.
pub fn folder_records_follow(&self, from: &str, to: &str) -> Vec<Op> {
self.folder_records_carried(from, to, true)
}
/// [`Catalog::folder_records_follow`] for a folder located where it already is on disk
/// (`folder.updateLocation`): nothing arrived at `to`, the library only learned that the
/// folder is there. So what it knows about folders at or below `to` (other folders of the
/// library there) stays; a record carried over replaces the one at the same place.
pub fn folder_records_located(&self, from: &str, to: &str) -> Vec<Op> {
self.folder_records_carried(from, to, false)
}
fn folder_records_carried(&self, from: &str, to: &str, clear_destination: bool) -> Vec<Op> {
let (Some(root), Some(dest)) = (record_key(from), record_key(to)) else { return Vec::new() };
if root == dest {
return Vec::new();
}
// a folder located below itself: what is already at or below `to` belongs to the folders
// of the library there, not to the folder located, so it is neither carried nor cleared
// (the record of `from` itself still lands at `to`, replacing the one there)
let nested = !clear_destination && key_within(&dest, &root);
let carried = |k: &str| key_within(k, &root) && !(nested && key_within(k, &dest));
let moved: Vec<(String, FolderRecord)> = self
.folder_records
.iter()
.filter(|(k, _)| key_within(k, &root))
.filter(|(k, _)| carried(k))
.filter_map(|(k, r)| {
let rest = folder_rest(k, &root)?;
let base = dest.trim_end_matches('/');
@@ -224,7 +241,7 @@ impl Catalog {
.collect();
// what was known at the old place goes, and so does what is left at the new one (also
// when the moved folder carries nothing: the folder now there is not the one it was about)
let stale = self.folder_records.keys().filter(|k| key_within(k, &root) || key_within(k, &dest));
let stale = self.folder_records.keys().filter(|k| carried(k) || (clear_destination && key_within(k, &dest)));
let mut ops: Vec<Op> = stale.map(|k| Op::SetFolderRecord { folder: k.clone(), record: None }).collect();
ops.extend(moved.into_iter().map(|(k, r)| Op::SetFolderRecord { folder: k, record: Some(r) }));
ops
+48 -1
View File
@@ -263,14 +263,36 @@ fn newer(what: String) -> CatalogError {
CatalogError::Newer(format!("{what}; this version reads catalog format v{VERSION} and older"))
}
/// The most `resync` spends re-validating the suffixes of one line whose whole bytes are not valid
/// UTF-8 (a real fragment ends at any byte): one line of a modest malformed log that holds a start
/// of a record in every few bytes used to re-validate the whole remaining suffix at every attempt
/// (CWE-407). Past the budget the rest of the line stays a torn tail, which the damaged path keeps
/// and reports, same state as before, minus the CPU.
const RESYNC_BUDGET_BYTES: usize = 64 << 20; // 64 MiB
/// A line that doesn't decode but ends with a whole record: a fragment of a failed append followed
/// by the record that retried it (the retry re-encodes the same ops with the same `seq`s). Returns
/// the fragment's length and the record, if that record continues the log (`seq <= last + 1`).
/// The record's CRC must match, so damage is never mistaken for it.
fn resync(line: &[u8], last: u64) -> Option<(usize, (u64, Op))> {
const START: &[u8] = b"{\"seq\":";
// One record per line, so a line of valid UTF-8 holds a valid suffix at every record start and
// one validation covers every candidate; only a line that is not wholly valid re-checks, and
// then up to the budget rather than the suffix length times the candidate count.
let whole = std::str::from_utf8(line).ok();
let mut spent = 0usize;
(1..line.len().saturating_sub(START.len() - 1)).filter(|&i| line[i..].starts_with(START)).find_map(|i| {
let rec = std::str::from_utf8(&line[i..]).ok().map(|l| l.trim_end_matches('\r')).and_then(decode_record)?;
let text = match &whole {
Some(t) => &t[i..],
None => {
if spent >= RESYNC_BUDGET_BYTES {
return None;
}
spent = spent.saturating_add(line.len() - i);
std::str::from_utf8(&line[i..]).ok()?
}
};
let rec = decode_record(text.trim_end_matches('\r'))?;
(rec.0 <= last.saturating_add(1)).then_some((i, rec))
})
}
@@ -714,3 +736,28 @@ impl Drop for Journal {
}
}
}
#[cfg(test)]
mod tests_resync {
use super::*;
/// A torn first line may end at any byte and a failed append's retried records may follow its
/// own start `{"seq":` more than once: `resync` finds the CRC-valid record after a fragment of
/// many record starts. UTF-8 is validated once for a valid line, and for a line that isn't
/// wholly valid only up to the budget instead of the suffix length per candidate (CWE-407).
#[test]
fn resync_finds_the_retried_record_after_a_fragment_of_many_starts() {
let op = Op::Batch { ops: vec![] };
let record = encode_record(5, &op);
let mut line = Vec::from(b"\xf2" as &[u8]); // an invalid byte, so the whole line is not UTF-8
for i in 0..1024 {
line.extend_from_slice(format!("{{\"seq\":{},\"crc\":{{", i).as_bytes());
}
let fragment = line.len();
line.extend_from_slice(record.as_bytes());
assert_eq!(resync(&line, 4).map(|(at, (seq, _))| (at, seq)), Some((fragment, 5)));
// a retry is still recognised past `last`: the fragment logs nothing valid, but the CRC
// matches, so the double `Some` is only reached with `last >= 4`
assert_eq!(resync(&line, 100).map(|(at, (seq, _))| (at, seq)), Some((fragment, 5)));
}
}
+67 -5
View File
@@ -48,7 +48,11 @@ pub fn reparent(k: &str, from: &str, to: &str) -> String {
/// name, its parents' and synonyms flat besides its path). Each once, cleaned.
pub fn from_file(flat: &[String], hierarchical: &[String]) -> Vec<String> {
let mut out: Vec<String> = hierarchical.iter().map(|k| clean(k)).collect();
out.extend(flat.iter().map(|k| clean(k)).filter(|k| !hierarchical.iter().any(|h| h.split(SEP).any(|level| same(level.trim(), k)))));
// One lowered set of every hierarchical level, not a search of `hierarchical` for each flat
// keyword: disjoint lists from sidecars made the old approach quadratic in both (CWE-407), and
// `same` allocates two lower-cased strings per comparison.
let levels: std::collections::HashSet<String> = hierarchical.iter().flat_map(|h| h.split(SEP).map(|l| l.trim().to_lowercase())).collect();
out.extend(flat.iter().map(|k| clean(k)).filter(|k| !levels.contains(&k.to_lowercase())));
dedupe(&mut out);
out
}
@@ -78,6 +82,9 @@ pub fn parse_keyword_list(text: &str) -> Result<Vec<(String, KeywordInfo)>> {
let mut out: Vec<(String, KeywordInfo)> = Vec::new();
// where each keyword is in `out`, by lower-case path (a search of `out` per line was quadratic)
let mut index: std::collections::HashMap<String, usize> = std::collections::HashMap::new();
// synonyms already accepted, by keyword (a search of each keyword's growing synonym list per
// line was quadratic and re-allocated both spellings every comparison — CWE-407)
let mut synonyms: std::collections::HashMap<usize, std::collections::HashSet<String>> = std::collections::HashMap::new();
// the keyword at each level above the line: its path and where it is in `out`
let mut above: Vec<(String, usize)> = Vec::new();
for (i, line) in text.split('\n').enumerate() {
@@ -103,9 +110,9 @@ pub fn parse_keyword_list(text: &str) -> Result<Vec<(String, KeywordInfo)>> {
if synonym.contains(SEP) {
return Err(bad(n, "“|” separates a keyword's levels: a synonym can't hold one"));
}
if let Some((_, info)) = out.get_mut(at)
&& !synonym.is_empty()
&& !info.synonyms.iter().any(|s| same(s, synonym))
if !synonym.is_empty()
&& synonyms.entry(at).or_default().insert(synonym.to_lowercase())
&& let Some((_, info)) = out.get_mut(at)
{
info.synonyms.push(synonym.to_string());
}
@@ -264,7 +271,14 @@ impl Catalog {
let mut chain: Vec<usize> = Vec::new();
let mut path = String::new();
let mut lower = String::new();
for part in k.split(SEP).map(str::trim).filter(|s| !s.is_empty()) {
// Level paths hold their every prefix (`path` and `lower` both), so a photo's XMP
// keyword split far more levels than a list file accepts (`MAX_LEVELS`) made the
// stored prefixes quadratic in the level count: a keyword nests [MAX_LEVELS]
// levels here, as a list file would, and what lies below is left out (CWE-400).
for (part_n, part) in (1..).zip(k.split(SEP).map(str::trim).filter(|s| !s.is_empty())) {
if part_n > MAX_LEVELS {
break;
}
if !path.is_empty() {
path.push(SEP);
lower.push(SEP);
@@ -505,7 +519,24 @@ impl Catalog {
}
}
let mut file = KeywordListFile::default();
// The tree is capped at [`MAX_LEVELS`] (its level rows hold every level's prefix twice, so a
// synthetic keyword past that depth made the stored bytes quadratic); whatever lies deeper
// comes from the photos' and list's own spellings instead, named unwritable as before.
let mut over_deep: std::collections::HashSet<String> = Default::default();
for k in self.listed_keywords().map(|k| &k.path) {
if k.split(SEP).count() > MAX_LEVELS {
over_deep.insert(k.clone());
}
}
for p in self.photos().filter(|p| p.in_library()) {
for k in &p.meta.keywords {
if clean(k).split(SEP).count() > MAX_LEVELS {
over_deep.insert(k.clone());
}
}
}
write(self, &self.keyword_tree(), 0, &mut file);
file.unwritable.extend(over_deep);
file
}
@@ -1470,4 +1501,35 @@ mod tests {
let s = c.keyword_suggestions(&[], "E", 5);
assert_eq!(s, ["eagle", "beach", "parade"], "prefix matches before substring matches");
}
/// A flat keyword already covered by a hierarchical level only in another spelling is not kept
/// again, whatever the size of either list (the per-flat-keyword search of every level, twice
/// lower-cased per comparison, was quadratic: CWE-407).
#[test]
fn from_file_matches_levels_in_any_case() {
let flat = ["Rome".to_string(), "Seaside".to_string()];
let hierarchical = ["travel|ITALY|rome".to_string()];
assert_eq!(from_file(&flat, &hierarchical), ["travel|ITALY|rome", "Seaside"]);
}
/// The same synonym, in any spelling, is kept once (the search of each keyword's growing
/// synonym list per line was quadratic, with lower-cased copies made per comparison).(CWE-407)
#[test]
fn parse_keyword_list_keeps_a_synonym_once() {
let read = parse_keyword_list("Travel\n\t{Trip}\n\t{journey}\n\t{TRIP}\n").unwrap();
assert_eq!(read.len(), 1);
assert_eq!(read[0].1.synonyms, ["Trip", "journey"]);
}
/// A keyword split into far more levels than a list file accepts is cut at [`MAX_LEVELS`] in the
/// tree, whose level rows hold every prefix twice: unbounded it made the stored bytes quadratic
/// in the level count of one short keyword (CWE-400).
#[test]
fn keyword_tree_caps_a_keyword_at_the_list_depth() {
let deep: Vec<String> = (0..4096).map(|i| format!("k{i}")).collect();
let joined = deep.join("|");
let (c, _) = lib(&[&[joined.as_str()]]);
let deepest = tree_paths(&c.keyword_tree()).iter().map(|(p, _)| p.split(SEP).count()).max().unwrap_or(0);
assert_eq!(deepest, MAX_LEVELS);
}
}
+69 -20
View File
@@ -36,7 +36,7 @@ pub use folders::{FolderNode, FolderRecord};
pub use journal::{Journal, LoadReport, PersistStats, SnapshotPolicy, SnapshotTiming};
pub use keywords::KeywordNode;
use lightcraft_develop::DevelopSettings;
pub use local::{DEFAULT_FORGET_DAYS, ForgetPlan, folder_of};
pub use local::{DEFAULT_FORGET_DAYS, ForgetPlan, LocalRecords, folder_of};
pub use lock::{LibraryLock, LockError, LockOwner};
pub use model::*;
pub use query::{DateGroup, Filter, Person, RatingOp, Sort, SortKey, mix64};
@@ -284,7 +284,8 @@ pub struct Catalog {
/// What the library keeps about its folders (folder identity → record), see [`folders`].
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
folder_records: BTreeMap<String, FolderRecord>,
/// Increments on every applied op.
/// Increments on every applied op, and when a batch fails and its rollback cannot undo it whole (the catalog
/// changed, so what is keyed by the revision is out of date).
#[serde(skip)]
pub revision: u64,
/// Which photos the latest ops touched ([`Catalog::touched_since`]).
@@ -348,6 +349,8 @@ struct Changes {
/// outside (a library just loaded takes a range of its own, so that nothing kept from another library is
/// taken for this one's): nothing is known of how the catalog got to where it is.
seen: u64,
/// A batch that failed could not be rolled back whole: what it changed stays, so what it noted must too.
torn: bool,
}
impl PartialEq for Changes {
@@ -610,13 +613,40 @@ impl Catalog {
self.albums.get_mut(&id).ok_or(CatalogError::NoAlbum(id))
}
/// Apply an op; returns its inverse. On error nothing changes.
/// Apply an op; returns its inverse. On error the catalog is as it was, the revision included, and nothing is
/// remembered of the op in what the latest ops touched. The one exception is a batch whose rollback fails: the
/// catalog did change, so the revision advances and anything may be taken to have changed.
pub fn apply(&mut self, op: Op) -> Result<Op> {
if self.changes.seen != self.revision {
// the revision was set from outside: what is remembered is of another past
self.changes = Changes { floor: self.revision, seen: self.revision, ..Default::default() };
}
let inv = self.apply_inner(op)?;
let broad = self.changes.broad;
self.changes.torn = false;
let inv = match self.apply_inner(op) {
Ok(inv) => inv,
Err(e) => {
let made = self.revision.saturating_add(1);
if self.changes.torn {
// (a batch the rollback of which failed changed something and says not what: it makes a
// revision, so what is kept for the one before is dropped, and anything may have changed, as
// after an op of a wider reach)
self.changes.torn = false;
self.changes.broad = made;
self.revision = made;
self.changes.seen = made;
} else {
// (everything the failed op, or the rollback of a batch, noted was made for the revision it
// would have made: nothing earlier has that one. A note the cap already dropped stays
// dropped, which only says less than is known, as the cap does.)
while self.changes.touched.back().is_some_and(|(r, _, _)| *r == made) {
self.changes.touched.pop_back();
}
self.changes.broad = broad;
}
return Err(e);
}
};
self.revision += 1;
self.changes.seen = self.revision;
Ok(inv)
@@ -962,7 +992,9 @@ impl Catalog {
Err(e) => {
// roll back what was applied
for inv in inverses.into_iter().rev() {
let _ = self.apply_inner(inv);
if self.apply_inner(inv).is_err() {
self.changes.torn = true;
}
}
return Err(e);
}
@@ -1066,26 +1098,41 @@ impl Catalog {
}
st.photos.len() >= 2
});
let parents: Vec<(AlbumId, Option<AlbumId>)> = self.albums.values().map(|a| (a.id, a.parent)).collect();
for (id, parent) in parents {
let Some(p) = parent else { continue };
let mut cur = Some(p);
let mut steps = 0usize;
let ok = loop {
match cur {
None => break true,
Some(c) if c == id || steps > self.albums.len() => break false,
Some(c) => match self.albums.get(&c) {
Some(a) if a.folder => cur = a.parent,
_ => break false,
},
let albums: Vec<AlbumId> = self.albums.keys().copied().collect();
// Validate each parent link once (CWE-407), caching only chains that now reach a root.
// A cached bad verdict is not enough: skipping its ancestors would leave their broken
// links/cycle intact when a lower-id child was the first to walk into that chain.
let mut valid: std::collections::HashSet<AlbumId> = std::collections::HashSet::new();
for id in albums {
let mut walked: Vec<AlbumId> = Vec::new();
let broken = {
let mut cur = id;
let mut on: std::collections::HashSet<AlbumId> = std::collections::HashSet::new();
loop {
if valid.contains(&cur) {
break None;
}
if !on.insert(cur) {
// Detach one actual cycle node, not the child that first reached it.
break self.albums.get(&cur).and_then(|a| a.parent).map(|parent| (cur, parent));
}
walked.push(cur);
let Some(parent) = self.albums.get(&cur).and_then(|a| a.parent) else { break None };
if !self.albums.get(&parent).is_some_and(|a| a.folder) {
break Some((cur, parent));
}
cur = parent;
}
steps += 1;
};
if !ok && let Some(a) = self.albums.get_mut(&id) {
if let Some((id, p)) = broken
&& let Some(a) = self.albums.get_mut(&id)
{
changed.push(format!("album {}: its folder {} is missing, not a folder or inside it: moved to the top level", id.0, p.0));
a.parent = None;
}
// Only visited nodes are cached, after every one can reach the repaired root or a
// previously validated chain. Unvisited ancestors never inherit a guessed verdict.
valid.extend(walked);
}
let past = |last: Option<u64>| last.map_or(1, |v| v.saturating_add(1));
let (photo, album, stack) = (
@@ -1164,4 +1211,6 @@ mod tests_repair;
#[cfg(test)]
mod tests_scale;
#[cfg(test)]
mod tests_tally;
#[cfg(test)]
mod tests_torn_append;
+39 -1
View File
@@ -34,6 +34,15 @@ use serde::Serialize;
use crate::{Catalog, Op, Photo, PhotoId, Source};
/// The Local records of some files ([`Catalog::local_records_of`]).
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct LocalRecords {
/// Only a record of having looked: nothing was changed on them and nothing refers to them.
pub untouched: Vec<PhotoId>,
/// Holding something of the person's, with their files.
pub kept: Vec<(PhotoId, String)>,
}
/// Default for how long a Local folder may go unbrowsed before its untouched records are
/// forgotten (days).
pub const DEFAULT_FORGET_DAYS: u32 = 30;
@@ -64,7 +73,8 @@ pub fn folder_of(p: &Photo) -> Option<String> {
// `/` and `\` are both separators whatever the platform (a catalog can move between them)
Source::File { path } => {
let key = crate::query::folder_key(path);
key.rsplit_once('/').map(|(dir, _)| if dir.is_empty() { "/".to_string() } else { dir.to_string() })
// a drive or share root keeps its trailing `/` in its key (`c:/`, `//srv/share/`)
key.rsplit_once('/').map(|(dir, _)| if dir.is_empty() { "/".to_string() } else { crate::query::folder_key(&format!("{dir}/")) })
}
Source::Demo { .. } => None,
}
@@ -176,6 +186,34 @@ impl Catalog {
&self.browsed
}
/// The Local records of `files` (paths as stored), for library photos about to point at
/// those files (a folder located where it was browsed, `folder.updateLocation`): a file has one
/// record, so the Local one gives way. Told apart: those that are only a record of having
/// looked (untouched, see the module docs, and nothing refers to them), which can go, and
/// those that hold something of the person's (a rating, an edit, a place in an album), with
/// their files. Records in Recently Deleted are not Local records to this.
pub fn local_records_of(&self, files: &HashSet<String>) -> LocalRecords {
let mut out = LocalRecords::default();
if files.is_empty() {
return out;
}
let referenced = self.referenced();
for p in self.photos.values().filter(|p| p.local && !p.deleted) {
let Source::File { path } = &p.source else { continue };
if !files.contains(path) {
continue;
}
if p.untouched_local() && !referenced.contains(&p.id) {
out.untouched.push(p.id);
} else {
out.kept.push((p.id, path.clone()));
}
}
out.untouched.sort_unstable();
out.kept.sort_unstable();
out
}
/// Plan forgetting untouched Local records whose folder wasn't browsed for `days` days (as of
/// `now`, ISO 8601). `in_use` keeps records the caller still needs. `days == 0` (never) or an
/// unreadable `now` forgets nothing. See the module docs.
+4 -2
View File
@@ -25,7 +25,7 @@
//! - Browser storage (OPFS) has no file locks: see the web host for its own guard.
use std::fs::{File, OpenOptions, TryLockError};
use std::io;
use std::io::{self, Write};
use std::path::{Path, PathBuf};
/// The lock file in the library directory.
@@ -116,9 +116,11 @@ impl LibraryLock {
}
Err(TryLockError::Error(e)) => return Ok(unlocked(e)),
}
// A new file in place of whatever is at the name: a link planted there in a library folder
// someone else prepared is removed, never written through (as for the staging files, #1502).
let owner = LockOwner::this_process(program);
if let Ok(json) = serde_json::to_vec_pretty(&owner)
&& let Err(e) = std::fs::write(dir.join(OWNER), json)
&& let Err(e) = crate::store::create_staging(&dir.join(OWNER)).and_then(|mut f| f.write_all(&json))
{
log::warn!("library {}: can't write {OWNER}: {e}", dir.display());
}
+18
View File
@@ -29,6 +29,10 @@ pub struct Filter {
/// Only these photos (Find Similar results…); empty = no constraint.
#[serde(skip_serializing_if = "Vec::is_empty")]
pub only: Vec<PhotoId>,
/// Show nothing: an explicit empty result (Find Similar with no matches), since an empty
/// `only` means "no constraint".
#[serde(skip_serializing_if = "std::ops::Not::not")]
pub only_none: bool,
/// Any of these labels (the filter bar's multi-select); empty = no constraint.
#[serde(skip_serializing_if = "Vec::is_empty")]
pub labels: Vec<ColorLabel>,
@@ -236,6 +240,17 @@ fn has_person(p: &Photo, name: &str) -> bool {
}
impl Filter {
/// The labels that `label` and `labels` let through together (they AND), in `labels`' order;
/// `None` when neither is set, empty when they exclude each other. The chip and the filter
/// bar's buttons show this, so they match what is filtered.
pub fn labels_shown(&self) -> Option<Vec<ColorLabel>> {
match (self.label, self.labels.is_empty()) {
(None, true) => None,
(Some(l), true) => Some(vec![l]),
(l, false) => Some(self.labels.iter().copied().filter(|x| l.is_none_or(|l| l == *x)).collect()),
}
}
/// Human-readable summary of the active rules (smart album tooltips, `album.list`).
pub fn describe(&self) -> String {
self.describe_in(None)
@@ -374,6 +389,9 @@ impl Filter {
if !self.labels.is_empty() && !p.label.is_some_and(|l| self.labels.contains(&l)) {
return false;
}
if self.only_none {
return false;
}
if !self.only.is_empty() && !prepared.only.as_ref().map_or_else(|| self.only.contains(&p.id), |only| only.contains(&p.id)) {
return false;
}
+94 -3
View File
@@ -432,12 +432,79 @@ thread_local! {
std::cell::RefCell::new(std::collections::HashMap::new());
}
thread_local! {
/// While [`AlbumPass`] is alive on this thread: the photos of each smart album that was counted whole, with
/// the longest chain of smart albums (itself included) its rules lead through.
static PASS: std::cell::RefCell<Option<std::collections::HashMap<crate::AlbumId, (usize, crate::tally::Members)>>> =
const { std::cell::RefCell::new(None) };
}
/// One pass over one catalog state that counts every smart album whole. Each album put in with `remember`
/// answers [`smart_album_holds`] from its photos, not from its rules, until the pass is dropped (it can't outlive
/// the state it was made of: it exists only inside the counting function).
///
/// Only an album whose rules lead to no loop may be remembered, and only with the height of its chain: its answer
/// then doesn't depend on which albums are being evaluated around it, as long as the chain still fits under
/// [`MAX_ALBUM_DEPTH`] from where it is asked (checked on every use; otherwise the rules are evaluated as before).
pub(crate) struct AlbumPass {
outer: Option<std::collections::HashMap<crate::AlbumId, (usize, crate::tally::Members)>>,
}
impl AlbumPass {
pub(crate) fn begin() -> AlbumPass {
AlbumPass { outer: PASS.with_borrow_mut(|p| p.replace(std::collections::HashMap::new())) }
}
/// `id` holds exactly `members`, and its rules lead through a chain of `height` smart albums (itself included),
/// none of them in a loop.
pub(crate) fn remember(&self, id: crate::AlbumId, height: usize, members: crate::tally::Members) {
PASS.with_borrow_mut(|p| {
if let Some(p) = p {
p.insert(id, (height, members));
}
});
}
/// Ends the pass, giving back what was remembered.
pub(crate) fn finish(self) -> std::collections::HashMap<crate::AlbumId, crate::tally::Members> {
PASS.with_borrow_mut(Option::take).unwrap_or_default().into_iter().map(|(id, (_, members))| (id, members)).collect()
}
}
impl Drop for AlbumPass {
fn drop(&mut self) {
let outer = self.outer.take();
PASS.with_borrow_mut(|p| *p = outer);
}
}
/// What a counting pass knows of smart album `id` for `photo`, when asking now can't give another answer than
/// evaluating its rules would: the chain below it fits under [`MAX_ALBUM_DEPTH`] from the albums being evaluated.
fn pass_answer(id: crate::AlbumId, photo: crate::PhotoId) -> Option<bool> {
PASS.with_borrow(|p| {
let (height, members) = p.as_ref()?.get(&id)?;
let depth = EVALUATING.with_borrow(Vec::len);
(depth + height <= MAX_ALBUM_DEPTH).then(|| members.holds(photo))
})
}
#[cfg(test)]
thread_local! {
/// How many times a smart album's rules were worked out on this thread (tests count them).
pub(crate) static EVALUATED: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
}
/// Whether `photo` is in smart album `id`, by `matches` (its rules), remembered for the rest of the
/// outermost question and guarded by [`evaluating`] (a loop or a chain too deep holds nothing).
pub(crate) fn smart_album_holds(id: crate::AlbumId, photo: crate::PhotoId, matches: impl FnOnce() -> bool) -> bool {
if let Some(known) = pass_answer(id, photo) {
return known;
}
if let Some(known) = ANSWERS.with_borrow(|a| a.get(&(id, photo)).copied()) {
return known;
}
#[cfg(test)]
EVALUATED.with(|n| n.set(n.get() + 1));
// the outermost question forgets the answers when it ends, however it ends
struct Forget(bool);
impl Drop for Forget {
@@ -456,7 +523,7 @@ pub(crate) fn smart_album_holds(id: crate::AlbumId, photo: crate::PhotoId, match
}
/// How deep smart albums may test smart albums before the innermost counts as holding nothing.
const MAX_ALBUM_DEPTH: usize = 32;
pub(crate) const MAX_ALBUM_DEPTH: usize = 32;
/// Run `f` as the evaluation of smart album `id`'s rules: `None` (so it holds nothing) when `id`
/// is already being evaluated (a loop the check would refuse, saved anyway) or the chain is
@@ -670,6 +737,11 @@ fn number(v: &Value) -> Option<f64> {
}
}
/// Widens an `f32` to the `f64` nearest its shortest decimal form (1.4f32 -> 1.4), so it compares equal to the value a user types.
fn f32_as_decimal(v: f32) -> f64 {
v.to_string().parse().unwrap_or(v as f64)
}
fn num_op(op: &str, have: Option<f64>, value: &Value) -> bool {
if op == "between" {
let (a, b) = match value {
@@ -953,8 +1025,8 @@ impl Rule {
"editDate" => date_op(op, p.edited.as_deref(), value),
"shutterSpeed" => num_op(op, crate::parse_shutter_seconds(&m.shutter), &shutter_value(value)),
"iso" => num_op(op, m.iso.map(|v| v as f64), value),
"aperture" => num_op(op, m.aperture.map(|v| v as f64), value),
"focalLength" => num_op(op, m.focal_mm.map(|v| v as f64), value),
"aperture" => num_op(op, m.aperture.map(f32_as_decimal), value),
"focalLength" => num_op(op, m.focal_mm.map(f32_as_decimal), value),
"longEdge" | "shortEdge" => {
let (w, h) = p.shown_size();
num_op(op, Some(if field == "longEdge" { w.max(h) } else { w.min(h) }.round()), value)
@@ -1129,6 +1201,25 @@ mod tests {
serde_json::from_value(v).unwrap()
}
#[test]
fn f32_boundary_values_match_gte_lte() {
let cat = Catalog::new();
let matches =
|p: &Photo, field: &str, op: &str, value: f64| rs(json!({"rules": [{"field": field, "op": op, "value": value}]})).matches(p, &cat);
for v in [1.4f32, 1.8, 2.8, 5.6, 0.95] {
let mut p = photo(1);
p.meta.aperture = Some(v);
p.meta.focal_mm = Some(v);
let w = v.to_string().parse::<f64>().unwrap();
for field in ["aperture", "focalLength"] {
assert!(matches(&p, field, "gte", w), "{field} gte {w}");
assert!(matches(&p, field, "lte", w), "{field} lte {w}");
assert!(!matches(&p, field, "gt", w), "{field} gt {w}");
assert!(!matches(&p, field, "lt", w), "{field} lt {w}");
}
}
}
#[test]
fn fields_ops_and_groups() {
let cat = Catalog::new();
+40 -2
View File
@@ -45,6 +45,18 @@ pub trait Store: Send {
}
}
/// Appends and truncates open the name's own file, so anything else at the name is the library's
/// problem: a link planted there redirects the write outside the directory (CWE-59), and a device
/// hangs the open. Refused with an error the open can show, before anything is written through.
fn check_regular_directory_entry(path: &Path) -> io::Result<()> {
match std::fs::symlink_metadata(path) {
Ok(md) if md.is_file() => Ok(()),
Ok(md) => Err(io::Error::other(format!("{} is a {:?}, not a regular file", path.display(), md.file_type()))),
Err(e) if e.kind() == io::ErrorKind::NotFound => Ok(()),
Err(e) => Err(e),
}
}
/// Files in a directory.
pub struct FsStore {
dir: PathBuf,
@@ -75,8 +87,8 @@ impl FsStore {
/// A new staging file at `tmp`: whatever is already there (a leftover from a crashed save, or a link) is removed first
/// (a link itself, never what it points to), then the file is created exclusively, so a write never follows a link
/// planted at that name into another file (issue #1502).
fn create_staging(tmp: &std::path::Path) -> io::Result<std::fs::File> {
/// planted at that name into another file (issue #1502). Also writes the lock's owner note.
pub(crate) fn create_staging(tmp: &std::path::Path) -> io::Result<std::fs::File> {
if std::fs::symlink_metadata(tmp).is_ok() {
std::fs::remove_file(tmp)?;
}
@@ -137,6 +149,7 @@ impl Store for FsStore {
fn append(&mut self, name: &str, data: &[u8]) -> io::Result<()> {
use std::io::Write;
if self.appender.as_ref().is_none_or(|(n, _)| n != name) {
check_regular_directory_entry(&self.path(name))?;
let existed = self.path(name).exists();
let f = std::fs::OpenOptions::new().create(true).append(true).open(self.path(name))?;
if !existed {
@@ -159,6 +172,7 @@ impl Store for FsStore {
if self.appender.as_ref().is_some_and(|(n, _)| n == name) {
self.appender = None;
}
check_regular_directory_entry(&self.path(name))?;
let f = match std::fs::OpenOptions::new().write(true).open(self.path(name)) {
Ok(f) => f,
// nothing to cut (an append that failed before creating the file)
@@ -266,4 +280,28 @@ mod tests_staging {
assert_eq!(std::fs::read(dir.join("catalog.log")).unwrap(), b"[1]");
let _ = std::fs::remove_dir_all(&dir);
}
/// The journal's append and truncate (which recovery uses) refuse a non-regular file at the
/// name instead of following it, so a link planted at `catalog.log` never receives library
/// bytes (CWE-59, Audit row 107); the snapshot write already removes such a link (#1502).
#[test]
fn append_and_truncate_refuse_a_link_at_the_name() {
let dir = std::env::temp_dir().join(format!("lc-log-link-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
let victim = dir.join("victim.txt");
std::fs::create_dir_all(&dir).unwrap();
std::fs::write(&victim, b"keep").unwrap();
std::os::unix::fs::symlink(&victim, dir.join("catalog.log")).unwrap();
let mut store = FsStore::open(&dir).unwrap();
assert!(store.append("catalog.log", b"appended").is_err());
assert!(store.truncate("catalog.log", 0).is_err());
assert_eq!(std::fs::read(&victim).unwrap(), b"keep");
assert!(std::fs::symlink_metadata(dir.join("catalog.log")).unwrap().file_type().is_symlink());
// A regular file keeps working.
std::fs::remove_file(dir.join("catalog.log")).unwrap();
std::fs::write(dir.join("catalog.log"), b"abc").unwrap();
store.truncate("catalog.log", 1).unwrap();
assert_eq!(std::fs::read(dir.join("catalog.log")).unwrap(), b"a");
let _ = std::fs::remove_dir_all(&dir);
}
}
+91 -14
View File
@@ -8,7 +8,9 @@
use std::collections::{HashMap, HashSet};
use crate::{AlbumId, Aspects, Catalog, PhotoId};
use crate::query::Filter;
use crate::rules::{AlbumPass, MAX_ALBUM_DEPTH};
use crate::{Album, AlbumId, Aspects, Catalog, PhotoId};
/// The most photos an edit may touch for the counts to follow it photo by photo.
const MAX_FOLLOWED: usize = 256;
@@ -20,7 +22,7 @@ const WORDS_TO_START: usize = 1024;
/// The photos a smart album holds.
#[derive(Clone, Debug)]
enum Members {
pub(crate) enum Members {
/// One bit a photo id, and how many are set.
Bits {
words: Vec<u64>,
@@ -41,8 +43,7 @@ impl Members {
}
}
#[cfg(test)]
fn holds(&self, id: PhotoId) -> bool {
pub(crate) fn holds(&self, id: PhotoId) -> bool {
match self {
Members::Bits { words, .. } => {
usize::try_from(id.0 / 64).ok().and_then(|at| words.get(at)).is_some_and(|word| word & (1u64 << (id.0 % 64)) != 0)
@@ -114,21 +115,46 @@ pub struct AlbumTally {
impl AlbumTally {
/// Counted from every photo.
///
/// A smart album that tests another (`album is #14`) reads that album's photos, counted once for the whole
/// pass, instead of working its rules out again for every photo: albums are counted shortest chain first.
/// The photos are kept only until this function returns, so they can't be older than the catalog counted.
/// An album whose rules lead into a loop, or through a chain too deep to be sure of, is counted by its rules
/// as before: the pass ends before the first of them (they sort last), so nothing they evaluate reads it.
pub fn of(cat: &Catalog) -> AlbumTally {
let heights = chain_heights(cat);
let height_of = |id: AlbumId| heights.get(&id).copied().flatten().filter(|h| *h <= MAX_ALBUM_DEPTH);
let mut smart: Vec<AlbumId> = cat.albums().filter(|a| a.is_smart()).map(|a| a.id).collect();
smart.sort_by_key(|id| height_of(*id).unwrap_or(usize::MAX));
let held_by = |id: AlbumId| {
let mut held = Members::none();
for p in cat.photos().filter(|p| cat.album_contains(id, p)) {
held.set(p.id, true);
}
held
};
let mut counts = HashMap::new();
let mut members = HashMap::new();
for album in cat.albums() {
if album.is_smart() {
let mut held = Members::none();
for p in cat.photos().filter(|p| cat.album_contains(album.id, p)) {
held.set(p.id, true);
}
counts.insert(album.id, held.len());
members.insert(album.id, held);
} else {
counts.insert(album.id, cat.album_count(album.id));
let split = smart.partition_point(|id| height_of(*id).is_some());
let (with_height, without) = smart.split_at(split);
let pass = AlbumPass::begin();
for id in with_height {
let held = held_by(*id);
counts.insert(*id, held.len());
if let Some(height) = height_of(*id) {
pass.remember(*id, height, held);
}
}
members.extend(pass.finish());
for id in without {
let held = held_by(*id);
counts.insert(*id, held.len());
members.insert(*id, held);
}
for album in cat.albums().filter(|a| !a.is_smart()) {
counts.insert(album.id, cat.album_count(album.id));
}
AlbumTally { revision: cat.revision, counts, members }
}
@@ -184,6 +210,57 @@ impl AlbumTally {
}
}
/// For each smart album, the most smart albums in a chain its rules lead through (itself included), or `None`
/// when a loop is reachable from it (its answer may then depend on which albums are being evaluated around it).
fn chain_heights(cat: &Catalog) -> HashMap<AlbumId, Option<usize>> {
struct Frame {
id: AlbumId,
tests: Vec<AlbumId>,
next: usize,
tallest: usize,
loops: bool,
}
let tests = |id: AlbumId| -> Vec<AlbumId> {
let tested = cat.album(id).and_then(|a| a.smart.as_deref()).map(Filter::albums_tested).unwrap_or_default();
tested.into_iter().filter(|t| cat.album(*t).is_some_and(Album::is_smart)).collect()
};
let frame = |id: AlbumId| Frame { id, tests: tests(id), next: 0, tallest: 0, loops: false };
let mut done: HashMap<AlbumId, Option<usize>> = HashMap::new();
let mut open: HashSet<AlbumId> = HashSet::new();
for root in cat.albums().filter(|a| a.is_smart()).map(|a| a.id) {
if done.contains_key(&root) {
continue;
}
open.insert(root);
let mut stack = vec![frame(root)];
while let Some(top) = stack.last_mut() {
if let Some(&kid) = top.tests.get(top.next) {
top.next += 1;
match done.get(&kid) {
Some(Some(h)) => top.tallest = top.tallest.max(*h),
Some(None) => top.loops = true,
None if open.contains(&kid) => top.loops = true,
None => {
open.insert(kid);
stack.push(frame(kid));
}
}
} else if let Some(finished) = stack.pop() {
open.remove(&finished.id);
let height = (!finished.loops).then_some(finished.tallest + 1);
done.insert(finished.id, height);
if let Some(parent) = stack.last_mut() {
match height {
Some(h) => parent.tallest = parent.tallest.max(h),
None => parent.loops = true,
}
}
}
}
}
done
}
#[cfg(test)]
mod tests {
use super::*;
+72 -4
View File
@@ -952,12 +952,13 @@ fn the_catalog_remembers_which_photos_its_latest_ops_changed_and_what_of_them()
c.apply(Op::SetRating { id: a, rating: 1 }).unwrap();
assert_eq!(c.touched_since(loaded), Some(vec![a]));
assert_eq!(c.touched_since(old), None);
// an op that fails changes nothing, and what is remembered never says less than what happened
// an op that fails changes nothing and is not remembered
let before = c.revision;
assert!(c.apply(Op::SetRating { id: PhotoId(9_999), rating: 1 }).is_err());
assert_eq!(c.revision, before);
assert_eq!(c.touched_since(before), Some(vec![]));
c.apply(Op::SetFlag { id: a, flag: Flag::Pick }).unwrap();
assert!(c.touched_since(before).is_some_and(|ids| ids.contains(&a)));
assert_eq!(c.touched_since(before), Some(vec![a]));
assert!(c.aspects_since(before).is_some_and(|x| x.any_of(Aspects::FLAG)));
// a batch that fails midway is undone as a whole; a batch in a batch is remembered photo by photo; a batch
// with an op that changes more than photos is not
@@ -965,8 +966,8 @@ fn the_catalog_remembers_which_photos_its_latest_ops_changed_and_what_of_them()
assert!(c.apply(Op::Batch { ops: vec![Op::SetRating { id: a, rating: 5 }, Op::SetRating { id: PhotoId(9_999), rating: 1 }] }).is_err());
assert_eq!((c.revision, c.photo(a).map(|p| p.rating)), (before, Some(1)));
c.apply(Op::Batch { ops: vec![Op::Batch { ops: vec![Op::SetRating { id: a, rating: 2 }] }] }).unwrap();
// (the photo the failed batch asked for and never found may be named too: more is said, never less)
assert!(c.touched_since(before).is_some_and(|ids| ids.contains(&a)));
// (the failed batch is not remembered: only the batch that worked)
assert_eq!(c.touched_since(before), Some(vec![a]));
let before = c.revision;
c.apply(Op::Batch { ops: vec![Op::SetRating { id: a, rating: 3 }, Op::RenameAlbum { id: album, name: "Journey".into() }] }).unwrap();
assert_eq!(c.touched_since(before), None);
@@ -978,3 +979,70 @@ fn the_catalog_remembers_which_photos_its_latest_ops_changed_and_what_of_them()
assert_eq!(c.touched_since(long_ago), None);
assert_eq!(c.touched_since(c.revision - 10), Some(vec![a]));
}
/// An op that fails leaves nothing behind in what the catalog remembers of its ops: no photo it never changed (one
/// that does not even exist), and, for a batch rolled back, none of what its first ops had touched.
#[test]
fn an_op_that_fails_leaves_no_touched_photos_behind() {
let mut c = Catalog::default();
let add = |c: &mut Catalog, name: &str| {
let id = c.alloc_photo_id();
c.apply(Op::AddPhoto {
photo: Box::new(Photo::new(id, Source::File { path: format!("/p/{name}") }, name, "JPEG", 60, 40, "2026-01-01T10:00:00")),
})
.unwrap();
id
};
let (a, b) = (add(&mut c, "a.jpg"), add(&mut c, "b.jpg"));
let missing = PhotoId(9_999);
let start = c.revision;
// every op that names a photo, on one that does not exist
for op in [Op::SetRating { id: missing, rating: 1 }, Op::SetFlag { id: missing, flag: Flag::Pick }, Op::RemovePhoto { id: missing }] {
assert!(c.apply(op).is_err());
assert_eq!(c.revision, start, "a failed op makes no revision");
assert_eq!(c.touched_since(start), Some(vec![]));
assert_eq!(c.aspects_since(start), Some(Aspects::NONE));
}
// an op that changes more than photos and fails does not make earlier revisions unknown
assert!(c.apply(Op::RenameAlbum { id: AlbumId(77), name: "x".into() }).is_err());
assert_eq!(c.touched_since(start), Some(vec![]));
// a batch that fails on its second op names nothing of its first, nor of the rollback
assert!(c.apply(Op::Batch { ops: vec![Op::SetRating { id: a, rating: 5 }, Op::SetRating { id: missing, rating: 1 }] }).is_err());
assert_eq!(c.revision, start);
assert_eq!(c.touched_since(start), Some(vec![]));
assert_eq!(c.aspects_since(start), Some(Aspects::NONE));
assert_eq!(c.photo(a).map(|p| p.rating), Some(0));
// a batch that ends in a failed removal of a photo that exists
assert!(c.apply(Op::Batch { ops: vec![Op::RemovePhoto { id: b }, Op::RemovePhoto { id: b }] }).is_err());
assert!(c.photo(b).is_some());
assert_eq!(c.touched_since(start), Some(vec![]));
// what succeeded after the failures is remembered, and only that
c.apply(Op::SetRating { id: a, rating: 2 }).unwrap();
assert_eq!(c.touched_since(start), Some(vec![a]));
assert_eq!(c.aspects_since(start), Some(Aspects::RATING));
}
/// A batch the rollback of which fails halfway leaves the catalog changed (here a stack is lost), so what the
/// catalog remembers must not say that nothing changed: anything may have.
#[test]
fn a_batch_that_cannot_be_rolled_back_whole_makes_earlier_changes_unknown() {
let mut c = Catalog::default();
let mut ids = Vec::new();
for name in ["a.jpg", "b.jpg"] {
let id = c.alloc_photo_id();
let photo = Photo::new(id, Source::File { path: format!("/p/{name}") }, name, "JPEG", 60, 40, "2026-01-01T10:00:00");
c.apply(Op::AddPhoto { photo: Box::new(photo) }).unwrap();
ids.push(id);
}
let stack = c.alloc_stack_id();
c.apply(Op::AddStack { stack: Stack { id: stack, photos: ids.clone(), collapsed: false } }).unwrap();
let start = c.revision;
// the photo goes, then its stack, then an op fails: putting the stack back needs the photo, which comes later
let failing =
Op::Batch { ops: vec![Op::RemovePhoto { id: ids[0] }, Op::RemoveStack { id: stack }, Op::SetRating { id: PhotoId(9_999), rating: 1 }] };
assert!(c.apply(failing).is_err());
assert_eq!(c.revision, start + 1, "the catalog changed, so what is kept for the revision before is out of date");
assert!(c.photo(ids[0]).is_some() && c.stack(stack).is_none(), "the rollback went only part of the way");
assert_eq!(c.touched_since(start), None, "so nothing is said of what changed");
assert_eq!(c.touched_since(c.revision), Some(vec![]), "from after it, it is known again");
}
@@ -161,6 +161,63 @@ fn an_unlabelled_folder_moved_onto_a_stale_label_does_not_inherit_it() {
assert_eq!(c.folder_color_label("/pics/holiday"), Some(ColorLabel::Purple));
}
#[test]
fn a_located_folder_keeps_what_the_library_knows_of_the_folders_already_at_the_destination() {
let mut c = library();
label(&mut c, "/pics/trip", Some(ColorLabel::Red));
label(&mut c, "/pics/trip/day1", Some(ColorLabel::Green));
// other folders of the library at the place it is said to be at: nothing arrived there
label(&mut c, "/pics/home", Some(ColorLabel::Blue));
label(&mut c, "/pics/home/old", Some(ColorLabel::Yellow));
let before = c.to_snapshot();
let ops = c.folder_records_located("/pics/trip", "/pics/home");
let undo = c.apply(Op::Batch { ops }).unwrap();
assert_eq!(c.folder_record("/pics/trip"), None, "what was known at the old place goes");
assert_eq!(c.folder_record("/pics/trip/day1"), None);
assert_eq!(c.folder_color_label("/pics/home"), Some(ColorLabel::Red), "a record carried over replaces the one at the same place");
assert_eq!(c.folder_color_label("/pics/home/old"), Some(ColorLabel::Yellow), "a folder of the library there keeps its own");
c.apply(undo).unwrap();
assert_eq!(c.to_snapshot(), before);
}
#[test]
fn a_located_folder_with_labels_inside_lands_them_at_the_same_places_below() {
let mut c = library();
label(&mut c, "/pics/trip", Some(ColorLabel::Red));
label(&mut c, "/pics/trip/day1", Some(ColorLabel::Green));
let ops = c.folder_records_located("/pics/trip", "/mnt/nas/trip");
c.apply(Op::Batch { ops }).unwrap();
assert_eq!(c.folder_color_label("/mnt/nas/trip"), Some(ColorLabel::Red));
assert_eq!(c.folder_color_label("/mnt/nas/trip/day1"), Some(ColorLabel::Green));
}
#[test]
fn a_folder_located_below_itself_leaves_the_records_of_the_folders_already_there_in_place() {
let mut c = library();
label(&mut c, "/pics/trip", Some(ColorLabel::Red));
label(&mut c, "/pics/trip/day1", Some(ColorLabel::Green));
// another folder of the library already sits at the place the folder is said to be at
label(&mut c, "/pics/trip/day1/raw", Some(ColorLabel::Blue));
let before = c.to_snapshot();
let ops = c.folder_records_located("/pics/trip", "/pics/trip/day1");
let undo = c.apply(Op::Batch { ops }).unwrap();
assert_eq!(c.folder_record("/pics/trip"), None, "what was known at the old place goes");
assert_eq!(c.folder_color_label("/pics/trip/day1"), Some(ColorLabel::Red), "a record carried over replaces the one at the same place");
assert_eq!(c.folder_color_label("/pics/trip/day1/raw"), Some(ColorLabel::Blue), "a folder below the destination keeps its own");
assert_eq!(c.folder_record("/pics/trip/day1/day1"), None, "the record at the destination is not carried below itself");
assert_eq!(c.folder_record("/pics/trip/day1/day1/raw"), None);
c.apply(undo).unwrap();
assert_eq!(c.to_snapshot(), before);
}
#[test]
fn a_folder_located_where_it_is_or_without_labels_gives_no_ops() {
let mut c = library();
assert!(c.folder_records_located("/pics/trip", "/elsewhere/trip").is_empty());
label(&mut c, "/pics/trip", Some(ColorLabel::Red));
assert!(c.folder_records_located("/pics/trip", "/pics/trip").is_empty());
}
#[test]
fn labels_survive_the_journal_without_a_snapshot() {
let m = MemStore::new();
+49
View File
@@ -1,5 +1,6 @@
//! Forgetting untouched Local browse records (see [`crate::local`]).
use std::collections::HashSet;
use std::sync::Arc;
use lightcraft_develop::DevelopSettings;
@@ -265,3 +266,51 @@ fn op_photo_ids_cover_nested_ops() {
op.photo_ids(&mut |id| ids.push(id.0));
assert_eq!(ids, vec![1, 2, 3, 4]);
}
#[test]
fn drive_and_share_root_records_are_forgotten() {
for (path, folder) in [("C:\\IMG_1.JPG", "C:\\"), ("//srv/share/x.jpg", "//srv/share/")] {
let mut c = Catalog::new();
let id = c.alloc_photo_id();
let mut p = Photo::new(id, Source::File { path: path.into() }, "x.jpg", "JPEG", 60, 40, OLD);
p.local = true;
p.set_local_baseline();
c.apply(Op::AddPhoto { photo: Box::new(p) }).unwrap();
c.apply(Op::SetBrowsed { folder: folder.into(), at: Some(OLD.into()) }).unwrap();
assert_eq!(plan(&c, 30).evict, vec![id], "{path}");
}
}
fn paths(folder: &str, ns: &[u32]) -> HashSet<String> {
ns.iter().map(|i| format!("/pics/{folder}/IMG_{i}.JPG")).collect()
}
#[test]
fn local_records_of_files_tell_what_can_give_way_from_what_holds_something() {
let mut c = Catalog::new();
let untouched = local(&mut c, "trip", 1);
let rated = local(&mut c, "trip", 2);
let in_album = local(&mut c, "trip", 3);
let _elsewhere = local(&mut c, "trip", 4);
c.apply(Op::SetRating { id: rated, rating: 3 }).unwrap();
let a = c.alloc_album_id();
let mut album = Album::new(a, "A");
album.photos.push(in_album);
c.apply(Op::AddAlbum { album }).unwrap();
let found = c.local_records_of(&paths("trip", &[1, 2, 3]));
assert_eq!(found.untouched, vec![untouched]);
assert_eq!(found.kept, vec![(rated, "/pics/trip/IMG_2.JPG".to_string()), (in_album, "/pics/trip/IMG_3.JPG".to_string())]);
}
#[test]
fn local_records_of_files_leave_out_library_photos_and_recently_deleted_ones() {
let mut c = Catalog::new();
let gone = local(&mut c, "trip", 1);
c.apply(Op::SetDeleted { id: gone, deleted: true }).unwrap();
let id = c.alloc_photo_id();
let p = Photo::new(id, Source::File { path: "/pics/trip/IMG_2.JPG".into() }, "IMG_2.JPG", "JPEG", 60, 40, OLD);
c.apply(Op::AddPhoto { photo: Box::new(p) }).unwrap();
assert_eq!(c.local_records_of(&paths("trip", &[1, 2])), LocalRecords::default());
assert_eq!(c.local_records_of(&HashSet::new()), LocalRecords::default());
}
+22
View File
@@ -62,6 +62,28 @@ fn leftover_lock_files_are_not_a_lock() {
let _ = std::fs::remove_dir_all(&dir);
}
/// A link planted at the owner note's name (`catalog.lock.owner`) in a library folder someone else
/// prepared is removed, never written through: the file it points to keeps its bytes, and the note
/// still lands in the library (as for the catalog's staging files, issue #1502).
#[cfg(unix)]
#[test]
fn a_link_at_the_owner_note_is_never_written_through() {
let _serial = serial();
let dir = temp_dir("owner-link");
let victim = dir.join("victim.txt");
std::fs::write(&victim, b"keep").unwrap();
std::os::unix::fs::symlink(&victim, dir.join(OWNER)).unwrap();
let l = LibraryLock::acquire(&dir, "LightCraft").unwrap();
assert!(l.held());
assert_eq!(std::fs::read(&victim).unwrap(), b"keep");
assert!(!std::fs::symlink_metadata(dir.join(OWNER)).unwrap().file_type().is_symlink(), "the link was replaced by the note");
let owner: LockOwner = serde_json::from_slice(&std::fs::read(dir.join(OWNER)).unwrap()).unwrap();
assert_eq!(owner.pid, std::process::id());
drop(l);
assert_eq!(std::fs::read(&victim).unwrap(), b"keep");
let _ = std::fs::remove_dir_all(&dir);
}
/// Another process holds the lock; when it is killed (a crash), the OS releases it.
#[test]
fn lock_held_by_another_process_is_released_when_it_dies() {
+68
View File
@@ -108,6 +108,74 @@ fn folder_loops_and_missing_parents_move_to_the_top() {
let _ = c.apply(Op::MoveAlbum { id: AlbumId(11), parent: Some(AlbumId(10)) });
}
/// Check the postcondition needed by every consumer that follows parent folders. This inspects
/// the repaired catalog, not its cache or a second implementation of the repair algorithm.
fn assert_all_album_chains_reach_roots(c: &Catalog) {
for album in c.albums.values() {
let mut seen = std::collections::HashSet::new();
let mut current = album;
while let Some(parent) = current.parent {
assert!(seen.insert(current.id), "album {} still reaches a cycle", album.id.0);
current = c.albums.get(&parent).expect("every retained parent exists");
assert!(current.folder, "every retained parent is a folder");
}
}
}
#[test]
fn a_child_visited_before_a_folder_cycle_keeps_its_content_and_gets_a_rooted_chain() {
let (c, ids) = sound();
let (mut c, report) = load_edited(&c, |v| {
// BTreeMap order visits10 first: cached Bad11/12 used to be skipped without repair.
v["albums"]["10"] = serde_json::to_value(album(10, Some(11), false, vec![ids[2]])).unwrap();
v["albums"]["11"] = serde_json::to_value(album(11, Some(12), true, vec![])).unwrap();
v["albums"]["12"] = serde_json::to_value(album(12, Some(11), true, vec![])).unwrap();
});
assert_all_album_chains_reach_roots(&c);
assert_eq!(c.album(AlbumId(10)).unwrap().parent, Some(AlbumId(11)), "the child remains in its actual folder");
assert_eq!(c.album(AlbumId(10)).unwrap().photos, vec![ids[2]], "repair preserves the child's photos");
assert_eq!(c.album(AlbumId(11)).unwrap().parent, None, "one cycle edge is repaired");
assert_eq!(c.album(AlbumId(12)).unwrap().parent, Some(AlbumId(11)), "the other folder edge is retained");
assert!(report.repaired.iter().any(|message| message.starts_with("album 11:")), "{:?}", report.repaired);
assert!(c.repair().is_empty(), "a second repair is idempotent");
}
#[test]
fn a_child_visited_before_a_folder_with_a_missing_parent_does_not_hide_the_bad_ancestor() {
let (c, ids) = sound();
let (mut c, report) = load_edited(&c, |v| {
v["albums"]["10"] = serde_json::to_value(album(10, Some(11), false, vec![ids[2]])).unwrap();
v["albums"]["11"] = serde_json::to_value(album(11, Some(999), true, vec![])).unwrap();
});
assert_all_album_chains_reach_roots(&c);
assert_eq!(c.album(AlbumId(10)).unwrap().parent, Some(AlbumId(11)));
assert_eq!(c.album(AlbumId(10)).unwrap().photos, vec![ids[2]]);
assert_eq!(c.album(AlbumId(11)).unwrap().parent, None, "repair the missing-parent edge itself");
assert!(report.repaired.iter().any(|message| message.starts_with("album 11:")), "{:?}", report.repaired);
assert!(c.repair().is_empty());
}
#[test]
fn a_long_valid_folder_chain_is_retained_when_its_child_is_visited_first() {
let (c, ids) = sound();
let (mut c, report) = load_edited(&c, |v| {
v["albums"]["10"] = serde_json::to_value(album(10, Some(100), false, vec![ids[2]])).unwrap();
for id in 100..612 {
v["albums"][id.to_string()] = serde_json::to_value(album(id, Some(id + 1), true, vec![])).unwrap();
}
v["albums"]["612"] = serde_json::to_value(album(612, None, true, vec![])).unwrap();
});
assert_all_album_chains_reach_roots(&c);
assert_eq!(c.album(AlbumId(10)).unwrap().parent, Some(AlbumId(100)));
assert_eq!(c.album(AlbumId(10)).unwrap().photos, vec![ids[2]]);
for id in 100..612 {
assert_eq!(c.album(AlbumId(id)).unwrap().parent, Some(AlbumId(id + 1)), "valid edge{id} remains");
}
assert_eq!(c.album(AlbumId(612)).unwrap().parent, None);
assert!(!report.repaired.iter().any(|message| message.starts_with("album ")), "valid parents must not be repaired: {:?}", report.repaired);
assert!(c.repair().is_empty());
}
/// Id counters left at the top (or behind the ids in use) by a damaged file: allocating no longer
/// overflows (a panic in debug builds) and never hands out an id already in use.
#[test]
+172
View File
@@ -0,0 +1,172 @@
//! A whole count of every album (`AlbumTally::of`) keeps each smart album's photos for the pass, so an album testing
//! another doesn't work its rules out again. The counts must be the ones each album gets counted alone.
use serde_json::json;
use super::*;
use crate::tally::AlbumTally;
fn smart(id: u64, rules: serde_json::Value) -> Album {
let rule_set = Some(serde_json::from_value(rules).unwrap());
Album { smart: Some(Box::new(Filter { rule_set, ..Default::default() })), ..Album::new(AlbumId(id), format!("Smart {id}")) }
}
fn photos(n: u64) -> Vec<Op> {
(0..n)
.map(|i| {
// folders with capitals, accents and a Windows path, so the text rules see every spelling
let dir = ["Asian/Japanese", "Été/Ünï", "C:\\Users\\Me\\Pictures", "plain", "[SITES]/x"][(i % 5) as usize];
let mut p = Photo::new(
PhotoId(i + 1),
Source::File { path: format!("/Volumes/share/photos/{dir}/name{}/IMG_{i:04}.jpg", i % 9) },
&format!("IMG_{i:04}.jpg"),
"JPEG",
6000,
4000,
"2026-01-01T00:00:00",
);
p.rating = (i % 6) as u8;
p.flag = [Flag::None, Flag::Pick, Flag::Reject][(i % 3) as usize];
p.label = (i % 4 == 0).then_some(ColorLabel::Red);
p.meta.keywords = (0..i % 4).map(|k| format!("kw{k}")).collect();
p.deleted = i % 37 == 0;
Op::AddPhoto { photo: Box::new(p) }
})
.collect()
}
fn is(album: u64) -> serde_json::Value {
json!({"field": "album", "op": "is", "value": album})
}
fn is_not(album: u64) -> serde_json::Value {
json!({"field": "album", "op": "isNot", "value": album})
}
fn path(op: &str, value: &str) -> serde_json::Value {
json!({"field": "filePath", "op": op, "value": value})
}
/// Every album's count in `c`, each album counted alone (no pass, rules worked out per photo).
fn counted_alone(c: &Catalog) -> Vec<(AlbumId, usize)> {
let mut v: Vec<_> = c.albums().map(|a| (a.id, c.album_count(a.id))).collect();
v.sort_unstable();
v
}
fn counted_together(c: &Catalog) -> Vec<(AlbumId, usize)> {
let tally = AlbumTally::of(c);
let mut v: Vec<_> = tally.counts().iter().map(|(id, n)| (*id, *n)).collect();
v.sort_unstable();
v
}
#[test]
fn a_whole_count_equals_each_album_counted_alone() {
let mut c = Catalog::new();
let mut ops = photos(240);
let mut plain = Album::new(AlbumId(1), "Picks");
plain.photos = (0..240).step_by(4).map(|i| PhotoId(i + 1)).collect();
let folder = Album { folder: true, ..Album::new(AlbumId(2), "Trips") };
let inside = Album { parent: Some(AlbumId(2)), photos: (0..240).step_by(3).map(|i| PhotoId(i + 1)).collect(), ..Album::new(AlbumId(3), "In") };
ops.extend([plain, folder, inside].map(|album| Op::AddAlbum { album }));
// the inner albums: #2-like, #14-like, then albums chained on it, the other way round, in groups and in different orders
ops.push(Op::AddAlbum {
album: smart(
10,
json!({"match": "any", "rules": [{"field": "flag", "op": "is", "value": "reject"}, {"field": "label", "op": "is", "value": "red"}]}),
),
});
ops.push(Op::AddAlbum {
album: smart(11, json!({"match": "all", "rules": [is_not(10), path("notContains", "Été"), path("notContains", "plain")]})),
});
for k in 0..9u64 {
let tail = path("contains", &format!("NAME{k}"));
let rules = match k % 3 {
0 => json!({"match": "all", "rules": [is(11), tail]}),
1 => json!({"match": "all", "rules": [json!({"field": "keywordCount", "op": "lte", "value": 2}), is(11), tail]}),
_ => json!({"match": "all", "rules": [tail, is(11)]}),
};
ops.push(Op::AddAlbum { album: smart(12 + k, rules) });
}
ops.push(Op::AddAlbum {
album: smart(
21,
json!({"match": "all", "rules": [{"group": {"match": "any", "rules": [is(11), is(1)]}}, {"field": "rating", "op": "gte", "value": 2}]}),
),
});
ops.push(Op::AddAlbum { album: smart(22, json!({"match": "all", "rules": [is_not(21), is(3)]})) });
ops.push(Op::AddAlbum { album: smart(23, json!({"match": "any", "rules": [is(2), is(999), is(1)]})) });
// a loop saved anyway (30 ↔ 31), albums that test it from outside (32, 33), and one that tests both worlds
ops.push(Op::AddAlbum { album: smart(30, json!({"rules": []})) });
ops.push(Op::AddAlbum { album: smart(31, json!({"rules": [is_not(30)]})) });
ops.push(Op::AddAlbum { album: smart(32, json!({"rules": [is(31), {"field": "rating", "op": "gte", "value": 1}]})) });
ops.push(Op::AddAlbum { album: smart(33, json!({"rules": [is_not(32), is(11)]})) });
ops.push(Op::AddAlbum { album: smart(34, json!({"rules": [is(34)]})) });
// a chain deeper than the limit on how deep albums may test albums
ops.push(Op::AddAlbum { album: smart(100, json!({"rules": [{"field": "rating", "op": "gte", "value": 1}]})) });
for k in 101..=140u64 {
ops.push(Op::AddAlbum { album: smart(k, json!({"rules": [is(k - 1)]})) });
}
ops.push(Op::AddAlbum { album: smart(141, json!({"rules": [is(140), is_not(11)]})) });
// an album past the depth limit that also tests a memoized album directly: where the shared album is read from the
// pass near the top, the chain below must still find it answered as it always did (200 ← 231 … 201, height 32)
ops.push(Op::AddAlbum { album: smart(200, json!({"rules": [{"field": "rating", "op": "gte", "value": 1}]})) });
for k in 201..=230u64 {
ops.push(Op::AddAlbum { album: smart(k, json!({"rules": [is(k + 1)]})) });
}
ops.push(Op::AddAlbum { album: smart(231, json!({"rules": [is(200)]})) });
ops.push(Op::AddAlbum { album: smart(250, json!({"match": "all", "rules": [is(200), is(201)]})) });
c.apply(Op::Batch { ops }).unwrap();
c.apply(Op::SetAlbumRules {
id: AlbumId(30),
rules: Box::new(Filter { rule_set: Some(serde_json::from_value(json!({"rules": [is_not(31)]})).unwrap()), ..Default::default() }),
})
.unwrap();
let alone = counted_alone(&c);
let count = |id: u64| alone.iter().find(|(a, _)| a.0 == id).map(|(_, n)| *n);
assert!(count(11) > Some(0) && count(131) > Some(0), "{alone:?}");
assert_eq!(count(132), Some(0), "the chain is too deep from here: holds nothing, as it did");
assert!(count(250) > Some(0), "200 is answered near the top, so the chain finds it answered");
assert_eq!(counted_together(&c), alone);
// the pass leaves nothing behind: the same answers whichever album asks first, and again after an edit
c.apply(Op::SetAlbumRules {
id: AlbumId(10),
rules: Box::new(Filter {
rule_set: Some(serde_json::from_value(json!({"rules": [{"field": "rating", "op": "is", "value": 5}]})).unwrap()),
..Default::default()
}),
})
.unwrap();
let after = counted_alone(&c);
assert_ne!(after, alone);
assert_eq!(counted_together(&c), after);
}
/// The reason for the pass: with an album tested by many, its rules are worked out once per photo, not once per
/// photo for each album that tests it.
#[test]
fn an_album_tested_by_many_is_worked_out_once_a_photo() {
let mut c = Catalog::new();
let mut ops: Vec<Op> = (0..60u64)
.map(|i| {
let mut p = Photo::new(PhotoId(i + 1), Source::Demo { scene: 0 }, "a.jpg", "JPEG", 10, 10, "2026-01-01T00:00:00");
p.rating = (i % 6) as u8;
Op::AddPhoto { photo: Box::new(p) }
})
.collect();
ops.push(Op::AddAlbum { album: smart(1, json!({"rules": [{"field": "rating", "op": "gte", "value": 1}]})) });
for k in 0..10u64 {
ops.push(Op::AddAlbum { album: smart(2 + k, json!({"rules": [is(1), {"field": "rating", "op": "lte", "value": k % 6}]})) });
}
c.apply(Op::Batch { ops }).unwrap();
let alone = counted_alone(&c);
crate::rules::EVALUATED.with(|n| n.set(0));
let tally = AlbumTally::of(&c);
let worked = crate::rules::EVALUATED.with(std::cell::Cell::get);
assert_eq!(worked, 11 * 60, "each of the 11 smart albums asks each of the 60 photos once");
let mut together: Vec<_> = tally.counts().iter().map(|(id, n)| (*id, *n)).collect();
together.sort_unstable();
assert_eq!(together, alone);
}
+1 -1
View File
@@ -100,7 +100,7 @@ jpegxl-rs / imagequant (GPL / C), dav1d / libheif (C).
## Performance (Apple M-series, release, synthetic 24 MP JPEG q90 4:2:0)
Run `cargo test --release -p lightcraft-codecs --test bench -- --ignored --nocapture`.
Run `cargo test --release -p lightcraft-codecs --test it bench:: -- --ignored --nocapture`.
| Operation | Time |
|---|---|
+108 -8
View File
@@ -17,6 +17,8 @@ pub(crate) struct Markers {
pub sof: u8,
/// Number of components in the first scan (< `components` for non-interleaved sequential files).
pub first_scan_components: u8,
/// Number of APP1 segments: capped before the allocating decoders walk them again.
pub app1: u32,
pub exif: Option<Vec<u8>>,
/// Offset of the EXIF TIFF header within the file.
pub exif_offset: usize,
@@ -83,6 +85,7 @@ pub(crate) fn parse_markers(b: &[u8]) -> Option<Markers> {
m.components = seg[5];
}
0xE1 => {
m.app1 = m.app1.saturating_add(1);
if seg.starts_with(b"Exif\0\0") && m.exif.is_none() {
m.exif = Some(seg[6..].to_vec());
m.exif_offset = start + 6;
@@ -121,6 +124,8 @@ pub(crate) fn decode(bytes: &[u8], opts: &DecodeOptions) -> Result<Decoded> {
decode_with_fallback(bytes, opts, None)
}
const MAX_APP1: u32 = 128;
/// Stored dimensions and EXIF orientation from the markers, without decoding the scan data. The
/// file is refused, as a decode would refuse it, when it has no frame header, an unsupported frame
/// type or component count, or when its markers don't run to an end-of-image marker after the
@@ -178,6 +183,11 @@ pub(crate) fn decode_with_fallback(bytes: &[u8], opts: &DecodeOptions, fallback:
return Err(Error::Malformed(F, "no frame header".into()));
}
check_size(F, m.width as u64, m.height as u64, opts)?;
// The allocating decoders below parse the markers again, all of them: the count is capped so
// their quadratic/unbounded handling of APP1 metadata stays linear in file size.
if m.app1 > MAX_APP1 {
return Err(Error::Malformed(F, "too many APP1 markers".into()));
}
let scale_to = opts.max_size.filter(|&(mw, mh)| mw > 0 && mh > 0 && (m.width >= mw.saturating_mul(2) || m.height >= mh.saturating_mul(2)));
// zune-jpeg handles the common cases fastest; jpeg-decoder covers DCT scaling, CMYK/YCCK,
// 12-bit, non-interleaved sequential scans (which zune-jpeg 0.5 mis-decodes with subsampling)
@@ -185,9 +195,9 @@ pub(crate) fn decode_with_fallback(bytes: &[u8], opts: &DecodeOptions, fallback:
let lossless = m.sof == 0xC3;
let non_interleaved = matches!(m.sof, 0xC0 | 0xC1) && m.first_scan_components < m.components;
let raw = if m.components == 4 || scale_to.is_some() || m.precision > 8 || non_interleaved || lossless {
decode_jpeg_decoder(bytes, &m, scale_to)?
decode_jpeg_decoder(bytes, &m, scale_to, opts)?
} else {
decode_zune(bytes, &m)?
decode_zune(bytes, &m, opts)?
};
// A raw container may carry the colour declaration of an otherwise untagged preview.
// Apply it before linearization/resizing; existing JPEG metadata always wins.
@@ -196,7 +206,7 @@ pub(crate) fn decode_with_fallback(bytes: &[u8], opts: &DecodeOptions, fallback:
finish(F, raw, meta, (m.width, m.height), opts)
}
fn decode_zune(bytes: &[u8], m: &Markers) -> Result<Raw> {
fn decode_zune(bytes: &[u8], m: &Markers, budget: &DecodeOptions) -> Result<Raw> {
use zune_core::bytestream::ZCursor;
use zune_core::colorspace::ColorSpace;
use zune_core::options::DecoderOptions;
@@ -207,6 +217,11 @@ fn decode_zune(bytes: &[u8], m: &Markers) -> Result<Raw> {
ColorSpace::RGB
});
let mut d = zune_jpeg::JpegDecoder::new_with_options(ZCursor::new(bytes), opts);
// zune's `decode` allocates straight from the frame header it parses, with its own caps
// set in us: check that header against the same limits the other decoders honour first.
d.decode_headers().map_err(|e| Error::Malformed(F, e.to_string()))?;
let (zw, zh) = d.info().map(|i| (i.width as u64, i.height as u64)).ok_or_else(|| Error::Malformed(F, "no info".into()))?;
check_size(F, zw, zh, budget)?;
let px = d.decode().map_err(|e| Error::Malformed(F, e.to_string()))?;
let info = d.info().ok_or_else(|| Error::Malformed(F, "no info".into()))?;
let (w, h) = (info.width as usize, info.height as usize);
@@ -217,10 +232,15 @@ fn decode_zune(bytes: &[u8], m: &Markers) -> Result<Raw> {
Ok(Raw { width: w, height: h, model, alpha: false, premultiplied: false, buf: Buf::U8(px), bit_depth: 8 })
}
fn decode_jpeg_decoder(bytes: &[u8], m: &Markers, scale_to: Option<(u32, u32)>) -> Result<Raw> {
fn decode_jpeg_decoder(bytes: &[u8], m: &Markers, scale_to: Option<(u32, u32)>, budget: &DecodeOptions) -> Result<Raw> {
use jpeg_decoder::PixelFormat;
let mut d = jpeg_decoder::Decoder::new(bytes);
d.read_info().map_err(|e| Error::Malformed(F, e.to_string()))?;
// Its frame header is what a decode allocates from (progressive coefficient buffers follow the
// unscaled MCU grid): check it against the same limits the other decoders honour, before any
// DCT scale is requested.
let info = d.info().ok_or_else(|| Error::Malformed(F, "no info".into()))?;
check_size(F, info.width as u64, info.height as u64, budget)?;
// `scale` is the DCT size, not a lossless resample: on a lossless frame it would cut the
// output to the top-left of each plane. Those decode at full size; `finish` scales after.
if let Some((mw, mh)) = scale_to.filter(|_| m.sof != 0xC3) {
@@ -369,11 +389,18 @@ pub(crate) fn decode_codes8(bytes: &[u8]) -> Result<(usize, usize, usize, Vec<u8
return Err(Error::Malformed(F, "no frame header".into()));
}
check_size(F, m.width as u64, m.height as u64, &DecodeOptions::default())?;
if m.app1 > MAX_APP1 {
return Err(Error::Malformed(F, "too many APP1 markers".into()));
}
if !matches!(m.components, 1 | 3) || m.precision != 8 {
return Err(Error::Malformed(F, "expected an 8-bit greyscale or RGB image".into()));
}
let non_interleaved = matches!(m.sof, 0xC0 | 0xC1) && m.first_scan_components < m.components;
let raw = if non_interleaved { decode_jpeg_decoder(bytes, &m, None)? } else { decode_zune(bytes, &m)? };
let raw = if non_interleaved {
decode_jpeg_decoder(bytes, &m, None, &DecodeOptions::default())?
} else {
decode_zune(bytes, &m, &DecodeOptions::default())?
};
let ch = raw.model.channels();
match raw.buf {
Buf::U8(v) if v.len() >= raw.width * raw.height * ch => Ok((raw.width, raw.height, ch, v)),
@@ -513,7 +540,7 @@ mod tests {
fn lossless_grayscale_samples_are_native_endian_and_left_justified() {
let bytes = lossless_jpeg(1, 8, 12);
let m = parse_markers(&bytes).unwrap();
let raw = decode_jpeg_decoder(&bytes, &m, None).unwrap();
let raw = decode_jpeg_decoder(&bytes, &m, None, &DecodeOptions::default()).unwrap();
let Buf::U16(v) = &raw.buf else { panic!("expected 16-bit samples") };
assert_eq!(v, &vec![2048u16 << 4; 8], "0x0800 native-endian, left-justified");
assert_eq!((raw.model, raw.bit_depth), (Model::Gray, 16));
@@ -525,7 +552,7 @@ mod tests {
fn lossless_high_precision_rgb_is_16_bit() {
let bytes = lossless_jpeg(3, 2, 12);
let m = parse_markers(&bytes).unwrap();
let raw = decode_jpeg_decoder(&bytes, &m, None).unwrap();
let raw = decode_jpeg_decoder(&bytes, &m, None, &DecodeOptions::default()).unwrap();
let Buf::U16(v) = &raw.buf else { panic!("expected 16-bit samples") };
assert_eq!(v, &vec![2048u16 << 4; 6]);
assert_eq!(raw.model, Model::Rgb);
@@ -536,7 +563,7 @@ mod tests {
fn lossless_high_precision_cmyk_is_16_bit() {
let bytes = lossless_jpeg(4, 2, 12);
let m = parse_markers(&bytes).unwrap();
let raw = decode_jpeg_decoder(&bytes, &m, None).unwrap();
let raw = decode_jpeg_decoder(&bytes, &m, None, &DecodeOptions::default()).unwrap();
let Buf::U16(v) = &raw.buf else { panic!("expected 16-bit samples") };
assert_eq!(v, &vec![2048u16 << 4; 8]);
assert_eq!((raw.model, raw.bit_depth), (Model::Cmyk, 16));
@@ -551,4 +578,77 @@ mod tests {
assert_eq!((d.width, d.height), (8, 1));
assert_eq!(d.bit_depth, 8);
}
/// Rewrites an encoded file's frame-header dimensions: same tables and scan, a hostile
/// annotated size.
fn with_dims(bytes: &[u8], w: u16, h: u16) -> Vec<u8> {
let mut out = bytes.to_vec();
let mut p = 2usize;
while p + 1 < out.len() {
if out[p] != 0xFF {
p += 1;
continue;
}
match out[p + 1] {
0xC0..=0xCF if out[p + 1] != 0xC4 && out[p + 1] != 0xC8 && out[p + 1] != 0xCC => {
out[p + 5..p + 7].copy_from_slice(&h.to_be_bytes());
out[p + 7..p + 9].copy_from_slice(&w.to_be_bytes());
return out;
}
0x01 | 0xD0..=0xD9 => p += 2,
0xFF => p += 1,
_ => {
let Some(l) = out.get(p + 2..p + 4) else { return out };
p += 2 + usize::from(u16::from_be_bytes([l[0], l[1]]));
}
}
}
out
}
/// A file claiming 65535×65535 must be refused for size before zune-jpeg allocates from
/// its frame header (4 GB+ of samples from roughly five hundred bytes of tables).
#[test]
fn zune_refuses_its_own_frame_header_over_the_budget() {
use crate::{ChromaSubsampling, EncodeImage, EncodeMeta, Samples, encode_jpeg};
let pixels = vec![128u8; 32 * 24];
let jpeg = encode_jpeg(&EncodeImage::new(32, 24, 1, Samples::U8(&pixels)), 90, ChromaSubsampling::S444, &EncodeMeta::default()).unwrap();
let hostile = with_dims(&jpeg, 65535, 65535);
assert_ne!(hostile, jpeg);
match crate::decode(&hostile, DecodeOptions::default()) {
Err(Error::TooLarge(w, h)) => assert_eq!((w, h), (65535, 65535)),
other => panic!("unexpected: {other:?}"),
}
}
/// The jpeg-decoder branch (a 4-component file) is checked the same way: its own
/// `read_info` result is what a full-size decode allocates from.
#[test]
fn jpeg_decoder_refuses_its_own_frame_header_over_the_budget() {
use crate::{ChromaSubsampling, EncodeImage, EncodeMeta, Samples, encode_jpeg};
let pixels = vec![128u8; 32 * 24 * 4];
let jpeg = encode_jpeg(&EncodeImage::new(32, 24, 4, Samples::U8(&pixels)), 90, ChromaSubsampling::S444, &EncodeMeta::default()).unwrap();
let hostile = with_dims(&jpeg, 65535, 65535);
match crate::decode(&hostile, DecodeOptions::default()) {
Err(Error::TooLarge(w, h)) => assert_eq!((w, h), (65535, 65535)),
other => panic!("unexpected: {other:?}"),
}
}
/// Extended XMP is reassembled from every APP1 fragment: an unbounded count is quadratic
/// memory in a marker walk, so the count is capped.
#[test]
fn an_app1_flood_is_refused_before_the_decoders_rewalk_it() {
let mut b = vec![0xFF, 0xD8];
// a 1-component SOF0: 8-bit, 2×2, component (1, 0x11, Q0)
b.extend_from_slice(&[0xFF, 0xC0, 0x00, 0x0B, 8, 0, 2, 0, 2, 1, 0x01, 0x11, 0x00]);
for _ in 0..200 {
b.extend_from_slice(&[0xFF, 0xE1, 0x00, 0x02]);
}
b.extend_from_slice(&[0xFF, 0xD9]);
match crate::decode(&b, DecodeOptions::default()) {
Err(Error::Malformed(F, m)) => assert!(m.contains("APP1"), "{m}"),
other => panic!("unexpected: {other:?}"),
}
}
}
+83
View File
@@ -5,6 +5,9 @@ use crate::{DecodeOptions, Decoded, Error, Format, Result};
use image::{DynamicImage, ImageDecoder};
pub(crate) fn decode(bytes: &[u8], format: Format, opts: &DecodeOptions) -> Result<Decoded> {
if format == Format::Gif {
gated_first_frame(bytes, opts)?;
}
let err = |e: image::ImageError| Error::Malformed(format, e.to_string());
let cursor = std::io::Cursor::new(bytes);
let (img, icc) = match format {
@@ -36,3 +39,83 @@ pub(crate) fn decode(bytes: &[u8], format: Format, opts: &DecodeOptions) -> Resu
let raw = Raw { width: w as usize, height: h as usize, model, alpha, premultiplied: false, buf: Buf::U8(buf), bit_depth: 8 };
finish(format, raw, Meta { icc, ..Default::default() }, (w, h), opts)
}
/// Bounds a GIF's first image descriptor: the decoder composites frames into the logical screen
/// (bounded by [`check_size`] on the canvas) but allocates its own frame buffers at the frame's
/// declared size, which a small file can inflate far past the canvas. The canvas check cannot
/// see that size, so it is read from the block headers here.
fn gated_first_frame(bytes: &[u8], opts: &DecodeOptions) -> Result<()> {
// The constants of the header grammar: signature, logical screen descriptor (with its global
// colour table), extension blocks (0x21, skipped by their sub-block chains) and image
// descriptors (0x2C).
const SCREEN: usize = 13;
if bytes.len() < SCREEN {
return Ok(()); // too broken for a frame; the decoder reports the damage itself
}
let gct_entries = 1usize << (u32::from(bytes[10] & 0x07) + 1); // at most 256
let gct_bytes = if (bytes[10] & 0x80) != 0 { gct_entries * 3 } else { 0 };
let mut p = SCREEN.saturating_add(gct_bytes);
while let Some(&kind) = bytes.get(p) {
match kind {
0x2C => {
// Image descriptor: two LE coordinates then two LE edges, after the separator.
let end = p.checked_add(10).filter(|&e| e <= bytes.len());
let Some(d) = end.and_then(|e| bytes.get(p + 1..e)) else {
break; // cut short mid-descriptor: the decoder reports that
};
let (w, h) = (u16::from_le_bytes([d[4], d[5]]) as u64, u16::from_le_bytes([d[6], d[7]]) as u64);
return check_size(Format::Gif, w, h, opts);
}
0x21 => {
// Extension: a label byte, then <len,data> sub-blocks ended by a zero length.
p = p.saturating_add(2);
loop {
let Some(&len) = bytes.get(p) else {
return Ok(()); // ran past the data: decode reports the truncation
};
p = p.saturating_add(1);
if len == 0 {
break;
}
p = p.saturating_add(len as usize);
}
}
_ => p = p.saturating_add(1),
}
}
Ok(())
}
#[cfg(test)]
mod tests {
use crate::{DecodeOptions, Error};
fn gif(blocks: &[u8]) -> Vec<u8> {
let mut b = b"GIF89a".to_vec();
// logical screen: 2×2, no global colour table, background 0, aspect 0
b.extend_from_slice(&[2, 0, 2, 0, 0x00, 0, 0]);
b.extend_from_slice(blocks);
b
}
/// `image`'s GIF decoder allocates its frame buffer at the image descriptor's declared
/// size, which the canvas bound ([`crate::decode`]'s `max_pixels` on the logical screen)
/// does not cover. A 20-byte file used to buy a 4 GB allocation before the gate.
#[test]
fn a_first_frame_over_the_bound_is_refused_without_allocating_it() {
let bytes = gif(&[0x2C, 0, 0, 0, 0, 0xFF, 0xFF, 0xFF, 0xFF, 0, 0x00]);
match crate::decode(&bytes, DecodeOptions::default()) {
Err(Error::TooLarge(w, h)) => assert_eq!((w, h), (65535, 65535)),
other => panic!("unexpected: {other:?}"),
}
}
/// A frame within the bound still reaches the decoder (which reports the truncated data);
/// the gate may only refuse on size.
#[test]
fn a_first_frame_within_the_bound_is_not_refused() {
let bytes = gif(&[0x2C, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0x00]);
let r = crate::decode(&bytes, DecodeOptions::default());
assert!(matches!(r, Err(Error::Malformed(..)) | Err(Error::TooLarge(..))), "{r:?}");
}
}
+64 -1
View File
@@ -7,6 +7,10 @@ use lightcraft_color::{Mat3, RgbSpace, Xy};
const F: Format = Format::Png;
/// Expansion cap for a compressed text chunk's payload: real XMP documents are a few hundred
/// kilobytes, so anything past this is hostile input, not metadata.
const MAX_TEXT_CHUNK: usize = 4 << 20;
fn err(e: impl std::fmt::Display) -> Error {
Error::Malformed(F, e.to_string())
}
@@ -42,11 +46,18 @@ pub(crate) fn decode(bytes: &[u8], opts: &DecodeOptions) -> Result<Decoded> {
};
let raw = Raw { width: out.width as usize, height: out.height as usize, model, alpha, premultiplied: false, buf: samples, bit_depth: depth };
// png's `get_text()` inflates a compressed chunk with no bound of its own: a tiny
// iTXt can claim an XMP document of arbitrary size. Expand through the bounded path
// instead, and drop any XMP bigger than the cap.
let xmp = info
.utf8_text
.iter()
.find(|t| t.keyword == "XML:com.adobe.xmp")
.and_then(|t| t.get_text().ok())
.and_then(|t| {
let mut t = t.clone();
t.decompress_text_with_limit(MAX_TEXT_CHUNK).ok()?;
t.get_text().ok()
})
.or_else(|| info.uncompressed_latin1_text.iter().find(|t| t.keyword == "XML:com.adobe.xmp").map(|t| t.text.clone()));
let exif = info.exif_metadata.as_ref().map(|e| {
let e = e.to_vec();
@@ -168,3 +179,55 @@ fn rgb_to_xyz_d50_checked(space: &RgbSpace) -> Option<Mat3> {
}
Some(crate::space::rgb_to_xyz_d50(space))
}
#[cfg(test)]
mod tests {
use crate::DecodeOptions;
/// A compressed text chunk is expanded by `png` through our own cap: an XMP document
/// bigger than it is dropped, not inflated. Compressing an 8 MiB string is a few
/// kilobytes — the reverse of what the unbounded `get_text()` path allowed.
#[test]
fn compressed_xmp_over_the_cap_is_dropped() {
let out = png_with_compressed_xmp(&"x".repeat(8 << 20));
let d = crate::decode(&out, DecodeOptions::default()).unwrap();
assert!(d.xmp.is_none());
assert_eq!((d.width, d.height), (1, 1));
}
/// XMP within the cap is still read, compressed or not.
#[test]
fn compressed_xmp_under_the_cap_is_read() {
let out = png_with_compressed_xmp(&"<?xpacket?><xmp/>".repeat(100));
let d = crate::decode(&out, DecodeOptions::default()).unwrap();
assert!(d.xmp.as_deref().is_some_and(|x| x.contains("xmp/")));
}
/// A little-endian PNG with one compressed iTXt chunk carrying `payload` as XMP.
fn png_with_compressed_xmp(payload: &str) -> Vec<u8> {
use png::text_metadata::{EncodableTextChunk, ITXtChunk};
let mut base = Vec::new();
{
let mut enc = png::Encoder::new(&mut base, 1, 1);
enc.set_color(png::ColorType::Grayscale);
enc.set_depth(png::BitDepth::Eight);
let mut w = enc.write_header().unwrap();
w.write_image_data(&[0x7F]).unwrap();
w.finish().unwrap();
}
// `ITXtChunk::encode` writes the complete chunk (length, type, payload, CRC), with
// the text compressed.
let mut chunk = Vec::new();
let mut xmp = ITXtChunk::new("XML:com.adobe.xmp", payload);
xmp.compressed = true;
xmp.compress_text().unwrap();
xmp.encode(&mut chunk).unwrap();
// Text chunks after IDAT land in `info` once the stream is finished: insert ours
// before the trailing IEND (a zero-length chunk is exactly 12 bytes).
let split = base.len() - 12;
let mut out = base[..split].to_vec();
out.extend_from_slice(&chunk);
out.extend_from_slice(&base[split..]);
out
}
}
@@ -1,5 +1,5 @@
//! Benchmark-ish, ignored by default. Run with:
//! `cargo test --release -p lightcraft-codecs --test bench -- --ignored --nocapture`
//! `cargo test --release -p lightcraft-codecs --test it bench:: -- --ignored --nocapture`
use lightcraft_codecs::*;
use std::time::Instant;
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
@@ -1,7 +1,6 @@
//! HEIC/HEIF through the codec API (feature `heif`), on lossless synthetic files from
//! `lightcraft_heif::testdata`: an iPhone-like grid with a Display P3 profile, rotation, EXIF, a
//! thumbnail and an HDR gain map; `nclx`-only colour; 10-bit; and hostile input.
#![cfg(feature = "heif")]
use lightcraft_codecs::*;
use lightcraft_heif::testdata::{Prop, Spec, build};
@@ -1,5 +1,3 @@
#![cfg(all(feature = "jxl", not(target_arch = "wasm32")))]
use lightcraft_codecs::exif::minimal_exif;
use lightcraft_codecs::*;
use lightcraft_raster::Rgba8;
+13
View File
@@ -0,0 +1,13 @@
//! The integration tests, one module per file, linked into one test binary.
//! `tests/jxl_tail_heap.rs` stays a binary of its own: it installs dhat as the global allocator.
mod bench;
mod corpus;
mod float_tiff;
mod fuzz;
mod header;
#[cfg(feature = "heif")]
mod heif;
#[cfg(all(feature = "jxl", not(target_arch = "wasm32")))]
mod jxl_streaming;
mod roundtrip;
File renamed without changes.
+1
View File
@@ -24,3 +24,4 @@ web-time = { workspace = true }
[dev-dependencies]
proptest = { workspace = true }
dhat = { workspace = true }
+35 -2
View File
@@ -9,7 +9,7 @@
//! compressed length and CRC-32, then the strips' bytes. `key` says what the product was made from (the photo's
//! content, the model, this code's version, the settings it depends on); a file with another key is stale.
use std::io::{Read, Write};
use std::io::{Read, Seek, Write};
use std::path::Path;
use lightcraft_raster::Rgb32f;
@@ -216,6 +216,10 @@ fn read_start(r: &mut impl Read) -> Result<Start, ProductError> {
r.read_exact(&mut e)?;
let l = u32::from_le_bytes([e[0], e[1], e[2], e[3]]) as usize;
let c = u32::from_le_bytes([e[4], e[5], e[6], e[7]]);
// a strip never compresses to nothing (it has at least one row, so at least one deflated pixel)
if l == 0 {
return Err(ProductError::Format("a strip is empty".into()));
}
// a strip never compresses to much more than its raw size
if l > 12 * h.strip * h.width + 4096 {
return Err(ProductError::Format("a strip is larger than it can be".into()));
@@ -305,7 +309,6 @@ pub fn read_window(path: &Path, key: &str, window: Option<Window>, factor: usize
}
// the picture rows and columns the blocks cover
let (x_end, y_end) = if whole { (w.x + w.width, w.y + w.height) } else { (w.x + ow * factor, w.y + oh * factor) };
let mut out = Rgb32f { width: ow, height: oh, data: allocate(ow * oh, [0.0; 3])? };
let lut = f16_table();
let value = |raw: &[u8], c: usize, n: usize, i: usize| -> f32 {
let (lo, hi) = (raw.get(2 * c * n + i).copied().unwrap_or(0), raw.get((2 * c + 1) * n + i).copied().unwrap_or(0));
@@ -318,6 +321,21 @@ pub fn read_window(path: &Path, key: &str, window: Option<Window>, factor: usize
let strips: Vec<usize> = (0..table.len()).filter(|&s| wanted(s)).collect();
// skip the strips before the first one wanted
let skipped: usize = table.iter().take(strips.first().copied().unwrap_or(0)).map(|e| e.0).sum();
// The output is reserved only after the wanted strips' compressed bytes are present and match their checksums:
// a malformed cache whose strips are empty, damaged or short are rejected here, before the (large) result is
// allocated, not after. Reading them twice is cheap next to decoding them, and the fold below re-seeks here.
let strips_at = f.stream_position()?;
f.seek_relative(i64::try_from(skipped).map_err(|_| ProductError::Format("the file is too large".into()))?)?;
for &s in &strips {
let Some(&(len, expected)) = table.get(s) else { return Err(ProductError::Format("a strip is missing".into())) };
let mut packed = allocate(len, 0u8)?;
f.read_exact(&mut packed)?;
if crc(&packed) != expected {
return Err(ProductError::Format(format!("strip {s} is damaged")));
}
}
let mut out = Rgb32f { width: ow, height: oh, data: allocate(ow * oh, [0.0; 3])? };
f.seek(std::io::SeekFrom::Start(strips_at))?;
f.seek_relative(i64::try_from(skipped).map_err(|_| ProductError::Format("the file is too large".into()))?)?;
for group in strips.chunks(GROUP) {
let mut packed = Vec::with_capacity(group.len());
@@ -422,6 +440,21 @@ mod tests {
let _ = std::fs::remove_file(p);
}
#[test]
fn an_empty_strip_entry_is_rejected_by_the_header() {
let p = temp("empty-strip");
let header = serde_json::to_vec(&Header { width: 16, height: 1, strip: STRIP_ROWS, key: "k".into() }).unwrap();
let mut bytes = Vec::from(MAGIC.as_slice());
bytes.extend_from_slice(&[VERSION, 0, 0, 0]);
bytes.extend_from_slice(&(header.len() as u32).to_le_bytes());
bytes.extend_from_slice(&header);
// one strip with a zero-length entry: it has a row, so its deflated bytes are never empty
bytes.extend_from_slice(&[0; 8]);
std::fs::write(&p, bytes).unwrap();
assert!(matches!(read_header_at(&p), Err(ProductError::Format(why)) if why.contains("empty")));
let _ = std::fs::remove_file(p);
}
fn temp(name: &str) -> std::path::PathBuf {
std::env::temp_dir().join(format!("lc-denoise-{name}-{}.lcdn", std::process::id()))
}
+50
View File
@@ -0,0 +1,50 @@
//! Peak heap of a `read_window` that must reject a product's strips (`crates/denoise-core/src/product.rs`), measured
//! with dhat's testing mode. The result is allocated only after the wanted strips' checksums are verified, so a cache
//! whose compressed bytes are damaged or absent fails without first reserving the (multi-megabyte) output. One test
//! per binary (dhat's profiler is global).
use lightcraft_denoise_core::product::read_window;
#[global_allocator]
static ALLOC: dhat::Alloc = dhat::Alloc;
fn temp(name: &str) -> std::path::PathBuf {
std::env::temp_dir().join(format!("lc-denoise-{name}-{}.lcdn", std::process::id()))
}
/// A 2000 × 1024 product of key `k` (a 24 MB result): its 8 strips each claim 8 bytes that do not match their
/// checksum, and the file is exactly the length its header and table declare. A 24 MB peak means the output was
/// reserved; the cap here bounds the header, the table and the strips' own bytes.
fn damaged_product() -> Vec<u8> {
let header = br#"{"width":2000,"height":1024,"strip":128,"key":"k"}"#;
const STRIP_LEN: usize = 8;
let mut bytes = Vec::from(b"LCDN".as_slice());
bytes.extend_from_slice(&[1, 0, 0, 0]);
bytes.extend_from_slice(&(header.len() as u32).to_le_bytes());
bytes.extend_from_slice(header);
let expected_len = 12 + header.len() + 8 * 8 + 8 * STRIP_LEN;
for _ in 0..8 {
// 8 claimed bytes whose recorded checksum does not match theirs (0xAA bytes do not have this CRC)
bytes.extend_from_slice(&(STRIP_LEN as u32).to_le_bytes());
bytes.extend_from_slice(&[0xAA, 0xBB, 0xCC, 0xDD]);
}
for _ in 0..8 {
bytes.extend_from_slice(&[0xAAu8; STRIP_LEN]);
}
assert_eq!(bytes.len(), expected_len, "the crafted file lies about its length");
bytes
}
#[test]
fn a_damaged_product_is_rejected_before_its_output_is_allocated() {
let bytes = damaged_product();
let p = temp("damaged-product-heap");
std::fs::write(&p, &bytes).unwrap();
let _profiler = dhat::Profiler::builder().testing().build();
let why = read_window(&p, "k", None, 1, None).err().unwrap().to_string();
let peak = dhat::HeapStats::get().max_bytes;
let _ = std::fs::remove_file(p);
assert!(why.contains("damaged"), "{why}");
eprintln!("peak heap {peak} bytes");
assert!(peak < 4 << 20, "peak heap {peak} bytes: the output was reserved before the strips were checked");
}
+71 -5
View File
@@ -4,6 +4,12 @@
use crate::net::{Net, NetError, Op, TensorId};
use crate::run::{Error, TileRunner};
/// The tensors' size cap: the same one the real runner's buffers share (`crates/denoise/src/cpu.rs`). A hostile `Net`
/// only ever has to satisfy the final shape: a `ConvT2` doubles the sides and a `MaxPool` halves them, so a small
/// accepted graph can zoom its intermediates far out and back. Every tensor is counted here, and a run whose layers
/// would retain more than one real tile's whole working set is refused before the memory behind it is reserved.
const MAX_VALUES: usize = 128 * 1024 * 1024;
/// A tensor in `[channels][side][side]` order.
struct T {
channels: usize,
@@ -11,12 +17,36 @@ struct T {
data: Vec<f32>,
}
/// A tensor's plain float count, in bounds.
fn count(channels: usize, side: usize) -> Result<usize, NetError> {
channels
.checked_mul(side)
.and_then(|n| n.checked_mul(side))
.filter(|&n| n > 0 && n <= MAX_VALUES)
.ok_or_else(|| NetError::Invalid("a tensor has no size or is too large".into()))
}
/// A zeroed tensor, keeping the memory the run keeps alive in bounds (`tensors` retains every layer's result).
fn tensor(channels: usize, side: usize, total: &mut usize) -> Result<Vec<f32>, NetError> {
let n = count(channels, side)?;
*total = (*total).saturating_add(n);
if *total > MAX_VALUES {
return Err(NetError::Invalid("the tile needs too much working memory".into()));
}
let mut data = Vec::new();
data.try_reserve_exact(n).map_err(|_| NetError::Invalid("not enough memory for a reference tile".into()))?;
data.resize(n, 0.0);
Ok(data)
}
/// Run `net` on `input` (`in_channels` planes of `tile × tile`).
pub fn run(net: &Net, tile: usize, input: &[f32]) -> Result<Vec<f32>, NetError> {
if !net.fits_tile(tile) || input.len() != net.in_channels * tile * tile {
return Err(NetError::Invalid("the tile does not fit the network".into()));
}
let mut tensors: Vec<T> = vec![T { channels: net.in_channels, side: tile, data: input.to_vec() }];
let input = input.to_vec();
let mut total = input.len();
let mut tensors = vec![T { channels: net.in_channels, side: tile, data: input }];
let get = |tensors: &[T], id: TensorId| -> Result<usize, NetError> {
if id < tensors.len() { Ok(id) } else { Err(NetError::Invalid("a layer reads a tensor that does not exist yet".into())) }
};
@@ -27,7 +57,7 @@ pub fn run(net: &Net, tile: usize, input: &[f32]) -> Result<Vec<f32>, NetError>
let b = c.src2.map(|s| get(&tensors, s)).transpose()?;
let side = tensors[a].side;
let pad = (c.k - 1) / 2;
let mut data = vec![0f32; c.cout * side * side];
let mut data = tensor(c.cout, side, &mut total)?;
// the input channels in order: the first source's, then the second's
let planes: Vec<&[f32]> =
std::iter::once(&tensors[a]).chain(b.map(|b| &tensors[b])).flat_map(|t| t.data.chunks(side * side)).collect();
@@ -56,7 +86,7 @@ pub fn run(net: &Net, tile: usize, input: &[f32]) -> Result<Vec<f32>, NetError>
let a = get(&tensors, c.src)?;
let side = tensors[a].side;
let out_side = 2 * side;
let mut data = vec![0f32; c.cout * out_side * out_side];
let mut data = tensor(c.cout, out_side, &mut total)?;
for co in 0..c.cout {
for y in 0..side {
for x in 0..side {
@@ -77,7 +107,7 @@ pub fn run(net: &Net, tile: usize, input: &[f32]) -> Result<Vec<f32>, NetError>
Op::MaxPool2 { src } => {
let a = get(&tensors, *src)?;
let (channels, side) = (tensors[a].channels, tensors[a].side / 2);
let mut data = vec![0f32; channels * side * side];
let mut data = tensor(channels, side, &mut total)?;
for ch in 0..channels {
for y in 0..side {
for x in 0..side {
@@ -92,7 +122,7 @@ pub fn run(net: &Net, tile: usize, input: &[f32]) -> Result<Vec<f32>, NetError>
let a = get(&tensors, *src)?;
let (channels, side) = (tensors[a].channels / 4, tensors[a].side);
let out_side = 2 * side;
let mut data = vec![0f32; channels * out_side * out_side];
let mut data = tensor(channels, out_side, &mut total)?;
for ch in 0..channels {
for y in 0..side {
for x in 0..side {
@@ -131,3 +161,39 @@ impl TileRunner for ReferenceRunner {
run(&self.net, self.tile, input).map_err(|e| Error::Runtime(e.to_string()))
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::net::ConvT2;
fn convt(src: TensorId, cin: usize, cout: usize) -> Op {
Op::ConvT2(ConvT2 { src, cin, cout, weight: vec![0.1; cin * cout * 4], bias: vec![0.0; cout], leaky: None })
}
/// A graph that fits the final-shape check while zooming its intermediates out and back: 256 `ConvT2`s double
/// the sides (256 tiers), 255 `MaxPool`s halve them back to the layer above the input.
fn hostile() -> Net {
let mut ops = Vec::new();
let mut last = 0usize;
for _ in 0..256 {
ops.push(convt(last, 1, 1));
last += 1;
}
for _ in 0..255 {
ops.push(Op::MaxPool2 { src: last });
last += 1;
}
Net::new(1, ops).unwrap()
}
/// The zoomed-out layers' memory was never reserved: either one tensor or the retained whole (the two bounds
/// share the cap) says why the run stops.
#[test]
fn a_graph_that_zooms_its_intermediates_out_and_back_is_refused_before_their_memory_is_reserved() {
assert!(matches!(
run(&hostile(), 1, &[1.0]),
Err(NetError::Invalid(why)) if why.contains("too large") || why.contains("too much working memory")
));
}
}
+4
View File
@@ -0,0 +1,4 @@
//! The integration tests, one module per file, linked into one test binary.
#[cfg(feature = "runtime")]
mod real_model;
@@ -2,7 +2,7 @@
//!
//! ```text
//! LC_DENOISE_MODEL=<model_bayer.onnx> LC_DENOISE_MOSAICS=<folder> LC_DENOISE_OUT=<folder> \
//! cargo test --release -p lightcraft-denoise --features runtime --test real_model -- --ignored --nocapture
//! cargo test --release -p lightcraft-denoise --features runtime --test it real_model:: -- --ignored --nocapture
//! ```
//!
//! `LC_DENOISE_MOSAICS` holds `<name>.mosaic.f32` (little-endian f32, normalised: black 0, white 1, not white
@@ -11,8 +11,6 @@
//! once, default 8), `LC_DENOISE_ONLY` (one name), `LC_DENOISE_ISO` (noise strength, default 1.0; 0 adds none),
//! `LC_DENOISE_FULL` (the whole frame instead of a 2048 × 1536 crop).
#![cfg(feature = "runtime")]
use std::path::{Path, PathBuf};
use std::time::Instant;
+1 -1
View File
@@ -39,7 +39,7 @@ thiserror = { workspace = true }
log = { workspace = true }
ab_glyph = { workspace = true }
# Apply the font’s vertical substitutions and origins to Japanese watermarks.
harfrust = { version = "0.12", default-features = false, features = ["std"] }
harfrust = { version = "0.13", default-features = false, features = ["std"] }
unicode-segmentation = "1.13"
lightcraft-segment = { workspace = true, optional = true }
+347 -28
View File
@@ -7,6 +7,16 @@
//! there"; the render worker finds out if it isn't), known answers are rechecked after
//! [`RECHECK`]. Everywhere else (CLI, MCP, tests) the default is synchronous: every question is a
//! fresh check, as before.
//!
//! The same worker can check a list of files, such as every library photo's, now and then
//! ([`Availability::sweep`]), one at a time and only while nothing is asked. That is for the
//! Missing Photos count ([`Availability::is_gone`]): one stream of checks on a share, not one per
//! thing that wants to know. The app starts the sweeps; nothing here does.
//!
//! A sweep of a large library on a share takes tens of minutes, and its `stat`s compete with the
//! reads of an import or a scan on the same share. So the worker starts no sweep check while the
//! caller says files are busy ([`Availability::sweep_yields_to`]); what is asked still gets
//! answered (within [`YIELD_POLL`]), and the sweep goes on where it stopped when the work ends.
use std::collections::{HashMap, HashSet, VecDeque};
use std::sync::{Arc, Mutex, MutexGuard, PoisonError};
@@ -14,9 +24,16 @@ use std::time::Duration;
/// Checks whether a path exists (injectable: tests use a slow one).
pub type ExistsProbe = Arc<dyn Fn(&str) -> bool + Send + Sync>;
/// Called (from the worker) when an answer changed, e.g. to repaint the UI.
/// Called (from the worker) when an answer changed, e.g. to repaint the UI (a sweep's first look
/// at a file that is there changes no answer anyone sees, so it wakes nobody).
pub type Waker = Arc<dyn Fn() + Send + Sync>;
/// Whether file work that a sweep should not compete with is going on.
pub type Busy = Arc<dyn Fn() -> bool + Send + Sync>;
/// How often a sweep that is giving way looks whether the work has ended.
const YIELD_POLL: Duration = Duration::from_millis(250);
/// How long a cached answer is used before it is checked again (it is still returned meanwhile).
pub const RECHECK: Duration = Duration::from_secs(15);
@@ -31,9 +48,17 @@ struct State {
known: HashMap<String, (bool, std::time::Instant)>,
queue: VecDeque<String>,
queued: HashSet<String>,
/// What a sweep has left to check: after the queue, whatever it holds.
sweep: VecDeque<String>,
/// The files found not there (and not found there since).
gone: HashSet<String>,
worker: bool,
/// Bumped whenever an answer changes (callers recompute what depends on it).
generation: u64,
/// Bumped when the sweep under way is dropped for another one ([`Availability::restart_sweeps`]).
sweep_epoch: u64,
/// Whether file work a sweep gives way to is going on.
busy: Option<Busy>,
/// Bumped whenever a file is found gone, or found back.
gone_generation: u64,
}
fn lock(m: &Mutex<State>) -> MutexGuard<'_, State> {
@@ -66,7 +91,8 @@ impl Availability {
self.probe = probe;
let mut st = lock(&self.state);
st.known.clear();
st.generation += 1;
st.gone.clear();
st.gone_generation += 1;
}
/// Answer from the cache and check on a worker thread from now on; `wake` runs when an
@@ -78,6 +104,14 @@ impl Availability {
self.background = Some(wake);
}
/// The sweep starts no check while `busy` says so (the caller's import, scan or Local read in
/// flight); asked files are answered all the same, and the sweep resumes where it was left when
/// `busy` turns false. `busy` should read state that a task's guard owns (the activity stack), so
/// a task that never ends cleanly can't hold the sweep for good.
pub fn sweep_yields_to(&self, busy: Busy) {
lock(&self.state).busy = Some(busy);
}
/// The existence check itself (blocking: for worker threads, e.g. the Rename preview's).
pub fn probe(&self) -> ExistsProbe {
self.probe.clone()
@@ -99,12 +133,7 @@ impl Availability {
if cached.is_none_or(|(_, at)| now.saturating_duration_since(at) >= RECHECK) && !st.queued.contains(path) {
st.queued.insert(path.to_string());
st.queue.push_back(path.to_string());
if !st.worker {
st.worker = true;
drop(st);
self.spawn_worker(wake.clone());
return cached.map(|c| c.0);
}
self.start(st, wake);
}
cached.map(|c| c.0)
}
@@ -114,9 +143,10 @@ impl Availability {
self.exists(path) == Some(false)
}
/// Changes whenever a cached answer changes.
pub fn generation(&self) -> u64 {
lock(&self.state).generation
/// Changes whenever a file is found gone or found back: what [`Availability::is_offline`]
/// says changed for some file. (A file found there at its first check changes nothing.)
pub fn gone_generation(&self) -> u64 {
lock(&self.state).gone_generation
}
/// Paths waiting for the worker.
@@ -124,37 +154,123 @@ impl Availability {
lock(&self.state).queue.len()
}
/// Check `paths` too, in the background and after whatever is asked meanwhile, skipping those
/// checked less than [`RECHECK`] ago. One sweep at a time: `false` (and nothing queued) while
/// one is under way, or in synchronous mode. A sweep's answers wake only when a file is found
/// gone or back.
pub fn sweep(&self, paths: Vec<String>) -> bool {
self.sweep_checked(None, paths)
}
fn sweep_checked(&self, epoch: Option<u64>, paths: Vec<String>) -> bool {
let Some(wake) = &self.background else { return false };
let mut st = lock(&self.state);
if !st.sweep.is_empty() || epoch.is_some_and(|e| e != st.sweep_epoch) {
return false;
}
st.sweep = paths.into();
self.start(st, wake);
true
}
/// Drop what the sweep under way has left (another library was opened: its files are not the
/// ones to check now) and say which sweeps may start from here ([`Availability::sweep_in`]): a
/// list of files being made for the old one is then refused.
pub fn restart_sweeps(&self) -> u64 {
let mut st = lock(&self.state);
st.sweep.clear();
st.sweep_epoch += 1;
st.sweep_epoch
}
/// [`Availability::sweep`], unless [`Availability::restart_sweeps`] was called since it
/// returned `epoch`.
pub fn sweep_in(&self, epoch: u64, paths: Vec<String>) -> bool {
self.sweep_checked(Some(epoch), paths)
}
/// Whether the file at `path` was found gone (and not found there since): `false` for a file
/// not checked yet.
pub fn is_gone(&self, path: &str) -> bool {
lock(&self.state).gone.contains(path)
}
/// How many files are found gone.
pub fn gone_len(&self) -> usize {
lock(&self.state).gone.len()
}
/// What the sweep under way has left to check (0: none under way, though its last file may still
/// be in flight).
pub fn sweeping(&self) -> usize {
lock(&self.state).sweep.len()
}
/// Start the worker, unless it runs.
fn start(&self, mut st: MutexGuard<'_, State>, wake: &Waker) {
if !st.worker {
st.worker = true;
drop(st);
self.spawn_worker(wake.clone());
}
}
fn spawn_worker(&self, wake: Waker) {
let (state, probe) = (self.state.clone(), self.probe.clone());
let started = std::thread::Builder::new().name("lc-availability".into()).spawn(move || {
loop {
let path = {
// what was asked first; a sweep's next file when nothing is (unless it is asked
// about too, or was checked a moment ago)
let (path, asked) = {
let mut st = lock(&state);
let Some(p) = st.queue.pop_front() else {
st.worker = false;
return;
};
p
let giving_way = if st.queue.is_empty() && !st.sweep.is_empty() { st.busy.clone() } else { None };
if let Some(busy) = giving_way {
drop(st);
if busy() {
// (the worker stays: a question asked meanwhile is answered within YIELD_POLL)
std::thread::sleep(YIELD_POLL);
continue;
}
st = lock(&state);
}
match st.queue.pop_front() {
Some(p) => (p, true),
None => loop {
let Some(p) = st.sweep.pop_front() else {
st.worker = false;
return;
};
let fresh = st.known.get(&p).is_some_and(|(_, at)| at.elapsed() < RECHECK);
if !fresh && !st.queued.contains(&p) {
break (p, false);
}
},
}
};
let ok = probe(&path);
let changed = {
let wakes = {
let mut st = lock(&state);
st.queued.remove(&path);
if asked {
st.queued.remove(&path);
}
let gone_changed = if ok { st.gone.remove(&path) } else { st.gone.insert(path.clone()) };
let prev = st.known.insert(path, (ok, std::time::Instant::now()));
let changed = prev.map(|p| p.0) != Some(ok);
if changed {
st.generation += 1;
if gone_changed {
st.gone_generation += 1;
}
changed
(asked && changed) || gone_changed
};
if changed {
if wakes {
wake();
}
}
});
if started.is_err() {
// try again on the next question
lock(&self.state).worker = false;
// try again on the next question; a sweep nobody works on would refuse every new one
let mut st = lock(&self.state);
st.worker = false;
st.sweep.clear();
}
}
}
@@ -210,6 +326,209 @@ mod tests {
assert!(a.is_offline("gone") && !a.is_offline("here") && !a.is_offline("unknown"));
assert_eq!(on_caller.load(Ordering::SeqCst), 0, "never probed on the calling thread");
assert!(woke.load(Ordering::SeqCst) >= 2);
assert!(a.generation() >= 2);
assert!(a.gone_generation() >= 2);
}
fn wait_until(what: &str, done: impl Fn() -> bool) {
let deadline = std::time::Instant::now() + Duration::from_secs(20);
while !done() {
assert!(std::time::Instant::now() < deadline, "timed out waiting: {what}");
std::thread::sleep(Duration::from_millis(2));
}
}
fn held<T>(m: &Mutex<T>) -> MutexGuard<'_, T> {
m.lock().unwrap_or_else(PoisonError::into_inner)
}
/// A background `Availability` whose probe logs what it checks; files named `gone…` are not there.
fn logged() -> (Availability, Arc<Mutex<Vec<String>>>, Arc<AtomicUsize>) {
let mut a = Availability::default();
let log = Arc::new(Mutex::new(Vec::new()));
let l = log.clone();
a.set_probe(Arc::new(move |p| {
held(&l).push(p.to_string());
!p.starts_with("gone")
}));
let woke = Arc::new(AtomicUsize::new(0));
let w = woke.clone();
a.run_in_background(Arc::new(move || {
w.fetch_add(1, Ordering::SeqCst);
}));
(a, log, woke)
}
#[test]
fn the_files_a_sweep_finds_gone_are_counted() {
let (a, _, _) = logged();
assert!(a.sweep(vec!["here".into(), "gone 1".into(), "also here".into(), "gone 2".into()]));
wait_until("the sweep", || a.sweeping() == 0 && a.pending() == 0);
wait_until("the answers", || a.gone_len() == 2);
assert!(a.is_gone("gone 1") && a.is_gone("gone 2") && !a.is_gone("here") && !a.is_gone("also here"));
}
#[test]
fn files_found_there_at_first_look_change_nothing_for_missing_photos() {
let (a, _, woke) = logged();
let before = a.gone_generation();
a.sweep(vec!["a".into(), "b".into(), "c".into()]);
wait_until("the answers", || ["a", "b", "c"].iter().all(|p| lock(&a.state).known.contains_key(*p)));
assert_eq!(a.gone_generation(), before, "nothing went missing");
assert_eq!(woke.load(Ordering::SeqCst), 0, "a sweep finding files there wakes nobody");
a.sweep(vec!["gone".into()]);
wait_until("the gone file", || a.gone_generation() != before);
wait_until("the wake", || woke.load(Ordering::SeqCst) == 1);
}
#[test]
fn what_is_asked_is_checked_before_the_rest_of_a_sweep_and_one_sweep_runs_at_a_time() {
let mut a = Availability::default();
let log = Arc::new(Mutex::new(Vec::<String>::new()));
// the first file of the sweep holds the worker until the test has asked about another
let gate = Arc::new((Mutex::new(false), std::sync::Condvar::new()));
let (l, g) = (log.clone(), gate.clone());
a.set_probe(Arc::new(move |p| {
if p == "sweep 0" {
let (open, cv) = &*g;
let mut open = held(open);
while !*open {
open = cv.wait(open).unwrap_or_else(PoisonError::into_inner);
}
}
held(&l).push(p.to_string());
true
}));
a.run_in_background(Arc::new(|| {}));
assert!(a.sweep((0..50).map(|i| format!("sweep {i}")).collect()));
assert!(!a.sweep(vec!["another".into()]), "a sweep under way: the next one waits for its turn");
assert_eq!(a.exists("asked"), None, "not known yet");
*held(&gate.0) = true;
gate.1.notify_all();
wait_until("the log", || held(&log).len() == 51);
let log = held(&log);
// (at most the file the worker was already checking comes first)
assert!(matches!(log.iter().position(|p| p == "asked"), Some(0 | 1)), "asked next, ahead of the sweep: {log:?}");
assert!(!log.iter().any(|p| p == "another"));
}
#[test]
fn a_new_sweep_replaces_the_one_under_way_and_a_list_made_for_the_old_one_is_refused() {
let mut a = Availability::default();
let log = Arc::new(Mutex::new(Vec::<String>::new()));
// the old sweep's first file holds the worker until the new one is in place
let gate = Arc::new((Mutex::new(false), std::sync::Condvar::new()));
let (l, g) = (log.clone(), gate.clone());
a.set_probe(Arc::new(move |p| {
if p == "old 0" {
let (open, cv) = &*g;
let mut open = held(open);
while !*open {
open = cv.wait(open).unwrap_or_else(PoisonError::into_inner);
}
}
held(&l).push(p.to_string());
true
}));
a.run_in_background(Arc::new(|| {}));
let old = a.restart_sweeps();
assert!(a.sweep_in(old, (0..20).map(|i| format!("old {i}")).collect()));
wait_until("the worker takes the first file", || a.sweeping() == 19);
// another library is opened: what is left of the old sweep goes, and a list still being made for it too
let new = a.restart_sweeps();
assert_eq!(a.sweeping(), 0);
assert!(!a.sweep_in(old, vec!["old late".into()]), "a list made for the old library is refused");
assert!(a.sweep_in(new, (0..3).map(|i| format!("new {i}")).collect()));
*held(&gate.0) = true;
gate.1.notify_all();
wait_until("the new sweep", || held(&log).len() == 4);
assert_eq!(*held(&log), ["old 0", "new 0", "new 1", "new 2"]);
}
#[test]
fn files_found_gone_are_told_apart_from_files_never_checked() {
let (a, _, _) = logged();
assert!(!a.is_gone("gone 1") && a.gone_len() == 0);
assert!(a.sweep(vec!["gone 1".into(), "here".into()]));
wait_until("the sweep", || a.sweeping() == 0 && a.pending() == 0 && a.gone_len() == 1);
assert!(a.is_gone("gone 1") && !a.is_gone("here") && !a.is_gone("never asked"));
}
fn busy_flag(a: &Availability) -> Arc<std::sync::atomic::AtomicBool> {
let busy = Arc::new(std::sync::atomic::AtomicBool::new(true));
let b = busy.clone();
a.sweep_yields_to(Arc::new(move || b.load(Ordering::SeqCst)));
busy
}
#[test]
fn a_sweep_gives_way_to_file_work_answers_what_is_asked_and_resumes_after() {
let (a, log, _) = logged();
let busy = busy_flag(&a);
assert!(a.sweep((0..20).map(|i| format!("sweep {i}")).collect()));
std::thread::sleep(YIELD_POLL * 3);
assert!(held(&log).is_empty(), "no sweep check while files are busy");
assert_eq!(a.sweeping(), 20, "nothing of the sweep is lost");
assert_eq!(a.exists("asked"), None);
wait_until("the answer", || a.exists("asked") == Some(true));
assert_eq!(*held(&log), ["asked"], "what is asked is answered, the sweep still waits");
busy.store(false, Ordering::SeqCst);
wait_until("the sweep", || a.sweeping() == 0);
wait_until("the log", || held(&log).len() == 21);
// it went on from its first file, not from scratch
assert_eq!(held(&log).iter().filter(|p| *p == "sweep 0").count(), 1);
}
#[test]
fn a_sweep_stopped_part_way_goes_on_from_there() {
let mut a = Availability::default();
let log = Arc::new(Mutex::new(Vec::<String>::new()));
let l = log.clone();
// the worker is held inside the fourth check until the sweep is told to give way
let hold = Arc::new(std::sync::atomic::AtomicBool::new(true));
let h = hold.clone();
a.set_probe(Arc::new(move |p| {
while p == "sweep 3" && h.load(Ordering::SeqCst) {
std::thread::sleep(Duration::from_millis(1));
}
held(&l).push(p.to_string());
true
}));
a.run_in_background(Arc::new(|| {}));
let busy = busy_flag(&a);
busy.store(false, Ordering::SeqCst);
assert!(a.sweep((0..100).map(|i| format!("sweep {i}")).collect()));
wait_until("a few checks", || held(&log).len() >= 3);
busy.store(true, Ordering::SeqCst);
hold.store(false, Ordering::SeqCst);
std::thread::sleep(YIELD_POLL * 2);
let stopped = held(&log).len();
assert_eq!(stopped, 4, "stopped part way, after the check it was in");
std::thread::sleep(YIELD_POLL * 2);
assert_eq!(held(&log).len(), stopped, "paused");
assert_eq!(a.sweeping(), 100 - stopped, "what is left is kept");
busy.store(false, Ordering::SeqCst);
wait_until("the sweep", || a.sweeping() == 0 && held(&log).len() == 100);
let mut all = held(&log).clone();
all.sort();
all.dedup();
assert_eq!(all.len(), 100, "every file checked once, none again from the start");
}
#[test]
fn a_sweep_that_yields_to_nothing_runs_as_before() {
let (a, log, _) = logged();
assert!(a.sweep(vec!["a".into(), "b".into()]));
wait_until("the sweep", || held(&log).len() == 2);
}
#[test]
fn a_sweep_skips_files_checked_a_moment_ago() {
let (a, log, _) = logged();
assert_eq!(a.exists("a"), None);
wait_until("the answer", || a.exists("a").is_some());
a.sweep(vec!["a".into(), "b".into()]);
wait_until("b", || held(&log).iter().any(|p| p == "b"));
wait_until("the sweep", || a.sweeping() == 0);
assert_eq!(held(&log).iter().filter(|p| *p == "a").count(), 1, "`a` was checked a moment ago");
}
}
+6 -2
View File
@@ -3066,10 +3066,14 @@ mod tests {
let bundled: crate::camera_profiles::CameraProfile = serde_json::from_str(json).unwrap();
let used = crate::camera_profiles::get(model).expect("a profile for the model");
assert_eq!(*used, bundled, "a local {model} profile overrides the built-in one; remove it to run this test");
let raw = lightcraft_raw::decode(&bytes).unwrap();
let mut raw = lightcraft_raw::decode(&bytes).unwrap();
assert_eq!(raw.metadata.model.as_deref(), Some(model));
// as the app does (`files.rs`): the camera's own lens warp pairs the sensor with its JPEG (Nikon's in-file
// distortion for the Z 6II and Z 8), and the lens corrections leave the opcodes before the proxy is binned
let lens = lens_geometry(&raw.opcodes.list3);
raw.opcodes.list3.retain(|op| !op.is_lens_correction());
let transform = lightcraft_raw::color::camera_transform(&raw, lightcraft_raw::color::as_shot_white_xy(&raw));
let look = fit_preview(&raw, &bytes, &transform, None).0.expect("fit accepted");
let look = fit_preview(&raw, &bytes, &transform, lens.as_ref()).0.expect("fit accepted");
let expected = bundled.matrix().mul(&transform.matrix.inverse().unwrap());
for (row, want) in look.matrix.0.iter().zip(expected.0) {
for (got, want) in row.iter().zip(want) {
+1
View File
@@ -28,6 +28,7 @@ pub const BUNDLED: &[(&str, &str)] = &[
("ILCA-99M2", include_str!("../../../assets/camera-profiles/ILCA-99M2.json")),
("ILCE-7CR", include_str!("../../../assets/camera-profiles/ILCE-7CR.json")),
("ILCE-7M4", include_str!("../../../assets/camera-profiles/ILCE-7M4.json")),
("ILCE-7RM5", include_str!("../../../assets/camera-profiles/ILCE-7RM5.json")),
("NIKON D5300", include_str!("../../../assets/camera-profiles/NIKON_D5300.json")),
("NIKON Z 6_2", include_str!("../../../assets/camera-profiles/NIKON_Z_6_2.json")),
("NIKON Z 8", include_str!("../../../assets/camera-profiles/NIKON_Z_8.json")),
+3
View File
@@ -482,6 +482,9 @@ pub(crate) fn follow_folder(s: &mut Session, from: &str, to: &str) {
// a library folder chosen at or below it follows too, as a source or as a filter
follow_library_folder(&mut s.library_folder, from, to);
follow_library_folder(&mut s.filter.library_folder, from, to);
if let Some(f) = s.shelved_filter.as_mut() {
follow_library_folder(&mut f.library_folder, from, to);
}
if let Some(b) = &mut s.browse
&& let Ok(rel) = Path::new(&b.path).strip_prefix(from)
{
+30 -8
View File
@@ -9,10 +9,12 @@ use serde_json::{Value, json};
use super::{CommandSpec, always, cmd};
use crate::{Result, Session};
/// Seconds between shots of the same burst at most.
const BURST_GAP: i64 = 10;
/// Signature similarity for "the same scene".
const SAME: f32 = 0.93;
/// Seconds between shots of the same burst at most (default of `burstGap`).
const BURST_GAP: f64 = 10.0;
/// Signature similarity for "the same scene" (default of `similarity`).
const SAME: f64 = 0.93;
/// The longest `burstGap` accepted: a day.
const MAX_GAP: f64 = 86_400.0;
fn secs(iso: &str) -> Option<i64> {
let d = lightcraft_meta::DateTime::parse_iso(iso)?;
@@ -26,6 +28,7 @@ fn analyze(s: &mut Session, p: &Value) -> Result<Value> {
let ids: Vec<PhotoId> = ids.into_iter().filter(|id| seen.insert(*id)).collect();
let reject_below = p.get("rejectBelow").and_then(Value::as_f64).map(|v| v as f32);
let pick_best = p.get("pickBest").and_then(Value::as_bool).unwrap_or(false);
let (gap, same) = burst_params(p)?;
// measure (thumbnail-level sources: fast, and enough for focus at 512 px)
let mut rows: Vec<(PhotoId, Option<i64>, f32, f32, [f32; 64])> = Vec::new();
let mut failed = Vec::new();
@@ -45,8 +48,8 @@ fn analyze(s: &mut Session, p: &Value) -> Result<Value> {
let mut next = 1u32;
for i in 1..rows.len() {
let (a, b) = (&rows[i - 1], &rows[i]);
let close = matches!((a.1, b.1), (Some(x), Some(y)) if (y - x).abs() <= BURST_GAP);
if close && lightcraft_pipeline::cull::similarity(&a.4, &b.4) >= SAME {
let close = matches!((a.1, b.1), (Some(x), Some(y)) if (y.saturating_sub(x)).unsigned_abs() as f64 <= gap);
if close && lightcraft_pipeline::cull::similarity(&a.4, &b.4) >= same {
let g = *group_of[i - 1].get_or_insert_with(|| {
next += 1;
next - 1
@@ -86,6 +89,22 @@ fn analyze(s: &mut Session, p: &Value) -> Result<Value> {
Ok(json!({"photos": out, "groups": best.len(), "rejected": rejected, "picked": picked, "failed": failed}))
}
/// `burstGap` (seconds, 0..=a day) and `similarity` (0..=1) of `photo.analyze`, refused before
/// anything is measured when they are not numbers in range.
fn burst_params(p: &Value) -> Result<(f64, f32)> {
const C: &str = "photo.analyze";
let num = |key: &str, d: f64, max: f64| -> Result<f64> {
match p.get(key) {
None | Some(Value::Null) => Ok(d),
Some(v) => match v.as_f64() {
Some(x) if x.is_finite() && (0.0..=max).contains(&x) => Ok(x),
_ => Err(super::bad(C, format!("`{key}` must be a number from 0 to {max}"))),
},
}
};
Ok((num("burstGap", BURST_GAP, MAX_GAP)?, num("similarity", SAME, 1.0)? as f32))
}
/// Find Similar: photos that look like `id` (signatures from thumbnail-level sources, cached by
/// content), most similar first; the view is filtered to them.
fn find_similar(s: &mut Session, p: &Value) -> Result<Value> {
@@ -109,13 +128,16 @@ fn find_similar(s: &mut Session, p: &Value) -> Result<Value> {
if let Some(v) = sig(s, q) {
let sim = lightcraft_pipeline::cull::similarity(&me, &v);
if sim >= min {
hits.push((q, sim));
hits.push((q, sim.clamp(0.0, 1.0)));
}
}
}
hits.sort_by(|a, b| b.1.total_cmp(&a.1));
if p.get("filter").and_then(Value::as_bool).unwrap_or(true) {
s.shelved_filter = None;
s.filter.only = hits.iter().map(|h| h.0).collect();
// an empty `only` means "no constraint": no hits is its own state that shows no photos
s.filter.only_none = s.filter.only.is_empty();
}
Ok(json!({"photos": hits.iter().map(|(i, v)| json!({"id": i.0, "similarity": (v * 1000.0).round() / 1000.0})).collect::<Vec<_>>()}))
}
@@ -136,7 +158,7 @@ pub fn specs() -> Vec<CommandSpec> {
"Assisted Culling",
[],
None,
"{ids?, rejectBelow?: sharpness 0..100, pickBest?: bool} — score the selected photos (else all in view) for focus (0..100) and clipping, group look-alike shots taken within 10 s and mark the sharpest of each; optionally reject blurry photos and pick each group's best; one undo step → {photos: [{id, sharpness, clipped, group, best}], groups, rejected, picked}",
"{ids?, rejectBelow?: sharpness 0..100, pickBest?: bool, burstGap?: seconds 0..86400 (10), similarity?: 0..1 (0.93)} — score the selected photos (else all in view) for focus (0..100) and clipping, group look-alike shots (signature similarity at least `similarity`) taken at most `burstGap` seconds apart and mark the sharpest of each; optionally reject blurry photos and pick each group's best; one undo step → {photos: [{id, sharpness, clipped, group, best}], groups, rejected, picked}",
always,
analyze
),
+5
View File
@@ -176,6 +176,11 @@ fn rename_person(s: &mut Session, p: &Value) -> Result<Value> {
if s.filter.person.as_deref().is_some_and(|n| n.trim().to_lowercase() == from_key) {
s.filter.person = Some(to.clone());
}
if let Some(f) = s.shelved_filter.as_mut()
&& f.person.as_deref().is_some_and(|n| n.trim().to_lowercase() == from_key)
{
f.person = Some(to.clone());
}
Ok(json!({"faces": faces, "photos": photos, "from": from, "to": to, "merged": other}))
}
+1
View File
@@ -38,6 +38,7 @@ fn apply(s: &mut Session, p: &Value) -> Result<Value> {
let name = str_param(p, "name").ok_or_else(|| bad(C, "missing `name`"))?;
let f = s.filter_presets.iter().find(|f| f.name.eq_ignore_ascii_case(name)).ok_or_else(|| bad(C, format!("no filter preset `{name}`")))?;
s.filter = f.filter.clone();
s.shelved_filter = None;
Ok(json!({"photos": s.visible().len()}))
}
+7
View File
@@ -63,6 +63,13 @@ impl PlaceNames {
self.lock().running
}
/// Whether a place names download has been started in this session (running, or ended).
#[cfg(test)]
pub(crate) fn download_started(&self) -> bool {
let sh = self.lock();
sh.running || sh.last.is_some()
}
/// The gazetteer in `dir`, parsed once and re-read only when the cities file's size or time changes.
fn load(&mut self, dir: &Path) -> std::result::Result<Arc<GeoNames>, lightcraft_geocode::GeocodeError> {
let file = dir.join(GEONAMES_FILES[0]);
+38 -1
View File
@@ -32,6 +32,12 @@ fn commit_keywords(s: &mut Session, label: &str, op: lightcraft_catalog::Op, fol
if let Some(k) = s.filter.keyword.clone() {
s.filter.keyword = follow(&k);
}
// a filter put aside by Enable Filters follows the keyword too
if let Some(f) = s.shelved_filter.as_mut()
&& let Some(k) = f.keyword.clone()
{
f.keyword = follow(&k);
}
Ok(json!({"changed": photos, "listed": listed}))
}
@@ -148,6 +154,16 @@ pub fn keyword_sets_json(s: &Session) -> Value {
json!({"sets": sets, "current": s.keyword_set.clone().unwrap_or_else(|| RECENT.into()), "keywords": current_keywords(s)})
}
/// Make the set `step` places after the current one current, in the Keyword Set menu's order
/// (Recent Keywords, then the saved sets), wrapping around from the last to the first and back.
fn step_set(s: &mut Session, step: isize) -> Result<Value> {
let at = s.keyword_set.as_deref().and_then(|n| s.keyword_sets.iter().position(|x| same_set(&x.name, n))).map_or(0, |i| i + 1);
let next = (at as isize + step).rem_euclid(s.keyword_sets.len() as isize + 1) as usize;
s.keyword_set = next.checked_sub(1).and_then(|i| s.keyword_sets.get(i)).map(|x| x.name.clone());
s.save_prefs()?;
Ok(keyword_sets_json(s))
}
pub fn specs() -> Vec<CommandSpec> {
vec![
cmd!(query "keyword.sets", "Keyword Sets", [], None, "{} → {sets: [{name, keywords}], current, keywords: the nine ⌥1–⌥9 apply}", always, |s, _| Ok(keyword_sets_json(s))),
@@ -168,6 +184,24 @@ pub fn specs() -> Vec<CommandSpec> {
s.save_prefs()?;
Ok(keyword_sets_json(s))
}),
cmd!(
"keyword.nextSet",
"Next Keyword Set",
[],
Some("Alt+0"),
"{}: the set after the current one (after the last, Recent Keywords)",
always,
|s, _| step_set(s, 1)
),
cmd!(
"keyword.previousSet",
"Previous Keyword Set",
[],
Some("Alt+Shift+0"),
"{}: the set before the current one (before Recent Keywords, the last)",
always,
|s, _| step_set(s, -1)
),
cmd!(
"keyword.saveSet",
"Save Keyword Set",
@@ -445,7 +479,10 @@ pub fn specs() -> Vec<CommandSpec> {
let mut r = json!({"keywords": file.written.len(), "captureOneRefuses": refuses, "unwritable": file.unwritable});
match str_param(p, "path").map(str::trim).filter(|x| !x.is_empty()) {
Some(path) => {
std::fs::write(path, &file.text).map_err(|e| crate::EngineError::Other(format!("{path}: {e}")))?;
s.check_write_target(path).map_err(|e| bad("keyword.export", e))?;
// A folder that isn't there is an error, not created.
lightcraft_catalog::safe_file::write_atomic(std::path::Path::new(path), file.text.as_bytes())
.map_err(|e| crate::EngineError::Other(format!("{path}: {e}")))?;
r["path"] = json!(path);
}
None => r["text"] = json!(file.text),
+168 -45
View File
@@ -7,6 +7,9 @@ use serde_json::{Value, json};
use super::{CommandSpec, always, bad, bool_or, cmd, has_active, has_library_selection, has_selection, ok, str_param};
use crate::{LibrarySource, Result, Selection, Session};
/// The selection after photos left the view (deleted, or rated, flagged or labelled out of it).
/// Its callers end an open edit (a slider drag) before their own change, as moving to the next
/// photo does, so it is its own undo step and comes first.
fn select_after_deletion(s: &mut Session, before: &[PhotoId]) {
let at = before.iter().position(|id| Some(*id) == s.selection.active);
let remaining: std::collections::HashSet<_> = s.visible_cloned().into_iter().collect();
@@ -201,7 +204,6 @@ fn change_rating(s: &mut Session, p: &Value, delta: i8, label: &str) -> Result<V
if n > 0 {
s.commit(label, Op::Batch { ops })?;
}
advance_if(s, p);
Ok(json!({"changed": n}))
}
@@ -220,10 +222,36 @@ fn change_flag(s: &mut Session, p: &Value, label: &str, step: impl Fn(Flag) -> F
if n > 0 {
s.commit(label, Op::Batch { ops })?;
}
advance_if(s, p);
Ok(json!({"changed": n}))
}
/// Rate, flag or label (`edit`), then `advance`. When the edit took the active photo out of the view (the filter bar,
/// Picks or a smart album shows it no more), culling goes on from its place, as after a delete: the photo after it is
/// active, or the one before it at the end (#2312), not the view's first photo. That photo is the next one, so
/// `advance` doesn't step past it. Photos named by `ids` / `id` leave the selection to the caller: Compare and Survey
/// keep showing the photo they rate.
fn cull(s: &mut Session, p: &Value, edit: impl FnOnce(&mut Session) -> Result<Value>) -> Result<Value> {
let named = [p.get("ids"), p.get("id")].into_iter().any(|t| t.is_some_and(|t| !t.is_null()));
// the view as it was, then an open drag ends first, as its own undo step (the selection may
// move on; the drag itself can take the photo out of the view)
let before = (!named).then(|| s.visible_shared().1);
if !named {
s.end_interaction()?;
}
let active = s.selection.active;
let v = edit(s)?;
if let Some(before) = before
&& active.is_some_and(|a| before.contains(&a) && !s.visible().contains(&a))
{
select_after_deletion(s, &before);
s.active_mask = None;
s.active_spot = None;
} else {
advance_if(s, p);
}
Ok(v)
}
fn advance_if(s: &mut Session, p: &Value) {
if bool_or(p, "advance", false) {
let _ = step(s, 1);
@@ -371,6 +399,9 @@ pub fn specs() -> Vec<CommandSpec> {
if s.catalog.album(a).is_none_or(|a| a.folder) {
return Err(bad("library.source", "no such album"));
}
// one album at a time: `to_filter` makes the album shown replace the filter's, and the
// filter bar has no album chip, so a left-over one would narrow the next source unseen
s.filter.album = None;
LibrarySource::Album(a)
}
"libraryFolder" => {
@@ -426,6 +457,7 @@ pub fn specs() -> Vec<CommandSpec> {
return Err(bad("library.filter", problems.iter().map(ToString::to_string).collect::<Vec<_>>().join("; ")));
}
s.filter = f;
s.shelved_filter = None;
Ok(json!({"count": s.visible().len()}))
}
),
@@ -469,6 +501,15 @@ pub fn specs() -> Vec<CommandSpec> {
if s.filter.library_folder.as_deref().is_some_and(|c| lightcraft_catalog::query::folder_within(c, path)) {
s.filter.library_folder = None;
}
// a filter put aside by Enable Filters must not bring the removed folder back
if let Some(f) = s.shelved_filter.as_mut()
&& f.library_folder.as_deref().is_some_and(|c| lightcraft_catalog::query::folder_within(c, path))
{
f.library_folder = None;
}
if s.shelved_filter.as_ref() == Some(&Filter::default()) {
s.shelved_filter = None;
}
// the folder being shown is gone: back to everything, as for a deleted album
if s.library_folder.as_deref().is_some_and(|c| lightcraft_catalog::query::folder_within(c, path)) {
s.library_folder = None;
@@ -510,8 +551,30 @@ pub fn specs() -> Vec<CommandSpec> {
),
cmd!("library.clearFilter", "Clear Filters", ["View"], None, "{}", always, |s, _| {
s.filter = Default::default();
s.shelved_filter = None;
Ok(json!({"count": s.visible().len()}))
}),
// Classic: Library > Enable Filters (Cmd+L) switches the filter off and on again as it was
cmd!(
"library.toggleFilters",
"Enable Filters",
["View"],
Some("Cmd+L"),
"{} — with a filter set, puts it aside and shows everything; with one put aside, brings it back as it was (stars, flag, text… together). Setting or clearing a filter while it is off forgets the one put aside → {enabled, count}",
always,
|s, _| {
if s.filter != Filter::default() {
s.shelved_filter = Some(std::mem::take(&mut s.filter));
} else if let Some(mut f) = s.shelved_filter.take() {
// one folder at a time: the folder shown now wins over the one the filter had
if s.source == LibrarySource::LibraryFolder {
f.library_folder = None;
}
s.filter = f;
}
Ok(json!({"enabled": s.shelved_filter.is_none(), "count": s.visible().len()}))
}
),
cmd!(
"library.sort",
"Sort",
@@ -663,7 +726,7 @@ pub fn specs() -> Vec<CommandSpec> {
cmd!(query "library.devices", "Cameras and Cards", [], None, "{} → [{name, path (its DCIM folder), root}] — mounted volumes with a DCIM folder", always, |_, _| {
Ok(serde_json::to_value(crate::devices::devices_now()).unwrap_or_default())
}),
cmd!(query "photo.copyMetadata", "Copy Metadata", ["Photo"], None, "{} — title, caption, copyright (notice, status, usage terms, info URL), creator, location and keywords of the active photo", has_active, |s, _| {
cmd!(query "photo.copyMetadata", "Copy Metadata", ["Photo"], Some("Cmd+Alt+Shift+C"), "{} — title, caption, copyright (notice, status, usage terms, info URL), creator, location and keywords of the active photo", has_active, |s, _| {
let id = s.active().ok_or_else(|| bad("photo.copyMetadata", "no active photo"))?;
let m = &s.catalog.photo(id).ok_or_else(|| bad("photo.copyMetadata", "no photo"))?.meta;
let v = json!({"title": m.title, "caption": m.caption, "altText": m.alt_text, "extendedDescription": m.extended_description,
@@ -677,7 +740,7 @@ pub fn specs() -> Vec<CommandSpec> {
"photo.pasteMetadata",
"Paste Metadata",
["Photo"],
None,
Some("Cmd+Alt+Shift+V"),
"{ids?, fields?: [title|caption|copyright|copyrightStatus|usageTerms|copyrightUrl|creator|location|keywords] (default: all copied)}",
has_selection,
|s, p| {
@@ -732,40 +795,66 @@ pub fn specs() -> Vec<CommandSpec> {
|s, p| { step(s, -row_step(p)) }
),
// ---- rating / flags / labels
cmd!("photo.rate", "Set Rating", ["Photo", "Set Rating"], None, "{rating: 0..5, ids?, advance?: bool}", has_selection, |s, p| {
let r = super::f64_req(p, "rating", "photo.rate")? as i64;
if !(0..=5).contains(&r) {
return Err(bad("photo.rate", "rating must be 0..5"));
cmd!(
"photo.rate",
"Set Rating",
["Photo", "Set Rating"],
None,
"{rating: 0..5, ids?, advance?: bool} — without `ids`, the selection moves on when this takes the active photo out of the view",
has_selection,
|s, p| {
let r = super::index_req(p, "rating", "photo.rate")
.ok()
.filter(|r| *r <= 5)
.ok_or_else(|| bad("photo.rate", "rating must be a whole number 0..5"))?;
cull(s, p, |s| for_targets(s, p, "Set Rating", |id| Some(Op::SetRating { id, rating: r as u8 })))
}
let v = for_targets(s, p, "Set Rating", |id| Some(Op::SetRating { id, rating: r as u8 }))?;
advance_if(s, p);
Ok(v)
}),
cmd!("photo.decreaseRating", "Decrease Rating", [], None, "{ids?, advance?: bool}", has_selection, |s, p| {
change_rating(s, p, -1, "Decrease Rating")
}),
cmd!("photo.increaseRating", "Increase Rating", [], None, "{ids?, advance?: bool}", has_selection, |s, p| {
change_rating(s, p, 1, "Increase Rating")
}),
cmd!("photo.flag", "Set Flag", ["Photo", "Set Flag"], None, "{flag: pick|reject|none, ids?, advance?: bool}", has_selection, |s, p| {
let f = str_param(p, "flag").and_then(Flag::parse).ok_or_else(|| bad("photo.flag", "flag must be pick|reject|none"))?;
let v = for_targets(s, p, "Set Flag", |id| Some(Op::SetFlag { id, flag: f }))?;
advance_if(s, p);
Ok(v)
}),
),
cmd!(
"photo.decreaseRating",
"Decrease Rating",
[],
None,
"{ids?, advance?: bool} — without `ids`, the selection moves on when this takes the active photo out of the view",
has_selection,
|s, p| { cull(s, p, |s| change_rating(s, p, -1, "Decrease Rating")) }
),
cmd!(
"photo.increaseRating",
"Increase Rating",
[],
None,
"{ids?, advance?: bool} — without `ids`, the selection moves on when this takes the active photo out of the view",
has_selection,
|s, p| { cull(s, p, |s| change_rating(s, p, 1, "Increase Rating")) }
),
cmd!(
"photo.flag",
"Set Flag",
["Photo", "Set Flag"],
None,
"{flag: pick|reject|none, ids?, advance?: bool} — without `ids`, the selection moves on when this takes the active photo out of the view",
has_selection,
|s, p| {
let f = str_param(p, "flag").and_then(Flag::parse).ok_or_else(|| bad("photo.flag", "flag must be pick|reject|none"))?;
cull(s, p, |s| for_targets(s, p, "Set Flag", |id| Some(Op::SetFlag { id, flag: f })))
}
),
// Classic: ` cycles the flag; Cmd+Up / Cmd+Down raise / lower it (Ctrl on Windows and Linux)
cmd!(
"photo.cycleFlag",
"Cycle Flag",
["Photo", "Set Flag"],
Some("`"),
"{ids?, advance?: bool} — each photo's own flag: unflagged -> pick -> reject -> unflagged",
"{ids?, advance?: bool} — each photo's own flag: unflagged -> pick -> reject -> unflagged; without `ids`, the selection moves on when this takes the active photo out of the view",
has_selection,
|s, p| {
change_flag(s, p, "Cycle Flag", |f| match f {
Flag::None => Flag::Pick,
Flag::Pick => Flag::Reject,
Flag::Reject => Flag::None,
cull(s, p, |s| {
change_flag(s, p, "Cycle Flag", |f| match f {
Flag::None => Flag::Pick,
Flag::Pick => Flag::Reject,
Flag::Reject => Flag::None,
})
})
}
),
@@ -774,34 +863,52 @@ pub fn specs() -> Vec<CommandSpec> {
"Increase Flag Status",
["Photo", "Set Flag"],
Some("Cmd+Up"),
"{ids?, advance?: bool} — reject -> unflagged -> pick (stops at pick)",
"{ids?, advance?: bool} — reject -> unflagged -> pick (stops at pick); without `ids`, the selection moves on when this takes the active photo out of the view",
has_selection,
|s, p| { change_flag(s, p, "Increase Flag Status", |f| if f == Flag::Reject { Flag::None } else { Flag::Pick }) }
|s, p| { cull(s, p, |s| change_flag(s, p, "Increase Flag Status", |f| if f == Flag::Reject { Flag::None } else { Flag::Pick })) }
),
cmd!(
"photo.decreaseFlag",
"Decrease Flag Status",
["Photo", "Set Flag"],
Some("Cmd+Down"),
"{ids?, advance?: bool} — pick -> unflagged -> reject (stops at reject)",
"{ids?, advance?: bool} — pick -> unflagged -> reject (stops at reject); without `ids`, the selection moves on when this takes the active photo out of the view",
has_selection,
|s, p| { change_flag(s, p, "Decrease Flag Status", |f| if f == Flag::Pick { Flag::None } else { Flag::Reject }) }
|s, p| { cull(s, p, |s| change_flag(s, p, "Decrease Flag Status", |f| if f == Flag::Pick { Flag::None } else { Flag::Reject })) }
),
cmd!(
"photo.pick",
"Flag as Pick",
["Photo", "Set Flag"],
Some("P"),
"{ids?, advance?: bool} — without `ids`, the selection moves on when this takes the active photo out of the view",
has_selection,
|s, p| { cull(s, p, |s| for_targets(s, p, "Flag as Pick", |id| Some(Op::SetFlag { id, flag: Flag::Pick }))) }
),
cmd!(
"photo.reject",
"Flag as Reject",
["Photo", "Set Flag"],
Some("X"),
"{ids?, advance?: bool} — without `ids`, the selection moves on when this takes the active photo out of the view",
has_selection,
|s, p| { cull(s, p, |s| for_targets(s, p, "Flag as Reject", |id| Some(Op::SetFlag { id, flag: Flag::Reject }))) }
),
cmd!(
"photo.unflag",
"Unflag",
["Photo", "Set Flag"],
Some("U"),
"{ids?, advance?: bool} — without `ids`, the selection moves on when this takes the active photo out of the view",
has_selection,
|s, p| { cull(s, p, |s| for_targets(s, p, "Unflag", |id| Some(Op::SetFlag { id, flag: Flag::None }))) }
),
cmd!("photo.pick", "Flag as Pick", ["Photo", "Set Flag"], Some("P"), "{ids?}", has_selection, |s, p| {
for_targets(s, p, "Flag as Pick", |id| Some(Op::SetFlag { id, flag: Flag::Pick }))
}),
cmd!("photo.reject", "Flag as Reject", ["Photo", "Set Flag"], Some("X"), "{ids?}", has_selection, |s, p| {
for_targets(s, p, "Flag as Reject", |id| Some(Op::SetFlag { id, flag: Flag::Reject }))
}),
cmd!("photo.unflag", "Unflag", ["Photo", "Set Flag"], Some("U"), "{ids?}", has_selection, |s, p| {
for_targets(s, p, "Unflag", |id| Some(Op::SetFlag { id, flag: Flag::None }))
}),
cmd!(
"photo.label",
"Set Color Label",
["Photo", "Set Color Label"],
None,
"{label: red|yellow|green|blue|purple|none, ids?}",
"{label: red|yellow|green|blue|purple|none, ids?, advance?: bool} — without `ids`, the selection moves on when this takes the active photo out of the view",
has_selection,
|s, p| {
// `none` (or null) clears; a missing label or one that isn't text is an error, not a clear
@@ -812,7 +919,7 @@ pub fn specs() -> Vec<CommandSpec> {
None => return Err(bad("photo.label", "missing `label` (red|yellow|green|blue|purple|none)")),
Some(_) => return Err(bad("photo.label", "label must be red|yellow|green|blue|purple|none")),
};
for_targets(s, p, "Set Color Label", |id| Some(Op::SetLabel { id, label: l }))
cull(s, p, |s| for_targets(s, p, "Set Color Label", |id| Some(Op::SetLabel { id, label: l })))
}
),
// ---- orientation
@@ -883,6 +990,8 @@ pub fn specs() -> Vec<CommandSpec> {
cmd!("photo.delete", "Delete Photo", ["Photo"], Some("Delete"), "{ids?} — moves to Recently Deleted", has_selection, |s, p| {
let before = s.visible_cloned();
let targets = s.targets(p)?;
// an open drag ends first, as its own undo step (the selection moves on)
s.end_interaction()?;
let n = mark_deleted(s, &targets, "Delete")?;
select_after_deletion(s, &before);
Ok(json!({"changed": n}))
@@ -912,6 +1021,8 @@ pub fn specs() -> Vec<CommandSpec> {
cmd!("photo.deletePermanently", "Delete Permanently", ["Photo"], None, "{ids?}", has_selection, |s, p| {
let before = s.visible_cloned();
let t = s.targets(p)?;
// before the inverse ops are taken, so undo brings the photo back with its edit
s.end_interaction()?;
let op = s.catalog.delete_photos_permanently_ops(&t);
s.commit("Delete Permanently", op)?;
select_after_deletion(s, &before);
@@ -1603,7 +1714,7 @@ pub fn specs() -> Vec<CommandSpec> {
s.compact_library()?;
ok()
}),
cmd!("library.clearPreviews", "Clear Preview Cache", ["File"], None, "{}", always, |s, _| {
cmd!("library.clearPreviews", "Discard Preview Cache", ["File"], None, "{}", always, |s, _| {
s.media.rendered.clear();
s.media.clear_sources();
ok()
@@ -1771,3 +1882,15 @@ mod relative_rating_tests;
#[cfg(test)]
#[path = "library/tests_flag_cycle.rs"]
mod flag_cycle_tests;
#[cfg(test)]
#[path = "library/tests_rated_out.rs"]
mod rated_out_tests;
#[cfg(test)]
#[path = "library/tests_enable_filters.rs"]
mod enable_filters_tests;
#[cfg(test)]
#[path = "library/tests_source_album_filter.rs"]
mod source_album_filter_tests;
@@ -0,0 +1,154 @@
//! `library.toggleFilters` (Library > Enable Filters, Cmd+L) through the real engine.
use lightcraft_catalog::{Filter, Flag, Op, Photo, PhotoId, Source};
use serde_json::json;
use crate::{LibrarySource, Result, Session};
fn session() -> Result<Session> {
let mut s = Session::new();
for (id, rating, flag) in [(1, 5, Flag::Pick), (2, 5, Flag::None), (3, 2, Flag::Pick), (4, 1, Flag::None)] {
let mut photo = Photo::new(
PhotoId(id),
Source::File { path: format!("/lightcraft-enable-filters/{id}.jpg") },
&format!("{id}.jpg"),
"JPEG",
40,
30,
"2026-10-23",
);
photo.rating = rating;
photo.flag = flag;
s.catalog.apply(Op::AddPhoto { photo: Box::new(photo) })?;
}
Ok(s)
}
fn stars_and_pick(s: &mut Session) {
s.execute("library.filter", &json!({"rating": 4, "flag": "pick"})).unwrap();
}
#[test]
fn off_shows_everything_and_on_brings_back_the_same_filter() {
let mut s = session().unwrap();
stars_and_pick(&mut s);
let set = s.filter.clone();
assert_eq!(s.visible().len(), 1, "the filter hides photos, or off and on would look alike");
let r = s.execute("library.toggleFilters", &json!({})).unwrap();
assert_eq!((r["enabled"].clone(), r["count"].clone()), (json!(false), json!(4)));
assert_eq!(s.filter, Filter::default());
assert_eq!(s.visible().len(), 4);
let r = s.execute("library.toggleFilters", &json!({})).unwrap();
assert_eq!((r["enabled"].clone(), r["count"].clone()), (json!(true), json!(1)));
assert_eq!(s.filter, set, "stars and flag come back together");
assert_eq!(s.visible().len(), 1);
}
#[test]
fn with_no_filter_it_does_nothing_and_leaves_nothing_to_bring_back() {
let mut s = session().unwrap();
let r = s.execute("library.toggleFilters", &json!({})).unwrap();
assert_eq!((r["enabled"].clone(), r["count"].clone()), (json!(true), json!(4)));
let r = s.execute("library.toggleFilters", &json!({})).unwrap();
assert_eq!(r["enabled"], json!(true));
assert_eq!(s.filter, Filter::default());
assert!(s.shelved_filter.is_none());
}
#[test]
fn setting_a_filter_while_off_turns_filtering_on_and_forgets_the_old_one() {
let mut s = session().unwrap();
stars_and_pick(&mut s);
s.execute("library.toggleFilters", &json!({})).unwrap();
s.execute("library.filter", &json!({"rating": 2})).unwrap();
assert_eq!(s.visible().len(), 3);
// off again, then on: the filter that was set last, not the first one
s.execute("library.toggleFilters", &json!({})).unwrap();
s.execute("library.toggleFilters", &json!({})).unwrap();
assert_eq!(s.filter.rating, 2);
assert_eq!(s.filter.flag, None);
}
#[test]
fn emptying_the_filter_by_hand_while_off_leaves_nothing_to_bring_back() {
let mut s = session().unwrap();
stars_and_pick(&mut s);
s.execute("library.toggleFilters", &json!({})).unwrap();
s.execute("library.filter", &json!({"rating": 2})).unwrap();
s.execute("library.filter", &json!({"rating": 0})).unwrap();
assert_eq!(s.filter, Filter::default());
s.execute("library.toggleFilters", &json!({})).unwrap();
assert_eq!(s.filter, Filter::default(), "the filter from before it was switched off is not back");
assert_eq!(s.visible().len(), 4);
}
#[test]
fn clearing_the_filter_while_off_leaves_nothing_to_bring_back() {
let mut s = session().unwrap();
stars_and_pick(&mut s);
s.execute("library.toggleFilters", &json!({})).unwrap();
s.execute("library.clearFilter", &json!({})).unwrap();
s.execute("library.toggleFilters", &json!({})).unwrap();
assert_eq!(s.filter, Filter::default());
assert_eq!(s.visible().len(), 4);
}
#[test]
fn applying_a_preset_while_off_turns_filtering_on_with_the_preset() {
let mut s = session().unwrap();
s.filter_presets.push(super::super::filters::FilterPreset { name: "Fives".into(), filter: Filter { rating: 5, ..Default::default() } });
stars_and_pick(&mut s);
s.execute("library.toggleFilters", &json!({})).unwrap();
s.execute("filter.applyPreset", &json!({"name": "Fives"})).unwrap();
s.execute("library.toggleFilters", &json!({})).unwrap();
s.execute("library.toggleFilters", &json!({})).unwrap();
assert_eq!(s.filter.rating, 5);
assert_eq!(s.filter.flag, None);
}
#[test]
fn a_folder_filter_does_not_come_back_over_the_folder_now_shown() {
let mut s = session().unwrap();
s.execute("library.filter", &json!({"rating": 4, "libraryFolder": "/lightcraft-enable-filters"})).unwrap();
s.execute("library.toggleFilters", &json!({})).unwrap();
s.source = LibrarySource::LibraryFolder;
s.library_folder = Some("/lightcraft-enable-filters".into());
s.execute("library.toggleFilters", &json!({})).unwrap();
assert_eq!(s.filter.rating, 4);
assert_eq!(s.filter.library_folder, None);
}
#[test]
fn removing_a_folder_while_off_drops_it_from_the_filter_put_aside() {
let mut s = session().unwrap();
s.execute("library.filter", &json!({"rating": 4, "libraryFolder": "/lightcraft-enable-filters"})).unwrap();
s.execute("library.toggleFilters", &json!({})).unwrap();
s.execute("library.removeFolder", &json!({"path": "/lightcraft-enable-filters"})).unwrap();
s.execute("library.toggleFilters", &json!({})).unwrap();
assert_eq!(s.filter.library_folder, None, "the removed folder is not brought back");
assert_eq!(s.filter.rating, 4, "the rest of the filter is");
}
#[test]
fn removing_the_only_criterion_put_aside_leaves_filtering_on() {
let mut s = session().unwrap();
s.execute("library.filter", &json!({"libraryFolder": "/lightcraft-enable-filters"})).unwrap();
s.execute("library.toggleFilters", &json!({})).unwrap();
s.execute("library.removeFolder", &json!({"path": "/lightcraft-enable-filters"})).unwrap();
assert!(s.shelved_filter.is_none(), "an empty filter is not left put aside");
}
#[test]
fn a_keyword_renamed_while_off_is_followed_by_the_filter_put_aside() {
let mut s = session().unwrap();
let mut photo = Photo::new(PhotoId(5), Source::File { path: "/lightcraft-enable-filters/5.jpg".into() }, "5.jpg", "JPEG", 40, 30, "2026-10-23");
photo.meta.keywords = vec!["cat".into()];
s.catalog.apply(Op::AddPhoto { photo: Box::new(photo) }).unwrap();
s.filter.keyword = Some("cat".into());
assert_eq!(s.visible().len(), 1);
s.execute("library.toggleFilters", &json!({})).unwrap();
s.execute("keyword.rename", &json!({"from": "cat", "to": "dog"})).unwrap();
let r = s.execute("library.toggleFilters", &json!({})).unwrap();
assert_eq!(r["enabled"], json!(true));
assert_eq!(s.filter.keyword.as_deref(), Some("dog"), "the put-aside filter follows the rename");
assert_eq!(s.visible().len(), 1);
}
@@ -0,0 +1,139 @@
//! A rating, flag or label that takes the active photo out of the view (the filter bar, Picks) goes on from its place:
//! the photo that took it, not the view's first photo (#2312).
use lightcraft_catalog::{ColorLabel, Flag, Op, Photo, PhotoId, Source};
use serde_json::json;
use crate::{Result, Selection, Session};
/// 100 photos with one star each, in file-name order (photo `n` is the `n`th), the filter showing one-star photos.
fn one_star() -> Result<Session> {
let mut s = Session::new();
for id in 1..=100 {
let mut photo = Photo::new(
PhotoId(id),
Source::File { path: format!("/lightcraft-rated-out/{id:03}.jpg") },
&format!("{id:03}.jpg"),
"JPEG",
40,
30,
"2026-10-19",
);
photo.rating = 1;
photo.flag = Flag::Pick;
photo.label = Some(ColorLabel::Red);
s.catalog.apply(Op::AddPhoto { photo: Box::new(photo) })?;
}
s.execute("library.sort", &json!({"key": "fileName", "ascending": true}))?;
s.execute("library.filter", &json!({"rating": 1, "ratingOp": "exactly"}))?;
Ok(s)
}
#[test]
fn a_star_taken_off_goes_on_to_the_next_photo() {
// Given photo 82 of 100 one-star photos, the filter showing one-star photos
let mut s = one_star().unwrap();
s.execute("library.select", &json!({"ids": [82]})).unwrap();
// When its star is taken off, Then it leaves the view and the photo after it is the one selected
s.execute("photo.rate", &json!({"rating": 0})).unwrap();
assert!(!s.visible_cloned().contains(&PhotoId(82)));
assert_eq!(s.selection, Selection::single(PhotoId(83)));
// and the next photo is the one after that, not the first one of the view
s.execute("library.next", &json!({})).unwrap();
assert_eq!(s.active(), Some(PhotoId(84)));
// With `advance`, the photo that took its place is the next one: no photo is skipped
s.execute("photo.rate", &json!({"rating": 0, "advance": true})).unwrap();
assert_eq!(s.active(), Some(PhotoId(85)));
s.execute("photo.decreaseRating", &json!({"advance": true})).unwrap();
assert_eq!(s.active(), Some(PhotoId(86)));
// The last photo of the view hands on to the one before it
s.execute("library.select", &json!({"ids": [100]})).unwrap();
s.execute("photo.rate", &json!({"rating": 0, "advance": true})).unwrap();
assert_eq!(s.active(), Some(PhotoId(99)));
// A rating that keeps the photo in the view advances as before
s.execute("photo.rate", &json!({"rating": 1, "advance": true})).unwrap();
assert_eq!(s.active(), Some(PhotoId(99)), "the last photo of the view stays");
s.execute("library.select", &json!({"ids": [10]})).unwrap();
s.execute("photo.rate", &json!({"rating": 1, "advance": true})).unwrap();
assert_eq!(s.active(), Some(PhotoId(11)));
}
#[test]
fn flags_and_labels_that_leave_the_view_go_on_to_the_next_photo_too() {
for (filter, command, params) in [
(json!({"flag": "pick"}), "photo.reject", json!({})),
(json!({"flag": "pick"}), "photo.flag", json!({"flag": "none"})),
(json!({"flag": "pick"}), "photo.cycleFlag", json!({})),
(json!({"flag": "pick"}), "photo.decreaseFlag", json!({})),
(json!({"label": "red"}), "photo.label", json!({"label": "none"})),
(json!({}), "photo.increaseRating", json!({})),
] {
let mut s = one_star().unwrap();
s.execute("library.filter", &filter).unwrap();
s.execute("library.select", &json!({"ids": [40]})).unwrap();
s.execute(command, &params).unwrap();
assert!(!s.visible_cloned().contains(&PhotoId(40)), "{command} {params}");
assert_eq!(s.selection, Selection::single(PhotoId(41)), "{command} {params}");
}
}
#[test]
fn several_photos_rated_out_go_on_after_the_active_one() {
let mut s = one_star().unwrap();
// three selected, the middle one active: all leave, the photo after the active one is selected
s.execute("library.select", &json!({"ids": [20, 21, 22], "active": 21})).unwrap();
s.execute("photo.rate", &json!({"rating": 0})).unwrap();
assert_eq!(s.selection, Selection::single(PhotoId(23)));
// the active one leaves and another selected photo stays: that one is active
s.execute("photo.rate", &json!({"ids": [30], "rating": 3})).unwrap();
s.execute("library.filter", &json!({"rating": 1, "ratingOp": "atLeast"})).unwrap();
s.execute("library.select", &json!({"ids": [29, 30], "active": 29})).unwrap();
s.execute("photo.decreaseRating", &json!({})).unwrap();
assert_eq!(s.selection, Selection { ids: vec![PhotoId(30)], active: Some(PhotoId(30)) });
}
#[test]
fn named_photos_and_a_held_view_keep_the_selection() {
// Photos named by `ids` leave the selection to the caller (Compare and Survey keep the photo they show)
let mut s = one_star().unwrap();
s.execute("library.select", &json!({"ids": [5, 6], "active": 6})).unwrap();
s.execute("photo.rate", &json!({"ids": [6], "rating": 0})).unwrap();
assert_eq!(s.selection, Selection { ids: vec![PhotoId(5), PhotoId(6)], active: Some(PhotoId(6)) });
// A held view keeps the photo, so the selection stays with it
let mut s = one_star().unwrap();
s.execute("library.holdView", &json!({"on": true})).unwrap();
// (what the grid shows is what is held)
assert_eq!(s.visible_cloned().len(), 100);
s.execute("library.select", &json!({"ids": [7]})).unwrap();
s.execute("photo.rate", &json!({"rating": 0})).unwrap();
assert!(s.visible_cloned().contains(&PhotoId(7)));
assert_eq!(s.selection, Selection::single(PhotoId(7)));
s.execute("photo.rate", &json!({"rating": 0, "advance": true})).unwrap();
assert_eq!(s.active(), Some(PhotoId(8)));
}
/// A slider drag still open when a rating takes the photo out of the view, or when the photo is
/// deleted, ends as its own undo step before that change, as moving to the next photo does: undo
/// brings the rating or the photo back first, then the edit. Before, the move left the drag open
/// with no undo step (CodeRabbit on #2689).
#[test]
fn an_open_edit_ends_before_the_selection_moves_on() {
let exposure = |s: &Session, id| s.develop_of(PhotoId(id)).map(|d| d.light.exposure);
for (command, params) in [("photo.rate", json!({"rating": 0})), ("photo.delete", json!({})), ("photo.deletePermanently", json!({}))] {
let mut s = one_star().unwrap();
s.execute("library.select", &json!({"ids": [82]})).unwrap();
s.execute("develop.beginInteraction", &json!({"label": "Exposure"})).unwrap();
s.execute("develop.set", &json!({"control": "light.exposure", "value": 1.5})).unwrap();
s.execute(command, &params).unwrap();
assert_eq!(s.active(), Some(PhotoId(83)), "{command}");
assert!(s.interaction.is_none(), "{command}: the drag ended with the move");
if command != "photo.deletePermanently" {
assert_eq!(exposure(&s, 82), Some(1.5), "{command}");
}
// undo: the rating or the photo comes back first, with the edit; then the edit goes
s.execute("edit.undo", &json!({})).unwrap();
assert!(s.visible_cloned().contains(&PhotoId(82)), "{command}: back in the view");
assert_eq!(exposure(&s, 82), Some(1.5), "{command}: with its edit");
s.execute("edit.undo", &json!({})).unwrap();
assert_eq!(exposure(&s, 82), Some(0.0), "{command}");
}
}
@@ -0,0 +1,61 @@
//! `library.source` with an album while the filter holds an album: the album shown wins.
use lightcraft_catalog::{Flag, Op, Photo, PhotoId, Source};
use serde_json::json;
use crate::{Result, Selection, Session};
/// Photos 1 to 3 (rated 5, 5 and 2). Album X holds photo 1 and album A holds photo 3.
fn session_with_two_albums() -> Result<(Session, u64, u64)> {
let mut s = Session::new();
for (id, rating) in [(1, 5), (2, 5), (3, 2)] {
let mut photo = Photo::new(
PhotoId(id),
Source::File { path: format!("/lightcraft-source-album/{id}.jpg") },
&format!("{id}.jpg"),
"JPEG",
40,
30,
"2026-10-23",
);
photo.rating = rating;
photo.flag = Flag::None;
s.catalog.apply(Op::AddPhoto { photo: Box::new(photo) })?;
}
let mut make = |name: &str, id: u64| {
s.selection = Selection { ids: vec![PhotoId(id)], active: None };
s.execute("album.create", &json!({"name": name, "addSelected": true})).unwrap()["id"].as_u64().unwrap()
};
let (x, a) = (make("X", 1), make("A", 3));
Ok((s, x, a))
}
#[test]
fn showing_an_album_drops_the_album_the_filter_held() {
let (mut s, x, a) = session_with_two_albums().unwrap();
s.execute("library.filter", &json!({"album": x, "rating": 4})).unwrap();
assert_eq!(s.visible().len(), 1, "the album narrows the grid, or the hidden album would not matter");
s.execute("library.source", &json!({"kind": "album", "id": a})).unwrap();
assert_eq!(s.filter.album, None, "one album at a time: the one shown wins, as for a library folder");
assert_eq!(s.filter.rating, 4, "the rest of the filter stays");
// the old album must not narrow the next source, with no chip to show it
s.execute("library.source", &json!({"kind": "all"})).unwrap();
assert_eq!(s.visible().len(), 2, "photos 1 and 2 have four stars or more");
}
#[test]
fn the_album_filter_stays_while_the_source_is_not_an_album() {
let (mut s, x, _) = session_with_two_albums().unwrap();
s.execute("library.filter", &json!({"album": x})).unwrap();
s.execute("library.source", &json!({"kind": "picks"})).unwrap();
s.execute("library.source", &json!({"kind": "all"})).unwrap();
assert_eq!(s.filter.album.map(|a| a.0), Some(x));
assert_eq!(s.visible().len(), 1);
}
#[test]
fn a_refused_album_source_leaves_the_filter_alone() {
let (mut s, x, _) = session_with_two_albums().unwrap();
s.execute("library.filter", &json!({"album": x})).unwrap();
assert!(s.execute("library.source", &json!({"kind": "album", "id": 9999})).is_err());
assert_eq!(s.filter.album.map(|a| a.0), Some(x));
}
+20 -5
View File
@@ -183,7 +183,7 @@ pub fn specs() -> Vec<CommandSpec> {
"Edit Capture Time",
[],
None,
"{ids?, time?: `2026-09-30T14:05:00` (the active photo gets it, the others shift by the same amount), each?: bool (every photo gets `time`), shift?: seconds, hours?: time-zone shift in hours} → {changed, captured: [..]}",
"{ids?, time?: `2026-09-30T14:05:00` (the active photo gets it, the others shift by the same amount), each?: bool (every photo gets `time`), shift?: seconds, hours?: time-zone shift in hours} → {changed, captured: [..], unreadable: photos skipped because their capture time can't be read}",
has_selection,
|s, p| {
use lightcraft_catalog::dates::{iso_seconds, normalize_iso, shift_iso};
@@ -213,12 +213,25 @@ pub fn specs() -> Vec<CommandSpec> {
let from = normalize_iso(anchor_date.trim().get(..10).unwrap_or(""))
.and_then(|_| iso_seconds(&anchor_date))
.ok_or_else(|| bad(c, "the photo's date doesn't parse"))?;
delta += iso_seconds(&t).unwrap_or(from) - from;
// the others move by the anchor's change, so it must read in full, not just
// its leading digits (a Lightroom catalog's capture time is copied verbatim);
// alone, it simply gets the time, which repairs it
if shift_iso(&anchor_date, 0).is_some() {
delta += iso_seconds(&t).unwrap_or(from) - from;
} else if targets.len() == 1 {
each = Some(t);
} else {
return Err(bad(
c,
"the active photo's capture time can't be read, so the other photos can't move by the same amount; set its time with it selected alone",
));
}
}
}
let mut ops = Vec::new();
let mut out = Vec::new();
let mut out_of_range = 0usize;
let mut unreadable = 0usize;
for id in &targets {
let from = match &each {
Some(t) => t.clone(),
@@ -226,9 +239,11 @@ pub fn specs() -> Vec<CommandSpec> {
};
let Some(new) = shift_iso(&from, delta) else {
// a date that reads but would leave years 0000–9999 is refused below; one
// that doesn't read is skipped, as before
if iso_seconds(&from).is_some() {
// that doesn't read is skipped and counted
if shift_iso(&from, 0).is_some() {
out_of_range += 1;
} else {
unreadable += 1;
}
continue;
};
@@ -244,7 +259,7 @@ pub fn specs() -> Vec<CommandSpec> {
if n > 0 {
s.commit("Edit Capture Time", Op::Batch { ops })?;
}
Ok(json!({"changed": n, "captured": out}))
Ok(json!({"changed": n, "captured": out, "unreadable": unreadable}))
}
),
cmd!(
+47 -2
View File
@@ -8,6 +8,18 @@ use super::{CommandSpec, bad, bool_or, cmd, f64_or, has_active, ok, point, str_p
use crate::segment::{Click, MAX_CLICKS};
use crate::{Result, Session};
/// A colour-range mask compares every pixel against every sample, so render and export cost is
/// pixels x samples. The interactive sampler stops at 5; automation (`mask.add`/`mask.update`)
/// passed its `samples` array straight through, so a few hundred samples on a large render hung.
const MAX_COLOR_RANGE_SAMPLES: usize = 64;
/// Caps a colour-range shape's sample list at [`MAX_COLOR_RANGE_SAMPLES`] (render cost is pixels x samples).
fn cap_color_range_samples(shape: &mut MaskShape) {
if let MaskShape::ColorRange { samples, .. } = shape {
samples.truncate(MAX_COLOR_RANGE_SAMPLES);
}
}
fn shape_from(kind: &str, p: &Value, c: &str) -> Result<MaskShape> {
Ok(match kind {
"brush" => MaskShape::Brush { strokes: vec![] },
@@ -35,7 +47,12 @@ fn shape_from(kind: &str, p: &Value, c: &str) -> Result<MaskShape> {
let samples: Vec<[f64; 3]> = p
.get("samples")
.and_then(Value::as_array)
.map(|a| a.iter().filter_map(|s| Some([s.get(0)?.as_f64()?, s.get(1)?.as_f64()?, s.get(2)?.as_f64()?])).collect())
.map(|a| {
a.iter()
.filter_map(|s| Some([s.get(0)?.as_f64()?, s.get(1)?.as_f64()?, s.get(2)?.as_f64()?]))
.take(MAX_COLOR_RANGE_SAMPLES)
.collect()
})
.unwrap_or_default();
MaskShape::ColorRange { samples, refine: f64_or(p, "refine", 50.0) }
}
@@ -381,8 +398,9 @@ pub fn specs() -> Vec<CommandSpec> {
}
),
cmd!("mask.update", "Update Mask Shape", [], None, "{id?, component?: index (0), shape: MaskShape JSON}", has_active, |s, p| {
let shape: MaskShape =
let mut shape: MaskShape =
serde_json::from_value(p.get("shape").cloned().unwrap_or_default()).map_err(|e| bad("mask.update", e.to_string()))?;
cap_color_range_samples(&mut shape);
let comp = optional_index(p, "component", "mask.update")?.unwrap_or(0);
let mid = mask_id(p, s.active_mask, "mask.update")?;
masks_edit(s, "mask.update", "Edit Mask", |masks, _| {
@@ -1123,3 +1141,30 @@ fn pressure_list(v: Option<&Value>, points: usize) -> Result<Vec<f32>> {
.collect::<Option<Vec<f32>>>()
.ok_or_else(|| bad("mask.brushStroke", "pressure must be a list of numbers 0..1"))
}
#[cfg(test)]
mod tests_color_range {
use super::*;
/// Automation (`mask.add`/`mask.update`) used to take any number of colour-range samples; render
/// cost is pixels x samples, so a few hundred hung a large render. The list is now capped.
#[test]
fn color_range_samples_are_capped() {
let samples: Vec<Value> = (0..1000).map(|i| json!([i as f64 / 1000.0, 0.1, 0.2])).collect();
let shape = shape_from("colorRange", &json!({ "samples": samples }), "mask.add").unwrap();
let MaskShape::ColorRange { samples, .. } = shape else { panic!("expected ColorRange") };
assert_eq!(samples.len(), MAX_COLOR_RANGE_SAMPLES);
// A reasonable request is unchanged.
let few = shape_from("colorRange", &json!({ "samples": [[0.1, 0.2, 0.3], [0.4, 0.5, 0.6]] }), "mask.add").unwrap();
let MaskShape::ColorRange { samples, .. } = few else { panic!() };
assert_eq!(samples.len(), 2);
// mask.update deserializes the shape directly; it is capped the same way.
let mut shape = shape_from("colorRange", &json!({ "samples": [[0.1, 0.2, 0.3]] }), "mask.add").unwrap();
if let MaskShape::ColorRange { samples, .. } = &mut shape {
samples.resize(1000, samples[0]);
}
cap_color_range_samples(&mut shape);
let MaskShape::ColorRange { samples, .. } = shape else { panic!() };
assert_eq!(samples.len(), MAX_COLOR_RANGE_SAMPLES);
}
}
+86 -6
View File
@@ -90,9 +90,77 @@ fn stored_hash(p: &Photo) -> Option<&str> {
p.content_hash.as_deref().map(|h| h.split(':').next().unwrap_or(h)).filter(|h| !h.is_empty())
}
/// The content hash of the file at `path` (as import computes it), if it can be read.
/// The content hash of the file at `path` (as import computes it), if it can be read. Read in
/// pieces: the file is whatever is on the disk, of any size.
fn file_hash(path: &str) -> Option<String> {
std::fs::read(path).ok().map(|b| lightcraft_preview::hash_bytes(&b).to_string())
hash_reader(std::fs::File::open(path).ok()?)
}
/// The content hash of what `r` gives, read in pieces (the hash import computes of the whole).
fn hash_reader(mut r: impl std::io::Read) -> Option<String> {
let mut hasher = lightcraft_preview::Hasher128::new();
let mut piece = vec![0u8; 1 << 16];
loop {
match r.read(&mut piece) {
Ok(0) => return Some(hasher.finish().to_string()),
Ok(n) => {
hasher.update(piece.get(..n)?);
}
Err(e) if e.kind() == std::io::ErrorKind::Interrupted => {}
Err(_) => return None,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
/// A reader that gives its bytes a few at a time, and an interruption in between.
struct Trickle<'a>(&'a [u8], bool);
impl std::io::Read for Trickle<'_> {
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
self.1 = !self.1;
if self.1 {
return Err(std::io::ErrorKind::Interrupted.into());
}
let n = self.0.len().min(buf.len()).min(7);
buf[..n].copy_from_slice(&self.0[..n]);
self.0 = &self.0[n..];
Ok(n)
}
}
#[test]
fn a_hash_read_in_pieces_is_the_hash_of_the_whole() {
// longer than one piece, so the read loop runs more than once
let bytes: Vec<u8> = (0..200_000u32).map(|i| (i % 251) as u8).collect();
let whole = lightcraft_preview::hash_bytes(&bytes).to_string();
assert_eq!(hash_reader(bytes.as_slice()).as_deref(), Some(whole.as_str()));
assert_eq!(hash_reader(Trickle(&bytes[..1000], false)).as_deref(), Some(lightcraft_preview::hash_bytes(&bytes[..1000]).to_string().as_str()));
assert_eq!(hash_reader(std::io::empty()).as_deref(), Some(lightcraft_preview::hash_bytes(&[]).to_string().as_str()));
}
/// A scratch folder that goes away with the test, however it ends.
struct Scratch(std::path::PathBuf);
impl Drop for Scratch {
fn drop(&mut self) {
let _ = std::fs::remove_dir_all(&self.0);
}
}
#[test]
fn a_file_hash_is_the_import_hash_and_a_missing_file_has_none() {
let dir = Scratch(std::env::temp_dir().join(format!("lc-file-hash-{}", std::process::id())));
let _ = std::fs::remove_dir_all(&dir.0);
std::fs::create_dir_all(&dir.0).unwrap();
let f = dir.0.join("a.bin");
std::fs::write(&f, b"pixels").unwrap();
assert_eq!(file_hash(f.to_str().unwrap()), Some(lightcraft_preview::hash_bytes(b"pixels").to_string()));
assert_eq!(file_hash(dir.0.join("nope").to_str().unwrap()), None);
}
}
/// An in-scope photo ([`checked_path`]) as Find Missing Photos sees it: its file, the size and
@@ -197,13 +265,24 @@ impl FindPlan {
}
}
/// How Find Missing compares file names: the part after the last `/` or `\`, in lower case. A
/// library made on Windows keeps Windows paths, whose `\` isn't a separator to `Path` on macOS or
/// Linux. Files on disk are named by the same rule, so a macOS or Linux file name that holds a
/// `\` (`a\b.jpg`) still meets its photo, and a `b.jpg` beside it makes that match ambiguous
/// rather than a guess. With no `a\b.jpg` there, such a photo may be found as `b.jpg`, if the
/// size on record agrees.
fn name_key(path: &str) -> String {
path.rsplit(['/', '\\']).next().unwrap_or_default().to_lowercase()
}
/// The file-system half of Find Missing Photos: which `candidates` are gone, and which file in
/// `folder` (recursively) is each one's. Touches only the disk — never the session — so the app
/// runs it on a worker thread (walking a big folder on a network share and hashing look-alikes
/// takes a while) and relinks the result with `library.findMissing {found, missing, ambiguous}`.
///
/// Matching, per photo:
/// 1. a file with the same name (any letter case), the same size and the same content hash;
/// 1. a file with the same name ([`name_key`]: any letter case), the same size and the same
/// content hash;
/// 2. renamed: a file of the same size with the same content hash (only same-size files are
/// read, so this stays cheap);
/// 3. without a hash to confirm (or the file was edited in place): the one file with the same
@@ -228,8 +307,9 @@ pub fn plan_find_missing(candidates: &[FindCandidate], folder: &str) -> std::res
let mut by_name: HashMap<String, Vec<String>> = HashMap::new();
let files: Vec<String> = crate::import::expand(&[folder.to_string()], None).into_iter().filter(|f| !in_use.contains(f.as_str())).collect();
for f in &files {
if let Some(n) = Path::new(f).file_name() {
by_name.entry(n.to_string_lossy().to_lowercase()).or_default().push(f.clone());
let name = name_key(f);
if !name.is_empty() {
by_name.entry(name).or_default().push(f.clone());
}
}
let mut by_size: Option<HashMap<u64, Vec<String>>> = None;
@@ -239,7 +319,7 @@ pub fn plan_find_missing(candidates: &[FindCandidate], folder: &str) -> std::res
let mut used: HashSet<String> = HashSet::new();
let mut plan = FindPlan::default();
for c in &lost {
let name = Path::new(&c.path).file_name().map(|n| n.to_string_lossy().to_lowercase()).unwrap_or_default();
let name = name_key(&c.path);
let size = c.size;
let hash = c.hash.as_deref();
let named: Vec<&String> = by_name
+74 -6
View File
@@ -8,12 +8,12 @@
//! * `print.get`, `print.set {layout?, image?, page?, guides?, job?}`, `print.reset`
//! * `print.cell.add|move|resize|delete|stack|photo|page`, `print.layout.autoLayout|newPage|clear`
//! * `print.exportPdf {path, ids?}`
//! * `print.template.list|save|apply|delete`
//! * `print.template.list|save|apply|delete`, `print.template.export|import` (`.lcprint` files)
use serde_json::{Value, json};
use super::{CommandSpec, always, bad, cmd, f64_req};
use crate::print::layout::{Layout, MAX_PAGES, Rect, Snap, Stack, paper_names, paper_size};
use crate::print::template::{MAX_TEMPLATES, PrintSettings, PrintTemplate, clean_name};
use crate::print::template::{FILE_EXT, MAX_FILE, MAX_TEMPLATES, PrintSettings, PrintTemplate, clean_name, unique_name};
use crate::{Result, Session};
fn rect_json(r: &Rect) -> Value {
@@ -277,10 +277,21 @@ fn template_name(p: &Value, id: &str) -> Result<String> {
clean_name(p.get("name").and_then(Value::as_str).ok_or_else(|| bad(id, "missing `name`"))?).map_err(|e| bad(id, e))
}
/// Save the preferences after a change to the templates; if that fails, the templates go back to
/// `before`, so the list matches what is saved and a retry does the same thing.
fn save_templates(s: &mut Session, before: Vec<PrintTemplate>) -> Result<()> {
if let Err(e) = s.save_prefs() {
s.print_templates = before;
return Err(e);
}
Ok(())
}
fn template_save(s: &mut Session, p: &Value) -> Result<Value> {
const ID: &str = "print.template.save";
let name = template_name(p, ID)?;
let t = PrintTemplate::new(&name, &s.print).map_err(|e| bad(ID, e))?;
let before = s.print_templates.clone();
match s.print_templates.iter_mut().find(|x| x.name.eq_ignore_ascii_case(&name)) {
Some(x) => *x = t,
None => {
@@ -290,7 +301,7 @@ fn template_save(s: &mut Session, p: &Value) -> Result<Value> {
s.print_templates.push(t);
}
}
s.save_prefs()?;
save_templates(s, before)?;
Ok(template_list(s))
}
@@ -309,15 +320,66 @@ fn template_apply(s: &mut Session, p: &Value) -> Result<Value> {
fn template_delete(s: &mut Session, p: &Value) -> Result<Value> {
const ID: &str = "print.template.delete";
let name = template_name(p, ID)?;
let before = s.print_templates.len();
let before = s.print_templates.clone();
s.print_templates.retain(|x| !x.name.eq_ignore_ascii_case(&name));
if s.print_templates.len() == before {
if s.print_templates.len() == before.len() {
return Err(bad(ID, format!("no print template `{name}`")));
}
s.save_prefs()?;
save_templates(s, before)?;
Ok(template_list(s))
}
fn template_path<'a>(p: &'a Value, id: &str) -> Result<&'a str> {
p.get("path").and_then(Value::as_str).map(str::trim).filter(|x| !x.is_empty()).ok_or_else(|| bad(id, "missing `path`"))
}
fn template_export(s: &mut Session, p: &Value) -> Result<Value> {
const ID: &str = "print.template.export";
let name = template_name(p, ID)?;
let mut path = std::path::PathBuf::from(template_path(p, ID)?);
if path.extension().is_none() {
path.set_extension(FILE_EXT);
}
let t = s
.print_templates
.iter()
.find(|x| x.name.eq_ignore_ascii_case(&name))
.ok_or_else(|| bad(ID, format!("no print template `{name}` (see print.template.list)")))?;
let bytes = serde_json::to_vec_pretty(&t.to_file()).map_err(|e| crate::EngineError::Other(e.to_string()))?;
let name = t.name.clone();
let target = path.to_string_lossy();
s.check_write_target(&target).map_err(|e| bad(ID, e))?;
crate::export::write_file(&target, &bytes).map_err(|e| bad(ID, e))?;
Ok(json!({"name": name, "path": target}))
}
fn template_import(s: &mut Session, p: &Value) -> Result<Value> {
const ID: &str = "print.template.import";
let path = std::path::Path::new(template_path(p, ID)?);
let meta = std::fs::metadata(path).map_err(|e| bad(ID, format!("{}: {e}", path.display())))?;
if !meta.is_file() {
return Err(bad(ID, format!("{}: not a file", path.display())));
}
if meta.len() > MAX_FILE {
return Err(bad(ID, format!("{}: {} bytes is too large for a print template (at most {} KB)", path.display(), meta.len(), MAX_FILE / 1024)));
}
let bytes = std::fs::read(path).map_err(|e| bad(ID, format!("{}: {e}", path.display())))?;
let mut t = PrintTemplate::from_file(&bytes).map_err(|e| bad(ID, format!("{}: {e}", path.display())))?;
if s.print_templates.len() >= MAX_TEMPLATES {
return Err(bad(ID, format!("at most {MAX_TEMPLATES} templates; delete one first")));
}
let name = unique_name(&t.name, &s.print_templates);
let renamed = name != t.name;
t.name = name.clone();
let before = s.print_templates.clone();
s.print_templates.push(t);
save_templates(s, before)?;
let mut out = template_list(s);
out["name"] = json!(name);
out["renamed"] = json!(renamed);
Ok(out)
}
pub fn specs() -> Vec<CommandSpec> {
vec![
cmd!(query "print.get", "Print Settings", [], None,
@@ -349,5 +411,11 @@ pub fn specs() -> Vec<CommandSpec> {
always, template_save),
cmd!(query "print.template.apply", "Apply Print Template", [], None, "{name} → print.get", always, template_apply),
cmd!(query "print.template.delete", "Delete Print Template", [], None, "{name} → print.template.list", always, template_delete),
cmd!(query "print.template.export", "Export Print Template", [], None,
"{name, path}: writes the template as a .lcprint JSON file ({format, version, name, settings}; `.lcprint` is added when the path has no extension); replaces an existing file, never an original → {name, path}",
always, template_export),
cmd!(query "print.template.import", "Import Print Template", [], None,
"{path: a .lcprint file (at most 256 KB)}: checked like a saved template (format, version, name, every setting; cell photos and guides are dropped) and added under its name; a name taken (any case) gets \" (2)\"… → print.template.list + {name, renamed}",
always, template_import),
]
}
+9 -5
View File
@@ -1,6 +1,8 @@
//! Read-only queries (not journaled): catalog, photos, develop state, controls, presets, albums.
use lightcraft_catalog::{Album, Photo, PhotoId};
use std::collections::HashMap;
use lightcraft_catalog::{Album, AlbumId, Photo, PhotoId};
use lightcraft_develop::{CONTROLS, controls};
use serde_json::{Value, json};
@@ -36,9 +38,9 @@ pub fn photo_summary(p: &Photo) -> Value {
/// instead of overflowing the stack.
const MAX_ALBUM_DEPTH: usize = 64;
fn album_json(a: &Album, all: &[Album], cat: &lightcraft_catalog::Catalog, depth: usize) -> Value {
fn album_json(a: &Album, all: &[Album], counts: &HashMap<AlbumId, usize>, cat: &lightcraft_catalog::Catalog, depth: usize) -> Value {
let children: Vec<Value> = if depth < MAX_ALBUM_DEPTH {
all.iter().filter(|c| c.parent == Some(a.id)).map(|c| album_json(c, all, cat, depth + 1)).collect()
all.iter().filter(|c| c.parent == Some(a.id)).map(|c| album_json(c, all, counts, cat, depth + 1)).collect()
} else {
Vec::new()
};
@@ -46,7 +48,7 @@ fn album_json(a: &Album, all: &[Album], cat: &lightcraft_catalog::Catalog, depth
"id": a.id.0,
"name": a.name,
"folder": a.folder,
"count": cat.album_count(a.id),
"count": counts.get(&a.id).copied().unwrap_or(0),
"cover": a.cover.map(|c| c.0),
"children": children,
});
@@ -152,7 +154,9 @@ pub fn specs() -> Vec<CommandSpec> {
}),
cmd!(query "albums.list", "List Albums", [], None, "{}", always, |s, _| {
let all: Vec<Album> = s.catalog.albums().cloned().collect();
Ok(Value::Array(all.iter().filter(|a| a.parent.is_none()).map(|a| album_json(a, &all, &s.catalog, 0)).collect()))
// every album counted in one pass, so a smart album tested by others is worked out once
let counts = lightcraft_catalog::tally::AlbumTally::of(&s.catalog);
Ok(Value::Array(all.iter().filter(|a| a.parent.is_none()).map(|a| album_json(a, &all, counts.counts(), &s.catalog, 0)).collect()))
}),
cmd!(query "photo.inspect", "Inspect Photo", [], None, "{id?}", always, |s, p| {
let id = photo_arg(s, p, "photo.inspect")?;
+1 -1
View File
@@ -379,7 +379,7 @@ pub fn specs() -> Vec<CommandSpec> {
"Slideshow Playback Settings",
[],
None,
"{slideDuration?: seconds (0.5–120), fadeDuration?: seconds (0 = cut; at most 10 and the slide duration), fadeColor?: \"#rrggbb\"|[r,g,b]|null (fade through a colour instead of crossfading), randomOrder?: bool, repeat?: bool, preparePreviews?: bool (render the next slides before they are due)} — omitted keys keep their value, {} reads them; saved with the library → {slideDuration, fadeDuration, fadeColor, randomOrder, repeat, preparePreviews}",
"{slideDuration?: seconds (0.5–120), fadeDuration?: seconds (0 = cut; at most 10 and the slide duration), fadeColor?: \"#rrggbb\"|[r,g,b]|null (fade through a colour instead of crossfading), randomOrder?: bool, repeat?: bool, preparePreviews?: bool (render the next slides before they are due), manual?: bool (manual slideshow: a slide stays up until ← or → changes it)} — omitted keys keep their value, {} reads them; saved with the library → {slideDuration, fadeDuration, fadeColor, randomOrder, repeat, preparePreviews, manual}",
always,
playback
),
+1 -1
View File
@@ -31,7 +31,7 @@ pub fn load(s: &mut Session) {
..Default::default()
};
// one demo photo carries a camera focus point (as a Sony raw would), on the bloom's centre, so
// View ▸ Show Focus Point and Zoom to Focus Point can be tried without a camera file
// View ▸ Focus Point and Zoom to Focus Point can be tried without a camera file
if sc.name == "Cosmos bloom" {
p.meta.focus = Some(lightcraft_meta::FocusPoint {
rect: lightcraft_geom::Rect { x0: 0.42, y0: 0.433, x1: 0.50, y1: 0.487 },
+13 -6
View File
@@ -38,13 +38,13 @@ impl std::error::Error for NotFollowed {}
/// was opened from.
pub fn resolve(stored: &str, document: Option<&Path>) -> Result<PathBuf, NotFollowed> {
let document = document.map(|d| if d.is_relative() { std::path::absolute(d).unwrap_or_else(|_| d.to_path_buf()) } else { d.to_path_buf() });
let path = craft_docpath::resolve(stored, document.as_deref()).map_err(NotFollowed::Refused)?;
// A path that is still relative would be taken from the working directory, which the
// document does not choose.
if path.is_relative() {
return Err(NotFollowed::NotHere);
match craft_docpath::resolve_absolute(stored, document.as_deref()) {
Ok(path) => Ok(path),
// A usable path that is not absolute here: a Windows path on macOS or Linux, or a
// relative path with no document folder to take it from.
Err(Refused::Invalid) if craft_docpath::resolve(stored, document.as_deref()).is_ok() => Err(NotFollowed::NotHere),
Err(r) => Err(NotFollowed::Refused(r)),
}
Ok(path)
}
#[cfg(test)]
@@ -59,5 +59,12 @@ mod tests {
assert_eq!(resolve(r"\\192.0.2.1\share\x.jpg", Some(&doc)), Err(NotFollowed::Refused(Refused::Network)));
assert_eq!(resolve("file://192.0.2.1/s/x.jpg", Some(&doc)), Err(NotFollowed::Refused(Refused::Network)));
assert_eq!(resolve(r"\\.\COM1", Some(&doc)), Err(NotFollowed::Refused(Refused::Device)));
#[cfg(windows)]
assert_eq!(resolve("Photos/2024:05/a.jpg", Some(&doc)), Err(NotFollowed::Refused(Refused::Invalid)));
#[cfg(not(windows))]
assert_eq!(resolve(r"C:\Photos\a.jpg", Some(&doc)), Err(NotFollowed::NotHere));
let gvfs = Path::new("/run/user/1000/gvfs/smb-share:server=tokyo,share=photos/catalog.lrcat");
#[cfg(not(windows))]
assert_eq!(resolve("a.jpg", Some(gvfs)), Ok(gvfs.with_file_name("a.jpg")));
}
}
+194 -38
View File
@@ -501,11 +501,12 @@ pub(crate) fn watermark_logo(path: &str) -> Option<std::sync::Arc<Rgba8>> {
}
/// A graphic file (a PNG with transparency, typically) as 8-bit sRGB with its alpha: the
/// watermark logo, the loupe's layout image. The error says why it couldn't be read or decoded.
/// watermark logo, the loupe's layout image. Applies EXIF orientation before either consumer sizes it.
/// The error says why it couldn't be read or decoded.
pub fn read_graphic(path: &str) -> Result<Rgba8, String> {
let bytes = std::fs::read(path).map_err(|e| format!("can't read {path}: {e}"))?;
let decoded = lightcraft_codecs::decode(&bytes, Default::default()).map_err(|e| format!("can't decode {path}: {e}"))?;
Ok(decoded.to_srgb8())
Ok(decoded.to_srgb8().into_oriented(lightcraft_geom::Orientation::from_exif(decoded.orientation)))
}
/// The graphic watermark: scaled (premultiplied, so transparent edges stay clean) to
@@ -518,20 +519,27 @@ fn logo_coverage(width: usize, height: usize, wm: &Watermark, mut blend_px: impl
if width == 0 || height == 0 || logo.width == 0 || logo.height == 0 {
return;
}
let lw = ((wm.image_width.clamp(0.01, 1.0) * width as f32).round() as usize).clamp(1, width);
let lh = ((lw as f32 * logo.height as f32 / logo.width as f32).round() as usize).clamp(1, height);
let turns = wm.quarter_turns();
// Reserve the inset on anchored axes before fitting the turned box. Centered axes
// ignore insets, just as placement below does. One scale preserves the graphic's shape.
let short = width.min(height) as f32;
let (ix, iy) = (wm.inset_x.clamp(0.0, 0.4) * short, wm.inset_y.clamp(0.0, 0.4) * short);
use Anchor::*;
let available_w = width as f64 - if matches!(wm.anchor, Top | Center | Bottom) { 0.0 } else { ix as f64 };
let available_h = height as f64 - if matches!(wm.anchor, Left | Center | Right) { 0.0 } else { iy as f64 };
let (max_w, max_h) = if turns % 2 == 1 { (available_h, available_w) } else { (available_w, available_h) };
let scale =
(wm.image_width.clamp(0.01, 1.0) as f64 * width as f64 / logo.width as f64).min(max_w / logo.width as f64).min(max_h / logo.height as f64);
let lw = (logo.width as f64 * scale).round().max(1.0) as usize;
let lh = (logo.height as f64 * scale).round().max(1.0) as usize;
let pre: lightcraft_raster::Image<[f32; 4]> = lightcraft_raster::Image::from_fn(logo.width, logo.height, |x, y| {
let p = logo.get(x, y);
let a = p[3] as f32 / 255.0;
[p[0] as f32 * a, p[1] as f32 * a, p[2] as f32 * a, a]
});
let scaled = lightcraft_raster::resample::resize(&pre, lw, lh, lightcraft_raster::resample::Filter::Mitchell);
let short = width.min(height) as f32;
let (ix, iy) = (wm.inset_x.clamp(0.0, 0.4) * short, wm.inset_y.clamp(0.0, 0.4) * short);
let turns = wm.quarter_turns();
let (bw, bh) = if turns % 2 == 1 { (lh, lw) } else { (lw, lh) };
let (w, h, lwf, lhf) = (width as f32, height as f32, bw as f32, bh as f32);
use Anchor::*;
let ox = match wm.anchor {
TopLeft | Left | BottomLeft => ix,
Top | Center | Bottom => (w - lwf) / 2.0,
@@ -658,8 +666,8 @@ pub struct ExportOptions {
/// format's default (TIFF 16, everything else 8). See [`ExportOptions::effective_depth`].
pub bit_depth: Option<u8>,
/// HDR output for photos edited in HDR: JPEG as an ISO 21496-1 gain map JPEG (the SDR
/// rendition plus a gain map, so the file looks right everywhere; quality as set, `limit_kb`
/// not applied), AVIF as 10-bit Rec. 2020 PQ, PNG as 16-bit Rec. 2020 PQ (`cICP`; viewers
/// rendition plus a gain map, so the file looks right everywhere; `limit_kb` includes both
/// images and their metadata), AVIF as 10-bit Rec. 2020 PQ, PNG as 16-bit Rec. 2020 PQ (`cICP`; viewers
/// without cICP support show it as sRGB, washed out), JPEG XL as 16-bit Rec. 2020 PQ, 32-bit float
/// TIFF with the highlights above SDR white kept. Other formats, and photos without an HDR
/// edit, are SDR.
@@ -1392,29 +1400,7 @@ pub fn encode_with_metadata(img: &Rgba8, o: &ExportOptions, meta: Option<&Metada
ExportFormat::Jpeg => {
// 4:4:4 normally; 4:2:0 when targeting a file size (much smaller at equal visual quality).
let sub = if o.limit_kb.is_some() { ChromaSubsampling::S420 } else { ChromaSubsampling::S444 };
let jpeg = |q: u8| encode::encode_jpeg(&e, q, sub, &meta);
match o.limit_kb {
Some(kb) => {
let limit = kb as usize * 1024;
// Binary search for the highest quality that fits.
let (mut lo, mut hi) = (1u8, o.quality);
let mut best = None;
while lo <= hi {
let mid = lo + (hi - lo) / 2;
let b = jpeg(mid).map_err(|e| e.to_string())?;
if b.len() <= limit {
best = Some(b);
lo = mid + 1;
} else if mid == 1 {
break;
} else {
hi = mid - 1;
}
}
return best.ok_or_else(|| format!("cannot fit within {kb} KB"));
}
None => jpeg(o.quality),
}
return encode_jpeg_with_limit(o, |q| encode::encode_jpeg(&e, q, sub, &meta));
}
ExportFormat::Png => encode::encode_png(&e, &meta),
ExportFormat::Tiff => encode::encode_tiff(&e, o.tiff_compression, &meta),
@@ -1428,6 +1414,27 @@ pub fn encode_with_metadata(img: &Rgba8, o: &ExportOptions, meta: Option<&Metada
r.map_err(|e| e.to_string())
}
/// Apply the same file-size policy to ordinary JPEGs and complete HDR gain map JPEGs.
fn encode_jpeg_with_limit(o: &ExportOptions, jpeg: impl Fn(u8) -> lightcraft_codecs::Result<Vec<u8>>) -> Result<Vec<u8>, String> {
let Some(kb) = o.limit_kb else { return jpeg(o.quality).map_err(|e| e.to_string()) };
let limit = u64::from(kb) * 1024;
let (mut lo, mut hi) = (1u8, o.quality.min(100));
let mut best = None;
while lo <= hi {
let mid = lo + (hi - lo) / 2;
let b = jpeg(mid).map_err(|e| e.to_string())?;
if b.len() as u64 <= limit {
best = Some(b);
lo = mid + 1;
} else if mid == 1 {
break;
} else {
hi = mid - 1;
}
}
best.ok_or_else(|| format!("cannot fit within {kb} KB"))
}
/// Output sharpening and the watermark on an 8-bit render in `o.effective_space()`.
fn finish_8bit(img: &Rgba8, o: &ExportOptions) -> Rgba8 {
let mut img = img.clone();
@@ -1475,7 +1482,7 @@ pub fn encode_gain_map_jpeg(sdr: &Rgba8, hdr: &DeepImage, o: &ExportOptions, met
let xmp = meta.map(|m| lightcraft_meta::write_xmp(m, None));
let em = EncodeMeta { icc: Some(&profile), exif: exif.as_deref(), xmp: xmp.as_deref(), ppi: Some(o.ppi) };
let sub = if o.limit_kb.is_some() { ChromaSubsampling::S420 } else { ChromaSubsampling::S444 };
gainmap::encode_jpeg(&EncodeImage::rgba8(&base), &map, &gm, o.quality, sub, &em).map_err(|e| e.to_string())
encode_jpeg_with_limit(o, |q| gainmap::encode_jpeg(&EncodeImage::rgba8(&base), &map, &gm, q, sub, &em))
}
/// The codecs' name of an output space (for its ICC profile).
@@ -1827,10 +1834,14 @@ impl PreparedExport {
};
let Some(dng) = dng else {
// the file is copied byte for byte: refuse rather than leak a location it embeds
if remove_location && lightcraft_meta::extract(&bytes).gps.is_some() {
return Err(format!(
"{label}: can't remove the location from an original file that embeds GPS; export as JPEG, TIFF or DNG instead"
));
if remove_location {
let metadata = lightcraft_meta::try_extract(&bytes)
.map_err(|e| format!("{label}: can't verify location metadata in this original file: {e}"))?;
if metadata.gps.is_some() {
return Err(format!(
"{label}: can't remove the location from an original file that embeds GPS; export as JPEG, TIFF or DNG instead"
));
}
}
return Ok(Exported {
file_name,
@@ -2015,6 +2026,18 @@ pub fn write_file_durable(path: &str, bytes: &[u8]) -> Result<(), String> {
write_with(path, bytes, true)
}
/// Write `path` only when nothing exists there: the destination is prepared as a complete temp
/// file and published with create-new semantics, so a file that appears after a caller's check is
/// never replaced. Screenshots of an initially absent path use this instead of the overwriting
/// writer.
pub fn write_file_new(path: &str, bytes: &[u8]) -> Result<(), String> {
let p = std::path::Path::new(path);
if let Some(dir) = p.parent().filter(|d| !d.as_os_str().is_empty()) {
std::fs::create_dir_all(dir).map_err(|e| format!("{}: {e}", dir.display()))?;
}
lightcraft_catalog::safe_file::write_new(p, bytes).map_err(|e| format!("{path}: {e}"))
}
fn write_with(path: &str, bytes: &[u8], durable: bool) -> Result<(), String> {
use lightcraft_catalog::safe_file::{write_atomic, write_atomic_nosync};
let p = std::path::Path::new(path);
@@ -2425,6 +2448,139 @@ mod tests {
assert!(b.len() <= kb as usize * 1024);
}
#[test]
fn hdr_jpeg_size_limit_respected() {
let img = Rgba8::from_fn(256, 192, |x, y| {
let n = (x as u32).wrapping_mul(73856093) ^ (y as u32).wrapping_mul(19349663);
[n as u8, (n >> 8) as u8, (n >> 16) as u8, 255]
});
let hdr = DeepImage {
width: img.width,
height: img.height,
space: OutputSpace::Srgb,
samples: DeepSamples::F32(
img.data.iter().flat_map(|p| p[..3].iter().map(|v| lightcraft_color::transfer::decode_srgb8(*v) * 4.0)).collect(),
),
};
let opts = ExportOptions { hdr: true, quality: 100, limit_kb: Some(u32::MAX), ..Default::default() };
let full = encode_gain_map_jpeg(&img, &hdr, &opts, None).unwrap();
let kb = (full.len() / 2048) as u32;
let limited = encode_gain_map_jpeg(&img, &hdr, &ExportOptions { limit_kb: Some(kb), ..opts.clone() }, None).unwrap();
assert!(limited.len() <= kb as usize * 1024, "{} bytes exceeds {kb} KB", limited.len());
let decoded = lightcraft_codecs::gainmap::read_jpeg(&limited).unwrap();
assert!(decoded.meta.alternate_headroom > 1.0, "the gain map must survive size limiting");
let impossible = encode_gain_map_jpeg(&img, &hdr, &ExportOptions { limit_kb: Some(1), ..opts }, None);
assert!(impossible.is_err_and(|e| e.contains("cannot fit within 1 KB")));
}
#[test]
fn graphic_watermark_honours_exif_orientation() {
let dir = std::env::temp_dir().join(format!("lc-wm-orientation-{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
let logo = Rgba8::from_fn(40, 20, |x, _| if x < 20 { [255, 0, 0, 255] } else { [0, 0, 255, 255] });
for orientation in [2, 6] {
let meta = Metadata { orientation: Some(lightcraft_geom::Orientation::from_exif(orientation)), ..Default::default() };
let exif = lightcraft_meta::write_exif(&meta);
let bytes = lightcraft_codecs::encode_jpeg(
&EncodeImage::rgba8(&logo),
100,
ChromaSubsampling::S444,
&EncodeMeta { exif: Some(&exif), ..Default::default() },
)
.unwrap();
let path = dir.join(format!("oriented-{orientation}.jpg"));
std::fs::write(&path, bytes).unwrap();
let wm = Watermark {
image: path.to_string_lossy().into(),
image_width: if orientation == 6 { 0.25 } else { 0.5 },
opacity: 1.0,
inset_x: 0.0,
inset_y: 0.0,
..Default::default()
};
let mut img = Rgba8::from_fn(80, 80, |_, _| [0, 0, 0, 255]);
draw_watermark(&mut img, &wm);
if orientation == 6 {
assert!(img.get(70, 45)[0] > 240 && img.get(70, 75)[2] > 240, "EXIF 6: red above blue");
} else {
assert!(img.get(45, 70)[2] > 240 && img.get(75, 70)[0] > 240, "EXIF 2: blue left of red");
}
}
std::fs::remove_dir_all(dir).unwrap();
}
#[test]
fn graphic_watermark_keeps_aspect_when_fitted_and_rotated() {
let dir = std::env::temp_dir().join(format!("lc-wm-aspect-{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
let path = dir.join("tall.png");
let logo = Rgba8::from_fn(40, 160, |_, _| [255, 0, 0, 255]);
std::fs::write(&path, lightcraft_codecs::encode_png(&EncodeImage::rgba8(&logo), &EncodeMeta::default()).unwrap()).unwrap();
for (rotation, expected) in [(0, (20, 80)), (90, (120, 30)), (180, (20, 80)), (270, (120, 30))] {
let wm = Watermark {
image: path.to_string_lossy().into(),
image_width: 0.5,
opacity: 1.0,
inset_x: 0.0,
inset_y: 0.0,
rotation,
..Default::default()
};
let mut img = Rgba8::from_fn(120, 80, |_, _| [0, 0, 0, 255]);
draw_watermark(&mut img, &wm);
let lit: Vec<_> = img.data.iter().enumerate().filter(|(_, p)| p[0] > 200).map(|(i, _)| (i % 120, i / 120)).collect();
let bounds = (
lit.iter().map(|p| p.0).max().unwrap() - lit.iter().map(|p| p.0).min().unwrap() + 1,
lit.iter().map(|p| p.1).max().unwrap() - lit.iter().map(|p| p.1).min().unwrap() + 1,
);
assert_eq!(bounds, expected, "{rotation} degrees: fitting must preserve the logo's aspect ratio");
}
std::fs::remove_dir_all(dir).unwrap();
}
#[test]
fn graphic_watermark_fit_preserves_anchor_insets() {
let dir = std::env::temp_dir().join(format!("lc-wm-insets-{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
let path = dir.join("tall.png");
let logo = Rgba8::from_fn(40, 160, |_, _| [255, 0, 0, 255]);
std::fs::write(&path, lightcraft_codecs::encode_png(&EncodeImage::rgba8(&logo), &EncodeMeta::default()).unwrap()).unwrap();
use Anchor::*;
for (anchor, rotation, expected) in [
(BottomRight, 90, (0, 44, 111, 71)),
(TopLeft, 270, (8, 8, 119, 35)),
(Right, 90, (0, 26, 111, 53)),
(BottomRight, 0, (94, 0, 111, 71)),
(TopLeft, 180, (8, 8, 25, 79)),
(Bottom, 0, (51, 0, 68, 71)),
// Centered axes ignore their inset rather than shrinking the image unnecessarily.
(Center, 90, (0, 25, 119, 54)),
(Top, 90, (0, 8, 119, 37)),
] {
let wm = Watermark {
image: path.to_string_lossy().into(),
image_width: 0.5,
opacity: 1.0,
inset_x: 0.1,
inset_y: 0.1,
rotation,
anchor,
..Default::default()
};
let mut img = Rgba8::from_fn(120, 80, |_, _| [0, 0, 0, 255]);
draw_watermark(&mut img, &wm);
let lit: Vec<_> = img.data.iter().enumerate().filter(|(_, p)| p[0] > 200).map(|(i, _)| (i % 120, i / 120)).collect();
let bounds = (
lit.iter().map(|p| p.0).min().unwrap(),
lit.iter().map(|p| p.1).min().unwrap(),
lit.iter().map(|p| p.0).max().unwrap(),
lit.iter().map(|p| p.1).max().unwrap(),
);
assert_eq!(bounds, expected, "{anchor:?}, {rotation} degrees");
}
std::fs::remove_dir_all(dir).unwrap();
}
#[test]
fn sharpen_increases_edge_contrast() {
let mut img = Rgba8::new(8, 8);
+17 -1
View File
@@ -1177,6 +1177,22 @@ pub(crate) mod tests {
assert_eq!(super::meta_of(&m).0.keywords, ["travel|Italy|Rome", "beach"]);
}
#[test]
fn psd_probe_reads_embedded_capture_time_gps_and_title() {
let capture = lightcraft_meta::DateTime::parse_iso("2023-05-14T14:40:39").unwrap();
let metadata = lightcraft_meta::Metadata {
capture_time: Some(capture),
gps: Some(lightcraft_meta::Gps { latitude: 45.0, longitude: 7.0, altitude: None }),
title: Some("PSD metadata".into()),
..Default::default()
};
let bytes = crate::tests_xmp::synthetic_psd_with_metadata(&metadata);
let probe = probe_bytes("metadata.psd", &bytes).unwrap();
assert_eq!(probe.captured.as_deref(), Some("2023-05-14T14:40:39"));
assert_eq!(probe.meta.gps, Some((45.0, 7.0)));
assert_eq!(probe.meta.title, "PSD metadata");
}
use super::*;
use lightcraft_codecs::{ChromaSubsampling, EncodeImage, EncodeMeta, Samples, encode_jpeg};
@@ -2809,7 +2825,7 @@ pub(crate) mod tests {
let _ = std::fs::remove_dir_all(&dir);
}
/// A Sony ARW of `model`: a 64 × 48 uncompressed 14-bit CFA strip in a SubIFD (as `crates/raw/tests/vendor_robust.rs`
/// A Sony ARW of `model`: a 64 × 48 uncompressed 14-bit CFA strip in a SubIFD (as `crates/raw/tests/it/vendor_robust.rs`
/// lays it out) whose IFD carries the full-frame 16–35 mm distortion table (tag 0x7037).
fn sony_arw(model: &str) -> Vec<u8> {
use lightcraft_tiff::tags::{self as t, photometric};
+83 -1
View File
@@ -36,7 +36,7 @@ use crate::media::ProbeInfo;
/// This includes preview-only raws; the file probe decides whether sensor decoding is available.
pub const EXTENSIONS: &[&str] = &[
"jpg", "jpeg", "png", "tif", "tiff", "webp", "dng", "cr2", "cr3", "nef", "nrw", "arw", "sr2", "srf", "raf", "orf", "rw2", "rwl", "raw", "pef",
"srw", "psd", "jxl", "gif", "bmp",
"srw", "psd", "psb", "jxl", "gif", "bmp",
// Decoded when built with the codecs' `heif` feature; otherwise the import reports them as
// failed with the reason ("HEIC/HEIF support isn't included in this build").
"heic", "heif", "avif", // no AV1 decoder yet: reported as failed, like a HEIC without the feature
@@ -374,6 +374,42 @@ pub fn is_supported(path: &Path) -> bool {
path.extension().is_some_and(|e| EXTENSIONS.contains(&e.to_string_lossy().to_lowercase().as_str()))
}
/// The web file picker's accept list, derived from [`EXTENSIONS`]. Each extension
/// is listed in both cases: browsers' `accept` matching is case-insensitive per
/// spec, but some implementations (e.g. Windows/Edge) match case-sensitively and
/// rejected uppercase `.NEF` files, so a lowercase-only list silently hid raws
/// such as CR3, NRW, RAF, ORF, RW2, RWL, RAW, PEF and SRW from the picker.
///
/// The picker only offers files this build can actually import: `.avif` has no
/// AV1 decoder in any build, and `.heic`/`.heif` decode only when the codecs'
/// `heif` feature is enabled ([`lightcraft_codecs::Format::Heif::can_decode`]).
/// [`EXTENSIONS`] itself stays complete — files on disk are still recognised,
/// and import reports them as failed, saying why.
pub fn accept_list() -> String {
let heif_decodable = lightcraft_codecs::Format::Heif.can_decode();
let mut out = String::new();
for &ext in EXTENSIONS {
// Skip extensions a web user could pick but this build could not import.
let pickable = match ext {
"avif" => false,
"heic" | "heif" => heif_decodable,
_ => true,
};
if !pickable {
continue;
}
if !out.is_empty() {
out.push(',');
}
out.push('.');
out.push_str(ext);
out.push(',');
out.push('.');
out.push_str(&ext.to_uppercase());
}
out
}
/// Expand files and folders (recursively) into supported files. `skip` (e.g. the library folder)
/// is never descended into. Each folder's walk is bounded ([`crate::walk::Limits::default`]); a
/// walk that stops at a bound is logged.
@@ -1607,6 +1643,52 @@ mod tests_known;
mod prepared_tests {
use super::*;
/// Extensions the picker may offer: every one in [`EXTENSIONS`], minus the
/// files this build cannot import (see [`accept_list`]).
fn pickable_extensions() -> Vec<&'static str> {
let heif_decodable = lightcraft_codecs::Format::Heif.can_decode();
EXTENSIONS
.iter()
.copied()
.filter(|ext| match *ext {
"avif" => false,
"heic" | "heif" => heif_decodable,
_ => true,
})
.collect()
}
#[test]
fn accept_list_covers_every_pickable_extension_in_both_cases() {
let list = accept_list();
for ext in pickable_extensions() {
assert!(list.contains(&format!(".{ext}")), "lowercase .{ext} missing from accept list");
assert!(list.contains(&format!(".{}", ext.to_uppercase())), "uppercase .{} missing from accept list", ext.to_uppercase());
}
}
/// The web picker must not offer files this build cannot import: `.avif` has
/// no AV1 decoder in any build, so it is never listed, in either case.
#[test]
fn accept_list_never_offers_avif() {
let list = accept_list();
assert!(!list.contains(".avif"), "{list}");
assert!(!list.contains(".AVIF"), "{list}");
}
/// `.heic`/`.heif` follow the codecs' `heif` feature: listed (in both cases)
/// only when this build can decode them; otherwise hidden, so a web user
/// cannot pick a file that would fail to import.
#[test]
fn accept_list_offers_heic_heif_only_when_decodable() {
let list = accept_list();
let listed = lightcraft_codecs::Format::Heif.can_decode();
for ext in ["heic", "heif"] {
assert_eq!(list.contains(&format!(".{ext}")), listed, "lowercase .{ext}");
assert_eq!(list.contains(&format!(".{}", ext.to_uppercase())), listed, "uppercase .{ext}");
}
}
#[test]
fn revalidate_add_only_converts_matching_ready_item() {
let mut s = Session::new();
+73 -12
View File
@@ -195,6 +195,31 @@ pub struct Interaction {
/// The most photos an edit may touch for the view to follow it photo by photo; a larger edit is worked out whole.
const MAX_FOLLOWED: usize = 256;
/// The established clock precision for relative-date views and sidebar counts.
fn clock_minute(now: &str) -> &str {
now.get(..16).unwrap_or(now)
}
/// Whether a query can change membership without a catalog edit, including the smart albums it tests.
fn filter_depends_on_clock(filter: &Filter, catalog: &Catalog) -> bool {
if filter.depends_on_now() {
return true;
}
let mut pending = filter.albums_tested();
let mut seen = std::collections::HashSet::new();
while let Some(id) = pending.pop() {
if !seen.insert(id) {
continue;
}
let Some(smart) = catalog.album(id).and_then(|album| album.smart.as_deref()) else { continue };
if smart.depends_on_now() {
return true;
}
pending.extend(smart.albums_tested());
}
false
}
/// Source of [`Session::visible_shared`] generations (process-wide, so two sessions never share one).
static VISIBLE_GEN: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(1);
static LIBRARY_GEN: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(1);
@@ -207,12 +232,16 @@ pub struct Session {
pub catalog: Catalog,
pub source: LibrarySource,
pub filter: Filter,
/// The filter `library.toggleFilters` put aside (Library > Enable Filters off); not persisted.
pub(crate) shelved_filter: Option<Filter>,
pub sort: Sort,
pub selection: Selection,
/// The grid order for the current source/filter/sort (cached by catalog revision).
/// Shared, so frontends can hold it across frames without copying ([`Session::visible_shared`]).
visible: Arc<[PhotoId]>,
visible_key: Option<(u64, String)>,
/// The minute used for a cached view whose rules depend on the session clock.
visible_clock_minute: Option<String>,
/// A view holds the photos it shows until it is refreshed (`library.holdView`): an edit that makes a photo stop
/// matching a smart album's rules, Picks or the filter bar leaves it where it is. Off: the photo goes at once.
pub hold_view: bool,
@@ -232,7 +261,7 @@ pub struct Session {
/// is recomputed, so frontends can key their per-view caches on it instead of hashing the ids.
visible_gen: u64,
/// Photos in the current source with no filter on (cached like `visible`).
total: Option<((u64, String), usize)>,
total: Option<((u64, String), usize, Option<String>)>,
pub undo: Vec<UndoEntry>,
/// Undo steps committed in this session, ever (the history drops its oldest past 1000, so
/// its length can't tell how many steps a command just made; see [`Session::commits`]).
@@ -422,10 +451,12 @@ impl Session {
catalog: Catalog::new(),
source: LibrarySource::All,
filter: Filter::default(),
shelved_filter: None,
sort: Sort::default(),
selection: Selection::default(),
visible: Vec::new().into(),
visible_key: None,
visible_clock_minute: None,
hold_view: false,
base: None,
view_followed: 0,
@@ -1027,6 +1058,11 @@ impl Session {
// ---------------------------------------------------------------- library view
/// Library > Enable Filters is off: a filter is put aside (`library.toggleFilters`).
pub fn filters_off(&self) -> bool {
self.shelved_filter.is_some()
}
/// Photos shown in the grid/filmstrip for the current source, filter and sort.
pub fn visible(&mut self) -> &[PhotoId] {
let mut key =
@@ -1042,12 +1078,20 @@ impl Session {
};
let view = format!("{}|{rules}", key.1);
if self.source == LibrarySource::Missing && self.media.availability.is_background() {
// the view fills in as the background checks find files gone
key.1.push_str(&format!("|{}", self.media.availability.generation()));
// the view fills in as the background checks find files gone (a file found there at
// its first check changes nothing it shows)
key.1.push_str(&format!("|{}", self.media.availability.gone_generation()));
}
if self.visible_key.as_ref() != Some(&key) {
// "in the last N days" rules count back from the session's clock
lightcraft_catalog::rules::set_now(Some((self.clock)()));
let key_changed = self.visible_key.as_ref() != Some(&key);
// A time-dependent cache needs one clock read to test its minute stamp. A new view also
// needs one value for the query, even when its previous cached view did not use the clock.
let current_now = (key_changed || self.visible_clock_minute.is_some()).then(|| (self.clock)());
let current_minute = current_now.as_deref().map(clock_minute);
let clock_changed = self.visible_clock_minute.as_deref().is_some_and(|cached| current_minute != Some(cached));
if key_changed || clock_changed {
// Use this same value to evaluate relative rules and stamp the result.
let now = current_now.unwrap_or_else(|| (self.clock)());
lightcraft_catalog::rules::set_now(Some(now.clone()));
let f = self.view_filter(self.source, &self.filter);
// A held view keeps the photos it shows that stopped matching what is asked of them (a smart album's
// rules, a flag, the filter bar), as long as they are still where the view looks: in the library for
@@ -1074,8 +1118,7 @@ impl Session {
let whole = own_order || matches!(self.source, LibrarySource::RecentlyAdded | LibrarySource::Missing);
// A view that asks about "the last N days" (itself, or through a smart album it looks into) changes
// with the clock as well as with edits: a photo nobody touched can stop matching. It is asked whole.
let clocked = f.depends_on_now()
|| (!f.albums_tested().is_empty() && self.catalog.albums().any(|al| al.smart.as_deref().is_some_and(Filter::depends_on_now)));
let clocked = filter_depends_on_clock(&f, &self.catalog);
let base = self.base.take().filter(|(of, _, _)| *of == view);
// A small edit to the same view: the photos it touched are taken out, asked about again, and put back
// at their place in the order. The others neither stopped matching nor moved.
@@ -1149,6 +1192,7 @@ impl Session {
self.visible = visible.into();
self.visible_gen = VISIBLE_GEN.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
self.visible_key = Some(key);
self.visible_clock_minute = clocked.then(|| clock_minute(&now).to_owned());
}
&self.visible
}
@@ -1181,12 +1225,20 @@ impl Session {
/// view, so a list followed many times over is compared, not only one followed once.
#[cfg(test)]
pub(crate) fn visible_in_full(&mut self) -> Vec<PhotoId> {
let kept = (self.base.clone(), self.visible_key.clone(), self.visible.clone(), self.visible_gen, self.view_followed, self.view_worked_out);
let kept = (
self.base.clone(),
self.visible_key.clone(),
self.visible_clock_minute.clone(),
self.visible.clone(),
self.visible_gen,
self.view_followed,
self.view_worked_out,
);
self.never_follow = true;
self.visible_key = None;
let whole = self.visible().to_vec();
self.never_follow = false;
(self.base, self.visible_key, self.visible, self.visible_gen, self.view_followed, self.view_worked_out) = kept;
(self.base, self.visible_key, self.visible_clock_minute, self.visible, self.visible_gen, self.view_followed, self.view_worked_out) = kept;
whole
}
@@ -1214,7 +1266,15 @@ impl Session {
}
}
let key = (self.catalog.revision, format!("{:?}|{:?}|{:?}", self.source, self.browse, self.library_folder));
if self.total.as_ref().map(|t| &t.0) != Some(&key) {
let key_changed = self.total.as_ref().map(|total| &total.0) != Some(&key);
let cached_minute = self.total.as_ref().and_then(|total| total.2.as_deref());
let current_now = cached_minute.map(|_| (self.clock)());
let current_minute = current_now.as_deref().map(clock_minute);
let clock_changed = cached_minute.is_some_and(|cached| current_minute != Some(cached));
if key_changed || clock_changed {
// The source count can be requested before `visible`; bind it to the session's clock itself.
let now = current_now.unwrap_or_else(|| (self.clock)());
lightcraft_catalog::rules::set_now(Some(now.clone()));
let mut f = self.source.to_filter(&Filter::default(), &self.catalog);
if self.source == LibrarySource::Folder {
let b = self.browse.clone().unwrap_or_default();
@@ -1226,7 +1286,8 @@ impl Session {
}
// (how many, not which: nothing to put in order)
let n = self.catalog.count(&f);
self.total = Some((key, n));
let clocked = filter_depends_on_clock(&f, &self.catalog);
self.total = Some((key, n, clocked.then(|| clock_minute(&now).to_owned())));
}
self.total.as_ref().map(|t| t.1)
}
+1 -1
View File
@@ -62,7 +62,7 @@ impl SettingsHashes {
}
/// Bump when the pipeline's output changes, to invalidate cached thumbnails.
pub const RENDER_CACHE_VERSION: u64 = 69;
pub const RENDER_CACHE_VERSION: u64 = 70;
/// Thumbnails render at one of these long edges (so window/cell size changes reuse the cache).
pub const THUMB_SIZES: [usize; 4] = [128, 256, 384, 512];
+228 -9
View File
@@ -6,17 +6,102 @@
use std::collections::HashMap;
use std::path::{Path, PathBuf};
#[cfg(not(any(unix, windows)))]
use std::time::SystemTime;
use lightcraft_catalog::safe_file::same_file;
use lightcraft_catalog::{Catalog, Source};
/// The directory entry a screenshot is about to write, pinned at request admission.
///
/// A later write must not replace a path that was absent when the request was admitted (for
/// example a photo imported while the capture was waiting), nor a destination that was replaced by
/// another file in the meantime. [`Session::revalidate_write_target`] checks this immediately
/// before the write, and an absent destination is then published with create-new semantics.
#[derive(Clone, Debug)]
pub struct WriteTarget {
path: String,
state: WriteTargetState,
/// The canonical parent folder at admission, when it existed: a retargeted parent symlink is a
/// different destination even when the file name is unchanged.
parent: Option<PathBuf>,
}
#[derive(Clone, Debug)]
enum WriteTargetState {
/// The directory entry did not exist at admission.
Absent,
/// The directory entry existed; this is its identity (not its contents).
Existing(FileIdentity),
}
/// Stable identity of a directory entry, from `symlink_metadata` (so replacing a symlink or a
/// hard link is a different entry). Unix uses device + inode; Windows uses the canonical path,
/// length and creation time; other platforms use a canonical/len/mtime fallback.
#[derive(Clone, Debug, PartialEq, Eq)]
struct FileIdentity {
#[cfg(unix)]
dev: u64,
#[cfg(unix)]
ino: u64,
#[cfg(windows)]
canonical: PathBuf,
#[cfg(windows)]
len: u64,
#[cfg(windows)]
created: u64,
#[cfg(not(any(unix, windows)))]
canonical: PathBuf,
#[cfg(not(any(unix, windows)))]
len: u64,
#[cfg(not(any(unix, windows)))]
modified: Option<SystemTime>,
}
impl FileIdentity {
fn of(path: &Path, meta: &std::fs::Metadata) -> Self {
#[cfg(unix)]
{
use std::os::unix::fs::MetadataExt;
let _ = path;
Self { dev: meta.dev(), ino: meta.ino() }
}
#[cfg(windows)]
{
// The volume serial/file index metadata is still unstable; use stable metadata that
// changes when a different file replaces the entry, and keep the same in-place
// content edits (which do not change the creation time or length) overwritable.
use std::os::windows::fs::MetadataExt;
Self { canonical: std::fs::canonicalize(path).unwrap_or_else(|_| path.to_path_buf()), len: meta.len(), created: meta.creation_time() }
}
#[cfg(not(any(unix, windows)))]
{
Self { canonical: std::fs::canonicalize(path).unwrap_or_else(|_| path.to_path_buf()), len: meta.len(), modified: meta.modified().ok() }
}
}
}
impl WriteTarget {
/// The destination path as the request named it.
pub fn path(&self) -> &str {
&self.path
}
/// Was the directory entry absent at admission? Such a destination is published with
/// create-new semantics, never with the overwriting writer.
pub fn was_absent(&self) -> bool {
matches!(&self.state, WriteTargetState::Absent)
}
}
/// The library's originals and their sidecars, looked up by file name.
#[derive(Debug, Default)]
pub struct OriginalGuard {
/// lower-case file name → (path on disk, what it is: "the original of IMG_1.CR3", …)
by_name: HashMap<String, Vec<(PathBuf, String)>>,
/// The entries of `by_name` that are symlinks, found on the first check that needs them.
links: std::sync::OnceLock<Vec<(PathBuf, String)>>,
/// Every indexed entry, built on the first check whose target name did not match: a hard
/// link or symlink may name an original differently from the catalogued path.
others: std::sync::OnceLock<Vec<(PathBuf, String)>>,
}
impl OriginalGuard {
@@ -34,7 +119,7 @@ impl OriginalGuard {
/// Protect `path` (an original named `label`) and its sidecars (`IMG_1.xmp`, `IMG_1.CR3.xmp`).
pub fn add(&mut self, path: &Path, label: &str) {
let Some(name) = path.file_name().map(|n| n.to_string_lossy().to_string()) else { return };
self.links = std::sync::OnceLock::new();
self.others = std::sync::OnceLock::new();
let mut put = |file: String, p: PathBuf, what: String| self.by_name.entry(file.to_lowercase()).or_default().push((p, what));
put(name.clone(), path.to_path_buf(), format!("the original of {label}"));
let stem = path.file_stem().map(|s| s.to_string_lossy().to_string()).unwrap_or_default();
@@ -44,6 +129,17 @@ impl OriginalGuard {
/// `Err` (a message for the user) when writing `target` would replace a protected file.
pub fn check(&self, target: &Path) -> Result<(), String> {
self.check_target(target, false)
}
/// [`Self::check`] plus hard links: a screenshot target must not replace an original reached
/// through a link with a different name. The per-entry identity scan is limited to this
/// long-lived, user-visible destination check, not every ordinary export overwrite.
pub fn check_identity(&self, target: &Path) -> Result<(), String> {
self.check_target(target, true)
}
fn check_target(&self, target: &Path, hard_links: bool) -> Result<(), String> {
if std::fs::symlink_metadata(target).is_err() {
return Ok(()); // nothing there to replace
}
@@ -61,12 +157,15 @@ impl OriginalGuard {
}
}
}
// An imported symlink can have a different name from its original. A write to
// that original would change what the library reads through the link. Only do
// this fallback for existing targets that the name index did not protect; the links are
// looked up once per guard, not once per target.
let links = self.links.get_or_init(|| self.by_name.values().flatten().filter(|(p, _)| std::fs::read_link(p).is_ok()).cloned().collect());
for (p, what) in links {
// An imported hard link or symlink can have a different name from its original. A write
// to that entry would change what the library reads. Only do this fallback for existing
// targets that the name index did not protect; the list is built once per guard, not once
// per target. Ordinary exports keep the cheaper symlink-only scan.
let others = self.others.get_or_init(|| self.by_name.values().flatten().cloned().collect());
for (p, what) in others {
if !hard_links && std::fs::read_link(p).is_err() {
continue;
}
if same_file(p, target) {
return Err(format!(
"{} is {what} in the library: LightCraft never writes over an original (choose another folder or file name)",
@@ -93,6 +192,69 @@ impl crate::Session {
}
self.original_guard().check(path)
}
/// Pin the destination of a long-running write (a screenshot) now.
///
/// The returned [`WriteTarget`] records whether the directory entry existed and, if so, its
/// identity, plus the canonical parent folder. Pass it to [`Self::revalidate_write_target`]
/// immediately before writing.
pub fn pin_write_target(&self, path: &str) -> Result<WriteTarget, String> {
let p = Path::new(path);
let parent = p.parent().filter(|d| !d.as_os_str().is_empty()).and_then(|d| std::fs::canonicalize(d).ok());
match std::fs::symlink_metadata(p) {
Err(error) if error.kind() == std::io::ErrorKind::NotFound => {
Ok(WriteTarget { path: path.to_string(), state: WriteTargetState::Absent, parent })
}
Err(error) => Err(format!("{path}: {error}")),
Ok(meta) => {
// Refuse a catalogued original or sidecar (including a differently named hard
// link) before the request is queued.
self.original_guard().check_identity(p)?;
Ok(WriteTarget { path: path.to_string(), state: WriteTargetState::Existing(FileIdentity::of(p, &meta)), parent })
}
}
}
/// Re-check a pinned target immediately before writing.
///
/// An absent target must still be absent; an existing target must still name the same
/// directory entry and must still pass the current catalog's original guard. A parent folder
/// whose canonical path changed (a retargeted symlink) is refused too.
pub fn revalidate_write_target(&self, target: &WriteTarget) -> Result<(), String> {
if let Some(parent) = &target.parent {
// Resolve the request's parent again: canonicalizing the saved resolved folder
// would miss a directory symlink in the original path being retargeted.
let now = Path::new(&target.path).parent().and_then(|path| std::fs::canonicalize(path).ok());
match now {
Some(now) if now == *parent => {}
_ => {
return Err(format!(
"{}: the destination folder changed while the screenshot was being captured; refusing to write",
target.path
));
}
}
}
match std::fs::symlink_metadata(&target.path) {
Err(error) if error.kind() == std::io::ErrorKind::NotFound => match &target.state {
WriteTargetState::Absent => Ok(()),
WriteTargetState::Existing(_) => {
Err(format!("{}: the file was removed while the screenshot was being captured; refusing to create it again", target.path))
}
},
Err(error) => Err(format!("{}: {error}", target.path)),
Ok(meta) => match &target.state {
WriteTargetState::Absent => Err(format!("{} appeared while the screenshot was being captured; refusing to replace it", target.path)),
WriteTargetState::Existing(identity) => {
if FileIdentity::of(Path::new(&target.path), &meta) != *identity {
return Err(format!("{} changed while the screenshot was being captured; refusing to replace the new file", target.path));
}
// The catalog may have gained this file as an original since admission.
self.original_guard().check_identity(Path::new(&target.path))
}
},
}
}
}
#[cfg(test)]
@@ -120,4 +282,61 @@ mod tests {
assert!(g.check(&d.join("new.jpg")).is_ok(), "a free name");
let _ = std::fs::remove_dir_all(&d);
}
#[test]
fn write_targets_pin_absent_paths_and_recheck_the_current_library() {
let d = std::env::temp_dir().join(format!("lc-write-targets-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&d);
std::fs::create_dir_all(&d).unwrap();
let mut s = crate::Session::new();
let original = d.join("IMG_1.jpg");
std::fs::write(&original, b"original").unwrap();
let photo = Photo::new(PhotoId(1), Source::File { path: original.to_string_lossy().to_string() }, "IMG_1.jpg", "JPEG", 10, 10, "2026-01-01");
s.catalog.apply(Op::AddPhoto { photo: Box::new(photo) }).unwrap();
// A path that was absent at admission is refused when anything appears there.
let free = d.join("new-shot.png");
let absent = s.pin_write_target(&free.to_string_lossy()).expect("free path");
assert!(absent.was_absent());
std::fs::write(&free, b"appeared").unwrap();
assert!(s.revalidate_write_target(&absent).unwrap_err().contains("appeared"));
std::fs::remove_file(&free).unwrap();
// An ordinary existing path can still be overwritten in place (same entry).
let allowed = d.join("old-shot.png");
std::fs::write(&allowed, b"old").unwrap();
let existing = s.pin_write_target(&allowed.to_string_lossy()).expect("ordinary file");
assert!(!existing.was_absent());
std::fs::write(&allowed, b"new").unwrap();
assert!(s.revalidate_write_target(&existing).is_ok(), "an allowed screenshot refresh keeps working");
// A hard link to an original is refused at admission, and an absent path that becomes
// such a link is refused at write time.
let linked = d.join("linked.png");
std::fs::hard_link(&original, &linked).unwrap();
assert!(s.pin_write_target(&linked.to_string_lossy()).unwrap_err().contains("original"));
std::fs::remove_file(&linked).unwrap();
let appears_as_link = d.join("late-link.png");
let late = s.pin_write_target(&appears_as_link.to_string_lossy()).expect("free path");
std::fs::hard_link(&original, &appears_as_link).unwrap();
assert!(s.revalidate_write_target(&late).unwrap_err().contains("appeared"));
// A path pinned as an ordinary file becomes an original through a catalog import while
// the capture is queued: the current guard refuses the write.
let imported = d.join("imported.png");
std::fs::write(&imported, b"not yet imported").unwrap();
let queued = s.pin_write_target(&imported.to_string_lossy()).expect("ordinary file");
let p = Photo::new(PhotoId(2), Source::File { path: imported.to_string_lossy().to_string() }, "imported.png", "PNG", 10, 10, "2026-01-01");
s.catalog.apply(Op::AddPhoto { photo: Box::new(p) }).unwrap();
assert!(s.revalidate_write_target(&queued).unwrap_err().contains("original"));
#[cfg(unix)]
{
use std::os::unix::fs::symlink;
let link = d.join("link-shot.png");
symlink(&original, &link).unwrap();
assert!(s.pin_write_target(&link.to_string_lossy()).unwrap_err().contains("original"));
}
let _ = std::fs::remove_dir_all(&d);
}
}
+126 -3
View File
@@ -46,6 +46,41 @@ const PAPER: &[(&str, f64, f64)] = &[
("5x7", 5.0, 7.0),
("8x10", 8.0, 10.0),
("13x19", 13.0, 19.0),
// Japanese photo printing and postcard sizes, at their exact metric dimensions (the US photo
// sizes above are close but not identical: L is 89 x 127 mm, 5x7 in is 127 x 177.8 mm).
("l", 89.0 / 25.4, 127.0 / 25.4),
("2l", 127.0 / 25.4, 178.0 / 25.4),
("kg", 102.0 / 25.4, 152.0 / 25.4),
("postcard", 100.0 / 25.4, 148.0 / 25.4),
("double-postcard", 148.0 / 25.4, 200.0 / 25.4),
("6cut", 203.0 / 25.4, 254.0 / 25.4),
("4cut", 254.0 / 25.4, 305.0 / 25.4),
("business-card", 55.0 / 25.4, 91.0 / 25.4),
];
/// Japanese customary names (and a few English spellings) for the papers in [`PAPER`], matched
/// after [`normalize_paper`]: `L判` is `l`, `はがき` is `postcard`, `六切` is `6cut`, and so on.
const PAPER_ALIASES: &[(&str, &str)] = &[
("L判", "l"),
("Lサイズ", "l"),
("L-size", "l"),
("2L判", "2l"),
("2Lサイズ", "2l"),
("2L-size", "2l"),
("KG判", "kg"),
("KGサイズ", "kg"),
("KG-size", "kg"),
("はがき", "postcard"),
("ハガキ", "postcard"),
("葉書", "postcard"),
("ポストカード", "postcard"),
("往復はがき", "double-postcard"),
("往復ハガキ", "double-postcard"),
("六切", "6cut"),
("6切", "6cut"),
("四切", "4cut"),
("4切", "4cut"),
("名刺", "business-card"),
];
/// Names accepted by [`PageSetup::paper`] (besides `custom`).
@@ -53,10 +88,39 @@ pub fn paper_names() -> impl Iterator<Item = &'static str> {
PAPER.iter().map(|p| p.0)
}
/// Portrait size in inches of a named paper (case-insensitive), `None` for unknown names and `custom`.
/// The form [`paper_name`] matches names in: lower case, letters and digits only, and `×`/`*` read
/// as `x`, so `L判`, `L判。`, `l-size` and `(l)` are one name and `4 × 6` is `4x6`.
fn normalize_paper(name: &str) -> String {
name.chars()
.filter_map(|c| match c {
'×' | '*' => Some('x'),
c if c.is_alphanumeric() => Some(c),
_ => None,
})
.collect::<String>()
.to_lowercase()
}
/// The canonical name of the paper `name` stands for, `None` for unknown names and `custom`.
/// `L判` is `l`, `はがき` is `postcard`, `六切` is `6cut`, and so on.
pub fn paper_name(name: &str) -> Option<&'static str> {
let n = normalize_paper(name);
if n.is_empty() {
return None;
}
PAPER
.iter()
.find(|p| normalize_paper(p.0) == n)
.map(|p| p.0)
.or_else(|| PAPER_ALIASES.iter().find(|(alias, _)| normalize_paper(alias) == n).map(|(_, name)| *name))
}
/// Portrait size in inches of a named paper, `None` for unknown names and `custom`. Case, spaces
/// and punctuation don't matter, and the Japanese customary names (`L判`, `2L判`, `KG判`, `はがき`,
/// `往復はがき`, `六切`, `四切`, `名刺`) are aliases of the papers above.
pub fn paper_size(name: &str) -> Option<(f64, f64)> {
let name = name.to_ascii_lowercase();
PAPER.iter().find(|p| p.0 == name).map(|p| (p.1, p.2))
let canonical = paper_name(name)?;
PAPER.iter().find(|p| p.0 == canonical).map(|p| (p.1, p.2))
}
/// An axis-aligned rectangle in inches.
@@ -935,6 +999,65 @@ mod tests {
assert!(p.size().is_err());
}
/// Japanese photo and postcard papers: exact metric sizes to 0.1 pt, the customary names as
/// aliases, and the ISO/US papers around them unchanged.
#[test]
fn japanese_photo_and_postcard_papers() {
let pt = |name: &str| {
let p = PageSetup { paper: name.into(), ..Default::default() };
p.size_points().unwrap_or_else(|e| panic!("{name}: {e}"))
};
for (name, w_mm, h_mm) in [
("L判", 89.0, 127.0),
("2L判", 127.0, 178.0),
("KG判", 102.0, 152.0),
("はがき", 100.0, 148.0),
("往復はがき", 148.0, 200.0),
("六切", 203.0, 254.0),
("四切", 254.0, 305.0),
("名刺", 55.0, 91.0),
] {
let (w, h) = pt(name);
let want = (w_mm / 25.4 * 72.0, h_mm / 25.4 * 72.0);
assert!((w - want.0).abs() < 0.1 && (h - want.1).abs() < 0.1, "{name}: {w} x {h} pt, wanted {want:?}");
assert!((w - h).abs() > 1e-6, "{name} is portrait");
}
// The customary and English spellings resolve to the same paper, including spaces and case.
for (a, b) in [
("L判", "l"),
("L判", " L "),
("L判", "L-size"),
("2L判", "2l"),
("KG判", "kg"),
("はがき", "postcard"),
("はがき", "ハガキ"),
("往復はがき", "double-postcard"),
("六切", "6cut"),
("六切", "6切"),
("四切", "4cut"),
("名刺", "business-card"),
("A4", "a 4"),
("4 × 6", "4x6"),
] {
assert_eq!(paper_size(a), paper_size(b), "{a} vs {b}");
}
// Punctuation is ignored, including the periods and Japanese marks a pasted name may carry.
for (name, canonical) in [("L判。", "l"), ("L判、", "l"), ("(L判)", "l"), ("l-size.", "l"), ("はがき。", "postcard"), ("6切。", "6cut")]
{
assert_eq!(paper_name(name), Some(canonical), "{name}");
}
assert_eq!(paper_name("custom"), None);
assert!(paper_size("poster").is_none());
// The papers that were already there keep their sizes.
assert_eq!(pt("letter"), (612.0, 792.0));
assert_eq!(pt("4x6"), (288.0, 432.0));
assert_eq!(pt("5x7"), (360.0, 504.0));
assert_eq!(pt("8x10"), (576.0, 720.0));
assert_eq!(pt("13x19"), (936.0, 1368.0));
let (w, h) = pt("A4");
assert!((w - 595.28).abs() < 0.01 && (h - 841.89).abs() < 0.01, "A4 {w} x {h}");
}
#[test]
fn hostile_numbers_are_rejected() {
for bad in [f64::NAN, f64::INFINITY, f64::NEG_INFINITY, -1.0, 1e9] {
+115
View File
@@ -30,6 +30,14 @@ impl PrintSettings {
pub const MAX_NAME: usize = 80;
/// Most templates kept.
pub const MAX_TEMPLATES: usize = 200;
/// File extension of an exported print template (JSON).
pub const FILE_EXT: &str = "lcprint";
/// `format` tag of an exported print template file.
const FILE_FORMAT: &str = "lightcraft.printTemplate";
/// Version of the template file this build writes and the newest it reads.
const FILE_VERSION: u64 = 1;
/// Largest template file read, bytes.
pub const MAX_FILE: u64 = 256 * 1024;
/// A saved print template: the layout, image settings, page and job of a print, without the
/// photos assigned to cells and without the guides (a view preference). The settings are kept as
@@ -74,6 +82,53 @@ impl PrintTemplate {
s.validate().map_err(|e| format!("template `{}`: {e}", self.name))?;
Ok(s)
}
/// The document of an exported template: `{format, version, name, settings}`.
pub fn to_file(&self) -> serde_json::Value {
serde_json::json!({"format": FILE_FORMAT, "version": FILE_VERSION, "name": self.name, "settings": self.settings})
}
/// A template read from an exported file. Refused: more than [`MAX_FILE`] bytes, not JSON,
/// another format or a newer version, a bad name, and settings that fail the checks a saved
/// template's settings get. Photos assigned to cells and guides in the file are dropped.
pub fn from_file(bytes: &[u8]) -> Result<Self, String> {
if bytes.len() as u64 > MAX_FILE {
return Err(format!("{} bytes is too large for a print template (at most {} KB)", bytes.len(), MAX_FILE / 1024));
}
let v: serde_json::Value = serde_json::from_slice(bytes).map_err(|e| format!("not a print template file: {e}"))?;
if v.get("format").and_then(serde_json::Value::as_str) != Some(FILE_FORMAT) {
return Err(format!("not a print template file (`format` is not \"{FILE_FORMAT}\")"));
}
match v.get("version").and_then(serde_json::Value::as_u64) {
Some(1..=FILE_VERSION) => {}
Some(n) => return Err(format!("print template file version {n} is not one this version of LightCraft reads (1 to {FILE_VERSION})")),
None => return Err("a print template file needs a `version` number".into()),
}
let name = clean_name(v.get("name").and_then(serde_json::Value::as_str).ok_or("a print template file needs a `name`")?)?;
let settings = v.get("settings").cloned().ok_or("a print template file needs `settings`")?;
let checked = PrintTemplate { name: name.clone(), settings }.settings(&Guides::default())?;
Self::new(&name, &checked)
}
}
/// `name` if no template in `taken` has it (in any case), else the first free `name (2)`,
/// `name (3)`…, with `name` shortened to keep the result within [`MAX_NAME`] characters.
pub fn unique_name(name: &str, taken: &[PrintTemplate]) -> String {
let free = |n: &str| !taken.iter().any(|t| t.name.eq_ignore_ascii_case(n));
if free(name) {
return name.to_string();
}
let mut n = 2usize;
loop {
let suffix = format!(" ({n})");
let keep = MAX_NAME.saturating_sub(suffix.chars().count());
let base: String = name.chars().take(keep).collect();
let candidate = format!("{}{suffix}", base.trim_end());
if free(&candidate) || n > taken.len() {
return candidate;
}
n += 1;
}
}
#[cfg(test)]
@@ -121,4 +176,64 @@ mod tests {
s.job.ppi = f32::NAN;
assert!(PrintTemplate::new("t", &s).is_err());
}
#[test]
fn a_template_file_reads_back_as_the_template() {
let mut s = PrintSettings::default();
s.layout.style = Style::ContactSheet;
s.page.background = [1, 2, 3];
let t = PrintTemplate::new("Proofs", &s).unwrap();
let bytes = serde_json::to_vec(&t.to_file()).unwrap();
assert_eq!(PrintTemplate::from_file(&bytes).unwrap(), t);
// a hand-written file: photos and guides are not taken in
let doc = json!({"format": FILE_FORMAT, "version": 1, "name": " Hand ", "settings": {
"layout": {"style": "customPackage", "cells": [{"id": 1, "rect": {"x": 1, "y": 1, "w": 2, "h": 2}, "photo": 9}]},
"guides": {"gridSize": 1.0}}});
let t = PrintTemplate::from_file(&serde_json::to_vec(&doc).unwrap()).unwrap();
assert_eq!(t.name, "Hand");
assert!(t.settings.get("guides").is_none());
assert!(t.settings.pointer("/layout/cells/0/rect").is_some());
assert_eq!(t.settings.pointer("/layout/cells/0/photo"), None);
}
#[test]
fn hostile_template_files_are_refused() {
let file = |v: serde_json::Value| PrintTemplate::from_file(&serde_json::to_vec(&v).unwrap()).unwrap_err();
let ok = |extra: serde_json::Value| {
let mut v = json!({"format": FILE_FORMAT, "version": 1, "name": "t", "settings": {}});
if let (Some(o), Some(e)) = (v.as_object_mut(), extra.as_object()) {
o.extend(e.clone());
}
v
};
assert!(PrintTemplate::from_file(b"not json").unwrap_err().contains("not a print template file"));
assert!(PrintTemplate::from_file(&vec![b' '; MAX_FILE as usize + 1]).unwrap_err().contains("too large"));
assert!(file(json!([1, 2])).contains("format"));
assert!(file(ok(json!({"format": "lightcraft.slideshowTemplate"}))).contains("format"));
assert!(file(ok(json!({"version": 2}))).contains("version 2"));
assert!(file(ok(json!({"version": 0}))).contains("version 0"));
assert!(file(ok(json!({"version": "1"}))).contains("`version` number"));
assert!(file(json!({"format": FILE_FORMAT, "name": "t", "settings": {}})).contains("`version` number"));
assert!(file(ok(json!({"name": "a\nb"}))).contains("template name"));
assert!(file(ok(json!({"name": 5}))).contains("`name`"));
assert!(file(json!({"format": FILE_FORMAT, "version": 1, "name": "t"})).contains("`settings`"));
assert!(file(ok(json!({"settings": {"job": {"ppi": 1e30}}}))).contains("template `t`"));
assert!(file(ok(json!({"settings": {"surprise": 1}}))).contains("template `t`"));
assert!(PrintTemplate::from_file(&serde_json::to_vec(&ok(json!({}))).unwrap()).is_ok());
}
#[test]
fn an_imported_name_never_takes_an_existing_one() {
let t = |n: &str| PrintTemplate::new(n, &PrintSettings::default()).unwrap();
assert_eq!(unique_name("Proofs", &[]), "Proofs");
assert_eq!(unique_name("Proofs", &[t("proofs")]), "Proofs (2)");
assert_eq!(unique_name("Proofs", &[t("Proofs"), t("PROOFS (2)")]), "Proofs (3)");
let long = "x".repeat(MAX_NAME);
let named = unique_name(&long, &[t(&long)]);
assert_eq!(named.chars().count(), MAX_NAME);
assert!(named.ends_with(" (2)"));
let many: Vec<PrintTemplate> = std::iter::once(t("A")).chain((2..=MAX_TEMPLATES).map(|n| t(&format!("A ({n})")))).collect();
let named = unique_name("A", &many);
assert!(!many.iter().any(|x| x.name.eq_ignore_ascii_case(&named)), "{named}");
}
}
+18 -2
View File
@@ -4,7 +4,7 @@
//! Template tokens (shared with import renaming and export file naming): `{name}` (current name
//! without extension), `{num}` (the number at the end of the name: `IMG_0042` → `0042`), `{seq}` /
//! `{seq:3}` (sequence number, zero-padded to 3 digits), `{date}` / `{date:%Y%m%d}` (capture date;
//! `%Y %y %m %d %H %M %S`), `{folder}` (the original's folder name), `{camera}`, `{lens}`, `{iso}`,
//! `%Y %y %m %B %b %d %H %M %S`), `{folder}` (the original's folder name), `{camera}`, `{lens}`, `{iso}`,
//! `{rating}`, `{title}`, `{creator}`, `{ext}` (extension, without the dot). Unknown tokens stay as
//! typed. Characters that aren't allowed in file names become `-`. The original extension is always
//! kept.
@@ -25,6 +25,7 @@
use std::collections::{HashMap, HashSet};
use std::path::{Path, PathBuf};
use lightcraft_catalog::dates::MONTHS;
use lightcraft_catalog::{Op, Photo, PhotoId, Source};
use serde::Serialize;
@@ -57,9 +58,12 @@ fn sanitize(s: &str) -> String {
t.trim().trim_matches('.').trim().to_string()
}
/// `%Y%m%d`-style formatting of an ISO local time.
/// `%Y%m%d`-style formatting of an ISO local time. `%B` / `%b` are the English month name
/// (`January` / `Jan`), the same in every interface language so folder names stay put; a month
/// that doesn't parse gives nothing.
fn format_date(iso: &str, fmt: &str) -> String {
let part = |a: usize, b: usize| iso.get(a..b).unwrap_or("00").to_string();
let month = iso.get(5..7).and_then(|m| m.parse::<usize>().ok()).and_then(|m| m.checked_sub(1)).and_then(|i| MONTHS.get(i)).copied().unwrap_or("");
let mut out = String::new();
let mut it = fmt.chars();
while let Some(c) = it.next() {
@@ -71,6 +75,8 @@ fn format_date(iso: &str, fmt: &str) -> String {
Some('Y') => out.push_str(&part(0, 4)),
Some('y') => out.push_str(&part(2, 4)),
Some('m') => out.push_str(&part(5, 7)),
Some('B') => out.push_str(month),
Some('b') => out.push_str(month.get(..3).unwrap_or(month)),
Some('d') => out.push_str(&part(8, 10)),
Some('H') => out.push_str(&part(11, 13)),
Some('M') => out.push_str(&part(14, 16)),
@@ -120,6 +126,8 @@ pub const DATE_DIRECTIVES: &[(&str, &str)] = &[
("%Y", "year, 4 digits"),
("%y", "year, 2 digits"),
("%m", "month 01–12"),
("%B", "month name (January)"),
("%b", "short month name (Jan)"),
("%d", "day 01–31"),
("%H", "hour 00–23"),
("%M", "minute"),
@@ -1670,6 +1678,8 @@ pub(crate) mod tests {
let mut p = sample_photo();
assert_eq!(expand_folder("{date:%Y}/{date:%Y%m%d}", &p, 1), vec!["2026", "20260114"]);
assert_eq!(expand_folder("{date:%Y}\\{date:%Y-%m}", &p, 1), vec!["2026", "2026-01"], "backslash separates too");
assert_eq!(expand_folder("{date:%Y}/{date:%B}", &p, 1), vec!["2026", "January"]);
assert_eq!(expand_folder("{date:%Y}/{date:%m %b}", &p, 1), vec!["2026", "01 Jan"]);
// metadata can't add levels or climb out
p.meta.camera = "../../etc/x".into();
p.meta.title = "..".into();
@@ -1711,6 +1721,12 @@ pub(crate) mod tests {
let v = format_date("2026-01-14T05:58:48", d);
assert!(!v.contains('%') || *d == "%%", "{d} is not a known directive");
}
assert_eq!(format_date("2026-09-30T14:05:00", "%B %b"), "September Sep");
assert_eq!(format_date("2026-12-01T00:00:00", "%d %B %Y"), "01 December 2026");
// a month that isn't 01–12 gives no name instead of panicking
for bad in ["2026-00-01", "2026-13-01", "2026-x", "", "2026-é1-01"] {
assert_eq!(format_date(bad, "%B|%b"), "|", "{bad:?}");
}
assert_eq!(unknown_tokens("{date}_{camra}-{seq:2}{x}{camra}"), vec!["{camra}".to_string(), "{x}".to_string()]);
assert!(unknown_tokens("{name}{ext}{date:%Y}").is_empty());
let json = token_help_json();
Loaded 100 of 430 files, more files were not shown because too many files have changed in this diff. Show more