diff --git a/.github/workflows/freebsd.yml b/.github/workflows/freebsd.yml index c63fdeef5..b10e65a75 100644 --- a/.github/workflows/freebsd.yml +++ b/.github/workflows/freebsd.yml @@ -31,7 +31,7 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: repository: storytold/craft-fonts - ref: 8dcdacd5153e64560d109541a47d806f26f048c0 + ref: 3d9ab3a132ed0eff457e424226a435a901da30d3 path: craft-fonts - name: Build and test in a FreeBSD VM uses: vmactions/freebsd-vm@c46abacb49f09938ca4e1702d15d836285d694cc # v1.5.9 diff --git a/.github/workflows/nix.yml b/.github/workflows/nix.yml new file mode 100644 index 000000000..d992f7433 --- /dev/null +++ b/.github/workflows/nix.yml @@ -0,0 +1,28 @@ +# Evaluates the Nix flake (no build, about a minute): `nix build` / `nix develop` stop at evaluation +# when Cargo.lock gains something nix/package.nix can't vendor, and nothing else would notice. +name: Nix + +on: + push: + branches: [main] + paths: ["flake.nix", "flake.lock", "nix/**", "Cargo.lock", ".github/workflows/nix.yml"] + pull_request: + paths: ["flake.nix", "flake.lock", "nix/**", "Cargo.lock", ".github/workflows/nix.yml"] + workflow_dispatch: + +permissions: + contents: read + +concurrency: + group: nix-${{ github.ref }} + cancel-in-progress: true + +jobs: + eval: + runs-on: ubuntu-latest + timeout-minutes: 15 + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - uses: cachix/install-nix-action@13d8dd58da0234aa297dedd986986ccb8e7f3e24 # v31.11.1 + - name: nix flake check --no-build + run: nix flake check --no-build --all-systems diff --git a/.github/workflows/packaging-lint.yml b/.github/workflows/packaging-lint.yml index 8953710ce..7ec855992 100644 --- a/.github/workflows/packaging-lint.yml +++ b/.github/workflows/packaging-lint.yml @@ -21,6 +21,7 @@ concurrency: jobs: lint: + timeout-minutes: 15 runs-on: ubuntu-latest env: ACTIONLINT_VERSION: 1.7.12 diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index b7e3036d6..392ee4eb7 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -37,6 +37,7 @@ env: jobs: version: + timeout-minutes: 10 name: version runs-on: ubuntu-latest # No `environment: release` here or on the other jobs that sign nothing: no release secrets. @@ -65,6 +66,7 @@ jobs: echo "### LightCraft v$v (\`${GITHUB_SHA::9}\`)" >>"$GITHUB_STEP_SUMMARY" macos: + timeout-minutes: 90 name: macOS universal needs: version runs-on: macos-15-xlarge # M2 Pro larger runner; use only in release.yml (actions_macos_xl budget caps spend) @@ -78,7 +80,7 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: repository: storytold/craft-fonts - ref: 8dcdacd5153e64560d109541a47d806f26f048c0 # bump deliberately + ref: 3d9ab3a132ed0eff457e424226a435a901da30d3 # bump deliberately path: craft-fonts - uses: dtolnay/rust-toolchain@686976e191b89faba57d3206551f0f330d8cb249 # stable with: @@ -114,6 +116,7 @@ jobs: if [ -n "${MACOS_KEYCHAIN:-}" ]; then security delete-keychain "$MACOS_KEYCHAIN" || true; fi windows: + timeout-minutes: 90 name: Windows ${{ matrix.arch }} needs: version runs-on: windows-latest @@ -136,7 +139,7 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: repository: storytold/craft-fonts - ref: 8dcdacd5153e64560d109541a47d806f26f048c0 # bump deliberately + ref: 3d9ab3a132ed0eff457e424226a435a901da30d3 # bump deliberately path: craft-fonts - uses: dtolnay/rust-toolchain@686976e191b89faba57d3206551f0f330d8cb249 # stable with: @@ -173,6 +176,7 @@ jobs: if-no-files-found: error linux: + timeout-minutes: 90 name: Linux ${{ matrix.arch }} needs: version # The oldest GitHub-hosted image, so the binaries only need glibc >= 2.35 (Ubuntu 22.04+, @@ -194,7 +198,7 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: repository: storytold/craft-fonts - ref: 8dcdacd5153e64560d109541a47d806f26f048c0 # bump deliberately + ref: 3d9ab3a132ed0eff457e424226a435a901da30d3 # bump deliberately path: craft-fonts - name: Install build and packaging tools run: | @@ -237,6 +241,7 @@ jobs: if-no-files-found: error flatpak: + timeout-minutes: 90 name: Flatpak ${{ matrix.arch }} needs: [version, linux] # Repackages the Linux job's tarball (the same binaries as the AppImage/deb/rpm); no Rust @@ -274,6 +279,7 @@ jobs: if-no-files-found: error linux-riscv64: + timeout-minutes: 90 name: Linux riscv64 needs: version # Cross-compiled on x86_64 (no hosted RISC-V runners) against the runner's own Ubuntu release @@ -291,7 +297,7 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: repository: storytold/craft-fonts - ref: 8dcdacd5153e64560d109541a47d806f26f048c0 # bump deliberately + ref: 3d9ab3a132ed0eff457e424226a435a901da30d3 # bump deliberately path: craft-fonts - uses: dtolnay/rust-toolchain@686976e191b89faba57d3206551f0f330d8cb249 # stable with: @@ -362,7 +368,7 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: repository: storytold/craft-fonts - ref: 8dcdacd5153e64560d109541a47d806f26f048c0 # bump deliberately + ref: 3d9ab3a132ed0eff457e424226a435a901da30d3 # bump deliberately path: craft-fonts - name: Build and package in a FreeBSD VM (tar.gz) # vmactions/freebsd-vm v1.5.9, pinned by commit (it runs our build). @@ -398,6 +404,7 @@ jobs: if-no-files-found: error web: + timeout-minutes: 90 name: Web (wasm) needs: version runs-on: ubuntu-latest @@ -411,7 +418,7 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: repository: storytold/craft-fonts - ref: 8dcdacd5153e64560d109541a47d806f26f048c0 # bump deliberately + ref: 3d9ab3a132ed0eff457e424226a435a901da30d3 # bump deliberately path: craft-fonts - uses: dtolnay/rust-toolchain@686976e191b89faba57d3206551f0f330d8cb249 # stable with: @@ -441,6 +448,7 @@ jobs: if-no-files-found: error release: + timeout-minutes: 10 name: Draft GitHub Release needs: [version, macos, windows, linux, linux-riscv64, flatpak, freebsd, web] runs-on: ubuntu-latest diff --git a/AGENTS.md b/AGENTS.md index 8cd199ff6..297682755 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -131,8 +131,8 @@ of it. Full standard: `../craftrules/standards/never-crash.md` → `ui-egui`, `mcp` (L5) → apps `lightcraft` (desktop), `lightcraft-cli` (render/commands/MCP). `scenes` generates demo photos. `xtask` = tooling (`ci`, `layers`, `assets`, `parity`, `wasm`, `corpus`, `stats`). `flake.nix` + `nix/package.nix` = the Nix package (`nix build` builds both binaries with the craft-fonts input, installs the -desktop file/icons/AppStream metadata and runs `cargo test --workspace`; `nix develop` = dev shell). Community-maintained and not -in CI: it may lag behind the workspace; see README → Quick start. +desktop file/icons/AppStream metadata and runs `cargo test --workspace`; `nix develop` = dev shell). Community-maintained; CI only +evaluates it (`.github/workflows/nix.yml`), so the build may lag behind the workspace; see README → Quick start. ## Contributor credits (About window) diff --git a/CLAUDE.md b/CLAUDE.md index 136e821bb..5b4702cdb 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -139,5 +139,5 @@ of it. Full standard: `../craftrules/standards/never-crash.md` → `ui-egui`, `mcp` (L5) → apps `lightcraft` (desktop), `lightcraft-cli` (render/commands/MCP). `scenes` generates demo photos. `xtask` = tooling (`ci`, `layers`, `assets`, `parity`, `wasm`, `corpus`, `stats`). `flake.nix` + `nix/package.nix` = the Nix package (`nix build` builds both binaries with the craft-fonts input, installs the -desktop file/icons/AppStream metadata and runs `cargo test --workspace`; `nix develop` = dev shell). Community-maintained and not -in CI: it may lag behind the workspace; see README → Quick start. +desktop file/icons/AppStream metadata and runs `cargo test --workspace`; `nix develop` = dev shell). Community-maintained; CI only +evaluates it (`.github/workflows/nix.yml`), so the build may lag behind the workspace; see README → Quick start. diff --git a/Cargo.lock b/Cargo.lock index 5f445192f..db6efd50e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3239,6 +3239,7 @@ dependencies = [ "lightcraft-raw", "lightcraft-scenes", "lightcraft-segment", + "lightcraft-slideshow", "lightcraft-tiff", "log", "miniz_oxide 0.8.9", @@ -3460,6 +3461,15 @@ dependencies = [ "thiserror 2.0.21", ] +[[package]] +name = "lightcraft-slideshow" +version = "0.8.0" +dependencies = [ + "lightcraft-raster", + "serde", + "serde_json", +] + [[package]] name = "lightcraft-sysmem" version = "0.8.0" @@ -3493,6 +3503,7 @@ dependencies = [ "lightcraft-pipeline", "lightcraft-preview", "lightcraft-raster", + "lightcraft-slideshow", "log", "serde", "serde_json", diff --git a/Cargo.toml b/Cargo.toml index 2b89df35e..de985d654 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -39,6 +39,7 @@ lightcraft-fetch = { path = "crates/fetch" } lightcraft-heif = { path = "crates/heif" } lightcraft-segment = { path = "crates/segment" } lightcraft-geocode = { path = "crates/geocode" } +lightcraft-slideshow = { path = "crates/slideshow" } # foundation thiserror = "2" diff --git a/NOTICE b/NOTICE index c9d650bb1..8b16eeba0 100644 --- a/NOTICE +++ b/NOTICE @@ -51,9 +51,10 @@ 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 -(the Japanese fonts BIZ UDPGothic, BIZ UDMincho and Shippori Mincho; the web build embeds only BIZ -UDPGothic Regular), under the SIL Open Font License 1.1. Release packages include each font's -licence as OFL-.txt. +(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-.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 diff --git a/README.md b/README.md index 3e93b41ee..cb6e5f6ab 100644 --- a/README.md +++ b/README.md @@ -307,11 +307,11 @@ LightCraft is young and moving fast: **alpha**. **Where we honestly stand** (det - **By feature count we're at ~81%** of Lightroom (core features 98%), tracked row by row in [docs/parity-checklist.md](docs/parity-checklist.md). -- **As a day-to-day Lightroom replacement we're nearer 61% for the whole product, ~49% for a typical working photographer and ~64% for +- **As a day-to-day Lightroom replacement we're nearer 61% for the whole product, ~51% for a typical working photographer and ~65% for 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 ILCE-7CR, ILCE-7M4, X-H2S, X-T4 and Nikon Z 6II 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, X-H2S, X-T4 and Nikon Z 6II 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 masks and denoise:** subject and sky selection are classical heuristics; - **network libraries:** a preview reads the raw's header — or, for a CR3 or a TIFF raw whose preview tag sits past it, exactly the ranges the sparse planner names — and its embedded JPEG only (56.5 MB → 5.9 MB, 10 s → 1 s on the test share), but the *developed* picture still needs the sensor data over the wire; see [docs/raw-preview-reads.md](docs/raw-preview-reads.md); @@ -405,7 +405,7 @@ CRAFT_FONTS_DIR=../craft-fonts cargo run --release -p lightcraft Without it LightCraft builds and runs the same, but Chinese and Japanese text have no glyphs. Fonts are never committed to this repo — the standard for what goes there lives with it: see the [craft-fonts README](https://github.com/storytold/craft-fonts#readme). -**Interface language:** **Edit → Language** (English, 简体中文, 繁體中文(台灣), 日本語, Português (Brasil), Español, Deutsch, Русский, Français) or **Settings → General → +**Interface language:** **Edit → Language** (English, 简体中文, 繁體中文(台灣), 日本語, Português (Brasil), Español, Deutsch, Русский, Français, Українська, Čeština) or **Settings → General → Language**; the choice applies immediately and persists. See [docs/localization.md](docs/localization.md). **Logs:** the desktop app writes its log to standard error and to `logs/lightcraft.log` in its settings folder diff --git a/README.zh-hans.md b/README.zh-hans.md index 5183d40a8..d6f19f60d 100644 --- a/README.zh-hans.md +++ b/README.zh-hans.md @@ -304,7 +304,7 @@ LightCraft 很年轻,而且进展很快。**我们如实所处的位置**( - **作为日常替代 Lightroom 的工具,我们更接近 60–70%。** 在单台机器上处理 JPEG/DNG 以及大多数尼康/索尼/ 较老佳能的 raw 都很出色。 - **最大的缺口:** - - **相机色彩校准:** 索尼、尼康、松下、富士和佳能 CR3 的 raw 会从各自的相机 JPEG 得到受保护的估计值,内置 ILCE-7CR、ILCE-7M4、X-H2S、X-T4 和尼康 Z 6II 配置文件;实测校准仍然缺失,其他 raw 或被拒绝的拟合会保留中性矩阵; + - **相机色彩校准:** 索尼、尼康、松下、富士和佳能 CR3 的 raw 会从各自的相机 JPEG 得到受保护的估计值,内置 ILCA-99M2、ILCE-7CR、ILCE-7M4、X-H2S、X-T4 和尼康 Z 6II 配置文件;实测校准仍然缺失,其他 raw 或被拒绝的拟合会保留中性矩阵; - **压缩的奥林巴斯 raw 和不受支持的 CR3 变体:** 这些在有内嵌 JPEG 预览时使用预览。富士无损/有损压缩 RAF 现在能解码传感器数据;[验证与已有图库重新加载说明](docs/raf-compression.md); - **AI 蒙版与降噪:** 主体和天空选择目前是经典启发式算法; - **屏幕上的 HDR 显示、视频,以及经典版式中的打印/画册/地图模块。** diff --git a/ROADMAP.md b/ROADMAP.md index 3f02a1199..a308a7f4f 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -2,7 +2,7 @@ **Stage: alpha** · next: beta, ~14 points and ~340–600 h away -> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-11 · **Change:** minor (Leica Q3 verified, 236 models; EOS R5 Mark II CR3 verified; RAW decoding 77%) · **Target:** Adobe Lightroom Classic 15.5 / Lightroom 9.6 +> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-14 · **Change:** minor (RAW: 82% of Adobe's models decode, 36% verified) · **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 @@ -15,8 +15,8 @@ from [docs/gaps.md](docs/gaps.md). The full assessment, method and evidence are | **Feature breadth** (checklist) | **81.1%** of 517 rows (P0 98.0%, P1 95.1%, P2 46.9%) | measured: [parity-checklist.md](docs/parity-checklist.md) recount | | **Feature breadth** (weighted by use) | **~77%** | estimated | | **Ready for real work** (full target) | **~61%** (56–66%) | estimated: weighted sum over the dimensions | -| **Mainstream practitioner** | **~49%** (44–54%) | estimated | -| **Essentials user** | **~64%** (59–69%) | estimated | +| **Mainstream practitioner** | **~51%** (46–56%) | estimated | +| **Essentials user** | **~65%** (60–70%) | estimated | | Remaining to **beta** | **~340–600 h** (Opus 5.5 agent hours, ~60% parallel) | estimated | | Remaining to **full parity** | **~1,000–1,800 h** (~70% parallel) | estimated | @@ -25,15 +25,15 @@ Each audience with its own hours (Opus 5.5 agent wall-clock hours to ~95%): | Audience | Ready | Opus 5.5 agent wall-clock hours to ~95% | Work that dominates | |---|---:|---:|---| | Full target (ready for real work) | **~61%** | **1,000–1,800 h** (~70% parallelizes) | raw colour, lens and camera data; AI models; Classic output modules; localization; plug-in ecosystem | -| Mainstream practitioner | **~49%** | **450–840 h** (~60% parallelizes) | camera colour and render fidelity; CR3 / ORF / HE NEF; lens database; subject / sky masks; Lightroom XMP exchange; stability | -| Essentials user | **~64%** | **160–300 h** (~50% parallelizes) | default raw look for popular cameras; launch stability; basic-slider feel; opening CR3 / HEIC files people send | +| Mainstream practitioner | **~51%** | **450–840 h** (~60% parallelizes) | camera colour and render fidelity; CR3 / ORF / HE NEF; lens database; subject / sky masks; Lightroom XMP exchange; stability | +| Essentials user | **~65%** | **160–300 h** (~50% parallelizes) | default raw look for popular cameras; launch stability; basic-slider feel; opening CR3 / HEIC files people send | The gap between breadth and readiness is quality and coverage, not missing buttons: camera colour is -estimated rather than measured, CR3 decodes on four verified Canon bodies, compressed ORF and HE NEF +estimated rather than measured, CR3 decodes on 26 verified Canon bodies (rarer CRX variants still open as previews), compressed ORF and HE NEF open as previews, rendering is tuned by eye, AI masks are heuristics, there is no lens database, and tests run in CI on FreeBSD only. **Why alpha:** core workflows run end to end, but readiness is below -the ~75% beta bar (it is above the ~40% alpha bar) and Lightroom's main inputs, raw files, still have blocking gaps (Canon, ~30% of -raw users, decodes CR3 on four verified bodies). It passes the alpha gate: all six core workflows +the ~75% beta bar (it is above the ~40% alpha bar) and Lightroom's main inputs, raw files, still have blocking gaps (compressed ORF and HE NEF open as previews; Canon, +~30% of raw users, decodes CR3 on 26 verified bodies). It passes the alpha gate: all six core workflows (import, cull, develop, save and reopen, batch looks, export) work end to end on macOS ([docs/roadmap.md](docs/roadmap.md#alpha-gate)). The three readiness numbers and their weights and discounts are in [target-app-parity.md](docs/target-app-parity.md#ready-for-real-work-mainstream-practitioner-and-essentials-user). @@ -45,11 +45,11 @@ Hours are calibrated on this repo's PR history; see | Dimension | Ready | Breadth | Remaining | Doc | |---|---:|---:|---:|---| | Features | ~61% | ~77% (checklist 81.1%) | 600–1,100 h | [target-app-parity](docs/target-app-parity.md#features) | -| ↳ RAW cameras, colour, lens profiles | ~56% | 81% of 1,446 Adobe models decode, 16% verified | 255–480 h | [raw-parity](docs/raw-parity.md) | +| ↳ RAW cameras, colour, lens profiles | ~60% | 82% of 1,446 Adobe models decode, 36% verified | 255–480 h | [raw-parity](docs/raw-parity.md) | | UI/UX fidelity | ~70% | ~85% | 34–58 h | [ui-parity](docs/ui-parity.md) | | File formats (non-raw) | ~70% | ~85% | 30–55 h | [file-format-parity](docs/file-format-parity.md) | | Hardware | ~40% | ~50% | 40–80 h | [hardware-parity](docs/hardware-parity.md) | -| Localization | ~52% of the 12 key languages | 7 of 12 present | 130–210 h (+145–255 h new scripts) | [localization-parity](docs/localization-parity.md) | +| Localization | ~55% of the 12 key languages | 7 of 12 present | 130–210 h (+145–255 h new scripts) | [localization-parity](docs/localization-parity.md) | | Performance | ~70% | ~80% | 30–50 h | [target-app-parity](docs/target-app-parity.md#by-dimension) | | Stability | ~60% | — | 30–60 h | [target-app-parity](docs/target-app-parity.md#by-dimension) | | Platforms | ~60% | macOS, Windows, Linux, FreeBSD, web | 25–45 h | [target-app-parity](docs/target-app-parity.md#by-dimension) | @@ -114,8 +114,23 @@ Ranked toward beta; detail in [docs/roadmap.md](docs/roadmap.md), gaps in [docs/ ## Progress log +- 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 + 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. +- 2026-10-12: Epson ERF (R-D1, R-D1s, R-D1x) decodes from sensor data, white balance measured from each + file's preview; 3 bodies verified on CC0 files. 240 models verified; headline numbers unchanged. - 2026-10-11: Leica Q3 verified by a recorded check of 38 private DNGs (all decode and develop at full size; mean ΔE to the camera JPEG 6.3–13.9). 236 models verified; headline numbers unchanged. +- 2026-10-12: 22 more Canon CR3 bodies verified (EOS R, RP, R3, R5, R6, R6 Mark II / III, R7, R10, R50, + R50 V, 90D, 250D, 850D, M200, M50 Mark II, M6 Mark II, 1D X Mark III, PowerShot G5 X II, G7 X III, SX70, + V1): one CC0 file each pinned and checked against the camera JPEG; all 110 CC0 CR3s on raw.pixls.us + 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 + 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 pinned, the QP maps of 33,665 private C-RAW files decode exactly ([cr3.md](docs/cr3.md)). 235 models @@ -132,6 +147,8 @@ Ranked toward beta; detail in [docs/roadmap.md](docs/roadmap.md), gaps in [docs/ / localization parity); `docs/parity.md` renamed `docs/parity-checklist.md`. Landed today: Nikon lossy-after-split NEF (#633), ORF 12.8 bpp (#651), HDR gain map JPEG / PQ AVIF / float TIFF export (#646), Lightroom-like Auto (#628), activity stack. +- 2026-10-11: Sony ILCA-99M2 (α99 II) camera colour: bundled profile from 144 photos; on 30 withheld photos + mean ΔE vs the camera JPEG 5.11 → 4.87, 25 of them closer. - 2026-10-09: Sony ILCE-7CR camera colour: bundled profile from 125 photos; on 26 withheld photos mean ΔE vs the camera JPEG 4.07 → 3.29. HEIC in release builds (#609), Samsung uncompressed SRW, contact-sheet PDF export, Ukrainian and French UI, release v0.5.0. @@ -163,7 +180,16 @@ Ranked toward beta; detail in [docs/roadmap.md](docs/roadmap.md), gaps in [docs/ | Date | Change | Summary | |---|---|---| +| 2026-10-13 | minor | Sony ILCA-99M2 camera colour profile in the log; numbers unchanged | +| 2026-10-14 | minor | Localization ~52% → ~55% (mean of the 12 key languages): de, es, fr, ja and uk catalogs filled | +| 2026-10-14 | minor | 21 Sony and Leica models verified (258 models, 18%); headline numbers unchanged | +| 2026-10-12 | minor | UI/UX remaining 34–58 h → 32–54 h (canvas tools by keyboard); headline numbers unchanged | +| 2026-10-14 | minor | 17 uncompressed Olympus and Samsung models verified on one CC0 file each (254 models verified, 18%); headline numbers unchanged | +| 2026-10-12 | minor | Mamiya MEF decoded and imported: ZD verified (241); headline numbers unchanged | +| 2026-10-12 | minor | Epson ERF decoded: 3 more verified models (240); headline numbers unchanged | | 2026-10-12 | minor | UI/UX remaining 40–70 h → 34–58 h (accessibility audit landed); headline numbers unchanged | +| 2026-10-12 | minor | 22 more CR3 bodies verified, EOS R6 Mark III white balance: RAW decoding 77% → 83%, RAW ~56% → ~60%; mainstream 49% → 51%, essentials 64% → 65%; full unchanged at ~61% | +| 2026-10-12 | minor | 117 Nikon and Fujifilm bodies verified (354 models, 24%); headline numbers unchanged | | 2026-10-11 | minor | EOS R5 Mark II CR3 verified: CR3 on four verified bodies, RAW decoding 76% → 77%; headline numbers unchanged | | 2026-10-10 | minor | RAW measured model by model; RAW decode and colour as explicit rows: full 59% → 61%, mainstream 51% → 49%, essentials 63% → 64%; beta ~14 points | | 2026-10-10 | minor | Full ready restored to the additive weighted sum (~59%; method aligned with the standard, no new evidence); hours per audience; beta ~16 points away | @@ -173,3 +199,5 @@ Ranked toward beta; detail in [docs/roadmap.md](docs/roadmap.md), gaps in [docs/ | 2026-10-08 | minor | *Where we stand* by dimension and by kind of user | | 2026-10-02 | major | Parity estimate and effort table | | 2026-09-30 | major | Created | +| 2026-10-14 | minor | 97 Canon CR2, Pentax and Ricoh models verified (334 models, 23%); headline numbers unchanged | +| 2026-10-14 | minor | RAW: 82% of Adobe's models decode, 36% verified (integration batch 36's CC0 per-model checks) | diff --git a/apps/lightcraft-cli/src/main.rs b/apps/lightcraft-cli/src/main.rs index b6cad57e0..04bf7a145 100644 --- a/apps/lightcraft-cli/src/main.rs +++ b/apps/lightcraft-cli/src/main.rs @@ -3,9 +3,9 @@ //! ```text //! lightcraft-cli run [--demo | --library DIR | --connect [ADDR]] [--import PATH]… CMD [key=value…]… //! lightcraft-cli mcp [--connect [ADDR]] [--demo] [--compact] [FILES/FOLDERS…] -//! lightcraft-cli render -o [--set control=value]… [--settings FILE.json] [--preset ID] [--size N] [--quality Q] +//! lightcraft-cli render -o [--set control=value]… [--settings FILE.json|FILE.lcpreset] [--preset ID] [--size N] [--quality Q] //! lightcraft-cli snapshot [--library DIR | --demo] [--script FILE.jsonl] [-o OUT.png] [--size WxH] [--scale S] [FILES…] -//! lightcraft-cli merge hdr|panorama|hdr-panorama [OPTIONS] FILES… +//! lightcraft-cli merge hdr|panorama|hdr-panorama|hdr-batch [OPTIONS] FILES… //! lightcraft-cli synth-merge hdr|panorama -o DIR //! lightcraft-cli denoise-serve [--model ID] [--models DIR] [--listen ADDR] [--token-env VAR] [--run-on auto|gpu|cpu] //! lightcraft-cli commands [--json] @@ -17,7 +17,7 @@ mod alloc_release; -use std::io::{BufReader, Write}; +use std::io::Write; use std::path::Path; use std::process::ExitCode; @@ -55,7 +55,8 @@ USAGE: Develop one file and export it (.jpg, .png, .tif, .webp, .avif, .jxl, .psd or .dng by extension) with the same encoder as the app's Export dialog. Options: --set CONTROL=VALUE set a develop slider, repeatable (e.g. --set light.exposure=0.5) - --settings FILE merge a partial develop-settings JSON file + --settings FILE merge a partial develop-settings JSON file, or apply the one preset of a + .lcpreset (preset.export) or XMP preset --preset ID apply a preset (see `commands`/presets.list) --size N long edge in pixels (default: full size, cropped) --quality Q JPEG/AVIF/JPEG XL quality 1..100 (default 92) @@ -79,9 +80,10 @@ USAGE: -o, --output OUT PNG path (a final screenshot is written here if the script took none) --size WxH window size in points (default 1600x1000) --scale S pixels per point (default 1) - lightcraft-cli merge hdr|panorama|hdr-panorama [OPTIONS] FILES… + lightcraft-cli merge hdr|panorama|hdr-panorama|hdr-batch [OPTIONS] FILES… Photo Merge: writes -HDR.dng / -Pano.dng / -HDR-Pano.dng next to the first file and - prints the result as JSON. Options: + prints the result as JSON. hdr-batch merges each bracket set among the files (photos taken + less than a second apart at different exposures), e.g. a whole folder. Options: --deghost none|low|medium|high --no-align --bracket N (HDR panorama) --projection auto|spherical|cylindrical|perspective --boundary-warp 0..100 --auto-crop --fill-edges --no-auto-settings @@ -364,8 +366,7 @@ fn mcp(args: &[String]) -> Result<(), String> { }; eprintln!("lightcraft-cli mcp: serving MCP on stdio ({})", backend.describe()); let mut server = Server::new(backend).with_command_tools(!compact); - let stdin = std::io::stdin(); - server.serve(BufReader::new(stdin), std::io::stdout()).map_err(|e| e.to_string()) + server.serve_stdio(std::io::stdout()).map_err(|e| e.to_string()) } fn merge(args: &[String]) -> Result<(), String> { @@ -373,7 +374,8 @@ fn merge(args: &[String]) -> Result<(), String> { Some("hdr") => "merge.hdr", Some("panorama" | "pano") => "merge.panorama", Some("hdr-panorama") => "merge.hdrPanorama", - _ => return Err("merge: expected hdr, panorama or hdr-panorama".into()), + Some("hdr-batch") => "merge.hdrBatch", + _ => return Err("merge: expected hdr, panorama, hdr-panorama or hdr-batch".into()), }; let mut p = serde_json::Map::new(); let mut files = Vec::new(); @@ -635,11 +637,41 @@ fn connect_port(addr: &str) -> &str { addr.rsplit_once(':').map_or(addr, |(_, port)| port) } +/// What `render --settings FILE` applies: a partial develop-settings object, or the one preset of +/// a preset file (`.lcpreset` from `preset.export`, XMP), applied as the app applies it. Merging a +/// `.lcpreset` as settings dropped its wrapper and applied nothing (#1730). +enum SettingsFile { + Partial(Value), + Preset(lightcraft_engine::develop::Preset), +} + +fn read_settings(p: &str) -> Result { + use lightcraft_engine::presets::{LCPRESET_EXT, parse_preset_file}; + let bytes = std::fs::read(p).map_err(|e| format!("{p}: {e}"))?; + let ext = Path::new(p).extension().map(|e| e.to_string_lossy().to_ascii_lowercase()).unwrap_or_default(); + let json = serde_json::from_slice::(&bytes); + // a settings object never has a top-level `presets` or `settings` key; a preset file always does + let preset_file = + ext == LCPRESET_EXT || ext == "xmp" || json.as_ref().is_ok_and(|v| v.is_array() || v.get("presets").is_some() || v.get("settings").is_some()); + if preset_file { + let mut list = parse_preset_file(p, &bytes).map_err(|e| format!("{p}: {e}"))?; + return match (list.pop(), list.len()) { + (Some(preset), 0) => Ok(SettingsFile::Preset(preset)), + (None, _) => Err(format!("{p}: no presets in this file")), + (Some(_), n) => Err(format!("{p}: {} presets in this file; --settings applies one, export it on its own", n + 1)), + }; + } + match json.map_err(|e| format!("{p}: {e}"))? { + v @ Value::Object(_) => Ok(SettingsFile::Partial(v)), + _ => Err(format!("{p}: not a develop-settings object or a preset file")), + } +} + fn render(args: &[String]) -> Result<(), String> { let mut input = None; let mut output = None; let mut values = serde_json::Map::new(); - let mut settings: Option = None; + let mut settings = None; let mut preset = None; let mut size: Option = None; let mut quality = 92u8; @@ -654,11 +686,7 @@ fn render(args: &[String]) -> Result<(), String> { let v: f64 = v.trim().parse().map_err(|_| format!("--set {k}: `{v}` is not a number"))?; values.insert(k.trim().to_string(), json!(v)); } - "--settings" => { - let p = take_value(args, &mut i, "--settings")?; - let text = std::fs::read_to_string(p).map_err(|e| format!("{p}: {e}"))?; - settings = Some(serde_json::from_str(&text).map_err(|e| format!("{p}: {e}"))?); - } + "--settings" => settings = Some(read_settings(take_value(args, &mut i, "--settings")?)?), "--preset" => preset = Some(take_value(args, &mut i, "--preset")?.to_string()), "--size" => size = Some(take_value(args, &mut i, "--size")?.parse().map_err(|_| "--size expects a number")?), "--quality" => quality = take_value(args, &mut i, "--quality")?.parse().map_err(|_| "--quality expects 1..100")?, @@ -684,8 +712,14 @@ fn render(args: &[String]) -> Result<(), String> { if let Some(p) = preset { run(&mut s, "preset.apply", json!({"id": p}))?; } - if let Some(st) = settings { - run(&mut s, "develop.merge", json!({"settings": st}))?; + match settings { + Some(SettingsFile::Partial(st)) => run(&mut s, "develop.merge", json!({"settings": st}))?, + Some(SettingsFile::Preset(mut p)) => { + p.id = "cli.settings".into(); + s.presets.push(p); + run(&mut s, "preset.apply", json!({"id": "cli.settings"}))?; + } + None => {} } if !values.is_empty() { run(&mut s, "develop.set", json!({"values": values}))?; @@ -763,12 +797,15 @@ fn snapshot(args: &[String]) -> Result<(), String> { let t0 = Instant::now(); let mut session = match &library { Some(dir) => { - let mut s = Session::new().with_fs().with_default_denoise_models().with_system_clock().with_default_face_models(); + let mut s = + Session::new().with_fs().with_default_denoise_models().with_system_clock().with_default_face_models().with_default_slide_templates(); s.open_library(dir, false).map_err(|e| library_error(dir, e))?; s } - None if files.is_empty() => Session::with_demo().with_fs().with_default_denoise_models().with_system_clock().with_default_face_models(), - None => Session::new().with_fs().with_default_denoise_models().with_system_clock().with_default_face_models(), + None if files.is_empty() => { + Session::with_demo().with_fs().with_default_denoise_models().with_system_clock().with_default_face_models().with_default_slide_templates() + } + None => Session::new().with_fs().with_default_denoise_models().with_system_clock().with_default_face_models().with_default_slide_templates(), }; // Apply compute policy before imports or the first scripted query can discover an adapter. session.execute("app.gpu", &json!({"enabled": false})).map_err(|e| e.to_string())?; @@ -832,7 +869,17 @@ fn snapshot(args: &[String]) -> Result<(), String> { match next_path(&mut shots) { Some(p) => { wrote_output = true; - params["path"] = json!(p); + // `params` is untrusted script JSON: `params["path"] = …` panics on + // an array or a string. Only an object (or null, like an absent + // `params`) may take the path. + if params.is_null() { + params = json!({}); + } + if let Some(o) = params.as_object_mut() { + o.insert("path".into(), json!(p)); + } else { + return Err(format!("{script}:{}: ui.screenshot: `params` must be a JSON object", n + 1)); + } } None => return Err(format!("{script}:{}: ui.screenshot needs a `path` (or give -o)", n + 1)), } diff --git a/apps/lightcraft-cli/tests/cli.rs b/apps/lightcraft-cli/tests/cli.rs index bf56b02ab..8782fb80f 100644 --- a/apps/lightcraft-cli/tests/cli.rs +++ b/apps/lightcraft-cli/tests/cli.rs @@ -139,6 +139,51 @@ fn render_subcommand() { assert!(String::from_utf8_lossy(&o.stderr).contains("unknown control")); } +/// Issue #1730: `render --settings` took a `.lcpreset` from `preset.export`, ignored its wrapper +/// and rendered the photo unedited. +#[test] +fn render_settings_applies_an_lcpreset_and_refuses_unknown_files() { + let input = tmp("lcpreset-in.png"); + gradient_png(&input); + let preset = tmp("bright.lcpreset"); + let (ok, lines, err) = run_cli( + &[ + "--import", + input.to_str().unwrap(), + "develop.set", + "control=light.exposure", + "value=1", + "preset.create", + "name=Bright", + "preset.export", + &format!("path={}", preset.display()), + ], + None, + ); + assert!(ok, "{err}: {lines:?}"); + let plain = tmp("lcpreset-plain.png"); + let st = Command::new(BIN).args(["render", input.to_str().unwrap(), "-o", plain.to_str().unwrap()]).status().unwrap(); + assert!(st.success()); + let bright = tmp("lcpreset-bright.png"); + let o = Command::new(BIN) + .args(["render", input.to_str().unwrap(), "-o", bright.to_str().unwrap(), "--settings", preset.to_str().unwrap()]) + .output() + .unwrap(); + assert!(o.status.success(), "{}", String::from_utf8_lossy(&o.stderr)); + assert!(mean(&bright) > mean(&plain) + 10.0, "the preset's exposure was not applied"); + + let other = tmp("other.lcpreset"); + std::fs::write(&other, r#"{"format": "other", "version": 1, "presets": []}"#).unwrap(); + let out = tmp("lcpreset-other.png"); + let o = Command::new(BIN) + .args(["render", input.to_str().unwrap(), "-o", out.to_str().unwrap(), "--settings", other.to_str().unwrap()]) + .output() + .unwrap(); + assert!(!o.status.success(), "an unreadable settings file was accepted"); + assert!(String::from_utf8_lossy(&o.stderr).contains("other.lcpreset"), "{}", String::from_utf8_lossy(&o.stderr)); + assert!(!out.exists(), "nothing is written when the settings can't be read"); +} + /// Issue #93: `render IMG -o IMG` replaced its own input with the render. #[test] fn render_refuses_to_overwrite_its_input() { @@ -273,6 +318,24 @@ fn snapshot_script_failure_exits_non_zero() { assert_eq!((d.width, d.height), (320, 200)); } +/// A script line whose `params` is not an object must be an error, never a panic: the +/// `ui.screenshot`-without-a-path branch used to index (`params["path"] = …`) an array and abort +/// the whole script. +#[test] +fn snapshot_script_with_non_object_params_is_an_error() { + let script = tmp("snap-bad-params.jsonl"); + let out = tmp("snap-bad-params.png"); + std::fs::write(&script, format!("{}\n", json!({"method": "ui.screenshot", "params": [1]}))).unwrap(); + let o = Command::new(BIN) + .args(["snapshot", "--demo", "--script", script.to_str().unwrap(), "-o", out.to_str().unwrap(), "--size", "320x200"]) + .output() + .unwrap(); + let stderr = String::from_utf8_lossy(&o.stderr); + assert!(!o.status.success(), "{stderr}"); + assert!(!stderr.contains("panicked"), "a bad `params` must be an error, not a panic: {stderr}"); + assert!(stderr.contains("`params` must be a JSON object"), "{stderr}"); +} + #[test] fn snapshot_ui_zoom_keeps_requested_pixel_dimensions() { let script = tmp("snap-ui-zoom.jsonl"); diff --git a/apps/lightcraft-cli/tests/mcp_shutdown.rs b/apps/lightcraft-cli/tests/mcp_shutdown.rs new file mode 100644 index 000000000..29d1ace93 --- /dev/null +++ b/apps/lightcraft-cli/tests/mcp_shutdown.rs @@ -0,0 +1,38 @@ +//! A broken protocol output must not require the client to close its input pipe. + +use std::io::{Read, Write}; +use std::process::{Command, Stdio}; +use std::time::{Duration, Instant}; + +#[test] +fn broken_stdout_exits_with_stdin_still_open() { + let mut child = Command::new(env!("CARGO_BIN_EXE_lightcraft-cli")) + .arg("mcp") + .stdin(Stdio::piped()) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .spawn() + .unwrap(); + let mut input = child.stdin.take().unwrap(); + drop(child.stdout.take()); + input.write_all(b"{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"initialize\"}\n{\"jsonrpc\":").unwrap(); + input.flush().unwrap(); + let deadline = Instant::now() + Duration::from_secs(10); + let status = loop { + if let Some(status) = child.try_wait().unwrap() { + break status; + } + if Instant::now() >= deadline { + child.kill().unwrap(); + child.wait().unwrap(); + panic!("MCP did not exit after stdout failed while stdin stayed open"); + } + std::thread::sleep(Duration::from_millis(10)); + }; + let mut stderr = String::new(); + child.stderr.take().unwrap().read_to_string(&mut stderr).unwrap(); + assert!(stderr.contains("serving MCP on stdio"), "CLI must reach the MCP transport: {stderr}"); + assert!(!status.success(), "broken protocol output must be reported: {stderr}"); + assert!(stderr.contains("lightcraft-cli:"), "CLI must report the transport error: {stderr}"); + drop(input); +} diff --git a/apps/lightcraft-web/src/web.rs b/apps/lightcraft-web/src/web.rs index 18c896367..6a4487d09 100644 --- a/apps/lightcraft-web/src/web.rs +++ b/apps/lightcraft-web/src/web.rs @@ -219,6 +219,8 @@ fn services(originals: Originals, backend: Option, files: Files, frozen save_keyword_list: None, pick_curve_preset_files: None, save_curve_preset_file: None, + pick_slideshow_templates: None, + save_slideshow_template: None, write: Some(Box::new(download)), // downloads happen on the main thread: exports run in the foreground on the web write_shared: None, diff --git a/apps/lightcraft-web/src/wire.rs b/apps/lightcraft-web/src/wire.rs index b3d685f5f..08f9e898d 100644 --- a/apps/lightcraft-web/src/wire.rs +++ b/apps/lightcraft-web/src/wire.rs @@ -41,6 +41,9 @@ pub struct WireJob { pub as_shot_temp: f64, pub as_shot_tint: f64, pub lens: Option, + /// The original's long edge (`SourceInfo::native_long`). + #[serde(default)] + pub native_long: Option, pub settings: Arc, pub max_w: usize, pub max_h: usize, @@ -80,6 +83,7 @@ impl WireJob { as_shot_temp: job.info.as_shot_temp, as_shot_tint: job.info.as_shot_tint, lens: job.info.lens, + native_long: job.info.native_long, settings: job.settings.clone(), max_w: job.request.max_w, max_h: job.request.max_h, @@ -101,6 +105,7 @@ impl WireJob { lens: self.lens, relative_wb: self.relative_wb, legacy_relative_wb: self.legacy_relative_wb, + native_long: self.native_long, ..Default::default() } } @@ -469,6 +474,7 @@ mod tests { as_shot_temp: 6500.0, as_shot_tint: 0.0, lens: None, + native_long: None, settings: Arc::new(DevelopSettings::default()), max_w: 32, max_h: 32, diff --git a/apps/lightcraft-web/src/workers.rs b/apps/lightcraft-web/src/workers.rs index f1523c377..a5e64886e 100644 --- a/apps/lightcraft-web/src/workers.rs +++ b/apps/lightcraft-web/src/workers.rs @@ -161,6 +161,8 @@ struct Busy { level: SourceLevel, key: u64, cache: Option<(Arc, Hash128)>, + /// A slideshow slide: the worker renders the photo, it is composed here when it comes back. + slide: Option>, } struct W { @@ -370,6 +372,13 @@ impl Workers { }); } g.remote_done += 1; + let rendered = match (rendered, &b.slide) { + (Ok(mut r), Some(slide)) => { + r.image = slide.compose(&r.image); + Ok(r) + } + (r, _) => r, + }; g.done.push(( b.slot, RenderResult { @@ -493,6 +502,7 @@ impl RenderOffload for Workers { level: job.level, key: job.key, cache: job.cache, + slide: job.slide, }); None } diff --git a/apps/lightcraft/src/appearance.rs b/apps/lightcraft/src/appearance.rs index 74333a301..223dbc560 100644 --- a/apps/lightcraft/src/appearance.rs +++ b/apps/lightcraft/src/appearance.rs @@ -51,7 +51,7 @@ fn decode(code: u32) -> Option { /// absolute path, and is never repeated. #[cfg(target_os = "linux")] fn gtk_theme() -> Option { - let exe = ["/usr/bin/gsettings", "/bin/gsettings", "/usr/local/bin/gsettings"].into_iter().find(|p| std::path::Path::new(p).is_file())?; + let exe = gsettings_exe(std::env::var_os("PATH").as_deref())?; let output = std::process::Command::new(exe).args(["get", "org.gnome.desktop.interface", "color-scheme"]).output().ok()?; if !output.status.success() { return None; @@ -59,6 +59,15 @@ fn gtk_theme() -> Option { parse_gtk_scheme(std::str::from_utf8(&output.stdout).ok()?) } +/// `gsettings` by absolute path: the absolute `PATH` entries first (NixOS has no /usr/bin/gsettings), +/// then the usual places for a sparse `PATH`. Relative entries are skipped, so nothing runs from +/// the working folder. +#[cfg(target_os = "linux")] +fn gsettings_exe(path: Option<&std::ffi::OsStr>) -> Option { + let listed = path.map(std::env::split_paths).into_iter().flatten().filter(|dir| dir.is_absolute()); + listed.chain(["/usr/bin", "/bin", "/usr/local/bin"].map(std::path::PathBuf::from)).map(|dir| dir.join("gsettings")).find(|exe| exe.is_file()) +} + #[cfg(target_os = "linux")] fn parse_gtk_scheme(s: &str) -> Option { match s.trim().trim_matches('\'') { @@ -174,6 +183,24 @@ mod tests { assert_eq!(calls.load(Ordering::Relaxed), after_settle); } + /// NixOS keeps `gsettings` in /run/current-system/sw/bin or the per-user profile: it is found + /// on `PATH`, but never through a relative entry, even one that leads to a real file. + #[cfg(target_os = "linux")] + #[test] + fn gsettings_is_found_on_absolute_path_entries_only() { + let dir = std::env::temp_dir().join(format!("lightcraft-gsettings-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + std::fs::write(dir.join("gsettings"), "").unwrap(); + let path = std::env::join_paths([std::path::Path::new("relative/bin"), &dir]).unwrap(); + assert_eq!(super::gsettings_exe(Some(&path)), Some(dir.join("gsettings"))); + let cwd = std::env::current_dir().unwrap(); + let relative = std::path::PathBuf::from("../".repeat(cwd.components().count() - 1)).join(dir.strip_prefix("/").unwrap()); + assert!(relative.join("gsettings").is_file()); + let found = super::gsettings_exe(Some(relative.as_os_str())); + assert!(found.as_ref().is_none_or(|p| p.is_absolute()), "{found:?}"); + let _ = std::fs::remove_dir_all(&dir); + } + #[cfg(target_os = "linux")] #[test] fn portal_values_and_gsettings_output_decode() { diff --git a/apps/lightcraft/src/main.rs b/apps/lightcraft/src/main.rs index 1c70e19ea..d6033f678 100644 --- a/apps/lightcraft/src/main.rs +++ b/apps/lightcraft/src/main.rs @@ -76,10 +76,13 @@ impl eframe::App for App { fn raw_input_hook(&mut self, _ctx: &egui::Context, raw: &mut egui::RawInput) { self.0.raw_input_hook(raw); } - fn ui(&mut self, ui: &mut egui::Ui, _frame: &mut eframe::Frame) { + 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); self.0.ui(ui); } fn on_exit(&mut self, _gl: Option<&eframe::glow::Context>) { + // (a size the window was just given is remembered too) + self.0.ui.window = self.1.window_at_exit(self.0.ui.window); if let Err(e) = self.1.save(&self.0) { log::error!("{e}"); } @@ -162,6 +165,46 @@ fn gpu_crash_notice(what: &str) -> String { ) } +/// Written when the window stopped drawing after the GPU device was lost (issue #1337): the next +/// launch renders on the CPU and says why. +fn gpu_lost_marker_path() -> Option { + gpu_marker_path().map(|p| p.with_file_name("gpu-lost.marker")) +} + +/// Why the GPU was lost, when the GPU pipeline stopped after a device failure in this process (a +/// driver reset, a hang, the Windows watchdog): `None` for other reasons it is off, or when it +/// never failed. +fn gpu_lost_reason() -> Option { + lightcraft_engine::gpu::unavailable_reason().and_then(|r| r.strip_prefix("stopped after a GPU failure: ").map(str::to_string)) +} + +/// Record that LightCraft stopped after the GPU was lost, for the next launch. +fn record_gpu_lost(marker: Option<&std::path::Path>, reason: &str) { + let Some(m) = marker else { return }; + if let Some(d) = m.parent() { + let _ = std::fs::create_dir_all(d); + } + if let Err(e) = std::fs::write(m, format!("{reason}\n")) { + log::warn!("could not write {}: {e}", m.display()); + } +} + +/// A GPU loss the last launch stopped on: its notice, once (the marker is removed, except by a +/// `--memory` session, which leaves the disk as it found it). +fn gpu_lost_check_at(marker: Option<&std::path::Path>, in_memory: bool) -> Option { + let m = marker?; + let reason = std::fs::read_to_string(m).ok()?; + if !in_memory { + let _ = std::fs::remove_file(m); + } + Some(format!( + "LightCraft stopped last time after the graphics driver reset the GPU ({}) while photos rendered on it, so GPU rendering \ + is now off and photos render on the CPU. To try the GPU again, turn on Settings ▸ Performance ▸ Use the GPU for rendering; \ + an up-to-date graphics driver may help.", + reason.trim() + )) +} + /// Where the system keeps monitor profiles, for the Choose Profile dialog to start in: the user's /// own profiles first (Linux: colord / DisplayCAL; macOS: ColorSync), then the system's. fn display_profile_dir() -> Option { @@ -183,6 +226,16 @@ fn config_dir() -> Option { lightcraft_engine::config::config_dir() } +/// The window at the size it was last used at, zoomed if it was (`window`), or at the usual size +/// when nothing is remembered. A zoomed window is given the size it goes back to. Where it +/// opens is the system's to say. +fn as_remembered(builder: egui::ViewportBuilder, window: Option) -> egui::ViewportBuilder { + let builder = builder.with_inner_size(window.map_or(lightcraft_ui_egui::window_place::DEFAULT_SIZE, |w| w.size)); + // (only ever asked to zoom: asked not to, macOS un-zooms a window whose size fills the screen + // to a size of its own choosing) + if window.is_some_and(|w| w.zoomed) { builder.with_maximized(true) } else { builder } +} + /// `/ui.json`, the saved UI state and app settings — `None` when nothing is read or /// written: `LIGHTCRAFT_NO_PREFS` (tests, scripts) or no home folder. fn prefs_path() -> Option { @@ -301,6 +354,11 @@ struct PrefsWriter { failing: bool, /// `ui.json` couldn't be read at launch: never overwrite it this session. keep_file: bool, + /// Follows the window, to remember the size it rests at (`UiState::window`). + window: lightcraft_ui_egui::window_place::WindowWatch, + /// When the window was first looked at: its watch goes by the clock from then on, not by + /// the interface's time, which stands still while the window is hidden. + window_since: Option, } impl PrefsWriter { @@ -331,8 +389,33 @@ impl PrefsWriter { Ok(()) } + /// What to remember of the window as the app quits ([`WindowWatch::at_exit`]). + /// + /// [`WindowWatch::at_exit`]: lightcraft_ui_egui::window_place::WindowWatch::at_exit + fn window_at_exit( + &mut self, + remembered: Option, + ) -> Option { + match self.window_since { + Some(since) => self.window.at_exit(remembered, since.elapsed().as_secs_f64()), + None => remembered, + } + } + fn tick(&mut self, app: &mut LightcraftApp, ctx: &egui::Context) { - let now = ctx.input(|i| i.time); + // (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))); + // 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`) + 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 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) + ctx.request_repaint_after(wait); + } let moved = app.ui.settings.library_path != self.library; if !moved && now - self.checked < 3.0 { return; @@ -603,6 +686,24 @@ fn services(cc: &eframe::CreationContext<'_>, log_file: Option<&std::path::Path> .save_file() .map(|p| p.to_string_lossy().to_string()) })), + pick_slideshow_templates: Some(Box::new(|| { + rfd::FileDialog::new() + .set_title(lightcraft_ui_egui::i18n::tr("Import Slideshow Templates")) + .add_filter_nocase(lightcraft_ui_egui::i18n::tr("Slideshow Templates"), &["json"]) + .pick_files() + .unwrap_or_default() + .into_iter() + .map(|p| p.to_string_lossy().to_string()) + .collect() + })), + save_slideshow_template: Some(Box::new(|name: &str| { + rfd::FileDialog::new() + .set_title(lightcraft_ui_egui::i18n::tr("Export Slideshow Template")) + .add_filter_nocase(lightcraft_ui_egui::i18n::tr("Slideshow Templates"), &["json"]) + .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)), @@ -616,10 +717,12 @@ fn services(cc: &eframe::CreationContext<'_>, log_file: Option<&std::path::Path> } } -/// The library session with the folder face models are kept in (`/models`). +/// The library session with the folders face models (`/models`) and slideshow templates +/// (`/slideshow-templates`) are kept in. fn open_session(in_memory: bool, dir: Option, seed_demo: bool) -> (Session, Option) { let (mut s, problem) = open_library_session(in_memory, dir, 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 s.apple_photos_background = true; (s, problem) @@ -808,7 +911,7 @@ fn main() -> eframe::Result { let library_dir = library_dir.or_else(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); + let gpu_crash = gpu_crash_check(in_memory).or_else(|| gpu_lost_check_at(gpu_lost_marker_path().as_deref(), in_memory)); let gpu_on = prefs.as_ref().is_none_or(|u| u.settings.gpu) && gpu_crash.is_none(); lightcraft_engine::gpu::set_enabled(gpu_on); // the window's renderer: probed (Windows) and with a fallback chain, so a driver that can't @@ -817,12 +920,15 @@ fn main() -> eframe::Result { let window_gpu::Chosen { renderer, wgpu_options, which: window_renderer, notice: window_notice } = window_gpu::choose(&win_files); win_files.arm(window_renderer); let win_files_app = win_files.clone(); + // the window came up: a failure after this is LightCraft stopping, not a start that failed + let came_up = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false)); + let came_up_app = came_up.clone(); + // how the window was when the app was last used (checked: the settings file is a file) + let opens_as = prefs.as_ref().and_then(|ui| ui.window).and_then(lightcraft_ui_egui::window_place::WindowPlace::checked); let options = eframe::NativeOptions { renderer, - viewport: egui::ViewportBuilder::default() - .with_title("LightCraft") - .with_inner_size([1600.0, 1000.0]) - .with_min_inner_size([900.0, 560.0]) + viewport: as_remembered(egui::ViewportBuilder::default().with_title("LightCraft"), opens_as) + .with_min_inner_size(lightcraft_ui_egui::window_place::MIN_SIZE) .with_drag_and_drop(true) .with_fullsize_content_view(true) .with_titlebar_shown(false) @@ -854,6 +960,7 @@ fn main() -> eframe::Result { Box::new(move |cc| { // 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()); // AI masks: the SAM 3 checkpoint (facebook/sam3) in /models/sam3, or LIGHTCRAFT_SAM3_DIR // (never required: without it, AI masks offer to download it; see docs/ai-masks.md) @@ -915,6 +1022,31 @@ fn main() -> eframe::Result { }), ) })); + if came_up.load(std::sync::atomic::Ordering::Relaxed) { + // the window was up: a panic that escaped it now is LightCraft stopping, typically the + // window's renderer after the graphics driver reset the GPU (issue #1337: egui-wgpu panics + // on the lost device). Not a failed start: no other renderer is tried, and the message + // says what happened. + let stopped = match started { + Ok(r) => r, + Err(_) => Err(eframe::Error::AppCreation("its window stopped drawing (the renderer panicked)".into())), + }; + if let Err(e) = &stopped { + let lost = gpu_lost_reason(); + if let Some(reason) = &lost + && !in_memory + { + record_gpu_lost(gpu_lost_marker_path().as_deref(), reason); + } + // (not "app creation error: …": the app had been created) + let what = match e { + eframe::Error::AppCreation(m) => m.to_string(), + e => e.to_string(), + }; + stopped_while_running(&stopped_text(&what, lost.as_deref()), log_file.as_deref()); + } + return stopped; + } // a panic that still escaped the renderer's start (the probe and error handler should // prevent it): an error like any other failed start let started = started.unwrap_or_else(|_| Err(eframe::Error::AppCreation("the window's renderer panicked while starting".into()))); @@ -929,6 +1061,34 @@ fn main() -> eframe::Result { started } +/// What to tell the user when LightCraft stopped while running (`error`), and why when the GPU +/// was lost (`lost`): GPU rendering is off from the next start. +fn stopped_text(error: &str, lost: Option<&str>) -> String { + match lost { + Some(reason) => format!( + "{error}. The graphics driver reset the GPU ({reason}). LightCraft renders on the CPU from its next start; \ + Settings ▸ Performance ▸ Use the GPU for rendering turns the GPU back on" + ), + None => error.to_string(), + } +} + +/// LightCraft stopped while its window was up: say so in the log and in a native message box +/// naming the log file (as [`startup_failed`] does for a start that failed). +fn stopped_while_running(text: &str, log_file: Option<&std::path::Path>) { + log::error!("LightCraft stopped: {text}"); + if std::env::var_os("LIGHTCRAFT_NO_PREFS").is_some() { + return; // tests and scripts: no dialog to click away + } + let (title, text) = lightcraft_ui_egui::i18n::stopped_message(text, log_file.map(|p| p.display().to_string()).as_deref()); + let _ = rfd::MessageDialog::new() + .set_level(rfd::MessageLevel::Error) + .set_title(title) + .set_description(text) + .set_buttons(rfd::MessageButtons::Ok) + .show(); +} + /// The window could not start (e.g. no usable graphics device): say so in the log and in a native /// message box naming the log file, instead of exiting without a trace — the release builds have /// no console on Windows, and an app started from Finder shows no standard error (issue #260). @@ -1131,6 +1291,37 @@ mod tests { let _ = std::fs::remove_dir_all(&d); } + /// Issue #1337: the window stopped drawing after the graphics driver reset the GPU during an + /// export, and the dialog said the window "could not open". The stop is recorded; the next + /// launch says why once and renders on the CPU (a `--memory` session leaves the record). + #[test] + fn a_gpu_lost_while_running_turns_the_gpu_off_next_time() { + let d = dir("gpu-lost"); + let m = d.join("gpu-lost.marker"); + assert_eq!(gpu_lost_check_at(Some(&m), false), None, "nothing recorded"); + assert_eq!(gpu_lost_check_at(None, false), None, "no settings folder"); + record_gpu_lost(Some(&m), "device lost (Unknown): Device is lost"); + let notice = gpu_lost_check_at(Some(&m), true).unwrap(); + assert!( + notice.contains("(device lost (Unknown): Device is lost)") + && notice.contains("GPU rendering is now off") + && notice.contains("Use the GPU for rendering"), + "{notice}" + ); + assert!(m.exists(), "a --memory session leaves it"); + assert!(gpu_lost_check_at(Some(&m), false).is_some()); + assert!(!m.exists()); + assert_eq!(gpu_lost_check_at(Some(&m), false), None, "said once"); + // the message box: LightCraft stopped (not "could not open its window"), and why + let text = stopped_text("its window stopped drawing (the renderer panicked)", Some("device lost (Unknown): Device is lost")); + assert!(text.contains("reset the GPU (device lost (Unknown)") && text.contains("renders on the CPU from its next start"), "{text}"); + assert_eq!(stopped_text("its window stopped drawing", None), "its window stopped drawing"); + let (title, body) = lightcraft_ui_egui::i18n::stopped_message(&text, Some("/logs/lightcraft.log")); + assert_eq!(title, "LightCraft stopped"); + assert!(body.starts_with("LightCraft stopped unexpectedly: its window stopped drawing") && body.ends_with("/logs/lightcraft.log"), "{body}"); + let _ = std::fs::remove_dir_all(&d); + } + /// Issue #136: a GPU init marker left by a crashed launch is reported once and removed. #[test] fn gpu_crash_marker_is_reported_once() { @@ -1237,6 +1428,96 @@ mod tests { let _ = std::fs::remove_dir_all(&d); } + /// Given what the settings remember of the window, when the app's window is built, then it + /// opens at that size, zoomed if it was, where the system puts it; with nothing remembered it + /// opens at the usual size. + #[test] + fn the_window_opens_as_it_was() { + use lightcraft_ui_egui::window_place::{DEFAULT_SIZE, WindowPlace}; + let fresh = as_remembered(egui::ViewportBuilder::default(), None); + assert_eq!((fresh.inner_size, fresh.position, fresh.maximized), (Some(egui::Vec2::from(DEFAULT_SIZE)), None, None)); + let b = as_remembered(egui::ViewportBuilder::default(), Some(WindowPlace { size: [1300.0, 800.0], zoomed: true })); + assert_eq!((b.inner_size, b.position, b.maximized), (Some(egui::vec2(1300.0, 800.0)), None, Some(true))); + // (not asked to un-zoom: macOS would then resize a window that fills its screen) + let b = as_remembered(egui::ViewportBuilder::default(), Some(WindowPlace { size: [1300.0, 800.0], zoomed: false })); + assert_eq!((b.inner_size, b.position, b.maximized), (Some(egui::vec2(1300.0, 800.0)), None, None)); + } + + /// Given a window that rests at a size, when the settings are written and read again, then + /// they hold how it was. A window in the file that is not one (a hand edit, another version's) + /// reads as nothing remembered and costs no other setting; and a window resized just before + /// the app quits is remembered at that size. + #[test] + fn the_settings_keep_how_the_window_was() { + use lightcraft_ui_egui::window_place::{Seen, WindowPlace}; + let d = dir("window"); + let path = d.join("ui.json"); + let mut app = LightcraftApp::new(lightcraft_engine::Session::with_demo(), Services::default()); + app.ui.settings.library_path = "/pictures/library".into(); + let mut writer = PrefsWriter { path: Some(path.clone()), ..Default::default() }; + writer.save(&app).unwrap(); + assert!(!std::fs::read_to_string(&path).unwrap().contains("\"window\""), "nothing to remember yet"); + let window = WindowPlace { size: [1300.0, 800.0], zoomed: true }; + app.ui.window = Some(window); + writer.save(&app).unwrap(); + let (restored, warning, _) = load_prefs_at(&path, false); + assert_eq!((restored.unwrap().window, warning), (Some(window), None)); + + // a window that is no window: the file is read all the same, with its other settings + let text = std::fs::read_to_string(&path).unwrap(); + for odd in [r#""window": {}"#, r#""window": "wide""#, r#""window": {"size": [1, 2, 3]}"#, r#""window": {"size": [null, 800.0]}"#] { + let start = text.find("\"window\"").unwrap(); + let end = start + text[start..].find('}').unwrap() + 1; + std::fs::write(&path, format!("{}{odd}{}", &text[..start], &text[end..])).unwrap(); + let (restored, warning, keep) = load_prefs_at(&path, true); + let restored = restored.unwrap_or_else(|| panic!("{odd}: {warning:?}")); + assert_eq!((restored.window, restored.settings.library_path.as_str(), warning, keep), (None, "/pictures/library", None, false), "{odd}"); + } + + // resized, and the app quits a moment later: the new size is what is written + let mut writer = PrefsWriter { path: Some(path.clone()), ..Default::default() }; + let rest = Some(WindowPlace { size: [1300.0, 800.0], zoomed: false }); + writer.window.note(rest, &Seen { size: Some([1500.0, 900.0]), ..Default::default() }, 0.0); + writer.window_since = std::time::Instant::now().checked_sub(std::time::Duration::from_millis(400)); + assert!(writer.window_since.is_some()); + assert_eq!(writer.window_at_exit(rest), Some(WindowPlace { size: [1500.0, 900.0], zoomed: false })); + assert_eq!(PrefsWriter::default().window_at_exit(rest), rest, "a window that was never seen"); + let _ = std::fs::remove_dir_all(&d); + } + + /// Given the app's window at a size, when the settings writer looks at it frame after frame, + /// then it asks to be woken while the size has still to rest, remembers the size once it has, + /// and asks for nothing more. A window in the settings that is not one (a script set it) is + /// put right before it is gone by. + #[test] + fn the_writer_follows_the_window_from_frame_to_frame() { + use lightcraft_ui_egui::window_place::WindowPlace; + let mut app = LightcraftApp::new(lightcraft_engine::Session::with_demo(), Services::default()); + let mut writer = PrefsWriter::default(); + let ctx = egui::Context::default(); + let pass = |look: &mut dyn FnMut(&egui::Context)| { + let raw = + egui::RawInput { screen_rect: Some(egui::Rect::from_min_size(egui::Pos2::ZERO, egui::vec2(1300.0, 800.0))), ..Default::default() }; + let mut out = ctx.run_ui(raw, |ui| look(ui.ctx())); + out.textures_delta.clear(); + out.viewport_output.get(&egui::ViewportId::ROOT).map(|v| v.repaint_delay) + }; + // (egui's own first frames ask for another: let them pass) + for _ in 0..3 { + pass(&mut |_| {}); + } + app.ui.window = Some(WindowPlace { size: [f32::NAN, 800.0], zoomed: true }); + let asked = pass(&mut |ctx| writer.tick(&mut app, ctx)); + assert_eq!(app.ui.window, None, "no window, and not yet at rest"); + assert!(asked.is_some_and(|wait| wait <= std::time::Duration::from_secs(1)), "{asked:?}"); + // a second later + writer.window_since = writer.window_since.and_then(|since| since.checked_sub(std::time::Duration::from_secs(1))); + pass(&mut |ctx| writer.tick(&mut app, ctx)); + assert_eq!(app.ui.window, Some(WindowPlace { size: [1300.0, 800.0], zoomed: false })); + let asked = pass(&mut |ctx| writer.tick(&mut app, ctx)); + assert!(asked.is_none_or(|wait| wait > std::time::Duration::from_secs(3600)), "at rest and remembered: {asked:?}"); + } + /// #1331: on macOS the process ends inside `on_exit`, before eframe would tear the window down. /// The check runs in a child (this test binary again), since on macOS it ends the process. #[test] diff --git a/apps/lightcraft/src/native_menu.rs b/apps/lightcraft/src/native_menu.rs index 256faa1f8..4ac40bcac 100644 --- a/apps/lightcraft/src/native_menu.rs +++ b/apps/lightcraft/src/native_menu.rs @@ -26,8 +26,9 @@ const QUIT: &str = "app.quit"; const SETTINGS: &str = "app.settings"; const SETTINGS_KEY: &str = "Cmd+,"; /// Keys whose meaning depends on the panel (X = reject or swap crop aspect, ⌫ = delete the photo -/// or the active mask): left to the egui handler, which knows the context. -const CONTEXTUAL: &[&str] = &["X", "Delete"]; +/// or the active mask; ⌘↑/⌘↓ flag status follows Compare/Survey's active photo): left to the egui +/// handler, which knows the context. +const CONTEXTUAL: &[&str] = &["X", "Delete", "Cmd+Up", "Cmd+Down"]; /// Shortcuts text fields need while they have focus. const TEXT_EDIT: &[&str] = &["Cmd+A", "Cmd+C", "Cmd+V", "Cmd+X", "Cmd+Z", "Cmd+Shift+Z"]; @@ -126,6 +127,7 @@ fn accelerator(sc: &str) -> Option { "=" => Code::Equal, "-" => Code::Minus, "'" => Code::Quote, + "`" => Code::Backquote, "," => Code::Comma, "Delete" => Code::Backspace, // with modifiers only: a bare Enter belongs to text fields and tools diff --git a/assets/ATTRIBUTION.md b/assets/ATTRIBUTION.md index a5e1716d7..30f070f4a 100644 --- a/assets/ATTRIBUTION.md +++ b/assets/ATTRIBUTION.md @@ -12,6 +12,7 @@ covers a whole directory), or if a licence file referenced in the Licence column | `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 | | `packaging/macos/dmg/` (`background.svg`, the `background.tiff` rendered from it by `generate.py`, and the `dmg-layout.DS_Store` layout file) | LightCraft macOS DMG window background | @XusBadia | original work for this repository, derived from the LightCraft app icon (`assets/app-icon/lightcraft.svg`, referenced via ``, not copied); text is outlined Inter / JetBrains Mono glyph paths | MIT OR Apache-2.0 | 2026-10-08 | none (original); used for the Finder window of the macOS DMG, `packaging/macos/dmg/generate.py` renders it to `background.tiff` | +| `assets/camera-profiles/ILCA-99M2.json` | Sony ILCA-99M2 camera colour profile (matrix + hue/saturation/value table), built into LightCraft | LightCraft contributors; source photographs supplied by @PiMinusMax | original work: fitted by `lightcraft-cli calibrate` to 144 usable ILCA-99M2 ARWs (of 150, seven shooting days, Standard creative style) 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-11 | none | | `assets/camera-profiles/ILCE-7CR.json` | Sony ILCE-7CR camera colour profile (matrix + hue/saturation/value table), built into LightCraft | LightCraft contributors; source photographs supplied by @AlexChambers | original work: fitted by `lightcraft-cli calibrate` to 125 usable ILCE-7CR ARWs (of 131, September 2025 to September 2026, Standard creative style) 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-09 | none | | `assets/camera-profiles/X-H2S.json`, `assets/camera-profiles/X-T4.json` | Fujifilm camera colour profiles (matrix + hue/saturation/value table), built into LightCraft | LightCraft contributors; source photographs supplied by Sebastian Pfluegelmeier | original work: fitted by `lightcraft-cli calibrate` to 201 X-H2S and 545 X-T4 RAFs 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-08 | none | | `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 | @@ -19,14 +20,16 @@ covers a whole directory), or if a licence file referenced in the Licence column | `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 | | `crates/ui-egui/src/icons.rs` | UI icons drawn as vector paths in code | LightCraft contributors | original work | MIT OR Apache-2.0 | 2026-09-30 | n/a | +| `crates/ui-egui/src/panels/person.rs` | Up arrow on the People page bulk-confirm control, drawn in code | LightCraft contributors | original work | MIT OR Apache-2.0 | 2026-10-13 | n/a | | `docs/images/*-tetons.jpg`, `docs/images/grid-pd.jpg` (screenshots containing it) | "The Tetons and the Snake River" | Ansel Adams (1942), U.S. National Archives | https://commons.wikimedia.org/wiki/File:Adams_The_Tetons_and_the_Snake_River.jpg | Public domain (U.S. federal government work) | 2026-09-30 | developed in LightCraft; shown inside app screenshots | | `docs/images/*-migrant-mother.jpg`, `docs/images/grid-pd.jpg` | "Migrant Mother" | Dorothea Lange (1936), Farm Security Administration / Library of Congress | https://commons.wikimedia.org/wiki/File:Lange-MigrantMother02.jpg | Public domain (U.S. federal government work) | 2026-09-30 | developed in LightCraft; shown inside app screenshots | | `docs/images/*-earthrise.jpg`, `docs/images/grid-pd.jpg` | "Earthrise" (AS08-14-2383) | NASA / Bill Anders, Apollo 8 (1968) | https://commons.wikimedia.org/wiki/File:NASA-Apollo8-Dec24-Earthrise.jpg | Public domain (NASA) | 2026-09-30 | developed in LightCraft; shown inside app screenshots | | `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 | 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-.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 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 | 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-.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 | | `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 | Notes - Fonts authored or published by Adobe (Source Sans/Serif/Code, Source Han, …) are not used, even though some are diff --git a/assets/camera-profiles/ILCA-99M2.json b/assets/camera-profiles/ILCA-99M2.json new file mode 100644 index 000000000..ff18a63c1 --- /dev/null +++ b/assets/camera-profiles/ILCA-99M2.json @@ -0,0 +1,1819 @@ +{ + "version": 1, + "model": "ILCA-99M2", + "files": 144, + "samples": 501511, + "matrix": [ + [1.1432775892083846, -0.27864150084892714, 0.13786480817829982], + [-0.05589204732762987, 1.1699605558717394, -0.11694884297799035], + [0.004313413220749851, -0.7088386949439993, 1.7263782534882033] + ], + "hue_sat": { + "hue_divisions": 72, + "sat_divisions": 5, + "val_divisions": 5, + "data": [ + [0.0, 1.0, 1.0], + [2.804254, 1.2235245, 1.0], + [5.0115886, 1.2228547, 1.0], + [5.1745224, 1.1897815, 1.0], + [3.4398973, 1.0871476, 1.0], + [0.0, 1.0, 1.0], + [2.1009562, 1.231483, 1.0], + [3.2371128, 1.1828748, 1.0], + [3.6905267, 1.1272595, 1.0], + [3.2581418, 1.0763286, 1.0], + [0.0, 1.0, 1.0], + [1.3609977, 1.2428942, 1.0], + [1.1722605, 1.1633604, 1.0], + [1.2722441, 1.0881792, 1.0], + [0.99433255, 1.026622, 1.0], + [0.0, 1.0, 1.0], + [-0.12375347, 1.2171124, 1.0], + [-0.6004144, 1.1662252, 1.0], + [-1.2418907, 1.1001159, 1.0], + [-1.3929275, 1.017488, 1.0], + [0.0, 1.0, 1.0], + [-1.4741973, 1.1985689, 1.0], + [-2.3110185, 1.1587616, 1.0], + [-3.2606716, 1.1098144, 1.0], + [-3.9140587, 1.0290464, 1.0], + [0.0, 1.0, 1.0], + [-2.748514, 1.1680661, 1.0], + [-3.8273644, 1.1325561, 1.0], + [-4.7215014, 1.0942773, 1.0], + [-5.6006517, 1.0109093, 1.0], + [0.0, 1.0, 1.0], + [-3.6385696, 1.1548748, 1.0], + [-4.8762474, 1.106731, 1.0], + [-5.845225, 1.0598869, 1.0], + [-6.7829485, 0.9773247, 1.0], + [0.0, 1.0, 1.0], + [-4.4043136, 1.1618558, 1.0], + [-5.663378, 1.1124074, 1.0], + [-6.676991, 1.0622271, 1.0], + [-7.2388797, 0.9792954, 1.0], + [0.0, 1.0, 1.0], + [-4.5636735, 1.1730139, 1.0], + [-5.5787325, 1.131743, 1.0], + [-6.434246, 1.0818648, 1.0], + [-7.713659, 0.977632, 1.0], + [0.0, 1.0, 1.0], + [-4.1642222, 1.1961858, 1.0], + [-5.135349, 1.1508327, 1.0], + [-5.9561267, 1.1025041, 1.0], + [-7.8126693, 0.9789329, 1.0], + [0.0, 1.0, 1.0], + [-3.3540795, 1.2313552, 1.0], + [-4.0874486, 1.1859667, 1.0], + [-5.090505, 1.1065466, 1.0], + [-6.980868, 0.94789493, 1.0], + [0.0, 1.0, 1.0], + [-3.2277284, 1.2495877, 1.0], + [-3.6526108, 1.1937634, 1.0], + [-3.5733712, 1.0832378, 1.0], + [-5.550715, 0.9253031, 1.0], + [0.0, 1.0, 1.0], + [-3.2758887, 1.2372755, 1.0], + [-2.8500462, 1.1516474, 1.0], + [-1.8038367, 1.0081928, 1.0], + [-2.1356683, 0.93026036, 1.0], + [0.0, 1.0, 1.0], + [-2.842822, 1.1995343, 1.0], + [-1.5608833, 1.0880193, 1.0], + [-0.6414115, 0.96593964, 1.0], + [-0.8170321, 0.9045082, 1.0], + [0.0, 1.0, 1.0], + [-1.8589126, 1.1368302, 1.0], + [-0.5850774, 1.0399925, 1.0], + [-0.4096108, 0.94941473, 1.0], + [-0.67716926, 0.89125115, 1.0], + [0.0, 1.0, 1.0], + [-0.50222045, 1.0790738, 1.0], + [0.13412993, 1.0091311, 1.0], + [0.04161173, 0.94268256, 1.0], + [0.21809827, 0.89096487, 1.0], + [0.0, 1.0, 1.0], + [0.5395973, 1.0523564, 1.0], + [0.68198884, 0.9942523, 1.0], + [0.8921194, 0.93612367, 1.0], + [1.5682732, 0.8875503, 1.0], + [0.0, 1.0, 1.0], + [1.4230179, 1.0399433, 1.0], + [1.7522293, 0.98858124, 1.0], + [2.120758, 0.93895113, 1.0], + [2.7880068, 0.8917508, 1.0], + [0.0, 1.0, 1.0], + [2.485209, 1.0362785, 1.0], + [3.2553241, 0.9826427, 1.0], + [3.6980822, 0.9420671, 1.0], + [3.6589043, 0.9088622, 1.0], + [0.0, 1.0, 1.0], + [3.8520021, 1.0283107, 1.0], + [4.4029727, 0.97790647, 1.0], + [4.6192904, 0.941655, 1.0], + [3.3324108, 0.9242873, 1.0], + [0.0, 1.0, 1.0], + [4.579471, 1.0396363, 1.0], + [5.1236196, 0.99359185, 1.0], + [4.4844737, 0.9637591, 1.0], + [0.93364024, 0.9805732, 1.0], + [0.0, 1.0, 1.0], + [5.6343756, 1.0391579, 1.0], + [5.9764285, 0.9963135, 1.0], + [4.2140217, 0.97128063, 1.0], + [0.31541073, 0.99498296, 1.0], + [0.0, 1.0, 1.0], + [6.3622503, 1.0628132, 1.0], + [5.916998, 1.0203458, 1.0], + [1.3126858, 0.9978764, 1.0], + [-0.040236503, 0.99987936, 1.0], + [0.0, 1.0, 1.0], + [6.1092844, 1.1148107, 1.0], + [5.2341003, 1.0724294, 1.0], + [0.57583106, 1.0097842, 1.0], + [-0.004625028, 1.0000128, 1.0], + [0.0, 1.0, 1.0], + [5.6949396, 1.1773212, 1.0], + [3.8311253, 1.1038187, 1.0], + [-0.11847352, 1.0086719, 1.0], + [-0.0105433175, 1.000084, 1.0], + [0.0, 1.0, 1.0], + [4.546873, 1.1809658, 1.0], + [0.6606819, 1.0607728, 1.0], + [-1.8319762, 0.98954993, 1.0], + [-0.11198713, 0.9991004, 1.0], + [0.0, 1.0, 1.0], + [2.975616, 1.171921, 1.0], + [-2.6058009, 1.0177505, 1.0], + [-5.031273, 0.9486056, 1.0], + [-0.71531844, 0.99169016, 1.0], + [0.0, 1.0, 1.0], + [2.3296447, 1.1538781, 1.0], + [-4.7061315, 0.9983956, 1.0], + [-7.1538033, 0.9327583, 1.0], + [-1.329432, 0.9859865, 1.0], + [0.0, 1.0, 1.0], + [1.33782, 1.1317362, 1.0], + [-5.738668, 0.9846136, 1.0], + [-8.032541, 0.93113464, 1.0], + [-1.414409, 0.9868347, 1.0], + [0.0, 1.0, 1.0], + [1.1102281, 1.1464978, 1.0], + [-4.347359, 1.0098535, 1.0], + [-5.8042417, 0.9558565, 1.0], + [-0.65224177, 0.99453866, 1.0], + [0.0, 1.0, 1.0], + [1.3978106, 1.1282454, 1.0], + [-2.5084882, 1.0191262, 1.0], + [-2.5297656, 0.9832681, 1.0], + [-0.15988652, 0.99876547, 1.0], + [0.0, 1.0, 1.0], + [1.6285329, 1.1082805, 1.0], + [-1.8376639, 1.0320127, 1.0], + [-1.5057008, 0.99416333, 1.0], + [-0.07763386, 0.99956375, 1.0], + [0.0, 1.0, 1.0], + [0.47474164, 1.1118255, 1.0], + [-2.2814848, 1.0398426, 1.0], + [-0.96154535, 0.9967108, 1.0], + [-0.038478848, 0.99972326, 1.0], + [0.0, 1.0, 1.0], + [-0.28353742, 1.1260961, 1.0], + [-2.6729789, 1.0359904, 1.0], + [-0.76547706, 0.9970432, 1.0], + [-0.02355436, 0.99970925, 1.0], + [0.0, 1.0, 1.0], + [0.65706646, 1.1279969, 1.0], + [-2.8462512, 1.0391037, 1.0], + [-0.5868237, 1.0005401, 1.0], + [-0.010716562, 0.9999053, 1.0], + [0.0, 1.0, 1.0], + [0.12108033, 1.1502995, 1.0], + [-1.9704113, 1.0370626, 1.0], + [-0.63179135, 1.0001434, 1.0], + [-0.015800219, 0.99995714, 1.0], + [0.0, 1.0, 1.0], + [1.0309358, 1.1655595, 1.0], + [0.13250555, 1.0445937, 1.0], + [-0.07715364, 1.0009365, 1.0], + [-0.0021050358, 0.99999684, 1.0], + [0.0, 1.0, 1.0], + [-0.2827152, 1.1324676, 1.0], + [0.014774211, 1.0343506, 1.0], + [-0.019916466, 1.0006523, 1.0], + [-0.00040274212, 1.0000006, 1.0], + [0.0, 1.0, 1.0], + [-2.2018666, 1.1418259, 1.0], + [-1.4933577, 1.0408006, 1.0], + [-0.3667708, 1.0002662, 1.0], + [-0.007263095, 0.9999904, 1.0], + [0.0, 1.0, 1.0], + [-2.64397, 1.1194679, 1.0], + [-1.6423713, 1.0442007, 1.0], + [-0.20484583, 1.0007143, 1.0], + [-0.0025689714, 0.99998134, 1.0], + [0.0, 1.0, 1.0], + [-3.8773065, 1.110005, 1.0], + [-2.6916754, 1.0417831, 1.0], + [-0.33602908, 1.0002605, 1.0], + [-0.0021417958, 0.9999703, 1.0], + [0.0, 1.0, 1.0], + [-3.714574, 1.0977588, 1.0], + [-3.5595996, 1.0386114, 1.0], + [-1.5827396, 1.0060165, 1.0], + [-0.028114408, 1.0000618, 1.0], + [0.0, 1.0, 1.0], + [-3.2299962, 1.0955467, 1.0], + [-3.3268821, 1.0478108, 1.0], + [-1.9395999, 1.0131217, 1.0], + [-0.075512886, 1.0002799, 1.0], + [0.0, 1.0, 1.0], + [-1.336435, 1.0960432, 1.0], + [-0.8794556, 1.0795935, 1.0], + [-0.29321554, 1.0271318, 1.0], + [0.00032340753, 1.0008185, 1.0], + [0.0, 1.0, 1.0], + [0.09420732, 1.1007514, 1.0], + [0.45092306, 1.0983796, 1.0], + [1.0769465, 1.0420003, 1.0], + [0.22761619, 1.0022283, 1.0], + [0.0, 1.0, 1.0], + [0.71772265, 1.1304002, 1.0], + [1.5783058, 1.1197016, 1.0], + [2.8271558, 1.0629567, 1.0], + [1.0452774, 1.0075884, 1.0], + [0.0, 1.0, 1.0], + [1.7700194, 1.1402828, 1.0], + [2.267522, 1.1383678, 1.0], + [2.3750477, 1.0799156, 1.0], + [0.46060455, 0.99580747, 1.0], + [0.0, 1.0, 1.0], + [2.9440982, 1.1368023, 1.0], + [3.6898718, 1.1418967, 1.0], + [3.8923314, 1.1259586, 1.0], + [1.2024251, 1.0325387, 1.0], + [0.0, 1.0, 1.0], + [4.2370567, 1.1504492, 1.0], + [4.6764765, 1.1490715, 1.0], + [3.910422, 1.1321807, 1.0], + [0.89606607, 1.0308274, 1.0], + [0.0, 1.0, 1.0], + [4.9599085, 1.134546, 1.0], + [5.1401796, 1.1103181, 1.0], + [3.7372687, 1.0921954, 1.0], + [0.55078083, 1.021753, 1.0], + [0.0, 1.0, 1.0], + [5.7153788, 1.1273357, 1.0], + [5.978767, 1.1330243, 1.0], + [3.9994862, 1.1207391, 1.0], + [0.36665595, 1.0121281, 1.0], + [0.0, 1.0, 1.0], + [6.2697597, 1.1095688, 1.0], + [5.6365104, 1.1205149, 1.0], + [3.4179032, 1.1214768, 1.0], + [0.26656342, 1.0103098, 1.0], + [0.0, 1.0, 1.0], + [6.1428804, 1.0995386, 1.0], + [3.956236, 1.1270815, 1.0], + [1.9768763, 1.1226226, 1.0], + [0.11463216, 1.0099398, 1.0], + [0.0, 1.0, 1.0], + [6.929935, 1.1041147, 1.0], + [3.3182452, 1.1096137, 1.0], + [1.2072796, 1.0818946, 1.0], + [-0.01571625, 1.005663, 1.0], + [0.0, 1.0, 1.0], + [7.265804, 1.1154059, 1.0], + [3.7104347, 1.0477185, 1.0], + [1.056846, 0.9974326, 1.0], + [0.03417005, 0.9998331, 1.0], + [0.0, 1.0, 1.0], + [7.849703, 1.1269237, 1.0], + [4.3993697, 1.0325269, 1.0], + [1.3606595, 0.99489033, 1.0], + [0.047941446, 0.99993676, 1.0], + [0.0, 1.0, 1.0], + [8.439311, 1.1298556, 1.0], + [3.303848, 1.0538055, 1.0], + [0.59302783, 1.0085934, 1.0], + [0.025294233, 1.0004109, 1.0], + [0.0, 1.0, 1.0], + [8.608997, 1.1400498, 1.0], + [3.3591669, 1.0461109, 1.0], + [0.40004572, 1.0048742, 1.0], + [0.01262109, 1.0001965, 1.0], + [0.0, 1.0, 1.0], + [6.761172, 1.1502578, 1.0], + [2.1108382, 1.0369791, 1.0], + [0.11530293, 1.0012623, 1.0], + [0.001693561, 1.0000176, 1.0], + [0.0, 1.0, 1.0], + [5.5255957, 1.1716747, 1.0], + [1.0459789, 1.037805, 1.0], + [0.09180857, 1.001637, 1.0], + [0.003279748, 1.0000362, 1.0], + [0.0, 1.0, 1.0], + [5.1823792, 1.1748279, 1.0], + [1.723147, 1.0416057, 1.0], + [0.2180456, 1.0030037, 1.0], + [0.005272273, 1.0000591, 1.0], + [0.0, 1.0, 1.0], + [5.7087555, 1.1780843, 1.0], + [2.619079, 1.0446409, 1.0], + [0.35640785, 1.0032725, 1.0], + [0.010388461, 1.0001107, 1.0], + [0.0, 1.0, 1.0], + [6.8262243, 1.1712428, 1.0], + [3.972946, 1.0097475, 1.0], + [0.6286015, 0.9970032, 1.0], + [0.017907226, 1.0001063, 1.0], + [0.0, 1.0, 1.0], + [6.524165, 1.182861, 1.0], + [3.200896, 1.0579331, 1.0], + [0.28860277, 1.0027157, 1.0], + [0.0036479658, 1.0000447, 1.0], + [0.0, 1.0, 1.0], + [5.831539, 1.1791148, 1.0], + [2.774682, 1.0702703, 1.0], + [0.35563686, 1.0059416, 1.0], + [0.0069925115, 1.0001086, 1.0], + [0.0, 1.0, 1.0], + [7.6593747, 1.1839095, 1.0], + [2.9386632, 1.0513804, 1.0], + [0.23482445, 0.999349, 1.0], + [0.0063177217, 1.0000361, 1.0], + [0.0, 1.0, 1.0], + [7.268381, 1.1659219, 1.0], + [2.5044994, 1.0421095, 1.0], + [0.20415363, 0.99924254, 1.0], + [0.0059488467, 1.0000399, 1.0], + [0.0, 1.0, 1.0], + [7.236816, 1.1485709, 1.0], + [4.5540986, 1.06799, 1.0], + [1.4449328, 1.0170963, 1.0], + [0.0639702, 1.0006945, 1.0], + [0.0, 1.0, 1.0], + [6.453048, 1.1589189, 1.0], + [6.3102107, 1.1038384, 1.0], + [3.1707234, 1.032528, 1.0], + [0.19751447, 1.00155, 1.0], + [0.0, 1.0, 1.0], + [6.372379, 1.202752, 1.0], + [7.4304466, 1.1593263, 1.0], + [5.6761837, 1.0836742, 1.0], + [0.6670318, 1.0070612, 1.0], + [0.0, 1.0, 1.0], + [5.3508396, 1.1789645, 1.0], + [7.0611863, 1.1524564, 1.0], + [7.2240868, 1.1157393, 1.0], + [2.2516217, 1.0274429, 1.0], + [0.0, 1.0, 1.0], + [4.5282054, 1.1895584, 1.0], + [6.615412, 1.2008698, 1.0], + [7.1562805, 1.1773871, 1.0], + [3.693685, 1.0640011, 1.0], + [0.0, 1.0, 1.0], + [2.989149, 1.1666261, 1.0], + [5.0449367, 1.163257, 1.0], + [5.3947783, 1.1525968, 1.0], + [4.8617563, 1.1140602, 1.0], + [0.0, 1.0, 1.0], + [2.4345527, 1.1707076, 1.0], + [4.030044, 1.1360824, 1.0], + [4.400234, 1.1141994, 1.0], + [3.9981327, 1.0895209, 1.0], + [0.0, 1.0, 1.0], + [1.6249478, 1.1449721, 1.0], + [2.3648932, 1.0950501, 1.0], + [2.5787578, 1.0738068, 1.0], + [1.557968, 1.048907, 1.0], + [0.0, 1.0, 1.0], + [1.1732466, 1.1023636, 1.0], + [1.5519327, 1.0694385, 1.0], + [0.8835925, 1.0562818, 1.0], + [-0.95382243, 1.0234519, 1.0], + [0.0, 1.0, 1.0], + [0.54519135, 1.0728073, 1.0], + [0.7171778, 1.047036, 1.0], + [-0.4340524, 1.0440289, 1.0], + [-3.1891012, 1.0054655, 1.0], + [0.0, 1.0, 1.0], + [-0.81599987, 1.0807294, 1.0], + [-1.1246754, 1.0611808, 1.0], + [-2.2497585, 1.0415246, 1.0], + [-4.411367, 0.973935, 1.0], + [0.0, 1.0, 1.0], + [-1.7861705, 1.0760409, 1.0], + [-2.835779, 1.0522164, 1.0], + [-3.9953759, 1.0148282, 1.0], + [-4.4476047, 0.9319428, 1.0], + [0.0, 1.0, 1.0], + [-2.4466016, 1.0813212, 1.0], + [-3.5611944, 1.0451691, 1.0], + [-4.5130477, 1.011627, 1.0], + [-4.224515, 0.92788553, 1.0], + [0.0, 1.0, 1.0], + [-3.003385, 1.1208798, 1.0], + [-4.1261635, 1.0943259, 1.0], + [-5.189995, 1.0589299, 1.0], + [-6.5665016, 0.95204496, 1.0], + [0.0, 1.0, 1.0], + [-2.5737576, 1.1543715, 1.0], + [-3.583895, 1.1235094, 1.0], + [-4.758327, 1.0856714, 1.0], + [-6.4036903, 0.95784587, 1.0], + [0.0, 1.0, 1.0], + [-1.783091, 1.1985654, 1.0], + [-2.344906, 1.1536651, 1.0], + [-3.4222968, 1.0852684, 1.0], + [-5.1206603, 0.9148598, 1.0], + [0.0, 1.0, 1.0], + [-1.8261324, 1.2225533, 1.0], + [-2.3288019, 1.1683235, 1.0], + [-1.7856505, 1.0476049, 1.0], + [-4.6820045, 0.88435495, 1.0], + [0.0, 1.0, 1.0], + [-1.9707233, 1.2195381, 1.0], + [-0.81782085, 1.122641, 1.0], + [0.9111474, 0.9622587, 1.0], + [-1.1497722, 0.88565606, 1.0], + [0.0, 1.0, 1.0], + [-2.09956, 1.2140585, 1.0], + [0.056621242, 1.0911676, 1.0], + [1.0300432, 0.9330744, 1.0], + [-0.18233444, 0.8674556, 1.0], + [0.0, 1.0, 1.0], + [-0.7154876, 1.144233, 1.0], + [1.4851263, 1.0231369, 1.0], + [0.555909, 0.9251492, 1.0], + [-0.18147656, 0.8636336, 1.0], + [0.0, 1.0, 1.0], + [1.3398509, 1.0599889, 1.0], + [1.6690949, 0.9969751, 1.0], + [0.37584245, 0.92953223, 1.0], + [0.07826649, 0.87178516, 1.0], + [0.0, 1.0, 1.0], + [2.508692, 1.0410949, 1.0], + [1.8833909, 0.99179685, 1.0], + [1.2507737, 0.9335844, 1.0], + [1.6161711, 0.8835863, 1.0], + [0.0, 1.0, 1.0], + [3.1758716, 1.0253911, 1.0], + [2.4910228, 0.9809797, 1.0], + [2.236299, 0.9303089, 1.0], + [2.6537087, 0.8825234, 1.0], + [0.0, 1.0, 1.0], + [3.6103837, 1.0229514, 1.0], + [3.1707804, 0.97286725, 1.0], + [3.3409152, 0.9399243, 1.0], + [3.5829213, 0.9052561, 1.0], + [0.0, 1.0, 1.0], + [4.3082023, 1.0349113, 1.0], + [4.284679, 0.9839242, 1.0], + [4.3494177, 0.9484854, 1.0], + [3.8492424, 0.9255855, 1.0], + [0.0, 1.0, 1.0], + [4.793538, 1.0573108, 1.0], + [5.6492386, 0.98787755, 1.0], + [6.214179, 0.91949385, 1.0], + [3.9530585, 0.92670876, 1.0], + [0.0, 1.0, 1.0], + [5.384946, 1.070319, 1.0], + [6.3971586, 0.98128706, 1.0], + [6.9482284, 0.89684695, 1.0], + [4.2139463, 0.9239156, 1.0], + [0.0, 1.0, 1.0], + [5.509941, 1.109065, 1.0], + [4.31208, 1.0552181, 1.0], + [-1.2347931, 0.9985642, 1.0], + [-0.86304265, 0.9993037, 1.0], + [0.0, 1.0, 1.0], + [5.1984997, 1.162435, 1.0], + [3.29675, 1.1169462, 1.0], + [-2.0880382, 1.0190989, 1.0], + [-0.5265214, 0.99782705, 1.0], + [0.0, 1.0, 1.0], + [4.2004547, 1.2332976, 1.0], + [2.4406395, 1.1688915, 1.0], + [-0.54659694, 1.020866, 1.0], + [-0.039971404, 0.99981636, 1.0], + [0.0, 1.0, 1.0], + [2.9126167, 1.2407047, 1.0], + [-0.152128, 1.1377726, 1.0], + [-2.4389071, 0.9923629, 1.0], + [-0.18998405, 0.99835, 1.0], + [0.0, 1.0, 1.0], + [0.832136, 1.2248999, 1.0], + [-4.439477, 1.0757953, 1.0], + [-6.5581284, 0.94083136, 1.0], + [-1.3619823, 0.98418033, 1.0], + [0.0, 1.0, 1.0], + [-0.8086253, 1.2119304, 1.0], + [-6.9719944, 1.0509604, 1.0], + [-8.899021, 0.9207292, 1.0], + [-2.5299528, 0.97328454, 1.0], + [0.0, 1.0, 1.0], + [-1.2520319, 1.1828909, 1.0], + [-7.6938405, 1.0309292, 1.0], + [-9.733912, 0.9186155, 1.0], + [-2.4881692, 0.97676903, 1.0], + [0.0, 1.0, 1.0], + [-0.6646547, 1.1841325, 1.0], + [-6.6125364, 1.0378011, 1.0], + [-7.26035, 0.942406, 1.0], + [-1.0572171, 0.99080205, 1.0], + [0.0, 1.0, 1.0], + [0.598152, 1.1774639, 1.0], + [-4.97536, 1.0513587, 1.0], + [-3.3090515, 0.9811577, 1.0], + [-0.20941074, 0.9982669, 1.0], + [0.0, 1.0, 1.0], + [0.010846901, 1.1766325, 1.0], + [-5.117737, 1.0593848, 1.0], + [-2.631352, 0.99419326, 1.0], + [-0.11850837, 0.9993512, 1.0], + [0.0, 1.0, 1.0], + [-1.5248753, 1.1829028, 1.0], + [-6.0299883, 1.0577121, 1.0], + [-2.215714, 0.99519426, 1.0], + [-0.067606665, 0.9995176, 1.0], + [0.0, 1.0, 1.0], + [-1.1450118, 1.1907917, 1.0], + [-5.886881, 1.0472964, 1.0], + [-2.5883603, 0.98812264, 1.0], + [-0.085475355, 0.9989431, 1.0], + [0.0, 1.0, 1.0], + [0.39547902, 1.2007595, 1.0], + [-4.610833, 1.0562892, 1.0], + [-1.7052215, 0.99749553, 1.0], + [-0.037703, 0.9996055, 1.0], + [0.0, 1.0, 1.0], + [-0.01975878, 1.2410866, 1.0], + [-1.2528956, 1.080659, 1.0], + [-0.68467104, 1.0020955, 1.0], + [-0.019976415, 0.99990904, 1.0], + [0.0, 1.0, 1.0], + [-0.2921033, 1.2522528, 1.0], + [0.5268386, 1.0997523, 1.0], + [0.03676964, 1.0044748, 1.0], + [-0.0015876454, 1.0000012, 1.0], + [0.0, 1.0, 1.0], + [-2.6588416, 1.1782904, 1.0], + [-0.8304472, 1.0511743, 1.0], + [-0.09593859, 1.0008658, 1.0], + [-0.0012349889, 0.9999909, 1.0], + [0.0, 1.0, 1.0], + [-5.5003, 1.1401259, 1.0], + [-2.4005525, 1.002213, 1.0], + [-0.7121907, 0.98226595, 1.0], + [-0.017461486, 0.999597, 1.0], + [0.0, 1.0, 1.0], + [-5.994106, 1.0949768, 1.0], + [-2.85081, 1.0083107, 1.0], + [-0.30721804, 0.9763625, 1.0], + [-0.0013189864, 0.9991703, 1.0], + [0.0, 1.0, 1.0], + [-5.196782, 1.1068348, 1.0], + [-3.2161317, 1.0495526, 1.0], + [-1.099006, 0.99147415, 1.0], + [-0.024919514, 0.9993193, 1.0], + [0.0, 1.0, 1.0], + [-4.84915, 1.05568, 1.0], + [-3.9007847, 1.03011, 1.0], + [-2.599225, 1.0099269, 1.0], + [-0.11461259, 0.99996513, 1.0], + [0.0, 1.0, 1.0], + [-4.721844, 1.055208, 1.0], + [-3.9854348, 1.0393229, 1.0], + [-2.5744975, 1.0209645, 1.0], + [-0.293057, 1.001174, 1.0], + [0.0, 1.0, 1.0], + [-4.059898, 1.0536826, 1.0], + [-2.5768554, 1.0555562, 1.0], + [-1.39879, 1.0136101, 1.0], + [-0.50610787, 0.9911003, 1.0], + [0.0, 1.0, 1.0], + [-2.9069445, 1.0556097, 1.0], + [-1.5781095, 1.0518695, 1.0], + [-0.8278498, 1.001524, 1.0], + [-0.32421526, 0.98277986, 1.0], + [0.0, 1.0, 1.0], + [-1.4363952, 1.0812076, 1.0], + [0.5487326, 1.0899016, 1.0], + [2.4110415, 1.0404066, 1.0], + [2.9115984, 1.0175672, 1.0], + [0.0, 1.0, 1.0], + [0.008678005, 1.1017222, 1.0], + [1.2356163, 1.1221837, 1.0], + [2.8750088, 1.0839373, 1.0], + [3.1348855, 1.032868, 1.0], + [0.0, 1.0, 1.0], + [0.88217217, 1.091444, 1.0], + [2.0927439, 1.1262348, 1.0], + [3.240285, 1.1331868, 1.0], + [2.67113, 1.0834583, 1.0], + [0.0, 1.0, 1.0], + [2.428248, 1.095952, 1.0], + [3.390544, 1.1449028, 1.0], + [3.5510561, 1.1565588, 1.0], + [2.31717, 1.0987086, 1.0], + [0.0, 1.0, 1.0], + [4.1580663, 1.0814137, 1.0], + [4.4028983, 1.1175768, 1.0], + [3.174666, 1.1427947, 1.0], + [2.0205822, 1.1122999, 1.0], + [0.0, 1.0, 1.0], + [6.2129626, 1.0714386, 1.0], + [6.590155, 1.0948668, 1.0], + [4.619345, 1.1303223, 1.0], + [1.1349491, 1.0543766, 1.0], + [0.0, 1.0, 1.0], + [6.7700667, 1.0417291, 1.0], + [6.674038, 1.0694815, 1.0], + [3.9405415, 1.1325624, 1.0], + [0.40215716, 1.0195748, 1.0], + [0.0, 1.0, 1.0], + [7.373421, 1.0177879, 1.0], + [5.4952292, 1.081058, 1.0], + [2.1721451, 1.1446264, 1.0], + [0.14383824, 1.0215358, 1.0], + [0.0, 1.0, 1.0], + [8.597993, 1.0280006, 1.0], + [4.9888263, 1.084475, 1.0], + [1.9800822, 1.1122874, 1.0], + [-0.23990147, 1.0166304, 1.0], + [0.0, 1.0, 1.0], + [9.2620325, 1.0467869, 1.0], + [6.4645605, 1.0722194, 1.0], + [2.8058643, 1.069191, 1.0], + [0.13071911, 1.005048, 1.0], + [0.0, 1.0, 1.0], + [9.7541, 1.079291, 1.0], + [7.8130198, 1.0582371, 1.0], + [3.0989928, 1.0383492, 1.0], + [0.14017454, 1.0023274, 1.0], + [0.0, 1.0, 1.0], + [9.04703, 1.1110121, 1.0], + [6.10701, 1.0926124, 1.0], + [2.378649, 1.0378062, 1.0], + [0.111506626, 1.001809, 1.0], + [0.0, 1.0, 1.0], + [9.323562, 1.1182009, 1.0], + [7.539196, 1.0773741, 1.0], + [2.3685758, 1.0214494, 1.0], + [0.07044682, 1.0008357, 1.0], + [0.0, 1.0, 1.0], + [8.691661, 1.143956, 1.0], + [7.088831, 1.0745311, 1.0], + [1.8496133, 1.0144777, 1.0], + [0.05676793, 1.0005342, 1.0], + [0.0, 1.0, 1.0], + [7.4111695, 1.2071966, 1.0], + [4.6049232, 1.1096628, 1.0], + [1.9098912, 1.0257343, 1.0], + [0.07917395, 1.0008571, 1.0], + [0.0, 1.0, 1.0], + [7.0088325, 1.2360815, 1.0], + [5.181472, 1.1226107, 1.0], + [2.5577261, 1.0353444, 1.0], + [0.112243205, 1.0013376, 1.0], + [0.0, 1.0, 1.0], + [8.824198, 1.2390603, 1.0], + [7.472616, 1.1490262, 1.0], + [3.6435897, 1.0531034, 1.0], + [0.21466114, 1.00276, 1.0], + [0.0, 1.0, 1.0], + [9.237296, 1.2194712, 1.0], + [7.4227743, 1.131169, 1.0], + [3.8712175, 1.049929, 1.0], + [0.27166268, 1.0031942, 1.0], + [0.0, 1.0, 1.0], + [8.786514, 1.1667371, 1.0], + [6.6639266, 1.1076087, 1.0], + [2.2446363, 1.0388349, 1.0], + [0.102016255, 1.0017875, 1.0], + [0.0, 1.0, 1.0], + [7.9329085, 1.1376165, 1.0], + [6.6409307, 1.1026622, 1.0], + [3.2540584, 1.0458134, 1.0], + [0.23406105, 1.0032705, 1.0], + [0.0, 1.0, 1.0], + [9.150625, 1.1396258, 1.0], + [6.7835217, 1.1053689, 1.0], + [3.3122694, 1.0424184, 1.0], + [0.22282204, 1.0027751, 1.0], + [0.0, 1.0, 1.0], + [9.179195, 1.1203728, 1.0], + [6.1122427, 1.0689247, 1.0], + [2.574998, 1.0149101, 1.0], + [0.17865337, 1.001089, 1.0], + [0.0, 1.0, 1.0], + [8.981582, 1.1198573, 1.0], + [6.162663, 1.0758042, 1.0], + [3.49672, 1.0282632, 1.0], + [0.5296274, 1.0024091, 1.0], + [0.0, 1.0, 1.0], + [7.104293, 1.1063842, 1.0], + [6.60601, 1.0895218, 1.0], + [6.026096, 1.0566804, 1.0], + [1.4251214, 1.0060695, 1.0], + [0.0, 1.0, 1.0], + [5.400258, 1.118055, 1.0], + [8.1627245, 1.1168847, 1.0], + [8.838523, 1.1060567, 1.0], + [3.2699623, 1.0280458, 1.0], + [0.0, 1.0, 1.0], + [4.0916386, 1.0858877, 1.0], + [7.020515, 1.0801034, 1.0], + [8.994645, 1.1054409, 1.0], + [6.3794594, 1.070257, 1.0], + [0.0, 1.0, 1.0], + [2.8969727, 1.0963963, 1.0], + [5.3678546, 1.111281, 1.0], + [7.492073, 1.145503, 1.0], + [6.806583, 1.107631, 1.0], + [0.0, 1.0, 1.0], + [11.097742, 0.7495534, 1.0], + [4.302149, 1.0255402, 1.0], + [4.3949976, 1.078089, 1.0], + [3.0446813, 1.0549035, 1.0], + [0.0, 1.0, 1.0], + [8.670632, 0.7986941, 1.0], + [4.084819, 1.0083629, 1.0], + [4.0668464, 1.0512977, 1.0], + [3.5106099, 1.0471594, 1.0], + [0.0, 1.0, 1.0], + [6.4860106, 0.8172128, 1.0], + [3.818937, 0.93780375, 1.0], + [3.2241101, 1.0080932, 1.0], + [2.3703701, 1.0238551, 1.0], + [0.0, 1.0, 1.0], + [4.833141, 0.8377232, 1.0], + [4.498794, 0.8643455, 1.0], + [3.6683908, 0.9057964, 1.0], + [1.7416582, 0.96007955, 1.0], + [0.0, 1.0, 1.0], + [4.6399913, 0.80761904, 1.0], + [4.839282, 0.8098061, 1.0], + [4.2472334, 0.8426727, 1.0], + [2.3996296, 0.87981254, 1.0], + [0.0, 1.0, 1.0], + [4.0833488, 0.78954774, 1.0], + [4.703831, 0.781375, 1.0], + [4.1265993, 0.8191039, 1.0], + [3.244208, 0.82794446, 1.0], + [0.0, 1.0, 1.0], + [2.2156487, 0.8334128, 1.0], + [2.5546548, 0.82133436, 1.0], + [2.4130013, 0.82352406, 1.0], + [2.1992364, 0.81915104, 1.0], + [0.0, 1.0, 1.0], + [1.719304, 0.8250702, 1.0], + [1.4982271, 0.8221971, 1.0], + [1.3897603, 0.840134, 1.0], + [1.7225292, 0.8336163, 1.0], + [0.0, 1.0, 1.0], + [1.0925283, 0.85481983, 1.0], + [0.7631879, 0.85975116, 1.0], + [0.027124116, 0.87992114, 1.0], + [0.60234094, 0.84217334, 1.0], + [0.0, 1.0, 1.0], + [1.077557, 0.872631, 1.0], + [1.0173641, 0.8593242, 1.0], + [0.46943918, 0.87838364, 1.0], + [2.6384852, 0.79771537, 1.0], + [0.0, 1.0, 1.0], + [0.7770302, 0.9353661, 1.0], + [1.0596069, 0.8860215, 1.0], + [1.2889328, 0.8582148, 1.0], + [4.9600816, 0.74249977, 1.0], + [0.0, 1.0, 1.0], + [0.34926006, 0.98829997, 1.0], + [0.18424937, 0.9600296, 1.0], + [1.1720486, 0.8844167, 1.0], + [2.579216, 0.7862993, 1.0], + [0.0, 1.0, 1.0], + [0.30250847, 0.9603346, 1.0], + [1.107201, 0.928601, 1.0], + [1.9961823, 0.8556583, 1.0], + [-0.3471245, 0.82866865, 1.0], + [0.0, 1.0, 1.0], + [-0.7629411, 1.0180708, 1.0], + [0.610268, 0.974038, 1.0], + [1.429175, 0.8520721, 1.0], + [0.48019677, 0.8095613, 1.0], + [0.0, 1.0, 1.0], + [-0.3008022, 0.9407405, 1.0], + [1.6219236, 0.9161009, 1.0], + [0.49270624, 0.8484004, 1.0], + [-0.12723985, 0.791222, 1.0], + [0.0, 1.0, 1.0], + [0.25840467, 0.85001826, 1.0], + [1.2374158, 0.87433594, 1.0], + [-0.47591653, 0.8426007, 1.0], + [-0.9091751, 0.79707766, 1.0], + [0.0, 1.0, 1.0], + [1.1872596, 0.8374762, 1.0], + [1.1351508, 0.8393883, 1.0], + [-0.09448363, 0.82199603, 1.0], + [-0.22021097, 0.80633336, 1.0], + [0.0, 1.0, 1.0], + [0.99161613, 0.82922834, 1.0], + [0.5485594, 0.83473396, 1.0], + [0.39220774, 0.826323, 1.0], + [0.58646864, 0.81625366, 1.0], + [0.0, 1.0, 1.0], + [0.25968486, 0.823715, 1.0], + [-0.11242166, 0.8438889, 1.0], + [0.41922796, 0.8506193, 1.0], + [1.028925, 0.8475653, 1.0], + [0.0, 1.0, 1.0], + [1.0410757, 0.82169753, 1.0], + [1.1827178, 0.83744586, 1.0], + [1.5406011, 0.8407932, 1.0], + [1.5877901, 0.88981646, 1.0], + [0.0, 1.0, 1.0], + [1.7911158, 0.83056414, 1.0], + [4.6160154, 0.8464025, 1.0], + [5.6431475, 0.8491971, 1.0], + [2.9428084, 0.91466653, 1.0], + [0.0, 1.0, 1.0], + [2.4177535, 0.8561214, 1.0], + [6.020544, 0.8638199, 1.0], + [6.9461846, 0.8605439, 1.0], + [3.500521, 0.92217606, 1.0], + [0.0, 1.0, 1.0], + [2.851688, 0.89191425, 1.0], + [1.421044, 0.9390341, 1.0], + [-1.1085083, 0.9700407, 1.0], + [-0.36786354, 0.9936673, 1.0], + [0.0, 1.0, 1.0], + [2.3683667, 0.9257322, 1.0], + [-0.26451036, 0.95317423, 1.0], + [-2.130682, 0.9368306, 1.0], + [-0.4093729, 0.986178, 1.0], + [0.0, 1.0, 1.0], + [1.5891942, 0.99163103, 1.0], + [0.045109738, 0.966062, 1.0], + [-0.53449297, 0.92987776, 1.0], + [-0.033470135, 0.9932046, 1.0], + [0.0, 1.0, 1.0], + [-0.27134877, 0.99536645, 1.0], + [0.0898188, 0.98402214, 1.0], + [0.27262244, 0.96843773, 1.0], + [0.009789813, 0.9986736, 1.0], + [0.0, 1.0, 1.0], + [-0.7730278, 1.0203122, 1.0], + [-2.9028432, 1.0235066, 1.0], + [-1.0447694, 0.9868708, 1.0], + [-0.04624136, 0.99908286, 1.0], + [0.0, 1.0, 1.0], + [-1.3631408, 1.0156548, 1.0], + [-3.9609292, 1.0409077, 1.0], + [-1.4876856, 0.9928084, 1.0], + [-0.095956564, 0.998979, 1.0], + [0.0, 1.0, 1.0], + [-1.7949396, 0.9771055, 1.0], + [-3.3644142, 1.0089597, 1.0], + [-0.6663962, 0.98575324, 1.0], + [-0.060724735, 0.9990037, 1.0], + [0.0, 1.0, 1.0], + [-2.6404126, 0.9683573, 1.0], + [-3.7188525, 0.9884891, 1.0], + [-0.33517227, 0.9891578, 1.0], + [-0.01776004, 0.99959093, 1.0], + [0.0, 1.0, 1.0], + [-1.5392375, 0.9850047, 1.0], + [-1.9149097, 0.9972289, 1.0], + [0.076658234, 0.99591476, 1.0], + [0.0015469929, 0.99989164, 1.0], + [0.0, 1.0, 1.0], + [-1.5000389, 0.9821583, 1.0], + [-2.8312242, 0.9990609, 1.0], + [-0.28693938, 0.9993521, 1.0], + [-0.003116089, 0.9999806, 1.0], + [0.0, 1.0, 1.0], + [-2.261437, 0.9956475, 1.0], + [-4.1936493, 0.99516, 1.0], + [-0.5251263, 0.99886537, 1.0], + [-0.0044811945, 0.99998087, 1.0], + [0.0, 1.0, 1.0], + [-2.1491625, 1.0218415, 1.0], + [-4.5739403, 1.0102247, 1.0], + [-0.68651015, 0.99985665, 1.0], + [-0.0075566974, 0.99995035, 1.0], + [0.0, 1.0, 1.0], + [-1.2349107, 0.9898649, 1.0], + [-1.9306059, 1.0036258, 1.0], + [-0.31590265, 0.99970144, 1.0], + [-0.0033171063, 0.9999775, 1.0], + [0.0, 1.0, 1.0], + [-1.0307009, 0.99630994, 1.0], + [0.30616513, 1.0119231, 1.0], + [0.06396701, 1.0005364, 1.0], + [0.00027660295, 0.9999968, 1.0], + [0.0, 1.0, 1.0], + [-2.346574, 0.9880379, 1.0], + [-0.8791935, 1.0093088, 1.0], + [-0.016242307, 1.0002874, 1.0], + [0.00025250288, 0.99999845, 1.0], + [0.0, 1.0, 1.0], + [-3.0779517, 0.97170013, 1.0], + [-1.4663464, 0.99664164, 1.0], + [-0.10389895, 0.9996869, 1.0], + [-0.00044551404, 0.9999951, 1.0], + [0.0, 1.0, 1.0], + [-4.567538, 0.9528182, 1.0], + [-1.9175193, 0.96272826, 1.0], + [-0.24028678, 0.988517, 1.0], + [-0.0031621323, 0.9997933, 1.0], + [0.0, 1.0, 1.0], + [-5.301078, 0.9462786, 1.0], + [-2.0612185, 0.9623441, 1.0], + [-0.22937998, 0.98045844, 1.0], + [-0.0035447287, 0.99954635, 1.0], + [0.0, 1.0, 1.0], + [-5.17369, 0.9347734, 1.0], + [-2.6256938, 0.9702652, 1.0], + [-0.9779252, 0.98185503, 1.0], + [-0.025275169, 0.999532, 1.0], + [0.0, 1.0, 1.0], + [-5.6190476, 0.87789106, 1.0], + [-4.1578727, 0.9216868, 1.0], + [-2.5072808, 0.9612179, 1.0], + [-0.089652225, 0.9986668, 1.0], + [0.0, 1.0, 1.0], + [-6.76994, 0.87418616, 1.0], + [-6.514886, 0.907811, 1.0], + [-4.9078856, 0.9322168, 1.0], + [-0.41067597, 0.99400187, 1.0], + [0.0, 1.0, 1.0], + [-6.1820183, 0.9030464, 1.0], + [-4.7838874, 0.9592428, 1.0], + [-3.901864, 0.9493777, 1.0], + [-2.8153791, 0.9527283, 1.0], + [0.0, 1.0, 1.0], + [-5.788914, 0.91208506, 1.0], + [-3.994215, 0.97003484, 1.0], + [-3.385323, 0.9577372, 1.0], + [-2.9413586, 0.9510517, 1.0], + [0.0, 1.0, 1.0], + [-5.824406, 0.83820003, 1.0], + [-3.1810389, 0.9660887, 1.0], + [-1.5774505, 0.9790124, 1.0], + [-0.8217753, 0.9740097, 1.0], + [0.0, 1.0, 1.0], + [-4.3065467, 0.80589384, 1.0], + [-2.6072268, 0.93019086, 1.0], + [-0.21090473, 0.9885001, 1.0], + [0.43375567, 0.9852358, 1.0], + [0.0, 1.0, 1.0], + [-2.482041, 0.80521226, 1.0], + [-1.9464252, 0.89081764, 1.0], + [-1.3067281, 0.97063005, 1.0], + [-0.08558672, 1.0085713, 1.0], + [0.0, 1.0, 1.0], + [0.005971689, 0.79016274, 1.0], + [0.35732928, 0.85779244, 1.0], + [0.1754206, 0.99188244, 1.0], + [0.22379632, 1.037684, 1.0], + [0.0, 1.0, 1.0], + [2.7745373, 0.8123556, 1.0], + [2.660167, 0.8928497, 1.0], + [1.9056137, 1.0349722, 1.0], + [1.3743136, 1.0741935, 1.0], + [0.0, 1.0, 1.0], + [5.84149, 0.94729656, 1.0], + [5.9234858, 0.99772394, 1.0], + [3.5638256, 1.0254989, 1.0], + [0.789219, 1.0284225, 1.0], + [0.0, 1.0, 1.0], + [6.216669, 0.94323486, 1.0], + [6.1197004, 0.97384584, 1.0], + [3.111261, 1.0015063, 1.0], + [0.13389735, 1.0010753, 1.0], + [0.0, 1.0, 1.0], + [6.8003964, 0.8915141, 1.0], + [6.054007, 0.933874, 1.0], + [2.107739, 1.0027483, 1.0], + [0.0493525, 1.0011154, 1.0], + [0.0, 1.0, 1.0], + [7.9880567, 0.84407914, 1.0], + [5.6231203, 0.93609124, 1.0], + [2.2157097, 1.0120378, 1.0], + [0.032817453, 1.0015986, 1.0], + [0.0, 1.0, 1.0], + [8.595032, 0.8414821, 1.0], + [5.738505, 0.9392406, 1.0], + [1.8269083, 1.0084609, 1.0], + [0.04897936, 1.0008367, 1.0], + [0.0, 1.0, 1.0], + [8.220856, 0.87541443, 1.0], + [5.0150123, 0.95354825, 1.0], + [1.1460975, 0.9991748, 1.0], + [0.023244264, 1.0001849, 1.0], + [0.0, 1.0, 1.0], + [6.352977, 0.90786576, 1.0], + [4.568452, 0.9657601, 1.0], + [1.6529517, 1.000971, 1.0], + [0.03857504, 1.0002371, 1.0], + [0.0, 1.0, 1.0], + [4.984532, 0.9082033, 1.0], + [4.602232, 0.9495377, 1.0], + [1.6734874, 0.9877272, 1.0], + [0.03744144, 0.9998395, 1.0], + [0.0, 1.0, 1.0], + [5.9091544, 0.93606, 1.0], + [5.298362, 0.9841254, 1.0], + [1.5838195, 1.0008063, 1.0], + [0.046534117, 1.0002071, 1.0], + [0.0, 1.0, 1.0], + [5.066354, 0.97590166, 1.0], + [4.5295296, 1.0173208, 1.0], + [1.7539304, 1.013229, 1.0], + [0.053133972, 1.0004649, 1.0], + [0.0, 1.0, 1.0], + [3.9509256, 0.96985763, 1.0], + [2.8036354, 1.0145437, 1.0], + [1.5774267, 1.01402, 1.0], + [0.066340216, 1.0006825, 1.0], + [0.0, 1.0, 1.0], + [5.067796, 0.9293189, 1.0], + [4.159626, 1.0254444, 1.0], + [2.7054405, 1.0338923, 1.0], + [0.16508698, 1.0020102, 1.0], + [0.0, 1.0, 1.0], + [6.1854787, 0.88944936, 1.0], + [4.127846, 0.9825014, 1.0], + [2.468719, 1.0214276, 1.0], + [0.17890404, 1.0017699, 1.0], + [0.0, 1.0, 1.0], + [5.5697684, 0.868272, 1.0], + [3.8859425, 0.95566565, 1.0], + [1.9449633, 1.0140244, 1.0], + [0.15235585, 1.0016147, 1.0], + [0.0, 1.0, 1.0], + [4.9004993, 0.8488444, 1.0], + [3.3493254, 0.9401458, 1.0], + [1.7450691, 1.0157793, 1.0], + [0.2219146, 1.0026209, 1.0], + [0.0, 1.0, 1.0], + [5.1511345, 0.8408119, 1.0], + [2.344393, 0.9062895, 1.0], + [-0.006539325, 0.989216, 1.0], + [-0.08388412, 0.9982721, 1.0], + [0.0, 1.0, 1.0], + [5.9436674, 0.83059716, 1.0], + [2.7960584, 0.8703427, 1.0], + [-0.36575812, 0.97218806, 1.0], + [-0.23437832, 0.9956032, 1.0], + [0.0, 1.0, 1.0], + [6.1871066, 0.7945113, 1.0], + [3.3608866, 0.8636077, 1.0], + [0.7976455, 0.97853297, 1.0], + [0.016725179, 0.99557835, 1.0], + [0.0, 1.0, 1.0], + [6.6724114, 0.8086163, 1.0], + [4.0700526, 0.9031084, 1.0], + [2.1568875, 0.99063873, 1.0], + [0.28391585, 0.99492174, 1.0], + [0.0, 1.0, 1.0], + [7.5037627, 0.8231193, 1.0], + [5.7297187, 0.9803322, 1.0], + [4.538334, 1.0247983, 1.0], + [0.78753865, 0.9985357, 1.0], + [0.0, 1.0, 1.0], + [10.182478, 0.7484477, 1.0], + [4.6068373, 0.93632764, 1.0], + [5.901852, 0.9994183, 1.0], + [1.9283233, 1.0053344, 1.0], + [0.0, 1.0, 1.0], + [11.154881, 0.70661646, 1.0], + [2.7159872, 0.93115985, 1.0], + [4.0488343, 1.0193834, 1.0], + [1.9382095, 1.0209278, 1.0], + [0.0, 1.0, 1.0], + [21.429089, 0.534035, 1.0], + [10.9904585, 0.6681652, 1.0], + [0.0045763263, 0.9355186, 1.0], + [0.13676341, 0.9799069, 1.0], + [0.0, 1.0, 1.0], + [19.529167, 0.5656457, 1.0], + [9.061795, 0.7158103, 1.0], + [1.5038702, 0.90302753, 1.0], + [0.78457785, 0.9086724, 1.0], + [0.0, 1.0, 1.0], + [15.508226, 0.5985953, 1.0], + [7.4871893, 0.7188201, 1.0], + [2.3094695, 0.89489657, 1.0], + [0.73899657, 0.9671641, 1.0], + [0.0, 1.0, 1.0], + [10.39154, 0.65087306, 1.0], + [6.622824, 0.6944927, 1.0], + [5.501718, 0.7407023, 1.0], + [1.7589878, 0.92959625, 1.0], + [0.0, 1.0, 1.0], + [8.076021, 0.62203556, 1.0], + [7.467209, 0.6181576, 1.0], + [7.245981, 0.6409904, 1.0], + [5.4263334, 0.7738036, 1.0], + [0.0, 1.0, 1.0], + [8.469799, 0.5848823, 1.0], + [8.423419, 0.5817134, 1.0], + [8.497577, 0.60529923, 1.0], + [9.111917, 0.69250333, 1.0], + [0.0, 1.0, 1.0], + [7.762021, 0.6242976, 1.0], + [7.8041244, 0.61129224, 1.0], + [8.319156, 0.64303446, 1.0], + [9.280437, 0.71576697, 1.0], + [0.0, 1.0, 1.0], + [5.9923735, 0.63731086, 1.0], + [4.8158092, 0.65566516, 1.0], + [6.311018, 0.6723989, 1.0], + [8.32342, 0.7326516, 1.0], + [0.0, 1.0, 1.0], + [6.2170925, 0.60791266, 1.0], + [5.0867386, 0.6423507, 1.0], + [6.415117, 0.66422945, 1.0], + [8.737183, 0.70679927, 1.0], + [0.0, 1.0, 1.0], + [5.2006907, 0.60027266, 1.0], + [4.8628798, 0.61094993, 1.0], + [6.024442, 0.6481432, 1.0], + [9.87192, 0.66732514, 1.0], + [0.0, 1.0, 1.0], + [4.6975617, 0.6112319, 1.0], + [4.514887, 0.6216314, 1.0], + [6.3849893, 0.63863033, 1.0], + [10.629338, 0.64065015, 1.0], + [0.0, 1.0, 1.0], + [4.5348377, 0.62149215, 1.0], + [3.3658662, 0.67045367, 1.0], + [5.1989746, 0.71183586, 1.0], + [8.826136, 0.6767556, 1.0], + [0.0, 1.0, 1.0], + [3.2873642, 0.6042733, 1.0], + [2.2218688, 0.65636855, 1.0], + [0.86957604, 0.82655025, 1.0], + [0.053957075, 0.88620996, 1.0], + [0.0, 1.0, 1.0], + [2.4001925, 0.6340274, 1.0], + [1.1141202, 0.70201296, 1.0], + [0.50959253, 0.8092494, 1.0], + [0.2311227, 0.8478709, 1.0], + [0.0, 1.0, 1.0], + [1.2452888, 0.66887766, 1.0], + [-0.07834225, 0.74380815, 1.0], + [-0.13215514, 0.7847734, 1.0], + [-0.38037032, 0.7592258, 1.0], + [0.0, 1.0, 1.0], + [-1.1262196, 0.6688091, 1.0], + [-1.8940061, 0.6305882, 1.0], + [-1.4030197, 0.7027344, 1.0], + [-1.2132488, 0.7527478, 1.0], + [0.0, 1.0, 1.0], + [-1.7841728, 0.6271489, 1.0], + [-2.0797975, 0.61307365, 1.0], + [-1.7369123, 0.6998982, 1.0], + [-1.4816345, 0.78196275, 1.0], + [0.0, 1.0, 1.0], + [-1.5889271, 0.6240376, 1.0], + [-1.8163804, 0.6369125, 1.0], + [-0.99799794, 0.7531041, 1.0], + [-0.5732026, 0.82648337, 1.0], + [0.0, 1.0, 1.0], + [-2.0947924, 0.62250054, 1.0], + [-2.1894937, 0.6211645, 1.0], + [-1.2518405, 0.701402, 1.0], + [-0.14743061, 0.8963739, 1.0], + [0.0, 1.0, 1.0], + [-1.3260301, 0.622353, 1.0], + [-1.4357653, 0.6204895, 1.0], + [-0.9074722, 0.7065318, 1.0], + [-0.00092270726, 0.94407517, 1.0], + [0.0, 1.0, 1.0], + [0.53253675, 0.609965, 1.0], + [0.7909907, 0.63592654, 1.0], + [0.8281013, 0.861355, 1.0], + [0.09484549, 0.99166083, 1.0], + [0.0, 1.0, 1.0], + [2.1939044, 0.6141987, 1.0], + [2.304398, 0.68116254, 1.0], + [1.0291927, 0.94084936, 1.0], + [0.07020744, 0.99777913, 1.0], + [0.0, 1.0, 1.0], + [2.544789, 0.6641931, 1.0], + [1.7962058, 0.7804687, 1.0], + [0.024910655, 0.9779748, 1.0], + [-0.006917878, 0.9988935, 1.0], + [0.0, 1.0, 1.0], + [2.512072, 0.6963922, 1.0], + [1.0768867, 0.8578466, 1.0], + [-0.15686627, 0.95935565, 1.0], + [-0.018332472, 0.9965962, 1.0], + [0.0, 1.0, 1.0], + [2.2941554, 0.7241867, 1.0], + [0.49197897, 0.8924251, 1.0], + [-0.07057831, 0.97556025, 1.0], + [0.0007015772, 0.9984997, 1.0], + [0.0, 1.0, 1.0], + [-0.14841639, 0.75324446, 1.0], + [0.3125728, 0.9184302, 1.0], + [0.12940219, 0.99002546, 1.0], + [0.004882609, 0.9996811, 1.0], + [0.0, 1.0, 1.0], + [1.0759145, 0.8147672, 1.0], + [0.36428088, 0.95350146, 1.0], + [0.044762604, 0.9945495, 1.0], + [0.0020008544, 0.99985397, 1.0], + [0.0, 1.0, 1.0], + [1.1844163, 0.82202315, 1.0], + [0.079763375, 0.9658192, 1.0], + [-0.040133536, 0.99962455, 1.0], + [-0.0008112378, 0.9999972, 1.0], + [0.0, 1.0, 1.0], + [1.3717666, 0.79799265, 1.0], + [0.041872248, 0.9601765, 1.0], + [-0.031498604, 0.9993519, 1.0], + [-0.00014435564, 0.99999434, 1.0], + [0.0, 1.0, 1.0], + [1.4442534, 0.8161425, 1.0], + [-0.063920654, 0.9636011, 1.0], + [-0.05635313, 0.9993309, 1.0], + [-0.00048342353, 0.99999565, 1.0], + [0.0, 1.0, 1.0], + [0.52015144, 0.8598735, 1.0], + [0.19060682, 0.97779083, 1.0], + [0.0042832624, 0.9997414, 1.0], + [4.660216e-05, 0.999999, 1.0], + [0.0, 1.0, 1.0], + [-0.7497536, 0.84093064, 1.0], + [-0.81031346, 0.97599655, 1.0], + [-0.09859322, 0.99943244, 1.0], + [-0.0006943415, 0.9999969, 1.0], + [0.0, 1.0, 1.0], + [-1.3677365, 0.8722283, 1.0], + [-1.8130565, 0.97921914, 1.0], + [-0.21724299, 0.9991554, 1.0], + [-0.0015388737, 0.99999464, 1.0], + [0.0, 1.0, 1.0], + [-0.91486025, 0.8624227, 1.0], + [-1.8185471, 0.98354757, 1.0], + [-0.18334843, 1.00006, 1.0], + [-0.0010572135, 1.0000012, 1.0], + [0.0, 1.0, 1.0], + [-0.7346696, 0.8424903, 1.0], + [-0.63329214, 0.9792088, 1.0], + [-0.059044026, 0.9998297, 1.0], + [-0.00033109298, 0.99999976, 1.0], + [0.0, 1.0, 1.0], + [-1.6148448, 0.8629954, 1.0], + [-0.1529285, 0.9853164, 1.0], + [-0.0012836644, 0.99992263, 1.0], + [2.836192e-07, 0.99999994, 1.0], + [0.0, 1.0, 1.0], + [-2.1700978, 0.8661239, 1.0], + [-0.63642955, 0.98434937, 1.0], + [-0.031005092, 0.99981445, 1.0], + [-0.000120288605, 0.99999934, 1.0], + [0.0, 1.0, 1.0], + [-2.4762814, 0.86192167, 1.0], + [-0.56582534, 0.98256135, 1.0], + [-0.020217514, 0.9998631, 1.0], + [-7.2207775e-05, 0.9999997, 1.0], + [0.0, 1.0, 1.0], + [-3.8507402, 0.82345366, 1.0], + [-0.7367012, 0.96947515, 1.0], + [-0.014731503, 0.9995469, 1.0], + [-6.203018e-05, 0.9999977, 1.0], + [0.0, 1.0, 1.0], + [-4.3435655, 0.7789662, 1.0], + [-1.2070022, 0.9441542, 1.0], + [-0.06403829, 0.99689096, 1.0], + [-0.0005338597, 0.999972, 1.0], + [0.0, 1.0, 1.0], + [-7.598504, 0.7135656, 1.0], + [-2.8674366, 0.8796157, 1.0], + [-0.2061764, 0.98925316, 1.0], + [-0.0019789634, 0.9999003, 1.0], + [0.0, 1.0, 1.0], + [-8.5659275, 0.68000054, 1.0], + [-6.2886615, 0.7959308, 1.0], + [-1.6636302, 0.9520329, 1.0], + [-0.02707145, 0.99932, 1.0], + [0.0, 1.0, 1.0], + [-9.858409, 0.65844935, 1.0], + [-10.290696, 0.7347493, 1.0], + [-7.691491, 0.82455605, 1.0], + [-0.43717942, 0.9903527, 1.0], + [0.0, 1.0, 1.0], + [-9.475192, 0.660726, 1.0], + [-8.357337, 0.8211995, 1.0], + [-6.577057, 0.9004506, 1.0], + [-2.6555653, 0.960382, 1.0], + [0.0, 1.0, 1.0], + [-9.107314, 0.6639968, 1.0], + [-6.700647, 0.8769558, 1.0], + [-5.7258368, 0.9253376, 1.0], + [-4.3847528, 0.93834984, 1.0], + [0.0, 1.0, 1.0], + [-8.857171, 0.6214388, 1.0], + [-7.408514, 0.77258706, 1.0], + [-5.1308303, 0.92324936, 1.0], + [-3.1422675, 0.95364594, 1.0], + [0.0, 1.0, 1.0], + [-7.9207935, 0.5845634, 1.0], + [-7.1896048, 0.6665354, 1.0], + [-4.057381, 0.85848784, 1.0], + [-0.73692507, 0.97766274, 1.0], + [0.0, 1.0, 1.0], + [-6.263444, 0.5832066, 1.0], + [-5.9315505, 0.6382356, 1.0], + [-6.1455026, 0.72098804, 1.0], + [-1.0433422, 0.96975046, 1.0], + [0.0, 1.0, 1.0], + [-2.4575808, 0.57827437, 1.0], + [-2.964139, 0.62386805, 1.0], + [-4.5475383, 0.7487994, 1.0], + [-1.4768721, 0.9732885, 1.0], + [0.0, 1.0, 1.0], + [1.157196, 0.57085544, 1.0], + [-0.23392056, 0.64966595, 1.0], + [-1.4028531, 0.88008684, 1.0], + [-1.9659572, 0.9766486, 1.0], + [0.0, 1.0, 1.0], + [4.5584664, 0.6198235, 1.0], + [4.1219063, 0.74054, 1.0], + [0.9924724, 0.93495196, 1.0], + [-0.7218012, 0.988608, 1.0], + [0.0, 1.0, 1.0], + [3.719361, 0.6867312, 1.0], + [3.9891121, 0.7887134, 1.0], + [1.6480545, 0.94551146, 1.0], + [0.059591822, 0.99834126, 1.0], + [0.0, 1.0, 1.0], + [4.201495, 0.7431256, 1.0], + [4.526226, 0.8292865, 1.0], + [1.1760861, 0.9666664, 1.0], + [0.023769544, 0.9994029, 1.0], + [0.0, 1.0, 1.0], + [4.170543, 0.76272196, 1.0], + [2.6068974, 0.8673706, 1.0], + [0.39172325, 0.9858963, 1.0], + [0.004990869, 0.9999019, 1.0], + [0.0, 1.0, 1.0], + [4.301262, 0.77241415, 1.0], + [2.272072, 0.8702671, 1.0], + [0.20433316, 0.98867095, 1.0], + [0.0015561674, 0.9999392, 1.0], + [0.0, 1.0, 1.0], + [1.9231374, 0.8245692, 1.0], + [1.1306319, 0.9499702, 1.0], + [0.12337404, 0.9968917, 1.0], + [0.0012145389, 0.9999798, 1.0], + [0.0, 1.0, 1.0], + [0.7749719, 0.84335756, 1.0], + [0.5761641, 0.95042235, 1.0], + [0.15160465, 0.9963909, 1.0], + [0.0022156653, 0.9999764, 1.0], + [0.0, 1.0, 1.0], + [0.24714066, 0.83536404, 1.0], + [0.69087815, 0.93358576, 1.0], + [0.13375169, 0.9901696, 1.0], + [0.00097174913, 0.9998683, 1.0], + [0.0, 1.0, 1.0], + [0.9913264, 0.84575355, 1.0], + [0.6101601, 0.9683786, 1.0], + [0.03825838, 0.9969422, 1.0], + [-0.00015123004, 0.9999521, 1.0], + [0.0, 1.0, 1.0], + [-0.36478883, 0.8678444, 1.0], + [0.0008414984, 0.9753911, 1.0], + [0.040139902, 0.9994379, 1.0], + [0.0008033622, 0.999998, 1.0], + [0.0, 1.0, 1.0], + [1.5517159, 0.860102, 1.0], + [-0.14287986, 0.9731134, 1.0], + [-0.0241953, 0.9983607, 1.0], + [0.00026539815, 0.9999833, 1.0], + [0.0, 1.0, 1.0], + [1.771025, 0.8242807, 1.0], + [0.39131552, 0.9689884, 1.0], + [0.10286286, 1.0004354, 1.0], + [0.0041896314, 1.0000376, 1.0], + [0.0, 1.0, 1.0], + [3.0717576, 0.7639471, 1.0], + [1.06725, 0.9274218, 1.0], + [0.16506667, 0.99773335, 1.0], + [0.0063294014, 0.99998826, 1.0], + [0.0, 1.0, 1.0], + [2.2291703, 0.7308612, 1.0], + [0.49462095, 0.8725961, 1.0], + [-0.10180267, 0.97933596, 1.0], + [-0.0027677065, 0.99942666, 1.0], + [0.0, 1.0, 1.0], + [1.6348059, 0.6752963, 1.0], + [-0.1813347, 0.81271625, 1.0], + [-0.452213, 0.9789637, 1.0], + [-0.02926826, 0.99930775, 1.0], + [0.0, 1.0, 1.0], + [0.977978, 0.63618356, 1.0], + [-0.62426865, 0.7659978, 1.0], + [-1.2976239, 0.96756303, 1.0], + [-0.16725117, 0.9974616, 1.0], + [0.0, 1.0, 1.0], + [2.5557356, 0.63749415, 1.0], + [-0.46793, 0.74725634, 1.0], + [-1.4595929, 0.9653957, 1.0], + [-0.23743904, 0.99673766, 1.0], + [0.0, 1.0, 1.0], + [6.1136274, 0.6158691, 1.0], + [1.4451386, 0.7140542, 1.0], + [-0.7944747, 0.9716655, 1.0], + [-0.17677365, 0.9974942, 1.0], + [0.0, 1.0, 1.0], + [12.495926, 0.6081697, 1.0], + [2.6334763, 0.75307316, 1.0], + [-0.12727743, 0.9822432, 1.0], + [-0.019888313, 0.9994333, 1.0], + [0.0, 1.0, 1.0], + [20.359663, 0.5505554, 1.0], + [5.360571, 0.7478232, 1.0], + [-0.15579183, 0.9834688, 1.0], + [0.0031478892, 0.9996698, 1.0], + [0.0, 1.0, 1.0], + [22.983652, 0.51872075, 1.0], + [11.201727, 0.65178657, 1.0], + [0.02786072, 0.96850365, 1.0], + [0.050246127, 0.9990089, 1.0], + [0.0, 1.0, 1.0], + [22.102345, 0.5159302, 1.0], + [13.516024, 0.6239164, 1.0], + [-0.2264105, 0.9579077, 1.0], + [0.011741261, 0.9989512, 1.0], + [0.0, 1.0, 1.0], + [16.458723, 0.6381099, 1.0], + [2.070874, 0.8461226, 1.0], + [-0.38940287, 0.94793105, 1.0], + [-0.004449404, 0.9940882, 1.0], + [0.0, 1.0, 1.0], + [14.319419, 0.6865272, 1.0], + [2.107183, 0.90325254, 1.0], + [0.08382885, 0.95650256, 1.0], + [0.08427957, 0.95735323, 1.0], + [0.0, 1.0, 1.0], + [11.907165, 0.7272812, 1.0], + [2.3358235, 0.9138314, 1.0], + [0.061927434, 0.9853146, 1.0], + [0.02880717, 0.98940504, 1.0], + [0.0, 1.0, 1.0], + [9.328565, 0.7501317, 1.0], + [3.736033, 0.8165723, 1.0], + [0.886498, 0.9493906, 1.0], + [0.53428864, 0.9778165, 1.0], + [0.0, 1.0, 1.0], + [7.8924174, 0.6558145, 1.0], + [8.357237, 0.58703214, 1.0], + [5.640144, 0.71630454, 1.0], + [2.3299222, 0.9120247, 1.0], + [0.0, 1.0, 1.0], + [8.797466, 0.5970874, 1.0], + [10.189405, 0.5419211, 1.0], + [9.585496, 0.602155, 1.0], + [8.76234, 0.7506968, 1.0], + [0.0, 1.0, 1.0], + [5.77069, 0.73146415, 1.0], + [8.458371, 0.6105788, 1.0], + [9.997233, 0.66665345, 1.0], + [14.185366, 0.72296506, 1.0], + [0.0, 1.0, 1.0], + [4.832553, 0.7476512, 1.0], + [6.631264, 0.64727205, 1.0], + [9.165473, 0.66433716, 1.0], + [12.874936, 0.71633804, 1.0], + [0.0, 1.0, 1.0], + [4.3384695, 0.7561362, 1.0], + [5.5324025, 0.6862774, 1.0], + [6.1772375, 0.7093537, 1.0], + [5.11874, 0.82211673, 1.0], + [0.0, 1.0, 1.0], + [4.3927083, 0.68014836, 1.0], + [5.3236537, 0.617722, 1.0], + [5.153285, 0.7291801, 1.0], + [6.883996, 0.7625799, 1.0], + [0.0, 1.0, 1.0], + [3.9320729, 0.6744753, 1.0], + [4.8258367, 0.61940265, 1.0], + [5.4672303, 0.7232466, 1.0], + [8.657131, 0.7020352, 1.0], + [0.0, 1.0, 1.0], + [1.7061602, 0.8554576, 1.0], + [1.1815847, 0.8894174, 1.0], + [1.0903149, 0.93655825, 1.0], + [2.8275063, 0.8868961, 1.0], + [0.0, 1.0, 1.0], + [0.90848404, 0.85196733, 1.0], + [0.4342817, 0.9318938, 1.0], + [0.04572374, 0.9942397, 1.0], + [0.0133670345, 0.9983886, 1.0], + [0.0, 1.0, 1.0], + [0.4749884, 0.8968156, 1.0], + [0.13983242, 0.95840764, 1.0], + [0.012114237, 0.9945305, 1.0], + [0.004142619, 0.9975343, 1.0], + [0.0, 1.0, 1.0], + [0.2421307, 0.94426733, 1.0], + [0.025308255, 0.97187287, 1.0], + [-0.013660883, 0.9893051, 1.0], + [-0.014684059, 0.9894599, 1.0], + [0.0, 1.0, 1.0], + [-0.15639894, 0.9373672, 1.0], + [-0.3664362, 0.8935984, 1.0], + [-0.15253535, 0.9533504, 1.0], + [-0.039207056, 0.98815924, 1.0], + [0.0, 1.0, 1.0], + [-0.43275413, 0.8943086, 1.0], + [-0.70259815, 0.840497, 1.0], + [-0.252648, 0.937427, 1.0], + [-0.03330618, 0.9936832, 1.0], + [0.0, 1.0, 1.0], + [0.06659569, 0.87807643, 1.0], + [-0.1700009, 0.8497151, 1.0], + [-0.12636127, 0.963959, 1.0], + [-0.013172978, 0.9975136, 1.0], + [0.0, 1.0, 1.0], + [-0.64482135, 0.87148684, 1.0], + [-0.74943525, 0.83117217, 1.0], + [-0.27330378, 0.93200976, 1.0], + [-0.021869717, 0.9946897, 1.0], + [0.0, 1.0, 1.0], + [-1.0401838, 0.8728109, 1.0], + [-0.73823136, 0.8744596, 1.0], + [-0.18287456, 0.95961267, 1.0], + [-0.009951859, 0.9977242, 1.0], + [0.0, 1.0, 1.0], + [-0.22627981, 0.88137406, 1.0], + [0.007839352, 0.9245987, 1.0], + [0.0014544588, 0.99349904, 1.0], + [-2.9291497e-05, 0.99989325, 1.0], + [0.0, 1.0, 1.0], + [-0.18447767, 0.8809199, 1.0], + [0.008496483, 0.9478462, 1.0], + [0.0067973994, 0.9979893, 1.0], + [5.0736588e-05, 0.9999927, 1.0], + [0.0, 1.0, 1.0], + [0.29172453, 0.92011845, 1.0], + [0.15676522, 0.9734236, 1.0], + [0.004252948, 0.99931306, 1.0], + [-1.0232604e-05, 0.9999954, 1.0], + [0.0, 1.0, 1.0], + [0.17869076, 0.9557188, 1.0], + [0.08755076, 0.99127334, 1.0], + [0.0010010353, 0.99973154, 1.0], + [-5.539131e-05, 0.9999892, 1.0], + [0.0, 1.0, 1.0], + [0.22946571, 0.9652117, 1.0], + [0.05355996, 0.9938835, 1.0], + [0.0004149077, 0.9998538, 1.0], + [8.350738e-06, 0.9999956, 1.0], + [0.0, 1.0, 1.0], + [-0.037329286, 0.9731236, 1.0], + [0.006478223, 0.9956182, 1.0], + [0.00065915973, 0.9999197, 1.0], + [2.1461079e-05, 0.9999988, 1.0], + [0.0, 1.0, 1.0], + [0.038507767, 0.98602444, 1.0], + [0.009278379, 0.9980656, 1.0], + [0.00043359466, 0.9999601, 1.0], + [1.2074154e-05, 0.99999934, 1.0], + [0.0, 1.0, 1.0], + [0.024321932, 0.99058217, 1.0], + [0.0005276345, 0.99881136, 1.0], + [-0.000108261214, 0.99998933, 1.0], + [-2.0593393e-06, 1.0, 1.0], + [0.0, 1.0, 1.0], + [0.08471028, 0.98001426, 1.0], + [0.007567339, 0.9977277, 1.0], + [-0.000107173975, 0.9999823, 1.0], + [-1.4792153e-06, 1.0, 1.0], + [0.0, 1.0, 1.0], + [0.21450363, 0.97752666, 1.0], + [0.027476575, 0.9972575, 1.0], + [1.0573019e-05, 0.99997705, 1.0], + [-2.127626e-06, 1.0, 1.0], + [0.0, 1.0, 1.0], + [0.13710256, 0.98749036, 1.0], + [0.021214448, 0.9985921, 1.0], + [0.00022974156, 0.99998766, 1.0], + [6.995456e-08, 1.0, 1.0], + [0.0, 1.0, 1.0], + [0.034353305, 0.9869362, 1.0], + [-0.0033373374, 0.99891263, 1.0], + [-0.0008769339, 0.99998987, 1.0], + [-6.4116425e-06, 0.99999994, 1.0], + [0.0, 1.0, 1.0], + [0.029380038, 0.9895625, 1.0], + [-0.010520089, 0.99891555, 1.0], + [-0.0019082854, 0.9999861, 1.0], + [-1.3866463e-05, 0.99999994, 1.0], + [0.0, 1.0, 1.0], + [0.085345775, 0.9883013, 1.0], + [-0.0024072328, 0.99874765, 1.0], + [-0.001102076, 0.99999017, 1.0], + [-6.650834e-06, 1.0, 1.0], + [0.0, 1.0, 1.0], + [0.09333435, 0.9841484, 1.0], + [0.007872991, 0.99843925, 1.0], + [-0.00031172566, 0.9999886, 1.0], + [-2.2500953e-06, 1.0, 1.0], + [0.0, 1.0, 1.0], + [-0.1495411, 0.9844692, 1.0], + [-0.009347158, 0.9987978, 1.0], + [-4.104763e-05, 0.9999937, 1.0], + [-4.1019636e-08, 1.0, 1.0], + [0.0, 1.0, 1.0], + [-0.13588922, 0.99047935, 1.0], + [-0.011877171, 0.9992225, 1.0], + [-0.00016244329, 0.99999523, 1.0], + [-4.7618443e-07, 1.0, 1.0], + [0.0, 1.0, 1.0], + [-0.18492526, 0.98800945, 1.0], + [-0.01919961, 0.9989189, 1.0], + [-0.00018170738, 0.9999931, 1.0], + [-2.8776438e-07, 1.0, 1.0], + [0.0, 1.0, 1.0], + [-0.5042778, 0.9758248, 1.0], + [-0.05973959, 0.99744993, 1.0], + [-0.00048369938, 0.99998105, 1.0], + [-3.4633175e-07, 1.0, 1.0], + [0.0, 1.0, 1.0], + [-1.1252605, 0.9543784, 1.0], + [-0.18540663, 0.99284697, 1.0], + [-0.0028871074, 0.99987376, 1.0], + [-9.758851e-06, 0.99999946, 1.0], + [0.0, 1.0, 1.0], + [-1.6847491, 0.9297688, 1.0], + [-0.30172828, 0.9871863, 1.0], + [-0.006863096, 0.9996188, 1.0], + [-3.727347e-05, 0.99999744, 1.0], + [0.0, 1.0, 1.0], + [-2.370997, 0.89369446, 1.0], + [-1.2190995, 0.9472215, 1.0], + [-0.12854093, 0.99495983, 1.0], + [-0.0013108054, 0.999956, 1.0], + [0.0, 1.0, 1.0], + [-4.9047966, 0.7847389, 1.0], + [-5.3030334, 0.8162339, 1.0], + [-1.4172684, 0.9577376, 1.0], + [-0.026107062, 0.999312, 1.0], + [0.0, 1.0, 1.0], + [-5.9655056, 0.73402, 1.0], + [-5.2766113, 0.82234603, 1.0], + [-1.8166275, 0.95730686, 1.0], + [-0.073980905, 0.99863815, 1.0], + [0.0, 1.0, 1.0], + [-7.649344, 0.66660523, 1.0], + [-6.673369, 0.77574635, 1.0], + [-3.8847363, 0.9271979, 1.0], + [-0.35958254, 0.9939365, 1.0], + [0.0, 1.0, 1.0], + [-8.770011, 0.6152831, 1.0], + [-8.663781, 0.6730373, 1.0], + [-4.5796795, 0.8964258, 1.0], + [-0.38371933, 0.9933861, 1.0], + [0.0, 1.0, 1.0], + [-9.064724, 0.563459, 1.0], + [-8.883273, 0.5998541, 1.0], + [-3.3382602, 0.8610413, 1.0], + [-0.06820223, 0.9978487, 1.0], + [0.0, 1.0, 1.0], + [-8.522412, 0.5443333, 1.0], + [-10.088787, 0.5584405, 1.0], + [-8.74115, 0.69652474, 1.0], + [-0.46212718, 0.9844496, 1.0], + [0.0, 1.0, 1.0], + [-5.3416877, 0.5573365, 1.0], + [-10.180397, 0.54992926, 1.0], + [-10.34654, 0.65771925, 1.0], + [-2.239636, 0.95445883, 1.0], + [0.0, 1.0, 1.0], + [-0.17010239, 0.6200763, 1.0], + [-3.7829561, 0.64616305, 1.0], + [-7.5884185, 0.8539007, 1.0], + [-7.036888, 0.9037384, 1.0], + [0.0, 1.0, 1.0], + [2.296787, 0.8043461, 1.0], + [1.2797425, 0.88472384, 1.0], + [-2.9147022, 0.94889057, 1.0], + [-2.639398, 0.96286654, 1.0], + [0.0, 1.0, 1.0], + [1.1746049, 0.87596625, 1.0], + [2.4819775, 0.8980377, 1.0], + [0.43790334, 0.9830677, 1.0], + [-0.006361744, 0.9996459, 1.0], + [0.0, 1.0, 1.0], + [1.4755538, 0.92148954, 1.0], + [3.057467, 0.9028049, 1.0], + [0.5373922, 0.9829736, 1.0], + [0.005417032, 0.99982744, 1.0], + [0.0, 1.0, 1.0], + [0.19876552, 0.97760195, 1.0], + [0.26070496, 0.99011695, 1.0], + [0.033617545, 0.9989797, 1.0], + [0.0002978694, 0.99999154, 1.0], + [0.0, 1.0, 1.0], + [0.07401737, 0.9809595, 1.0], + [0.0669684, 0.9947014, 1.0], + [0.0036516946, 0.99973106, 1.0], + [1.5824207e-05, 0.99999887, 1.0], + [0.0, 1.0, 1.0], + [-0.24908549, 0.9819961, 1.0], + [-0.027645566, 0.9976866, 1.0], + [-0.00042068172, 0.9999474, 1.0], + [1.7829321e-06, 0.9999998, 1.0], + [0.0, 1.0, 1.0], + [-0.45939854, 0.97829735, 1.0], + [-0.06557831, 0.99736834, 1.0], + [-0.0014225548, 0.99993753, 1.0], + [7.041205e-07, 0.99999976, 1.0], + [0.0, 1.0, 1.0], + [-0.97001255, 0.94978815, 1.0], + [-0.06016365, 0.9956392, 1.0], + [0.0005834204, 0.9998938, 1.0], + [1.0331767e-05, 0.99999905, 1.0], + [0.0, 1.0, 1.0], + [0.8007346, 0.9450692, 1.0], + [0.061064422, 0.9958661, 1.0], + [0.0004179476, 0.99996316, 1.0], + [-5.38278e-07, 0.99999976, 1.0], + [0.0, 1.0, 1.0], + [0.7554829, 0.97007614, 1.0], + [0.038412966, 0.99764246, 1.0], + [-1.2250363e-05, 0.99998575, 1.0], + [-4.3817803e-07, 1.0, 1.0], + [0.0, 1.0, 1.0], + [0.4214841, 0.98421025, 1.0], + [0.018357918, 0.9986999, 1.0], + [-0.0001539443, 0.99998885, 1.0], + [-2.1144313e-06, 0.99999994, 1.0], + [0.0, 1.0, 1.0], + [0.26302785, 0.9877291, 1.0], + [0.016418263, 0.99897736, 1.0], + [0.00017517614, 0.999994, 1.0], + [3.7461887e-06, 1.0, 1.0], + [0.0, 1.0, 1.0], + [0.2549705, 0.9871261, 1.0], + [0.019887371, 0.99840975, 1.0], + [-3.7766345e-05, 0.99995524, 1.0], + [-7.270728e-06, 0.9999991, 1.0], + [0.0, 1.0, 1.0], + [0.57600856, 0.9830809, 1.0], + [0.028567612, 0.99657667, 1.0], + [-0.00476558, 0.99963075, 1.0], + [-0.00016184518, 0.9999903, 1.0], + [0.0, 1.0, 1.0], + [0.023581166, 0.9658987, 1.0], + [-0.12978074, 0.9894591, 1.0], + [-0.010897471, 0.999496, 1.0], + [-0.0003179809, 0.9999912, 1.0], + [0.0, 1.0, 1.0], + [-0.48003206, 0.947442, 1.0], + [-0.28566536, 0.98114216, 1.0], + [-0.02372709, 0.99919075, 1.0], + [-0.0012802386, 0.9999792, 1.0], + [0.0, 1.0, 1.0], + [-0.30012533, 0.9344908, 1.0], + [-0.415363, 0.9781973, 1.0], + [-0.04023375, 0.99892974, 1.0], + [-0.002275092, 0.9999678, 1.0], + [0.0, 1.0, 1.0], + [1.2335646, 0.91651773, 1.0], + [-0.35089287, 0.9740984, 1.0], + [-0.03576802, 0.9988748, 1.0], + [-0.0019647575, 0.99997276, 1.0], + [0.0, 1.0, 1.0], + [4.4362745, 0.8536556, 1.0], + [-0.5057072, 0.96044815, 1.0], + [-0.05471109, 0.99809283, 1.0], + [-0.00035249075, 0.99999046, 1.0], + [0.0, 1.0, 1.0], + [12.081045, 0.7132523, 1.0], + [0.5422661, 0.9341002, 1.0], + [-0.06538023, 0.99676913, 1.0], + [-0.00047848828, 0.9999808, 1.0], + [0.0, 1.0, 1.0], + [17.529392, 0.60886335, 1.0], + [1.5537803, 0.8626087, 1.0], + [-0.3528088, 0.98406494, 1.0], + [-0.0052189324, 0.99973387, 1.0], + [0.0, 1.0, 1.0], + [18.210768, 0.5879146, 1.0], + [1.4481329, 0.77672154, 1.0], + [-0.9064065, 0.9183754, 1.0], + [-0.024934258, 0.99743956, 1.0] + ], + "srgb_value": true + } +} diff --git a/contributors/people.toml b/contributors/people.toml index 27a8c8d9c..bec67597c 100644 --- a/contributors/people.toml +++ b/contributors/people.toml @@ -25,9 +25,23 @@ sync_github_name = false [people.rock-meister] sync_github_name = true +[people.Th4nat0s] +display_name = "Thanat0s" + [people.sanyamsingh001] display_name = "sanyamsingh001" [people.ArekatlaNishanthchowdary] real_name = "Arekatla Nishanth Chowdary" sync_github_name = true + +[people.Losminek] +sync_github_name = true + +[people.FooVoo] +real_name = "Aleksandr Batyrev" +sync_github_name = true + +[people.Silvertree2010] +display_name = "Netter" +sync_github_name = false diff --git a/crates/catalog/src/journal.rs b/crates/catalog/src/journal.rs index 98fd616a3..289ee4fb8 100644 --- a/crates/catalog/src/journal.rs +++ b/crates/catalog/src/journal.rs @@ -51,6 +51,7 @@ //! | 6 | folder labels | `Op::SetFolderRecord`, `Catalog.folder_records` | //! | 7 | Kelvin white balance for raws whose camera has matrices (#730) | `Op::SetAsShotWb`, `WhiteBalance.scale` | //! | 8 | History cursor: Go to History Step adds no step | `Op::SetHistoryCursor`, `Photo.history_cursor` | +//! | 9 | preview-only recheck | `Photo.preview_checked`, `Op::SetContent.preview_checked` | //! //! Rules: //! - **Bump [`VERSION`]** (and add a row above) in the change that adds an [`Op`] variant or a @@ -75,7 +76,7 @@ pub const LOG: &str = "catalog.log"; const FORMAT: &str = "lightcraft-catalog"; /// The catalog format this build writes (and the newest it reads). See the module docs → /// *Format versions*; bump it whenever an [`Op`] variant or a serialized field is added. -pub const VERSION: u32 = 8; +pub const VERSION: u32 = 9; /// When [`Journal::wants_snapshot`] says it's time to compact the log. #[derive(Clone, Copy, Debug)] diff --git a/crates/catalog/src/lib.rs b/crates/catalog/src/lib.rs index ed4cb75b4..df2713657 100644 --- a/crates/catalog/src/lib.rs +++ b/crates/catalog/src/lib.rs @@ -25,6 +25,7 @@ pub mod rules; pub mod safe_file; pub mod stacks; pub mod store; +pub mod tally; use std::collections::BTreeMap; use std::sync::Arc; @@ -214,6 +215,10 @@ pub enum Op { /// `None` = its raw data decodes (or it isn't a raw). #[serde(default, skip_serializing_if = "Option::is_none")] preview_only: Option, + /// The app version that decided `preview_only` (see [`Photo::preview_checked`]); empty + /// when it is `None`. + #[serde(default, skip_serializing_if = "String::is_empty")] + preview_checked: String, }, /// The lens data a photo's file carries (what Reload finds when it was read after import). SetEmbeddedLens { @@ -281,6 +286,71 @@ pub struct Catalog { /// Increments on every applied op. #[serde(skip)] pub revision: u64, + /// Which photos the latest ops touched ([`Catalog::touched_since`]). + #[serde(skip)] + changes: Changes, +} + +/// What of a photo an op changed: enough to tell which tallies of the library an edit leaves as they were (a +/// rating changes no folder's count; a slider changes none at all). +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +pub struct Aspects(u16); + +impl Aspects { + pub const NONE: Aspects = Aspects(0); + pub const RATING: Aspects = Aspects(1); + pub const FLAG: Aspects = Aspects(1 << 1); + pub const LABEL: Aspects = Aspects(1 << 2); + /// Develop settings, versions and history. + pub const DEVELOP: Aspects = Aspects(1 << 3); + /// Title, caption, keywords, people, place and the rest of the metadata. + pub const META: Aspects = Aspects(1 << 4); + pub const CAPTURED: Aspects = Aspects(1 << 5); + /// Where its file is. + pub const FILE: Aspects = Aspects(1 << 6); + pub const ANALYSIS: Aspects = Aspects(1 << 7); + /// Whether it is in the library at all: added, removed, deleted, restored, taken in from Local. + pub const PRESENCE: Aspects = Aspects(1 << 8); + /// Everything: the photo may count differently for any tally. + pub const ALL: Aspects = Aspects(u16::MAX); + + pub const fn with(self, other: Aspects) -> Aspects { + Aspects(self.0 | other.0) + } + + /// Whether any of `other` is among these. + pub const fn any_of(self, other: Aspects) -> bool { + self.0 & other.0 != 0 + } +} + +/// The most photos remembered as touched; older changes are then answered with "anything may have changed". +const MAX_TOUCHED: usize = 8192; + +/// A short memory of what the latest ops changed, so that what is derived from the catalog (the grid's list) can +/// follow a small edit without being worked out again from every photo. +#[derive(Clone, Debug, Default)] +struct Changes { + /// The photos ops changed, each with the revision the op made and what of the photo it changed. Oldest first. + touched: std::collections::VecDeque<(u64, PhotoId, Aspects)>, + /// What the op being applied changes of its photo. + now: Aspects, + /// The latest revision made by an op that changed more than photos of its own (an album, a stack, a keyword of + /// the list…): what it changed for which photos is not known. + broad: u64, + /// Changes up to this revision are no longer all in `touched`. + floor: u64, + /// The revision the last op recorded here made. When the catalog's revision is another, it was set from + /// 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, +} + +impl PartialEq for Changes { + /// Two catalogs are the same catalog whatever each remembers of how it got there. + fn eq(&self, _: &Self) -> bool { + true + } } impl Catalog { @@ -479,20 +549,110 @@ impl Catalog { // ---- writes fn photo_mut(&mut self, id: PhotoId) -> Result<&mut Photo> { + self.touch(id); self.photos.get_mut(&id).map(Arc::make_mut).ok_or(CatalogError::NoPhoto(id)) } + + /// The op being applied changes photo `id` (or adds or removes it). + fn touch(&mut self, id: PhotoId) { + // (the revision the op will make: `apply` counts up once it is done) + self.changes.touched.push_back((self.revision.saturating_add(1), id, self.changes.now)); + while self.changes.touched.len() > MAX_TOUCHED { + if let Some((revision, _, _)) = self.changes.touched.pop_front() { + self.changes.floor = revision; + } + } + } + + /// What of its photos the ops since `revision` changed, taken together ([`Aspects::NONE`] when there were + /// none). `None` when that is not known ([`Catalog::touched_since`]). A tally of the library that reads only + /// other things of a photo is still right. + pub fn aspects_since(&self, revision: u64) -> Option { + if !self.remembers(revision) { + return None; + } + Some(self.changes.touched.iter().filter(|(r, _, _)| *r > revision).fold(Aspects::NONE, |all, (_, _, a)| all.with(*a))) + } + + /// Whether every change since the catalog was at `revision` is remembered photo by photo. + fn remembers(&self, revision: u64) -> bool { + self.changes.seen == self.revision && revision <= self.revision && revision >= self.changes.floor && self.changes.broad <= revision + } + + /// The photos changed, added or removed since the catalog was at `revision`, each once. `None` when that is not + /// known: an op since then changed more than photos of its own, or it was too long ago. Then anything may + /// have changed. + pub fn touched_since(&self, revision: u64) -> Option> { + if !self.remembers(revision) { + return None; + } + let mut ids: Vec = self.changes.touched.iter().filter(|(r, _, _)| *r > revision).map(|(_, id, _)| *id).collect(); + ids.sort_unstable(); + ids.dedup(); + Some(ids) + } fn album_mut(&mut self, id: AlbumId) -> Result<&mut Album> { self.albums.get_mut(&id).ok_or(CatalogError::NoAlbum(id)) } /// Apply an op; returns its inverse. On error nothing changes. pub fn apply(&mut self, op: Op) -> Result { + 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)?; self.revision += 1; + self.changes.seen = self.revision; Ok(inv) } fn apply_inner(&mut self, op: Op) -> Result { + // An op that changes one photo says which (`photo_mut`, and adding or removing it below). Every other kind + // can change what any photo matches or where it sorts: an album's photos or rules, a stack, a keyword's + // name. Listed kind by kind, so a new one has to be put on one side. + match &op { + // (a photo that comes, goes, or changes what it is to the library counts for every tally; so do the + // rare kinds whose reach nobody has to think about) + Op::AddPhoto { photo } => { + self.changes.now = Aspects::ALL; + self.touch(photo.id); + } + Op::RemovePhoto { id } => { + self.changes.now = Aspects::ALL; + self.touch(*id); + } + Op::SetDeleted { .. } | Op::SetLocal { .. } | Op::SetContent { .. } | Op::SetEmbeddedLens { .. } | Op::SetAsShotWb { .. } => { + self.changes.now = Aspects::ALL + } + Op::SetRating { .. } => self.changes.now = Aspects::RATING, + Op::SetFlag { .. } => self.changes.now = Aspects::FLAG, + Op::SetLabel { .. } => self.changes.now = Aspects::LABEL, + Op::SetMeta { .. } => self.changes.now = Aspects::META, + Op::SetCaptured { .. } => self.changes.now = Aspects::CAPTURED, + Op::SetFile { .. } | Op::Relink { .. } => self.changes.now = Aspects::FILE, + Op::SetAnalysis { .. } => self.changes.now = Aspects::ANALYSIS, + Op::SetDevelop { .. } | Op::SetVersions { .. } | Op::SetHistory { .. } | Op::PushHistory { .. } | Op::SetHistoryCursor { .. } => { + self.changes.now = Aspects::DEVELOP + } + // (its ops say for themselves, one by one) + Op::Batch { .. } => {} + Op::AddAlbum { .. } + | Op::RemoveAlbum { .. } + | Op::RenameAlbum { .. } + | Op::MoveAlbum { .. } + | Op::SetAlbumOrder { .. } + | Op::SetAlbumPhotos { .. } + | Op::SetAlbumCover { .. } + | Op::SetAlbumRules { .. } + | Op::AddStack { .. } + | Op::RemoveStack { .. } + | Op::SetStack { .. } + | Op::SetLabelName { .. } + | Op::SetBrowsed { .. } + | Op::SetKeyword { .. } + | Op::SetFolderRecord { .. } => self.changes.broad = self.revision.saturating_add(1), + } Ok(match op { Op::AddPhoto { photo } => { if self.photos.contains_key(&photo.id) { @@ -700,7 +860,7 @@ impl Catalog { let old_format = format.map(|f| std::mem::replace(&mut p.format, f)); Op::Relink { id, file_name: old_name, source: std::mem::replace(&mut p.source, source), format: old_format } } - Op::SetContent { id, width, height, file_size, content_hash, preview_only } => { + Op::SetContent { id, width, height, file_size, content_hash, preview_only, preview_checked } => { let p = self.photo_mut(id)?; Op::SetContent { id, @@ -709,6 +869,7 @@ impl Catalog { file_size: std::mem::replace(&mut p.file_size, file_size), content_hash: std::mem::replace(&mut p.content_hash, content_hash), preview_only: std::mem::replace(&mut p.preview_only, preview_only), + preview_checked: std::mem::replace(&mut p.preview_checked, preview_checked), } } Op::SetEmbeddedLens { id, lens } => { diff --git a/crates/catalog/src/local.rs b/crates/catalog/src/local.rs index 9cfc14f35..093c22427 100644 --- a/crates/catalog/src/local.rs +++ b/crates/catalog/src/local.rs @@ -183,15 +183,7 @@ impl Catalog { let mut plan = ForgetPlan { now: now.to_string(), ..Default::default() }; let now_s = crate::stacks::iso_seconds(now); let max_age = i64::from(days) * 86_400; - let mut referenced: HashSet = HashSet::new(); - for a in self.albums.values() { - referenced.extend(a.photos.iter().copied()); - referenced.extend(a.cover); - } - for s in self.stacks.values() { - referenced.extend(s.photos.iter().copied()); - } - referenced.extend(self.photos.values().filter_map(|p| p.copy_of)); + let referenced = self.referenced(); // the fingerprints are the costly part: work them out for the candidates in parallel let candidates: Vec<&Photo> = self .photos @@ -237,6 +229,30 @@ impl Catalog { plan.unstamp = self.browsed.keys().filter(|f| !remaining.contains(*f)).cloned().collect(); plan } + + /// Of `ids`, the untouched Local records nothing refers to (no album, stack or virtual copy): + /// the ones [`Catalog::forget_local_plan`] could forget, whatever their folder's time. + pub fn forgettable_local(&self, ids: &[PhotoId]) -> Vec { + let referenced = self.referenced(); + let candidates: Vec<&Photo> = + ids.iter().filter(|id| !referenced.contains(id)).filter_map(|id| self.photos.get(id)).filter(|p| p.local).map(|p| &**p).collect(); + let untouched = untouched_of(&candidates); + ids.iter().copied().filter(|id| untouched.contains(id)).collect() + } + + /// Photos an album (as a member or its cover), a stack or a virtual copy refers to. + fn referenced(&self) -> HashSet { + let mut referenced: HashSet = HashSet::new(); + for a in self.albums.values() { + referenced.extend(a.photos.iter().copied()); + referenced.extend(a.cover); + } + for s in self.stacks.values() { + referenced.extend(s.photos.iter().copied()); + } + referenced.extend(self.photos.values().filter_map(|p| p.copy_of)); + referenced + } } /// A folder browsed at `at` is past the period (never when `days` is 0 or a time is unreadable). diff --git a/crates/catalog/src/model.rs b/crates/catalog/src/model.rs index 434ea3a1e..d195441db 100644 --- a/crates/catalog/src/model.rs +++ b/crates/catalog/src/model.rs @@ -293,6 +293,12 @@ pub struct Photo { /// picture style baked in — so it is treated as a rendered (non-raw) source. #[serde(default, skip_serializing_if = "Option::is_none")] pub preview_only: Option, + /// The app version (`CARGO_PKG_VERSION`) whose decoder set [`Photo::preview_only`]; empty when + /// it isn't set, and in catalogs from before format v9. A preview-only photo whose stamp + /// differs from the running version is rechecked when its library opens (issue #662): newer + /// builds may decode the file, older ones may decode less. + #[serde(default, skip_serializing_if = "String::is_empty")] + pub preview_checked: String, /// A Local record: the fingerprint of its state when the browse catalogued it, to tell /// whether the user changed anything since (see [`crate::local`]). #[serde(default, skip_serializing_if = "Option::is_none")] @@ -318,7 +324,7 @@ pub struct Analysis { /// file's white balance is edited relative to its as-shot look or in Kelvin depends on the camera: /// [`relative_wb_camera`]. pub fn relative_wb_format(format: &str) -> bool { - ["ARW", "NEF", "NRW", "RW2", "RWL", "RAW", "RAF", "CR3", "CR2", "PEF", "SRW", "ORF"].iter().any(|f| format.eq_ignore_ascii_case(f)) + ["ARW", "NEF", "NRW", "RW2", "RWL", "RAW", "RAF", "CR3", "CR2", "PEF", "SRW", "ORF", "ERF"].iter().any(|f| format.eq_ignore_ascii_case(f)) } /// Whether a raw of `format` shot on `camera` (Exif make and model, as [`Meta::camera`] keeps them) is developed @@ -332,6 +338,16 @@ pub fn relative_wb_camera(format: &str, camera: &str) -> bool { relative_wb_format(format) && lightcraft_raw::spectral::find_camera(camera).is_none() && !lightcraft_raw::kelvin_presets::covers_camera(camera) } +/// This build's version: what a photo's [`Photo::preview_checked`] stamp is compared with. Every +/// workspace crate shares one version, so it is the app's. +pub const APP_VERSION: &str = env!("CARGO_PKG_VERSION"); + +/// The [`Photo::preview_checked`] stamp for a `preview_only` decided by this build: its version +/// when the photo is preview-only, empty otherwise. +pub fn preview_stamp(preview_only: &Option) -> String { + if preview_only.is_some() { APP_VERSION.to_string() } else { String::new() } +} + impl Photo { /// The History step the settings are at (the cursor, clamped to the list; `None` = no history). pub fn history_index(&self) -> Option { @@ -371,9 +387,20 @@ impl Photo { import_look: None, analysis: None, preview_only: None, + preview_checked: String::new(), local_baseline: None, } } + /// Sets [`Photo::preview_only`] as this build decided it, with its stamp. + pub fn set_preview_only(&mut self, preview_only: Option) { + self.preview_checked = preview_stamp(&preview_only); + self.preview_only = preview_only; + } + /// A preview-only photo whose flag was set by another app version (or before stamps existed), + /// so this build should probe its file again (issue #662). + pub fn preview_only_stale(&self) -> bool { + self.preview_only.is_some() && self.preview_checked != APP_VERSION + } /// A raw file developed from its sensor data: not a rendered image, and not a raw shown from /// its embedded preview ([`Photo::preview_only`]). pub fn develops_raw(&self) -> bool { @@ -577,6 +604,7 @@ mod edited_tests { ("a.cr2", "CR2", true), ("a.pef", "pef", true), ("a.orf", "ORF", true), + ("a.erf", "ERF", true), ("a.dng", "DNG", false), ] { let mut p = Photo::new(PhotoId(1), Source::Demo { scene: 0 }, name, format, 10, 10, "2026-10-01T00:00:00"); @@ -609,6 +637,8 @@ mod edited_tests { ("SONY ILCE-7RM4A", "ARW", false), ("NIKON CORPORATION NIKON D850", "NEF", false), ("Canon Canon EOS 5D Mark III", "CR2", false), + ("Canon Canon EOS R6m2", "CR3", false), + ("Canon Canon EOS R7", "CR3", true), ("FUJIFILM X-T4", "RAF", false), ("ILCE-7M3", "ARW", false), ("SONY ILCE-7M2", "ARW", true), @@ -624,7 +654,11 @@ mod edited_tests { p.as_shot_wb = Some((4986.0, -2.0)); assert_eq!(p.relative_wb(), relative, "{camera} {format}"); assert_eq!(relative_wb_camera(format, camera), relative, "{camera} {format}"); - assert_eq!(p.fitted_look(), format == "ARW" || format == "NEF" || format == "CR2" || format == "RAF", "{camera} {format}"); + assert_eq!( + p.fitted_look(), + format == "ARW" || format == "NEF" || format == "CR2" || format == "CR3" || format == "RAF", + "{camera} {format}" + ); let wb = p.camera_defaults().wb; assert_eq!((wb.temp, wb.tint), if relative { (6500.0, 0.0) } else { (4986.0, -2.0) }, "{camera} {format}"); assert_eq!(wb.scale, if relative { WbScale::Legacy } else { WbScale::Kelvin }, "{camera} {format}"); diff --git a/crates/catalog/src/query.rs b/crates/catalog/src/query.rs index a9956703d..883319b32 100644 --- a/crates/catalog/src/query.rs +++ b/crates/catalog/src/query.rs @@ -94,6 +94,27 @@ pub struct Sort { pub seed: u64, } +impl Sort { + /// Which of two photos comes first in this order (never `Equal` for two photos: the id decides last). + pub fn order(&self, a: &Photo, b: &Photo) -> std::cmp::Ordering { + let o = match self.key { + SortKey::CaptureDate => a.captured.cmp(&b.captured).then_with(|| a.imported.cmp(&b.imported)), + SortKey::ImportDate => a.imported.cmp(&b.imported), + SortKey::EditDate => a.edited.cmp(&b.edited), + SortKey::FileName => cmp_name(&a.file_name, &b.file_name), + SortKey::Rating => a.rating.cmp(&b.rating), + SortKey::FileSize => a.file_size.cmp(&b.file_size), + SortKey::Random => shuffle_rank(self.seed, a.id).cmp(&shuffle_rank(self.seed, b.id)), + } + // Ties (one import batch, a burst within a second, equal ratings) sort by file name, the + // order cameras number frames, so the order doesn't depend on how ids were allocated; + // the id only keeps it stable. + .then_with(|| if self.key == SortKey::FileName { Equal } else { cmp_name(&a.file_name, &b.file_name) }) + .then(a.id.cmp(&b.id)); + if self.ascending { o } else { o.reverse() } + } +} + impl Default for Sort { fn default() -> Self { Sort { key: SortKey::CaptureDate, ascending: false, group: crate::GroupBy::Auto, seed: 0 } @@ -369,38 +390,65 @@ pub fn cmp_name(a: &str, b: &str) -> std::cmp::Ordering { impl Catalog { /// Photos matching `filter`, in `sort` order (ties broken by file name, then id). pub fn query(&self, filter: &Filter, sort: &Sort) -> Vec { + self.query_keeping(filter, sort, &std::collections::HashSet::new(), filter) + } + + /// [`Catalog::query`], and with them the photos of `keep` that still match `within`, each at its place in `sort` + /// order: a view that holds the photos it showed when an edit makes them stop matching `filter` (a smart + /// album's rules, the filter bar), as long as they are still where the view looks (`within`: in the library, + /// the album, the folder; not deleted). + pub fn query_keeping(&self, filter: &Filter, sort: &Sort, keep: &std::collections::HashSet, within: &Filter) -> Vec { let root = filter.library_root(); - let mut v: Vec<&Photo> = self.photos().map(|p| p.as_ref()).filter(|p| filter.matches_in(p, self, root.as_deref())).collect(); - v.sort_by(|a, b| { - let o = match sort.key { - SortKey::CaptureDate => a.captured.cmp(&b.captured).then_with(|| a.imported.cmp(&b.imported)), - SortKey::ImportDate => a.imported.cmp(&b.imported), - SortKey::EditDate => a.edited.cmp(&b.edited), - SortKey::FileName => cmp_name(&a.file_name, &b.file_name), - SortKey::Rating => a.rating.cmp(&b.rating), - SortKey::FileSize => a.file_size.cmp(&b.file_size), - SortKey::Random => shuffle_rank(sort.seed, a.id).cmp(&shuffle_rank(sort.seed, b.id)), - } - // Ties (one import batch, a burst within a second, equal ratings) sort by file name, the - // order cameras number frames, so the order doesn't depend on how ids were allocated; - // the id only keeps it stable. - .then_with(|| if sort.key == SortKey::FileName { Equal } else { cmp_name(&a.file_name, &b.file_name) }) - .then(a.id.cmp(&b.id)); - if sort.ascending { o } else { o.reverse() } - }); + let within_root = within.library_root(); + let mut v: Vec<&Photo> = self + .photos() + .map(|p| p.as_ref()) + .filter(|p| filter.matches_in(p, self, root.as_deref()) || (keep.contains(&p.id) && within.matches_in(p, self, within_root.as_deref()))) + .collect(); + v.sort_by(|a, b| sort.order(a, b)); v.into_iter().map(|p| p.id).collect() } + /// The list `was` (a [`Catalog::query`] of `filter` in `sort` order, or a [`Catalog::query_keeping`]), after ops + /// that touched only the photos `touched` (sorted by id, each once: [`Catalog::touched_since`]): those photos + /// taken out, and the ones that match `filter` now put back at their place in the order. With `within`, a + /// touched photo that was in the list and still matches `within` stays, as `query_keeping` would keep it. What + /// the query would give, without asking every photo again. + pub fn follow(&self, was: &[PhotoId], touched: &[PhotoId], filter: &Filter, sort: &Sort, within: Option<&Filter>) -> Vec { + let listed: std::collections::HashSet = was.iter().copied().filter(|id| touched.binary_search(id).is_ok()).collect(); + let mut ids: Vec = was.iter().copied().filter(|id| !listed.contains(id)).collect(); + let (root, within_root) = (filter.library_root(), within.and_then(Filter::library_root)); + for id in touched { + let Some(p) = self.photo(*id) else { continue }; + let kept = within.is_some_and(|w| listed.contains(id) && w.matches_in(p, self, within_root.as_deref())); + if filter.matches_in(p, self, root.as_deref()) || kept { + // (the list is in order without the touched photos, and stays so as each is put back) + let at = ids.partition_point(|other| self.photo(*other).is_some_and(|o| sort.order(o, p).is_lt())); + ids.insert(at, *id); + } + } + ids + } + + /// How many photos match `filter`: [`Catalog::query`]'s length without its sorting. + pub fn count(&self, filter: &Filter) -> usize { + let root = filter.library_root(); + self.photos().filter(|p| filter.matches_in(p, self, root.as_deref())).count() + } + /// Year → month → day counts for the "By Date" section (newest first). pub fn date_groups(&self) -> Vec { use std::collections::BTreeMap; let mut years: BTreeMap<&str, (BTreeMap<&str, usize>, BTreeMap<&str, usize>)> = BTreeMap::new(); for p in self.photos().filter(|p| p.in_library()) { let Some(d) = p.captured.as_deref() else { continue }; - if d.len() >= 10 { - let (months, days) = years.entry(&d[..4]).or_default(); - *months.entry(&d[..7]).or_default() += 1; - *days.entry(&d[..10]).or_default() += 1; + // `get`, not `[..]`: a corrupt or foreign `captured` (a Lightroom catalog's + // captureTime is copied verbatim) may not be ASCII, and slicing it on a byte index + // would panic — the same rule as `dates::group_label`. + if let (Some(year), Some(month), Some(day)) = (d.get(..4), d.get(..7), d.get(..10)) { + let (months, days) = years.entry(year).or_default(); + *months.entry(month).or_default() += 1; + *days.entry(day).or_default() += 1; } } let newest_first = |m: BTreeMap<&str, usize>| m.into_iter().rev().map(|(k, n)| (k.to_string(), n)).collect::>(); diff --git a/crates/catalog/src/stacks.rs b/crates/catalog/src/stacks.rs index 6ffb11f52..1c6063256 100644 --- a/crates/catalog/src/stacks.rs +++ b/crates/catalog/src/stacks.rs @@ -231,6 +231,60 @@ impl Catalog { } } +impl Catalog { + /// The bracket sets among `ids`, an HDR merge each: every stack one of them is in (its photos, less earlier merge + /// results), then the unstacked ones in capture order, a set running while each starts within about a second of the + /// previous one's end (its shutter time; capture times are whole seconds). A run is a set when its exposures span at + /// least ½ EV: a burst at one exposure is no bracket. Photos without a capture time or shutter speed are left out. + pub fn bracket_sets(&self, ids: &[PhotoId]) -> Vec> { + let merged = |p: &crate::Photo| crate::query::merged_kind(&p.file_name).is_some(); + let mut sets: Vec> = Vec::new(); + let mut stacks = HashSet::new(); + let mut loose = Vec::new(); + for id in ids { + match self.stack_of(*id) { + Some(s) if stacks.insert(s.id) => { + let set: Vec = s.photos.iter().copied().filter(|p| self.photos.get(p).is_some_and(|p| !merged(p))).collect(); + if set.len() >= 2 { + sets.push(set); + } + } + Some(_) => {} + None => loose.push(*id), + } + } + // (capture second, file name, id, shutter seconds, EV) + let mut timed: Vec<(i64, &str, PhotoId, f64, f64)> = loose + .iter() + .filter_map(|id| self.photos.get(id)) + .filter(|p| !p.deleted && !merged(p)) + .filter_map(|p| { + let t = iso_seconds(p.captured.as_deref()?)?; + let s = crate::model::parse_shutter_seconds(&p.meta.shutter)?; + let n = p.meta.aperture.filter(|n| n.is_finite() && *n > 0.0).unwrap_or(1.0) as f64; + let iso = p.meta.iso.filter(|i| *i > 0).unwrap_or(100) as f64; + Some((t, p.file_name.as_str(), p.id, s, (s * iso / (n * n)).log2())) + }) + .collect(); + timed.sort_by(|a, b| a.0.cmp(&b.0).then_with(|| cmp_name(a.1, b.1)).then(a.2.cmp(&b.2))); + timed.dedup_by_key(|x| x.2); + let mut runs: Vec> = Vec::new(); + for x in timed { + match runs.last_mut() { + Some(r) if r.last().is_some_and(|l| (x.0 - l.0) as f64 <= l.3.ceil() + 1.0) => r.push(x), + _ => runs.push(vec![x]), + } + } + for r in runs { + let (lo, hi) = r.iter().fold((f64::INFINITY, f64::NEG_INFINITY), |(lo, hi), x| (lo.min(x.4), hi.max(x.4))); + if r.len() >= 2 && hi - lo >= 0.5 { + sets.push(r.iter().map(|x| x.2).collect()); + } + } + sets + } +} + /// Seconds since 1970-01-01 for an ISO 8601 local time (`2026-04-01T10:00:00`, `2026-04-01 10:00`, /// `2026-04-01`; fractions and zone suffixes are ignored). `None` if it doesn't parse. pub fn iso_seconds(s: &str) -> Option { @@ -358,6 +412,46 @@ mod tests { assert_eq!(back.clone().alloc_stack_id(), c.clone().alloc_stack_id()); } + #[test] + fn bracket_sets_by_time_and_exposure() { + // (time, shutter, ISO, file name) + let shots = [ + ("2026-08-01T09:20:36", "1/125", 640, "A1.ARW"), // a handheld bracket + ("2026-08-01T09:20:36", "1/125", 200, "A2.ARW"), + ("2026-08-01T09:20:37", "1/125", 2000, "A3.ARW"), + ("2026-08-01T09:20:37", "", 0, "A3-HDR.dng"), // an earlier merge of it + ("2026-08-01T09:20:44", "1/125", 800, "B1.ARW"), // 7 s later: the next bracket + ("2026-08-01T09:20:44", "1/125", 250, "B2.ARW"), + ("2026-08-01T09:20:50", "1/1000", 160, "C1.ARW"), // a burst at one exposure + ("2026-08-01T09:20:50", "1/1000", 160, "C2.ARW"), + ("2026-06-27T23:34:59", "2", 320, "N1.ARW"), // 2 s night frames, 2.1 s apart + ("2026-06-27T23:35:01", "2", 100, "N2.ARW"), + ("2026-06-27T23:35:04", "2", 800, "N3.ARW"), + ("2026-06-27T23:36:00", "1/40", 250, "S1.ARW"), // stacked by hand: merged whatever its times + ("2026-06-27T23:39:00", "1/40", 250, "S2.ARW"), + ]; + let mut c = Catalog::new(); + let mut ids = Vec::new(); + for (t, sh, iso, name) in shots { + let id = c.alloc_photo_id(); + let mut p = Photo::new(id, Source::Demo { scene: 1 }, name, "ARW", 60, 40, "2026-01-01T00:00:00"); + p.captured = Some(t.into()); + p.meta.shutter = sh.into(); + p.meta.iso = Some(iso); + p.meta.aperture = Some(5.6); + c.apply(Op::AddPhoto { photo: Box::new(p) }).unwrap(); + ids.push(id); + } + let op = c.group_ops(ids[11], &[ids[12]], true).unwrap(); + c.apply(op).unwrap(); + let sets = c.bracket_sets(&ids); + assert_eq!(sets, vec![vec![ids[11], ids[12]], vec![ids[8], ids[9], ids[10]], vec![ids[0], ids[1], ids[2]], vec![ids[4], ids[5]]]); + // a stack's merge result on top is left out; a stack selected twice is merged once + let op = c.stack_with_ops(ids[3], &[ids[0], ids[1], ids[2]]).unwrap(); + c.apply(op).unwrap(); + assert_eq!(c.bracket_sets(&[ids[3], ids[0]]), vec![vec![ids[0], ids[1], ids[2]]]); + } + #[test] fn auto_stack_by_capture_time() { let times = ["2026-04-01T10:00:00", "2026-04-01T10:00:02", "2026-04-01T10:00:09", "2026-04-01T11:00:00", "2026-04-01T11:00:01"]; diff --git a/crates/catalog/src/store.rs b/crates/catalog/src/store.rs index a672d00e7..2f3dc4912 100644 --- a/crates/catalog/src/store.rs +++ b/crates/catalog/src/store.rs @@ -73,6 +73,16 @@ 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 { + if std::fs::symlink_metadata(tmp).is_ok() { + std::fs::remove_file(tmp)?; + } + std::fs::OpenOptions::new().write(true).create_new(true).open(tmp) +} + impl Store for FsStore { fn read(&mut self, name: &str) -> io::Result>> { match std::fs::read(self.path(name)) { @@ -89,7 +99,7 @@ impl Store for FsStore { } let tmp = self.path(&format!("{name}.tmp")); { - let mut f = std::fs::File::create(&tmp)?; + let mut f = create_staging(&tmp)?; f.write_all(data)?; f.sync_all()?; } @@ -105,7 +115,7 @@ impl Store for FsStore { } let tmp = self.path(&format!("{name}.tmp")); let written = (|| { - let mut w = Counter { inner: io::BufWriter::with_capacity(1 << 20, std::fs::File::create(&tmp)?), n: 0 }; + let mut w = Counter { inner: io::BufWriter::with_capacity(1 << 20, create_staging(&tmp)?), n: 0 }; fill(&mut w)?; w.flush()?; let f = w.inner.into_inner().map_err(|e| e.into_error())?; @@ -231,3 +241,29 @@ impl Store for MemStore { self.background.then(|| Box::new(self.clone()) as Box) } } + +#[cfg(test)] +mod tests_staging { + use super::*; + + /// A link planted at a staging name (`.tmp`) is removed, never followed: the file it points to keeps its + /// bytes and the save still lands (issue #1502). + #[cfg(unix)] + #[test] + fn a_link_at_the_staging_name_is_never_written_through() { + let dir = std::env::temp_dir().join(format!("lc-staging-link-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + let mut store = FsStore::open(&dir).unwrap(); + let victim = dir.join("victim.txt"); + std::fs::write(&victim, b"keep").unwrap(); + std::os::unix::fs::symlink(&victim, dir.join("catalog.snap.tmp")).unwrap(); + store.write_atomic("catalog.snap", b"{}").unwrap(); + assert_eq!(std::fs::read(&victim).unwrap(), b"keep"); + assert_eq!(std::fs::read(dir.join("catalog.snap")).unwrap(), b"{}"); + std::os::unix::fs::symlink(&victim, dir.join("catalog.log.tmp")).unwrap(); + store.write_atomic_with("catalog.log", &mut |w| w.write_all(b"[1]")).unwrap(); + assert_eq!(std::fs::read(&victim).unwrap(), b"keep"); + assert_eq!(std::fs::read(dir.join("catalog.log")).unwrap(), b"[1]"); + let _ = std::fs::remove_dir_all(&dir); + } +} diff --git a/crates/catalog/src/tally.rs b/crates/catalog/src/tally.rs new file mode 100644 index 000000000..8f15e5ec4 --- /dev/null +++ b/crates/catalog/src/tally.rs @@ -0,0 +1,234 @@ +//! How many photos each album shows, kept up to date from what the latest ops touched. +//! +//! A smart album's count asks every photo whether its rules hold. Asked again after every edit, that is a pass over +//! the library for each smart album each time a keyword is given. [`AlbumTally`] remembers which photos each smart +//! album holds, and after a small edit asks only the photos the edit touched ([`Catalog::touched_since`]). An op +//! that changes more than photos of its own (an album's rules or photos, a stack, the keyword list) has it counted +//! whole again, as before. + +use std::collections::{HashMap, HashSet}; + +use crate::{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; +/// Bits are kept while they cost no more than this many words a photo held, and these to start with. Past that +/// (photo ids far apart: a library whose ids don't count up from 1) a set is kept instead, so what an album +/// remembers is always in proportion to the photos it holds, never to the largest id in the library. +const WORDS_A_MEMBER: usize = 4; +const WORDS_TO_START: usize = 1024; + +/// The photos a smart album holds. +#[derive(Clone, Debug)] +enum Members { + /// One bit a photo id, and how many are set. + Bits { + words: Vec, + len: usize, + }, + Set(HashSet), +} + +impl Members { + fn none() -> Members { + Members::Bits { words: Vec::new(), len: 0 } + } + + fn len(&self) -> usize { + match self { + Members::Bits { len, .. } => *len, + Members::Set(set) => set.len(), + } + } + + #[cfg(test)] + 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) + } + Members::Set(set) => set.contains(&id), + } + } + + /// Put `id` in or take it out. + fn set(&mut self, id: PhotoId, held: bool) { + if let Members::Bits { words, len } = self { + let bit = 1u64 << (id.0 % 64); + match usize::try_from(id.0 / 64) { + Ok(at) if at < words.len() => { + if let Some(word) = words.get_mut(at) { + match (*word & bit != 0, held) { + (false, true) => { + *word |= bit; + *len = len.saturating_add(1); + } + (true, false) => { + *word &= !bit; + *len = len.saturating_sub(1); + } + _ => {} + } + } + return; + } + // (past the end: not held, so there is nothing to take out) + _ if !held => return, + // room for it, as long as the bits stay in proportion to the photos held + Ok(at) if at < len.saturating_mul(WORDS_A_MEMBER).saturating_add(WORDS_TO_START) => { + words.resize(at + 1, 0); + if let Some(word) = words.get_mut(at) { + *word |= bit; + *len = len.saturating_add(1); + } + return; + } + // ids too far apart for bits: a set from here on + _ => { + let ids = words + .iter() + .enumerate() + .flat_map(|(at, word)| (0..64u64).filter(move |b| word & (1u64 << b) != 0).map(move |b| PhotoId(at as u64 * 64 + b))); + *self = Members::Set(ids.collect()); + } + } + } + if let Members::Set(set) = self { + if held { + set.insert(id); + } else { + set.remove(&id); + } + } + } +} + +/// [`Catalog::album_count`] of every album, as of one revision of the catalog. +#[derive(Clone, Debug)] +pub struct AlbumTally { + revision: u64, + counts: HashMap, + /// The smart albums' photos. + members: HashMap, +} + +impl AlbumTally { + /// Counted from every photo. + pub fn of(cat: &Catalog) -> AlbumTally { + 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)); + } + } + AlbumTally { revision: cat.revision, counts, members } + } + + /// The count of each album. + pub fn counts(&self) -> &HashMap { + &self.counts + } + + /// The catalog's revision the counts are of. + pub fn revision(&self) -> u64 { + self.revision + } + + /// Bring the counts up to `cat` as it is now. `true` when that was done from the photos the ops since touched + /// (or nothing had changed), `false` when every photo was asked again. + /// + /// `cat` has to be the catalog the counts were made of, further along. Another catalog is told apart only by + /// its revisions: a library that is loaded takes a range of its own (and then everything is counted again), but + /// two catalogs both counting from 0 look like one. + pub fn update(&mut self, cat: &Catalog) -> bool { + if self.revision == cat.revision { + return true; + } + let followed = match (cat.touched_since(self.revision), cat.aspects_since(self.revision)) { + (Some(touched), Some(changed)) if touched.len() <= MAX_FOLLOWED => { + self.follow(cat, &touched, changed); + true + } + _ => false, + }; + if !followed { + *self = AlbumTally::of(cat); + } + followed + } + + /// The counts after ops that touched only `touched`: each smart album asks those photos again; an album of + /// chosen photos counts again only when a photo came, went or was deleted (who is in it did not change: that + /// is an op on the album, and not followed). + fn follow(&mut self, cat: &Catalog, touched: &[PhotoId], changed: Aspects) { + for (album, held) in &mut self.members { + for id in touched { + held.set(*id, cat.photo(*id).is_some_and(|p| cat.album_contains(*album, p))); + } + self.counts.insert(*album, held.len()); + } + if changed.any_of(Aspects::PRESENCE) { + for album in cat.albums().filter(|a| !a.is_smart()) { + self.counts.insert(album.id, cat.album_count(album.id)); + } + } + self.revision = cat.revision; + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn ids(m: &Members, of: &[u64]) -> Vec { + of.iter().copied().filter(|id| m.holds(PhotoId(*id))).collect() + } + + #[test] + fn members_are_kept_as_bits_at_the_edges_of_a_word_and_past_the_end() { + let mut m = Members::none(); + let probe = [0, 1, 63, 64, 65, 127, 128, 5000]; + assert!(ids(&m, &probe).is_empty() && m.len() == 0); + for id in [0, 63, 64, 5000] { + m.set(PhotoId(id), true); + } + assert_eq!((ids(&m, &probe), m.len()), (vec![0, 63, 64, 5000], 4)); + // put in twice, taken out twice, taken out where nothing is, far past the end: counted once each way + m.set(PhotoId(63), true); + m.set(PhotoId(64), false); + m.set(PhotoId(64), false); + m.set(PhotoId(1), false); + m.set(PhotoId(900_000), false); + assert_eq!((ids(&m, &probe), m.len()), (vec![0, 63, 5000], 3)); + assert!(matches!(m, Members::Bits { .. })); + } + + #[test] + fn photo_ids_far_apart_are_kept_as_a_set_in_proportion_to_the_photos_held() { + // one photo with an id in the thousands of millions: no bit for every id up to it + let mut m = Members::none(); + for id in [3, 70, 9_000_000_000] { + m.set(PhotoId(id), true); + } + assert!(matches!(m, Members::Set(_)), "a set once the ids are too far apart for bits"); + assert_eq!((ids(&m, &[3, 70, 71, 9_000_000_000, u64::MAX]), m.len()), (vec![3, 70, 9_000_000_000], 3)); + m.set(PhotoId(u64::MAX), true); + m.set(PhotoId(3), false); + assert_eq!((ids(&m, &[3, 70, 9_000_000_000, u64::MAX]), m.len()), (vec![70, 9_000_000_000, u64::MAX], 3)); + // an album of many photos with ids counting up stays bits, however many + let mut dense = Members::none(); + for id in 1..=200_000u64 { + dense.set(PhotoId(id), true); + } + assert!(matches!(&dense, Members::Bits { words, .. } if words.len() <= 200_000 / 64 + 1)); + assert_eq!(dense.len(), 200_000); + } +} diff --git a/crates/catalog/src/tests.rs b/crates/catalog/src/tests.rs index acfbb1bb1..fa06e7204 100644 --- a/crates/catalog/src/tests.rs +++ b/crates/catalog/src/tests.rs @@ -123,6 +123,23 @@ fn filter_search_sort() { assert_eq!(c.keywords(), vec![("ocean".to_string(), 1), ("summer".to_string(), 1)]); } +/// A `captured` string that is not ASCII (the Lightroom importer copies `captureTime` into an +/// `AddPhoto` op verbatim, and a JSON snapshot is valid UTF-8 whatever it holds) must not be +/// sliced on a byte index in `date_groups`: grouping counts the well-formed dates and ignores +/// the rest instead of panicking on a char boundary. +#[test] +fn date_groups_ignores_a_non_ascii_captured() { + let mut c = Catalog::new(); + let ok = photo(&mut c, "ok.jpg", "2026-05-01T10:00:00"); + // `AddPhoto` takes the field as it is, like `library.importLightroom` building the op + let bad = photo(&mut c, "bad.jpg", "日本語日本語"); + assert_ne!(ok, bad); + let g = c.date_groups(); + assert_eq!(g.len(), 1, "the one well-formed date still groups"); + assert_eq!((g[0].year.as_str(), g[0].count), ("2026", 1)); + assert_eq!(g[0].days.iter().map(|d| d.1).sum::(), 1); +} + #[test] fn snapshot_and_log_replay() { let mut c = Catalog::new(); @@ -265,13 +282,30 @@ fn preview_only_survives_serde_and_set_content_undo() { let old: Catalog = serde_json::from_value(v).unwrap(); assert_eq!(old.photo(a).unwrap().preview_only, None); assert!(old.photo(a).unwrap().develops_raw()); - // Reload: the file decodes now → cleared; undo (through the journal's JSON) restores it - let op = Op::SetContent { id: a, width: 6016, height: 4000, file_size: 1, content_hash: None, preview_only: None }; + // a newer build still can't decode it: the reason and the stamp of the version that decided + // it are recorded together (#662) + let op = Op::SetContent { + id: a, + width: 6000, + height: 4000, + file_size: 1, + content_hash: None, + preview_only: Some("Nikon Huffman-compressed NEF".into()), + preview_checked: "0.6.0".into(), + }; + c.apply(op).unwrap(); + assert_eq!(c.photo(a).unwrap().preview_checked, "0.6.0"); + // Reload: the file decodes now → cleared, with its stamp; undo (through the journal's JSON) + // restores both + let op = + Op::SetContent { id: a, width: 6016, height: 4000, file_size: 1, content_hash: None, preview_only: None, preview_checked: String::new() }; let inv = c.apply(op).unwrap(); assert!(c.photo(a).unwrap().develops_raw()); + assert_eq!(c.photo(a).unwrap().preview_checked, ""); let inv: Op = serde_json::from_str(&serde_json::to_string(&inv).unwrap()).unwrap(); c.apply(inv).unwrap(); assert_eq!(c.photo(a).unwrap().preview_only.as_deref(), Some("Nikon Huffman-compressed NEF")); + assert_eq!(c.photo(a).unwrap().preview_checked, "0.6.0"); assert_eq!(c.photo(a).unwrap().width, 6000); } @@ -737,3 +771,81 @@ fn cmp_name_ignores_case_without_allocating() { assert_eq!(cmp_name("DSC_0001", "DSC_0002"), Less); assert_eq!(cmp_name("a", "ab"), Less); } + +/// What the catalog remembers of its latest ops: which photos each changed and what of them, as long as that can be +/// said photo by photo. +#[test] +fn the_catalog_remembers_which_photos_its_latest_ops_changed_and_what_of_them() { + let mut c = Catalog::default(); + let photo = |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) = (photo(&mut c, "a.jpg"), photo(&mut c, "b.jpg")); + let start = c.revision; + assert_eq!(c.touched_since(start), Some(vec![])); + assert_eq!(c.aspects_since(start), Some(Aspects::NONE)); + // a rating, then a flag on another photo, as one batch + c.apply(Op::SetRating { id: a, rating: 3 }).unwrap(); + assert_eq!(c.touched_since(start), Some(vec![a])); + assert_eq!(c.aspects_since(start), Some(Aspects::RATING)); + let after_rating = c.revision; + c.apply(Op::Batch { ops: vec![Op::SetFlag { id: b, flag: Flag::Pick }, Op::SetRating { id: a, rating: 4 }] }).unwrap(); + assert_eq!(c.touched_since(start), Some(vec![a, b]), "each once"); + assert_eq!(c.touched_since(after_rating), Some(vec![a, b])); + assert_eq!(c.aspects_since(after_rating), Some(Aspects::FLAG.with(Aspects::RATING))); + assert_eq!(c.touched_since(c.revision), Some(vec![])); + // a photo that goes counts for everything + let before = c.revision; + c.apply(Op::RemovePhoto { id: b }).unwrap(); + assert_eq!(c.touched_since(before), Some(vec![b])); + assert!(c.aspects_since(before).is_some_and(|x| x.any_of(Aspects::PRESENCE) && x.any_of(Aspects::FILE))); + // an op that changes more than photos of its own: nothing is said of what came before it + let before = c.revision; + let album = c.alloc_album_id(); + c.apply(Op::AddAlbum { album: Album::new(album, "Trip") }).unwrap(); + assert_eq!(c.touched_since(before), None); + assert_eq!(c.aspects_since(start), None); + assert_eq!(c.touched_since(c.revision), Some(vec![]), "from after it, it is known again"); + // a revision that is not this catalog's past + assert_eq!(c.touched_since(c.revision + 5), None); + // a revision set from outside (a library just loaded): nothing is known of how the catalog got there, and after + // the next op nothing of before it + let old = c.revision; + c.revision = 7 << 32; + assert_eq!(c.touched_since(old), None); + assert_eq!(c.aspects_since(c.revision), None); + let loaded = c.revision; + 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 + let before = c.revision; + assert!(c.apply(Op::SetRating { id: PhotoId(9_999), rating: 1 }).is_err()); + assert_eq!(c.revision, before); + c.apply(Op::SetFlag { id: a, flag: Flag::Pick }).unwrap(); + assert!(c.touched_since(before).is_some_and(|ids| ids.contains(&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 + let before = c.revision; + 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))); + 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); + // more ops than are remembered: the oldest are answered with "anything" + let long_ago = c.revision; + for i in 0..9000u32 { + c.apply(Op::SetRating { id: a, rating: (i % 5) as u8 }).unwrap(); + } + assert_eq!(c.touched_since(long_ago), None); + assert_eq!(c.touched_since(c.revision - 10), Some(vec![a])); +} diff --git a/crates/catalog/src/tests_format_version.rs b/crates/catalog/src/tests_format_version.rs index e2461781a..e31d7fe0c 100644 --- a/crates/catalog/src/tests_format_version.rs +++ b/crates/catalog/src/tests_format_version.rs @@ -74,6 +74,29 @@ fn v3_library_loads_and_is_upgraded() { assert_eq!(snapshot_version(&m), u64::from(VERSION)); } +/// A v6 library (before preview-only photos recorded which version decided it, #662) opens with +/// an empty stamp on its preview-only photos, so they are rechecked once, and is rewritten in the +/// current format. +#[test] +fn v6_preview_only_photo_loads_unstamped_and_is_upgraded() { + let mut c = Catalog::new(); + let id = c.alloc_photo_id(); + let mut p = Photo::new(id, Source::File { path: "/p/a.cr3".into() }, "a.cr3", "CR3", 160, 120, "2026-01-01"); + p.kind = MediaKind::Raw; + p.preview_only = Some("Cr3 files are not decoded yet".into()); + c.apply(Op::AddPhoto { photo: Box::new(p) }).unwrap(); + let snap = c.to_snapshot(); + assert!(!snap.contains("preview_checked"), "an empty stamp isn't written"); + let m = MemStore::new(); + m.set(SNAPSHOT, format!("{{\"format\":\"lightcraft-catalog\",\"version\":6,\"seq\":1,\"catalog\":{snap}}}\n").into_bytes()); + let (_, loaded, r) = Journal::open(Box::new(m.clone())).unwrap(); + let p = loaded.photo(id).unwrap(); + assert_eq!(p.preview_only.as_deref(), Some("Cr3 files are not decoded yet")); + assert_eq!(p.preview_checked, ""); + assert_eq!(r.upgraded_from, Some(6)); + assert_eq!(snapshot_version(&m), u64::from(VERSION)); +} + #[test] fn versionless_log_only_library_is_upgraded() { let mut c = Catalog::new(); @@ -171,7 +194,8 @@ fn bad_crc_is_still_a_torn_tail() { /// Guard for the version rule (see `journal` → *Format versions*): adding an [`Op`] variant breaks /// this match. When it does, bump `journal::VERSION`, add a row to the format table, and add the -/// variant here under the new version. +/// variant here under the new version. A version that only adds serialized fields raises +/// `NEWEST_FIELDS` instead. #[test] fn op_variants_are_versioned() { fn since(op: &Op) -> u32 { @@ -214,6 +238,8 @@ fn op_variants_are_versioned() { Op::SetHistoryCursor { .. } => 8, } } + /// v9: `Photo.preview_checked` and `Op::SetContent.preview_checked`. + const NEWEST_FIELDS: u32 = 9; let newest = since(&Op::SetHistoryCursor { id: PhotoId(1), cursor: None }); - assert_eq!(newest, VERSION, "the newest op's version must be the current format version"); + assert_eq!(newest.max(NEWEST_FIELDS), VERSION, "the newest op's or fields' version must be the current format version"); } diff --git a/crates/codecs/src/jpeg.rs b/crates/codecs/src/jpeg.rs index e40cf7e73..b8de12bdd 100644 --- a/crates/codecs/src/jpeg.rs +++ b/crates/codecs/src/jpeg.rs @@ -75,14 +75,12 @@ pub(crate) fn parse_markers(b: &[u8]) -> Option { let end = (p + 2 + len).min(b.len()); let seg = &b[start..end]; match marker { - 0xC0..=0xCF if marker != 0xC4 && marker != 0xC8 && marker != 0xCC => { - if seg.len() >= 6 && m.components == 0 { - m.precision = seg[0]; - m.sof = marker; - m.height = u16::from_be_bytes([seg[1], seg[2]]) as u32; - m.width = u16::from_be_bytes([seg[3], seg[4]]) as u32; - m.components = seg[5]; - } + 0xC0..=0xCF if marker != 0xC4 && marker != 0xC8 && marker != 0xCC && seg.len() >= 6 && m.components == 0 => { + m.precision = seg[0]; + m.sof = marker; + m.height = u16::from_be_bytes([seg[1], seg[2]]) as u32; + m.width = u16::from_be_bytes([seg[3], seg[4]]) as u32; + m.components = seg[5]; } 0xE1 => { if seg.starts_with(b"Exif\0\0") && m.exif.is_none() { @@ -182,9 +180,11 @@ pub(crate) fn decode_with_fallback(bytes: &[u8], opts: &DecodeOptions, fallback: check_size(F, m.width as u64, m.height as u64, opts)?; 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 and non-interleaved sequential scans (which zune-jpeg 0.5 mis-decodes with subsampling). + // 12-bit, non-interleaved sequential scans (which zune-jpeg 0.5 mis-decodes with subsampling) + // and every lossless (SOF3) file, which zune-jpeg refuses outright. + 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 { + let raw = if m.components == 4 || scale_to.is_some() || m.precision > 8 || non_interleaved || lossless { decode_jpeg_decoder(bytes, &m, scale_to)? } else { decode_zune(bytes, &m)? @@ -221,7 +221,9 @@ fn decode_jpeg_decoder(bytes: &[u8], m: &Markers, scale_to: Option<(u32, u32)>) use jpeg_decoder::PixelFormat; let mut d = jpeg_decoder::Decoder::new(bytes); d.read_info().map_err(|e| Error::Malformed(F, e.to_string()))?; - if let Some((mw, mh)) = scale_to { + // `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) { // Ask for the smallest DCT scale still covering the target box. let (sw, sh) = scaled_request(m.width, m.height, mw, mh); d.scale(sw, sh).map_err(|e| Error::Malformed(F, e.to_string()))?; @@ -231,16 +233,25 @@ fn decode_jpeg_decoder(bytes: &[u8], m: &Markers, scale_to: Option<(u32, u32)>) let (w, h) = (info.width as usize, info.height as usize); let (model, buf, depth) = match info.pixel_format { PixelFormat::L8 => (Model::Gray, Buf::U8(px), 8), - PixelFormat::L16 => { - let v = px.as_chunks::<2>().0.iter().map(|c| u16::from_be_bytes([c[0], c[1]])).collect(); - (Model::Gray, Buf::U16(v), 16) - } + PixelFormat::L16 => (Model::Gray, Buf::U16(lossless_samples(&px, m.precision)), 16), + // A lossless frame over 8 bits per sample still reports `RGB24` (or `CMYK32`), but its + // buffer holds two native-endian bytes per sample like L16. + PixelFormat::RGB24 if m.precision > 8 => (Model::Rgb, Buf::U16(lossless_samples(&px, m.precision)), 16), PixelFormat::RGB24 => (Model::Rgb, Buf::U8(px), 8), + PixelFormat::CMYK32 if m.precision > 8 => (Model::Cmyk, Buf::U16(lossless_samples(&px, m.precision)), 16), PixelFormat::CMYK32 => (Model::Cmyk, Buf::U8(px), 8), }; Ok(Raw { width: w, height: h, model, alpha: false, premultiplied: false, buf, bit_depth: depth }) } +/// jpeg-decoder's lossless samples: one native-endian `u16` per sample holding the raw +/// `precision`-bit value. Left-justify it into the 16-bit range the rest of the pipeline reads, +/// as every 16-bit decoder feeding [`Buf::U16`] does. +fn lossless_samples(px: &[u8], precision: u8) -> Vec { + let shift = 16u8.saturating_sub(precision); + px.as_chunks::<2>().0.iter().map(|c| u16::from_ne_bytes([c[0], c[1]]) << shift).collect() +} + /// The first `0xFF` byte, eight bytes at a time (scan data has one every few hundred bytes: this /// is most of [`header`]'s time). fn find_ff(b: &[u8]) -> Option { @@ -450,4 +461,94 @@ mod tests { let m = parse_markers(&[0xFF, 0xD8, 0xFF, 0xE1, 0, 200, 1, 2]).unwrap(); assert!(m.exif.is_none()); } + + // --- lossless (SOF3) synthetic files: one DHT table with a single 1-bit category-0 code, so + // every pixel is a zero difference and decodes to the predictor's initial sample. No such + // encoder exists here; the bytes are the smallest a decoder accepts. + + fn lossless_header(out: &mut Vec, components: u8, w: u16, precision: u8) { + // DHT: one DC table (id 0) with a single 1-bit code for the category-0 symbol. + out.extend_from_slice(&[0xFF, 0xC4]); + out.extend_from_slice(&(2u16 + 1 + 16 + 1).to_be_bytes()); + out.push(0x00); + let mut counts = [0u8; 16]; + counts[0] = 1; + out.extend_from_slice(&counts); + out.push(0x00); + // SOF3 + out.extend_from_slice(&[0xFF, 0xC3]); + out.extend_from_slice(&(8u16 + 3 * components as u16).to_be_bytes()); + out.push(precision); + out.extend_from_slice(&1u16.to_be_bytes()); + out.extend_from_slice(&w.to_be_bytes()); + out.push(components); + for c in 1..=components { + out.extend_from_slice(&[c, 0x11, 0]); + } + // SOS: predictor Ra, Se = 0, Ah/Al = 0 + out.extend_from_slice(&[0xFF, 0xDA]); + out.extend_from_slice(&(6u16 + 2 * components as u16).to_be_bytes()); + out.push(components); + for c in 1..=components { + out.extend_from_slice(&[c, 0]); + } + out.extend_from_slice(&[1, 0, 0]); + } + + /// A minimal lossless JPEG, `w` x 1, `components` samples per pixel, every sample the initial + /// prediction `1 << (precision - 1)`. + fn lossless_jpeg(components: u8, w: u16, precision: u8) -> Vec { + let mut out = vec![0xFF, 0xD8]; + lossless_header(&mut out, components, w, precision); + // entropy: one zero bit per sample (a category-0 code), padded to whole bytes + out.extend_from_slice(&vec![0u8; (components as usize * w as usize).div_ceil(8)]); + out.extend_from_slice(&[0xFF, 0xD9]); + out + } + + /// jpeg-decoder emits lossless samples native-endian (its `to_ne_bytes`) holding the raw + /// `precision`-bit value, so the old `from_be_bytes` swapped every sample (2048 -> 8) and the + /// raw value was never left-justified into the 16-bit buffer. + #[test] + 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 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)); + } + + /// A lossless frame over 8 bits per sample reports `RGB24`, but its buffer is two bytes per + /// sample: it must become `Buf::U16`, not `Buf::U8` over double-length rows. + #[test] + 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 Buf::U16(v) = &raw.buf else { panic!("expected 16-bit samples") }; + assert_eq!(v, &vec![2048u16 << 4; 6]); + assert_eq!(raw.model, Model::Rgb); + } + + /// The same 16-bit wrapping for four components: `CMYK32` over 8 bits is two bytes a sample. + #[test] + 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 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)); + } + + /// zune-jpeg refuses SOF3 ("Lossless Huffman"), so an 8-bit lossless file must route to + /// jpeg-decoder; `header` accepts it, so `decode` used to fail a file we listed as readable. + #[test] + fn eight_bit_lossless_jpeg_decodes() { + let bytes = lossless_jpeg(1, 8, 8); + let d = crate::decode(&bytes, DecodeOptions::default()).unwrap(); + assert_eq!((d.width, d.height), (8, 1)); + assert_eq!(d.bit_depth, 8); + } } diff --git a/crates/codecs/src/jpeg_par.rs b/crates/codecs/src/jpeg_par.rs index 6ec9b6a80..c8d9e5023 100644 --- a/crates/codecs/src/jpeg_par.rs +++ b/crates/codecs/src/jpeg_par.rs @@ -3,8 +3,11 @@ //! The image is cut into bands of MCU rows separated by restart markers (`DRI` + `RSTn`): DC //! prediction restarts at every marker, so bands are entropy-coded independently on all cores and //! concatenated. Quantization uses the example tables of T.81 Annex K scaled by the usual 1–100 -//! quality convention; Huffman coding uses the Annex K example tables (interleaved baseline, readable -//! by every decoder). Supports grayscale and YCbCr 4:4:4 / 4:2:0. +//! quality convention. Huffman tables are made for each image from its own symbol counts (T.81 +//! Annex K.2): a first pass over the bands counts the symbols, a second codes them. The file stays +//! interleaved baseline with the tables in its `DHT` segments, readable by every decoder, and is +//! smaller than with the Annex K example tables at the same pixels. Supports grayscale and YCbCr +//! 4:4:4 / 4:2:0. use crate::encode::ChromaSubsampling; @@ -61,16 +64,103 @@ impl Huff { let (mut c, mut k) = (0u16, 0usize); for (len, &n) in bits.iter().enumerate() { for _ in 0..n { - code[vals[k] as usize] = (c, len as u8 + 1); - c += 1; + if let Some(&v) = vals.get(k) { + code[v as usize] = (c, len as u8 + 1); + } + c = c.wrapping_add(1); k += 1; } - c <<= 1; + c = c.wrapping_shl(1); } Huff { code } } } +/// Symbol counts of one Huffman table: index = symbol (DC size category, or AC run/size byte). +type Freq = [u64; 256]; + +/// The code lengths (`BITS`, counts per length 1..16) and symbols (`HUFFVAL`) of an optimal +/// Huffman table for `freq`, limited to 16-bit codes and never assigning the all-ones code +/// (T.81 Annex K.2, figures K.1–K.4). Symbols with a zero count get no code. At least one symbol +/// is always present (a table nothing used gets symbol 0), so the result is a valid `DHT`. +fn optimal_table(freq: &Freq) -> ([u8; 16], Vec) { + // 256 is the reserved pseudo-symbol: counted once, it takes the longest code, which is + // then dropped so no real code is all ones (K.2) + let mut f = [0u64; 257]; + f[..256].copy_from_slice(freq); + if f[..256].iter().all(|&c| c == 0) { + f[0] = 1; + } + f[256] = 1; + let mut size = [0usize; 257]; + let mut next: [Option; 257] = [None; 257]; + loop { + // the two smallest non-zero counts; on equal counts the larger symbol goes first (K.1) + let mut c1: Option = None; + for i in 0..257 { + if f[i] > 0 && c1.is_none_or(|c| f[i] <= f[c]) { + c1 = Some(i); + } + } + let Some(c1) = c1 else { break }; + let mut c2: Option = None; + for i in 0..257 { + if i != c1 && f[i] > 0 && c2.is_none_or(|c| f[i] <= f[c]) { + c2 = Some(i); + } + } + let Some(c2) = c2 else { break }; + f[c1] = f[c1].saturating_add(f[c2]); + f[c2] = 0; + // every symbol in both trees goes one level deeper; c2's chain is appended to c1's + let mut k = c1; + size[k] += 1; + while let Some(n) = next[k] { + k = n; + size[k] += 1; + } + next[k] = Some(c2); + let mut k = c2; + size[k] += 1; + while let Some(n) = next[k] { + k = n; + size[k] += 1; + } + } + // counts per code length; 257 symbols need at most 256 levels + let mut bits = [0usize; 257]; + for &s in &size { + if s > 0 { + bits[s.min(256)] += 1; + } + } + // limit to 16 bits (K.3): move pairs of the longest codes up, one level at a time (the + // deepest level of a full tree always holds an even number of codes) + for i in (17..257).rev() { + while bits[i] >= 2 { + let Some(j) = (1..i - 1).rev().find(|&j| bits[j] > 0) else { break }; + bits[i] -= 2; + bits[i - 1] += 1; + bits[j + 1] += 2; + bits[j] -= 1; + } + } + // drop the reserved symbol's code: it is one of the longest + if let Some(i) = (1..=16).rev().find(|&i| bits[i] > 0) { + bits[i] -= 1; + } + // symbols by code length, then by value (K.4); the reserved symbol is not listed + let mut vals = Vec::new(); + for len in 1..257 { + vals.extend((0..256).filter(|&s| size[s] == len).map(|s| s as u8)); + } + let mut out = [0u8; 16]; + for (o, &b) in out.iter_mut().zip(&bits[1..=16]) { + *o = b.min(255) as u8; + } + (out, vals) +} + fn scaled_quant(base: &[u16; 64], quality: u8) -> [u16; 64] { let q = quality.clamp(1, 100) as u32; let scale = if q < 50 { 5000 / q } else { 200 - 2 * q }; @@ -177,13 +267,15 @@ fn magnitude(v: i32) -> (u32, u32) { (size, bits & ((1u32 << size) - 1)) } -fn encode_block(w: &mut Bits, q: &[i16; 64], pred: &mut i32, dc: &Huff, ac: &Huff) { +/// The Huffman symbols of one quantized block in coding order: `sym(ac, symbol, bits, n)` for the +/// DC size category (`ac` false) and each AC run/size, ZRL and EOB (`ac` true), each followed by +/// its `n` extra bits `bits`. +#[inline(always)] +fn block_symbols(q: &[i16; 64], pred: &mut i32, mut sym: impl FnMut(bool, usize, u32, u32)) { let d = q[0] as i32 - *pred; *pred = q[0] as i32; let (s, b) = magnitude(d); - let (c, l) = dc.code[s as usize]; - w.put(c as u32, l as u32); - w.put(b, s); + sym(false, s as usize, b, s); let mut run = 0u32; for &zi in &ZIGZAG[1..] { let v = q[zi] as i32; @@ -192,22 +284,45 @@ fn encode_block(w: &mut Bits, q: &[i16; 64], pred: &mut i32, dc: &Huff, ac: &Huf continue; } while run >= 16 { - let (c, l) = ac.code[0xF0]; - w.put(c as u32, l as u32); + sym(true, 0xF0, 0, 0); run -= 16; } let (s, b) = magnitude(v); - let (c, l) = ac.code[((run << 4) | s) as usize]; - w.put(c as u32, l as u32); - w.put(b, s); + sym(true, ((run << 4) | s) as usize, b, s); run = 0; } if run > 0 { - let (c, l) = ac.code[0x00]; - w.put(c as u32, l as u32); + sym(true, 0x00, 0, 0); } } +/// One coded symbol of the entropy-coded data: bits 0–7 the symbol, 8–9 its table (0 DC luma, +/// 1 AC luma, 2 DC chroma, 3 AC chroma), 10–13 the number of extra bits, 14–29 those bits (a +/// baseline coefficient needs at most 11). +type Token = u32; + +/// Append one block's symbols to `out` and count them in `freq` (by table, as in [`Token`]). +fn tokenize_block(q: &[i16; 64], pred: &mut i32, chroma: bool, out: &mut Vec, freq: &mut [Freq; 4]) { + let base = if chroma { 2 } else { 0 }; + block_symbols(q, pred, |is_ac, s, bits, n| { + let table = base + is_ac as usize; + freq[table][s & 0xFF] += 1; + out.push((s as u32 & 0xFF) | (table as u32) << 8 | (n & 0xF) << 10 | (bits & 0xFFFF) << 14); + }); +} + +/// Code a band's symbols with `tables` (indexed as in [`Token`]). +fn write_tokens(tokens: &[Token], tables: &[Huff; 4]) -> Vec { + let mut w = Bits::new(); + for &t in tokens { + let (s, table, n, bits) = (t & 0xFF, (t >> 8) & 3, (t >> 10) & 0xF, t >> 14); + let (c, l) = tables[table as usize].code[s as usize]; + w.put(c as u32, l as u32); + w.put(bits, n); + } + w.finish() +} + /// Planar component samples (level-shifted to −128..127) with edge replication on read. struct Plane { w: usize, @@ -249,7 +364,8 @@ fn dht(out: &mut Vec, class_id: u8, bits: &[u8; 16], vals: &[u8]) { segment(out, 0xC4, &p); } -/// Encode interleaved 8-bit samples (`channels` 1, 3 or 4; alpha is dropped). +/// Encode interleaved 8-bit samples (`channels` 1, 3 or 4; alpha is dropped), with Huffman +/// tables made for the image. pub fn encode( data: &[u8], width: usize, @@ -258,6 +374,21 @@ pub fn encode( quality: u8, sub: ChromaSubsampling, app_segments: &[(u8, Vec)], +) -> Vec { + encode_with(data, width, height, channels, quality, sub, app_segments, true) +} + +/// [`encode`], with optimized Huffman tables or (`optimize` false) the Annex K example tables. +#[allow(clippy::too_many_arguments)] +fn encode_with( + data: &[u8], + width: usize, + height: usize, + channels: usize, + quality: u8, + sub: ChromaSubsampling, + app_segments: &[(u8, Vec)], + optimize: bool, ) -> Vec { let gray = channels == 1; let (hs, vs) = match (gray, sub) { @@ -331,42 +462,67 @@ pub fn encode( let ql = scaled_quant(&Q_LUMA, quality); let qc = scaled_quant(&Q_CHROMA, quality); let (rl, rc) = (recip_table(&ql), recip_table(&qc)); - let (dcl, acl) = (Huff::new(&DC_LUMA_BITS, &DC_VALS), Huff::new(&AC_LUMA_BITS, &AC_LUMA_VALS)); - let (dcc, acc) = (Huff::new(&DC_CHROMA_BITS, &DC_VALS), Huff::new(&AC_CHROMA_BITS, &AC_CHROMA_VALS)); - let (mcu_w, mcu_h) = (8 * hs, 8 * vs); let (mx, my) = (width.div_ceil(mcu_w), height.div_ceil(mcu_h)); // restart interval: whole MCU rows per band, ≤ 65535 MCUs, enough bands to keep all cores busy let rows_per_band = (my / 64).clamp(1, (65_535 / mx.max(1)).max(1)); let bands: Vec = (0..my.div_ceil(rows_per_band)).collect(); - let encode_band = |band: &usize| -> Vec { - let mut w = Bits::new(); - let mut pred = [0i32; 3]; + // first pass: each band's quantized blocks as symbols, with their counts per table (DC + // prediction restarts per band, as the restart markers say) + let tokenize_band = |band: &usize| -> (Vec, [Freq; 4]) { let (mut blk, mut q) = ([0f32; 64], [0i16; 64]); + let (mut tokens, mut freq) = (Vec::new(), [[0u64; 256]; 4]); + let mut pred = [0i32; 3]; for mcu_y in band * rows_per_band..((band + 1) * rows_per_band).min(my) { for mcu_x in 0..mx { for vy in 0..vs { for hx in 0..hs { planes[0].block(mcu_x * hs + hx, mcu_y * vs + vy, &mut blk); fdct_quant(&blk, &rl, &mut q); - encode_block(&mut w, &q, &mut pred[0], &dcl, &acl); + tokenize_block(&q, &mut pred[0], false, &mut tokens, &mut freq); } } if !gray { - for k in 1..3 { - planes[k].block(mcu_x, mcu_y, &mut blk); + for (plane, pred) in planes.iter().zip(pred.iter_mut()).take(3).skip(1) { + plane.block(mcu_x, mcu_y, &mut blk); fdct_quant(&blk, &rc, &mut q); - encode_block(&mut w, &q, &mut pred[k], &dcc, &acc); + tokenize_block(&q, pred, true, &mut tokens, &mut freq); } } } } - w.finish() + (tokens, freq) }; #[cfg(feature = "parallel")] - let coded: Vec> = bands.par_iter().map(encode_band).collect(); + let tokenized: Vec<(Vec, [Freq; 4])> = bands.par_iter().map(tokenize_band).collect(); #[cfg(not(feature = "parallel"))] - let coded: Vec> = bands.iter().map(encode_band).collect(); + let tokenized: Vec<(Vec, [Freq; 4])> = bands.iter().map(tokenize_band).collect(); + drop(planes); + // the tables: DC luma, AC luma, DC chroma, AC chroma + let tables: [([u8; 16], Vec); 4] = if optimize { + let mut total = [[0u64; 256]; 4]; + for (_, f) in &tokenized { + for (t, f) in total.iter_mut().zip(f) { + for (t, f) in t.iter_mut().zip(f) { + *t = t.saturating_add(*f); + } + } + } + total.each_ref().map(optimal_table) + } else { + [ + (DC_LUMA_BITS, DC_VALS.to_vec()), + (AC_LUMA_BITS, AC_LUMA_VALS.to_vec()), + (DC_CHROMA_BITS, DC_VALS.to_vec()), + (AC_CHROMA_BITS, AC_CHROMA_VALS.to_vec()), + ] + }; + let huff = tables.each_ref().map(|(bits, vals)| Huff::new(bits, vals)); + // second pass: code the symbols + #[cfg(feature = "parallel")] + let coded: Vec> = tokenized.into_par_iter().map(|(t, _)| write_tokens(&t, &huff)).collect(); + #[cfg(not(feature = "parallel"))] + let coded: Vec> = tokenized.into_iter().map(|(t, _)| write_tokens(&t, &huff)).collect(); // headers let mut out = Vec::with_capacity(coded.iter().map(Vec::len).sum::() + 4096); @@ -396,11 +552,12 @@ pub fn encode( sof.extend_from_slice(&[3, 1, ((hs as u8) << 4) | vs as u8, 0, 2, 0x11, 1, 3, 0x11, 1]); } segment(&mut out, 0xC0, &sof); - dht(&mut out, 0x00, &DC_LUMA_BITS, &DC_VALS); - dht(&mut out, 0x10, &AC_LUMA_BITS, &AC_LUMA_VALS); + let [(dlb, dlv), (alb, alv), (dcb, dcv), (acb, acv)] = &tables; + dht(&mut out, 0x00, dlb, dlv); + dht(&mut out, 0x10, alb, alv); if !gray { - dht(&mut out, 0x01, &DC_CHROMA_BITS, &DC_VALS); - dht(&mut out, 0x11, &AC_CHROMA_BITS, &AC_CHROMA_VALS); + dht(&mut out, 0x01, dcb, dcv); + dht(&mut out, 0x11, acb, acv); } let interval = (rows_per_band * mx) as u16; if coded.len() > 1 { @@ -495,6 +652,100 @@ mod tests { } } + /// The code lengths of `(bits, vals)`, checked: every listed symbol once, at most 16 bits, + /// and room left in the code space (the all-ones code is never assigned, Kraft sum < 1). + fn check_table(bits: &[u8; 16], vals: &[u8]) { + assert_eq!(bits.iter().map(|&b| b as usize).sum::(), vals.len()); + let mut seen = [false; 256]; + for &v in vals { + assert!(!seen[v as usize], "symbol {v} listed twice"); + seen[v as usize] = true; + } + let kraft: f64 = bits.iter().enumerate().map(|(i, &n)| n as f64 / 2f64.powi(i as i32 + 1)).sum(); + assert!(kraft < 1.0, "code space full: {kraft}"); + let h = Huff::new(bits, vals); + for &v in vals { + let (c, l) = h.code[v as usize]; + assert!((1..=16).contains(&l)); + assert_ne!(c as u32, (1u32 << l) - 1, "symbol {v} has the all-ones code"); + } + } + + #[test] + fn optimal_tables_are_valid_and_bounded() { + // one symbol, two, a typical spread and every symbol + let mut one = [0u64; 256]; + one[7] = 5; + let mut two = [0u64; 256]; + two[0] = 1; + two[0xF0] = 1_000_000; + let typical: Freq = std::array::from_fn(|s| if s % 3 == 0 { (s as u64 * 7919) % 1000 + 1 } else { 0 }); + let all: Freq = std::array::from_fn(|s| s as u64 + 1); + // Fibonacci counts make an unbounded Huffman tree ~40 levels deep: the 16-bit limit applies + let mut fib = [0u64; 256]; + let (mut a, mut b) = (1u64, 1u64); + for f in fib.iter_mut().take(40) { + *f = a; + (a, b) = (b, a + b); + } + for (name, f) in [("one", one), ("two", two), ("typical", typical), ("all", all), ("fib", fib), ("none", [0; 256])] { + let (bits, vals) = optimal_table(&f); + check_table(&bits, &vals); + for s in 0..256 { + assert_eq!(vals.contains(&(s as u8)), f[s] > 0 || (name == "none" && s == 0), "{name}: symbol {s}"); + } + } + // more frequent symbols never get longer codes + let (bits, vals) = optimal_table(&typical); + let h = Huff::new(&bits, &vals); + for &x in &vals { + for &y in &vals { + if typical[x as usize] > typical[y as usize] { + assert!(h.code[x as usize].1 <= h.code[y as usize].1, "{x} vs {y}"); + } + } + } + // the Annex K example tables pass the same checks + check_table(&AC_LUMA_BITS, &AC_LUMA_VALS); + check_table(&DC_CHROMA_BITS, &DC_VALS); + } + + /// Issue #1373: tables made for the image give a smaller file with exactly the same pixels as + /// the Annex K example tables (the quantized coefficients are the same), at every quality and + /// subsampling, and both decoders read it. + #[test] + fn optimized_tables_shrink_files_without_changing_pixels() { + let (w, h) = (517, 333); + // gradients plus deterministic noise, like a photo's grain + let mut seed = 0x2545_f491_u32; + let src: Vec = test_rgb(w, h) + .into_iter() + .map(|v| { + seed ^= seed << 13; + seed ^= seed >> 17; + seed ^= seed << 5; + v.saturating_add((seed % 23) as u8) + }) + .collect(); + for (q, sub) in [(100, ChromaSubsampling::S444), (92, ChromaSubsampling::S444), (75, ChromaSubsampling::S420), (30, ChromaSubsampling::S420)] + { + let std = encode_with(&src, w, h, 3, q, sub, &[], false); + let opt = encode(&src, w, h, 3, q, sub, &[]); + assert!(opt.len() < std.len(), "q{q} {sub:?}: {} ≥ {}", opt.len(), std.len()); + let a = jpeg_decoder::Decoder::new(&std[..]).decode().expect("standard tables"); + let b = jpeg_decoder::Decoder::new(&opt[..]).decode().expect("optimized tables"); + assert_eq!(a, b, "q{q} {sub:?}: pixels differ"); + let z = zune_jpeg::JpegDecoder::new(zune_core::bytestream::ZCursor::new(&opt[..])).decode().expect("zune-jpeg"); + assert_eq!(z.len(), b.len()); + } + // grayscale too + let gray: Vec = src.iter().step_by(3).copied().collect(); + let std = encode_with(&gray, w, h, 1, 90, ChromaSubsampling::S444, &[], false); + let opt = encode(&gray, w, h, 1, 90, ChromaSubsampling::S444, &[]); + assert!(opt.len() < std.len()); + assert_eq!(jpeg_decoder::Decoder::new(&std[..]).decode().unwrap(), jpeg_decoder::Decoder::new(&opt[..]).decode().unwrap()); + } + #[test] fn grayscale_and_quality_monotone() { let (w, h) = (300, 200); diff --git a/crates/codecs/src/lib.rs b/crates/codecs/src/lib.rs index e524cffe8..33ab32efb 100644 --- a/crates/codecs/src/lib.rs +++ b/crates/codecs/src/lib.rs @@ -12,6 +12,7 @@ //! - [`encode`] writes JPEG, PNG, TIFF, lossless WebP and, on native targets, AVIF (feature `avif`) and //! JPEG XL ([`encode_jxl`], feature `jxl`), embedding //! ICC/EXIF/XMP. [`icc::write_matrix_trc`] builds profiles for export. +//! - [`video`] encodes AV1 video (native, feature `avif`) and writes it as MP4 or WebM. //! //! No decoder panics on malformed input (property-tested). #![forbid(unsafe_code)] @@ -36,6 +37,7 @@ mod psd; mod sniff; pub mod space; mod tiff_codec; +pub mod video; mod webp; pub use encode::{ diff --git a/crates/codecs/src/video.rs b/crates/codecs/src/video.rs new file mode 100644 index 000000000..3991269d8 --- /dev/null +++ b/crates/codecs/src/video.rs @@ -0,0 +1,1158 @@ +//! Video: AV1 encoding of RGBA frames (`rav1e`, native targets with the `avif` feature) and two +//! small muxers of our own, written from the public specifications only: +//! +//! - **MP4** (ISO/IEC 14496-12 ISO base media file format + the AOM "AV1 Codec ISO Media File +//! Format Binding"): `ftyp`, one `mdat` (64-bit size, patched at the end) holding the samples, +//! then `moov` with one video track whose sample entry is `av01` + `av1C` + `colr` (`nclx`). +//! - **WebM** (Matroska / EBML as restricted by WebM, codec mapping `V_AV1`): EBML header, +//! Segment (size patched at the end) with SeekHead, Info, Tracks, Clusters of SimpleBlocks (the +//! last frame a BlockGroup with its duration) and Cues. +//! +//! Frames may last several frame periods: a picture held on screen is one sample with a long +//! duration (variable frame rate), so a still slide costs one encoded frame however long it +//! stays up. Durations are counted in frame periods of the nominal rate. +//! +//! Samples are AV1 temporal units without temporal delimiter OBUs (both bindings ask for that); +//! the configuration record carries the sequence header OBU. No audio track. + +use std::io::{Seek, SeekFrom, Write}; + +use crate::{Error, Result}; + +/// The container a video is written in. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Container { + Mp4, + WebM, +} + +impl Container { + pub fn id(self) -> &'static str { + match self { + Container::Mp4 => "mp4", + Container::WebM => "webm", + } + } + + pub fn parse(s: &str) -> Option { + match s.trim().to_ascii_lowercase().as_str() { + "mp4" => Some(Container::Mp4), + "webm" => Some(Container::WebM), + _ => None, + } + } +} + +/// One encoded picture: an AV1 temporal unit (no temporal delimiter), whether it is a key frame +/// (a sync sample), and how many frame periods it stays on screen (≥ 1). +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct Sample { + pub data: Vec, + pub key: bool, + pub duration: u64, +} + +/// The longest video the muxers write, in frame periods (keeps every timestamp in range). +pub const MAX_PERIODS: u64 = 60 * 60 * 24 * 120; + +fn io(e: std::io::Error) -> Error { + Error::Encode(format!("video: {e}")) +} + +fn bad(msg: impl Into) -> Error { + Error::Encode(format!("video: {}", msg.into())) +} + +// ---------------------------------------------------------------- AV1 OBUs + +/// OBU types (AV1 specification §6.2.2). +pub const OBU_SEQUENCE_HEADER: u8 = 1; +pub const OBU_TEMPORAL_DELIMITER: u8 = 2; +pub const OBU_PADDING: u8 = 15; + +/// Read a LEB128 value at `pos` (at most 8 bytes); returns (value, bytes read). +fn leb128(data: &[u8], pos: usize) -> Result<(u64, usize)> { + let mut v = 0u64; + for i in 0..8 { + let b = *data.get(pos + i).ok_or_else(|| bad("truncated OBU size"))?; + v |= u64::from(b & 0x7f) << (7 * i); + if b & 0x80 == 0 { + return Ok((v, i + 1)); + } + } + Err(bad("OBU size longer than 8 bytes")) +} + +/// The OBUs of a temporal unit: (type, the whole OBU's byte range). Every OBU but the last must +/// carry its size (as the low-overhead bitstream format requires). +pub fn obus(data: &[u8]) -> Result)>> { + let mut out = Vec::new(); + let mut pos = 0usize; + while let Some(&header) = data.get(pos) { + if header & 0x80 != 0 { + return Err(bad("OBU forbidden bit set")); + } + let kind = (header >> 3) & 0x0f; + let ext = header & 0x04 != 0; + let has_size = header & 0x02 != 0; + let mut p = pos + 1 + usize::from(ext); + let end = if has_size { + let (size, n) = leb128(data, p)?; + p += n; + let size = usize::try_from(size).map_err(|_| bad("OBU too large"))?; + p.checked_add(size).filter(|e| *e <= data.len()).ok_or_else(|| bad("OBU runs past the end of the packet"))? + } else { + data.len() + }; + if p > end { + return Err(bad("truncated OBU header")); + } + out.push((kind, pos..end)); + pos = end; + } + Ok(out) +} + +/// A temporal unit as a sample: temporal delimiters and padding dropped. Also returns its +/// sequence header OBU, if it has one. +pub fn to_sample_data(tu: &[u8]) -> Result<(Vec, Option>)> { + let mut data = Vec::with_capacity(tu.len()); + let mut seq = None; + for (kind, r) in obus(tu)? { + let Some(bytes) = tu.get(r) else { continue }; + match kind { + OBU_TEMPORAL_DELIMITER | OBU_PADDING => {} + _ => { + if kind == OBU_SEQUENCE_HEADER && seq.is_none() { + seq = Some(bytes.to_vec()); + } + data.extend_from_slice(bytes); + } + } + } + Ok((data, seq)) +} + +/// The `av1C` configuration record (AV1-ISOBMFF §2.3) for 8-bit 4:2:0 main profile, followed by +/// `seq_header` (the sequence header OBU, may be empty). Matroska's `CodecPrivate` is the same. +pub fn av1_config(seq_header: &[u8]) -> Vec { + // marker 1, version 1 | profile 0, level 31 (unconstrained) | tier 0, 8-bit, not mono, + // subsampling x and y, chroma position 0 | no initial presentation delay + let mut v = vec![0x81, 31, 0b0000_1100, 0]; + v.extend_from_slice(seq_header); + v +} + +// ---------------------------------------------------------------- MP4 + +fn mp4_box(kind: &[u8; 4], body: &[u8]) -> Result> { + let size = u32::try_from(body.len().checked_add(8).ok_or_else(|| bad("box too large"))?).map_err(|_| bad("box too large"))?; + let mut v = Vec::with_capacity(body.len() + 8); + v.extend_from_slice(&size.to_be_bytes()); + v.extend_from_slice(kind); + v.extend_from_slice(body); + Ok(v) +} + +fn full_box(kind: &[u8; 4], version: u8, flags: u32, body: &[u8]) -> Result> { + let mut b = Vec::with_capacity(body.len() + 4); + b.push(version); + b.extend_from_slice(&flags.to_be_bytes()[1..]); + b.extend_from_slice(body); + mp4_box(kind, &b) +} + +/// The unity transformation matrix of `mvhd` / `tkhd`. +fn matrix() -> Vec { + let mut m = Vec::with_capacity(36); + for v in [0x0001_0000u32, 0, 0, 0, 0x0001_0000, 0, 0, 0, 0x4000_0000] { + m.extend_from_slice(&v.to_be_bytes()); + } + m +} + +/// An MP4 writer: [`Mp4Writer::add`] streams each sample into `mdat`; [`Mp4Writer::finish`] +/// patches its size and appends `moov`. +pub struct Mp4Writer { + w: W, + width: u16, + height: u16, + fps: u32, + /// Where `mdat`'s 64-bit size is, and where its data starts. + mdat_size_at: u64, + data_start: u64, + written: u64, + sizes: Vec, + durations: Vec, + keys: Vec, + config: Option>, +} + +/// Track ticks per frame period (ffmpeg's habit: 512 per frame). +const TICKS: u64 = 512; + +impl Mp4Writer { + pub fn new(mut w: W, width: u32, height: u32, fps: u32) -> Result { + let (width, height) = (u16::try_from(width).map_err(|_| bad("too wide"))?, u16::try_from(height).map_err(|_| bad("too tall"))?); + if width == 0 || height == 0 || !(1..=240).contains(&fps) { + return Err(bad("bad size or frame rate")); + } + let mut ftyp = Vec::new(); + ftyp.extend_from_slice(b"isom"); + ftyp.extend_from_slice(&0x200u32.to_be_bytes()); + for brand in [b"isom", b"iso6", b"av01", b"mp41"] { + ftyp.extend_from_slice(brand); + } + w.write_all(&mp4_box(b"ftyp", &ftyp)?).map_err(io)?; + let start = w.stream_position().map_err(io)?; + // size 1 = a 64-bit size follows (patched in finish) + w.write_all(&1u32.to_be_bytes()).map_err(io)?; + w.write_all(b"mdat").map_err(io)?; + w.write_all(&0u64.to_be_bytes()).map_err(io)?; + Ok(Mp4Writer { + w, + width, + height, + fps, + mdat_size_at: start + 8, + data_start: start + 16, + written: 0, + sizes: Vec::new(), + durations: Vec::new(), + keys: Vec::new(), + config: None, + }) + } + + /// The `av1C` record ([`av1_config`]); needed before [`Self::finish`]. + pub fn set_config(&mut self, config: Vec) { + self.config = Some(config); + } + + pub fn add(&mut self, s: &Sample) -> Result<()> { + let size = u32::try_from(s.data.len()).map_err(|_| bad("sample too large"))?; + if s.duration == 0 { + return Err(bad("a sample lasts at least one frame")); + } + if self.sizes.is_empty() && !s.key { + return Err(bad("the first sample must be a key frame")); + } + self.w.write_all(&s.data).map_err(io)?; + self.written = self.written.saturating_add(u64::from(size)); + self.sizes.push(size); + self.durations.push(s.duration); + if s.key { + let n = u32::try_from(self.sizes.len()).map_err(|_| bad("too many samples"))?; + self.keys.push(n); + } + Ok(()) + } + + /// Frame periods written so far. + pub fn periods(&self) -> u64 { + self.durations.iter().sum() + } + + pub fn finish(mut self) -> Result { + if self.sizes.is_empty() { + return Err(bad("no frames")); + } + let config = self.config.take().ok_or_else(|| bad("no AV1 configuration"))?; + let periods = self.periods(); + if periods > MAX_PERIODS { + return Err(bad("video too long")); + } + let end = self.w.stream_position().map_err(io)?; + self.w.seek(SeekFrom::Start(self.mdat_size_at)).map_err(io)?; + self.w.write_all(&(end - (self.mdat_size_at - 8)).to_be_bytes()).map_err(io)?; + self.w.seek(SeekFrom::Start(end)).map_err(io)?; + let moov = self.moov(&config, periods)?; + self.w.write_all(&moov).map_err(io)?; + self.w.flush().map_err(io)?; + Ok(self.w) + } + + fn moov(&self, config: &[u8], periods: u64) -> Result> { + let n = u32::try_from(self.sizes.len()).map_err(|_| bad("too many samples"))?; + let timescale = u32::try_from(u64::from(self.fps) * TICKS).map_err(|_| bad("bad frame rate"))?; + let ticks = periods.checked_mul(TICKS).ok_or_else(|| bad("video too long"))?; + // the movie's own timescale: milliseconds + let movie_ms = periods.saturating_mul(1000) / u64::from(self.fps); + let u32_of = |v: u64| u32::try_from(v).map_err(|_| bad("video too long")); + let (track_ticks, movie_ms) = (u32_of(ticks)?, u32_of(movie_ms)?); + + let mut mvhd = Vec::new(); + mvhd.extend_from_slice(&[0; 8]); // creation, modification + mvhd.extend_from_slice(&1000u32.to_be_bytes()); + mvhd.extend_from_slice(&movie_ms.to_be_bytes()); + mvhd.extend_from_slice(&0x0001_0000u32.to_be_bytes()); // rate 1.0 + mvhd.extend_from_slice(&0x0100u16.to_be_bytes()); // volume 1.0 + mvhd.extend_from_slice(&[0; 10]); + mvhd.extend_from_slice(&matrix()); + mvhd.extend_from_slice(&[0; 24]); // pre_defined + mvhd.extend_from_slice(&2u32.to_be_bytes()); // next track id + + let mut tkhd = Vec::new(); + tkhd.extend_from_slice(&[0; 8]); + tkhd.extend_from_slice(&1u32.to_be_bytes()); // track id + tkhd.extend_from_slice(&[0; 4]); + tkhd.extend_from_slice(&movie_ms.to_be_bytes()); + tkhd.extend_from_slice(&[0; 8]); + tkhd.extend_from_slice(&[0; 2]); // layer + tkhd.extend_from_slice(&[0; 2]); // alternate group + tkhd.extend_from_slice(&[0; 2]); // volume (video: 0) + tkhd.extend_from_slice(&[0; 2]); + tkhd.extend_from_slice(&matrix()); + tkhd.extend_from_slice(&(u32::from(self.width) << 16).to_be_bytes()); + tkhd.extend_from_slice(&(u32::from(self.height) << 16).to_be_bytes()); + + let mut mdhd = Vec::new(); + mdhd.extend_from_slice(&[0; 8]); + mdhd.extend_from_slice(×cale.to_be_bytes()); + mdhd.extend_from_slice(&track_ticks.to_be_bytes()); + mdhd.extend_from_slice(&0x55c4u16.to_be_bytes()); // language "und" + mdhd.extend_from_slice(&[0; 2]); + + let mut hdlr = Vec::new(); + hdlr.extend_from_slice(&[0; 4]); + hdlr.extend_from_slice(b"vide"); + hdlr.extend_from_slice(&[0; 12]); + hdlr.extend_from_slice(b"LightCraft video\0"); + + let mut vmhd = Vec::new(); + vmhd.extend_from_slice(&[0; 8]); // graphics mode, opcolor + let url = full_box(b"url ", 0, 1, &[])?; + let mut dref = 1u32.to_be_bytes().to_vec(); + dref.extend_from_slice(&url); + let dinf = mp4_box(b"dinf", &full_box(b"dref", 0, 0, &dref)?)?; + + // av01 visual sample entry + let mut av01 = Vec::new(); + av01.extend_from_slice(&[0; 6]); + av01.extend_from_slice(&1u16.to_be_bytes()); // data reference index + av01.extend_from_slice(&[0; 16]); // pre_defined, reserved + av01.extend_from_slice(&self.width.to_be_bytes()); + av01.extend_from_slice(&self.height.to_be_bytes()); + av01.extend_from_slice(&0x0048_0000u32.to_be_bytes()); // 72 dpi + av01.extend_from_slice(&0x0048_0000u32.to_be_bytes()); + av01.extend_from_slice(&[0; 4]); + av01.extend_from_slice(&1u16.to_be_bytes()); // frame count + let mut name = [0u8; 32]; + let label = b"AOM Coding"; + name[0] = label.len() as u8; + name[1..=label.len()].copy_from_slice(label); + av01.extend_from_slice(&name); + av01.extend_from_slice(&0x0018u16.to_be_bytes()); // depth + av01.extend_from_slice(&(-1i16).to_be_bytes()); + av01.extend_from_slice(&mp4_box(b"av1C", config)?); + // BT.709 primaries, transfer and matrix, limited range + let mut colr = b"nclx".to_vec(); + colr.extend_from_slice(&[0, 1, 0, 1, 0, 1, 0]); + av01.extend_from_slice(&mp4_box(b"colr", &colr)?); + let mut pasp = 1u32.to_be_bytes().to_vec(); + pasp.extend_from_slice(&1u32.to_be_bytes()); + av01.extend_from_slice(&mp4_box(b"pasp", &pasp)?); + let mut stsd = 1u32.to_be_bytes().to_vec(); + stsd.extend_from_slice(&mp4_box(b"av01", &av01)?); + + // decoding times: runs of equal durations + let mut runs: Vec<(u32, u64)> = Vec::new(); + for d in &self.durations { + match runs.last_mut() { + Some((count, delta)) if *delta == *d => *count += 1, + _ => runs.push((1, *d)), + } + } + let mut stts = u32::try_from(runs.len()).map_err(|_| bad("too many samples"))?.to_be_bytes().to_vec(); + for (count, delta) in runs { + stts.extend_from_slice(&count.to_be_bytes()); + stts.extend_from_slice(&u32_of(delta.saturating_mul(TICKS))?.to_be_bytes()); + } + let mut stsc = 1u32.to_be_bytes().to_vec(); + for v in [1u32, n, 1] { + stsc.extend_from_slice(&v.to_be_bytes()); + } + let mut stsz = 0u32.to_be_bytes().to_vec(); + stsz.extend_from_slice(&n.to_be_bytes()); + for s in &self.sizes { + stsz.extend_from_slice(&s.to_be_bytes()); + } + // one chunk holding every sample, at the start of mdat's data + let chunk_offset = match u32::try_from(self.data_start) { + Ok(o) => full_box(b"stco", 0, 0, &[1u32.to_be_bytes(), o.to_be_bytes()].concat())?, + Err(_) => { + let mut b = 1u32.to_be_bytes().to_vec(); + b.extend_from_slice(&self.data_start.to_be_bytes()); + full_box(b"co64", 0, 0, &b)? + } + }; + let mut stbl = Vec::new(); + stbl.extend_from_slice(&full_box(b"stsd", 0, 0, &stsd)?); + stbl.extend_from_slice(&full_box(b"stts", 0, 0, &stts)?); + if self.keys.len() < self.sizes.len() { + let mut stss = u32::try_from(self.keys.len()).map_err(|_| bad("too many samples"))?.to_be_bytes().to_vec(); + for k in &self.keys { + stss.extend_from_slice(&k.to_be_bytes()); + } + stbl.extend_from_slice(&full_box(b"stss", 0, 0, &stss)?); + } + stbl.extend_from_slice(&full_box(b"stsc", 0, 0, &stsc)?); + stbl.extend_from_slice(&full_box(b"stsz", 0, 0, &stsz)?); + stbl.extend_from_slice(&chunk_offset); + + let mut minf = full_box(b"vmhd", 0, 1, &vmhd)?; + minf.extend_from_slice(&dinf); + minf.extend_from_slice(&mp4_box(b"stbl", &stbl)?); + let mut mdia = full_box(b"mdhd", 0, 0, &mdhd)?; + mdia.extend_from_slice(&full_box(b"hdlr", 0, 0, &hdlr)?); + mdia.extend_from_slice(&mp4_box(b"minf", &minf)?); + let mut trak = full_box(b"tkhd", 0, 3, &tkhd)?; + trak.extend_from_slice(&mp4_box(b"mdia", &mdia)?); + let mut moov = full_box(b"mvhd", 0, 0, &mvhd)?; + moov.extend_from_slice(&mp4_box(b"trak", &trak)?); + mp4_box(b"moov", &moov) + } +} + +// ---------------------------------------------------------------- WebM + +/// An EBML element id with its length marker, as written (e.g. `0x1A45DFA3`). +fn ebml_id(id: u32) -> Vec { + let b = id.to_be_bytes(); + let skip = b.iter().take_while(|x| **x == 0).count().min(3); + b[skip..].to_vec() +} + +/// An EBML size, in the fewest bytes (all-ones values are reserved for "unknown"). +fn ebml_size(n: u64) -> Vec { + for len in 1..=8u32 { + if n < (1u64 << (7 * len)) - 1 { + let mut v = Vec::with_capacity(len as usize); + let marked = n | (1u64 << (7 * len)); + let bytes = marked.to_be_bytes(); + v.extend_from_slice(&bytes[(8 - len as usize)..]); + return v; + } + } + vec![0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe] +} + +/// An 8-byte EBML size (patchable in place). +fn ebml_size8(n: u64) -> [u8; 8] { + let mut b = (n & 0x00ff_ffff_ffff_ffff).to_be_bytes(); + b[0] = 0x01; + b +} + +fn el(id: u32, body: &[u8]) -> Vec { + let mut v = ebml_id(id); + v.extend_from_slice(&ebml_size(body.len() as u64)); + v.extend_from_slice(body); + v +} + +fn el_uint(id: u32, n: u64) -> Vec { + let b = n.to_be_bytes(); + let skip = b.iter().take_while(|x| **x == 0).count().min(7); + el(id, &b[skip..]) +} + +fn el_float(id: u32, f: f64) -> Vec { + el(id, &f.to_be_bytes()) +} + +const EBML: u32 = 0x1A45_DFA3; +const SEGMENT: u32 = 0x1853_8067; +const SEEK_HEAD: u32 = 0x114D_9B74; +const SEEK: u32 = 0x4DBB; +const SEEK_ID: u32 = 0x53AB; +const SEEK_POSITION: u32 = 0x53AC; +const VOID: u32 = 0xEC; +const INFO: u32 = 0x1549_A966; +const TIMESTAMP_SCALE: u32 = 0x2A_D7B1; +const DURATION: u32 = 0x4489; +const TRACKS: u32 = 0x1654_AE6B; +const TRACK_ENTRY: u32 = 0xAE; +const CLUSTER: u32 = 0x1F43_B675; +const TIMESTAMP: u32 = 0xE7; +const SIMPLE_BLOCK: u32 = 0xA3; +const BLOCK_GROUP: u32 = 0xA0; +const BLOCK: u32 = 0xA1; +const BLOCK_DURATION: u32 = 0x9B; +const REFERENCE_BLOCK: u32 = 0xFB; +const CUES: u32 = 0x1C53_BB6B; + +/// Room kept after the Segment header for the SeekHead written at the end. +const SEEK_ROOM: usize = 96; + +/// A WebM writer (see the module docs). Timestamps are in milliseconds. +pub struct WebmWriter { + w: W, + width: u32, + height: u32, + fps: u32, + segment_size_at: u64, + segment_start: u64, + duration_at: u64, + info_at: u64, + tracks_at: Option, + config: Option>, + /// Frame periods before the pending sample. + periods: u64, + /// The sample not written yet (the last one becomes a BlockGroup with its duration). + pending: Option<(Sample, u64)>, + cluster: Vec, + cluster_ts: u64, + last_block_ts: u64, + cues: Vec<(u64, u64)>, +} + +impl WebmWriter { + pub fn new(mut w: W, width: u32, height: u32, fps: u32) -> Result { + if width == 0 || height == 0 || width > 65_535 || height > 65_535 || !(1..=240).contains(&fps) { + return Err(bad("bad size or frame rate")); + } + let mut header = Vec::new(); + header.extend_from_slice(&el_uint(0x4286, 1)); // EBMLVersion + header.extend_from_slice(&el_uint(0x42F7, 1)); // EBMLReadVersion + header.extend_from_slice(&el_uint(0x42F2, 4)); // EBMLMaxIDLength + header.extend_from_slice(&el_uint(0x42F3, 8)); // EBMLMaxSizeLength + header.extend_from_slice(&el(0x4282, b"webm")); // DocType + header.extend_from_slice(&el_uint(0x4287, 4)); // DocTypeVersion + header.extend_from_slice(&el_uint(0x4285, 2)); // DocTypeReadVersion + w.write_all(&el(EBML, &header)).map_err(io)?; + w.write_all(&ebml_id(SEGMENT)).map_err(io)?; + let segment_size_at = w.stream_position().map_err(io)?; + w.write_all(&ebml_size8(0)).map_err(io)?; + let segment_start = w.stream_position().map_err(io)?; + // a Void the SeekHead replaces at the end + w.write_all(&el(VOID, &[0; SEEK_ROOM - 2])).map_err(io)?; + let info_at = w.stream_position().map_err(io)? - segment_start; + let mut info = el_uint(TIMESTAMP_SCALE, 1_000_000); + let duration_offset = info.len() + 3; // after Duration's id (2 bytes) and size (1 byte) + info.extend_from_slice(&el_float(DURATION, 0.0)); + info.extend_from_slice(&el(0x4D80, b"LightCraft")); // MuxingApp + info.extend_from_slice(&el(0x5741, b"LightCraft")); // WritingApp + let info_el = el(INFO, &info); + let duration_at = segment_start + info_at + (info_el.len() - info.len()) as u64 + duration_offset as u64; + w.write_all(&info_el).map_err(io)?; + Ok(WebmWriter { + w, + width, + height, + fps, + segment_size_at, + segment_start, + duration_at, + info_at, + tracks_at: None, + config: None, + periods: 0, + pending: None, + cluster: Vec::new(), + cluster_ts: 0, + last_block_ts: 0, + cues: Vec::new(), + }) + } + + /// The `CodecPrivate` (the `av1C` record, [`av1_config`]); needed before the first sample. + pub fn set_config(&mut self, config: Vec) { + self.config = Some(config); + } + + fn ms(&self, periods: u64) -> u64 { + periods.saturating_mul(1000) / u64::from(self.fps) + } + + fn write_tracks(&mut self) -> Result<()> { + let config = self.config.clone().ok_or_else(|| bad("no AV1 configuration"))?; + let mut video = el_uint(0xB0, u64::from(self.width)); // PixelWidth + video.extend_from_slice(&el_uint(0xBA, u64::from(self.height))); // PixelHeight + let mut entry = el_uint(0xD7, 1); // TrackNumber + entry.extend_from_slice(&el_uint(0x73C5, 1)); // TrackUID + entry.extend_from_slice(&el_uint(0x83, 1)); // TrackType video + entry.extend_from_slice(&el_uint(0x9C, 0)); // FlagLacing + entry.extend_from_slice(&el(0x86, b"V_AV1")); // CodecID + entry.extend_from_slice(&el(0x63A2, &config)); // CodecPrivate + entry.extend_from_slice(&el_uint(0x23E383, 1_000_000_000 / u64::from(self.fps))); // DefaultDuration (ns) + entry.extend_from_slice(&el(0xE0, &video)); + let at = self.w.stream_position().map_err(io)? - self.segment_start; + self.w.write_all(&el(TRACKS, &el(TRACK_ENTRY, &entry))).map_err(io)?; + self.tracks_at = Some(at); + Ok(()) + } + + fn flush_cluster(&mut self) -> Result<()> { + if self.cluster.is_empty() { + return Ok(()); + } + let mut body = el_uint(TIMESTAMP, self.cluster_ts); + body.extend_from_slice(&self.cluster); + let at = self.w.stream_position().map_err(io)? - self.segment_start; + self.w.write_all(&el(CLUSTER, &body)).map_err(io)?; + self.cues.push((self.cluster_ts, at)); + self.cluster.clear(); + Ok(()) + } + + /// The block header: track 1, timestamp relative to the cluster, flags. + fn block(&self, data: &[u8], ts: u64, flags: u8) -> Result> { + let rel = i16::try_from(ts.saturating_sub(self.cluster_ts)).map_err(|_| bad("block timestamp out of range"))?; + let mut b = Vec::with_capacity(data.len() + 4); + b.push(0x81); + b.extend_from_slice(&rel.to_be_bytes()); + b.push(flags); + b.extend_from_slice(data); + Ok(b) + } + + /// Start a new cluster at a key frame or before the relative timestamp could overflow. + fn place(&mut self, key: bool, ts: u64) -> Result<()> { + if !self.cluster.is_empty() && (key || ts.saturating_sub(self.cluster_ts) > 30_000) { + self.flush_cluster()?; + } + if self.cluster.is_empty() { + self.cluster_ts = ts; + } + Ok(()) + } + + fn write_simple(&mut self, s: &Sample, ts: u64) -> Result<()> { + self.place(s.key, ts)?; + let b = self.block(&s.data, ts, if s.key { 0x80 } else { 0 })?; + self.cluster.extend_from_slice(&el(SIMPLE_BLOCK, &b)); + self.last_block_ts = ts; + Ok(()) + } + + pub fn add(&mut self, s: &Sample) -> Result<()> { + if s.duration == 0 { + return Err(bad("a sample lasts at least one frame")); + } + if self.tracks_at.is_none() { + if !s.key { + return Err(bad("the first sample must be a key frame")); + } + self.write_tracks()?; + } + if let Some((p, start)) = self.pending.take() { + let ts = self.ms(start); + self.write_simple(&p, ts)?; + } + let start = self.periods; + self.periods = self.periods.saturating_add(s.duration); + if self.periods > MAX_PERIODS { + return Err(bad("video too long")); + } + self.pending = Some((s.clone(), start)); + Ok(()) + } + + pub fn periods(&self) -> u64 { + self.periods + } + + pub fn finish(mut self) -> Result { + let (last, start) = self.pending.take().ok_or_else(|| bad("no frames"))?; + let ts = self.ms(start); + self.place(last.key, ts)?; + let mut group = el(BLOCK, &self.block(&last.data, ts, 0)?); + group.extend_from_slice(&el_uint(BLOCK_DURATION, self.ms(self.periods).saturating_sub(ts).max(1))); + if !last.key { + // the previous block, as a (negative) offset from this one + let back = i64::try_from(ts.saturating_sub(self.last_block_ts)).unwrap_or(i64::MAX).max(1); + el_int_into(&mut group, REFERENCE_BLOCK, -back); + } + self.cluster.extend_from_slice(&el(BLOCK_GROUP, &group)); + self.flush_cluster()?; + // cues: one per cluster (each starts with a key frame or carries on from one) + let cues_at = self.w.stream_position().map_err(io)? - self.segment_start; + let mut cues = Vec::new(); + for (t, pos) in &self.cues { + let mut positions = el_uint(0xF7, 1); // CueTrack + positions.extend_from_slice(&el_uint(0xF1, *pos)); // CueClusterPosition + let mut point = el_uint(0xB3, *t); // CueTime + point.extend_from_slice(&el(0xB7, &positions)); // CueTrackPositions + cues.extend_from_slice(&el(0xBB, &point)); // CuePoint + } + self.w.write_all(&el(CUES, &cues)).map_err(io)?; + let end = self.w.stream_position().map_err(io)?; + // SeekHead in the room kept for it, the rest stays a Void + let tracks_at = self.tracks_at.ok_or_else(|| bad("no tracks"))?; + let mut seeks = Vec::new(); + for (id, at) in [(INFO, self.info_at), (TRACKS, tracks_at), (CUES, cues_at)] { + let mut seek = el(SEEK_ID, &ebml_id(id)); + seek.extend_from_slice(&el(SEEK_POSITION, &at.to_be_bytes())); + seeks.extend_from_slice(&el(SEEK, &seek)); + } + let head = el(SEEK_HEAD, &seeks); + let rest = SEEK_ROOM.checked_sub(head.len()).filter(|r| *r >= 2).ok_or_else(|| bad("seek head too large"))?; + let mut room = head; + room.extend_from_slice(&el(VOID, &vec![0; rest - 2])); + self.w.seek(SeekFrom::Start(self.segment_start)).map_err(io)?; + self.w.write_all(&room).map_err(io)?; + self.w.seek(SeekFrom::Start(self.duration_at)).map_err(io)?; + self.w.write_all(&(self.ms(self.periods) as f64).to_be_bytes()).map_err(io)?; + self.w.seek(SeekFrom::Start(self.segment_size_at)).map_err(io)?; + self.w.write_all(&ebml_size8(end - self.segment_start)).map_err(io)?; + self.w.seek(SeekFrom::Start(end)).map_err(io)?; + self.w.flush().map_err(io)?; + Ok(self.w) + } +} + +fn el_int_into(out: &mut Vec, id: u32, v: i64) { + let b = v.to_be_bytes(); + // the fewest bytes that keep the sign + let mut skip = 0; + while skip < 7 && ((b[skip] == 0 && b[skip + 1] & 0x80 == 0) || (b[skip] == 0xff && b[skip + 1] & 0x80 != 0)) { + skip += 1; + } + out.extend_from_slice(&el(id, &b[skip..])); +} + +// ---------------------------------------------------------------- either container + +/// An MP4 or WebM writer. +pub enum VideoWriter { + Mp4(Mp4Writer), + WebM(WebmWriter), +} + +impl VideoWriter { + pub fn new(w: W, container: Container, width: u32, height: u32, fps: u32) -> Result { + Ok(match container { + Container::Mp4 => VideoWriter::Mp4(Mp4Writer::new(w, width, height, fps)?), + Container::WebM => VideoWriter::WebM(WebmWriter::new(w, width, height, fps)?), + }) + } + + pub fn set_config(&mut self, config: Vec) { + match self { + VideoWriter::Mp4(m) => m.set_config(config), + VideoWriter::WebM(m) => m.set_config(config), + } + } + + pub fn add(&mut self, s: &Sample) -> Result<()> { + match self { + VideoWriter::Mp4(m) => m.add(s), + VideoWriter::WebM(m) => m.add(s), + } + } + + pub fn periods(&self) -> u64 { + match self { + VideoWriter::Mp4(m) => m.periods(), + VideoWriter::WebM(m) => m.periods(), + } + } + + pub fn finish(self) -> Result { + match self { + VideoWriter::Mp4(m) => m.finish(), + VideoWriter::WebM(m) => m.finish(), + } + } +} + +// ---------------------------------------------------------------- AV1 encoder + +/// How to encode: frame size (even numbers; odd ones are rounded down), nominal frame rate, +/// quality 1–100 and rav1e speed 1 (slow) – 10 (fast). +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct VideoConfig { + pub width: u32, + pub height: u32, + pub fps: u32, + pub quality: u8, + pub speed: u8, +} + +/// Is AV1 video encoding in this build (native, `avif` feature)? +pub const AV1_AVAILABLE: bool = cfg!(all(feature = "avif", not(target_arch = "wasm32"))); + +/// BT.709 limited-range 4:2:0 planes of an sRGB RGBA image (alpha ignored), `w × h` (even). +pub fn yuv420(rgba: &[[u8; 4]], w: usize, h: usize) -> Result<(Vec, Vec, Vec)> { + if w == 0 || h == 0 || !w.is_multiple_of(2) || !h.is_multiple_of(2) || w.checked_mul(h).is_none_or(|n| rgba.len() < n) { + return Err(bad("frame size")); + } + const KR: f32 = 0.2126; + const KB: f32 = 0.0722; + const KG: f32 = 1.0 - KR - KB; + let mut y = vec![0u8; w * h]; + let (cw, ch) = (w / 2, h / 2); + let mut u = vec![0u8; cw * ch]; + let mut v = vec![0u8; cw * ch]; + let luma = |p: [u8; 4]| KR * f32::from(p[0]) + KG * f32::from(p[1]) + KB * f32::from(p[2]); + for (i, out) in y.iter_mut().enumerate() { + let p = rgba.get(i).copied().unwrap_or([0; 4]); + *out = (16.0 + luma(p) * (219.0 / 255.0)).round().clamp(0.0, 255.0) as u8; + } + for cy in 0..ch { + for cx in 0..cw { + let mut acc = [0f32; 3]; + for (dx, dy) in [(0, 0), (1, 0), (0, 1), (1, 1)] { + let p = rgba.get((cy * 2 + dy) * w + cx * 2 + dx).copied().unwrap_or([0; 4]); + for (a, c) in acc.iter_mut().zip(p) { + *a += f32::from(c) / 4.0; + } + } + let l = KR * acc[0] + KG * acc[1] + KB * acc[2]; + let cb = (acc[2] - l) / (2.0 * (1.0 - KB)); + let cr = (acc[0] - l) / (2.0 * (1.0 - KR)); + let i = cy * cw + cx; + if let Some(o) = u.get_mut(i) { + *o = (128.0 + cb * (224.0 / 255.0)).round().clamp(0.0, 255.0) as u8; + } + if let Some(o) = v.get_mut(i) { + *o = (128.0 + cr * (224.0 / 255.0)).round().clamp(0.0, 255.0) as u8; + } + } + } + Ok((y, u, v)) +} + +/// The quantizer for `quality` (ravif's curve, so qualities match the AVIF export). +pub fn quantizer(quality: u8) -> usize { + let q = f32::from(quality.clamp(1, 100)) / 100.0; + let x = if q >= 0.82 { + (1.0 - q) * 2.6 + } else if q > 0.25 { + 1.0 - 0.125 - q * 0.5 + } else { + 1.0 - q + }; + (x * 255.0).round().clamp(0.0, 255.0) as usize +} + +/// An AV1 encoder fed RGBA frames, each with how many frame periods it stays up; it hands back +/// the samples as they come out (in order: no frame reordering). +pub struct Av1Encoder { + #[cfg(all(feature = "avif", not(target_arch = "wasm32")))] + ctx: rav1e::Context, + /// Each frame sent so far: its duration (by input frame number). + #[cfg_attr(not(all(feature = "avif", not(target_arch = "wasm32"))), allow(dead_code))] + durations: Vec, + width: usize, + height: usize, + seq_header: Option>, +} + +impl Av1Encoder { + pub fn new(c: &VideoConfig) -> Result { + let (w, h) = ((c.width & !1) as usize, (c.height & !1) as usize); + if w < 16 || h < 16 || w > 8192 || h > 8192 || !(1..=240).contains(&c.fps) { + return Err(bad("unsupported video size or frame rate")); + } + #[cfg(all(feature = "avif", not(target_arch = "wasm32")))] + { + use rav1e::prelude::*; + let mut ec = EncoderConfig::with_speed_preset(c.speed.clamp(1, 10)); + ec.width = w; + ec.height = h; + ec.time_base = Rational::new(1, u64::from(c.fps)); + ec.bit_depth = 8; + ec.chroma_sampling = ChromaSampling::Cs420; + ec.pixel_range = PixelRange::Limited; + ec.color_description = Some(ColorDescription { + color_primaries: ColorPrimaries::BT709, + transfer_characteristics: TransferCharacteristics::BT709, + matrix_coefficients: MatrixCoefficients::BT709, + }); + // in order, one shown frame per packet: samples map 1:1 to the frames sent + ec.low_latency = true; + let q = quantizer(c.quality); + ec.quantizer = q; + ec.min_quantizer = q.min(255) as u8; + ec.min_key_frame_interval = 1; + ec.max_key_frame_interval = 120; + // tiles are what rav1e encodes in parallel (16 measured best for 1080p on 32 threads) + ec.tiles = std::thread::available_parallelism().map_or(4, |n| n.get()).clamp(2, 16); + let ctx: Context = Config::new().with_encoder_config(ec).new_context().map_err(|e| bad(format!("AV1: {e}")))?; + Ok(Av1Encoder { ctx, durations: Vec::new(), width: w, height: h, seq_header: None }) + } + #[cfg(not(all(feature = "avif", not(target_arch = "wasm32"))))] + { + let _ = (w, h); + Err(bad("AV1 video encoding is not available in this build")) + } + } + + /// The frame size actually encoded (even numbers). + pub fn size(&self) -> (usize, usize) { + (self.width, self.height) + } + + /// The configuration record for the container ([`av1_config`]), once the first sample (with + /// the sequence header) has come out. + pub fn config_record(&self) -> Option> { + self.seq_header.as_ref().map(|s| av1_config(s)) + } + + /// Encode `rgba` (`width × height` of [`Self::size`]) shown for `periods` frame periods; + /// `key` forces a key frame (a picture that doesn't lean on earlier ones: a held slide is + /// then clean of the slide before it, and seeking lands on it). + pub fn push(&mut self, rgba: &[[u8; 4]], periods: u64, key: bool) -> Result> { + let (y, u, v) = yuv420(rgba, self.width, self.height)?; + #[cfg(all(feature = "avif", not(target_arch = "wasm32")))] + { + let mut frame = self.ctx.new_frame(); + for (plane, (data, w)) in frame.planes.iter_mut().zip([(&y, self.width), (&u, self.width / 2), (&v, self.width / 2)]) { + plane.copy_from_raw_u8(data, w, 1); + } + let params = rav1e::prelude::FrameParameters { + frame_type_override: if key { rav1e::prelude::FrameTypeOverride::Key } else { rav1e::prelude::FrameTypeOverride::No }, + ..Default::default() + }; + self.ctx.send_frame((frame, params)).map_err(|e| bad(format!("AV1: {e}")))?; + self.durations.push(periods.max(1)); + self.drain() + } + #[cfg(not(all(feature = "avif", not(target_arch = "wasm32"))))] + { + let _ = (y, u, v, periods, key); + Err(bad("AV1 video encoding is not available in this build")) + } + } + + /// Flush the encoder: the samples still inside. + pub fn finish(&mut self) -> Result> { + #[cfg(all(feature = "avif", not(target_arch = "wasm32")))] + { + self.ctx.flush(); + self.drain() + } + #[cfg(not(all(feature = "avif", not(target_arch = "wasm32"))))] + Err(bad("AV1 video encoding is not available in this build")) + } + + #[cfg(all(feature = "avif", not(target_arch = "wasm32")))] + fn drain(&mut self) -> Result> { + use rav1e::prelude::*; + let mut out = Vec::new(); + loop { + match self.ctx.receive_packet() { + Ok(p) => { + let (data, seq) = to_sample_data(&p.data)?; + if self.seq_header.is_none() { + self.seq_header = seq; + } + let duration = usize::try_from(p.input_frameno).ok().and_then(|i| self.durations.get(i)).copied().unwrap_or(1); + out.push(Sample { data, key: p.frame_type == FrameType::KEY, duration }); + } + Err(EncoderStatus::Encoded) => {} + Err(EncoderStatus::NeedMoreData | EncoderStatus::LimitReached) => break, + Err(e) => return Err(bad(format!("AV1: {e}"))), + } + } + Ok(out) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use std::io::Cursor; + + /// The boxes of an MP4 byte range: (type, body range). + pub fn boxes(d: &[u8], range: std::ops::Range) -> Vec<([u8; 4], std::ops::Range)> { + let mut out = Vec::new(); + let mut p = range.start; + while p + 8 <= range.end { + let size32 = u32::from_be_bytes(d[p..p + 4].try_into().unwrap()) as usize; + let kind: [u8; 4] = d[p + 4..p + 8].try_into().unwrap(); + let (hdr, size) = if size32 == 1 { (16, u64::from_be_bytes(d[p + 8..p + 16].try_into().unwrap()) as usize) } else { (8, size32) }; + assert!(size >= hdr && p + size <= range.end, "box {} overruns", String::from_utf8_lossy(&kind)); + out.push((kind, p + hdr..p + size)); + p += size; + } + assert_eq!(p, range.end, "trailing bytes"); + out + } + + fn find(d: &[u8], r: std::ops::Range, path: &[&[u8; 4]]) -> std::ops::Range { + let mut r = r; + for k in path { + r = boxes(d, r).into_iter().find(|(t, _)| t == *k).unwrap_or_else(|| panic!("no {}", String::from_utf8_lossy(*k))).1; + } + r + } + + fn samples() -> Vec { + // fake AV1 data: an OBU_FRAME (type 6) with a size field + let frame = |b: u8| vec![0x32, 2, b, b]; + vec![ + Sample { data: frame(1), key: true, duration: 30 }, + Sample { data: frame(2), key: false, duration: 1 }, + Sample { data: frame(3), key: false, duration: 1 }, + Sample { data: frame(4), key: true, duration: 58 }, + ] + } + + #[test] + fn obus_split_and_temporal_delimiters_go() { + // TD, sequence header (3 bytes), frame (2 bytes) + let tu = [0x12, 0, 0x0a, 3, 1, 2, 3, 0x32, 2, 9, 9]; + let o = obus(&tu).unwrap(); + assert_eq!(o.iter().map(|(k, _)| *k).collect::>(), vec![2, 1, 6]); + let (data, seq) = to_sample_data(&tu).unwrap(); + assert_eq!(data, &tu[2..]); + assert_eq!(seq.unwrap(), &tu[2..7]); + // hostile: sizes past the end, forbidden bit, endless LEB128 + assert!(obus(&[0x32, 9, 1]).is_err()); + assert!(obus(&[0x80]).is_err()); + assert!(obus(&[0x32, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]).is_err()); + assert!(obus(&[0x36]).is_err(), "extension byte missing"); + assert_eq!(av1_config(&[1, 2])[..], [0x81, 31, 0x0c, 0, 1, 2]); + } + + #[test] + fn mp4_box_tree_samples_and_durations() { + let mut m = Mp4Writer::new(Cursor::new(Vec::new()), 64, 36, 30).unwrap(); + assert!(m.add(&Sample { data: vec![1], key: false, duration: 1 }).is_err(), "must start on a key frame"); + for s in samples() { + m.add(&s).unwrap(); + } + assert!(Mp4Writer::new(Cursor::new(Vec::new()), 64, 36, 30).unwrap().finish().is_err(), "no frames"); + m.set_config(av1_config(&[0x0a, 1, 0])); + let d = m.finish().unwrap().into_inner(); + let top = boxes(&d, 0..d.len()); + assert_eq!(top.iter().map(|(k, _)| *k).collect::>(), vec![*b"ftyp", *b"mdat", *b"moov"]); + assert_eq!(&d[top[1].1.clone()], &[0x32, 2, 1, 1, 0x32, 2, 2, 2, 0x32, 2, 3, 3, 0x32, 2, 4, 4]); + let stbl = find(&d, 0..d.len(), &[b"moov", b"trak", b"mdia", b"minf", b"stbl"]); + let kinds: Vec<_> = boxes(&d, stbl.clone()).into_iter().map(|(k, _)| k).collect(); + assert_eq!(kinds, vec![*b"stsd", *b"stts", *b"stss", *b"stsc", *b"stsz", *b"stco"]); + let be = |r: usize| u32::from_be_bytes(d[r..r + 4].try_into().unwrap()); + let stsz = find(&d, stbl.clone(), &[b"stsz"]); + assert_eq!(be(stsz.start + 8), 4, "sample count"); + let stts = find(&d, stbl.clone(), &[b"stts"]); + assert_eq!(be(stts.start + 4), 3, "runs: 30, 1×2, 58"); + let total: u32 = (0..3).map(|i| be(stts.start + 8 + i * 8) * be(stts.start + 12 + i * 8)).sum(); + assert_eq!(total, 90 * 512); + let stss = find(&d, stbl.clone(), &[b"stss"]); + assert_eq!((be(stss.start + 4), be(stss.start + 8), be(stss.start + 12)), (2, 1, 4)); + let stco = find(&d, stbl.clone(), &[b"stco"]); + assert_eq!(be(stco.start + 8) as usize, top[1].1.start, "chunk offset = mdat data"); + let stsd = find(&d, stbl, &[b"stsd"]); + let av01 = boxes(&d, stsd.start + 8..stsd.end); + assert_eq!(av01[0].0, *b"av01"); + let entry = av01[0].1.clone(); + assert_eq!(u16::from_be_bytes([d[entry.start + 24], d[entry.start + 25]]), 64); + let inner: Vec<_> = boxes(&d, entry.start + 78..entry.end).into_iter().map(|(k, _)| k).collect(); + assert_eq!(inner, vec![*b"av1C", *b"colr", *b"pasp"]); + let mdhd = find(&d, 0..d.len(), &[b"moov", b"trak", b"mdia", b"mdhd"]); + assert_eq!((be(mdhd.start + 12), be(mdhd.start + 16)), (30 * 512, 90 * 512)); + let mvhd = find(&d, 0..d.len(), &[b"moov", b"mvhd"]); + assert_eq!(be(mvhd.start + 16), 3000, "3 s in ms"); + } + + /// The EBML elements of a byte range: (id, body range). + fn elements(d: &[u8], range: std::ops::Range) -> Vec<(u32, std::ops::Range)> { + let vint = |p: usize| -> (u64, usize) { + let len = d[p].leading_zeros() as usize + 1; + let mut v = u64::from(d[p]) & (0xff >> len); + for i in 1..len { + v = (v << 8) | u64::from(d[p + i]); + } + (v, len) + }; + let mut out = Vec::new(); + let mut p = range.start; + while p < range.end { + let idlen = d[p].leading_zeros() as usize + 1; + let id = d[p..p + idlen].iter().fold(0u32, |a, b| (a << 8) | u32::from(*b)); + let (size, n) = vint(p + idlen); + let start = p + idlen + n; + let end = start + size as usize; + assert!(end <= range.end, "element {id:x} overruns"); + out.push((id, start..end)); + p = end; + } + out + } + + #[test] + fn webm_element_tree_blocks_and_duration() { + let mut m = WebmWriter::new(Cursor::new(Vec::new()), 64, 36, 30).unwrap(); + assert!(m.add(&Sample { data: vec![0x32, 0], key: true, duration: 1 }).is_err(), "configuration first"); + let mut m = WebmWriter::new(Cursor::new(Vec::new()), 64, 36, 30).unwrap(); + m.set_config(av1_config(&[0x0a, 1, 0])); + for s in samples() { + m.add(&s).unwrap(); + } + let d = m.finish().unwrap().into_inner(); + let top = elements(&d, 0..d.len()); + assert_eq!(top.iter().map(|(i, _)| *i).collect::>(), vec![EBML, SEGMENT]); + let seg = elements(&d, top[1].1.clone()); + let ids: Vec<_> = seg.iter().map(|(i, _)| *i).collect(); + assert_eq!(ids, vec![SEEK_HEAD, VOID, INFO, TRACKS, CLUSTER, CLUSTER, CUES]); + let info = elements(&d, seg[2].1.clone()); + let dur = info.iter().find(|(i, _)| *i == DURATION).unwrap().1.clone(); + assert_eq!(f64::from_be_bytes(d[dur].try_into().unwrap()), 3000.0); + let track = elements(&d, elements(&d, seg[3].1.clone())[0].1.clone()); + let codec = track.iter().find(|(i, _)| *i == 0x86).unwrap().1.clone(); + assert_eq!(&d[codec], b"V_AV1"); + let first: Vec<_> = elements(&d, seg[4].1.clone()).into_iter().map(|(i, _)| i).collect(); + assert_eq!(first, vec![TIMESTAMP, SIMPLE_BLOCK, SIMPLE_BLOCK, SIMPLE_BLOCK]); + let second = elements(&d, seg[5].1.clone()); + assert_eq!(d[second[0].1.clone()], [0x04, 0x2a], "the second cluster starts at 1066 ms (32 periods)"); + let group = elements(&d, second[1].1.clone()); + assert_eq!(group.iter().map(|(i, _)| *i).collect::>(), vec![BLOCK, BLOCK_DURATION]); + // the SeekHead points at Info, Tracks and Cues + let seg_start = top[1].1.start; + for seek in elements(&d, seg[0].1.clone()) { + let e = elements(&d, seek.1); + let pos = d[e[1].1.clone()].iter().fold(0usize, |a, b| (a << 8) | usize::from(*b)); + let id = d[e[0].1.clone()].iter().fold(0u32, |a, b| (a << 8) | u32::from(*b)); + assert!(seg.iter().any(|(i, r)| *i == id && r.start > seg_start + pos), "{id:x}"); + } + let mut neg = Vec::new(); + el_int_into(&mut neg, REFERENCE_BLOCK, -33); + assert_eq!(neg, vec![0xfb, 0x81, 0xdf]); + assert_eq!(ebml_size(126), vec![0xfe]); + assert_eq!(ebml_size(127), vec![0x40, 0x7f]); + } + + #[test] + fn hostile_sizes_are_refused() { + assert!(Mp4Writer::new(Cursor::new(Vec::new()), 0, 10, 30).is_err()); + assert!(Mp4Writer::new(Cursor::new(Vec::new()), 70_000, 10, 30).is_err()); + assert!(WebmWriter::new(Cursor::new(Vec::new()), 10, 10, 0).is_err()); + assert!(yuv420(&[[0; 4]; 3], 2, 2).is_err()); + assert!(yuv420(&[[0; 4]; 9], 3, 3).is_err()); + let (y, u, v) = yuv420(&[[255; 4]; 4], 2, 2).unwrap(); + assert_eq!((y, u, v), (vec![235; 4], vec![128], vec![128])); + assert!(quantizer(100) < quantizer(50) && quantizer(50) < quantizer(1)); + assert!(Av1Encoder::new(&VideoConfig { width: 8, height: 8, fps: 30, quality: 50, speed: 10 }).is_err()); + } + + #[cfg(all(feature = "avif", not(target_arch = "wasm32")))] + #[test] + fn encodes_av1_into_a_playable_mp4() { + let c = VideoConfig { width: 64, height: 48, fps: 24, quality: 60, speed: 10 }; + let mut e = Av1Encoder::new(&c).unwrap(); + let mut out = Vec::new(); + for (i, periods) in [(0u8, 24u64), (80, 1), (160, 1), (240, 30)] { + let img = vec![[i, 255 - i, 128, 255]; 64 * 48]; + out.extend(e.push(&img, periods, periods > 1).unwrap()); + } + out.extend(e.finish().unwrap()); + assert_eq!(out.len(), 4, "one sample per frame sent"); + assert!(out[0].key); + assert_eq!(out.iter().map(|s| s.duration).sum::(), 56); + for s in &out { + assert!(obus(&s.data).unwrap().iter().all(|(k, _)| *k != OBU_TEMPORAL_DELIMITER)); + } + let config = e.config_record().unwrap(); + assert_eq!(obus(&config[4..]).unwrap()[0].0, OBU_SEQUENCE_HEADER); + let mut m = VideoWriter::new(Cursor::new(Vec::new()), Container::Mp4, 64, 48, 24).unwrap(); + m.set_config(config); + for s in &out { + m.add(s).unwrap(); + } + let d = m.finish().unwrap().into_inner(); + assert_eq!(boxes(&d, 0..d.len()).len(), 3); + } +} diff --git a/crates/develop/src/settings.rs b/crates/develop/src/settings.rs index 1895776b3..a40f094c2 100644 --- a/crates/develop/src/settings.rs +++ b/crates/develop/src/settings.rs @@ -900,11 +900,24 @@ pub struct BrushStroke { pub density: f64, pub erase: bool, pub auto_mask: bool, + /// Pen pressure (0..1) at each of `points`, recorded when the brush's pressure switch is on: it scales each dab's + /// radius and flow. Empty (the default, a mouse or the switch off) is full pressure everywhere; a point past the + /// end of a shorter list takes the last value. + #[serde(skip_serializing_if = "Vec::is_empty")] + pub pressure: Vec, +} + +impl BrushStroke { + /// The pressure at point `i` (0..1; 1 without pressure data, a hostile value clamped, NaN as 1). + pub fn pressure_at(&self, i: usize) -> f32 { + let p = self.pressure.get(i).or(self.pressure.last()).copied().unwrap_or(1.0); + if p.is_nan() { 1.0 } else { p.clamp(0.0, 1.0) } + } } impl Default for BrushStroke { fn default() -> Self { - Self { points: Vec::new(), size: 0.03, feather: 50.0, flow: 100.0, density: 100.0, erase: false, auto_mask: false } + Self { points: Vec::new(), size: 0.03, feather: 50.0, flow: 100.0, density: 100.0, erase: false, auto_mask: false, pressure: Vec::new() } } } diff --git a/crates/engine/Cargo.toml b/crates/engine/Cargo.toml index 2229c42a9..8878f670f 100644 --- a/crates/engine/Cargo.toml +++ b/crates/engine/Cargo.toml @@ -29,6 +29,7 @@ lightcraft-faces = { workspace = true } miniz_oxide = { workspace = true } lightcraft-merge = { workspace = true } lightcraft-geocode = { workspace = true } +lightcraft-slideshow = { workspace = true } rayon = { workspace = true } serde = { workspace = true } serde_json = { workspace = true } diff --git a/crates/engine/build.rs b/crates/engine/build.rs index e097889ff..056c7adc8 100644 --- a/crates/engine/build.rs +++ b/crates/engine/build.rs @@ -3,7 +3,7 @@ //! Recipe from craft-fonts' docs/integration.md; craft-fonts is never a Cargo dependency, and this //! script only reads a local directory (no network). //! -//! wasm32 (the web build) embeds only the UI font, BIZ UDPGothic Regular (~4.5 MB), to keep the +//! wasm32 embeds BIZ UDPGothic Regular (~4.5 MB) and Noto Sans Thai (~214 KiB), to keep the //! `.wasm` within the web size budget (Cloudflare serves at most 25 MiB per file); the export //! watermark falls back to it there. Native builds embed every font. use std::fmt::Write as _; @@ -69,6 +69,7 @@ fn craft_fonts(dir: &Path, required: bool) -> Result { let mut out = String::new(); let mut has_japanese_ui = false; let mut has_chinese_ui = false; + let mut has_thai_ui = false; for line in text.lines().map(str::trim).filter(|l| !l.is_empty() && !l.starts_with('#')) { let f: Vec<&str> = line.split(" | ").map(str::trim).collect(); let [family, style, file, scripts, ..] = f.as_slice() else { @@ -76,7 +77,8 @@ fn craft_fonts(dir: &Path, required: bool) -> Result { }; has_japanese_ui |= (*family, *style) == ("BIZ UDPGothic", "Regular"); has_chinese_ui |= (*family, *style) == ("Noto Sans CJK SC", "Regular") && scripts.split(',').any(|script| script.trim() == "Hans"); - if wasm && (*family, *style) != ("BIZ UDPGothic", "Regular") { + has_thai_ui |= (*family, *style) == ("Noto Sans Thai", "Regular") && scripts.split(',').any(|script| script.trim() == "Thai"); + if wasm && !matches!((*family, *style), ("BIZ UDPGothic" | "Noto Sans Thai", "Regular")) { continue; } let path = dir.join(file).canonicalize().map_err(|e| format!("{file}: {e}"))?; @@ -89,6 +91,9 @@ fn craft_fonts(dir: &Path, required: bool) -> Result { path.display().to_string(), ); } + if required && !has_thai_ui { + return Err("required Thai UI face Noto Sans Thai Regular (Thai) is missing from fonts/manifest.txt".into()); + } if required && !has_japanese_ui { return Err("required Japanese UI face BIZ UDPGothic Regular is missing from fonts/manifest.txt".into()); } diff --git a/crates/engine/examples/raw_frame.rs b/crates/engine/examples/raw_frame.rs new file mode 100644 index 000000000..ce6c63e85 --- /dev/null +++ b/crates/engine/examples/raw_frame.rs @@ -0,0 +1,499 @@ +//! Measure where a camera's own JPEG sits inside its raw sensor data, and which edge bands of that data hold no +//! picture: `cargo xtask raw-frame [--json] FILE…` (or `cargo run --release -p lightcraft-engine --example raw_frame -- +//! [--json] FILE…`). It checks the crop any decoder applies against the camera's own framing, and measures the entries +//! of Nikon `MEASURED_BODIES` (`crates/raw/src/vendor/nef.rs`) for bodies that record +//! no crop. It works for any colour-filter raw that embeds a full-size camera JPEG (checked on Nikon, Sony, Canon CR2 / +//! CR3, Pentax and Samsung files, where it finds the decoder's crop); bodies with a smaller embedded preview (most +//! Fujifilm, Panasonic and Olympus files) get the edge bands only. +//! +//! **Frame.** For each raw it aligns the largest embedded JPEG with the sensor data: both are reduced to half-size +//! luminance (the raw by summing each 2×2 colour-filter block), and every offset that keeps the JPEG inside the data is +//! scored by the correlation of their gradient magnitudes. The best offset is refined to sub-pixel precision; a peak +//! that is weak, ambiguous or between two pixels is flagged. Files are grouped by model and data size (bit depths +//! pooled: both read out the same area); when 3 clean files agree, the group prints its table entries (siblings of the +//! same readout that agree on the same frame count towards the 3; see `MIN_FILES`). Use several +//! files with detail across the frame: a blank sky, fog or a lens cap has nothing to align on (low score). The JPEG +//! must be the camera's full-size one, unrotated and without in-camera geometry corrections (distortion control off); +//! a smaller preview is not aligned. +//! +//! **Edge bands.** From each edge inwards (at most 128 lines) it counts lines that hold no picture: *empty or dark* +//! (mean below 1% of the range over black) or *masked* (flat, patternless and above black: even and odd samples +//! alike within 4 standard errors or within 0.4% of white, since some filler holds a small fixed offset between the +//! two, as on the D90 and D5000; spread no more than the line's own neighbour noise and under 0.4% of white). A +//! band that the decoder's crop still includes is developed as picture: the coloured bars of issue #976. This is a +//! survey aid: a dark band can also be deep shadow, so read it with the frame. +use std::collections::BTreeMap; + +use lightcraft_codecs::DecodeOptions; +use lightcraft_raw::RawData; + +/// Smallest correlation peak worth trusting, and how far it must stand above the best offset outside its 3×3 +/// neighbourhood (measured: 0.75–0.88 on detailed D5100 / D7000 frames, runners-up ≥ 0.05 lower). +const MIN_SCORE: f64 = 0.5; +const MIN_MARGIN: f64 = 0.02; +/// Largest sub-pixel distance from a whole pixel accepted as a clean alignment. +const MAX_FRACTION: f64 = 0.35; +/// Search at most this many half-size offsets in each direction (384 px of margin around the JPEG: Canon bodies keep +/// up to ~280 px of masked border). +const MAX_SEARCH: usize = 192; +/// Windows of up to this many offsets are searched exhaustively (every Nikon body); larger ones coarse to fine. +const FULL_SEARCH: usize = 2048; +/// Edge bands are counted up to this many lines from each edge. +const MAX_BAND: usize = 128; +/// Clean, agreeing files an entry needs: of one model and data size (bit depths pooled: both read out the same area), +/// or, with at least one of its own, together with sibling models of the same readout (data and JPEG size) whose own +/// files agree on the same frame. +const MIN_FILES: usize = 3; + +struct Frame { + jpeg: (usize, usize), + /// Sub-pixel offset of the JPEG's top left corner in sensor pixels. + at: (f64, f64), + score: f64, + margin: f64, +} + +impl Frame { + fn whole(&self) -> (usize, usize) { + (self.at.0.round().max(0.0) as usize, self.at.1.round().max(0.0) as usize) + } + fn clean(&self) -> bool { + let frac = |v: f64| (v - v.round()).abs(); + self.score >= MIN_SCORE && self.margin >= MIN_MARGIN && frac(self.at.0) <= MAX_FRACTION && frac(self.at.1) <= MAX_FRACTION + } +} + +/// Lines without picture at one edge: empty or dark ones, then masked ones (or the other way round), from the edge. +#[derive(Clone, Copy, Default)] +struct Band { + dark: usize, + masked: usize, +} + +impl Band { + fn total(self) -> usize { + self.dark + self.masked + } +} + +struct Measured { + model: String, + size: (usize, usize), + bits: u32, + black: f64, + white: f64, + /// The crop the decoder applies, in sensor-data coordinates: x, y, width, height. + crop: (usize, usize, usize, usize), + /// Left, right, top, bottom. + bands: [Band; 4], + frame: Result, + /// With a clean frame: what the crop holds beyond it. + beyond: Option<[Beyond; 4]>, +} + +impl Measured { + /// Lines of each edge band (left, right, top, bottom) inside the decoder's crop: developed as picture. + fn in_crop(&self) -> [usize; 4] { + let (w, h) = self.size; + let (x, y, cw, ch) = self.crop; + let [l, r, t, b] = self.bands.map(Band::total); + [l.saturating_sub(x), (x + cw).saturating_sub(w - r.min(w)), t.saturating_sub(y), (y + ch).saturating_sub(h - b.min(h))] + } +} + +/// What the decoder's crop holds outside the camera's JPEG frame, per side (left, right, top, bottom): lines that are +/// masked (flat above black: a coloured bar), dark (empty or optical black: a dark edge) or picture (trimmed by the +/// camera, harmless). +#[derive(Clone, Copy, Default)] +struct Beyond { + masked: usize, + dark: usize, + picture: usize, +} + +fn main() { + let args: Vec = std::env::args().skip(1).collect(); + let json = args.iter().any(|a| a == "--json"); + let files: Vec<&String> = args.iter().filter(|a| *a != "--json").collect(); + if files.is_empty() { + eprintln!("usage: raw_frame [--json] FILE… (raw files whose largest embedded JPEG is the camera's full-size one)"); + std::process::exit(2); + } + // model, data size, JPEG size -> (bit depth, frame) of each file + let mut groups: BTreeMap<(String, (usize, usize), (usize, usize)), Vec<(u32, Frame)>> = BTreeMap::new(); + for f in files { + let m = match measure(f) { + Ok(m) => m, + Err(e) => { + if json { + println!("{}", serde_json::json!({ "file": f, "error": e })); + } else { + println!("{f}: skipped: {e}"); + } + continue; + } + }; + let in_crop = m.in_crop(); + if json { + let band = |b: Band| serde_json::json!({ "dark": b.dark, "masked": b.masked }); + let frame = match &m.frame { + Ok(fr) => serde_json::json!({ + "jpeg": [fr.jpeg.0, fr.jpeg.1], "at": [fr.at.0, fr.at.1], "score": fr.score, "margin": fr.margin, "clean": fr.clean() + }), + Err(e) => serde_json::json!({ "error": e }), + }; + println!( + "{}", + serde_json::json!({ + "file": f, "model": m.model, "size": [m.size.0, m.size.1], "bits": m.bits, "black": m.black, "white": m.white, + "crop": [m.crop.0, m.crop.1, m.crop.2, m.crop.3], + "bands": { "left": band(m.bands[0]), "right": band(m.bands[1]), "top": band(m.bands[2]), "bottom": band(m.bands[3]) }, + "in_crop": { "left": in_crop[0], "right": in_crop[1], "top": in_crop[2], "bottom": in_crop[3] }, + "beyond_frame": m.beyond.map(|b| { + let side = |b: Beyond| serde_json::json!({ "masked": b.masked, "dark": b.dark, "picture": b.picture }); + serde_json::json!({ "left": side(b[0]), "right": side(b[1]), "top": side(b[2]), "bottom": side(b[3]) }) + }), + "frame": frame, + }) + ); + } else { + let frame = match &m.frame { + Ok(fr) => format!( + "JPEG {}×{} at ({:.2}, {:.2}), score {:.3}, margin {:.3}{}", + fr.jpeg.0, + fr.jpeg.1, + fr.at.0, + fr.at.1, + fr.score, + fr.margin, + if fr.clean() { "" } else { " (not clean: weak, ambiguous or between pixels)" } + ), + Err(e) => format!("no frame: {e}"), + }; + println!("{f}: {} {}×{} {}-bit, {frame}", m.model, m.size.0, m.size.1, m.bits); + let side = |name: &str, b: Band| match (b.dark, b.masked) { + (0, 0) => None, + (d, 0) => Some(format!("{name} {d} dark")), + (0, k) => Some(format!("{name} {k} masked")), + (d, k) => Some(format!("{name} {d} dark + {k} masked")), + }; + let bands: Vec = ["left", "right", "top", "bottom"].iter().zip(m.bands).filter_map(|(n, b)| side(n, b)).collect(); + let shown: Vec = + ["left", "right", "top", "bottom"].iter().zip(in_crop).filter(|(_, n)| *n > 0).map(|(s, n)| format!("{s} {n}")).collect(); + println!( + " edge bands: {}; decoder crop ({}, {}, {}, {}){}", + if bands.is_empty() { "none".to_string() } else { bands.join(", ") }, + m.crop.0, + m.crop.1, + m.crop.2, + m.crop.3, + match m.beyond { + Some(b) => { + let parts: Vec = ["left", "right", "top", "bottom"] + .iter() + .zip(b) + .filter(|(_, b)| b.masked + b.dark + b.picture > 0) + .map(|(s, b)| format!("{s} {} masked / {} dark / {} picture", b.masked, b.dark, b.picture)) + .collect(); + if parts.is_empty() { + "; nothing beyond the camera frame".to_string() + } else { + format!("; beyond the camera frame: {}", parts.join(", ")) + } + } + None if shown.is_empty() => String::new(), + None => format!(" still includes edge bands: {} lines", shown.join(", ")), + } + ); + } + if let Ok(fr) = m.frame { + groups.entry((m.model.clone(), m.size, fr.jpeg)).or_default().push((m.bits, fr)); + } + } + if json { + return; + } + println!(); + // each group's agreed frame: (model, data size, JPEG size) -> (offset, clean files), when its clean files agree + let agreed: BTreeMap<&(String, (usize, usize), (usize, usize)), ((usize, usize), usize)> = groups + .iter() + .filter_map(|(key, frames)| { + let offsets: Vec<(usize, usize)> = frames.iter().filter(|(_, f)| f.clean()).map(|(_, f)| f.whole()).collect(); + let first = *offsets.first()?; + offsets.iter().all(|o| *o == first).then_some((key, (first, offsets.len()))) + }) + .collect(); + for (key, frames) in &groups { + let (model, size, jpeg) = key; + let clean = frames.iter().filter(|(_, f)| f.clean()).count(); + let mut depths: Vec = frames.iter().map(|(b, _)| *b).collect(); + depths.sort_unstable(); + depths.dedup(); + let label = format!("{model} {}×{}", size.0, size.1); + let Some(&((x, y), _)) = agreed.get(key) else { + if clean == 0 { + println!("{label}: no clean alignment in {} files; no entry", frames.len()); + } else { + let offsets: Vec<(usize, usize)> = frames.iter().filter(|(_, f)| f.clean()).map(|(_, f)| f.whole()).collect(); + println!("{label}: files disagree: {offsets:?}; no entry"); + } + continue; + }; + let note = if clean < frames.len() { format!(", {} not clean", frames.len() - clean) } else { String::new() }; + println!("{label}: {clean} of {} files agree on ({x}, {y}){note}", frames.len()); + // siblings: other models whose sensor readout matches (same data and JPEG size) and whose own files agree on + // the same frame + let siblings: Vec<(&str, usize)> = agreed + .iter() + .filter(|((m, s, j), (o, _))| m != model && s == size && j == jpeg && *o == (x, y)) + .map(|((m, _, _), (_, n))| (m.as_str(), *n)) + .collect(); + let pooled = clean + siblings.iter().map(|(_, n)| n).sum::(); + if clean < MIN_FILES { + if pooled < MIN_FILES { + println!(" pending: {} more clean file(s) needed for an entry", MIN_FILES - pooled); + continue; + } + let names: Vec = siblings.iter().map(|(m, n)| format!("{m} ({n})")).collect(); + println!(" with siblings of the same readout and frame: {}", names.join(", ")); + } + for bits in depths { + println!( + " MeasuredBody {{ model: {model:?}, size: ({}, {}), bits: {bits}, crop: ({x}, {y}, {}, {}), white: None }},", + size.0, size.1, jpeg.0, jpeg.1 + ); + } + } +} + +fn measure(path: &str) -> Result { + let bytes = std::fs::read(path).map_err(|e| e.to_string())?; + let raw = lightcraft_raw::decode(&bytes).map_err(|e| e.to_string())?; + let RawData::U16(d) = &raw.data else { return Err("not 16-bit integer sensor data".into()) }; + if raw.cpp != 1 || raw.cfa.is_none() { + return Err("not colour-filter sensor data".into()); + } + let (w, h) = (raw.width, raw.height); + if w < 4 || h < 4 || d.len() < w.saturating_mul(h) { + return Err("sensor data shorter than its size".into()); + } + let black = f64::from(raw.black.values.iter().sum::() / raw.black.values.len().max(1) as f32).max(0.0); + let white = f64::from(raw.white.first().copied().unwrap_or(((1u32 << raw.bits.min(16)) - 1) as f32)).max(black + 1.0); + let at = |x: usize, y: usize| f64::from(d.get(y * w + x).copied().unwrap_or(0)); + let bands = [ + band((0..w.min(MAX_BAND)).map(|x| line(h, |i| at(x, i))), black, white), + band((0..w.min(MAX_BAND)).map(|k| line(h, |i| at(w - 1 - k, i))), black, white), + band((0..h.min(MAX_BAND)).map(|y| line(w, |i| at(i, y))), black, white), + band((0..h.min(MAX_BAND)).map(|k| line(w, |i| at(i, h - 1 - k))), black, white), + ]; + // the decoder's crop is relative to the active area: in sensor-data coordinates here + let a = raw.active_area; + let c = lightcraft_raw::Rect::new(a.x + raw.crop.x, a.y + raw.crop.y, raw.crop.width, raw.crop.height); + let frame = frame(&bytes, d, w, h); + let beyond = frame.as_ref().ok().filter(|f| f.clean()).map(|f| { + let ((fx, fy), (fw, fh)) = (f.whole(), f.jpeg); + let kinds = |lines: Vec| { + lines.iter().fold(Beyond::default(), |mut b, l| { + match kind(l, black, white) { + Kind::Dark => b.dark += 1, + Kind::Masked => b.masked += 1, + Kind::Picture => b.picture += 1, + } + b + }) + }; + let col = |x: usize| line(h, |i| at(x, i)); + let row = |y: usize| line(w, |i| at(i, y)); + [ + kinds((c.x..fx.min(c.x + c.width)).map(col).collect()), + kinds(((fx + fw).max(c.x)..c.x + c.width).map(col).collect()), + kinds((c.y..fy.min(c.y + c.height)).map(row).collect()), + kinds(((fy + fh).max(c.y)..c.y + c.height).map(row).collect()), + ] + }); + Ok(Measured { + model: raw.metadata.model.clone().unwrap_or_default().trim().to_string(), + size: (w, h), + bits: raw.bits, + black, + white, + crop: (c.x, c.y, c.width, c.height), + bands, + frame, + beyond, + }) +} + +/// Statistics of one line of sensor samples: mean, |even − odd| mean, spread, neighbour noise (same colour, 2 apart) +/// and the number of sample pairs. +struct Line { + mean: f64, + pattern: f64, + spread: f64, + noise: f64, + pairs: f64, +} + +fn line(n: usize, at: impl Fn(usize) -> f64) -> Line { + let stride = ((n / 512).max(1)).div_ceil(2) * 2; + let (mut k, mut even, mut odd, mut var, mut noise) = (0f64, 0f64, 0f64, 0f64, 0f64); + let idx: Vec = (0..n.saturating_sub(2)).step_by(stride).collect(); + for &i in &idx { + even += at(i); + odd += at(i + 1); + k += 1.0; + } + let (even, odd) = (even / k.max(1.0), odd / k.max(1.0)); + for &i in &idx { + var += (at(i) - even).powi(2); + noise += (at(i + 2) - at(i)).powi(2) / 2.0; + } + Line { mean: (even + odd) / 2.0, pattern: (even - odd).abs(), spread: (var / k.max(1.0)).sqrt(), noise: (noise / k.max(1.0)).sqrt(), pairs: k } +} + +enum Kind { + Dark, + Masked, + Picture, +} + +fn kind(l: &Line, black: f64, white: f64) -> Kind { + let range = white - black; + let slack = white / 4096.0; + if l.mean < black + range * 0.01 { + Kind::Dark + } else if l.mean > black + range * 0.02 + && l.mean < white * 0.25 + // patternless within the noise, or with a small fixed offset between the CFA phases (D90 / D5000 filler) + && (l.pattern < 4.0 * l.noise * (2.0 / l.pairs.max(1.0)).sqrt() + slack || l.pattern < white * 0.004) + && l.spread < 1.5 * l.noise + slack + && l.spread < white * 0.004 + { + Kind::Masked + } else { + Kind::Picture + } +} + +fn band(lines: impl Iterator, black: f64, white: f64) -> Band { + let mut b = Band::default(); + for l in lines { + match kind(&l, black, white) { + Kind::Dark => b.dark += 1, + Kind::Masked => b.masked += 1, + Kind::Picture => break, + } + } + b +} + +fn frame(bytes: &[u8], d: &[u16], w: usize, h: usize) -> Result { + let jpeg = lightcraft_raw::embedded_preview(bytes).ok_or("no embedded preview")?; + let jpeg = lightcraft_codecs::decode(&jpeg, DecodeOptions::default()).map_err(|e| e.to_string())?; + let (jw, jh) = (jpeg.image.width, jpeg.image.height); + if jw > w || jh > h || jw * 10 < w * 9 || jh * 10 < h * 9 { + return Err(format!("the largest preview ({jw}×{jh}) is not a full-size JPEG of the {w}×{h} data")); + } + // half-size luminance, square-rooted so the linear raw and the JPEG weigh their tones alike + let (rw, rh) = (w / 2, h / 2); + let raw_y: Vec = (0..rh * rw) + .map(|i| { + let (x, y) = (2 * (i % rw), 2 * (i / rw)); + let s: f64 = + [(x, y), (x + 1, y), (x, y + 1), (x + 1, y + 1)].iter().map(|&(x, y)| f64::from(d.get(y * w + x).copied().unwrap_or(0))).sum(); + s.sqrt() + }) + .collect(); + let (pw, ph) = (jw / 2, jh / 2); + let px = &jpeg.image.data; + let jpg_y: Vec = (0..ph * pw) + .map(|i| { + let (x, y) = (2 * (i % pw), 2 * (i / pw)); + let s: f64 = [(x, y), (x + 1, y), (x, y + 1), (x + 1, y + 1)] + .iter() + .map(|&(x, y)| px.get(y * jw + x).map_or(0.0, |p| f64::from(0.2126 * p[0] + 0.7152 * p[1] + 0.0722 * p[2]).max(0.0))) + .sum(); + s.sqrt() + }) + .collect(); + let (sx, sy) = ((rw - pw).min(MAX_SEARCH), (rh - ph).min(MAX_SEARCH)); + let grad = |img: &[f64], stride: usize, x: usize, y: usize| { + let at = |x: usize, y: usize| img.get(y * stride + x).copied().unwrap_or(0.0); + (at(x + 1, y) - at(x, y)).hypot(at(x, y + 1) - at(x, y)) + }; + let normalise = |v: Vec| { + let n = v.len().max(1) as f64; + let mean = v.iter().sum::() / n; + let sd = (v.iter().map(|a| (a - mean).powi(2)).sum::() / n).sqrt().max(1e-12); + v.into_iter().map(|a| (a - mean) / sd).collect::>() + }; + // gradient magnitudes on every `step`-th half-size pixel, normalised, and the correlation at one offset + let sample = |step: usize| { + let points: Vec<(usize, usize)> = + (0..ph.saturating_sub(1)).step_by(step).flat_map(|y| (0..pw.saturating_sub(1)).step_by(step).map(move |x| (x, y))).collect(); + let gj = normalise(points.iter().map(|&(x, y)| grad(&jpg_y, pw, x, y)).collect()); + (points, gj) + }; + let score = |(points, gj): &(Vec<(usize, usize)>, Vec), dx: usize, dy: usize| { + let gr = normalise(points.iter().map(|&(x, y)| grad(&raw_y, rw, x + dx, y + dy)).collect()); + gr.iter().zip(gj).map(|(a, b)| a * b).sum::() / gj.len().max(1) as f64 + }; + let fine = sample(2); + // every offset when the window is small (every Nikon body); else every 4th on sparser points, then each offset + // around the best of those + let mut scores = vec![vec![f64::NEG_INFINITY; sx + 1]; sy + 1]; + let mut coarse_best = f64::NEG_INFINITY; + let (mut cx, mut cy) = (0, 0); + let window = if (sx + 1) * (sy + 1) <= FULL_SEARCH { + (0..=sx, 0..=sy) + } else { + let coarse = sample(8); + for dy in (0..=sy).step_by(4) { + for dx in (0..=sx).step_by(4) { + let v = score(&coarse, dx, dy); + if let Some(s) = scores.get_mut(dy).and_then(|r| r.get_mut(dx)) { + *s = v; + } + if v > coarse_best { + (coarse_best, cx, cy) = (v, dx, dy); + } + } + } + (cx.saturating_sub(5)..=(cx + 5).min(sx), cy.saturating_sub(5)..=(cy + 5).min(sy)) + }; + for dy in window.1.clone() { + for dx in window.0.clone() { + if let Some(s) = scores.get_mut(dy).and_then(|r| r.get_mut(dx)) { + *s = score(&fine, dx, dy); + } + } + } + let at = |dx: usize, dy: usize| scores.get(dy).and_then(|r| r.get(dx)).copied().unwrap_or(f64::NEG_INFINITY); + let (mut bx, mut by) = (0, 0); + for dy in 0..=sy { + for dx in 0..=sx { + if at(dx, dy) > at(bx, by) { + (bx, by) = (dx, dy); + } + } + } + let best = at(bx, by); + let runner_up = (0..=sy) + .flat_map(|dy| (0..=sx).map(move |dx| (dx, dy))) + .filter(|&(dx, dy)| dx.abs_diff(bx) > 1 || dy.abs_diff(by) > 1) + .map(|(dx, dy)| at(dx, dy)) + .fold(f64::NEG_INFINITY, f64::max); + // parabola through the peak and its neighbours (none at the window's edge) + let refine = |a: f64, b: f64, c: f64| { + let k = a - 2.0 * b + c; + if a.is_finite() && c.is_finite() && k < 0.0 { (0.5 * (a - c) / k).clamp(-0.5, 0.5) } else { 0.0 } + }; + let fx = if bx > 0 { refine(at(bx - 1, by), best, at(bx + 1, by)) } else { 0.0 }; + let fy = if by > 0 { refine(at(bx, by - 1), best, at(bx, by + 1)) } else { 0.0 }; + Ok(Frame { + jpeg: (jw, jh), + at: (2.0 * (bx as f64 + fx), 2.0 * (by as f64 + fy)), + score: best, + margin: if runner_up.is_finite() { best - runner_up } else { best }, + }) +} diff --git a/crates/engine/src/activity.rs b/crates/engine/src/activity.rs index 6fc618039..8c0a44f18 100644 --- a/crates/engine/src/activity.rs +++ b/crates/engine/src/activity.rs @@ -73,7 +73,7 @@ impl Unit { pub struct TaskInfo { pub id: u64, /// A stable id: `import`, `export`, `scan`, `previews`, `smartPreviews`, `lightroom`, `merge`, `download`, - /// `faces`, `denoise`, `findMissing`, `applePhotos`. + /// `faces`, `denoise`, `findMissing`, `applePhotos`, `cache`. pub kind: &'static str, /// English; the frontend translates it. pub label: String, diff --git a/crates/engine/src/camera_preview.rs b/crates/engine/src/camera_preview.rs index 260068d1c..f5e5d0250 100644 --- a/crates/engine/src/camera_preview.rs +++ b/crates/engine/src/camera_preview.rs @@ -30,9 +30,10 @@ use lightcraft_raw::{ /// states, for every make (#778). /// 4: a tone curve from black-and-white camera JPEGs (#577). /// 5: dark JPEG luminance pairs for the camera toe (#1307). -/// 6: the sensor proxy follows the file's lens warp when the camera JPEG is lens-corrected. -/// 7: the bundled NIKON Z 6_2 profile. -pub const LOOK_VERSION: u32 = 7; +/// 6: the ILCA-99M2 profile. +/// 7: the sensor proxy follows the file's lens warp when the camera JPEG is lens-corrected. +/// 8: the bundled NIKON Z 6_2 profile. +pub const LOOK_VERSION: u32 = 8; #[derive(Clone, Debug)] pub(crate) struct CameraLook { @@ -65,6 +66,7 @@ pub(crate) fn file_local_look(format: RawFormat) -> bool { | RawFormat::Pef | RawFormat::Srw | RawFormat::Orf + | RawFormat::Erf ) } @@ -123,9 +125,30 @@ pub(crate) fn starting_colour_tags(raw: &mut RawImage, bytes: &[u8]) -> Option Option) -> Option { +pub(crate) fn dng_colour(tags: &ColorData, declared: &crate::files::DngWhite, t: &CameraTransform, look: Option<&CameraLook>) -> Option { let m = look.map_or(t.matrix, |l| l.matrix.mul(&t.matrix)); // the DNG model renders the camera's white at unit brightness: what a look does to white's brightness goes // into the baseline exposure let white = m.apply([1.0; 3]); let k = (white[0] + white[1] + white[2]) / 3.0; - if !(k.is_finite() && k > 1e-6) || !t.wb.iter().all(|w| w.is_finite() && *w > 0.0) { + if !(k.is_finite() && k > 1e-6 && t.wb.iter().all(|w| w.is_finite() && *w > 0.0)) { return None; } let m = Mat3(m.0.map(|r| r.map(|v| v / k))); let forward = bradford(D65, D50).mul(&REC2020.to_xyz()).mul(&m); let wb = t.wb.map(f64::from); let neutral = wb.map(|w| wb.iter().cloned().fold(f64::MAX, f64::min) / w); - let camera_to_xyz = bradford(D50, t.white_xy).mul(&forward).mul(&Mat3::diag(1.0 / neutral[0], 1.0 / neutral[1], 1.0 / neutral[2])); + let (illuminant, color_matrix) = match declared { + crate::files::DngWhite::Matrices(c) if lightcraft_raw::color::has_matrix(c) => (c.illuminant, c.color_matrix), + crate::files::DngWhite::Matrices(_) => ([21, 0], [Some(declaring(t.white_xy, neutral)?), None]), + crate::files::DngWhite::At(xy) => ([21, 0], [Some(declaring(*xy, neutral)?), None]), + }; + if !color_matrix.iter().flatten().all(|m| m.0.iter().flatten().all(|v| v.is_finite())) { + return None; + } Some(ColorData { - illuminant: [21, 0], - color_matrix: [Some(camera_to_xyz.inverse()?), None], - forward_matrix: [Some(forward), None], + illuminant, + color_matrix, + // a second colour matrix comes with a second forward matrix (DNG spec): the same one, fitted at one white + forward_matrix: [Some(forward), color_matrix[1].map(|_| forward)], camera_calibration: [None, None], analog_balance: None, as_shot_neutral: Some(neutral), @@ -168,6 +204,17 @@ fn dng_colour(tags: &ColorData, t: &CameraTransform, look: Option<&CameraLook>) }) } +/// An XYZ → camera colour matrix under which camera `neutral` is white `xy`: the generic camera model, scaled per +/// channel. `None` when the model gives no positive neutral there. +fn declaring(xy: Xy, neutral: [f64; 3]) -> Option { + let g = lightcraft_raw::color::xyz_to_camera(&ColorData::default(), xy); + let n = g.apply(xy.to_xyz()); + if !n.iter().all(|v| v.is_finite() && *v > 1e-9) { + return None; + } + Some(Mat3::diag(neutral[0] / n[0], neutral[1] / n[1], neutral[2] / n[2]).mul(&g)) +} + /// A camera tone curve as a DNG `ProfileToneCurve` (linear in, linear out, 1 = white): sampled densely over the /// 12 stops below white, the range the curve is read back over (`files::dng_tone_curve`). fn dng_tone_curve(tone: &CameraTone) -> Option { @@ -2400,6 +2447,7 @@ mod tests { RawFormat::Pef, RawFormat::Srw, RawFormat::Orf, + RawFormat::Erf, ]; assert!(local.into_iter().all(file_local_look)); // DNG carries its own colour model @@ -2467,15 +2515,15 @@ mod tests { assert_eq!(info.camera_tone.is_some(), look.is_some()); } - /// A public ILCE-7CR sample (skipped without the corpus) takes its colour from the bundled ILCE-7CR profile and - /// passes the acceptance gates with it: the fitted look keeps the profile's matrix and table, which the photo's - /// own fit (the fallback when a profile is rejected) would not. - #[test] - fn corpus_a7cr_uses_the_bundled_profile() { + /// A public sample of `model` (skipped without the corpus) takes its colour from the bundled profile for `model` + /// and passes the acceptance gates with it: the fitted look keeps the profile's matrix and table, which the + /// photo's own fit (the fallback when a profile is rejected) would not. + fn corpus_uses_the_bundled_profile(file: &str, model: &str) { let path = std::env::var_os("LIGHTCRAFT_CORPUS") .map(std::path::PathBuf::from) .unwrap_or_else(|| std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../../corpus")) - .join("raw/arw-sony-a7cr-lossless-l.arw"); + .join("raw") + .join(file); let bytes = match std::fs::read(&path) { Ok(bytes) => bytes, Err(e) if e.kind() == std::io::ErrorKind::NotFound => { @@ -2484,12 +2532,12 @@ mod tests { } Err(e) => panic!("{}: {e}", path.display()), }; - let (_, json) = crate::camera_profiles::BUNDLED.iter().find(|(m, _)| *m == "ILCE-7CR").expect("ILCE-7CR is built in"); + let (_, json) = crate::camera_profiles::BUNDLED.iter().find(|(m, _)| *m == model).expect("the model is built in"); let bundled: crate::camera_profiles::CameraProfile = serde_json::from_str(json).unwrap(); - let used = crate::camera_profiles::get("ILCE-7CR").expect("ILCE-7CR profile"); - assert_eq!(*used, bundled, "a local ILCE-7CR profile overrides the built-in one; remove it to run this test"); + 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(); - assert_eq!(raw.metadata.model.as_deref(), Some("ILCE-7CR")); + assert_eq!(raw.metadata.model.as_deref(), Some(model)); 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 expected = bundled.matrix().mul(&transform.matrix.inverse().unwrap()); @@ -2534,6 +2582,16 @@ mod tests { assert_eq!(look.hue_sat, bundled.hue_sat, "the look's table is not the profile's"); } + #[test] + fn corpus_a7cr_uses_the_bundled_profile() { + corpus_uses_the_bundled_profile("arw-sony-a7cr-lossless-l.arw", "ILCE-7CR"); + } + + #[test] + fn corpus_a99m2_uses_the_bundled_profile() { + corpus_uses_the_bundled_profile("arw-sony-a99m2.arw", "ILCA-99M2"); + } + /// A public D7500 NEF (skipped without the corpus): its look is fitted to its own JPEG and white /// balance is relative to the as-shot look. #[test] @@ -2622,7 +2680,7 @@ mod tests { let samples = [[0.2, 0.3, 0.1], [0.05, 0.02, 0.4], [0.6, 0.6, 0.6], [0.01, 0.5, 0.03]]; let render = |m: &Mat3, wb: [f32; 3], ev: f64, c: [f64; 3]| m.apply(std::array::from_fn(|i| c[i] * wb[i] as f64)).map(|v| v * ev.exp2()); for look in [Some(&look), None] { - let dng = dng_colour(&ColorData::default(), &t, look).unwrap(); + let dng = dng_colour(&ColorData::default(), &crate::files::DngWhite::At(t.white_xy), &t, look).unwrap(); let back = camera_transform_of(&dng, as_shot_white_of(&dng, None)); for i in 0..3 { assert!((back.wb[i] - t.wb[i]).abs() < 1e-4 * t.wb[i], "{:?} vs {:?}", back.wb, t.wb); @@ -2647,6 +2705,52 @@ mod tests { } } + /// The merged DNG declares the white balance its source shows, also when the look's matrix doesn't keep white + /// neutral, as fitted looks often don't, and renders at As Shot as the source does. Real ARW merges declared their + /// colour matrix from the look: As Shot about 7000 K / +115 where the ARWs show 4800 K / +8. + #[test] + fn dng_colour_declares_the_white_balance_the_source_shows() { + use crate::files::DngWhite; + use lightcraft_raw::color::{as_shot_white_of, camera_transform_of, wb_multipliers}; + let tags = ColorData::default(); + let as_shot = [2.5039f32, 1.0, 1.5078]; + let t = camera_transform_of(&tags, as_shot_white_of(&tags, Some(as_shot))); + let mut look = dro_test_look(&std::array::from_fn(|i| 0.005 * 1.2f32.powi(i as i32)), &|x| 0.95 * x / (x + 0.1)); + look.matrix = Mat3([[1.75, -0.4, -0.2], [-0.2, 1.35, -0.2], [0.05, -0.3, 1.3]]); + let tint = look.matrix.apply([1.0; 3]); + assert!((tint[0] - tint[1]).abs() > 0.1, "a look that tints white: {tint:?}"); + let renders_as_shot = |dng: &ColorData| { + let r = camera_transform_of(dng, as_shot_white_of(dng, None)); + assert!(r.wb.iter().zip(t.wb).all(|(a, b)| (a - b).abs() < 1e-4 * b), "{:?} vs {:?}", r.wb, t.wb); + let m = look.matrix.mul(&t.matrix); + for c in [[0.2, 0.3, 0.1], [0.05, 0.02, 0.4], [0.6, 0.6, 0.6]] { + let want = m.apply(std::array::from_fn(|i| c[i] * t.wb[i] as f64)).map(|v| v * t.baseline_exposure.exp2()); + let got = r.matrix.apply(std::array::from_fn(|i| c[i] * r.wb[i] as f64)).map(|v| v * dng.baseline_exposure.exp2()); + assert!(want.iter().zip(got).all(|(a, b)| (a - b).abs() < 1e-4), "{c:?}: {want:?} vs {got:?}"); + } + }; + // a white the source's scale shows (Sony's presets, the relative scale's 6500 K / 0) + for xy in [lightcraft_color::cct::temp_tint_to_xy(5600.0, 3.0), lightcraft_color::cct::temp_tint_to_xy(6500.0, 0.0)] { + let dng = dng_colour(&tags, &DngWhite::At(xy), &t, Some(&look)).unwrap(); + let back = as_shot_white_of(&dng, None); + assert!((back.x - xy.x).abs() < 1e-6 && (back.y - xy.y).abs() < 1e-6, "as shot {back:?}, shown {xy:?}"); + renders_as_shot(&dng); + } + // a camera with measured matrices (the ILCE-7M3): every white read through them, as for the source + let camera = lightcraft_raw::spectral::find(Some("SONY"), "ILCE-7M3").expect("a covered camera"); + let mut model = tags.clone(); + assert!(camera.fill(&mut model)); + let shown = lightcraft_raw::spectral::as_shot_white_through(camera, &tags, Some(as_shot)).unwrap(); + let dng = dng_colour(&tags, &DngWhite::Matrices(Box::new(model.clone())), &t, Some(&look)).unwrap(); + let back = as_shot_white_of(&dng, None); + assert!((back.x - shown.x).abs() < 1e-5 && (back.y - shown.y).abs() < 1e-5, "as shot {back:?}, shown {shown:?}"); + for w in [Xy::new(0.3127, 0.329), Xy::new(0.4476, 0.4074), Xy::new(0.3457, 0.3585)] { + let (a, b) = (wb_multipliers(&model, w), wb_multipliers(&dng, w)); + assert!(a.iter().zip(b).all(|(a, b)| (a - b).abs() < 1e-6 * a), "{w:?}: {a:?} vs {b:?}"); + } + renders_as_shot(&dng); + } + /// A look with tone curve `f` at knots `xs` and a colour model and chroma curve to keep. fn dro_test_look(xs: &[f32; 32], f: &dyn Fn(f32) -> f32) -> CameraLook { CameraLook { diff --git a/crates/engine/src/camera_profiles.rs b/crates/engine/src/camera_profiles.rs index 41fc9ca2f..01239b6a1 100644 --- a/crates/engine/src/camera_profiles.rs +++ b/crates/engine/src/camera_profiles.rs @@ -25,6 +25,7 @@ const MAX_FILE: u64 = 4 << 20; /// Profiles built into LightCraft (`assets/camera-profiles/`, see `assets/ATTRIBUTION.md`): /// `(model, JSON)`. 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")), ("NIKON Z 6_2", include_str!("../../../assets/camera-profiles/NIKON_Z_6_2.json")), diff --git a/crates/engine/src/cmd/browse.rs b/crates/engine/src/cmd/browse.rs index a681ec14b..49f6e46e9 100644 --- a/crates/engine/src/cmd/browse.rs +++ b/crates/engine/src/cmd/browse.rs @@ -6,11 +6,14 @@ //! Each browse records when the folders it listed were browsed. Untouched records of folders //! not browsed for [`Session::forget_local_days`] are forgotten (when the library opens, or with //! `library.forgetLocal`); see [`lightcraft_catalog::local`] for what "untouched" means. Files -//! and sidecars on disk are never touched, and browsing the folder again brings them back. +//! and sidecars on disk are never touched, and browsing the folder again brings them back. A +//! browse also forgets at once the untouched records of the folder's files that are gone from +//! the disk (deleted or moved by another app), so the folder shows what is on disk. use std::path::Path; -use lightcraft_catalog::{Op, PhotoId}; +use lightcraft_catalog::query::in_folder; +use lightcraft_catalog::{Op, PhotoId, Source}; use serde_json::{Value, json}; use super::{CommandSpec, always, bad, bool_or, cmd, str_param}; @@ -42,11 +45,73 @@ fn browse(s: &mut Session, p: &Value) -> Result { // looking at a folder never brings its deleted photos back (importing the files does) let opts = ImportOptions { mode: ImportMode::Add, local: true, on_deleted: crate::import::OnDeleted::Skip, ..Default::default() }; let report = import_with(s, &files, &opts)?; + let forgotten = forget_gone(s, &dir_s, subfolders, &files); stamp_browsed(s, &dir_s, &files); s.browse = Some(Browse { path: dir_s.clone(), subfolders }); s.source = LibrarySource::Folder; + s.keep_active_in_view(); let shown = s.visible().len(); - Ok(json!({"path": dir_s, "subfolders": subfolders, "photos": shown, "new": report.imported.len(), "failed": report.failed.len()})) + Ok(json!({ + "path": dir_s, + "subfolders": subfolders, + "photos": shown, + "new": report.imported.len(), + "failed": report.failed.len(), + "forgotten": forgotten, + })) +} + +impl Session { + /// Forget the untouched Local records of files gone from the folder `dir` (with its subfolders when + /// `subfolders`) that a browse just listed as `files`: what `library.browse` does after reading a folder, for a + /// UI that reads it on a worker thread instead. Returns how many went. + pub fn forget_gone_from(&mut self, dir: &str, subfolders: bool, files: &[String]) -> usize { + forget_gone(self, dir, subfolders, files) + } +} + +/// Local records in the browsed folder whose files are no longer on disk (deleted or moved by +/// another app) and that nothing was changed on are forgotten, as `library.forgetLocal` forgets +/// them: the folder shows what is on disk, not photos that fail to load (and log an error) on +/// every visit (issue #1392). Records the user changed stay, shown as missing, and so do those +/// the session holds (selected, edited, in the undo history) or with a smart preview. Returns how +/// many went. +fn forget_gone(s: &mut Session, dir: &str, subfolders: bool, files: &[String]) -> usize { + let listed: std::collections::HashSet<&str> = files.iter().map(String::as_str).collect(); + let held = held(s); + let smart = s.media.smart_dir.clone(); + let gone: Vec = s + .catalog + .photos() + .filter(|p| p.local && !p.deleted && !held.contains(&p.id)) + // a smart preview may be all that is left of the picture + .filter(|p| !smart.as_ref().is_some_and(|d| crate::smart::file_path(d, p).exists())) + .filter(|p| match &p.source { + // a file left out of the listing is checked on disk: another spelling of its path, or + // a listing that missed it, keeps it + Source::File { path } => in_folder(path, dir, subfolders) && !listed.contains(path.as_str()) && !Path::new(path).exists(), + Source::Demo { .. } => false, + }) + .map(|p| p.id) + .collect(); + if gone.is_empty() { + return 0; + } + let mut forgotten = 0; + for chunk in s.catalog.forgettable_local(&gone).chunks(2000) { + let op = Op::Batch { ops: chunk.iter().map(|id| Op::RemovePhoto { id: *id }).collect() }; + match s.catalog.apply(op.clone()) { + Ok(_) => { + s.pending_log.push(op); + forgotten += chunk.len(); + } + Err(e) => log::warn!("forget Local records of files gone from {dir}: {e}"), + } + } + if forgotten > 0 { + log::info!("forgot {forgotten} untouched Local record(s) of files gone from {dir}"); + } + forgotten } /// Folders whose browse time is refreshed at most this often (keeps repeated browsing from @@ -82,7 +147,15 @@ fn stamp_browsed(s: &mut Session, root: &str, files: &[String]) { /// Photos the session still needs, whatever their state: shown, selected, being edited, or /// referred to by an undo / redo step. fn in_use(s: &mut Session) -> std::collections::HashSet { - let mut keep: std::collections::HashSet = s.visible().iter().copied().collect(); + let mut keep = held(s); + keep.extend(s.visible().iter().copied()); + keep +} + +/// Photos the session holds whatever is shown: selected, being edited, or referred to by an undo +/// / redo step. +fn held(s: &Session) -> std::collections::HashSet { + let mut keep: std::collections::HashSet = std::collections::HashSet::new(); keep.extend(s.selection.ids.iter().copied()); keep.extend(s.selection.active); keep.extend(s.previous_active); diff --git a/crates/engine/src/cmd/color.rs b/crates/engine/src/cmd/color.rs index 55ea8b5ab..111159e7a 100644 --- a/crates/engine/src/cmd/color.rs +++ b/crates/engine/src/cmd/color.rs @@ -156,7 +156,7 @@ fn targeted(s: &mut Session, p: &Value) -> Result { } fn index(p: &Value, c: &str) -> Result { - Ok(f64_req(p, "index", c)? as usize) + super::index_req(p, "index", c) } pub fn specs() -> Vec { diff --git a/crates/engine/src/cmd/convert.rs b/crates/engine/src/cmd/convert.rs index 4d7f13aeb..20ed66edd 100644 --- a/crates/engine/src/cmd/convert.rs +++ b/crates/engine/src/cmd/convert.rs @@ -35,7 +35,8 @@ pub(crate) fn write_dng_with(file_bytes: Option<&crate::merge::ByteReader>, path Some(r) => r(path)?, None => std::fs::read(path).map_err(|e| format!("{path}: {e}"))?, }; - let raw = lightcraft_raw::decode(&bytes).map_err(|e| format!("{path}: {e}"))?; + let mut raw = lightcraft_raw::decode(&bytes).map_err(|e| format!("{path}: {e}"))?; + crate::camera_preview::with_starting_colour(&mut raw, &bytes); drop(bytes); let dng = lightcraft_raw::write_dng(&raw, &lightcraft_raw::DngWriteOptions { xmp: (!packet.is_empty()).then_some(packet), ..Default::default() }) .map_err(|e| e.to_string())?; @@ -201,12 +202,16 @@ fn edit_copy(s: &mut Session, p: &Value, id: lightcraft_catalog::PhotoId, opts: /// The op that brings a photo up to date with what its file is now (`info`, a fresh probe), or /// `None` when nothing changed. Covers a raw that became decodable (or stopped being: a different -/// file relinked), i.e. a change of [`lightcraft_catalog::Photo::preview_only`]. +/// file relinked), i.e. a change of [`lightcraft_catalog::Photo::preview_only`], and a +/// preview-only photo this version hasn't checked yet: its stamp is refreshed so it isn't probed +/// again (see [`lightcraft_catalog::Photo::preview_checked`]). pub(crate) fn content_op(id: lightcraft_catalog::PhotoId, ph: &lightcraft_catalog::Photo, info: crate::media::ProbeInfo) -> Option { + let preview_checked = lightcraft_catalog::preview_stamp(&info.preview_only); let same = info.content_hash == ph.content_hash && info.file_size == ph.file_size && (info.width, info.height) == (ph.width, ph.height) - && info.preview_only == ph.preview_only; + && info.preview_only == ph.preview_only + && preview_checked == ph.preview_checked; (!same).then_some(Op::SetContent { id, width: info.width, @@ -214,6 +219,7 @@ pub(crate) fn content_op(id: lightcraft_catalog::PhotoId, ph: &lightcraft_catalo file_size: info.file_size, content_hash: info.content_hash, preview_only: info.preview_only, + preview_checked, }) } @@ -271,6 +277,37 @@ pub(crate) fn as_shot_wb_op(id: lightcraft_catalog::PhotoId, ph: &lightcraft_cat (ph.develops_raw() && !ph.relative_wb() && ph.as_shot_wb != Some(read)).then_some(Op::SetAsShotWb { id, wb: Some(read) }) } +/// The ops that bring photo `id` and its virtual copies up to date with what their file is now +/// (`info`, a fresh probe), appended to `ops`; the photos they change are appended to `changed`. +/// Reload and the preview-only recheck (`library.recheckPreviewOnly`) share it. +pub(crate) fn reload_ops( + cat: &lightcraft_catalog::Catalog, + id: lightcraft_catalog::PhotoId, + info: &crate::media::ProbeInfo, + ops: &mut Vec, + changed: &mut Vec, +) { + let Some(ph) = cat.photo(id) else { return }; + // camera fields the catalog lacks (e.g. a raw imported before its format was read) are filled in; + // nothing already set is overwritten + let meta_ops = fill_missing_meta(id, ph, info); + let mut mine: Vec = + content_op(id, ph, info.clone()).into_iter().chain(meta_ops).chain(lens_ops(id, ph, info)).chain(as_shot_wb_op(id, ph, info)).collect(); + if !mine.is_empty() { + ops.append(&mut mine); + changed.push(id); + } + // virtual copies share the file + for c in cat.photos().filter(|c| c.copy_of == Some(id)) { + let theirs: Vec = + content_op(c.id, c, info.clone()).into_iter().chain(lens_ops(c.id, c, info)).chain(as_shot_wb_op(c.id, c, info)).collect(); + if !theirs.is_empty() { + ops.extend(theirs); + changed.push(c.id); + } + } +} + /// Re-read photos whose files changed on disk (an external editor saved them): new size, /// dimensions and content hash, cached sources dropped. → {reloaded: [ids]} fn reload(s: &mut Session, p: &Value) -> Result { @@ -286,25 +323,8 @@ fn reload(s: &mut Session, p: &Value) -> Result { let mut ops = Vec::new(); let mut reloaded = Vec::new(); for ((id, _), info) in paths.into_iter().zip(probed) { - let (Ok(info), Some(ph)) = (info, s.catalog.photo(id)) else { continue }; - // camera fields the catalog lacks (e.g. a raw imported before its format was read) are filled in; - // nothing already set is overwritten - let meta_ops = fill_missing_meta(id, ph, &info); - let mut mine: Vec = - content_op(id, ph, info.clone()).into_iter().chain(meta_ops).chain(lens_ops(id, ph, &info)).chain(as_shot_wb_op(id, ph, &info)).collect(); - if !mine.is_empty() { - ops.append(&mut mine); - reloaded.push(id); - } - // virtual copies share the file - for c in s.catalog.photos().filter(|c| c.copy_of == Some(id)) { - let theirs: Vec = - content_op(c.id, c, info.clone()).into_iter().chain(lens_ops(c.id, c, &info)).chain(as_shot_wb_op(c.id, c, &info)).collect(); - if !theirs.is_empty() { - ops.extend(theirs); - reloaded.push(c.id); - } - } + let Ok(info) = info else { continue }; + reload_ops(&s.catalog, id, &info, &mut ops, &mut reloaded); } for id in &reloaded { s.media.forget(*id); diff --git a/crates/engine/src/cmd/curves.rs b/crates/engine/src/cmd/curves.rs index 77b061b78..7388f8e57 100644 --- a/crates/engine/src/cmd/curves.rs +++ b/crates/engine/src/cmd/curves.rs @@ -349,9 +349,8 @@ fn export_presets(s: &mut Session, p: &Value) -> Result { if chosen.is_empty() { return Err(bad(C, "no curve presets to export (save one first, or pass names)")); } - if let Some(dir) = path.parent().filter(|d| !d.as_os_str().is_empty()) { - std::fs::create_dir_all(dir).map_err(|e| crate::EngineError::Other(format!("{}: {e}", dir.display())))?; - } - std::fs::write(&path, to_file(&chosen)).map_err(|e| crate::EngineError::Other(format!("{}: {e}", path.display())))?; + let target = path.to_string_lossy(); + s.check_write_target(&target).map_err(|e| bad(C, e))?; + crate::export::write_file_durable(&target, to_file(&chosen).as_bytes()).map_err(crate::EngineError::Other)?; Ok(json!({"path": path.display().to_string(), "count": chosen.len()})) } diff --git a/crates/engine/src/cmd/develop.rs b/crates/engine/src/cmd/develop.rs index 6f064b260..37406fab4 100644 --- a/crates/engine/src/cmd/develop.rs +++ b/crates/engine/src/cmd/develop.rs @@ -5,7 +5,7 @@ use lightcraft_develop::{DevelopSettings, Preset, ProcessVersion, Section, Setti use lightcraft_geom::{CropGeometry, CropHandle, Point, Rect, crop_fit_angle, drag_crop}; use serde_json::{Value, json}; -use super::{CommandSpec, always, bad, bool_or, cmd, f64_or, f64_req, has_active, has_clipboard, has_selection, ok, str_param}; +use super::{CommandSpec, always, bad, bool_or, cmd, f64_or, f64_req, has_active, has_clipboard, has_selection, index_req, ok, str_param}; use crate::{Result, Session, media::SourceLevel}; fn active(s: &Session, c: &str) -> Result { @@ -144,7 +144,7 @@ fn user_preset<'a>(s: &'a mut Session, p: &Value, c: &str) -> Result<&'a mut Pre /// Change version `p.index` of the active photo (`f` gets it and the current settings). fn edit_version(s: &mut Session, p: &Value, c: &str, label: &str, f: impl FnOnce(&mut Version, std::sync::Arc)) -> Result { let id = active(s, c)?; - let i = f64_req(p, "index", c)? as usize; + let i = index_req(p, "index", c)?; let ph = s.catalog.photo(id).ok_or_else(|| bad(c, "no photo"))?; let mut versions = ph.versions.clone(); let v = versions.get_mut(i).ok_or_else(|| bad(c, "no such version"))?; @@ -588,14 +588,14 @@ pub fn specs() -> Vec { "Straighten", [], None, - "{angle: degrees -45..45} — keeps the largest crop of the current aspect", + "{angle: degrees -45..45} — keeps the crop if it still fits, else shrinks it about its centre (the largest crop of its aspect stays the largest)", has_active, |s, p| { let id = active(s, "crop.straighten")?; let (w, h) = crop_dims(s, id); let angle = f64_req(p, "angle", "crop.straighten")?.clamp(-45.0, 45.0); edit(s, "crop.straighten", "Straighten", |d| { - // keep the current crop if it still fits, else the largest of its aspect that does + // keep the current crop if it still fits, else shrink it about its centre until it does d.crop.geometry = d.crop.geometry.with_angle(angle, w, h); Ok(()) }) @@ -678,10 +678,11 @@ pub fn specs() -> Vec { let (w, h) = crop_dims(s, id); edit(s, "crop.rotateAspect", "Rotate Crop", |d| { let r = d.crop.geometry.rect_px(w, h); - let a = r.height() / r.width(); + // a degenerate crop has no aspect to rotate: fall back to the image's own + let a = Some(r.height() / r.width()).filter(|a| a.is_finite() && *a > 0.0); d.crop.aspect = d.crop.aspect.map(|(x, y)| (y, x)); let angle = d.crop.geometry.angle; - d.crop.geometry = crop_fit_angle(w, h, angle, Some(a)); + d.crop.geometry = crop_fit_angle(w, h, angle, a); Ok(()) }) }), @@ -1060,14 +1061,14 @@ pub fn specs() -> Vec { }), cmd!("version.restore", "Restore Version", [], None, "{index}", has_active, |s, p| { let id = active(s, "version.restore")?; - let i = f64_req(p, "index", "version.restore")? as usize; + let i = index_req(p, "index", "version.restore")?; let v = s.catalog.photo(id).and_then(|ph| ph.versions.get(i).cloned()).ok_or_else(|| bad("version.restore", "no such version"))?; s.set_develop(id, (*v.settings).clone(), &format!("Restore {}", v.name))?; ok() }), cmd!("version.delete", "Delete Version", [], None, "{index}", has_active, |s, p| { let id = active(s, "version.delete")?; - let i = f64_req(p, "index", "version.delete")? as usize; + let i = index_req(p, "index", "version.delete")?; let mut versions = s.catalog.photo(id).map(|ph| ph.versions.clone()).unwrap_or_default(); if i >= versions.len() { return Err(bad("version.delete", "no such version")); diff --git a/crates/engine/src/cmd/face_recognize.rs b/crates/engine/src/cmd/face_recognize.rs index e099f13ec..7e9e459cf 100644 --- a/crates/engine/src/cmd/face_recognize.rs +++ b/crates/engine/src/cmd/face_recognize.rs @@ -1,10 +1,11 @@ //! Recognition commands: `faces.index` (embed the faces in the library), `faces.suggest` (who might an unnamed //! face be?), `faces.person` (one person's faces, and the unnamed faces that look like them), `faces.unnamed` (every //! unnamed face, look-alikes together) and `faces.setName` / `faces.nameFaces` (name one face, or many at once, which is -//! what teaches the suggestions). +//! what teaches the suggestions). `faces.renamePerson` renames every in-library face of one person, or merges two +//! people when asked. //! //! Everything here is a suggestion for the user to confirm; nothing is ever named automatically. All of it is -//! catalog-only (no sidecar is written) and `faces.setName` is undoable. +//! catalog-only (no sidecar is written) and naming is undoable. use lightcraft_catalog::Op; use serde_json::{Value, json}; @@ -92,6 +93,92 @@ fn set_name(s: &mut Session, p: &Value) -> Result { Ok(json!({"id": id.0, "index": index, "name": name})) } +/// `faces.renamePerson {from, to, merge?}`: give every in-library face of `from` the exact name `to`, as one undo +/// step. Names match the People view (trimmed, case-insensitive). A `to` that is already someone else is refused +/// unless `merge` is set, and then the two people become one. The sidecar is not touched, and a refusal changes nothing. +fn rename_person(s: &mut Session, p: &Value) -> Result { + const C: &str = "faces.renamePerson"; + let from = match p.get("from") { + Some(Value::String(v)) => { + let t = v.trim(); + if t.is_empty() { + return Err(bad(C, "missing `from`")); + } + t.to_string() + } + Some(_) => return Err(bad(C, "`from` must be text")), + None => return Err(bad(C, "missing `from`")), + }; + let to = match p.get("to") { + Some(Value::String(v)) => { + let t = v.trim(); + if t.is_empty() { + return Err(bad(C, "`to` must be a name")); + } + if t.chars().count() > 200 || t.chars().any(char::is_control) { + return Err(bad(C, "a name is at most 200 characters, without control characters")); + } + t.to_string() + } + Some(_) => return Err(bad(C, "`to` must be text")), + None => return Err(bad(C, "missing `to`")), + }; + let merge = super::bool_or(p, "merge", false); + let from_key = from.to_lowercase(); + let to_key = to.to_lowercase(); + let mut matched = 0usize; + let mut other = false; + let mut faces = 0usize; + let mut ops = Vec::new(); + let ids: Vec<_> = s.catalog.photos().filter(|ph| ph.in_library()).map(|ph| ph.id).collect(); + for id in ids { + let Some(ph) = s.catalog.photo(id) else { continue }; + let mut indexes = Vec::new(); + for (i, r) in ph.meta.regions.iter().enumerate() { + if r.kind != lightcraft_meta::RegionKind::Face { + continue; + } + let Some(n) = r.name.as_deref().map(str::trim).filter(|n| !n.is_empty()) else { continue }; + let key = n.to_lowercase(); + if key == from_key { + matched += 1; + if r.name.as_deref() != Some(to.as_str()) { + indexes.push(i); + } + } else if key == to_key { + other = true; + } + } + if indexes.is_empty() { + continue; + } + let mut meta = ph.meta.clone(); + for i in indexes { + if let Some(region) = meta.regions.get_mut(i) { + region.name = Some(to.clone()); + faces += 1; + } + } + ops.push(Op::SetMeta { id, meta: Box::new(meta) }); + } + if matched == 0 { + return Err(bad(C, format!("no person named {from}"))); + } + if faces == 0 { + return Err(bad(C, format!("{to} is already their name"))); + } + if other && !merge { + return Err(bad(C, format!("{to} is already another person; pass merge: true to combine them"))); + } + let photos = ops.len(); + s.commit(if other { "Merge People" } else { "Rename Person" }, Op::Batch { ops })?; + s.skip_auto_write = true; + if s.filter.person.as_deref().is_some_and(|n| n.trim().to_lowercase() == from_key) { + s.filter.person = Some(to.clone()); + } + Ok(json!({"faces": faces, "photos": photos, "from": from, "to": to, "merged": other})) +} + /// Most faces `faces.unnamed` lists. const MAX_UNNAMED: usize = 3000; @@ -275,7 +362,7 @@ mod imp { } let job = s.preview_job(id, EDGE, EDGE, false, &DevelopSettings::default())?; let preview = s.scan_preview_of(&photo); - Some(Prepared { tag, epoch: s.faces.epoch, id, job, preview, todo, detect, detector }) + Some(Prepared { tag, epoch: s.faces.epoch, id, content: crate::media::content_key(&photo), job, preview, todo, detect, detector }) } /// The faces the detector found in a photo that had none become its face regions: unnamed, marked as detected (so a @@ -299,13 +386,16 @@ mod imp { } } - /// Take a finished photo into the index (ignored when it was made for another model or another library). Returns - /// (embedded, failed). + /// Take a finished photo into the index (ignored when it was made for another model or another library, or from + /// a file the photo no longer has: issue #1524). Returns (embedded, failed). fn ingest(s: &mut Session, d: Done) -> (usize, usize) { s.faces.in_flight.remove(&d.id); if d.epoch != s.faces.epoch || d.tag != s.faces.index.tag { return (0, 0); } + if s.catalog.photo(d.id).is_none_or(|p| crate::media::content_key(p) != d.content) { + return (0, 0); + } if d.detect_wanted { match &d.found { Some(found) => { @@ -830,7 +920,8 @@ mod imp { } fn finished(s: &Session, id: PhotoId, found: Option>, results: Vec<(Rect, Option>, Rect)>) -> Done { - Done { tag: s.faces.index.tag.clone(), epoch: s.faces.epoch, id, results, detect_wanted: true, found } + let content = s.catalog.photo(id).map(|p| crate::media::content_key(p)).unwrap_or_default(); + Done { tag: s.faces.index.tag.clone(), epoch: s.faces.epoch, id, content, results, detect_wanted: true, found } } #[test] @@ -915,6 +1006,42 @@ mod imp { let _ = std::fs::remove_dir_all(&d); } + /// A Reload that finds the file replaced (an outside edit) gives the photo other pixels: what a worker made from + /// the old file is dropped when it comes back, the faces already embedded from it are forgotten and the photo is + /// searched again (issue #1524). + #[test] + fn faces_made_from_a_replaced_file_are_dropped() { + let (mut s, d) = loaded("replaced"); + let (a, b) = two_photos(&s); + let rect = region(0.2, None).rect; + s.faces.index.insert(a.0, &rect, unit(&[(0, 1.0)])); + s.faces.scanned.insert(a.0); + s.faces.index.insert(b.0, &rect, unit(&[(1, 1.0)])); + s.faces.scanned.insert(b.0); + let old = finished(&s, a, Some(vec![rect]), vec![(rect, Some(unit(&[(2, 1.0)])), rect)]); + let (width, height) = s.catalog.photo(a).map(|p| (p.width, p.height)).unwrap(); + let replace = Op::SetContent { + id: a, + width, + height, + file_size: 1234, + content_hash: Some("edited".into()), + preview_only: None, + preview_checked: String::new(), + }; + s.commit("Reload Changed Files", replace).unwrap(); + assert!(s.faces.index.get(a.0, &rect).is_none() && !s.faces.scanned.contains(a.0), "the old file's faces are forgotten"); + assert!(needs_search(&s, s.catalog.photo(a).unwrap()), "and the photo is searched again"); + assert!(s.faces.index.get(b.0, &rect).is_some() && s.faces.scanned.contains(b.0), "other photos keep theirs"); + assert_eq!(ingest(&mut s, old), (0, 0), "a result made from the old file is dropped"); + assert!(s.catalog.photo(a).unwrap().meta.regions.is_empty() && !s.faces.scanned.contains(a.0)); + // what is made from the new file is taken in + let new = finished(&s, a, Some(vec![rect]), vec![(rect, Some(unit(&[(3, 1.0)])), rect)]); + assert_eq!(ingest(&mut s, new), (1, 0)); + assert!(s.faces.scanned.contains(a.0) && s.faces.index.get(a.0, &rect).is_some()); + let _ = std::fs::remove_dir_all(&d); + } + #[test] fn the_unnamed_faces_that_look_like_a_person_come_best_first() { let (mut s, d) = loaded("similar"); @@ -1047,5 +1174,14 @@ pub fn specs() -> Vec { super::always, set_name ), + cmd!( + "faces.renamePerson", + "Rename Person", + [], + None, + "{from, to, merge?: bool} → {faces, photos, from, to, merged} — give every in-library face of `from` the name `to` (trimmed, any capitalization), one undo step; refused when `to` is already someone else unless `merge` is true, which combines the two; the sidecar is not touched", + super::always, + rename_person + ), ] } diff --git a/crates/engine/src/cmd/library.rs b/crates/engine/src/cmd/library.rs index 3a5575ea8..cc3e654c7 100644 --- a/crates/engine/src/cmd/library.rs +++ b/crates/engine/src/cmd/library.rs @@ -167,6 +167,19 @@ fn album_targets_or_err(s: &Session, command: &str, targets: Vec) -> Re Ok((kept, skipped)) } +/// Move to Recently Deleted and drop the photos from stacks so leftover members do not keep +/// showing `i/n` (issue #1493). +fn mark_deleted(s: &mut Session, ids: &[PhotoId], label: &str) -> Result { + let mut ops: Vec = ids.iter().map(|id| Op::SetDeleted { id: *id, deleted: true }).collect(); + let n = ops.len(); + if n == 0 { + return Ok(0); + } + ops.extend(s.catalog.remove_from_stacks_ops(ids)); + s.commit(label, Op::Batch { ops })?; + Ok(n) +} + /// Apply one op per target as a single undo step. fn for_targets(s: &mut Session, p: &Value, label: &str, f: impl Fn(PhotoId) -> Option) -> Result { let targets = s.targets(p); @@ -196,6 +209,25 @@ fn change_rating(s: &mut Session, p: &Value, delta: i8, label: &str) -> Result pick -> reject -> unflagged, +/// raising/lowering clamps on reject < unflagged < pick. Unchanged photos make no history step. +fn change_flag(s: &mut Session, p: &Value, label: &str, step: impl Fn(Flag) -> Flag) -> Result { + let mut ops = Vec::new(); + for id in s.targets(p) { + let old = s.catalog.photo(id).ok_or(lightcraft_catalog::CatalogError::NoPhoto(id))?.flag; + let flag = step(old); + if flag != old { + ops.push(Op::SetFlag { id, flag }); + } + } + let n = ops.len(); + if n > 0 { + s.commit(label, Op::Batch { ops })?; + } + advance_if(s, p); + Ok(json!({"changed": n})) +} + fn advance_if(s: &mut Session, p: &Value) { if bool_or(p, "advance", false) { let _ = step(s, 1); @@ -232,6 +264,10 @@ fn rotate(s: &mut Session, p: &Value, cw: bool) -> Result { if let Some(d) = s.develop_of(id) { let mut d = (*d).clone(); d.orientation = d.orientation.rotated(cw); + // the crop lives in the oriented frame, so it turns with the image (else it keeps the old width and height and cuts the photo) + let turn = lightcraft_geom::Orientation::Normal.rotated(cw); + d.crop.geometry.rect = turn.map_norm_rect(d.crop.geometry.rect); + d.crop.aspect = d.crop.aspect.map(|(w, h)| (h, w)); ops.extend(s.develop_op(id, d, "Rotate")); } } @@ -362,11 +398,8 @@ pub fn specs() -> Vec { } other => return Err(bad("library.source", format!("unknown source `{other}`"))), }; - let vis = s.visible_cloned(); - if s.selection.active.is_none_or(|a| !vis.contains(&a)) { - s.selection = vis.first().map(|f| Selection::single(*f)).unwrap_or_default(); - } - Ok(json!({"count": vis.len()})) + s.keep_active_in_view(); + Ok(json!({"count": s.visible().len()})) } ), cmd!( @@ -436,8 +469,7 @@ pub fn specs() -> Vec { if covers_other_disks(s, path, &ids) && !bool_or(p, "disk", false) { return Err(bad(C, format!("{path} holds whole disks: pass `disk: true` to remove everything on them"))); } - let ops = ids.iter().map(|id| Op::SetDeleted { id: *id, deleted: true }).collect(); - s.commit("Remove Folder from Library", Op::Batch { ops })?; + mark_deleted(s, &ids, "Remove Folder from Library")?; if s.filter.library_folder.as_deref().is_some_and(|c| lightcraft_catalog::query::folder_within(c, path)) { s.filter.library_folder = None; } @@ -510,6 +542,44 @@ pub fn specs() -> Vec { ok() } ), + cmd!( + "library.refresh", + "Refresh View", + ["View"], + None, + "{} — show what matches now: a view that holds its photos (`library.holdView`) lets go of the ones that stopped matching a smart album's rules, Picks or the filter bar since it was opened, sorted or last refreshed", + always, + |s, _| { + s.refresh_view(); + ok() + } + ), + cmd!( + "library.holdView", + "Hold View Until Refreshed", + ["View"], + None, + "{on?: bool (toggles when omitted)} → {hold} — when on, an edit that makes a photo stop matching the view (a keyword given in a smart album of photos without keywords, a flag taken off in Picks, a rating under the filter bar's) keeps it in the view until `library.refresh`, Reshuffle, or another source, filter or sort; photos that start matching still appear at once, and deleting a photo or taking it out of the album or folder shown still takes it out of the view. Changing a smart album's rules is another view too. Off (the default): the view follows every edit. Kept with the library's preferences; asking for the state it already has changes nothing", + always, + |s, p| { + let on = match p.get("on") { + None | Some(Value::Null) => !s.hold_view, + Some(Value::Bool(on)) => *on, + Some(_) => return Err(bad("library.holdView", "on must be true or false")), + }; + if on != s.hold_view { + let was = s.hold_view; + s.hold_view = on; + if let Err(e) = s.save_prefs() { + // not kept: not changed either, so asking again does what was asked + s.hold_view = was; + return Err(e); + } + s.refresh_view(); + } + Ok(json!({"hold": s.hold_view})) + } + ), cmd!( "library.shuffle", "Reshuffle", @@ -687,6 +757,40 @@ pub fn specs() -> Vec { advance_if(s, p); Ok(v) }), + // 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", + 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, + }) + } + ), + cmd!( + "photo.increaseFlag", + "Increase Flag Status", + ["Photo", "Set Flag"], + Some("Cmd+Up"), + "{ids?, advance?: bool} — reject -> unflagged -> pick (stops at pick)", + has_selection, + |s, p| { 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)", + has_selection, + |s, p| { 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?}", has_selection, |s, p| { for_targets(s, p, "Flag as Pick", |id| Some(Op::SetFlag { id, flag: Flag::Pick })) }), @@ -779,9 +883,10 @@ pub fn specs() -> Vec { // ---- delete / restore cmd!("photo.delete", "Delete Photo", ["Photo"], Some("Delete"), "{ids?} — moves to Recently Deleted", has_selection, |s, p| { let before = s.visible_cloned(); - let v = for_targets(s, p, "Delete", |id| Some(Op::SetDeleted { id, deleted: true }))?; + let targets = s.targets(p); + let n = mark_deleted(s, &targets, "Delete")?; select_after_deletion(s, &before); - Ok(v) + Ok(json!({"changed": n})) }), cmd!("photo.restore", "Restore", ["Photo"], None, "{ids?}", has_selection, |s, p| for_targets(s, p, "Restore", |id| Some(Op::SetDeleted { id, @@ -1334,17 +1439,38 @@ pub fn specs() -> Vec { Ok(result) } ), - cmd!("album.removePhotos", "Remove from Album", [], None, "{id: albumId, ids?}", has_selection, |s, p| { - let id = album_param(p, "id", "album.removePhotos")?; - let targets = ids_param(p).unwrap_or_else(|| s.targets(&Value::Null)); - let al = s.catalog.album(id).ok_or_else(|| bad("album.removePhotos", "no such album"))?; - if al.is_smart() { - return Err(bad("album.removePhotos", "smart albums update automatically: change their rules")); + cmd!( + "album.removePhotos", + "Remove from Album", + ["Photo"], + None, + "{id?: albumId (default: the album on screen), ids?} — takes the photos out of the album only: they stay in the library and in their folders (issue #1805; photo.delete removes them from the library) → {removed, album: name}", + has_selection, + |s, p| { + let id = match p.get("id") { + Some(_) => album_param(p, "id", "album.removePhotos")?, + None => match s.source { + LibrarySource::Album(a) => a, + _ => return Err(bad("album.removePhotos", "no album: pass `id` or show an album")), + }, + }; + let targets = ids_param(p).unwrap_or_else(|| s.targets(&Value::Null)); + let al = s.catalog.album(id).ok_or_else(|| bad("album.removePhotos", "no such album"))?; + if al.is_smart() { + return Err(bad("album.removePhotos", "smart albums update automatically: change their rules")); + } + if al.folder { + return Err(bad("album.removePhotos", "album folders don't hold photos")); + } + let name = al.name.clone(); + let photos: Vec = al.photos.iter().copied().filter(|x| !targets.contains(x)).collect(); + let removed = al.photos.len().saturating_sub(photos.len()); + if removed > 0 { + s.commit("Remove from Album", Op::SetAlbumPhotos { id, photos })?; + } + Ok(json!({"removed": removed, "album": name})) } - let photos: Vec = al.photos.iter().copied().filter(|x| !targets.contains(x)).collect(); - s.commit("Remove from Album", Op::SetAlbumPhotos { id, photos })?; - ok() - }), + ), cmd!("album.setCover", "Set as Album Cover", [], None, "{id: albumId, photo?: photoId}", has_active, |s, p| { let id = album_param(p, "id", "album.setCover")?; let photo = p.get("photo").and_then(Value::as_u64).map(PhotoId).or(s.active()); @@ -1623,3 +1749,7 @@ mod gps_tests { #[cfg(test)] #[path = "library/tests_relative_ratings.rs"] mod relative_rating_tests; + +#[cfg(test)] +#[path = "library/tests_flag_cycle.rs"] +mod flag_cycle_tests; diff --git a/crates/engine/src/cmd/library/tests_flag_cycle.rs b/crates/engine/src/cmd/library/tests_flag_cycle.rs new file mode 100644 index 000000000..6a476dc1d --- /dev/null +++ b/crates/engine/src/cmd/library/tests_flag_cycle.rs @@ -0,0 +1,100 @@ +//! `photo.cycleFlag` / `photo.increaseFlag` / `photo.decreaseFlag` through the real engine. +use lightcraft_catalog::{Flag, Op, Photo, PhotoId, Source}; +use serde_json::json; + +use crate::{Result, Selection, Session}; + +fn session() -> Result { + let mut s = Session::new(); + for (id, flag) in [(1, Flag::None), (2, Flag::Pick), (3, Flag::Reject), (4, Flag::None)] { + let mut photo = Photo::new( + PhotoId(id), + Source::File { path: format!("/lightcraft-flag-cycle/{id}.jpg") }, + &format!("{id}.jpg"), + "JPEG", + 40, + 30, + "2026-10-13", + ); + photo.flag = flag; + s.catalog.apply(Op::AddPhoto { photo: Box::new(photo) })?; + } + s.execute("library.sort", &json!({"key": "fileName", "ascending": true}))?; + s.execute("library.select", &json!({"ids": [1, 2, 3], "active": 2}))?; + Ok(s) +} + +fn flags(s: &Session) -> Vec { + (1..=4).map(|id| s.catalog.photo(PhotoId(id)).unwrap().flag).collect() +} + +#[test] +fn cycle_steps_unflagged_pick_reject_and_wraps() { + let mut s = session().unwrap(); + s.execute("library.select", &json!({"ids": [1]})).unwrap(); + let seen: Vec = (0..4) + .map(|_| { + s.execute("photo.cycleFlag", &json!({})).unwrap(); + flags(&s)[0] + }) + .collect(); + assert_eq!(seen, [Flag::Pick, Flag::Reject, Flag::None, Flag::Pick]); +} + +#[test] +fn cycle_uses_each_photos_own_flag_in_one_undo_step() { + let mut s = session().unwrap(); + let undo = s.undo.len(); + assert_eq!(s.execute("photo.cycleFlag", &json!({})).unwrap()["changed"], 3); + assert_eq!(flags(&s), [Flag::Pick, Flag::Reject, Flag::None, Flag::None]); + assert_eq!(s.undo.len(), undo + 1); + assert_eq!(s.active(), Some(PhotoId(2))); + s.execute("edit.undo", &json!({})).unwrap(); + assert_eq!(flags(&s), [Flag::None, Flag::Pick, Flag::Reject, Flag::None]); + s.execute("edit.redo", &json!({})).unwrap(); + assert_eq!(flags(&s), [Flag::Pick, Flag::Reject, Flag::None, Flag::None]); +} + +#[test] +fn explicit_ids_active_fallback_advance_and_errors() { + let mut s = session().unwrap(); + s.execute("photo.cycleFlag", &json!({"ids": [4]})).unwrap(); + assert_eq!(flags(&s), [Flag::None, Flag::Pick, Flag::Reject, Flag::Pick]); + s.selection = Selection { ids: vec![], active: Some(PhotoId(2)) }; + s.execute("photo.cycleFlag", &json!({})).unwrap(); + assert_eq!(flags(&s)[1], Flag::Reject); + s.execute("photo.cycleFlag", &json!({"advance": true})).unwrap(); + assert_eq!(flags(&s)[1], Flag::None); + assert_eq!(s.active(), Some(PhotoId(3))); + let undo = s.undo.len(); + assert!(s.execute("photo.cycleFlag", &json!({"ids": [1, 999]})).is_err()); + assert_eq!(flags(&s)[0], Flag::None); + assert_eq!(s.undo.len(), undo); + s.selection = Selection::default(); + assert!(s.execute("photo.cycleFlag", &json!({})).is_err()); + assert_eq!(s.undo.len(), undo); +} + +#[test] +fn increase_and_decrease_clamp_without_wrapping() { + let mut s = session().unwrap(); + assert_eq!(s.execute("photo.increaseFlag", &json!({})).unwrap()["changed"], 2); + assert_eq!(flags(&s), [Flag::Pick, Flag::Pick, Flag::None, Flag::None]); + assert_eq!(s.execute("photo.increaseFlag", &json!({"ids": [2]})).unwrap()["changed"], 0); + s.execute("photo.decreaseFlag", &json!({})).unwrap(); + assert_eq!(flags(&s), [Flag::None, Flag::None, Flag::Reject, Flag::None]); + s.execute("photo.decreaseFlag", &json!({})).unwrap(); + assert_eq!(flags(&s), [Flag::Reject, Flag::Reject, Flag::Reject, Flag::None]); + let undo = s.undo.len(); + assert_eq!(s.execute("photo.decreaseFlag", &json!({})).unwrap()["changed"], 0); + assert_eq!(s.undo.len(), undo, "no change, no history step"); +} + +#[test] +fn registered_with_classic_shortcuts() { + let specs = crate::command_specs(); + let key = |id: &str| specs.iter().find(|c| c.id == id).and_then(|c| c.shortcut); + assert_eq!(key("photo.cycleFlag"), Some("`")); + assert_eq!(key("photo.increaseFlag"), Some("Cmd+Up")); + assert_eq!(key("photo.decreaseFlag"), Some("Cmd+Down")); +} diff --git a/crates/engine/src/cmd/mask_effect_files.rs b/crates/engine/src/cmd/mask_effect_files.rs new file mode 100644 index 000000000..e1e099bc8 --- /dev/null +++ b/crates/engine/src/cmd/mask_effect_files.rs @@ -0,0 +1,681 @@ +//! Mask effect preset files: Lightroom Classic local adjustment presets (`.lrtemplate`, Lua, +//! `type = "LocalizedAdjustmentPreset"`), Camera Raw local correction presets (`.xmp` with +//! `crs:Local*` fields, from its `LocalCorrections` folder), our `.lceffect` JSON, and `.zip` +//! bundles of these. Folders, and Lightroom users' "divider" presets (`10 ----- DETAIL ----- 10`, +//! empty presets that head a numbered section of the menu), become groups. +//! +//! Read from the files' public structure and black-box observation; settings we have no slider +//! for are reported and kept with the effect (an export carries them). + +use lightcraft_develop::LocalAdjustments; +use serde_json::{Map, Value, json}; + +use super::mask_effects::{MaskEffect, upsert}; +use super::{bad, str_param}; +use crate::preset_import::{Lua, group_from_dir, parse_lua, read_zip}; +use crate::{Result, Session}; + +/// File extension of exported effect presets (JSON). +pub const EFFECT_EXT: &str = "lceffect"; +/// `format` tag of an exported effect-preset file. +const FILE_FORMAT: &str = "lightcraft.maskEffects"; +/// The Lua `type` of a Lightroom local adjustment preset. +const LR_LOCAL_TYPE: &str = "LocalizedAdjustmentPreset"; +/// Larger files aren't presets (or bundles of them). +const MAX_FILE: u64 = 64 << 20; +/// How deep nested Lua tables are kept as JSON. +const MAX_DEPTH: usize = 16; + +/// An effect read from a file, with the settings it couldn't keep and where it came from. +#[derive(Clone, Debug)] +pub struct Found { + pub effect: MaskEffect, + pub unmapped: Vec, + pub file: String, + /// Effects of one folder (or folder inside a bundle) share this key: divider presets group + /// the effects next to them. + pub dir: String, + /// The group the folder gives (when the file names none). + pub dir_group: Option, + /// The file named its group (`.lceffect`, `crs:Group`): kept. + pub has_group: bool, +} + +/// A readable name for a Lightroom local setting we can't carry over. +fn setting_label(key: &str) -> String { + let k = key.trim_start_matches("Local"); + let lower = k.to_ascii_lowercase(); + match lower.as_str() { + "grain" => "Grain".into(), + "toningluminance" => "Color luminance".into(), + "pointcolors" => "Point Color".into(), + "refinesaturation" => "Refine Saturation".into(), + "curverefinesaturation" => "Refine Saturation".into(), + "maincurve" | "redcurve" | "greencurve" | "bluecurve" => "Curve".into(), + _ => k.to_string(), + } +} + +/// A Lua value as JSON (tables nested at most [`MAX_DEPTH`] deep). +fn lua_json(v: &Lua, depth: usize) -> Value { + match v { + Lua::Nil => Value::Null, + Lua::Bool(b) => json!(b), + Lua::Num(n) => serde_json::Number::from_f64(*n).map(Value::Number).unwrap_or(Value::Null), + Lua::Str(s) => json!(s), + Lua::Table(..) if depth >= MAX_DEPTH => Value::Null, + Lua::Table(items, fields) if fields.is_empty() => Value::Array(items.iter().map(|x| lua_json(x, depth + 1)).collect()), + Lua::Table(_, fields) => Value::Object(fields.iter().map(|(k, x)| (k.clone(), lua_json(x, depth + 1))).collect()), + } +} + +/// Does a setting value do anything (a non-zero number, a non-empty table, …)? +fn meaningful(v: &Value) -> bool { + match v { + Value::Null => false, + Value::Bool(b) => *b, + Value::Number(n) => n.as_f64().is_some_and(|x| x.abs() > 1e-9), + Value::String(s) => !s.is_empty(), + Value::Array(a) => !a.is_empty(), + Value::Object(o) => !o.is_empty(), + } +} + +/// Is `text` a Lightroom local adjustment preset (`.lrtemplate`)? +pub fn is_lr_local_template(text: &str) -> bool { + parse_lua(text.trim_start_matches('\u{feff}')) + .ok() + .and_then(|root| root.get("type").and_then(Lua::str).map(|t| t == LR_LOCAL_TYPE)) + .unwrap_or(false) +} + +/// `$$$/Path/Key=Default` (a localisable string) → `Default`. +fn delocalize(t: &str) -> &str { + t.strip_prefix("$$$/").and_then(|r| r.split_once('=')).map_or(t, |(_, d)| d) +} + +/// A Lightroom local adjustment preset → an effect named after its title (else `stem`), and the +/// settings we have no equivalent for (e.g. `Grain`, `Point Color`), which the effect keeps in +/// `extra`. +pub fn parse_lr_local_template(text: &str, stem: &str) -> std::result::Result<(MaskEffect, Vec), String> { + let root = parse_lua(text.trim_start_matches('\u{feff}'))?; + if root.get("type").and_then(Lua::str) != Some(LR_LOCAL_TYPE) { + return Err("not a local adjustment preset".into()); + } + let Some(Lua::Table(_, fields)) = root.get("value") else { return Err("no adjustments in this local preset".into()) }; + // `exposure2012` → `LocalExposure2012`: the template's keys are the XMP `Local*` names, lower camel case + let crs = |k: &str| { + let mut c = k.chars(); + c.next().map(|f| format!("Local{}{}", f.to_ascii_uppercase(), c.as_str())).unwrap_or_default() + }; + let num = |k: &str| fields.iter().find(|(f, _)| crs(f) == k).and_then(|(_, v)| if let Lua::Num(n) = v { Some(*n) } else { None }); + let values = crate::crs_masks::local_values(num); + let mut extra = Map::new(); + for (k, v) in fields { + if crate::crs_masks::is_local_field(&crs(k)) { + continue; + } + let j = lua_json(v, 0); + if meaningful(&j) { + extra.insert(k.clone(), j); + } + } + let title = root.get("title").or_else(|| root.get("internalName")).and_then(Lua::str).map(str::trim).filter(|t| !t.is_empty()); + let name = title.map(|t| delocalize(t).trim().to_string()).filter(|t| !t.is_empty()).unwrap_or_else(|| stem.to_string()); + build(name, values, extra) +} + +fn build(name: String, values: Map, extra: Map) -> std::result::Result<(MaskEffect, Vec), String> { + let adjust: LocalAdjustments = serde_json::from_value(Value::Object(values)).map_err(|e| format!("unreadable local preset: {e}"))?; + let mut unmapped: Vec = extra.keys().map(|k| setting_label(k)).collect(); + unmapped.sort(); + unmapped.dedup(); + let effect = MaskEffect { name, adjust, extra, ..Default::default() }.normalized(); + Ok((effect, unmapped)) +} + +/// The local slider fields of a Camera Raw local correction preset: its top-level `crs:Local*` +/// properties, or (a preset typed `Local` without them) its first correction's. +fn xmp_local_fields(d: &lightcraft_meta::XmpData) -> Option> { + let mut out = Map::new(); + for (k, v) in &d.properties { + if let Some(name) = k.strip_prefix("crs:").filter(|n| n.starts_with("Local") && !n.contains('/')) + && let Some(x) = v.first() + { + out.insert(name.to_string(), json!(x.trim())); + } + } + if !out.is_empty() { + return Some(out); + } + let typed_local = d.properties.get("crs:PresetType").and_then(|v| v.first()).is_some_and(|t| t.trim().eq_ignore_ascii_case("local")); + if !typed_local { + return None; + } + let correction = crate::crs_masks::CONTAINERS.iter().find_map(|c| d.values.get(*c).and_then(|l| l.items().first()))?; + let lightcraft_meta::XmpValue::Struct(m) = correction else { return None }; + for (k, v) in m { + if let Some(name) = k.strip_prefix("crs:").filter(|n| n.starts_with("Local")) + && let Some(x) = v.text() + { + out.insert(name.to_string(), json!(x)); + } + } + Some(out) +} + +/// Is `text` a Camera Raw local correction preset (XMP)? +pub fn is_local_xmp(text: &str) -> bool { + lightcraft_meta::parse_xmp(text.trim_start_matches('\u{feff}')).ok().and_then(|d| xmp_local_fields(&d)).is_some() +} + +/// A Camera Raw local correction preset (XMP) → an effect named after `crs:Name` (else `stem`), +/// in `crs:Group` if it names one. +pub fn parse_local_xmp(text: &str, stem: &str) -> std::result::Result<(MaskEffect, Vec), String> { + let d = lightcraft_meta::parse_xmp(text.trim_start_matches('\u{feff}')).map_err(|e| e.to_string())?; + let fields = xmp_local_fields(&d).ok_or("not a local correction preset (no crs:Local… settings)")?; + let num = |v: &Value| v.as_str().map(|s| s.strip_prefix('+').unwrap_or(s)).and_then(|s| s.parse::().ok()).filter(|x| x.is_finite()); + let values = crate::crs_masks::local_values(|k| fields.get(k).and_then(num)); + let mut extra = Map::new(); + for (k, v) in &fields { + if crate::crs_masks::is_local_field(k) { + continue; + } + let v = num(v).map_or_else(|| v.clone(), |x| json!(x)); + if meaningful(&v) { + extra.insert(k.clone(), v); + } + } + let first = |k: &str| d.properties.get(k).and_then(|v| v.first()).map(|s| s.trim().to_string()).filter(|s| !s.is_empty()); + let name = first("crs:Name").unwrap_or_else(|| stem.to_string()); + let (mut e, unmapped) = build(name, values, extra)?; + e.group = first("crs:Group").unwrap_or_default(); + Ok((e, unmapped)) +} + +/// Effects in an `.lceffect` file: `{format, version, effects: [...]}`, a bare array, or one effect. +pub fn parse_file(bytes: &[u8]) -> std::result::Result, String> { + let v: Value = serde_json::from_slice(bytes).map_err(|e| format!("not an effect preset file: {e}"))?; + let items = match v { + Value::Object(mut o) if o.contains_key("effects") => o.remove("effects").unwrap_or_default(), + v @ Value::Array(_) => v, + v @ Value::Object(_) => Value::Array(vec![v]), + _ => return Err("not an effect preset file".into()), + }; + let list: Vec = serde_json::from_value(items).map_err(|e| format!("not an effect preset file: {e}"))?; + let list: Vec = list.into_iter().filter(|e| !e.name.trim().is_empty()).map(MaskEffect::normalized).collect(); + if list.is_empty() { + return Err("no effect presets in the file".into()); + } + Ok(list) +} + +fn ext_of(name: &str) -> String { + std::path::Path::new(name).extension().map(|e| e.to_string_lossy().to_ascii_lowercase()).unwrap_or_default() +} + +fn stem_of(name: &str) -> String { + std::path::Path::new(name).file_stem().map(|s| s.to_string_lossy().to_string()).filter(|s| !s.is_empty()).unwrap_or_else(|| "Effect".into()) +} + +type Parsed = std::result::Result)>, String>; + +/// The effects in file `name` (`bytes`) if it is a local preset (`.lrtemplate` of the local type, +/// a local correction `.xmp`, an `.lceffect`); `None` for any other file (a develop preset…). +pub fn parse_bytes(name: &str, bytes: &[u8]) -> Option { + let stem = stem_of(name); + match ext_of(name).as_str() { + "lrtemplate" => { + let text = String::from_utf8_lossy(bytes); + is_lr_local_template(&text).then(|| parse_lr_local_template(&text, &stem).map(|e| vec![e])) + } + "xmp" => { + let text = String::from_utf8_lossy(bytes); + is_local_xmp(&text).then(|| parse_local_xmp(&text, &stem).map(|e| vec![e])) + } + EFFECT_EXT => Some(parse_file(bytes).map(|v| v.into_iter().map(|e| (e, Vec::new())).collect())), + _ => None, + } +} + +/// The group effects in folder `dir` (relative to the imported folder) go to. +pub fn group_for_dir(dir: &str) -> Option { + effect_group_from_dir(dir) +} + +/// Folder names that only say "local presets" (`Local Adjustment Presets`, `LocalCorrections`…) +/// give no group. +fn effect_group_from_dir(dir: &str) -> Option { + const FILLER: &[&str] = &[ + "local", + "adjustment", + "adjustments", + "localcorrections", + "corrections", + "correction", + "brush", + "brushes", + "mask", + "masks", + "effect", + "effects", + ]; + let generic = |part: &str| { + let lower = part.to_ascii_lowercase(); + let words: Vec<&str> = lower.split(|c: char| !c.is_alphanumeric()).filter(|w| !w.is_empty()).collect(); + words.iter().any(|w| FILLER.contains(w)) + && words.iter().all(|w| FILLER.contains(w) || matches!(*w, "presets" | "preset" | "lightroom" | "camera" | "raw" | "classic")) + }; + let kept: Vec<&str> = dir.split(['/', '\\']).filter(|p| !p.is_empty() && !generic(p)).collect(); + group_from_dir(&kept.join("/")) +} + +/// Effects found in `bytes` of file `file` (a `.zip` is opened: the local presets inside). +/// `dir_group`: the group the file's folder gives. Errors are `(file, message)`. +pub fn found_in(file: &str, bytes: &[u8], dir_group: Option, out: &mut Vec, failed: &mut Vec<(String, String)>) -> bool { + let dir = std::path::Path::new(file).parent().map(|d| d.to_string_lossy().to_string()).unwrap_or_default(); + if ext_of(file) == "zip" { + let Ok(entries) = read_zip(bytes) else { return false }; + let zip_group = effect_group_from_dir(&stem_of(file)).or(dir_group); + let mut any = false; + for (inner, data) in entries { + let inner_dir = inner.rsplit_once('/').map(|(d, _)| d.to_string()).unwrap_or_default(); + let group = effect_group_from_dir(&inner_dir).or_else(|| zip_group.clone()); + let label = format!("{file}/{inner}"); + match parse_bytes(&inner, &data) { + Some(Ok(list)) => { + any = true; + push(out, list, &label, format!("{file}/{inner_dir}"), group, &inner); + } + Some(Err(e)) => { + any = true; + failed.push((label, e)); + } + None => {} + } + } + return any; + } + match parse_bytes(file, bytes) { + Some(Ok(list)) => { + push(out, list, file, dir, dir_group, file); + true + } + Some(Err(e)) => { + failed.push((file.to_string(), e)); + true + } + None => false, + } +} + +fn push(out: &mut Vec, list: Vec<(MaskEffect, Vec)>, file: &str, dir: String, dir_group: Option, name: &str) { + let named = ext_of(name) == EFFECT_EXT; + for (effect, unmapped) in list { + let has_group = named || !effect.group.is_empty(); + out.push(Found { effect, unmapped, file: file.to_string(), dir: dir.clone(), dir_group: dir_group.clone(), has_group }); + } +} + +/// The number a preset name starts with (`24a-Eye` → 24). +fn leading_number(name: &str) -> Option { + let digits: String = name.trim().chars().take_while(char::is_ascii_digit).take(18).collect(); + digits.parse().ok() +} + +/// The section title of a divider preset — an effect that sets nothing, named like +/// `10 ----- DETAIL ENHANCERS ----- 10` (a run of at least three `-`, `=`, `_`, `*`, `#`, `~`, `—`). +pub fn divider_title(e: &MaskEffect) -> Option { + if !e.is_empty() { + return None; + } + let marks = ['-', '=', '_', '*', '#', '~', '—', '–', '•', '.']; + let chars: Vec = e.name.chars().collect(); + let has_run = chars.windows(3).any(|w| marks.contains(&w[0]) && w[0] == w[1] && w[1] == w[2]); + if !has_run { + return None; + } + let title: String = e.name.chars().map(|c| if marks.contains(&c) { ' ' } else { c }).collect(); + let title = title.trim().trim_start_matches(|c: char| c.is_ascii_digit() || c.is_whitespace()); + let title = title.trim_end_matches(|c: char| c.is_ascii_digit() || c.is_whitespace()); + let title = title.split_whitespace().collect::>().join(" "); + if !title.chars().any(char::is_alphanumeric) { + return None; + } + // "DETAIL ENHANCERS" → "Detail Enhancers" + let shouting = title.chars().any(char::is_alphabetic) && !title.chars().any(char::is_lowercase); + Some(if shouting { + title + .split(' ') + .map(|w| { + let mut c = w.chars(); + c.next().map(|f| f.to_uppercase().chain(c.flat_map(char::to_lowercase)).collect::()).unwrap_or_default() + }) + .collect::>() + .join(" ") + } else { + title + }) +} + +/// Give `found` their groups; returns the divider presets (which become groups, not effects) as +/// `(found, group)`. A numbered effect goes to the divider with the largest number not above its +/// own (`12 Sky` under `10 --- DETAIL --- 10`); others to their folder's group. +pub fn assign_groups(found: Vec, group: Option<&str>) -> (Vec, Vec<(Found, String)>) { + let (dividers, mut effects): (Vec<(Found, Option)>, Vec<(Found, Option)>) = + found.into_iter().map(|f| (divider_title(&f.effect).filter(|_| !f.has_group), f)).map(|(t, f)| (f, t)).partition(|(_, t)| t.is_some()); + let dividers: Vec<(Found, String)> = dividers.into_iter().filter_map(|(f, t)| t.map(|t| (f, t))).collect(); + for (f, _) in &mut effects { + if let Some(g) = group { + f.effect.group = g.to_string(); + continue; + } + if f.has_group { + continue; + } + let section = leading_number(&f.effect.name).and_then(|n| { + dividers + .iter() + .filter(|(d, _)| d.dir == f.dir) + .filter_map(|(d, t)| leading_number(&d.effect.name).filter(|m| *m <= n).map(|m| (m, t))) + .max_by_key(|(m, _)| *m) + .map(|(_, t)| t.clone()) + }); + f.effect.group = section.or_else(|| f.dir_group.clone()).unwrap_or_default(); + } + (effects.into_iter().map(|(f, _)| f).collect(), dividers) +} + +/// Add the found effects (grouped; same-named ones replaced; effects left from an earlier import +/// of a divider dropped) and save → the report rows `(imported, dividers)`. +pub fn add_found(s: &mut Session, found: Vec, group: Option<&str>, dry: bool) -> Result<(Vec, Vec)> { + let (effects, dividers) = assign_groups(found, group); + let rows: Vec = + effects.iter().map(|f| json!({"name": f.effect.name, "group": f.effect.group, "file": f.file, "unmapped": f.unmapped})).collect(); + let div_rows: Vec = dividers.iter().map(|(f, g)| json!({"name": f.effect.name, "group": g, "file": f.file})).collect(); + if dry || (effects.is_empty() && dividers.is_empty()) { + return Ok((rows, div_rows)); + } + s.mask_effects.retain(|e| !(e.is_empty() && dividers.iter().any(|(d, _)| d.effect.name.eq_ignore_ascii_case(&e.name)))); + for f in effects { + upsert(s, f.effect); + } + s.save_prefs()?; + Ok((rows, div_rows)) +} + +/// The files under `paths` an effect import reads (folders walked, at most 8 levels), each with +/// the group its folder gives and whether it was named (not found in a folder). +fn effect_files(paths: &[String]) -> Vec<(String, Option, bool)> { + fn walk(dir: &std::path::Path, base: &std::path::Path, depth: usize, out: &mut Vec<(String, Option, bool)>) { + let Ok(rd) = std::fs::read_dir(dir) else { return }; + let mut entries: Vec<_> = rd.flatten().map(|e| e.path()).collect(); + entries.sort(); + for p in entries { + if p.is_dir() { + if depth < 8 { + walk(&p, base, depth + 1, out); + } + } else if p.extension().is_some_and(|e| ["lrtemplate", EFFECT_EXT, "xmp", "zip"].iter().any(|x| e.eq_ignore_ascii_case(x))) { + let rel = p.parent().and_then(|d| d.strip_prefix(base).ok()).map(|d| d.to_string_lossy().to_string()); + out.push((p.to_string_lossy().to_string(), rel.and_then(|d| effect_group_from_dir(&d)), false)); + } + } + } + let mut out = Vec::new(); + for x in paths { + let p = std::path::Path::new(x); + if p.is_dir() { + walk(p, p.parent().unwrap_or(std::path::Path::new("")), 0, &mut out); + } else { + out.push((x.clone(), None, true)); + } + } + out +} + +/// Read a file of at most [`MAX_FILE`] bytes. +pub fn read_capped(path: &str) -> std::result::Result, String> { + let len = std::fs::metadata(path).map_err(|e| e.to_string())?.len(); + if len > MAX_FILE { + return Err(format!("too large for a preset file ({} MB)", len >> 20)); + } + std::fs::read(path).map_err(|e| e.to_string()) +} + +pub fn import_effects(s: &mut Session, p: &Value) -> Result { + const C: &str = "mask.importEffects"; + let mut paths: Vec = + p.get("paths").and_then(Value::as_array).map(|a| a.iter().filter_map(Value::as_str).map(str::to_string).collect()).unwrap_or_default(); + if let Some(x) = str_param(p, "path") { + paths.push(x.to_string()); + } + if paths.is_empty() { + return Err(bad(C, "missing `path` / `paths`")); + } + let group = str_param(p, "group").map(str::trim).filter(|g| !g.is_empty()); + let dry = p.get("dryRun").and_then(Value::as_bool).unwrap_or(false); + let (mut found, mut failed) = (Vec::new(), Vec::new()); + for (f, dir_group, named) in effect_files(&paths) { + match read_capped(&f) { + Ok(bytes) => { + if !found_in(&f, &bytes, dir_group, &mut found, &mut failed) && named { + failed.push((f, "not a local adjustment preset".into())); + } + } + Err(e) => failed.push((f, e)), + } + } + let (imported, dividers) = add_found(s, found, group, dry)?; + Ok(json!({"imported": imported, "dividers": dividers, "failed": failed.into_iter().map(|(f, e)| json!([f, e])).collect::>()})) +} + +pub fn export_effects(s: &mut Session, p: &Value) -> Result { + const C: &str = "mask.exportEffects"; + let path = str_param(p, "path").filter(|x| !x.trim().is_empty()).ok_or_else(|| bad(C, "missing `path`"))?; + let mut path = std::path::PathBuf::from(path); + if path.extension().is_none() { + path.set_extension(EFFECT_EXT); + } + let names: Option> = + p.get("names").and_then(Value::as_array).map(|a| a.iter().filter_map(Value::as_str).map(str::to_string).collect()); + let group = str_param(p, "group").map(str::trim); + let chosen: Vec<&MaskEffect> = s + .mask_effects + .iter() + .filter(|e| names.as_ref().is_none_or(|n| n.iter().any(|x| x.eq_ignore_ascii_case(&e.name)))) + .filter(|e| group.is_none_or(|g| g.eq_ignore_ascii_case(&e.group))) + .collect(); + if chosen.is_empty() { + return Err(bad(C, "no effect presets to export")); + } + if let Some(dir) = path.parent().filter(|d| !d.as_os_str().is_empty()) { + std::fs::create_dir_all(dir).map_err(|e| crate::EngineError::Other(format!("{}: {e}", dir.display())))?; + } + let text = serde_json::to_string_pretty(&json!({"format": FILE_FORMAT, "version": 2, "effects": chosen})) + .map_err(|e| crate::EngineError::Other(e.to_string()))?; + std::fs::write(&path, text).map_err(|e| crate::EngineError::Other(format!("{}: {e}", path.display())))?; + Ok(json!({"path": path.display().to_string(), "count": chosen.len()})) +} + +#[cfg(test)] +mod tests { + use super::*; + + // A synthetic template in the shape Lightroom writes (our own values). + const TEMPLATE: &str = r#"s = { + id = "00000000-0000-0000-0000-000000000001", + internalName = "Soft Glow", + title = "Soft Glow", + type = "LocalizedAdjustmentPreset", + value = { + clarity = 0.5, + clarity2012 = -0.25, + exposure = 0, + exposure2012 = 0.125, + grain = 0.25, + highlights2012 = -0.5, + luminanceNoise = 0.1, + refineSaturation = 0, + temperature = 0.2, + texture = -0.3, + toningHue = 240, + toningLuminance = 0.1, + toningSaturation = 0, + pointColors = { + { + HueRange = { LowerFull = 0.3, LowerNone = 0, UpperFull = 0.6, UpperNone = 1, }, + HueShift = -0.05, + RangeAmount = 0.5, + SrcHue = 0.8, + }, + }, + }, + version = 0, +}"#; + + #[test] + fn reads_a_lightroom_local_template() { + assert!(is_lr_local_template(TEMPLATE)); + let (e, unmapped) = parse_lr_local_template(TEMPLATE, "file").unwrap(); + assert_eq!(e.name, "Soft Glow"); + assert!((e.adjust.exposure - 0.5).abs() < 1e-9, "exposure2012 is a quarter of the stops"); + assert!((e.adjust.clarity + 25.0).abs() < 1e-9, "the 2012 field wins over the old one"); + assert!((e.adjust.highlights + 50.0).abs() < 1e-9); + assert!((e.adjust.temp - 20.0).abs() < 1e-9); + assert!((e.adjust.texture + 30.0).abs() < 1e-9); + assert!((e.adjust.noise - 10.0).abs() < 1e-9); + assert!((e.adjust.amount - 100.0).abs() < 1e-9); + assert_eq!(unmapped, vec!["Color luminance".to_string(), "Grain".into(), "Point Color".into()]); + assert_eq!(e.extra.get("grain"), Some(&json!(0.25)), "kept for export"); + assert_eq!(e.extra["pointColors"][0]["HueRange"]["UpperFull"], json!(0.6)); + assert!(!e.extra.contains_key("refineSaturation"), "zero settings aren't kept"); + } + + #[test] + fn develop_templates_are_not_local_ones() { + let develop = r#"s = { title = "X", type = "Develop", value = { settings = { Exposure2012 = 1 } } }"#; + assert!(!is_lr_local_template(develop)); + assert!(parse_lr_local_template(develop, "x").is_err()); + assert!(parse_bytes("x.lrtemplate", develop.as_bytes()).is_none(), "left to the develop preset import"); + } + + #[test] + fn malformed_files_are_errors_not_panics() { + for bad in [ + "garbage {", + "", + "s = {", + r#"s = { type = "LocalizedAdjustmentPreset", value = 3 }"#, + r#"s = { type = "LocalizedAdjustmentPreset" }"#, + r#"s = { type = "LocalizedAdjustmentPreset", value = { exposure2012 = "x", grain = {{{{}}}} } }"#, + "s = { type = \"LocalizedAdjustmentPreset\", value = { exposure2012 = 1e400, contrast2012 = -1e400 } }", + "\u{feff}s = { type = \"LocalizedAdjustmentPreset\", title = \"\", value = {} }", + ] { + let _ = parse_lr_local_template(bad, "x"); + let _ = parse_bytes("x.lrtemplate", bad.as_bytes()); + } + // deeply nested tables + let deep = format!("s = {{ type = \"LocalizedAdjustmentPreset\", value = {{ pointColors = {}1{} }} }}", "{".repeat(200), "}".repeat(200)); + let _ = parse_lr_local_template(&deep, "x"); + let (e, _) = parse_lr_local_template("s = { type = \"LocalizedAdjustmentPreset\", value = { exposure2012 = 9 } }", "Stem").unwrap(); + assert_eq!((e.name.as_str(), e.adjust.exposure), ("Stem", 4.0), "values clamp, the file name names it"); + for bad in ["", "", "\u{0}\u{1}"] { + assert!(parse_bytes("x.xmp", bad.as_bytes()).is_none_or(|r| r.is_err())); + } + for bad in ["", "3", "[]", "{\"effects\": 3}", "[{\"name\": 4}]", "{\"effects\": [{\"name\": \" \"}]}"] { + assert!(parse_file(bad.as_bytes()).is_err(), "{bad}"); + } + assert!(!found_in("x.zip", b"PK\x05\x06garbage", None, &mut Vec::new(), &mut Vec::new())); + } + + /// A Camera Raw local correction preset (our own values). + const ACR: &str = r#" + + Lift Shadows + Faces +"#; + + #[test] + fn reads_a_camera_raw_local_preset() { + assert!(is_local_xmp(ACR)); + let (e, unmapped) = parse_local_xmp(ACR, "file").unwrap(); + assert_eq!((e.name.as_str(), e.group.as_str()), ("Lift Shadows", "Faces")); + assert_eq!((e.adjust.exposure, e.adjust.shadows), (1.0, 40.0)); + assert_eq!(unmapped, vec!["Grain".to_string()]); + // a develop preset is not one + let develop = ACR + .replace( + "crs:LocalExposure2012=\"+0.25\" crs:LocalShadows2012=\"0.4\" crs:LocalGrain=\"0.2\" crs:LocalTexture=\"0\"", + "crs:Exposure2012=\"1\"", + ) + .replace("PresetType=\"Local\"", "PresetType=\"Normal\""); + assert!(!is_local_xmp(&develop)); + // a preset typed Local that keeps its sliders in a correction + let nested = r#" + + +"#; + let (e, _) = parse_local_xmp(nested, "Soft").unwrap(); + assert_eq!((e.name.as_str(), e.adjust.clarity), ("Soft", -50.0)); + } + + fn found(name: &str, exposure: f64, dir: &str) -> Found { + Found { + effect: MaskEffect { name: name.into(), adjust: LocalAdjustments { exposure, ..Default::default() }, ..Default::default() }, + unmapped: Vec::new(), + file: format!("{dir}/{name}.lrtemplate"), + dir: dir.into(), + dir_group: None, + has_group: false, + } + } + + #[test] + fn dividers_become_groups() { + let e = |n: &str| MaskEffect { name: n.into(), ..Default::default() }; + assert_eq!(divider_title(&e("10 ----- DETAIL ENHANCERS ----- 10")).as_deref(), Some("Detail Enhancers")); + assert_eq!(divider_title(&e("50--------MISC-----50")).as_deref(), Some("Misc")); + assert_eq!(divider_title(&e("=== Portraits ===")).as_deref(), Some("Portraits")); + assert_eq!(divider_title(&e("11_General_-_All_Purpose")), None, "no run of marks"); + assert_eq!(divider_title(&e("-----")), None, "no title"); + let busy = MaskEffect { adjust: LocalAdjustments { exposure: 1.0, ..Default::default() }, ..e("20 --- X --- 20") }; + assert_eq!(divider_title(&busy), None, "a divider sets nothing"); + + let list = vec![ + found("01 Burn", -1.0, "a"), + found("10 ----- DETAIL ENHANCERS ----- 10", 0.0, "a"), + found("12 Sky", 0.5, "a"), + found("20 ----- RETOUCHING ----- 20", 0.0, "a"), + found("24a-Eye-Cleaner", 0.1, "a"), + found("99 Late", 0.1, "a"), + found("Burn (Darken)", -0.5, "a"), + Found { dir_group: Some("Other".into()), ..found("15 Elsewhere", 0.2, "b") }, + ]; + let (effects, dividers) = assign_groups(list.clone(), None); + let groups: Vec<(&str, &str)> = effects.iter().map(|f| (f.effect.name.as_str(), f.effect.group.as_str())).collect(); + assert_eq!( + groups, + vec![ + ("01 Burn", ""), + ("12 Sky", "Detail Enhancers"), + ("24a-Eye-Cleaner", "Retouching"), + ("99 Late", "Retouching"), + ("Burn (Darken)", ""), + ("15 Elsewhere", "Other"), + ] + ); + assert_eq!(dividers.len(), 2); + let (effects, _) = assign_groups(list, Some("Mine")); + assert!(effects.iter().all(|f| f.effect.group == "Mine")); + } + + #[test] + fn folder_names_that_only_say_local_presets_give_no_group() { + assert_eq!(effect_group_from_dir("Local Adjustment Presets"), None); + assert_eq!(effect_group_from_dir("LocalCorrections"), None); + assert_eq!(effect_group_from_dir("Local Adjustment Presets/Portrait Brushes"), Some("Portrait Brushes".into())); + assert_eq!(effect_group_from_dir("Skin Kit"), Some("Skin Kit".into())); + } +} diff --git a/crates/engine/src/cmd/mask_effects.rs b/crates/engine/src/cmd/mask_effects.rs new file mode 100644 index 000000000..387909a6d --- /dev/null +++ b/crates/engine/src/cmd/mask_effects.rs @@ -0,0 +1,756 @@ +//! Mask effect presets: named sets of local adjustment sliders picked from the Masking panel's +//! Effect menu (Lightroom's local adjustment / brush presets), saved with the library preferences. +//! Grouped (folders in the menu), favourites, partial presets (only some sliders, with or without +//! Amount) and optional brush settings. Files: [`super::mask_effect_files`]. + +use std::cmp::Ordering; + +use lightcraft_catalog::PhotoId; +use lightcraft_develop::{DevelopSettings, LocalAdjustments, Mask}; +use serde::{Deserialize, Serialize}; +use serde_json::{Map, Value, json}; + +use super::mask_effect_files as files; +use super::{CommandSpec, always, bad, cmd, has_active, str_param}; +use crate::{Result, Session}; + +/// The local sliders an effect can set (`LocalAdjustments` field names; Amount aside). +pub const FIELDS: &[&str] = &[ + "temp", + "tint", + "exposure", + "contrast", + "highlights", + "shadows", + "whites", + "blacks", + "texture", + "clarity", + "dehaze", + "hue", + "saturation", + "sharpness", + "noise", + "moire", + "defringe", + "color_hue", + "color_sat", +]; + +/// Brush settings an effect can carry (Lightroom keeps these per brush, not in presets). +#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)] +#[serde(default)] +pub struct BrushSettings { + /// Radius as a fraction of the image's long edge. + pub size: f64, + /// 0..100. + pub feather: f64, + pub flow: f64, + pub density: f64, + pub auto_mask: bool, +} + +impl Default for BrushSettings { + fn default() -> Self { + Self { size: 0.04, feather: 50.0, flow: 100.0, density: 100.0, auto_mask: false } + } +} + +impl BrushSettings { + fn clamped(self) -> Self { + let c = |v: f64, lo: f64, hi: f64, d: f64| if v.is_finite() { v.clamp(lo, hi) } else { d }; + let d = Self::default(); + Self { + size: c(self.size, 0.002, 0.5, d.size), + feather: c(self.feather, 0.0, 100.0, d.feather), + flow: c(self.flow, 1.0, 100.0, d.flow), + density: c(self.density, 1.0, 100.0, d.density), + auto_mask: self.auto_mask, + } + } +} + +/// A named set of local adjustment sliders. Applying one sets the sliders it names (all of them +/// when `fields` is empty, as Lightroom's presets do: those it doesn't change go to 0); the mask's +/// Amount stays unless `with_amount`. +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +#[serde(default)] +pub struct MaskEffect { + pub name: String, + pub adjust: LocalAdjustments, + /// The Effect menu folder ("" = none). + #[serde(skip_serializing_if = "String::is_empty")] + pub group: String, + #[serde(skip_serializing_if = "std::ops::Not::not")] + pub favorite: bool, + /// The sliders this effect sets ([`FIELDS`] names); empty = all of them. + #[serde(skip_serializing_if = "Vec::is_empty")] + pub fields: Vec, + /// Applying sets the mask's Amount to `adjust.amount` too. + #[serde(skip_serializing_if = "std::ops::Not::not")] + pub with_amount: bool, + /// Brush settings the brush takes when the effect is applied. + #[serde(skip_serializing_if = "Option::is_none")] + pub brush: Option, + /// Settings of an imported preset that we have no slider for (Lightroom key → value), kept so + /// an export carries them. + #[serde(skip_serializing_if = "Map::is_empty")] + pub extra: Map, +} + +impl Default for MaskEffect { + fn default() -> Self { + Self { + name: String::new(), + adjust: LocalAdjustments::default(), + group: String::new(), + favorite: false, + fields: Vec::new(), + with_amount: false, + brush: None, + extra: Map::new(), + } + } +} + +/// Field `k` of `a` (0 for an unknown name). +pub fn get(a: &LocalAdjustments, k: &str) -> f64 { + serde_json::to_value(a).ok().and_then(|v| v.get(k).and_then(Value::as_f64)).unwrap_or(0.0) +} + +/// `a` with field `k` set to `v` (unchanged for an unknown name). +pub fn set(a: &LocalAdjustments, k: &str, v: f64) -> LocalAdjustments { + let Ok(Value::Object(mut o)) = serde_json::to_value(a) else { return *a }; + if !o.contains_key(k) || !v.is_finite() { + return *a; + } + o.insert(k.into(), json!(v)); + serde_json::from_value(Value::Object(o)).unwrap_or(*a) +} + +/// The sliders of `a` away from 0 (the colour's hue only with its saturation). +pub fn changed_fields(a: &LocalAdjustments) -> Vec { + let mut out: Vec = FIELDS.iter().filter(|k| get(a, k).abs() > 1e-9 && **k != "color_hue").map(|k| k.to_string()).collect(); + if a.color_sat.abs() > 1e-9 { + out.push("color_hue".into()); + } + out +} + +impl MaskEffect { + /// The sliders this effect sets. + pub fn field_list(&self) -> Vec<&str> { + if self.fields.is_empty() { FIELDS.to_vec() } else { FIELDS.iter().copied().filter(|k| self.fields.iter().any(|f| f == k)).collect() } + } + /// `a` with this preset's sliders (and Amount, if it carries it). + pub fn apply(&self, a: &LocalAdjustments) -> LocalAdjustments { + let mut out = if self.fields.is_empty() { + LocalAdjustments { amount: a.amount, ..self.adjust } + } else { + self.field_list().iter().fold(*a, |acc, k| set(&acc, k, get(&self.adjust, k))) + }; + if self.with_amount { + out.amount = self.adjust.amount; + } + out + } + /// Does `a` carry exactly this preset's sliders? A tint's hue doesn't count without its + /// saturation (it does nothing then). + pub fn matches(&self, a: &LocalAdjustments) -> bool { + let fields = self.field_list(); + let colorless = |x: &LocalAdjustments| x.color_sat.abs() < 1e-9; + let same = |k: &str| (get(&self.adjust, k) - get(a, k)).abs() < 1e-6; + fields.iter().all(|k| *k == "color_hue" && colorless(&self.adjust) && colorless(a) || same(k)) + && (!self.with_amount || (self.adjust.amount - a.amount).abs() < 1e-6) + } + /// Does the effect set nothing (all its sliders at 0, nothing kept from an import)? + pub fn is_empty(&self) -> bool { + self.extra.is_empty() && changed_fields(&self.adjust).iter().all(|k| !self.field_list().contains(&k.as_str())) + } + /// The same effect with its values in range and its field list normalised. + pub fn normalized(mut self) -> Self { + self.name = self.name.trim().to_string(); + self.group = self.group.trim().to_string(); + let amount = if self.with_amount { self.adjust.amount } else { 100.0 }; + self.adjust = super::masks::clamp_local(LocalAdjustments { amount, ..self.adjust }); + self.fields.retain(|f| FIELDS.contains(&f.as_str())); + if self.fields.iter().any(|f| f == "color_sat") && !self.fields.iter().any(|f| f == "color_hue") { + self.fields.push("color_hue".into()); + } + self.fields.sort_by_key(|f| FIELDS.iter().position(|k| k == f)); + self.fields.dedup(); + if self.fields.len() == FIELDS.len() { + self.fields.clear(); + } + self.brush = self.brush.map(BrushSettings::clamped); + self + } +} + +/// Compare names the way people number their presets: digit runs as numbers ("2 …" before +/// "10 …"), letters ignoring case. +pub fn natural_cmp(a: &str, b: &str) -> Ordering { + fn chunks(s: &str) -> Vec<(bool, String)> { + let mut out: Vec<(bool, String)> = Vec::new(); + for c in s.chars() { + let d = c.is_ascii_digit(); + match out.last_mut() { + Some((kind, text)) if *kind == d => text.push(c), + _ => out.push((d, c.to_string())), + } + } + out + } + let (x, y) = (chunks(a), chunks(b)); + for (p, q) in x.iter().zip(y.iter()) { + let o = match (p, q) { + ((true, m), (true, n)) => { + let (m, n) = (m.trim_start_matches('0'), n.trim_start_matches('0')); + m.len().cmp(&n.len()).then_with(|| m.cmp(n)) + } + ((_, m), (_, n)) => m.to_lowercase().cmp(&n.to_lowercase()), + }; + if o != Ordering::Equal { + return o; + } + } + x.len().cmp(&y.len()).then_with(|| a.cmp(b)) +} + +/// The Effect menu's order: groups in the order they first appear (ungrouped first), each with +/// its effects (indices into `effects`) sorted by name. +pub fn menu_order(effects: &[MaskEffect]) -> Vec<(String, Vec)> { + let mut groups: Vec<(String, Vec)> = Vec::new(); + if effects.iter().any(|e| e.group.is_empty()) { + groups.push((String::new(), Vec::new())); + } + for (i, e) in effects.iter().enumerate() { + match groups.iter_mut().find(|(g, _)| g.eq_ignore_ascii_case(&e.group)) { + Some((_, v)) => v.push(i), + None => groups.push((e.group.clone(), vec![i])), + } + } + for (_, v) in &mut groups { + v.sort_by(|a, b| match (effects.get(*a), effects.get(*b)) { + (Some(x), Some(y)) => natural_cmp(&x.name, &y.name), + _ => Ordering::Equal, + }); + } + groups +} + +/// Name of the effect the mask adjustments `a` match, if any (full presets before partial ones). +pub fn matching_effect(s: &Session, a: &LocalAdjustments) -> Option { + let full = s.mask_effects.iter().filter(|e| e.fields.is_empty()).find(|e| e.matches(a)); + full.or_else(|| s.mask_effects.iter().find(|e| e.matches(a))).map(|e| e.name.clone()) +} + +fn position(s: &Session, name: &str) -> Option { + s.mask_effects.iter().position(|x| x.name.eq_ignore_ascii_case(name.trim())) +} + +/// Add or replace (same name, ignoring case) an effect; a replaced one keeps its star. +pub(crate) fn upsert(s: &mut Session, e: MaskEffect) { + match position(s, &e.name).and_then(|i| s.mask_effects.get_mut(i)) { + Some(x) => { + let favorite = x.favorite || e.favorite; + *x = MaskEffect { favorite, ..e }; + } + None => s.mask_effects.push(e), + } +} + +fn name_param(p: &Value, c: &str) -> Result { + name_of(p, "name", c) +} + +fn name_of(p: &Value, key: &str, c: &str) -> Result { + str_param(p, key).map(str::trim).filter(|n| !n.is_empty()).map(str::to_string).ok_or_else(|| bad(c, format!("missing `{key}`"))) +} + +fn strings(p: &Value, key: &str) -> Option> { + p.get(key).and_then(Value::as_array).map(|a| a.iter().filter_map(Value::as_str).map(str::to_string).collect()) +} + +/// The names a command acts on: `names: [...]` or `name`. +fn names_param(p: &Value, c: &str) -> Result> { + match strings(p, "names").filter(|v| !v.is_empty()) { + Some(v) => Ok(v), + None => name_param(p, c).map(|n| vec![n]), + } +} + +fn effect_index(s: &Session, name: &str, c: &str) -> Result { + position(s, name).ok_or_else(|| bad(c, format!("no effect `{name}`"))) +} + +/// The selected mask's adjustments on the active photo. +fn selected_mask(s: &Session, p: &Value, c: &str) -> Result { + let id = s.active().ok_or_else(|| bad(c, "no active photo"))?; + let mid = p + .get("id") + .and_then(Value::as_u64) + .and_then(|v| u32::try_from(v).ok()) + .or(s.active_mask) + .ok_or_else(|| bad(c, "no mask selected (give `id`)"))?; + let d = s.develop_of(id).unwrap_or_default(); + d.masks.iter().find(|m| m.id == mid).cloned().ok_or_else(|| bad(c, format!("no mask {mid}"))) +} + +/// The kind of a mask's first component (`brush`, `linear`, `radial`, `subject`…): the tool the +/// effect is remembered for. +pub fn mask_tool(m: &Mask) -> Option { + m.components.first().and_then(|c| serde_json::to_value(&c.shape).ok()).and_then(|v| v.get("kind").and_then(Value::as_str).map(str::to_string)) +} + +/// The adjustments a new mask made with `tool` starts with: the effect last applied to a mask of +/// that kind (Lightroom carries the chosen effect over to the next mask), else all at 0. +pub fn new_mask_adjust(s: &Session, tool: &str) -> LocalAdjustments { + s.mask_tool_effects + .get(tool) + .and_then(|name| position(s, name)) + .and_then(|i| s.mask_effects.get(i)) + .map(|e| super::masks::clamp_local(e.apply(&LocalAdjustments::default()))) + .unwrap_or_default() +} + +fn list(s: &Session) -> Value { + let current = s + .active() + .and_then(|id| s.develop_of(id)) + .and_then(|d| s.active_mask.and_then(|mid| d.masks.iter().find(|m| m.id == mid).map(|m| m.adjust))) + .and_then(|a| matching_effect(s, &a)); + let groups: Vec = menu_order(&s.mask_effects) + .into_iter() + .map(|(g, ix)| json!({"name": g, "effects": ix.iter().filter_map(|i| s.mask_effects.get(*i)).map(|e| e.name.clone()).collect::>()})) + .collect(); + json!({"effects": s.mask_effects, "groups": groups, "current": current, "toolEffects": s.mask_tool_effects}) +} + +/// Masks on `d` named like one of `names` (ignoring case). +fn masks_named(d: &DevelopSettings, names: &[String]) -> Vec { + d.masks.iter().filter(|m| names.iter().any(|n| n.eq_ignore_ascii_case(&m.name))).map(|m| m.id).collect() +} + +fn apply_effect(s: &mut Session, p: &Value) -> Result { + const C: &str = "mask.applyEffect"; + let name = name_param(p, C)?; + let effect = s.mask_effects.get(effect_index(s, &name, C)?).cloned().ok_or_else(|| bad(C, format!("no effect `{name}`")))?; + let id = s.active().ok_or_else(|| bad(C, "no active photo"))?; + let d = s.develop_of(id).unwrap_or_default(); + // the masks: `ids`, `all`, else `id` or the selected one + let ids: Vec = if p.get("all").and_then(Value::as_bool).unwrap_or(false) { + d.masks.iter().map(|m| m.id).collect() + } else if let Some(a) = p.get("ids").and_then(Value::as_array) { + a.iter().filter_map(Value::as_u64).filter_map(|v| u32::try_from(v).ok()).collect() + } else { + vec![selected_mask(s, p, C)?.id] + }; + if ids.is_empty() { + return Err(bad(C, "no masks to apply the effect to")); + } + if let Some(missing) = ids.iter().find(|m| !d.masks.iter().any(|x| x.id == **m)) { + return Err(bad(C, format!("no mask {missing}"))); + } + let set_masks = |d: &DevelopSettings, ids: &[u32]| { + let mut d = d.clone(); + for m in d.masks.iter_mut().filter(|m| ids.contains(&m.id)) { + m.adjust = super::masks::clamp_local(effect.apply(&m.adjust)); + } + d + }; + let label = format!("Mask Effect: {}", effect.name); + // with `photos: "selected"`, the other selected photos' masks of the same names too + let others: Vec<(PhotoId, DevelopSettings)> = if str_param(p, "photos") == Some("selected") { + let names: Vec = d.masks.iter().filter(|m| ids.contains(&m.id)).map(|m| m.name.clone()).collect(); + s.selection + .ids + .iter() + .filter(|x| **x != id) + .filter_map(|x| s.develop_of(*x).map(|o| (*x, o))) + .filter_map(|(x, o)| { + let found = masks_named(&o, &names); + (!found.is_empty()).then(|| (x, set_masks(&o, &found))) + }) + .collect() + } else { + Vec::new() + }; + let mine = set_masks(&d, &ids); + let photos = 1 + others.len(); + if others.is_empty() { + s.set_develop(id, mine, &label)?; + } else { + // one undo step for every photo + let mut ops: Vec<_> = s.develop_op(id, mine, &label).into_iter().collect(); + ops.extend(others.into_iter().filter_map(|(x, o)| s.develop_op(x, o, &label))); + s.commit(&label, lightcraft_catalog::Op::Batch { ops })?; + } + // the next mask made with this tool starts with the effect + if p.get("remember").and_then(Value::as_bool).unwrap_or(true) { + for m in d.masks.iter().filter(|m| ids.contains(&m.id)) { + if let Some(tool) = mask_tool(m) { + s.mask_tool_effects.insert(tool, effect.name.clone()); + } + } + s.save_prefs()?; + } + Ok(json!({"name": effect.name, "masks": ids, "photos": photos, "brush": effect.brush})) +} + +/// The fields a save keeps: `fields: [..]` | `"changed"` | `"all"` (default). +fn fields_param(p: &Value, a: &LocalAdjustments, c: &str) -> Result> { + match p.get("fields") { + None | Some(Value::Null) => Ok(Vec::new()), + Some(Value::String(x)) if x == "all" => Ok(Vec::new()), + Some(Value::String(x)) if x == "changed" => { + let f = changed_fields(a); + if f.is_empty() { Err(bad(c, "the mask changes no slider; nothing to save")) } else { Ok(f) } + } + Some(Value::Array(v)) => { + let f: Vec = v.iter().filter_map(Value::as_str).filter(|k| FIELDS.contains(k)).map(str::to_string).collect(); + if f.is_empty() { Err(bad(c, format!("`fields` names no slider (one of {})", FIELDS.join(", ")))) } else { Ok(f) } + } + Some(_) => Err(bad(c, "`fields` is \"all\", \"changed\" or a list of slider names")), + } +} + +fn brush_param(p: &Value, c: &str) -> Result> { + match p.get("brush") { + None | Some(Value::Null) | Some(Value::Bool(false)) => Ok(None), + Some(v) => serde_json::from_value::(v.clone()).map(|b| Some(b.clamped())).map_err(|e| bad(c, format!("`brush`: {e}"))), + } +} + +fn save_effect(s: &mut Session, p: &Value) -> Result { + const C: &str = "mask.saveEffect"; + let name = name_param(p, C)?; + let adjust = selected_mask(s, p, C)?.adjust; + let group = str_param(p, "group").map(|g| g.trim().to_string()).unwrap_or_default(); + let e = MaskEffect { + name, + adjust, + group, + fields: fields_param(p, &adjust, C)?, + with_amount: p.get("amount").and_then(Value::as_bool).unwrap_or(false), + brush: brush_param(p, C)?, + ..Default::default() + }; + upsert(s, e.normalized()); + s.save_prefs()?; + Ok(list(s)) +} + +/// Overwrite an effect's sliders with the selected mask's (keeping which sliders and whether +/// Amount it carries, its group and star). +fn update_effect(s: &mut Session, p: &Value) -> Result { + const C: &str = "mask.updateEffect"; + let name = name_param(p, C)?; + let i = effect_index(s, &name, C)?; + let adjust = selected_mask(s, p, C)?.adjust; + let brush = brush_param(p, C)?; + let e = s.mask_effects.get_mut(i).ok_or_else(|| bad(C, format!("no effect `{name}`")))?; + let mut next = MaskEffect { adjust, extra: Map::new(), ..e.clone() }; + if let Some(f) = p.get("fields").filter(|v| !v.is_null()) { + next.fields = fields_param(&json!({"fields": f}), &adjust, C)?; + } + if let Some(a) = p.get("amount").and_then(Value::as_bool) { + next.with_amount = a; + } + if brush.is_some() { + next.brush = brush; + } + *e = next.normalized(); + s.save_prefs()?; + Ok(list(s)) +} + +fn rename_effect(s: &mut Session, p: &Value) -> Result { + const C: &str = "mask.renameEffect"; + let name = name_param(p, C)?; + let to = name_of(p, "to", C)?; + let i = effect_index(s, &name, C)?; + if position(s, &to).is_some_and(|j| j != i) { + return Err(bad(C, format!("an effect named `{to}` exists already"))); + } + if let Some(e) = s.mask_effects.get_mut(i) { + e.name = to.clone(); + } + for v in s.mask_tool_effects.values_mut() { + if v.eq_ignore_ascii_case(&name) { + *v = to.clone(); + } + } + s.save_prefs()?; + Ok(list(s)) +} + +/// A name like `base` that no effect has: `base`, `base 2`, `base 3`… +fn free_name(s: &Session, base: &str) -> String { + (1..10_000) + .map(|n| if n == 1 { base.to_string() } else { format!("{base} {n}") }) + .find(|n| position(s, n).is_none()) + .unwrap_or_else(|| base.into()) +} + +fn duplicate_effect(s: &mut Session, p: &Value) -> Result { + const C: &str = "mask.duplicateEffect"; + let name = name_param(p, C)?; + let e = s.mask_effects.get(effect_index(s, &name, C)?).cloned().ok_or_else(|| bad(C, format!("no effect `{name}`")))?; + let to = match str_param(p, "to").map(str::trim).filter(|t| !t.is_empty()) { + Some(t) if position(s, t).is_some() => return Err(bad(C, format!("an effect named `{t}` exists already"))), + Some(t) => t.to_string(), + None => free_name(s, &format!("{} copy", e.name)), + }; + s.mask_effects.push(MaskEffect { name: to.clone(), favorite: false, ..e }); + s.save_prefs()?; + let mut out = list(s); + out["name"] = json!(to); + Ok(out) +} + +fn delete_effect(s: &mut Session, p: &Value) -> Result { + const C: &str = "mask.deleteEffect"; + let names = names_param(p, C)?; + if let Some(missing) = names.iter().find(|n| position(s, n).is_none()) { + return Err(bad(C, format!("no effect `{missing}`"))); + } + s.mask_effects.retain(|x| !names.iter().any(|n| n.eq_ignore_ascii_case(&x.name))); + s.mask_tool_effects.retain(|_, v| !names.iter().any(|n| n.eq_ignore_ascii_case(v))); + s.save_prefs()?; + Ok(list(s)) +} + +fn favorite_effect(s: &mut Session, p: &Value) -> Result { + const C: &str = "mask.favoriteEffect"; + let names = names_param(p, C)?; + let mut idx = Vec::new(); + for n in &names { + idx.push(effect_index(s, n, C)?); + } + let want = p.get("favorite").and_then(Value::as_bool); + for i in idx { + if let Some(e) = s.mask_effects.get_mut(i) { + e.favorite = want.unwrap_or(!e.favorite); + } + } + s.save_prefs()?; + Ok(list(s)) +} + +fn set_group(s: &mut Session, p: &Value) -> Result { + const C: &str = "mask.setEffectGroup"; + let names = names_param(p, C)?; + let group = str_param(p, "group").map(|g| g.trim().to_string()).ok_or_else(|| bad(C, "missing `group` (\"\" = none)"))?; + let mut idx = Vec::new(); + for n in &names { + idx.push(effect_index(s, n, C)?); + } + // an existing group's spelling wins + let group = s.mask_effects.iter().map(|e| &e.group).find(|g| g.eq_ignore_ascii_case(&group)).cloned().unwrap_or(group); + for i in idx { + if let Some(e) = s.mask_effects.get_mut(i) { + e.group = group.clone(); + } + } + s.save_prefs()?; + Ok(list(s)) +} + +fn rename_group(s: &mut Session, p: &Value) -> Result { + const C: &str = "mask.renameEffectGroup"; + let group = name_of(p, "group", C)?; + let to = str_param(p, "to").map(|g| g.trim().to_string()).ok_or_else(|| bad(C, "missing `to` (\"\" = ungroup)"))?; + let mut n = 0; + for e in s.mask_effects.iter_mut().filter(|e| e.group.eq_ignore_ascii_case(&group)) { + e.group = to.clone(); + n += 1; + } + if n == 0 { + return Err(bad(C, format!("no group `{group}`"))); + } + s.save_prefs()?; + Ok(list(s)) +} + +fn tool_effect(s: &mut Session, p: &Value) -> Result { + const C: &str = "mask.toolEffect"; + let tool = name_of(p, "tool", C)?; + match str_param(p, "name").map(str::trim).filter(|n| !n.is_empty()) { + Some(n) => { + let i = effect_index(s, n, C)?; + let name = s.mask_effects.get(i).map(|e| e.name.clone()).unwrap_or_default(); + s.mask_tool_effects.insert(tool, name); + } + None => { + s.mask_tool_effects.remove(&tool); + } + } + s.save_prefs()?; + Ok(json!({"toolEffects": s.mask_tool_effects})) +} + +pub fn specs() -> Vec { + vec![ + cmd!( + query "mask.effects", + "Mask Effect Presets", + [], + None, + "{} → {effects: [{name, adjust, group?, favorite?, fields?, with_amount?, brush?, extra?}], groups: [{name, effects: [name]}] (menu order), current: the effect the selected mask matches or null, toolEffects: {tool: effect a new mask of that kind starts with}}", + always, + |s, _| Ok(list(s)) + ), + cmd!( + "mask.applyEffect", + "Apply Mask Effect", + [], + None, + "{name, id? | ids?: [maskId] | all?: bool, photos?: \"selected\" (also the masks of the same names on the other selected photos, one undo step), remember?: bool (true: new masks of this kind start with it)} — sets the effect's sliders (all, or the ones it keeps; Amount only if it carries it) → {name, masks, photos, brush}", + has_active, + apply_effect + ), + cmd!( + "mask.saveEffect", + "Save Mask Effect", + [], + None, + "{name, id?, group?, fields?: \"all\" (default) | \"changed\" | [slider], amount?: bool (carry Amount), brush?: {size, feather, flow, density, auto_mask}} — the selected mask's sliders as an effect preset (replaces one of that name), saved with the library", + has_active, + save_effect + ), + cmd!( + "mask.updateEffect", + "Update Mask Effect", + [], + None, + "{name, id?, fields?, amount?, brush?} — overwrite the effect with the selected mask's sliders (keeps its group, star and which sliders it carries unless given)", + has_active, + update_effect + ), + cmd!("mask.renameEffect", "Rename Mask Effect", [], None, "{name, to}", always, rename_effect), + cmd!( + "mask.duplicateEffect", + "Duplicate Mask Effect", + [], + None, + "{name, to?} (default \" copy\") → list + {name}", + always, + duplicate_effect + ), + cmd!("mask.deleteEffect", "Delete Mask Effect", [], None, "{name | names: [name]} — removes effect presets", always, delete_effect), + cmd!("mask.favoriteEffect", "Favorite Mask Effect", [], None, "{name | names, favorite?: bool (default: toggle)}", always, favorite_effect), + cmd!("mask.setEffectGroup", "Move Mask Effect to Group", [], None, "{name | names, group: name (\"\" = none)}", always, set_group), + cmd!( + "mask.renameEffectGroup", + "Rename Mask Effect Group", + [], + None, + "{group, to} (\"\" ungroups; an existing name merges)", + always, + rename_group + ), + cmd!( + "mask.toolEffect", + "Set Effect for New Masks", + [], + None, + "{tool: brush|linear|radial|subject|…, name?} — the effect new masks of that kind start with (no name: none)", + always, + tool_effect + ), + cmd!( + "mask.importEffects", + "Import Mask Effects", + [], + None, + "{path | paths: [file or folder], group?, dryRun?} — Lightroom Classic local adjustment presets (.lrtemplate), Camera Raw local correction presets (.xmp), .lceffect files and .zip bundles of these; folders and Lightroom's divider presets (\"10 ----- DETAIL ----- 10\") become groups; same-named effects are replaced → {imported: [{name, group, file, unmapped}], dividers: [{name, group, file}], failed: [[file, error]]}", + always, + files::import_effects + ), + cmd!( + "mask.exportEffects", + "Export Mask Effects", + [], + None, + "{path, names?: [name], group?: name (default: all)} — writes an .lceffect JSON file (groups, stars and kept Lightroom settings included) → {path, count}", + always, + files::export_effects + ), + ] +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn apply_keeps_amount_and_matches_ignore_it() { + let e = MaskEffect { name: "E".into(), adjust: LocalAdjustments { exposure: 1.0, ..Default::default() }, ..Default::default() }; + let current = LocalAdjustments { shadows: 40.0, amount: 60.0, ..Default::default() }; + let out = e.apply(¤t); + assert_eq!((out.exposure, out.shadows, out.amount), (1.0, 0.0, 60.0)); + assert!(e.matches(&out)); + assert!(!e.matches(¤t)); + } + + #[test] + fn partial_effects_leave_other_sliders_and_can_carry_amount() { + let e = MaskEffect { + name: "Lift".into(), + adjust: LocalAdjustments { shadows: 30.0, clarity: 99.0, amount: 70.0, ..Default::default() }, + fields: vec!["shadows".into()], + with_amount: true, + ..Default::default() + }; + let current = LocalAdjustments { exposure: 0.5, clarity: 10.0, ..Default::default() }; + let out = e.apply(¤t); + assert_eq!((out.exposure, out.shadows, out.clarity, out.amount), (0.5, 30.0, 10.0, 70.0)); + assert!(e.matches(&out)); + assert!(!e.matches(&LocalAdjustments { amount: 100.0, ..out }), "Amount counts when carried"); + } + + #[test] + fn hue_without_saturation_still_matches() { + let e = MaskEffect { + name: "E".into(), + adjust: LocalAdjustments { color_hue: 240.0, exposure: 0.25, ..Default::default() }, + ..Default::default() + }; + assert!(e.matches(&LocalAdjustments { exposure: 0.25, ..Default::default() })); + assert!(!e.matches(&LocalAdjustments { exposure: 0.25, color_sat: 10.0, ..Default::default() })); + } + + #[test] + fn natural_order_and_groups() { + let mut v = vec!["10 b", "2 a", "Burn", "01 x", "1 y"]; + v.sort_by(|a, b| natural_cmp(a, b)); + assert_eq!(v, vec!["01 x", "1 y", "2 a", "10 b", "Burn"]); + let e = |n: &str, g: &str| MaskEffect { name: n.into(), group: g.into(), ..Default::default() }; + let list = vec![e("12 b", "Detail"), e("3 a", ""), e("11 a", "detail"), e("20 r", "Retouch"), e("Burn", "")]; + let order = menu_order(&list); + let names: Vec<(String, Vec<&str>)> = order.iter().map(|(g, ix)| (g.clone(), ix.iter().map(|i| list[*i].name.as_str()).collect())).collect(); + assert_eq!( + names, + vec![("".to_string(), vec!["3 a", "Burn"]), ("Detail".to_string(), vec!["11 a", "12 b"]), ("Retouch".to_string(), vec!["20 r"]),] + ); + } + + #[test] + fn normalized_clamps_and_tidies_fields() { + let e = MaskEffect { + name: " X ".into(), + adjust: LocalAdjustments { exposure: 99.0, amount: 150.0, ..Default::default() }, + fields: vec!["color_sat".into(), "bogus".into(), "exposure".into()], + brush: Some(BrushSettings { size: f64::NAN, flow: 0.0, ..Default::default() }), + ..Default::default() + } + .normalized(); + assert_eq!(e.name, "X"); + assert_eq!(e.adjust.exposure, 4.0); + assert_eq!(e.adjust.amount, 100.0, "Amount isn't carried"); + assert_eq!(e.fields, vec!["exposure", "color_hue", "color_sat"]); + let b = e.brush.unwrap(); + assert_eq!((b.size, b.flow), (0.04, 1.0)); + } +} diff --git a/crates/engine/src/cmd/masks.rs b/crates/engine/src/cmd/masks.rs index 25ff3f24d..a53cc9ea7 100644 --- a/crates/engine/src/cmd/masks.rs +++ b/crates/engine/src/cmd/masks.rs @@ -178,11 +178,14 @@ pub fn specs() -> Vec { return Ok(json!({"pending": true})); } let next = s.active().and_then(|id| s.develop_of(id)).map(|d| d.next_mask_id()).unwrap_or(1); + // the effect last applied to a mask of this kind + let adjust = super::mask_effects::new_mask_adjust(s, &kind); let out = masks_edit(s, "mask.add", "Add Mask", |masks, active| { masks.push(Mask { id: next, name: name.unwrap_or_else(|| format!("Mask {next}")), components: vec![MaskComponent { name: None, op: MaskOp::Add, invert: false, shape }], + adjust, ..Default::default() }); *active = Some(next); @@ -230,7 +233,7 @@ pub fn specs() -> Vec { "Brush Stroke", [], None, - "{id?: maskId, points: [[x,y],…] normalized, size?: fraction of long edge (0.03), feather?, flow?, density?, erase?: bool, autoMask?: bool}", + "{id?: maskId, points: [[x,y],…] normalized, size?: fraction of long edge (0.03), feather?, flow?, density?, erase?: bool, autoMask?: bool, pressure?: [p,…] pen pressure 0..1 per point (scales each dab's size and flow; omitted: full)}", has_active, |s, p| { let pts: Vec = p @@ -240,6 +243,7 @@ pub fn specs() -> Vec { .iter() .filter_map(|q| Some(Point::new(q.get(0)?.as_f64()?, q.get(1)?.as_f64()?))) .collect(); + let pts_len = pts.len(); let d = BrushStroke::default(); let stroke = BrushStroke { points: pts, @@ -249,14 +253,16 @@ pub fn specs() -> Vec { density: f64_or(p, "density", d.density), erase: bool_or(p, "erase", false), auto_mask: bool_or(p, "autoMask", false), + pressure: pressure_list(p.get("pressure"), pts_len)?, }; let next = s.active().and_then(|id| s.develop_of(id)).map(|d| d.next_mask_id()).unwrap_or(1); let want = p.get("id").and_then(Value::as_u64).map(|v| v as u32).or(s.active_mask); + let adjust = super::mask_effects::new_mask_adjust(s, "brush"); masks_edit(s, "mask.brushStroke", "Brush", |masks, active| { let i = match want.and_then(|m| masks.iter().position(|x| x.id == m)) { Some(i) => i, None => { - masks.push(Mask { id: next, name: format!("Mask {next}"), ..Default::default() }); + masks.push(Mask { id: next, name: format!("Mask {next}"), adjust, ..Default::default() }); *active = Some(next); masks.len() - 1 } @@ -921,7 +927,7 @@ pub fn specs() -> Vec { has_active, |s, p| { let c = "redeye.catchlight"; - let i = super::f64_req(p, "index", c)? as usize; + let i = super::index_req(p, "index", c)?; let id = s.active().ok_or_else(|| bad(c, "no active photo"))?; let mut dd = (*s.develop_of(id).unwrap_or_default()).clone(); let eye = dd.red_eye.get_mut(i).ok_or_else(|| bad(c, "no such eye"))?; @@ -936,7 +942,7 @@ pub fn specs() -> Vec { ), cmd!("redeye.delete", "Delete Red Eye Correction", [], None, "{index}", has_active, |s, p| { let c = "redeye.delete"; - let i = super::f64_req(p, "index", c)? as usize; + let i = super::index_req(p, "index", c)?; let id = s.active().ok_or_else(|| bad(c, "no active photo"))?; let mut dd = (*s.develop_of(id).unwrap_or_default()).clone(); if i >= dd.red_eye.len() { @@ -979,7 +985,7 @@ fn pick_source(s: &mut Session, id: crate::PhotoId, d: &lightcraft_develop::Deve lightcraft_pipeline::spots::pick_source(&src, &info, d, spot, avoid) } -fn clamp_local(mut a: LocalAdjustments) -> LocalAdjustments { +pub(crate) fn clamp_local(mut a: LocalAdjustments) -> LocalAdjustments { let c = |v: &mut f64, lo: f64, hi: f64| *v = if v.is_finite() { v.clamp(lo, hi) } else { 0.0 }; c(&mut a.exposure, -4.0, 4.0); for v in [ @@ -1007,3 +1013,17 @@ fn clamp_local(mut a: LocalAdjustments) -> LocalAdjustments { c(&mut a.amount, 0.0, 200.0); a } + +/// `mask.brushStroke`'s `pressure`: one number 0..1 per point (clamped), at most one per point; absent or `null` +/// is no pressure data. +fn pressure_list(v: Option<&Value>, points: usize) -> Result> { + let Some(v) = v.filter(|v| !v.is_null()) else { return Ok(Vec::new()) }; + let list = v.as_array().ok_or_else(|| bad("mask.brushStroke", "pressure must be a list of numbers 0..1"))?; + if list.len() > points { + return Err(bad("mask.brushStroke", "pressure has more values than points")); + } + list.iter() + .map(|q| q.as_f64().filter(|f| f.is_finite()).map(|f| f.clamp(0.0, 1.0) as f32)) + .collect::>>() + .ok_or_else(|| bad("mask.brushStroke", "pressure must be a list of numbers 0..1")) +} diff --git a/crates/engine/src/cmd/merge.rs b/crates/engine/src/cmd/merge.rs index a8869153a..708614453 100644 --- a/crates/engine/src/cmd/merge.rs +++ b/crates/engine/src/cmd/merge.rs @@ -1,4 +1,4 @@ -//! Photo Merge commands: `merge.hdr`, `merge.panorama`, `merge.hdrPanorama`. +//! Photo Merge commands: `merge.hdr`, `merge.panorama`, `merge.hdrPanorama`, `merge.hdrBatch`. //! //! Run synchronously here (CLI, MCP, control channel); the desktop UI plans the same job with //! [`Session::plan_merge`] and runs it on a worker thread. `preview: true` merges ≤ 1024 px frames @@ -40,6 +40,26 @@ fn run(s: &mut Session, id: &str, p: &Value) -> Result { Ok(info) } +/// `merge.hdrBatch`: an HDR merge of each bracket set among the photos ([`lightcraft_catalog::Catalog::bracket_sets`]), +/// one after the other. A set that fails is reported and the others still run. +fn run_batch(s: &mut Session, p: &Value) -> Result { + const ID: &str = "merge.hdrBatch"; + let (kind, finish) = crate::merge::parse("merge.hdr", p)?; + let ids: Vec = s.targets(p); + let sets = s.catalog.bracket_sets(&ids); + if sets.is_empty() { + return Err(bad(ID, crate::merge::NO_BRACKET_SETS)); + } + let (mut merged, mut failed) = (Vec::new(), Vec::new()); + for set in sets { + match s.merge_now(kind.clone(), finish.clone(), &set) { + Ok(v) => merged.push(v), + Err(e) => failed.push(json!({"photos": set.iter().map(|p| p.0).collect::>(), "error": e.to_string()})), + } + } + Ok(json!({"merged": merged, "failed": failed})) +} + pub fn specs() -> Vec { vec![ cmd!( @@ -56,7 +76,7 @@ pub fn specs() -> Vec { "Panorama Merge", [], None, - "{ids?, projection=auto|spherical|cylindrical|perspective, boundaryWarp=0..100, autoCrop=false, fillEdges=false, autoSettings=true, stack=false, maxMegapixels=40, preview=false, previewPath?}", + "{ids?, projection=auto|spherical|cylindrical|perspective, boundaryWarp=0..100, autoCrop=false, fillEdges=false, autoSettings=true, stack=false, maxMegapixels=full size (≤ 512, held to memory), preview=false, previewPath?}", can_merge, |s, p| run(s, "merge.panorama", p) ), @@ -69,5 +89,14 @@ pub fn specs() -> Vec { can_merge, |s, p| run(s, "merge.hdrPanorama", p) ), + cmd!( + "merge.hdrBatch", + "HDR for Each Bracket Set", + ["Photo", "Photo Merge"], + None, + "{ids?, align=true, deghost=none|low|medium|high, autoSettings=true, stack=false}: an HDR merge of each bracket set among the photos (each stack, then photos taken less than a second apart at exposures ≥ ½ EV apart) → {merged: [{id, path, …}], failed: [{photos, error}]}", + can_merge, + run_batch + ), ] } diff --git a/crates/engine/src/cmd/mod.rs b/crates/engine/src/cmd/mod.rs index 4de460761..41e3b0929 100644 --- a/crates/engine/src/cmd/mod.rs +++ b/crates/engine/src/cmd/mod.rs @@ -24,6 +24,8 @@ pub mod keywords; pub mod library; pub mod lut_profiles; pub mod manage; +pub mod mask_effect_files; +pub mod mask_effects; mod masks; mod merge; pub mod metadata; @@ -33,6 +35,8 @@ mod prefs; mod preset_files; pub mod previews; mod query; +pub mod recheck; +mod slideshow; mod sync; mod xmp; @@ -145,6 +149,7 @@ pub fn command_specs() -> &'static [CommandSpec] { v.extend(manage::specs()); v.extend(previews::specs()); v.extend(lut_profiles::specs()); + v.extend(mask_effects::specs()); v.extend(cull::specs()); v.extend(convert::specs()); v.extend(convert::edit_specs()); @@ -153,10 +158,12 @@ pub fn command_specs() -> &'static [CommandSpec] { v.extend(xmp::specs()); v.extend(preset_files::specs()); v.extend(prefs::specs()); + v.extend(slideshow::specs()); v.extend(export::specs()); v.extend(before::specs()); v.extend(browse::specs()); v.extend(missing::specs()); + v.extend(recheck::specs()); v.extend(sync::specs()); v.extend(metadata::specs()); v.extend(geocode::specs()); @@ -187,6 +194,15 @@ pub(crate) fn bad(cmd: &str, msg: impl Into) -> EngineError { pub(crate) fn f64_req(p: &Value, key: &str, cmd: &str) -> Result { p.get(key).and_then(Value::as_f64).ok_or_else(|| bad(cmd, format!("missing number `{key}`"))) } +/// A non-negative integer parameter: `index: -1` must be an error, not element 0 (`-1.0 as usize` +/// saturates to 0), just as `history.restore` already refuses a negative step index. +pub(crate) fn index_req(p: &Value, key: &str, cmd: &str) -> Result { + let i = f64_req(p, key, cmd)?; + if !(i.is_finite() && i >= 0.0) { + return Err(bad(cmd, format!("`{key}` must be a non-negative integer"))); + } + Ok(i as usize) +} pub(crate) fn f64_or(p: &Value, key: &str, d: f64) -> f64 { p.get(key).and_then(Value::as_f64).unwrap_or(d) } diff --git a/crates/engine/src/cmd/prefs.rs b/crates/engine/src/cmd/prefs.rs index 78de9d850..1f1301a51 100644 --- a/crates/engine/src/cmd/prefs.rs +++ b/crates/engine/src/cmd/prefs.rs @@ -43,6 +43,9 @@ fn prefs(s: &mut Session, p: &Value) -> Result { if let Some(v) = i.get("perCamera").and_then(Value::as_bool) { d.per_camera = v; } + if let Some(v) = i.get("showOnCard").and_then(Value::as_bool) { + d.show_on_card = v; + } if let Some(v) = i.get("copyright").and_then(Value::as_str) { d.copyright = v.trim().to_string(); } @@ -107,7 +110,7 @@ pub fn specs() -> Vec { "Library Preferences", [], None, - "{import?: {rawPreset?: presetId|\"default\", otherPreset?: presetId|\"default\", perCamera?: bool, cameras?: [{camera: \"Make Model\", preset: presetId|null}], copyright?: text, creator?: text (given to imported photos without one)}, camera?: {camera, preset?, remove?: bool}, cacheMb?: n (0 = default), forgetLocalDays?: n (forget untouched Local photos of folders not browsed for n days; 0 = never)} — develop defaults applied on import (raws / other images / per camera) and the thumbnail cache size, saved with the library → {xmp, import, cacheMb, forgetLocalDays, persistent}", + "{import?: {rawPreset?: presetId|\"default\", otherPreset?: presetId|\"default\", perCamera?: bool, cameras?: [{camera: \"Make Model\", preset: presetId|null}], copyright?: text, creator?: text (given to imported photos without one), showOnCard?: bool (open the import review when a camera or card is connected)}, camera?: {camera, preset?, remove?: bool}, cacheMb?: n (0 = default), forgetLocalDays?: n (forget untouched Local photos of folders not browsed for n days; 0 = never)} — develop defaults applied on import (raws / other images / per camera) and the thumbnail cache size, saved with the library → {xmp, import, cacheMb, forgetLocalDays, persistent}", always, prefs )] diff --git a/crates/engine/src/cmd/preset_files.rs b/crates/engine/src/cmd/preset_files.rs index fc385f34c..f65c0e374 100644 --- a/crates/engine/src/cmd/preset_files.rs +++ b/crates/engine/src/cmd/preset_files.rs @@ -2,6 +2,7 @@ use serde_json::{Value, json}; +use super::mask_effect_files as mef; use super::{CommandSpec, always, bad, cmd, str_param}; use crate::preset_import::{group_from_dir, read_presets}; use crate::presets::{LCPRESET_EXT, expand_preset_paths, to_lcpreset}; @@ -18,6 +19,10 @@ fn import(s: &mut Session, p: &Value) -> Result { let mut imported = Vec::new(); let mut failed = Vec::new(); let mut skipped = 0usize; + // local adjustment (brush) presets — Lightroom's .lrtemplate, Camera Raw's local .xmp, our + // .lceffect, also inside zips — become mask effects + let mut effects = Vec::new(); + let mut effect_failed = Vec::new(); for top in &paths { let from_dir = std::path::Path::new(top).is_dir(); for f in expand_preset_paths(std::slice::from_ref(top)) { @@ -25,8 +30,27 @@ fn import(s: &mut Session, p: &Value) -> Result { // the path from the imported folder (inclusive) to the file's folder let base = std::path::Path::new(top).parent().unwrap_or(std::path::Path::new("")); let rel = std::path::Path::new(&f).parent().and_then(|d| d.strip_prefix(base).ok()).map(|d| d.to_string_lossy().to_string()); - let dir_group = rel.filter(|_| from_dir).and_then(|d| group_from_dir(&d)); - let parsed = std::fs::read(&f).map_err(|e| e.to_string()).and_then(|b| read_presets(&f, &b, dir_group)); + let rel = rel.filter(|_| from_dir); + let bytes = match mef::read_capped(&f) { + Ok(b) => b, + Err(e) => { + failed.push(json!([f, e])); + continue; + } + }; + let lower = f.to_ascii_lowercase(); + let maybe_local = [".lrtemplate", ".xmp", ".zip", ".lceffect"].iter().any(|x| lower.ends_with(x)); + let before = effects.len() + effect_failed.len(); + let local = maybe_local && mef::found_in(&f, &bytes, rel.as_deref().and_then(mef::group_for_dir), &mut effects, &mut effect_failed); + // a zip may hold develop presets as well + if local && !lower.ends_with(".zip") { + continue; + } + let dir_group = rel.and_then(|d| group_from_dir(&d)); + let parsed = read_presets(&f, &bytes, dir_group); + if local && parsed.is_err() && effects.len() + effect_failed.len() > before { + continue; + } match parsed { Ok(list) => { for mut it in list { @@ -45,7 +69,9 @@ fn import(s: &mut Session, p: &Value) -> Result { } } } - Ok(json!({"imported": imported, "skipped": skipped, "failed": failed})) + failed.extend(effect_failed.into_iter().map(|(f, e)| json!([f, e]))); + let (effects, dividers) = mef::add_found(s, effects, group.as_deref(), dry)?; + Ok(json!({"imported": imported, "skipped": skipped, "failed": failed, "effects": effects, "effectDividers": dividers})) } fn export(s: &mut Session, p: &Value) -> Result { @@ -69,10 +95,9 @@ fn export(s: &mut Session, p: &Value) -> Result { if chosen.is_empty() { return Err(bad("preset.export", "no presets to export (create one first, or pass ids/group)")); } - if let Some(dir) = path.parent().filter(|d| !d.as_os_str().is_empty()) { - std::fs::create_dir_all(dir).map_err(|e| crate::EngineError::Other(format!("{}: {e}", dir.display())))?; - } - std::fs::write(&path, to_lcpreset(&chosen)).map_err(|e| crate::EngineError::Other(format!("{}: {e}", path.display())))?; + let target = path.to_string_lossy(); + s.check_write_target(&target).map_err(|e| bad("preset.export", e))?; + crate::export::write_file_durable(&target, to_lcpreset(&chosen).as_bytes()).map_err(crate::EngineError::Other)?; Ok(json!({"path": path.display().to_string(), "count": chosen.len()})) } @@ -83,7 +108,7 @@ pub fn specs() -> Vec { "Import Presets", [], None, - "{paths: [file or folder], group?, dryRun?} — .lcpreset, XMP presets, .lrtemplate, photos carrying edits (DNG/JPEG/TIFF \"DNG presets\"), Luminar looks (.lmp, .mplumpack collections) and .zip bundles of these; folders give their name as group; crs: fields mapped per docs/xmp-interop.md → {imported: [{id,name,group,file,unmapped}], skipped, failed}", + "{paths: [file or folder], group?, dryRun?} — .lcpreset, XMP presets, .lrtemplate, photos carrying edits (DNG/JPEG/TIFF \"DNG presets\"), Luminar looks (.lmp, .mplumpack collections) and .zip bundles of these; folders give their name as group; crs: fields mapped per docs/xmp-interop.md; local adjustment presets (Lightroom .lrtemplate, Camera Raw local .xmp, .lceffect) become mask effects (`mask.effects`; divider presets become their groups) → {imported: [{id,name,group,file,unmapped}], skipped, failed, effects: [{name,group,file,unmapped}], effectDividers: [{name,group,file}]}", always, import ), diff --git a/crates/engine/src/cmd/recheck.rs b/crates/engine/src/cmd/recheck.rs new file mode 100644 index 000000000..494426b90 --- /dev/null +++ b/crates/engine/src/cmd/recheck.rs @@ -0,0 +1,129 @@ +//! Preview-only raws rechecked after an update (issue #662): a raw imported while its format or +//! variant couldn't be decoded keeps [`Photo::preview_only`] until something re-reads its file. +//! Each flag is stamped with the app version that set it ([`Photo::preview_checked`]); when a +//! library opens in another version, its stale preview-only photos are probed again and get +//! exactly what Reload from Disk would give them: a file that decodes now loses the flag, one that +//! still doesn't gets the new reason and a fresh stamp, so it isn't probed again. +//! +//! The work is split so the app can probe on a worker thread: [`stale`] lists what to probe (no +//! file-system calls), [`probe`] reads the files without the session, and [`apply`] applies the +//! results as LightCraft's own bookkeeping (journaled, not an undo step). Agents and tests use +//! `library.recheckPreviewOnly`, which does all three at once. + +use lightcraft_catalog::{Catalog, Op, Photo, PhotoId}; +use serde_json::{Value, json}; + +use super::{CommandSpec, always, bad, cmd}; +use crate::media::{FileProbe, ProbeInfo}; +use crate::{Result, Session}; + +/// A preview-only photo to probe, and what the library knew about it when it was listed (so a +/// result for a photo that changed meanwhile is dropped). +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct Stale { + pub id: PhotoId, + pub path: String, + preview_only: Option, + preview_checked: String, +} + +impl Stale { + fn of(p: &Photo, path: &str) -> Stale { + Stale { id: p.id, path: path.to_string(), preview_only: p.preview_only.clone(), preview_checked: p.preview_checked.clone() } + } + + /// The photo is still as it was listed: same file, flag and stamp. + fn current(&self, p: &Photo) -> bool { + crate::cmd::missing::checked_path(p) == Some(self.path.as_str()) + && p.preview_only == self.preview_only + && p.preview_checked == self.preview_checked + } +} + +/// The library's preview-only photos whose flag another app version set (or every preview-only +/// photo, with `all`): library photos with a file, not virtual copies (they follow the photo they +/// were copied from). No file-system calls. +pub fn stale(cat: &Catalog, all: bool) -> Vec { + cat.photos() + .filter(|p| p.copy_of.is_none() && p.preview_only.is_some() && (all || p.preview_only_stale())) + .filter_map(|p| crate::cmd::missing::checked_path(p).map(|path| Stale::of(p, path))) + .collect() +} + +/// The session's file probe, to [`probe`] with on a worker thread; `None` when the session has no +/// file access (a demo or browser session), where nothing can be rechecked. +pub fn file_probe(s: &Session) -> Option { + s.media.file_probe.clone() +} + +/// Probe each listed photo's file as Reload would. Doesn't touch the session. +pub fn probe(probe: &FileProbe, stale: Vec) -> Vec<(Stale, std::result::Result)> { + let paths: Vec = stale.iter().map(|x| x.path.clone()).collect(); + stale.into_iter().zip(crate::import::probe_with(probe, &paths)).collect() +} + +/// What a recheck changed. +#[derive(Clone, Debug, Default, PartialEq, Eq)] +pub struct Rechecked { + /// Photos brought up to date (virtual copies included). + pub updated: Vec, + /// Of those, the ones that develop from their raw data now. + pub cleared: Vec, +} + +impl Rechecked { + pub fn to_json(&self) -> Value { + json!({"updated": self.updated.iter().map(|i| i.0).collect::>(), "cleared": self.cleared.iter().map(|i| i.0).collect::>()}) + } +} + +/// Apply [`probe`]'s results: each photo gets the ops Reload would give it, all in one batch that is +/// journaled but isn't an undo step. A file that can't be read (missing, offline) changes nothing, +/// so it is tried again next time; a photo that changed since it was listed (relinked, reloaded, +/// removed) is skipped. +pub fn apply(s: &mut Session, results: Vec<(Stale, std::result::Result)>) -> Result { + let mut ops = Vec::new(); + let mut out = Rechecked::default(); + for (item, info) in results { + let Ok(info) = info else { continue }; + if !s.catalog.photo(item.id).is_some_and(|p| item.current(p)) { + continue; + } + let first = out.updated.len(); + crate::cmd::convert::reload_ops(&s.catalog, item.id, &info, &mut ops, &mut out.updated); + if info.preview_only.is_none() { + out.cleared.extend_from_slice(&out.updated[first..]); + } + } + if !ops.is_empty() { + s.apply_system(Op::Batch { ops })?; + } + for id in &out.updated { + s.media.forget(*id); + } + Ok(out) +} + +fn recheck(s: &mut Session, p: &Value) -> Result { + const C: &str = "library.recheckPreviewOnly"; + let all = p.get("all").and_then(Value::as_bool).unwrap_or(false); + // without file access every probe "succeeds" with nothing known, which would clear every flag + let fp = file_probe(s).ok_or_else(|| bad(C, "this session has no file access"))?; + let listed = stale(&s.catalog, all); + let n = listed.len(); + let mut r = apply(s, probe(&fp, listed))?.to_json(); + r["checked"] = json!(n); + Ok(r) +} + +pub fn specs() -> Vec { + vec![cmd!( + "library.recheckPreviewOnly", + "Recheck Preview-Only Raws", + [], + None, + "{all?: bool} — probe again the raws shown from their embedded preview whose flag another LightCraft version set (the app does this in the background when a library opens; `all` rechecks every preview-only raw, e.g. when testing a decoder build of the same version), and bring each up to date as Reload from Disk would; not an undo step, develop settings and history are kept; files that can't be read are left for next time → {checked, updated: [ids], cleared: [ids] (develop from their raw data now)}", + always, + recheck + )] +} diff --git a/crates/engine/src/cmd/slideshow.rs b/crates/engine/src/cmd/slideshow.rs new file mode 100644 index 000000000..33ff76ec0 --- /dev/null +++ b/crates/engine/src/cmd/slideshow.rs @@ -0,0 +1,513 @@ +//! `slideshow.playback`: the slideshow's Playback settings (slide and fade durations, fade colour, +//! random order, repeat, preparing previews in advance). `slideshow.layout`: how a slide looks +//! (Layout, Options and Backdrop: margins, aspect preview, zoom to fill, stroke, shadow, backdrop +//! colour, colour wash, background image). Both are library preferences, saved in the library's +//! `prefs.json`; the app's slideshow (View ▸ Slideshow) plays with them. `slideshow.overlays`: +//! the identity plate, rating stars, text overlays with tokens and the watermark drawn on every +//! slide (also a library preference). `slideshow.titles`: the intro and ending screens (colour, +//! identity plate or text, scale, duration; a library preference). `slideshow.preview` composes +//! one slide, or a title screen, as a PNG (with the library's settings, or a template's). +//! `slideshow.templates.*`: the Template Browser (built-in and user templates; see +//! [`crate::slide_template`]). `slideshow.export`: the slideshow as a PDF, a JPEG sequence or an +//! AV1 video, in the background ([`crate::slideshow_export`]); `slideshow.exportStatus` reads how +//! it is going. + +use lightcraft_catalog::PhotoId; +use serde_json::{Value, json}; + +use super::{CommandSpec, always, bad, cmd, str_param}; +use crate::slide_template::{self as tpl, SlideLook}; +use crate::{Result, Session}; + +/// Largest side `slideshow.preview` renders. +pub const PREVIEW_MAX: usize = 4096; + +fn playback(s: &mut Session, p: &Value) -> Result { + let changes = p.as_object().is_some_and(|o| !o.is_empty()); + if changes { + let next = s.slideshow.merged(p).map_err(|e| bad("slideshow.playback", e))?; + if next != s.slideshow { + s.slideshow = next; + s.save_prefs()?; + } + } + Ok(s.slideshow.to_json()) +} + +fn layout(s: &mut Session, p: &Value) -> Result { + let changes = p.as_object().is_some_and(|o| !o.is_empty()); + if changes { + let next = s.slide_layout.merged(p).map_err(|e| bad("slideshow.layout", e))?; + if next != s.slide_layout { + s.slide_layout = next; + s.save_prefs()?; + } + } + Ok(s.slide_layout.to_json()) +} + +fn overlays(s: &mut Session, p: &Value) -> Result { + let changes = p.as_object().is_some_and(|o| !o.is_empty()); + if changes { + let next = s.slide_overlays.merged(p).map_err(|e| bad("slideshow.overlays", e))?; + if next != s.slide_overlays { + s.slide_overlays = next; + s.save_prefs()?; + } + } + let mut out = s.slide_overlays.to_json(); + out["warnings"] = json!(s.slide_overlays.warnings()); + Ok(out) +} + +fn titles(s: &mut Session, p: &Value) -> Result { + let changes = p.as_object().is_some_and(|o| !o.is_empty()); + if changes { + let next = s.slide_titles.merged(p).map_err(|e| bad("slideshow.titles", e))?; + if next != s.slide_titles { + s.slide_titles = next; + s.save_prefs()?; + } + } + let mut out = s.slide_titles.to_json(); + out["warnings"] = json!(s.slide_titles.warnings(&s.slide_overlays)); + Ok(out) +} + +/// A slide size from the request: both sides given, one (the other from the aspect preview), or +/// neither (1280 wide). Each side 16–[`PREVIEW_MAX`]. +fn preview_size(layout: &lightcraft_slideshow::SlideSettings, p: &Value) -> Result<(usize, usize)> { + const C: &str = "slideshow.preview"; + let side = |k: &str| -> Result> { + match p.get(k) { + None | Some(Value::Null) => Ok(None), + Some(v) => match v.as_u64() { + Some(n) if (16..=PREVIEW_MAX as u64).contains(&n) => Ok(Some(n as usize)), + _ => Err(bad(C, format!("`{k}` must be a whole number of pixels from 16 to {PREVIEW_MAX}, not {v}"))), + }, + } + }; + let ratio = layout.aspect.ratio(f64::NAN); + let clamp = |v: f64| (v.round() as usize).clamp(16, PREVIEW_MAX); + Ok(match (side("width")?, side("height")?) { + (Some(w), Some(h)) => (w, h), + (Some(w), None) => (w, clamp(w as f64 / ratio)), + (None, Some(h)) => (clamp(h as f64 * ratio), h), + (None, None) => (1280, clamp(1280.0 / ratio)), + }) +} + +/// Write `png` to `path`, or put it in the reply as base64. +fn deliver(png: &[u8], path: Option<&str>, out: &mut Value) -> Result<()> { + match path { + Some(path) => { + crate::export::write_file(path, png).map_err(crate::EngineError::Other)?; + out["path"] = json!(path); + } + None => out["png"] = json!(base64(png)), + } + Ok(()) +} + +fn encode_png(img: &lightcraft_raster::Rgba8) -> Result> { + lightcraft_codecs::encode_png(&lightcraft_codecs::EncodeImage::rgba8(img), &lightcraft_codecs::EncodeMeta::default()) + .map_err(|e| crate::EngineError::Other(format!("PNG: {e}"))) +} + +/// `slideshow.preview {title}`: a title screen (whether or not it is switched on). +fn preview_title(s: &mut Session, p: &Value, title: &Value, look: &SlideLook) -> Result { + const C: &str = "slideshow.preview"; + let t = title + .as_str() + .and_then(crate::slideshow::Title::parse) + .ok_or_else(|| bad(C, format!("`title` must be \"intro\" or \"ending\", not {title}")))?; + if p.get("id").is_some_and(|v| !v.is_null()) { + return Err(bad(C, "pass `id` or `title`, not both")); + } + let (w, h) = preview_size(&look.layout, p)?; + let path = str_param(p, "path").map(str::trim).filter(|x| !x.is_empty()); + if let Some(path) = path { + s.check_write_target(path).map_err(|e| bad(C, e))?; + } + let c = crate::slide_title::TitleCompose::new(&look.titles, &look.overlays, t, w, h); + let img = c.compose(); + let png = encode_png(&img)?; + let rect = |r: lightcraft_slideshow::Rect| json!([r.x0, r.y0, r.x1, r.y1]); + let mut out = json!({ + "title": t.id(), + "enabled": look.titles.screen(t).enabled, + "width": img.width, + "height": img.height, + "plate": c.plate_box().map(rect), + }); + deliver(&png, path, &mut out)?; + Ok(out) +} + +fn preview(s: &mut Session, p: &Value) -> Result { + const C: &str = "slideshow.preview"; + // a template's look (the Template Browser's hover preview), else the library's settings + let template = match p.get("template") { + None | Some(Value::Null) => None, + Some(Value::String(id)) => Some(s.slide_template(id.trim()).map_err(|e| bad(C, e))?), + Some(v) => return Err(bad(C, format!("`template` must be a template id, not {v}"))), + }; + let look = template.as_ref().map_or_else(|| SlideLook::of(s), |t| t.look.clone()); + if let Some(t) = p.get("title").filter(|v| !v.is_null()) { + let mut out = preview_title(s, p, t, &look)?; + if let Some(t) = &template { + out["template"] = json!(t.id); + } + return Ok(out); + } + let id = match p.get("id") { + None | Some(Value::Null) => s.active().ok_or_else(|| bad(C, "no photo selected (select one or pass `id`)"))?, + Some(v) => PhotoId(v.as_u64().ok_or_else(|| bad(C, format!("`id` must be a photo id, not {v}")))?), + }; + if s.catalog.photo(id).is_none() { + return Err(bad(C, format!("no photo {}", id.0))); + } + let (w, h) = preview_size(&look.layout, p)?; + let path = str_param(p, "path").map(str::trim).filter(|x| !x.is_empty()); + if let Some(path) = path { + s.check_write_target(path).map_err(|e| bad(C, e))?; + } + let settings = look.layout.clone(); + let img = s.slide_now_look(&settings, &look.overlays, id, w, h).map_err(crate::EngineError::Other)?; + let png = encode_png(&img)?; + let aspect = s.slide_photo_aspect(id).unwrap_or(1.5); + let l = lightcraft_slideshow::layout(&settings, aspect, img.width, img.height); + let rect = |r: lightcraft_slideshow::Rect| json!([r.x0, r.y0, r.x1, r.y1]); + let mut out = json!({ + "id": id.0, + "width": img.width, + "height": img.height, + "cell": rect(l.cell), + "photo": rect(l.visible), + }); + let photo = s.catalog.photo(id).cloned(); + let o = crate::slide_overlay::SlideOverlays::resolve(&look.overlays, photo.as_deref()); + out["overlays"] = o.boxes(&l).into_iter().map(|(k, r)| json!({"kind": k.id(), "box": rect(r)})).collect(); + if let Some(t) = &template { + out["template"] = json!(t.id); + } + deliver(&png, path, &mut out)?; + Ok(out) +} + +/// Standard padded base64 (for an image returned in the reply). +fn base64(bytes: &[u8]) -> String { + const A: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + let mut out = String::with_capacity(bytes.len().div_ceil(3) * 4); + for chunk in bytes.chunks(3) { + let b = [chunk.first().copied().unwrap_or(0), chunk.get(1).copied().unwrap_or(0), chunk.get(2).copied().unwrap_or(0)]; + let n = (b[0] as u32) << 16 | (b[1] as u32) << 8 | b[2] as u32; + let at = |shift: u32| A.get((n >> shift) as usize & 63).copied().unwrap_or(b'=') as char; + out.push(at(18)); + out.push(at(12)); + out.push(if chunk.len() > 1 { at(6) } else { '=' }); + out.push(if chunk.len() > 2 { at(0) } else { '=' }); + } + out +} + +// ------------------------------------------------------------------------------- templates + +/// The settings as the `slideshow.*` commands return them (for template replies). +fn settings_json(s: &Session) -> Value { + json!({ + "playback": s.slideshow.to_json(), + "layout": s.slide_layout.to_json(), + "overlays": s.slide_overlays.to_json(), + "titles": s.slide_titles.to_json(), + }) +} + +fn tpl_list(s: &mut Session, _p: &Value) -> Result { + let l = s.slide_templates(); + Ok(json!({ + "templates": l.templates.iter().map(|t| t.info()).collect::>(), + "previous": s.slide_templates_previous.is_some(), + "folder": s.slide_templates_dir.as_ref().map(|d| d.to_string_lossy().to_string()), + "problems": l.problems, + })) +} + +/// The template `id` or `name` names. +fn named(s: &mut Session, p: &Value, c: &str) -> Result> { + s.slide_template_named(str_param(p, "id"), str_param(p, "name")).map_err(|e| bad(c, e)) +} + +fn tpl_apply(s: &mut Session, p: &Value) -> Result { + const C: &str = "slideshow.templates.apply"; + let t = named(s, p, C)?; + let warnings = s.apply_slide_look(&t.look)?; + let mut out = json!({"applied": t.id, "name": t.name, "warnings": warnings, "previous": s.slide_templates_previous.is_some()}); + out["settings"] = settings_json(s); + Ok(out) +} + +fn tpl_reply(t: &tpl::Template) -> Value { + t.info() +} + +fn tpl_save(s: &mut Session, p: &Value) -> Result { + const C: &str = "slideshow.templates.save"; + let name = str_param(p, "name").ok_or_else(|| bad(C, "missing `name`"))?; + let replace = match p.get("replace") { + None | Some(Value::Null) => false, + Some(v) => v.as_bool().ok_or_else(|| bad(C, format!("`replace` must be true or false, not {v}")))?, + }; + let t = s.save_slide_template(name, replace).map_err(|e| bad(C, e))?; + Ok(tpl_reply(&t)) +} + +fn id_param<'a>(p: &'a Value, c: &str) -> Result<&'a str> { + str_param(p, "id").map(str::trim).filter(|x| !x.is_empty()).ok_or_else(|| bad(c, "missing `id` (a template id from slideshow.templates.list)")) +} + +fn tpl_update(s: &mut Session, p: &Value) -> Result { + const C: &str = "slideshow.templates.update"; + let id = id_param(p, C)?; + let t = s.update_slide_template(id).map_err(|e| bad(C, e))?; + Ok(tpl_reply(&t)) +} + +fn tpl_rename(s: &mut Session, p: &Value) -> Result { + const C: &str = "slideshow.templates.rename"; + let id = id_param(p, C)?; + let name = str_param(p, "name").ok_or_else(|| bad(C, "missing `name`"))?; + let t = s.rename_slide_template(id, name).map_err(|e| bad(C, e))?; + Ok(tpl_reply(&t)) +} + +fn tpl_delete(s: &mut Session, p: &Value) -> Result { + const C: &str = "slideshow.templates.delete"; + let id = id_param(p, C)?; + let t = s.delete_slide_template(id).map_err(|e| bad(C, e))?; + Ok(json!({"deleted": t.id, "name": t.name})) +} + +fn tpl_import(s: &mut Session, p: &Value) -> Result { + const C: &str = "slideshow.templates.import"; + let path = str_param(p, "path").map(str::trim).filter(|x| !x.is_empty()).ok_or_else(|| bad(C, "missing `path`"))?; + let (t, renamed) = s.import_slide_template(std::path::Path::new(path)).map_err(|e| bad(C, e))?; + let mut out = tpl_reply(&t); + out["renamed"] = json!(renamed); + out["warnings"] = json!(t.look.warnings(s)); + Ok(out) +} + +fn tpl_export(s: &mut Session, p: &Value) -> Result { + const C: &str = "slideshow.templates.export"; + let id = id_param(p, C)?; + let path = str_param(p, "path").map(str::trim).filter(|x| !x.is_empty()).ok_or_else(|| bad(C, "missing `path`"))?; + let t = s.slide_template(id).map_err(|e| bad(C, e))?; + s.check_write_target(path).map_err(|e| bad(C, e))?; + let bytes = serde_json::to_vec_pretty(&t.to_doc()).map_err(|e| crate::EngineError::Other(e.to_string()))?; + crate::export::write_file(path, &bytes).map_err(crate::EngineError::Other)?; + Ok(json!({"id": t.id, "name": t.name, "path": path})) +} + +/// `slideshow.export`: prepare on this thread, run on a worker (or here with `wait`). +fn export(s: &mut Session, p: &Value) -> Result { + const C: &str = "slideshow.export"; + if cfg!(target_arch = "wasm32") { + return Err(bad(C, "slideshow export needs the desktop app or lightcraft-cli")); + } + if s.slideshow_export.as_ref().is_some_and(|r| r.running()) { + return Err(bad(C, "a slideshow export is already running (activity.cancel stops it)")); + } + let prepared = crate::slideshow_export::prepare(s, p).map_err(|e| bad(C, e))?; + let wait = p.get("wait").and_then(Value::as_bool).unwrap_or(false); + let cancel = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false)); + let task = s.activity.start("export", "Exporting slideshow", crate::activity::Cancel::Flag(cancel.clone())); + let total = prepared.total(); + task.progress(0, total); + task.detail(&prepared.request.path); + let settings = &prepared.request.settings; + let run = std::sync::Arc::new(crate::slideshow_export::ExportRun { + task_id: task.id(), + format: settings.format.id().into(), + path: prepared.request.path.clone(), + total: total.into(), + cancel, + ..Default::default() + }); + s.slideshow_export = Some(run.clone()); + let started = json!({ + "taskId": task.id(), + "background": !wait, + "format": settings.format.id(), + "path": prepared.request.path, + "slides": prepared.len(), + "seconds": prepared.seconds(), + "total": total, + "width": prepared.width, + "height": prepared.height, + "warnings": prepared.warnings, + }); + if wait { + crate::slideshow_export::run_to_end(prepared, &run, &task); + drop(task); + return match run.result() { + Some(Ok(v)) => Ok(v), + Some(Err(e)) => Err(bad(C, e)), + None => Err(bad(C, "the export did not finish")), + }; + } + #[cfg(not(target_arch = "wasm32"))] + { + // the row goes when the worker ends, however it ends + let r = run.clone(); + std::thread::Builder::new() + .name("lc-slideshow-export".into()) + .spawn(move || crate::slideshow_export::run_to_end(prepared, &r, &task)) + .map_err(|e| bad(C, format!("could not start: {e}")))?; + } + Ok(started) +} + +fn export_status(s: &mut Session, _: &Value) -> Result { + Ok(s.slideshow_export.as_ref().map_or_else(|| json!({"running": false}), |r| r.json())) +} + +pub fn specs() -> Vec { + vec![ + cmd!( + "slideshow.playback", + "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}", + always, + playback + ), + cmd!( + "slideshow.layout", + "Slideshow Layout Settings", + [], + None, + "{margins?: all four, marginLeft?, marginRight?, marginTop?, marginBottom?: fractions of the slide's shorter side (0–0.45; with linkMargins one sets all four), linkMargins?: bool, showGuides?: bool (margin guides in the slide editor), aspect?: \"screen\"|\"16:9\"|\"4:3\" (aspect preview), zoomToFill?: bool (crop the photo to fill its cell), strokeBorder?: bool, strokeColor?: \"#rrggbb\"|[r,g,b], strokeWidth?: fraction of the shorter side (0–0.05), castShadow?: bool, shadowOpacity?: 0–1, shadowOffset?: 0–0.1, shadowRadius?: 0–0.1 (fractions of the shorter side), shadowAngle?: degrees counter-clockwise from the right (−45 = down right), backdropColor?: colour, colorWash?: bool, washColor?: colour, washOpacity?: 0–1, washAngle?: degrees (the side the wash colour is full at), backgroundImage?: photo id|null, backgroundOpacity?: 0–1} — omitted keys keep their value, {} reads them; out-of-range numbers are clamped, wrong types refused; saved with the library → the settings", + always, + layout + ), + cmd!( + "slideshow.overlays", + "Slideshow Overlay Settings", + [], + None, + "{identityPlate?: {enabled?, text?, image?: PNG/JPEG file (drawn instead of the text; \"\" = the text), size?: height as a fraction of the slide's shorter side (0.005–0.5), color?: \"#rrggbb\"|[r,g,b], opacity?: 0–1, …placement, behind?: bool (render behind the photo)}, ratingStars?: {enabled?, color?, size?: a star's height (0.005–0.15), …placement}, texts?: [text overlay] (replaces the list, at most 16; each entry merged onto the one at its index), addText?: text overlay ({} = the default \"{file}\"), removeText?: index, showWatermark?: bool, watermark?: text | the export watermark object {text, vertical, size, opacity, anchor, inset, color, shadow, image, imageWidth} (merged; sizes relative to the photo)} where a text overlay is {text?: template with tokens ({file}, {name}, {title}, {caption}, {date} (YYYY-MM-DD) / {date:%d.%m.%Y}, {exposure}, {shutter}, {aperture}, {focal}, {iso}, {camera}, {lens}, {rating}, {creator}, {copyright}, {folder}, {ext}; unknown tags show as typed), size?: line height (0.005–0.3), color?, opacity?, …placement, shadow?: bool, shadowOpacity?: 0–1, shadowOffset?, shadowRadius?: 0–0.05 (fractions of the shorter side), shadowAngle?: degrees counter-clockwise from the right} and the placement keys are anchor?: topLeft|top|topRight|left|center|right|bottomLeft|bottom|bottomRight, offsetX?, offsetY?: fractions of the slide's shorter side (x right, y down; −1–1), relativeTo?: \"slide\"|\"photo\" (the overlay is kept on the slide) — omitted keys keep their value, {} reads them; out-of-range numbers are clamped, wrong types refused; saved with the library; slideshow playback and slideshow.preview draw them → the settings and warnings (an unreadable identity plate image, unknown tokens)", + always, + overlays + ), + cmd!( + "slideshow.titles", + "Slideshow Title Settings", + [], + None, + "{intro?: screen, ending?: screen} where a screen is {enabled?: bool, color?: background \"#rrggbb\"|[r,g,b], content?: \"none\"|\"identityPlate\" (the Overlays identity plate, its text or image)|\"text\", text?: the text shown with content \"text\", overrideColor?: bool (draw the plate or text in textColor instead of the identity plate's colour; an image plate keeps its colours), textColor?: colour, scale?: height of the plate or text line as a fraction of the slide's shorter side (0.005–0.5; text wider than the slide shrinks to fit), duration?: seconds (0.5–120) | null (the slide duration)} — the intro plays once before the first slide, the ending after the last one when repeat is off; both fade with the playback fade; random order never moves them. Omitted keys keep their value, {} reads them; out-of-range numbers are clamped, wrong types refused; saved with the library → the settings and warnings (an unreadable identity plate image, a screen with nothing to show)", + always, + titles + ), + cmd!( + query "slideshow.templates.list", + "Slideshow Templates", + [], + None, + "{} → {templates: [{id: \"builtin:…\"|\"user:…\", name, builtin, group: \"builtin\"|\"user\", description?, path? (a user template's file)}], previous: bool (the settings before the last apply can be restored with apply {id: \"previous\"}), folder: the user templates folder | null, problems: [\"file: why it was skipped\"]} — built-ins first, then user templates by name (files in /slideshow-templates, at most 1000)", + always, + tpl_list + ), + cmd!( + "slideshow.templates.apply", + "Apply Slideshow Template", + [], + None, + "{id: template id | \"previous\" (the settings before the last apply) | name: template name (case-insensitive)} — replaces the playback (prepare previews kept), layout, overlays and titles settings with the template's and saves them with the library; there is no undo: the replaced settings are kept as \"previous\" for this session → {applied, name, warnings: [a background photo not in this library, an unreadable identity plate / watermark image…], previous, settings: {playback, layout, overlays, titles}}", + always, + tpl_apply + ), + cmd!( + "slideshow.templates.save", + "Save Slideshow Template", + [], + None, + "{name: 1–80 characters, replace?: bool (over a user template of that name)} — the current playback, layout, overlays and titles settings as a user template, /slideshow-templates/.json (path separators and other reserved characters become _); a name taken (case-insensitive) or a built-in's is refused unless replace → {id, name, builtin, group, path}", + always, + tpl_save + ), + cmd!( + "slideshow.templates.update", + "Update Slideshow Template", + [], + None, + "{id: user template} — replace its settings with the current ones (name kept); built-ins can't change → {id, name, builtin, group, path}", + always, + tpl_update + ), + cmd!( + "slideshow.templates.rename", + "Rename Slideshow Template", + [], + None, + "{id: user template, name} — its file follows the name; a name taken is refused → {id (the new one), name, builtin, group, path}", + always, + tpl_rename + ), + cmd!( + "slideshow.templates.delete", + "Delete Slideshow Template", + [], + None, + "{id: user template} — deletes its file → {deleted, name}", + always, + tpl_delete + ), + cmd!( + "slideshow.templates.import", + "Import Slideshow Template", + [], + None, + "{path: a template .json file (at most 256 KB)} — checked like a saved one (format, version, every setting) and saved as a user template under its name (else the file's); a name taken gets \" (2)\"… → {id, name, builtin, group, path, renamed, warnings}", + always, + tpl_import + ), + cmd!( + "slideshow.templates.export", + "Export Slideshow Template", + [], + None, + "{id: template (built-in or user), path: .json file to write} → {id, name, path}", + always, + tpl_export + ), + cmd!( + query "slideshow.preview", + "Slideshow Slide Preview", + [], + None, + "{id?: photo (default: the active one) | title?: \"intro\"|\"ending\", template?: a template id (slideshow.templates.list): compose with its settings instead of the library's, which don't change (the Template Browser's hover preview), width?, height?: pixels (16–4096; one given: the other from the aspect preview; neither: 1280 wide), path?: write the PNG here} — the photo's slide composed with the slideshow layout and overlays → {id, width, height, cell: [x0,y0,x1,y1], photo: [x0,y0,x1,y1] (the visible photo, slide pixels), overlays: [{kind: identityPlate|ratingStars|textN|watermark, box: [x0,y0,x1,y1]}], path | png (base64 PNG)}; with `title`, that title screen (`slideshow.titles`; drawn even while it is off) → {title, enabled, width, height, plate: [x0,y0,x1,y1] | null (the plate or text), path | png}", + always, + preview + ), + cmd!( + query "slideshow.export", + "Export Slideshow", + [], + None, + "{format: \"pdf\"|\"jpeg\"|\"video\", path: the .pdf / .mp4 / .webm file, or for jpeg the folder (created if missing), size?: pdf/jpeg the long side in pixels (64–8192, default 1920), video the frame height (64–2160 or \"720p\"/\"1080p\", default 720), quality?: 1–100 (JPEG quality, default 90; video default 75), fps?: 24|25|30|60 (video, default 30), container?: \"mp4\"|\"webm\" (video, default mp4), fullScreen?: bool (pdf: open in the viewer's full screen mode, default true), speed?: 1–10 (video encoder speed, default 10 = fastest), name?: jpeg file name prefix (default the folder's name: -001.jpg…), ids?: photo ids (default the photos in view, in view order), wait?: bool (run here and reply with the result)} — the photos' slides with the slideshow layout, overlays and the intro/ending screens that are on; video is AV1 (no audio), each slide held for its duration and fades rendered as frames, at most 2 hours. Runs in the background with a row in the activity stack (activity.cancel {id: taskId} stops it; nothing is left behind: files are written as .part and renamed when complete); a photo that can't be rendered is skipped with a warning → {taskId, background, format, path, slides, seconds, total, width, height, warnings}; with wait: the result (slideshow.exportStatus)", + always, + export + ), + cmd!( + query "slideshow.exportStatus", + "Slideshow Export Status", + [], + None, + "{} → the last slideshow export: {taskId, format, path, running, done, total (slides, or frames for video), cancelled, result?: {format, path, width, height, slides, skipped, warnings, pages? | files? | frames, samples, keyFrames, seconds, fps, container, codec: \"av1\", audio: false; bytes}, error?} | {running: false} when there was none", + always, + export_status + ), + ] +} diff --git a/crates/engine/src/cmd/xmp.rs b/crates/engine/src/cmd/xmp.rs index cd89aa896..84987b15b 100644 --- a/crates/engine/src/cmd/xmp.rs +++ b/crates/engine/src/cmd/xmp.rs @@ -108,6 +108,18 @@ pub fn specs() -> Vec { always, prefs ), + cmd!( + "library.retrySidecars", + "Write Waiting XMP Sidecars", + [], + None, + "{} — writes again the automatic XMP sidecars that failed (folder offline, read-only or full) whose originals are reachable now → {written, waiting}", + always, + |s, _| { + let written = s.retry_unsaved_sidecars(); + Ok(json!({"written": written, "waiting": s.unsaved_sidecars()})) + } + ), cmd!("library.toggleAutoWriteXmp", "Automatically Write Changes into XMP", ["File"], None, "{} → {autoWrite}", always, |s, _| { let on = !s.xmp.auto_write; prefs(s, &json!({"autoWrite": on})) diff --git a/crates/engine/src/contact_sheet.rs b/crates/engine/src/contact_sheet.rs index e83ad8c67..4dd440265 100644 --- a/crates/engine/src/contact_sheet.rs +++ b/crates/engine/src/contact_sheet.rs @@ -1,4 +1,5 @@ -//! Paginated contact sheets, rendered with the normal export pipeline and packaged as PDF. +//! Paginated contact sheets, rendered with the normal export pipeline and packaged as PDF +//! ([`crate::pdf`]). use crate::export::{Anchor, ExportFormat, ExportOptions, Watermark, draw_watermark, encode_image}; use crate::{Session, media::RenderJob}; use lightcraft_pipeline::{OutputDepth, OutputSpace}; @@ -114,7 +115,7 @@ impl Prepared { let capacity = self.options.columns * self.options.rows; let photos = self.items.len(); let pages = photos.div_ceil(capacity); - let mut pdf = Pdf::new(pages, l.points); + let mut pdf = crate::pdf::Pdf::new(Vec::new())?; let mut page = Rgba8::filled(l.width, l.height, [255; 4]); for (i, (name, job)) in self.items.into_iter().enumerate() { if !progress(i, &name) { @@ -149,17 +150,17 @@ impl Prepared { } if slot + 1 == capacity || i + 1 == photos { let jpg = encode_image(&page, &ExportOptions { format: ExportFormat::Jpeg, quality: 92, ..Default::default() })?; - if pdf.bytes.len().saturating_add(jpg.len()) > 256 * 1024 * 1024 { + if pdf.len().saturating_add(jpg.len()) > 256 * 1024 * 1024 { return Err("contact sheet exceeds 256 MiB; select fewer photos".into()); } - pdf.page(i / capacity, &jpg, l.width, l.height); + pdf.image_page(&jpg, l.width, l.height, l.points, "")?; page.data.fill([255; 4]); } } if !progress(photos, "PDF") { return Err("Contact sheet cancelled".into()); } - Ok(Document { bytes: pdf.finish(), pages, photos }) + Ok(Document { bytes: pdf.finish("")?, pages, photos }) } } @@ -196,67 +197,6 @@ fn blit(dst: &mut Rgba8, src: &Rgba8, x: usize, y: usize) -> Result<(), String> Ok(()) } -// Minimal PDF 1.4: one JPEG XObject per page, explicit byte lengths and classic xref offsets. -struct Pdf { - bytes: Vec, - offsets: Vec, - points: (f64, f64), -} - -impl Pdf { - fn new(pages: usize, points: (f64, f64)) -> Self { - let mut p = Self { bytes: b"%PDF-1.4\n%\xe2\xe3\xcf\xd3\n".to_vec(), offsets: vec![0], points }; - p.object(1, b"<< /Type /Catalog /Pages 2 0 R >>"); - let kids = (0..pages).map(|i| format!("{} 0 R", 3 + i * 3)).collect::>().join(" "); - p.object(2, format!("<< /Type /Pages /Count {pages} /Kids [{kids}] >>").as_bytes()); - p - } - - fn object(&mut self, id: usize, body: &[u8]) { - self.offsets.push(self.bytes.len()); - self.bytes.extend_from_slice(format!("{id} 0 obj\n").as_bytes()); - self.bytes.extend_from_slice(body); - self.bytes.extend_from_slice(b"\nendobj\n"); - } - - fn stream(&mut self, id: usize, dictionary: &str, bytes: &[u8]) { - let mut body = format!("<< {dictionary} /Length {} >>\nstream\n", bytes.len()).into_bytes(); - body.extend_from_slice(bytes); - body.extend_from_slice(b"\nendstream"); - self.object(id, &body); - } - - fn page(&mut self, index: usize, jpg: &[u8], width: usize, height: usize) { - let id = 3 + index * 3; - let (w, h) = self.points; - self.object( - id, - format!( - "<< /Type /Page /Parent 2 0 R /MediaBox [0 0 {w:.2} {h:.2}] /Resources << /XObject << /Im {} 0 R >> >> /Contents {} 0 R >>", - id + 2, - id + 1 - ) - .as_bytes(), - ); - self.stream(id + 1, "", format!("q {w:.2} 0 0 {h:.2} 0 0 cm /Im Do Q").as_bytes()); - self.stream( - id + 2, - &format!("/Type /XObject /Subtype /Image /Width {width} /Height {height} /ColorSpace /DeviceRGB /BitsPerComponent 8 /Filter /DCTDecode"), - jpg, - ); - } - - fn finish(mut self) -> Vec { - let start = self.bytes.len(); - self.bytes.extend_from_slice(format!("xref\n0 {}\n0000000000 65535 f \n", self.offsets.len()).as_bytes()); - for offset in self.offsets.iter().skip(1) { - self.bytes.extend_from_slice(format!("{offset:010} 00000 n \n").as_bytes()); - } - self.bytes.extend_from_slice(format!("trailer\n<< /Size {} /Root 1 0 R >>\nstartxref\n{start}\n%%EOF\n", self.offsets.len()).as_bytes()); - self.bytes - } -} - #[cfg(test)] mod tests { use super::*; diff --git a/crates/engine/src/crs_masks.rs b/crates/engine/src/crs_masks.rs index 483d1778b..f5384e23d 100644 --- a/crates/engine/src/crs_masks.rs +++ b/crates/engine/src/crs_masks.rs @@ -64,13 +64,20 @@ const LOCAL: &[(&str, &str, f64)] = &[ ("LocalClarity", "clarity", 100.0), ]; -fn adjust(c: &XmpValue) -> Value { +/// Is `crs` (without the `crs:` prefix) a local slider field we map? +pub(crate) fn is_local_field(crs: &str) -> bool { + LOCAL.iter().any(|(k, _, _)| *k == crs) +} + +/// Our local adjustment values from the `Local*` slider fields that `get` finds (the name without +/// the `crs:` prefix → the stored fraction); process-version 2012 fields win over the older ones. +pub(crate) fn local_values(get: impl Fn(&str) -> Option) -> serde_json::Map { let mut a = serde_json::Map::new(); for (crs, ours, k) in LOCAL { if a.contains_key(*ours) { continue; } - if let Some(v) = num(c, &format!("crs:{crs}")) { + if let Some(v) = get(crs).filter(|v| v.is_finite()) { let lim = if *ours == "exposure" { 4.0 } else if *ours == "color_hue" { @@ -82,6 +89,11 @@ fn adjust(c: &XmpValue) -> Value { a.insert((*ours).into(), json!(v)); } } + a +} + +fn adjust(c: &XmpValue) -> Value { + let mut a = local_values(|crs| num(c, &format!("crs:{crs}"))); let amount = num(c, "crs:CorrectionAmount").unwrap_or(1.0); a.insert("amount".into(), json!((amount * 100.0).clamp(0.0, 200.0))); Value::Object(a) @@ -98,8 +110,46 @@ fn pt(x: f64, y: f64) -> Value { json!({"x": x, "y": y}) } -/// One mask component → our component JSON; `Err(kind)` names an unsupported one. -fn component(m: &XmpValue, aspect: f64) -> Result { +/// Whether a `Mask/CircularGradient` component puts its effect inside the ellipse, given the +/// container it came from (`crs:Flipped` means different things in different formats). +/// +/// What Lightroom Classic does, from its exports of synthetic DNGs whose masks we wrote (issue #1815): +/// - **`MaskGroupBasedCorrections`, `crs:Version` ≥ 2** (what Lightroom writes today): an ordinary +/// radial — effect inside — is written `Flipped="true"`, so `Flipped` is the "inside" flag. +/// Inverting the mask is a separate field, `crs:MaskInverted` (see [`component`]). `Flipped="false"` +/// is read as outside, the same sense as the legacy filter's; Lightroom never wrote one for us. +/// - **`MaskGroupBasedCorrections` without `crs:Version`**: Lightroom renders the effect inside +/// whatever `Flipped` says, and rewrites both `Flipped="false"` and `"true"` as `Flipped="true"` +/// with `Version="2"`. So `Flipped` is ignored here. +/// - **`CircularGradientBasedCorrections`** (the process-2012 Radial Filter): its default effect was +/// outside, and "Invert Mask" — effect inside — was recorded as `Flipped="true"`. The same "inside" +/// flag as version 2, with outside as the default when it is missing. +fn radial_inside(m: &XmpValue, container: &str) -> bool { + let flipped = flag(m, "crs:Flipped"); + if container == "crs:MaskGroupBasedCorrections" { + if num(m, "crs:Version").is_some_and(|v| v >= 2.0) { flipped.unwrap_or(true) } else { true } + } else { + flipped.unwrap_or(false) + } +} + +/// A `Mask/CircularGradient` component's feather on our 0..100 scale. +/// +/// Lightroom halves `crs:Feather` when it upgrades a `MaskGroupBasedCorrections` radial without +/// `crs:Version` to version 2 (50 → 25, 100 → 50) and renders both the same, so the version-less +/// value is on twice the scale. Version 2's value is used as it is: its 25 and 50 put the middle of +/// Lightroom's fall-off at about 0.87 and 0.76 of the radius, ours at about 0.86 and 0.73. (The +/// shape differs: Lightroom's fall-off straddles the ellipse edge, ours ends at it.) The legacy +/// Radial Filter's value is used as it is: no observation says otherwise. +fn radial_feather(m: &XmpValue, container: &str) -> f64 { + let f = num(m, "crs:Feather").unwrap_or(50.0); + let versionless_group = container == "crs:MaskGroupBasedCorrections" && !num(m, "crs:Version").is_some_and(|v| v >= 2.0); + (if versionless_group { f / 2.0 } else { f }).clamp(0.0, 100.0) +} + +/// One mask component of a correction in `container` → our component JSON; `Err(kind)` names an +/// unsupported one. +fn component(m: &XmpValue, container: &str, aspect: f64) -> Result { let what = text(m, "crs:What").unwrap_or(""); let op = match num(m, "crs:MaskBlendMode").unwrap_or(0.0) as i64 { 1 => "subtract", @@ -123,8 +173,9 @@ fn component(m: &XmpValue, aspect: f64) -> Result { "rx": ((r - l).abs() / 2.0 * sx).max(1e-3), "ry": ((b - t).abs() / 2.0 * sy).max(1e-3), "angle": g("Angle", 0.0), - "feather": g("Feather", 50.0).clamp(0.0, 100.0), - "invert": flag(m, "crs:Flipped").unwrap_or(false), + "feather": radial_feather(m, container), + // our radial's `invert` = effect outside; `crs:MaskInverted` stays the component's + "invert": !radial_inside(m, container), }) } "Mask/Paint" => { @@ -193,7 +244,7 @@ pub fn masks(values: &Values, aspect: f64) -> (Vec, Vec) { let comps = c.field("crs:CorrectionMasks").map(XmpValue::items).unwrap_or_default(); let mut parts = Vec::new(); for m in comps { - match component(m, aspect) { + match component(m, container, aspect) { Ok(v) => parts.push(v), Err(kind) => skipped.push(kind), } @@ -392,7 +443,7 @@ mod tests { - + @@ -433,7 +484,8 @@ mod tests { let MaskShape::Radial { center, rx, ry, angle, feather, invert } = &rad.components[0].shape else { panic!() }; assert!((center.x - 0.5).abs() < 1e-9 && (center.y - 0.5).abs() < 1e-9); assert!((rx - 0.3).abs() < 1e-9 && (ry - 0.2 / 1.5).abs() < 1e-9, "radii in long-edge units: {rx} {ry}"); - assert_eq!((*angle, *feather, *invert), (10.0, 40.0, true)); + assert_eq!((*angle, *feather, *invert), (10.0, 40.0, false), "Flipped=\"true\" is Lightroom's ordinary radial: effect inside"); + assert!(!rad.components[0].invert); let MaskShape::LuminanceRange { lo, hi, lo_feather, .. } = &rad.components[1].shape else { panic!() }; assert!(lo < hi && *lo_feather > 0.0); let brush = &s.masks[2]; @@ -491,6 +543,114 @@ mod tests { assert!((s["ry"].as_f64().unwrap() - 0.3).abs() < 1e-9, "square photo: y radius = its box half-height"); } + /// One radial correction in `container`, with `attrs` on its mask component (values written + /// for this test; the component attributes follow what Lightroom Classic writes). + fn radial_packet(container: &str, attrs: &str) -> String { + format!( + r#" + + + + + + + + +"# + ) + } + + fn radial_settings(container: &str, attrs: &str) -> DevelopSettings { + let d = lightcraft_meta::parse_xmp(&radial_packet(container, attrs)).unwrap(); + let (partial, unmapped) = crate::crs::to_partial_report(&d.properties, Some(&d.values), crate::crs::Target::Any, 1.5); + assert!(unmapped.is_empty(), "{unmapped:?}"); + let s = DevelopSettings::default().merged(&partial).unwrap(); + assert_eq!(s.masks.len(), 1, "{:?}", s.masks); + s + } + + /// (the radial's `invert`, its feather, the component's `invert`). + fn radial_of(s: &DevelopSettings) -> (bool, f64, bool) { + let c = &s.masks[0].components[0]; + let MaskShape::Radial { invert, feather, .. } = &c.shape else { panic!("{:?}", c.shape) }; + (*invert, *feather, c.invert) + } + + /// How much brighter the mask makes the centre and a corner of a flat grey frame (8-bit green). + fn lift_inside_outside(s: &DevelopSettings) -> (i32, i32) { + let img = lightcraft_raster::Image { width: 90, height: 60, data: vec![[0.18f32; 3]; 90 * 60] }; + let render = + |d: &DevelopSettings| lightcraft_pipeline::render(&img, &Default::default(), d, &lightcraft_pipeline::RenderRequest::fit(90, 60)).image; + let (with, without) = (render(s), render(&DevelopSettings { light: s.light, ..Default::default() })); + let g = |im: &lightcraft_raster::Rgba8, x: usize, y: usize| i32::from(im.data[y * im.width + x][1]); + (g(&with, 45, 30) - g(&without, 45, 30), g(&with, 3, 3) - g(&without, 3, 3)) + } + + #[test] + fn lightroom_radial_flipped_is_inside() { + // as Lightroom Classic writes an ordinary radial mask today (#1815) + let s = radial_settings("MaskGroupBasedCorrections", r#"crs:MaskInverted="false" crs:Feather="25" crs:Flipped="true" crs:Version="2""#); + assert_eq!(radial_of(&s), (false, 25.0, false)); + let (inside, outside) = lift_inside_outside(&s); + assert!(inside > 20 && outside == 0, "the effect is inside the circle: {inside} {outside}"); + // no MaskInverted at all reads the same + let s = radial_settings("MaskGroupBasedCorrections", r#"crs:Feather="25" crs:Flipped="true" crs:Version="2""#); + assert_eq!(radial_of(&s), (false, 25.0, false)); + } + + #[test] + fn lightroom_radial_mask_inverted_is_outside() { + let s = radial_settings("MaskGroupBasedCorrections", r#"crs:MaskInverted="true" crs:Feather="25" crs:Flipped="true" crs:Version="2""#); + assert_eq!(radial_of(&s), (false, 25.0, true), "the inversion is the component's"); + let (inside, outside) = lift_inside_outside(&s); + assert!(inside == 0 && outside > 20, "the effect is outside the circle: {inside} {outside}"); + // version 2 with Flipped="false": outside, the legacy sense of the flag + let s = radial_settings("MaskGroupBasedCorrections", r#"crs:MaskInverted="false" crs:Feather="25" crs:Flipped="false" crs:Version="2""#); + assert_eq!(radial_of(&s), (true, 25.0, false)); + } + + #[test] + fn versionless_group_radial_is_inside_with_half_the_feather() { + // Lightroom renders both inside and rewrites them Flipped="true", Feather halved, Version="2" + for flipped in ["true", "false"] { + let s = radial_settings("MaskGroupBasedCorrections", &format!(r#"crs:MaskInverted="false" crs:Feather="50" crs:Flipped="{flipped}""#)); + assert_eq!(radial_of(&s), (false, 25.0, false), "Flipped={flipped}"); + let (inside, outside) = lift_inside_outside(&s); + assert!(inside > 20 && outside == 0, "Flipped={flipped}: {inside} {outside}"); + } + } + + #[test] + fn legacy_radial_filter_is_outside_unless_flipped() { + // the process-2012 Radial Filter: effect outside by default, "Invert Mask" = Flipped="true" + let s = radial_settings("CircularGradientBasedCorrections", r#"crs:Feather="50" crs:Flipped="false""#); + assert_eq!(radial_of(&s), (true, 50.0, false)); + let (inside, outside) = lift_inside_outside(&s); + assert!(inside == 0 && outside > 20, "default: outside: {inside} {outside}"); + assert_eq!(radial_of(&radial_settings("CircularGradientBasedCorrections", r#"crs:Feather="50""#)), (true, 50.0, false)); + let s = radial_settings("CircularGradientBasedCorrections", r#"crs:Feather="50" crs:Flipped="true""#); + assert_eq!(radial_of(&s), (false, 50.0, false)); + let (inside, outside) = lift_inside_outside(&s); + assert!(inside > 20 && outside == 0, "inverted: inside: {inside} {outside}"); + } + + #[test] + fn lrtemplate_radial_filter_reads_flipped() { + // .lrtemplate presets and catalog develop settings are the same Lua tables + let t = |flipped: &str| { + format!( + r#"s = {{ title = "Rad", value = {{ settings = {{ CircularGradientBasedCorrections = {{ + {{ What = "Correction", CorrectionAmount = 1, LocalExposure2012 = 0.25, CorrectionMasks = {{ + {{ What = "Mask/CircularGradient", Top = 0.25, Left = 0.25, Bottom = 0.75, Right = 0.75, Angle = 0, Feather = 50, Flipped = {flipped}, MaskValue = 1 }} }} }} }} }} }} }}"# + ) + }; + for (flipped, outside) in [("true", false), ("false", true)] { + let v = crate::preset_import::read_presets("Rad.lrtemplate", t(flipped).as_bytes(), None).unwrap(); + let s = DevelopSettings::default().merged(&v[0].preset.settings).unwrap(); + assert_eq!(radial_of(&s).0, outside, "Flipped = {flipped}"); + } + } + /// Spot removal in the shape Lightroom Classic writes it (values written for this test): /// a healed circle with an explicit source, a cloned circle with an automatic source, a /// brushed removal, a circular-gradient spot, one without a mask, and the legacy list. diff --git a/crates/engine/src/demo.rs b/crates/engine/src/demo.rs index 6a89dda88..2cbc20cee 100644 --- a/crates/engine/src/demo.rs +++ b/crates/engine/src/demo.rs @@ -409,7 +409,7 @@ mod tests { #[test] fn a_scene_is_rendered_once_however_many_ask() { use std::sync::atomic::{AtomicUsize, Ordering}; - let (renders, peak) = within(60, || { + let (renders, peak) = within(120, || { let slot = Arc::new(SceneSlot::default()); let (running, peak, renders) = (Arc::new(AtomicUsize::new(0)), Arc::new(AtomicUsize::new(0)), Arc::new(AtomicUsize::new(0))); let render = { @@ -454,7 +454,7 @@ mod tests { /// and that pool's limits; other threads render in parallel on the scene pool. #[test] fn a_rayon_worker_renders_alone_on_its_own_thread() { - let ((how, caller, renderer), (other_how, other_name)) = within(30, || { + let ((how, caller, renderer), (other_how, other_name)) = within(120, || { let pool = rayon::ThreadPoolBuilder::new().num_threads(2).build().unwrap(); let slot = SceneSlot::default(); let mut seen = None; @@ -485,7 +485,7 @@ mod tests { /// minutes of CPU time on a small machine). #[test] fn callers_wait_for_the_generation_running() { - let (a, b, renders) = within(30, || { + let (a, b, renders) = within(120, || { let slot = Arc::new(SceneSlot::default()); let renders = Arc::new(std::sync::atomic::AtomicUsize::new(0)); let (started_tx, started) = std::sync::mpsc::channel::<()>(); @@ -518,7 +518,7 @@ mod tests { /// generates the scene again. #[test] fn a_failed_generation_is_an_error_and_the_next_request_tries_again() { - let (first, waiters, again) = within(30, || { + let (first, waiters, again) = within(120, || { let slot = Arc::new(SceneSlot::default()); let (started_tx, started) = std::sync::mpsc::channel::<()>(); let (go, go_rx) = std::sync::mpsc::channel::<()>(); @@ -564,7 +564,7 @@ mod tests { assert!(matches!(st.outcome(n), Some(Err(_))), "even once the retry succeeded"); assert!(matches!(st.outcome(m), Some(Ok(_)))); // and with threads: a retry racing the waiters of a failed generation never hands them its result - let wrong = within(60, || { + let wrong = within(120, || { let mut wrong = 0; for _ in 0..20 { let slot = Arc::new(SceneSlot::default()); diff --git a/crates/engine/src/devices.rs b/crates/engine/src/devices.rs index 399d03fde..366cb5273 100644 --- a/crates/engine/src/devices.rs +++ b/crates/engine/src/devices.rs @@ -104,6 +104,18 @@ impl DeviceWatch { s.list.clone() } + /// Like [`DeviceWatch::get`], but `None` until the first scan has landed, so an empty list + /// means no cards rather than not looked yet. + pub fn get_scanned(self: &Arc) -> Option> { + let _ = self.get(); + let s = self.state(); + #[cfg(not(target_arch = "wasm32"))] + let scanned = s.scanned.is_some(); + #[cfg(target_arch = "wasm32")] + let scanned = false; + scanned.then(|| s.list.clone()) + } + /// Scan now on this thread, and serve the result to later [`DeviceWatch::get`] calls. pub fn scan_now(&self) -> Vec { let list = (self.scanner)(); @@ -148,6 +160,12 @@ pub fn devices() -> Vec { global().get() } +/// [`devices`], but `None` until the first scan lands. The import prompt uses it so a card that was +/// already in when LightCraft started isn't taken for a new one. +pub fn devices_scanned() -> Option> { + global().get_scanned() +} + /// Scan the mounted volumes now (blocking), for explicit requests. pub fn devices_now() -> Vec { global().scan_now() @@ -285,4 +303,24 @@ mod tests { assert!(watch.scan_now().is_empty()); assert_eq!(changes.load(Ordering::SeqCst), 2); } + + #[test] + fn get_scanned_waits_for_the_first_scan() { + let gate = Arc::new(std::sync::atomic::AtomicBool::new(false)); + let g = gate.clone(); + let watch = DeviceWatch::new( + move || { + while !g.load(Ordering::SeqCst) { + std::thread::sleep(Duration::from_millis(5)); + } + vec![card("NIKON Z 8")] + }, + Duration::from_secs(60), + ); + assert_eq!(watch.get_scanned(), None, "nothing scanned yet"); + assert_eq!(watch.get_scanned(), None); + gate.store(true, Ordering::SeqCst); + assert!(until(|| watch.get_scanned().is_some())); + assert_eq!(watch.get_scanned(), Some(vec![card("NIKON Z 8")])); + } } diff --git a/crates/engine/src/export.rs b/crates/engine/src/export.rs index 403cb71f6..b9e414d51 100644 --- a/crates/engine/src/export.rs +++ b/crates/engine/src/export.rs @@ -162,6 +162,29 @@ pub enum Conflict { Skip, } +/// Where a batch's files go (Lightroom Classic's Export To). +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub enum DestinationMode { + /// One folder for the whole batch ([`Destination::dir`]). + #[default] + SpecificFolder, + /// Each photo's own folder: the folder its original is in. A photo without one on disk (a + /// demo photo, a photo in the browser's storage) or whose folder is gone gets an error. + SameAsOriginal, +} + +impl DestinationMode { + /// `specificFolder` / `sameAsOriginal` (any case). + pub fn parse(s: &str) -> Option { + match s.trim().to_ascii_lowercase().as_str() { + "specificfolder" => Some(Self::SpecificFolder), + "sameasoriginal" => Some(Self::SameAsOriginal), + _ => None, + } + } +} + /// A named set of `app.export` params. #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] pub struct ExportPreset { @@ -293,58 +316,8 @@ fn turn(turns: u8, x: f32, y: f32, w: f32, h: f32) -> (f32, f32) { } } -/// Inter SemiBold (OFL, see assets/ATTRIBUTION.md). -static WATERMARK_FONT: &[u8] = include_bytes!("../../../assets/fonts/Inter-SemiBold.ttf"); - -/// The watermark faces: Inter first, then the craft-fonts CJK faces (Mincho first, the watermark's -/// serif look; then any other CJK face — a watermark has no UI language, so every CJK script is -/// offered). Without craft-fonts that is Inter alone, and CJK characters draw as Inter's -/// missing-glyph box. -fn watermark_fonts(craft: &'static [crate::fonts::CraftFont]) -> Vec> { - let mut cjk: Vec<_> = crate::fonts::cjk(craft).collect(); - cjk.sort_by_key(|f| !f.is_mincho()); - std::iter::once(WATERMARK_FONT) - .chain(cjk.into_iter().map(|f| f.bytes)) - .filter_map(|bytes| ab_glyph::FontRef::try_from_slice(bytes).ok()) - .collect() -} - -/// Shape a whole grapheme in one cell. CJK uses vertical origins and forms; Latin stays upright. -/// Unlike Unicode presentation-form characters, GSUB works with BIZ UD fonts too. -fn watermark_cell_glyphs( - font: &ab_glyph::FontRef<'_>, - data: &harfrust::ShaperData, - text: &str, - vertical: bool, - px: f32, - left: f32, - top: f32, -) -> Option> { - use ab_glyph::{Font, ScaleFont}; - let face = harfrust::FontRef::new(font.font_data()).ok()?; - let mut buf = harfrust::UnicodeBuffer::new(); - buf.push_str(text); - buf.set_direction(if vertical { harfrust::Direction::TopToBottom } else { harfrust::Direction::LeftToRight }); - buf.guess_segment_properties(); - let shaped = data.shaper(&face).build().shape(buf, harfrust::ShapeOptions::new()); - if shaped.glyph_infos().is_empty() { - return None; - } - let sf = font.as_scaled(px); - let (sx, sy) = (sf.h_scale_factor(), sf.v_scale_factor()); - let advance_x = shaped.glyph_positions().iter().map(|p| p.x_advance as f32 * sx).sum::(); - let advance_y = shaped.glyph_positions().iter().map(|p| -(p.y_advance as f32) * sy).sum::(); - let (mut x, mut y) = if vertical { (left + px / 2.0, top + (px - advance_y) / 2.0) } else { (left + (px - advance_x) / 2.0, top + sf.ascent()) }; - let mut glyphs = Vec::new(); - for (info, pos) in shaped.glyph_infos().iter().zip(shaped.glyph_positions()) { - let gid = u16::try_from(info.glyph_id).ok().filter(|g| *g != 0)?; - let pen = ab_glyph::point(x + pos.x_offset as f32 * sx, y - pos.y_offset as f32 * sy); - glyphs.push(ab_glyph::GlyphId(gid).with_scale_and_position(px, pen)); - x += pos.x_advance as f32 * sx; - y -= pos.y_advance as f32 * sy; - } - Some(glyphs) -} +#[cfg(test)] +use crate::text::{TEXT_FONT as WATERMARK_FONT, cell_glyphs as watermark_cell_glyphs, faces as watermark_fonts}; /// Draw `wm` onto `img` (straight alpha blending of the encoded values). pub fn draw_watermark(img: &mut Rgba8, wm: &Watermark) { @@ -369,6 +342,23 @@ fn blend_watermark(img: &mut Rgba8, wm: &Watermark, floor_scale: f32) { }); } +/// Draw `wm` into the `w × h` area of `img` whose top-left corner is (`x0`, `y0`), sized and +/// anchored as on an image of that size (a slideshow slide's photo). The area is clipped to `img`. +pub fn draw_watermark_in(img: &mut Rgba8, wm: &Watermark, x0: usize, y0: usize, w: usize, h: usize) { + let (width, height) = (img.width, img.height); + watermark_coverage(w, h, wm, crate::fonts::CRAFT_FONTS, 1.0, |x, y, k, col| { + let (px, py) = (x0.saturating_add(x), y0.saturating_add(y)); + if px >= width || py >= height { + return; + } + if let Some(p) = img.data.get_mut(py * width + px) { + for c in 0..3 { + p[c] = (p[c] as f32 + (col[c] as f32 - p[c] as f32) * k).round() as u8; + } + } + }); +} + /// Draw `wm` onto a high-bit-depth image (`wm.color` is in the image's space, 8-bit encoded; float /// images are linear and blend in linear light). pub fn draw_watermark_deep(img: &mut DeepImage, wm: &Watermark) { @@ -403,7 +393,6 @@ fn watermark_coverage( floor_scale: f32, mut blend_px: impl FnMut(usize, usize, f32, [u8; 3]), ) { - use ab_glyph::{Font, PxScale, ScaleFont, point}; if !wm.image.trim().is_empty() { logo_coverage(width, height, wm, blend_px); return; @@ -412,97 +401,10 @@ fn watermark_coverage( if text.is_empty() || width == 0 || height == 0 { return; } - let fonts = watermark_fonts(craft); - let Some(latin) = fonts.first() else { return }; - let vertical_data: Vec<_> = if wm.vertical { - fonts.iter().map(|font| harfrust::FontRef::new(font.font_data()).ok().map(|face| harfrust::ShaperData::new(&face))).collect() - } else { - Vec::new() - }; let short = width.min(height) as f32; let px = (wm.size.clamp(0.005, 0.5) * short).max(6.0 * floor_scale); - let mut glyphs = Vec::new(); - let mut x = 0.0f32; - let mut y = 0.0f32; - let mut tw = 0.0f32; - let mut prev = None; - let columns = text.split('\n').count(); - let mut column = 0usize; - use unicode_segmentation::UnicodeSegmentation; - // Horizontal watermarks retain scalar layout and kerning; only vertical cells use graphemes. - let cells: Box> = - if wm.vertical { Box::new(text.graphemes(true)) } else { Box::new(text.char_indices().filter_map(|(i, c)| text.get(i..i + c.len_utf8()))) }; - for cell in cells { - let Some(original) = cell.chars().next() else { continue }; - if cell == "\n" || (wm.vertical && cell == "\r\n") { - if wm.vertical { - column = column.saturating_add(1); - tw = tw.max(y); - y = 0.0; - } else { - tw = tw.max(x); - x = 0.0; - y += px; - } - prev = None; - continue; - } - let cjk = matches!(original as u32, 0x3000..=0x30FF | 0x3400..=0x9FFF | 0xFF01..=0xFF60); - if wm.vertical && (cjk || cell.chars().count() > 1) { - let left = columns.saturating_sub(column.saturating_add(1)) as f32 * px; - let shaped = fonts.iter().zip(&vertical_data).enumerate().find_map(|(face, (font, data))| { - let data = data.as_ref()?; - let cluster = watermark_cell_glyphs(font, data, cell, cjk, px, left, y)?; - let substituted = cluster.len() != 1 || cluster.first().is_some_and(|g| g.id != font.glyph_id(original)); - if cjk && !substituted && matches!(original, '、' | '。' | 'ー' | '(' | ')' | '「' | '」' | '『' | '』' | '【' | '】') { - return None; - } - Some((face, cluster)) - }); - if let Some((face, cluster)) = shaped { - glyphs.extend(cluster.into_iter().map(|g| (face, g, None))); - y += px; - continue; - } - } - let vertical_form = match original { - '、' => '︑', - '。' => '︒', - '(' => '︵', - ')' => '︶', - '「' => '﹁', - '」' => '﹂', - '『' => '﹃', - '』' => '﹄', - '【' => '︻', - '】' => '︼', - '…' => '︙', - other => other, - }; - let has = |font: &ab_glyph::FontRef<'_>, c: char| font.glyph_id(c).0 != 0; - let ch = if wm.vertical && fonts.iter().skip(1).any(|f| has(f, vertical_form)) { vertical_form } else { original }; - // The first face with the glyph; Inter (its missing-glyph box) when none has it. - let face = fonts.iter().position(|f| has(f, ch)).unwrap_or(0); - let sf = fonts.get(face).unwrap_or(latin).as_scaled(PxScale::from(px)); - let id = sf.glyph_id(ch); - if wm.vertical { - let left = columns.saturating_sub(column.saturating_add(1)) as f32 * px; - let pen = point(left + (px - sf.h_advance(id)) / 2.0, y + sf.ascent()); - let rotation = matches!(original, 'ー' | '—' | '–').then_some((left + px / 2.0, y + px / 2.0)); - glyphs.push((face, id.with_scale_and_position(px, pen), rotation)); - y += px; - } else { - if let Some((previous_face, previous)) = prev - && previous_face == face - { - x += sf.kern(previous, id); - } - glyphs.push((face, id.with_scale_and_position(px, point(x, y + sf.ascent())), None)); - x += sf.h_advance(id); - prev = Some((face, id)); - } - } - let (tw, th) = if wm.vertical { (columns as f32 * px, tw.max(y)) } else { (tw.max(x), y + px) }; + let Some(layout) = crate::text::TextLayout::new(text, px, wm.vertical, craft) else { return }; + let (tw, th) = (layout.width, layout.height); // laid out unturned in a tw × th box, placed by the box it becomes let turns = wm.quarter_turns(); let (bw, bh) = if turns % 2 == 1 { (th, tw) } else { (tw, th) }; @@ -520,35 +422,29 @@ fn watermark_coverage( BottomLeft | Bottom | BottomRight => h - iy - bh, }; let alpha = wm.opacity.clamp(0.0, 1.0); - let mut blend = |gx: i32, gy: i32, cov: f32, col: [u8; 3], a: f32| { - if gx < 0 || gy < 0 || gx >= width as i32 || gy >= height as i32 { - return; - } - blend_px(gx as usize, gy as usize, (cov * a).clamp(0.0, 1.0), col); - }; let passes: &[(f32, [u8; 3], f32)] = if wm.shadow { &[((px * 0.05).max(floor_scale), [0, 0, 0], 0.45), (0.0, wm.color, 1.0)] } else { &[(0.0, wm.color, 1.0)] }; for &(off, col, a) in passes { - for (face, g, rotation) in &glyphs { - if let Some(o) = fonts.get(*face).and_then(|font| font.outline_glyph(g.clone())) { - let b = o.px_bounds(); - o.draw(|gx, gy, cov| { - let (sx, sy) = (b.min.x + gx as f32, b.min.y + gy as f32); - let (sx, sy) = match rotation { - Some((cx, cy)) => (cx - (sy - cy), cy + (sx - cx)), - None => (sx, sy), - }; - // turned with the box; the shadow still falls down and to the right - let (sx, sy) = turn(turns, sx, sy, tw, th); - blend((ox + off + sx) as i32, (oy + off + sy) as i32, cov, col, a * alpha); - }); + let mut put = |gx: i32, gy: i32, cov: f32| { + if gx < 0 || gy < 0 || gx >= width as i32 || gy >= height as i32 { + return; } + blend_px(gx as usize, gy as usize, (cov * a * alpha).clamp(0.0, 1.0), col); + }; + if turns == 0 { + layout.draw_in(ox + off, oy + off, w, h, &mut put); + } else { + // turned with the box; the shadow still falls down and to the right + layout.draw_points(|x, y, cov| { + let (x, y) = turn(turns, x, y, tw, th); + put((ox + off + x) as i32, (oy + off + y) as i32, cov); + }); } } } /// A watermark graphic, decoded once per file (and again when the file changes). -fn watermark_logo(path: &str) -> Option> { +pub(crate) fn watermark_logo(path: &str) -> Option> { use std::collections::HashMap; use std::sync::{Arc, Mutex}; static CACHE: Mutex)>>> = Mutex::new(None); @@ -560,12 +456,19 @@ fn watermark_logo(path: &str) -> Option> { { return Some(img.clone()); } - let bytes = std::fs::read(path).ok()?; - let img = Arc::new(lightcraft_codecs::decode(&bytes, Default::default()).ok()?.to_srgb8()); + let img = Arc::new(read_graphic(path).ok()?); map.insert(path.to_string(), (modified, img.clone())); Some(img) } +/// 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. +pub fn read_graphic(path: &str) -> Result { + 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()) +} + /// The graphic watermark: scaled (premultiplied, so transparent edges stay clean) to /// `image_width` of the photo, placed like the text, blended with its alpha × opacity. fn logo_coverage(width: usize, height: usize, wm: &Watermark, mut blend_px: impl FnMut(usize, usize, f32, [u8; 3])) { @@ -682,6 +585,8 @@ pub struct ExportOptions { pub naming: String, /// First `{seq}` value. pub start_number: u32, + /// A specific folder for the whole batch, or each photo's own folder. + pub destination_mode: DestinationMode, /// Created inside the destination folder (empty = none). pub subfolder: String, /// When a file of the same name exists in the destination folder. @@ -734,6 +639,7 @@ impl Default for ExportOptions { sharpen_amount: SharpenAmount::Standard, naming: "{name}".into(), start_number: 1, + destination_mode: DestinationMode::SpecificFolder, subfolder: String::new(), conflict: Conflict::Unique, tiff_compression: TiffCompression::Deflate, @@ -776,6 +682,7 @@ pub const OPTION_PARAMS: &[&str] = &[ "sharpenAmount", "naming", "startNumber", + "destinationMode", "subfolder", "conflict", "tiffCompression", @@ -966,11 +873,12 @@ impl ExportOptions { "sharpenAmount" => one_of(k, v, &["low", "standard", "high"])?, "hdrTransfer" => one_of(k, v, &["pq", "hlg"])?, "conflict" => one_of(k, v, &["unique", "overwrite", "skip"])?, + "destinationMode" => one_of(k, v, &["specificFolder", "sameAsOriginal"])?, "tiffCompression" => one_of(k, v, &["none", "lzw", "zip", "deflate"])?, "jxlEffort" => one_of(k, v, &["fast", "normal"])?, "dngCompression" => { serde_json::from_value::(v.clone()) - .map_err(|_| bad(format!("`{k}` must be one of lossless|deflate|uncompressed")))?; + .map_err(|_| bad(format!("`{k}` must be one of lossless|deflate|uncompressed|lossy")))?; } "metadata" => one_of(k, v, &["all", "allExceptCamera", "copyright", "none"])?, "colorSpace" => { @@ -995,7 +903,7 @@ impl ExportOptions { } /// A `watermark` value: text, or an object of [`WATERMARK_PARAMS`] (issue #183). - fn validate_watermark(cmd: &str, v: &serde_json::Value) -> crate::Result<()> { + pub(crate) fn validate_watermark(cmd: &str, v: &serde_json::Value) -> crate::Result<()> { use serde_json::Value; let bad = |msg: String| crate::cmd::bad(cmd, msg); let obj = match v { @@ -1078,6 +986,7 @@ impl ExportOptions { sharpen_amount: enm(p, "sharpenAmount").unwrap_or(d.sharpen_amount), naming: s("naming").map_or(d.naming, str::to_string), start_number: u("startNumber").map_or(d.start_number, |v| v.min(999_999) as u32), + destination_mode: s("destinationMode").and_then(DestinationMode::parse).unwrap_or(d.destination_mode), subfolder: s("subfolder").map_or(d.subfolder, |v| v.trim().replace(['\\', ':', '\0'], "_")), conflict: enm(p, "conflict").unwrap_or(d.conflict), tiff_compression: match s("tiffCompression").map(str::to_ascii_lowercase).as_deref() { @@ -1608,6 +1517,9 @@ pub fn export_photo(session: &mut crate::Session, id: lightcraft_catalog::PhotoI pub struct PreparedExport { pub photo: lightcraft_catalog::PhotoId, pub file_name: String, + /// The folder of the photo's original on disk ([`DestinationMode::SameAsOriginal`]); `None` + /// for a demo photo or one in the browser's storage. + pub source_dir: Option, work: Work, /// The library's originals, which [`run_batch`] never writes over (shared by a batch). guard: std::sync::Arc, @@ -1633,6 +1545,9 @@ enum Work { read: Option, packet: String, dng: Option, + /// Lossy DNG: JPEG quality and the long edge to scale to (`None`: full size). + quality: u8, + lossy_edge: Option, /// The original must not carry a location ([`removes_location`]). remove_location: bool, label: String, @@ -1667,6 +1582,7 @@ fn prepare_guarded( ) -> Result { let p = session.catalog.photo(id).ok_or("no such photo")?; let file_name = o.file_name_for(p, seq); + let source_dir = original_folder(&p.source); // HDR output only for photos edited in HDR; the rest of the batch exports as usual let sdr_opts; let o = if o.hdr && !p.develop.hdr.enabled { @@ -1702,13 +1618,29 @@ fn prepare_guarded( read: session.media.file_bytes.clone(), packet: crate::sidecar::sidecar_packet_with(p, &session.catalog, removes_location(p, &session.catalog, o)), dng, + quality: o.quality, + lossy_edge: o.resize.is_some().then(|| { + let (w, h) = output_size(p, o); + u32::try_from(w.max(h)).unwrap_or(u32::MAX) + }), remove_location: removes_location(p, &session.catalog, o), label: p.file_name.clone(), size: (p.width as usize, p.height as usize), }; (work, weight) }; - Ok(PreparedExport { photo: id, file_name, work, guard, weight }) + Ok(PreparedExport { photo: id, file_name, source_dir, work, guard, weight }) +} + +/// The folder a photo's original is in, for [`DestinationMode::SameAsOriginal`]. None for a demo +/// photo, and on the web, where an original lives in the browser's storage, not in a folder. +fn original_folder(source: &lightcraft_catalog::Source) -> Option { + let lightcraft_catalog::Source::File { path } = source else { return None }; + if cfg!(target_arch = "wasm32") { + return None; + } + let dir = std::path::Path::new(path).parent()?.to_string_lossy().to_string(); + (!dir.is_empty()).then_some(dir) } /// Why an export of `out` pixels (`w`, `h`), from a photo of `native_px` pixels at its own size, @@ -1761,7 +1693,7 @@ impl PreparedExport { }; Ok(Exported { file_name, bytes, width: r.image.width, height: r.image.height, sidecars: Vec::new(), gpu_fallback }) } - Work::File { path, read, packet, dng, label, size, remove_location } => { + Work::File { path, read, packet, dng, quality, lossy_edge, label, size, remove_location } => { let bytes = match &read { Some(r) => r(&path)?, None => std::fs::read(&path).map_err(|e| format!("{path}: {e}"))?, @@ -1785,12 +1717,16 @@ impl PreparedExport { if lightcraft_raw::probe(&bytes).is_none() { return Err(format!("{label}: DNG export needs a raw photo")); } - let raw = lightcraft_raw::decode(&bytes).map_err(|e| format!("{label}: {e}"))?; + let mut raw = lightcraft_raw::decode(&bytes).map_err(|e| format!("{label}: {e}"))?; + crate::camera_preview::with_starting_colour(&mut raw, &bytes); drop(bytes); let compression = match dng { DngCompression::Lossless => lightcraft_raw::DngCompression::Lj92 { tile: 256 }, DngCompression::Deflate => lightcraft_raw::DngCompression::Deflate { tile: 256, half: false }, DngCompression::Uncompressed => lightcraft_raw::DngCompression::Uncompressed, + DngCompression::Lossy => { + lightcraft_raw::DngCompression::LossyJpeg { tile: 256, quality: quality.clamp(1, 100), long_edge: lossy_edge } + } }; let dng = lightcraft_raw::write_dng(&raw, &lightcraft_raw::DngWriteOptions { xmp: Some(packet), compression, ..Default::default() }) .map_err(|e| e.to_string())?; @@ -1810,11 +1746,15 @@ pub enum DngCompression { /// Deflate (zip) tiles with prediction. Deflate, Uncompressed, + /// Lossy DNG: demosaiced linear data as JPEG tiles at the export quality, scaled to the resize + /// setting when there is one (Lightroom's "Use Lossy Compression"). + Lossy, } /// The destination of a batch: a folder (with `ExportOptions::subfolder` and the conflict policy -/// applied to each file together with its sidecars), or one exact file path (single photo; an -/// ordinary file already there is replaced). Either way a catalogued original (or its sidecar) +/// applied to each file together with its sidecars; with [`DestinationMode::SameAsOriginal`] each +/// photo's own folder instead of `dir`), or one exact file path (single photo; an ordinary file +/// already there is replaced). Either way a catalogued original (or its sidecar) /// is never written over: see [`crate::originals`]. #[derive(Clone, Debug, Default)] pub struct Destination { @@ -2011,12 +1951,12 @@ fn run_batch_with( if !progress(i, &item.file_name) { break; } - let (photo, name, guard) = (item.photo, item.file_name.clone(), item.guard.clone()); - placer.place(photo, name, &guard, run_gated(item))?; + let (photo, name, guard, source_dir) = (item.photo, item.file_name.clone(), item.guard.clone(), item.source_dir.clone()); + placer.place(photo, name, source_dir.as_deref(), &guard, run_gated(item))?; } return Ok(std::mem::take(&mut placer.out)); } - let heads: Vec<_> = items.iter().map(|i| (i.photo, i.file_name.clone(), i.guard.clone())).collect(); + let heads: Vec<_> = items.iter().map(|i| (i.photo, i.file_name.clone(), i.guard.clone(), i.source_dir.clone())).collect(); let queue = Queue { lane: std::sync::Mutex::new(Lane { items: items.into_iter().map(Some).collect(), next: 0, placed: 0, stop: false, done: Default::default() }), cv: std::sync::Condvar::new(), @@ -2031,12 +1971,12 @@ fn run_batch_with( } } let placed = || { - for (i, (photo, name, guard)) in heads.into_iter().enumerate() { + for (i, (photo, name, guard, source_dir)) in heads.into_iter().enumerate() { if !progress(i, &name) { break; } let e = queue.result(i); - placer.place(photo, name, &guard, e)?; + placer.place(photo, name, source_dir.as_deref(), &guard, e)?; } Ok::<_, String>(()) }; @@ -2147,12 +2087,11 @@ impl Queue { } } -/// Where a batch's files go: each photo's path (subfolder, conflict policy, sidecars, never an -/// original) and its write, in batch order. +/// Where a batch's files go: each photo's path (destination folder, subfolder, conflict policy, +/// sidecars, never an original) and its write, in batch order. struct Placer<'a> { o: &'a ExportOptions, to: &'a Destination, - dir: String, single: bool, taken: std::collections::HashSet, out: Vec, @@ -2173,29 +2112,53 @@ impl<'a> Placer<'a> { exists: &'a dyn Fn(&str) -> bool, stop_on_error: bool, ) -> Placer<'a> { - let dir = if o.subfolder.is_empty() { to.dir.clone() } else { join_path(&to.dir, &o.subfolder) }; - Placer { o, to, dir, single: false, taken: Default::default(), out: Vec::new(), write, exists, stop_on_error } + Placer { o, to, single: false, taken: Default::default(), out: Vec::new(), write, exists, stop_on_error } } - /// Place photo `photo`'s export result (file `name`): write it and record the outcome. `Err` - /// only when it failed and `stop_on_error` is set. + /// The folder photo `name` (its original in `source_dir`) is written to: the batch's folder or + /// the photo's own ([`DestinationMode`]), plus the subfolder. An error for a photo with no + /// folder of its own, or whose folder is gone: never a file written somewhere else instead. + fn folder_for(&self, name: &str, source_dir: Option<&str>) -> Result { + let base = match self.o.destination_mode { + DestinationMode::SpecificFolder => self.to.dir.clone(), + DestinationMode::SameAsOriginal => { + let Some(d) = source_dir else { + return Err(format!( + "{name}: the photo has no folder on disk (a demo photo or one in the browser's storage), so it can't be exported to the same folder as its original; choose a specific folder" + )); + }; + if !(self.exists)(d) { + return Err(format!("{name}: the original's folder is missing ({d}); reconnect it or choose a specific folder")); + } + d.to_string() + } + }; + Ok(if self.o.subfolder.is_empty() { base } else { join_path(&base, &self.o.subfolder) }) + } + + /// Place photo `photo`'s export result (file `name`, its original in `source_dir`): write it + /// and record the outcome. `Err` only when it failed and `stop_on_error` is set. fn place( &mut self, photo: lightcraft_catalog::PhotoId, name: String, + source_dir: Option<&str>, guard: &crate::originals::OriginalGuard, e: Result, ) -> Result<(), String> { use serde_json::json; - let (o, dir, exists) = (self.o, &self.dir, self.exists); - let e = match e { - Ok(e) => e, + // an exact file path needs no folder + let dir = if self.to.exact.is_some() && self.single { Ok(String::new()) } else { self.folder_for(&name, source_dir) }; + let (o, exists) = (self.o, self.exists); + let (dir, e) = match dir.and_then(|dir| e.map(|e| (dir, e))) { + Ok(v) => v, Err(err) if self.stop_on_error => return Err(err), Err(err) => { self.out.push(json!({"photo": photo.0, "file": name, "error": err})); return Ok(()); } }; + let dir = dir.as_str(); // the exported file and its sidecars let group = |main: &str| std::iter::once(main.to_string()).chain(e.sidecars.iter().map(|(x, _)| sidecar_path(main, x))).collect::>(); let path = match self.to.exact.as_deref().filter(|_| self.single) { @@ -3190,6 +3153,35 @@ mod tests { ExportOptions::from_params(&json!({"quality": 92, "longEdge": null, "watermark": null})).unwrap(); } + /// Issue #956: `destinationMode` picks a specific folder (the default) or each photo's own + /// folder; it is checked like the other params and kept in presets and Export with Previous. + #[test] + fn destination_mode_param() { + use serde_json::json; + assert_eq!(ExportOptions::default().destination_mode, DestinationMode::SpecificFolder); + for (v, want) in [ + ("sameAsOriginal", DestinationMode::SameAsOriginal), + ("SameAsOriginal", DestinationMode::SameAsOriginal), + ("specificFolder", DestinationMode::SpecificFolder), + ] { + assert_eq!(ExportOptions::from_params(&json!({"destinationMode": v})).unwrap().destination_mode, want, "{v}"); + } + let e = ExportOptions::from_params(&json!({"destinationMode": "nextToIt"})).unwrap_err().to_string(); + assert!(e.contains("`destinationMode` must be one of specificFolder|sameAsOriginal"), "{e}"); + assert!(ExportOptions::from_params(&json!({"destinationMode": 1})).is_err()); + assert!( + ExportOptions::from_params(&json!({"destinationMod": "sameAsOriginal"})) + .unwrap_err() + .to_string() + .contains("did you mean `destinationMode`") + ); + // saved (a preset, `lastExport`) and read back + let o = ExportOptions { destination_mode: DestinationMode::SameAsOriginal, subfolder: "Exports".into(), ..Default::default() }; + let j = o.to_json(); + assert_eq!(j["destinationMode"], "sameAsOriginal"); + assert_eq!(ExportOptions::from_params(&ExportOptions::known_keys_only(&j)).unwrap(), o); + } + /// Issue #183: the `watermark` object's keys, kinds and ranges are checked (and documented in /// the `app.export` param string), instead of unknown keys passing and sizes clamping silently. #[test] diff --git a/crates/engine/src/faces_index.rs b/crates/engine/src/faces_index.rs index fd4d9f2e7..83e86b099 100644 --- a/crates/engine/src/faces_index.rs +++ b/crates/engine/src/faces_index.rs @@ -55,6 +55,8 @@ pub(crate) struct Index { skipped: std::collections::HashSet, /// The cache file on disk belongs to this model, so new records can be appended to it. file_matches: bool, + /// Records were dropped ([`Index::forget_photo`]): the cache file is written fresh at the next save. + rewrite: bool, pub path: Option, } @@ -87,6 +89,16 @@ impl Index { self.skipped.insert((photo, region_key(rect))); } + /// Drop every face of `photo` (its file was replaced, so they were made from other pixels): they are embedded + /// again, and the cache file is rewritten without them at the next save. + pub fn forget_photo(&mut self, photo: u64) { + let before = self.entries.len(); + self.entries.retain(|k, _| k.0 != photo); + self.unsaved.retain(|(k, _)| k.0 != photo); + self.skipped.retain(|k| k.0 != photo); + self.rewrite |= self.entries.len() != before; + } + #[cfg(test)] pub fn insert(&mut self, photo: u64, rect: &Rect, embedding: Vec) { self.insert_with_view(photo, rect, embedding, rect); @@ -102,7 +114,7 @@ impl Index { /// Whether there are embeddings not yet written to the cache file. pub fn has_unsaved(&self) -> bool { - !self.unsaved.is_empty() + !self.unsaved.is_empty() || self.rewrite } /// Start over for `tag` (`dim` numbers per face), loading what the cache file at `path` holds for it. What the @@ -116,6 +128,7 @@ impl Index { self.unsaved.clear(); self.skipped.clear(); self.file_matches = false; + self.rewrite = false; if let Some(p) = self.path.clone() { self.load(&p); } @@ -175,17 +188,19 @@ impl Index { r } - /// Write what was added since the last save: appended when the file already belongs to this model, else the - /// whole index is written fresh (atomically). A failure is returned, the records stay pending. + /// Write what was added since the last save: appended when the file already belongs to this model, else (or when + /// records were dropped) the whole index is written fresh (atomically). A failure is returned, the records stay + /// pending. pub fn save(&mut self) -> std::io::Result<()> { let Some(path) = self.path.clone() else { self.unsaved.clear(); + self.rewrite = false; return Ok(()); }; - if self.unsaved.is_empty() { + if self.unsaved.is_empty() && !self.rewrite { return Ok(()); } - if self.file_matches { + if self.file_matches && !self.rewrite { let mut f = std::fs::OpenOptions::new().append(true).open(&path)?; let mut buf = Vec::new(); for (k, e) in &self.unsaved { @@ -201,6 +216,7 @@ impl Index { std::fs::write(&part, &buf)?; std::fs::rename(&part, &path)?; self.file_matches = true; + self.rewrite = false; } self.unsaved.clear(); Ok(()) @@ -228,6 +244,8 @@ pub(crate) struct Scanned { unsaved: Vec, path: Option, file_matches: bool, + /// A photo was taken out ([`Scanned::remove`]): the record is written fresh at the next save. + rewrite: bool, } impl Scanned { @@ -249,8 +267,16 @@ impl Scanned { } } + /// Search `photo` again (its file was replaced). + pub fn remove(&mut self, photo: u64) { + if self.set.remove(&photo) { + self.unsaved.retain(|p| *p != photo); + self.rewrite = true; + } + } + pub fn has_unsaved(&self) -> bool { - !self.unsaved.is_empty() + !self.unsaved.is_empty() || self.rewrite } /// Start over with the record at `path` (written first: what was not yet saved is not lost). @@ -259,6 +285,7 @@ impl Scanned { self.set.clear(); self.unsaved.clear(); self.file_matches = false; + self.rewrite = false; self.path = path; if let Some(p) = self.path.clone() { self.load(&p); @@ -292,12 +319,13 @@ impl Scanned { pub fn save(&mut self) -> std::io::Result<()> { let Some(path) = self.path.clone() else { self.unsaved.clear(); + self.rewrite = false; return Ok(()); }; - if self.unsaved.is_empty() { + if self.unsaved.is_empty() && !self.rewrite { return Ok(()); } - if self.file_matches { + if self.file_matches && !self.rewrite { let mut f = std::fs::OpenOptions::new().append(true).open(&path)?; let buf: Vec = self.unsaved.iter().flat_map(|id| id.to_le_bytes()).collect(); f.write_all(&buf)?; @@ -308,6 +336,7 @@ impl Scanned { std::fs::write(&part, &buf)?; std::fs::rename(&part, &path)?; self.file_matches = true; + self.rewrite = false; } self.unsaved.clear(); Ok(()) @@ -406,6 +435,43 @@ mod tests { assert_eq!(b.len(), 0, "a non-finite vector is not loaded back"); let _ = std::fs::remove_file(path); } + + /// The faces and the search of a photo whose file was replaced are dropped from the files too, not only from memory: + /// they don't come back at the next launch (issue #1524). + #[test] + fn a_forgotten_photo_stays_forgotten_after_reloading() { + let dir = std::env::temp_dir().join(format!("lc-faces-forget-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + std::fs::create_dir_all(&dir).unwrap(); + let (emb, scan) = (dir.join("face-embeddings.bin"), dir.join("face-scanned.bin")); + let mut a = Index::default(); + a.reset("m@abc", 2, Some(emb.clone())); + a.insert(1, &rect(0.1), vec![1.0, 0.0]); + a.insert(2, &rect(0.1), vec![0.0, 1.0]); + a.save().unwrap(); + a.forget_photo(1); + assert!(a.has_unsaved()); + a.save().unwrap(); + a.insert(3, &rect(0.1), vec![1.0, 1.0]); + a.save().unwrap(); + let mut b = Index::default(); + b.reset("m@abc", 2, Some(emb)); + assert!(!b.contains(1, &rect(0.1)) && b.contains(2, &rect(0.1)) && b.contains(3, &rect(0.1))); + let mut s = Scanned::default(); + s.reset(Some(scan.clone())); + s.insert(1); + s.insert(2); + s.save().unwrap(); + s.remove(1); + assert!(s.has_unsaved()); + s.save().unwrap(); + s.insert(3); + s.save().unwrap(); + let mut t = Scanned::default(); + t.reset(Some(scan)); + assert!(!t.contains(1) && t.contains(2) && t.contains(3)); + let _ = std::fs::remove_dir_all(&dir); + } } /// What the recognition commands keep between calls: the loaded model and the embeddings made with it. @@ -456,4 +522,12 @@ impl FacesState { self.saved_at = None; self.epoch = self.epoch.wrapping_add(1); } + + /// Photo `id`'s file was replaced by other content: the faces made from the old pixels are dropped and the photo is + /// searched again (issue #1524). What a worker is still making from the old file is dropped when it comes back. + pub fn content_replaced(&mut self, id: lightcraft_catalog::PhotoId) { + self.index.forget_photo(id.0); + self.scanned.remove(id.0); + self.scan_failed.remove(&id.0); + } } diff --git a/crates/engine/src/faces_worker.rs b/crates/engine/src/faces_worker.rs index 81ff6adae..96bd4f124 100644 --- a/crates/engine/src/faces_worker.rs +++ b/crates/engine/src/faces_worker.rs @@ -134,6 +134,9 @@ pub(crate) struct Prepared { /// The session's face epoch when this was prepared: another library has been opened since if it differs. pub epoch: u64, pub id: PhotoId, + /// The photo's content key ([`crate::media::content_key`]) when this was prepared: its file was replaced (Reload + /// after an outside edit) since if it differs. + pub content: String, pub job: RenderJob, /// A raw's embedded preview, tried before the render job (much faster than decoding the raw). pub preview: Option<(String, PreviewLoader)>, @@ -150,6 +153,7 @@ pub(crate) struct Done { pub tag: String, pub epoch: u64, pub id: PhotoId, + pub content: String, /// Each face's region, its embedding (`None` when it could not be made) and the box to show it by. pub results: Vec<(Rect, Option>, Rect)>, /// Faces were to be looked for in this photo. @@ -177,7 +181,7 @@ fn face_rect(f: &Face) -> Option { /// Do the work for one photo. Never panics: whatever goes wrong, its faces come back as `None`. pub(crate) fn process(p: Prepared, embedder: &Embedder) -> Done { - let Prepared { tag, epoch, id, job, preview, todo, detect, detector } = p; + let Prepared { tag, epoch, id, content, job, preview, todo, detect, detector } = p; let fallback = |todo: Vec| todo.into_iter().map(|r| (r, None, r)).collect::>(); let rects = todo.clone(); let run = catch_unwind(AssertUnwindSafe(|| { @@ -238,7 +242,7 @@ pub(crate) fn process(p: Prepared, embedder: &Embedder) -> Done { (results, new_faces) })); let (results, found) = run.unwrap_or_else(|_| (fallback(rects), None)); - Done { tag, epoch, id, results, detect_wanted: detect, found } + Done { tag, epoch, id, content, results, detect_wanted: detect, found } } type Job = (Arc, Prepared); diff --git a/crates/engine/src/files.rs b/crates/engine/src/files.rs index af027ff7b..2c3c85341 100644 --- a/crates/engine/src/files.rs +++ b/crates/engine/src/files.rs @@ -18,7 +18,8 @@ fn meta_of(m: &lightcraft_meta::Metadata) -> (Meta, Option) { let camera = [m.make.clone().unwrap_or_default(), m.model.clone().unwrap_or_default()].join(" ").trim().to_string(); let meta = Meta { camera, - lens: m.lens_model.clone().unwrap_or_default(), + // (a camera that writes no lens name, like Nikon's DSLRs, still says what range the lens covers) + lens: m.lens_model.clone().or_else(|| m.lens_spec.and_then(lightcraft_meta::lens_spec_name)).unwrap_or_default(), focal_mm: m.focal_length.map(|f| f as f32), aperture: m.f_number.map(|f| f as f32), shutter, @@ -499,6 +500,8 @@ fn load_bytes_now( camera_tone, mattes, local_tone, + // (the catalog knows the original's size: `media::source_info`) + native_long: None, }, )); } @@ -517,17 +520,28 @@ fn load_bytes_now( /// (`lightcraft_raw::spectral`), else when its camera's white-balance presets give one /// (`lightcraft_raw::kelvin_presets`): either turns its as-shot multipliers into Kelvin and tint, nothing else — the /// look stays the file's fit. Any other raw is edited relative to its as-shot look (6500 K / 0 means as shot). -struct WbScaleOf { +pub(crate) struct WbScaleOf { own_matrix: bool, relative: bool, /// Kelvin through the camera's measured matrices or presets: edited on the relative scale before those were used. legacy_relative: bool, /// The as-shot white through the measured matrices or the presets, when that is where the scale comes from. scale_white: Option, + /// The camera's measured matrices, when they give the scale. + camera: Option<&'static lightcraft_raw::spectral::Camera>, +} + +/// The white balance a DNG of a raw's camera RGB (Photo Merge) declares so that it shows as its source does +/// ([`WbScaleOf::dng_white`]). +pub(crate) enum DngWhite { + /// Colour tags with the camera's measured matrices: the DNG reads every white through them, as the source does. + Matrices(Box), + /// The white the source's scale shows at As Shot. + At(lightcraft_color::Xy), } impl WbScaleOf { - fn new( + pub(crate) fn new( format: lightcraft_raw::RawFormat, color: &lightcraft_raw::ColorData, wb_multipliers: Option<[f32; 3]>, @@ -544,7 +558,23 @@ impl WbScaleOf { None => None, }; let covered = camera.is_some() || presets; - WbScaleOf { own_matrix, relative: fitted && !covered, legacy_relative: fitted && covered, scale_white } + WbScaleOf { own_matrix, relative: fitted && !covered, legacy_relative: fitted && covered, scale_white, camera } + } + + /// What a DNG of this raw's camera RGB declares so that it shows the white balance as this scale does: the + /// camera's measured matrices when they give the scale, else the white the scale shows at As Shot (the presets' + /// reading; 6500 K / 0 on the relative scale; else `own`, the white the file's tags give). + pub(crate) fn dng_white(&self, color: &lightcraft_raw::ColorData, own: lightcraft_color::Xy) -> DngWhite { + if let Some(camera) = self.camera { + let mut c = lightcraft_raw::ColorData { profile: Default::default(), kelvin_presets: None, ..color.clone() }; + if camera.fill(&mut c) { + return DngWhite::Matrices(Box::new(c)); + } + } + if self.relative { + return DngWhite::At(lightcraft_color::cct::temp_tint_to_xy(6500.0, 0.0)); + } + DngWhite::At(self.scale_white.unwrap_or(own)) } /// The as-shot temperature and tint on the file's scale: the relative scale's reference, the white through /// the measured matrices or the presets, else `own` (the white the file's own tags give). Whole numbers, as the @@ -1051,10 +1081,24 @@ impl crate::Session { } #[cfg(test)] -mod tests { +pub(crate) mod tests { /// Keywords from a file keep their hierarchy: the paths of `lr:hierarchicalSubject`, not the /// flat names of `dc:subject` that LightCraft's exports (and Lightroom Classic) write besides /// them; an exported `travel|Italy|Rome` comes back as itself. + #[test] + fn a_lens_without_a_name_is_shown_by_its_range() { + // a Nikon DSLR raw: no LensModel, the range from the maker note + let m = lightcraft_meta::Metadata { lens_spec: Some([18.0, 135.0, 3.5, 5.6]), ..Default::default() }; + assert_eq!(super::meta_of(&m).0.lens, "18.0-135.0 mm f/3.5-5.6"); + let named = lightcraft_meta::Metadata { + lens_model: Some("NIKKOR Z 24-70mm f/4 S".into()), + lens_spec: Some([24.0, 70.0, 4.0, 4.0]), + ..Default::default() + }; + assert_eq!(super::meta_of(&named).0.lens, "NIKKOR Z 24-70mm f/4 S"); + assert_eq!(super::meta_of(&lightcraft_meta::Metadata::default()).0.lens, ""); + } + #[test] fn imported_keywords_keep_their_hierarchy() { let m = lightcraft_meta::Metadata { @@ -1128,6 +1172,40 @@ mod tests { assert!(!lightcraft_catalog::relative_wb_camera("DNG", "SONY ILCE-7M3")); } + /// A merged DNG (Photo Merge) of a raw without colour matrices shows the As Shot temperature and tint its source + /// shows, on every scale: through the measured matrices (ILCE-7M3), through the presets (ILCA-99M2), the relative + /// scale's 6500 K / 0 (ILCE-7M2) and a file's own white (an unfitted format). Merges of real ILCE-7M3 brackets + /// opened at 6113–7307 K / +115 where the ARWs show 3932–5268 K / +7. + #[test] + fn a_merged_dng_shows_the_white_balance_its_source_shows() { + use lightcraft_raw::{ColorData, RawFormat}; + let meta = |make: &str, model: &str| lightcraft_meta::Metadata { make: Some(make.into()), model: Some(model.into()), ..Default::default() }; + let g = |r: f32, b: f32| [r / 1024.0, 1.0, b / 1024.0]; + let presets = lightcraft_raw::kelvin_presets::KelvinPresets::new([ + (2500.0, g(1336.0, 3672.0)), + (3200.0, g(1720.0, 2620.0)), + (4500.0, g(2256.0, 1880.0)), + (6000.0, g(2680.0, 1544.0)), + (8500.0, g(3136.0, 1300.0)), + ]); + let with_presets = ColorData { kelvin_presets: presets, ..Default::default() }; + let cases = [ + (RawFormat::Arw, ColorData::default(), [2.5039f32, 1.0, 1.5078], meta("SONY", "ILCE-7M3")), + (RawFormat::Arw, with_presets, g(2500.0, 1668.0), meta("SONY", "ILCA-99M2")), + (RawFormat::Arw, ColorData::default(), [2.2, 1.0, 1.6], meta("SONY", "ILCE-7M2")), + (RawFormat::Rw2, ColorData::default(), [2.0, 1.0, 1.4], meta("Panasonic", "DC-G9")), + ]; + for (format, color, mul, meta) in cases { + let own = lightcraft_raw::color::as_shot_white_of(&color, Some(mul)); + let source = WbScaleOf::new(format, &color, Some(mul), &meta); + let t = lightcraft_raw::color::camera_transform_of(&color, own); + let dng = crate::camera_preview::dng_colour(&color, &source.dng_white(&color, own), &t, None).unwrap(); + let merged = WbScaleOf::new(RawFormat::Dng, &dng, None, &meta); + let shown = source.as_shot(own); + assert_eq!(merged.as_shot(lightcraft_raw::color::as_shot_white_of(&dng, None)), shown, "{:?}", meta.model); + } + } + /// A RAF whose preview pointer is in its header, then a large raw tail the reader must not need. fn raf_with_embedded_jpeg(jpeg: &[u8], tail: usize) -> Vec { let head = 160; @@ -1636,7 +1714,7 @@ mod tests { } /// A CR3-shaped file (not decodable yet) whose only content is a `PRVW` preview box. - fn cr3_with_preview(w: u32, h: u32) -> Vec { + pub(crate) fn cr3_with_preview(w: u32, h: u32) -> Vec { let px: Vec = (0..w * h).flat_map(|i| [(i % 251) as u8, 128, 200]).collect(); let jpeg = encode_jpeg(&EncodeImage::new(w, h, 3, Samples::U8(&px)), 90, ChromaSubsampling::S444, &EncodeMeta::default()).unwrap(); let mut b = b"\0\0\0\x18ftypcrx \0\0\0\x01crx isom".to_vec(); @@ -2160,10 +2238,13 @@ mod tests { let p = s.catalog.photo(id).unwrap().clone(); let why = p.preview_only.clone().expect("marked preview only"); assert!(why.to_uppercase().contains("CR3"), "the decoder's reason: {why}"); + // stamped with the version that decided it, so a later version rechecks it (#662) + assert_eq!(p.preview_checked, lightcraft_catalog::APP_VERSION); + assert!(!p.preview_only_stale()); assert_eq!(p.kind, MediaKind::Raw, "still a raw file (filters, Convert to DNG…)"); assert!(!p.develops_raw()); // rendered like the JPEG it is: relative white balance, display tone curve, no raw defaults - assert_eq!(crate::media::source_info(&p), SourceInfo::default()); + assert_eq!(SourceInfo { native_long: None, ..crate::media::source_info(&p) }, SourceInfo::default()); assert_eq!(*p.develop, lightcraft_develop::DevelopSettings::default()); assert!(s.render_job(id, 48, 48, false, true).unwrap().run().rendered.is_ok()); // agents see it @@ -2177,14 +2258,17 @@ mod tests { let mut s = crate::Session::new().with_fs(); s.open_library(&lib, false).unwrap(); assert_eq!(s.catalog.photo(id).unwrap().preview_only.as_deref(), Some(why.as_str())); + assert_eq!(s.catalog.photo(id).unwrap().preview_checked, lightcraft_catalog::APP_VERSION); // the file decodes now (here: replaced by a DNG): Reload clears it, undo brings it back std::fs::write(&path, crate::tests_xmp::synthetic_dng_with(None, lightcraft_meta::Metadata::default())).unwrap(); let r = s.execute("photo.reload", &json!({"ids": [id.0]})).unwrap(); assert_eq!(r["reloaded"], json!([id.0]), "{r}"); assert!(s.catalog.photo(id).unwrap().develops_raw()); + assert_eq!(s.catalog.photo(id).unwrap().preview_checked, "", "the stamp goes with the flag"); assert!(crate::media::source_info(s.catalog.photo(id).unwrap()).raw); s.execute("edit.undo", &json!({})).unwrap(); assert_eq!(s.catalog.photo(id).unwrap().preview_only.as_deref(), Some(why.as_str())); + assert_eq!(s.catalog.photo(id).unwrap().preview_checked, lightcraft_catalog::APP_VERSION); // relinking to a file that decodes clears it too let dng = files.join("DSC_0001.dng"); std::fs::write(&dng, crate::tests_xmp::synthetic_dng_with(None, lightcraft_meta::Metadata::default())).unwrap(); @@ -2403,4 +2487,112 @@ mod tests { assert!(q.rendered.is_ok(), "{:?}", q.rendered.err()); 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` + /// lays it out) whose IFD carries the full-frame 16–35 mm distortion table (tag 0x7037). + fn sony_arw(model: &str) -> Vec { + use lightcraft_tiff::tags::{self as t, photometric}; + use lightcraft_tiff::{ByteOrder, IfdBuilder, ImageData, TiffWriter, Value}; + // 3:2, the aspect Sony's lens opcodes are limited to + let (w, h) = (72u32, 48u32); + let mut raw = IfdBuilder::new(); + raw.set(t::NEW_SUBFILE_TYPE, Value::Long(vec![0])); + raw.set(t::IMAGE_WIDTH, Value::Long(vec![w])); + raw.set(t::IMAGE_LENGTH, Value::Long(vec![h])); + raw.set(t::BITS_PER_SAMPLE, Value::Short(vec![14])); + raw.set(t::COMPRESSION, Value::Short(vec![1])); + raw.set(t::PHOTOMETRIC, Value::Short(vec![photometric::CFA])); + raw.set(t::CFA_REPEAT_PATTERN_DIM, Value::Short(vec![2, 2])); + raw.set(t::CFA_PATTERN_EP, Value::Byte(vec![0, 1, 1, 2])); + raw.set(0x7037, Value::SShort(vec![16, 0, 2, 7, 16, 28, 43, 61, 83, 109, 139, 175, 217, 267, 325, 395, 479])); + raw.set_image(ImageData::Strips { rows_per_strip: h, strips: vec![2000u16.to_le_bytes().repeat((w * h) as usize)] }); + let mut ifd0 = IfdBuilder::new(); + ifd0.set(t::MAKE, Value::Ascii("SONY".into())); + ifd0.set(t::MODEL, Value::Ascii(model.into())); + ifd0.add_sub_ifd(raw); + TiffWriter::new(ByteOrder::Little, false).write(&[ifd0]).unwrap() + } + + /// Sony's embedded distortion correction is on by default: an ARW of a verified body imports with "Enable Lens + /// Corrections" on and still counts as unedited; another body's tables aren't read, so its correction is off. + #[test] + fn arw_with_lens_data_imports_with_profile_corrections_on() { + let dir = std::env::temp_dir().join(format!("lc-arw-lens-default-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + std::fs::create_dir_all(&dir).unwrap(); + let mut s = crate::Session::new().with_fs(); + for (model, on) in [("ILCE-7RM6", true), ("ILCE-7M4", false)] { + let path = dir.join(format!("{model}.arw")); + std::fs::write(&path, sony_arw(model)).unwrap(); + let r = s.execute("library.import", &serde_json::json!({"paths": [path.to_string_lossy()]})).unwrap(); + let p = s.catalog.photo(lightcraft_catalog::PhotoId(r["imported"][0].as_u64().unwrap())).unwrap(); + assert_eq!((p.format.as_str(), p.embedded_lens.is_some(), p.develop.optics.lens_profile), ("ARW", on, on), "{model}"); + assert!(!p.is_edited(), "{model}: as imported"); + } + let _ = std::fs::remove_dir_all(&dir); + } + + /// RW2 and DNG keep their default: the correction is on when the file carries one (RW2 distortion, a DNG's + /// opcodes), off when it doesn't. + #[test] + fn rw2_and_dng_defaults_unchanged() { + use lightcraft_catalog::{Photo, PhotoId, Source}; + let s = crate::Session::new(); + let lens = lightcraft_develop::EmbeddedLens { warp: Some(Default::default()), ..Default::default() }; + for format in ["RW2", "DNG"] { + for embedded in [None, Some(lens)] { + let name = format!("x.{format}"); + let mut p = Photo::new(PhotoId(1), Source::File { path: format!("/{name}") }, &name, format, 64, 48, ""); + p.kind = MediaKind::Raw; + p.embedded_lens = embedded; + crate::import::apply_import_defaults(&s, &mut p); + assert_eq!(p.develop.optics.lens_profile, embedded.is_some(), "{format} {embedded:?}"); + } + } + } + + /// Stored settings stay as they are when the library opens again: an ARW catalogued before its lens data was read + /// (none, correction off) keeps the correction off and counts as unedited, and one whose user switched the + /// correction off keeps it off. + #[test] + fn existing_photo_settings_are_not_rewritten() { + use lightcraft_catalog::{Op, PhotoId}; + let dir = std::env::temp_dir().join(format!("lc-arw-lens-existing-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + let (lib, files) = (dir.join("lib"), dir.join("files")); + std::fs::create_dir_all(&files).unwrap(); + let mut s = crate::Session::new().with_fs(); + s.open_library(&lib, false).unwrap(); + let import = |s: &mut crate::Session, name: &str| { + let path = files.join(name); + // trailing bytes of its own: not a duplicate of the other file + std::fs::write(&path, [sony_arw("ILCE-7RM6"), name.as_bytes().to_vec()].concat()).unwrap(); + let r = s.execute("library.import", &serde_json::json!({"paths": [path.to_string_lossy()]})).unwrap(); + PhotoId(r["imported"][0].as_u64().unwrap()) + }; + let old = import(&mut s, "old.arw"); + // as a release that didn't read the Sony tables catalogued it + let mut d = (*s.catalog.photo(old).unwrap().develop).clone(); + d.optics.lens_profile = false; + let ops = vec![ + Op::SetEmbeddedLens { id: old, lens: None }, + Op::SetDevelop { id: old, settings: std::sync::Arc::new(d), label: "x".into(), edited: None }, + ]; + s.commit("x", Op::Batch { ops }).unwrap(); + let off = import(&mut s, "off.arw"); + s.execute("develop.merge", &serde_json::json!({"settings": {"optics": {"lens_profile": false}}})).unwrap(); + let before: Vec<_> = [old, off].iter().map(|id| s.catalog.photo(*id).unwrap().clone()).collect(); + assert!(before.iter().all(|p| !p.develop.optics.lens_profile)); + s.persist().unwrap(); + drop(s); + let mut s = crate::Session::new().with_fs(); + s.open_library(&lib, false).unwrap(); + for b in &before { + let p = s.catalog.photo(b.id).unwrap(); + assert_eq!((&p.develop, &p.embedded_lens), (&b.develop, &b.embedded_lens), "{}", p.file_name); + } + assert!(!s.catalog.photo(old).unwrap().is_edited(), "still as imported"); + drop(s); + let _ = std::fs::remove_dir_all(&dir); + } } diff --git a/crates/engine/src/import.rs b/crates/engine/src/import.rs index ecb80741e..307595feb 100644 --- a/crates/engine/src/import.rs +++ b/crates/engine/src/import.rs @@ -39,7 +39,7 @@ pub const EXTENSIONS: &[&str] = &[ // 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 - "iiq", "crw", "mrw", "x3f", "kdc", "mos", "erf", "3fr", "fff", + "iiq", "crw", "mrw", "x3f", "kdc", "mos", "erf", "3fr", "fff", "mef", ]; #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] @@ -310,6 +310,8 @@ pub struct ImportDefaults { pub auto_folder: Option, pub auto_copy: bool, pub auto_album: Option, + /// Open the import review when a camera or memory card is connected. + pub show_on_card: bool, } #[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)] @@ -470,6 +472,12 @@ pub(crate) fn probe_paths(s: &Session, paths: &[String]) -> Vec Vec> { + let progress = ScanProgress::default(); + probe_all(Some(probe), paths, &progress.done, &progress.cancel) +} + /// Probe `paths` (several at once), counting each in `done`. Stops before the next file once /// `cancel` is set: the files not reached come back as errors. fn probe_all( @@ -1386,7 +1394,7 @@ pub fn commit_prepared(s: &mut Session, opts: &ImportOptions, now: &str, prepare p.as_shot_wb = info.as_shot_wb; p.content_hash = info.content_hash; p.embedded_lens = info.embedded_lens; - p.preview_only = info.preview_only.clone(); + p.set_preview_only(info.preview_only.clone()); apply_import_defaults(s, &mut p); if let Some(sc) = &sidecar { crate::sidecar::merge_into(&mut p, sc, &now); @@ -1532,6 +1540,7 @@ impl Session { stages: None, view_cache: None, display: None, + slide: None, preview_cut: None, }; let embedded = match (&self.media.preview_loader, c.kind) { diff --git a/crates/engine/src/import_move.rs b/crates/engine/src/import_move.rs index c92ee2796..06f22be40 100644 --- a/crates/engine/src/import_move.rs +++ b/crates/engine/src/import_move.rs @@ -128,7 +128,10 @@ pub(crate) fn sidecars_of(src: &Path) -> Vec<(PathBuf, bool)> { if !ext.eq_ignore_ascii_case("xmp") || !e.path().is_file() { return None; } - if base == name { + // `IMG_1.cr3.xmp` names `IMG_1.CR3` too: some apps lower-case the photo's extension + let full = base == name + || base.rsplit_once('.').is_some_and(|(s, x)| s == stem && name.rsplit_once('.').is_some_and(|(_, y)| x.eq_ignore_ascii_case(y))); + if full { Some((e.path(), false)) } else if base == stem { Some((e.path(), true)) diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index 0f9be6e52..7df97e837 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -55,6 +55,7 @@ pub mod memory; pub mod merge; mod model_download; pub mod originals; +mod pdf; pub mod preset_import; pub mod preset_luminar; pub mod presets; @@ -62,8 +63,15 @@ pub mod proof; pub mod rename; pub mod segment; pub mod sidecar; +pub mod slide; +pub mod slide_overlay; +pub mod slide_template; +pub mod slide_title; +pub mod slideshow; +pub mod slideshow_export; pub mod smart; pub mod sync; +pub mod text; mod view; pub mod walk; @@ -172,6 +180,9 @@ pub struct Interaction { pub original: Arc, } +/// 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; + /// 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); @@ -190,6 +201,21 @@ pub struct Session { /// Shared, so frontends can hold it across frames without copying ([`Session::visible_shared`]). visible: Arc<[PhotoId]>, visible_key: Option<(u64, 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, + /// The view last worked out, the catalog's revision then, and the photos in it before stacks are arranged. + /// A small edit is followed from it: only the photos the edit touched are asked about again and put at their + /// place in the order ([`Session::visible`]). It is also what a held view holds ([`Session::hold_view`]). + /// Another source, filter or sort is another view, and so is a smart album whose rules were changed. + base: Option<(String, u64, Vec)>, + /// How many times the view followed an edit from the list it had, and how many times it was worked out from + /// every photo (`ui.inspect` → `perf`): what tells a slow library from a slow edit. + pub view_followed: u64, + pub view_worked_out: u64, + /// Tests: work the view out whole every time (what following has to equal). + #[cfg(test)] + never_follow: bool, /// Identifies the current `visible` list: a new value (unique in the process) every time it /// is recomputed, so frontends can key their per-view caches on it instead of hashing the ids. visible_gen: u64, @@ -206,6 +232,9 @@ pub struct Session { /// Set by a command whose change must not rewrite the photo's XMP sidecar even with auto-write on /// (a catalog-only edit of data the sidecar writer does not emit); consumed when the command ends. pub(crate) skip_auto_write: bool, + /// Photos whose automatic XMP sidecar write failed (their folder offline, read-only or full), written again by + /// [`Session::retry_unsaved_sidecars`] once their file is reachable. + pub(crate) unsaved_sidecars: std::collections::BTreeSet, /// AI denoise: the model in use, the photos that have their picture and the work in progress. pub(crate) denoise: denoise::State, /// Where the host keeps face models (one folder each); `None` where there is no file system (the web). @@ -269,6 +298,11 @@ pub struct Session { /// The user's point-curve presets (`curve.*`; built-ins: [`cmd::curves::builtin_presets`]), /// persisted in prefs.json. pub curve_presets: Vec, + /// Mask effect presets (the Masking panel's Effect menu). + pub mask_effects: Vec, + /// The effect a new mask of each kind (`brush`, `linear`…) starts with: the one last applied + /// to a mask of that kind. + pub mask_tool_effects: std::collections::BTreeMap, /// Saved colour-label name sets. pub label_sets: Vec, /// Look-alike signatures by content key (Find Similar). @@ -296,12 +330,33 @@ pub struct Session { /// Background tasks in flight (imports, exports, preview builds…), for the activity stack and /// `activity.list` / `activity.cancel`. pub activity: activity::Activity, + /// The last (or running) slideshow export (`slideshow.export`). + pub slideshow_export: Option>, /// The last (or running) Build Previews. pub preview_build: Option>, /// Develop defaults applied on import (persisted in prefs.json). pub import_defaults: import::ImportDefaults, /// Disk budget of the library's thumbnail cache in MB (0 = default; persisted in prefs.json). pub cache_mb: u32, + /// Slideshow playback settings (`slideshow.playback`; persisted in prefs.json). + pub slideshow: slideshow::PlaybackSettings, + /// Slideshow slide layout: Layout, Options and Backdrop (`slideshow.layout`; persisted in + /// prefs.json). + pub slide_layout: slide::SlideSettings, + /// Slideshow overlays (identity plate, rating stars, text overlays, watermark). + pub slide_overlays: slide_overlay::OverlaySettings, + /// Slideshow intro and ending screens (`slideshow.titles`; persisted in prefs.json). + pub slide_titles: slide_title::TitleSettings, + /// Where user slideshow templates are kept (`/slideshow-templates`; + /// [`Session::with_default_slide_templates`]). `None`: the built-in templates only. + pub slide_templates_dir: Option, + /// The slideshow settings before the last template was applied ("Previous settings"; this + /// session only: library preferences have no undo). + pub slide_templates_previous: Option, + /// The templates as last listed. + pub(crate) slide_templates_cache: slide_template::Cache, + /// The background image the slides last rendered (shared by slide jobs: rendered once). + pub slide_backdrop: std::sync::Arc, /// Where smart previews are kept when not in the library folder (persisted in prefs.json). pub smart_previews_dir: Option, /// Untouched Local records of folders not browsed for this many days are forgotten when the @@ -345,6 +400,12 @@ impl Session { selection: Selection::default(), visible: Vec::new().into(), visible_key: None, + hold_view: false, + base: None, + view_followed: 0, + view_worked_out: 0, + #[cfg(test)] + never_follow: false, visible_gen: 0, total: None, undo: Vec::new(), @@ -353,6 +414,7 @@ impl Session { pending_sidecar_repairs: Vec::new(), interaction: None, skip_auto_write: false, + unsaved_sidecars: Default::default(), denoise: Default::default(), face_models_dir: None, face_downloads: Default::default(), @@ -385,6 +447,8 @@ impl Session { metadata_presets: Vec::new(), filter_presets: Vec::new(), curve_presets: Vec::new(), + mask_effects: Vec::new(), + mask_tool_effects: Default::default(), label_sets: Vec::new(), signatures: Default::default(), lut_profiles: Vec::new(), @@ -398,8 +462,17 @@ impl Session { folder_changes: None, activity: Default::default(), preview_build: None, + slideshow_export: None, import_defaults: import::ImportDefaults::default(), cache_mb: 0, + slideshow: slideshow::PlaybackSettings::default(), + slide_layout: slide::SlideSettings::default(), + slide_overlays: slide_overlay::OverlaySettings::default(), + slide_titles: slide_title::TitleSettings::default(), + slide_templates_dir: None, + slide_templates_previous: None, + slide_templates_cache: None, + slide_backdrop: Default::default(), smart_previews_dir: None, forget_local_days: lightcraft_catalog::DEFAULT_FORGET_DAYS, #[cfg(not(target_arch = "wasm32"))] @@ -509,10 +582,16 @@ impl Session { // Retain only cache IDs while the catalog consumes the operation. Cloning an import // batch here also cloned every photo, although commit already keeps the journal copy. let mut invalidated = Vec::new(); + // photos whose file was replaced: the faces made from the old pixels go too (issue #1524) + let mut replaced = Vec::new(); let mut pending = vec![&op]; while let Some(op) = pending.pop() { match op { - Op::SetFile { id, .. } | Op::Relink { id, .. } | Op::SetContent { id, .. } | Op::RemovePhoto { id } => invalidated.push(*id), + Op::SetContent { id, .. } => { + invalidated.push(*id); + replaced.push(*id); + } + Op::SetFile { id, .. } | Op::Relink { id, .. } | Op::RemovePhoto { id } => invalidated.push(*id), Op::AddPhoto { photo } => invalidated.push(photo.id), Op::Batch { ops } => pending.extend(ops), _ => {} @@ -522,6 +601,12 @@ impl Session { for id in invalidated { self.media.forget(id); } + #[cfg(not(target_arch = "wasm32"))] + for id in replaced { + self.faces.content_replaced(id); + } + #[cfg(target_arch = "wasm32")] + let _ = replaced; Ok(inv) } @@ -547,8 +632,8 @@ impl Session { } /// Apply an op that is LightCraft's own bookkeeping rather than something the user did (faces found by the background - /// scan, a raw's as-shot white read by a decode): journaled like any op, but not an undo step, and it leaves the - /// redo stack alone. + /// scan, a raw's as-shot white read by a decode, raws rechecked after an update): journaled like any op, but not + /// an undo step, and it leaves the redo stack alone. pub(crate) fn apply_system(&mut self, op: Op) -> Result<()> { let fwd = op.clone(); self.catalog.apply(op)?; @@ -859,6 +944,16 @@ impl Session { pub fn visible(&mut self) -> &[PhotoId] { let mut key = (self.catalog.revision, format!("{:?}|{:?}|{:?}|{:?}|{:?}", self.source, self.filter, self.sort, self.browse, self.library_folder)); + // What a held view is told apart by: what is shown and how, and for a smart album its rules (rules that were + // changed ask something else). Not the checks of Missing Photos below: they fill the view in, they are + // no other view. + let asks = matches!(self.source, LibrarySource::Picks) + || matches!(self.source, LibrarySource::Album(a) if self.catalog.album(a).is_some_and(|al| al.is_smart())); + let rules = match self.source { + LibrarySource::Album(a) if asks => self.catalog.album(a).map(|al| format!("{:?}", al.smart)).unwrap_or_default(), + _ => String::new(), + }; + 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())); @@ -866,18 +961,58 @@ impl Session { 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 mut f = self.source.to_filter(&self.filter, &self.catalog); - if self.source == LibrarySource::Folder { - // no folder chosen: nothing (an empty path matches nothing) - let b = self.browse.clone().unwrap_or_default(); - f.folder = Some(b.path); - f.subfolders = b.subfolders; + 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 + // a view that is only a question, else in the album or folder shown. Taking a photo out of an album + // or the library takes it out of the view at once, held or not. + // (only a view that asks something can lose a photo to an edit: the others are queried as ever, at no + // cost for the hold) + let holds = self.hold_view && (asks || self.filter != Filter::default()); + let within = holds.then(|| { + if asks { + self.view_filter(LibrarySource::All, &Filter::default()) + } else if self.source == LibrarySource::RecentlyAdded { + // (how far back it reaches was worked out for `f`: not a second pass over the library) + Filter { imported_from: f.imported_from.clone(), ..Default::default() } + } else { + self.view_filter(self.source, &Filter::default()) + } + }); + // Views whose order or photos are not the query's alone are worked out whole each time: an album in + // its own order, Recently Added (how far back it reaches moves with every import), Missing Photos. + let own_order = matches!(self.source, LibrarySource::Album(a) if self.sort.key == lightcraft_catalog::SortKey::CaptureDate + && self.filter == Filter::default() + && self.catalog.album(a).is_some_and(|al| !al.is_smart())); + 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 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. + #[cfg(test)] + let clocked = clocked || self.never_follow; + let followed = base.as_ref().filter(|_| !whole && !clocked).and_then(|(_, revision, ids)| { + let touched = self.catalog.touched_since(*revision).filter(|touched| touched.len() <= MAX_FOLLOWED)?; + Some(self.catalog.follow(ids, &touched, &f, &self.sort, within.as_ref())) + }); + match followed { + Some(_) => self.view_followed += 1, + None => self.view_worked_out += 1, } - if self.source == LibrarySource::LibraryFolder { - // no folder chosen: nothing (`.` names no folder) - f.library_folder = Some(self.library_folder.clone().unwrap_or_else(|| ".".into())); - } - let mut visible = self.catalog.query(&f, &self.sort); + let mut visible = match (followed, &base, &within) { + (Some(ids), _, _) => ids, + (None, Some((_, _, held)), Some(within)) => { + let keep: std::collections::HashSet = held.iter().copied().collect(); + self.catalog.query_keeping(&f, &self.sort, &keep, within) + } + _ => self.catalog.query(&f, &self.sort), + }; + // what the view shows now is what the next edit is followed from, and what it holds until it is + // refreshed (taken before stacks are arranged, so a photo folded into a stack is held with it) + self.base = (!whole || holds).then(|| (view, self.catalog.revision, visible.clone())); if visible.is_empty() && self.source == LibrarySource::LibraryFolder && !self.folder_holds_photos() { // the shown folder lost its last photo (deleted, moved, removed): everything, not // an empty grid under the name of a folder that is gone from the sidebar @@ -927,6 +1062,43 @@ impl Session { &self.visible } + /// `source` as a filter over `base` (the filter bar's, or none), with the folder it shows. + fn view_filter(&self, source: LibrarySource, base: &Filter) -> Filter { + let mut f = source.to_filter(base, &self.catalog); + if source == LibrarySource::Folder { + // no folder chosen: nothing (an empty path matches nothing) + let b = self.browse.clone().unwrap_or_default(); + f.folder = Some(b.path); + f.subfolders = b.subfolders; + } + if source == LibrarySource::LibraryFolder { + // no folder chosen: nothing (`.` names no folder) + f.library_folder = Some(self.library_folder.clone().unwrap_or_else(|| ".".into())); + } + f + } + + /// Let go of what a held view kept: the view shows what matches now ([`Session::hold_view`]). + pub fn refresh_view(&mut self) { + self.base = None; + self.visible_key = None; + } + + /// [`Session::visible`] worked out from every photo, whatever was followed before: what following an edit + /// has to equal. + /// Nothing the session keeps is changed by asking: the next edit is followed from where the last one left the + /// 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 { + let kept = (self.base.clone(), self.visible_key.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; + whole + } + /// Whether the library still holds a photo imported from the folder a `LibraryFolder` source /// shows. fn folder_holds_photos(&self) -> bool { @@ -940,6 +1112,16 @@ impl Session { if self.source == LibrarySource::Missing { return None; } + if self.filter == Filter::default() && !self.hold_view { + // nothing narrows the source: what the view lists is all of it, and is counted already (not a view + // that holds photos that stopped matching: those are not of the source any more) + let _ = self.visible(); + if let Some((_, revision, ids)) = &self.base + && *revision == self.catalog.revision + { + return Some(ids.len()); + } + } 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 mut f = self.source.to_filter(&Filter::default(), &self.catalog); @@ -951,7 +1133,8 @@ impl Session { if self.source == LibrarySource::LibraryFolder { f.library_folder = Some(self.library_folder.clone().unwrap_or_else(|| ".".into())); } - let n = self.catalog.query(&f, &self.sort).len(); + // (how many, not which: nothing to put in order) + let n = self.catalog.count(&f); self.total = Some((key, n)); } self.total.as_ref().map(|t| t.1) @@ -961,6 +1144,21 @@ impl Session { self.visible().to_vec() } + /// After the source changes, the active photo follows the rule every source shares: it stays + /// when the view shows it, else the view's first photo becomes the active (and only selected) + /// one, or nothing when the view is empty. A view never keeps an active photo it doesn't show + /// (issue #1447: a Local folder kept a library photo active, so Detail and the info showed a + /// photo the grid didn't). + pub fn keep_active_in_view(&mut self) { + let active = self.selection.active; + let vis = self.visible(); + if active.is_some_and(|a| vis.contains(&a)) { + return; + } + let first = vis.first().copied(); + self.selection = first.map(Selection::single).unwrap_or_default(); + } + /// The visible photos without copying them, with their generation: equal generations mean /// the very same list (same catalog revision, source, filter, sort), so a frontend can cache /// whatever it derives from the list under that number. @@ -1002,6 +1200,8 @@ mod tests; #[cfg(test)] mod tests_album_order; #[cfg(test)] +mod tests_album_remove; +#[cfg(test)] mod tests_albums_local; #[cfg(test)] mod tests_color; @@ -1028,6 +1228,8 @@ mod tests_forget_local; #[cfg(test)] mod tests_history_cursor; #[cfg(test)] +mod tests_hold_view; +#[cfg(test)] mod tests_import; #[cfg(test)] mod tests_import_move; @@ -1040,6 +1242,8 @@ mod tests_library; #[cfg(test)] mod tests_lightroom_folder; #[cfg(test)] +mod tests_mask_effects; +#[cfg(test)] mod tests_merge; #[cfg(test)] mod tests_organize; @@ -1052,12 +1256,20 @@ mod tests_presets_scale; #[cfg(test)] mod tests_process; #[cfg(test)] +mod tests_recheck; +#[cfg(test)] mod tests_segment; #[cfg(test)] mod tests_settings_files; #[cfg(test)] +mod tests_slide; +#[cfg(test)] +mod tests_slideshow_export; +#[cfg(test)] mod tests_spots; #[cfg(test)] mod tests_sync; #[cfg(test)] +mod tests_view_follow; +#[cfg(test)] mod tests_xmp; diff --git a/crates/engine/src/library.rs b/crates/engine/src/library.rs index 0305623f2..74a1b3985 100644 --- a/crates/engine/src/library.rs +++ b/crates/engine/src/library.rs @@ -215,6 +215,10 @@ struct PrefsFile { filter_presets: Vec, /// Point-curve presets. curve_presets: Vec, + /// Mask effect presets. + mask_effects: Vec, + /// The effect new masks of each kind start with. + mask_tool_effects: std::collections::BTreeMap, /// Colour-label name sets. label_sets: Vec, /// Imported LUT profiles. @@ -232,6 +236,16 @@ struct PrefsFile { /// Days after which untouched Local records of unbrowsed folders are forgotten (missing = /// the default, 0 = never). forget_local_days: Option, + /// Slideshow playback settings. + slideshow: crate::slideshow::PlaybackSettings, + /// Slideshow slide layout (margins, options, backdrop). + slide_layout: crate::slide::SlideSettings, + /// Slideshow overlays. + slide_overlays: crate::slide_overlay::OverlaySettings, + /// Slideshow intro and ending screens. + slide_titles: crate::slide_title::TitleSettings, + /// A view holds the photos it shows until it is refreshed (`library.holdView`). + hold_view: bool, } fn presets_json(s: &Session) -> String { @@ -322,6 +336,38 @@ impl SettingsLoad { } impl Session { + /// Take over what the thumbnail cache wrote under its name of before (`.jpg` files, which + /// photo galleries took for the person's photos: issues #610, #1066), once for a cache + /// folder that was not made or tidied by this build yet. On a thread of its own, with a row + /// in the activity stack that can stop it: a large cache is some 100 000 files to rename. + fn tidy_thumbnail_cache(&mut self) { + #[cfg(not(target_arch = "wasm32"))] + { + use std::sync::atomic::{AtomicBool, Ordering}; + let cache = self.media.rendered.clone(); + if !cache.disk().is_some_and(|d| d.needs_tidying()) { + return; + } + let stop = std::sync::Arc::new(AtomicBool::new(false)); + let task = self.activity.start("cache", "Tidying the thumbnail cache", crate::activity::Cancel::Flag(stop.clone())); + let spawned = std::thread::Builder::new().name("lc-thumbs-tidy".into()).spawn(move || { + // the row stays while this runs (it is dropped with the closure, however that ends) + let _row = task; + if let Some(disk) = cache.disk() { + // (also given up when the library is closed or another is opened: the cache + // is retired then, and it is gone on with when this library opens again) + let stopped = || stop.load(Ordering::Relaxed) || cache.generation() == u64::MAX; + let taken = disk.take_over_old(&stopped, &|done, of| _row.progress(done as u64, of as u64)); + log::info!("thumbnail cache: {taken} thumbnails taken over under their new name in {}", disk.dir().display()); + } + }); + // (not started: the thumbnails are still found one by one, and it is tried at the next open) + if let Err(e) = spawned { + log::warn!("thumbnail cache: could not be tidied: {e}"); + } + } + } + /// Check, without closing the library open now, that [`Session::open_library`] can be expected /// to open `dir`: not a Lightroom catalog, not some other file, a readable folder (or one not /// made yet), not locked by another program. Returns the library folder ([`resolve_dir`]). @@ -422,6 +468,8 @@ impl Session { static LOADS: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0); catalog.revision = LOADS.fetch_add(1, std::sync::atomic::Ordering::Relaxed).wrapping_add(1) << 32; self.catalog = catalog; + // (nothing of the list the last library showed is this one's) + self.refresh_view(); self.library_generation = crate::LIBRARY_GEN.fetch_add(1, std::sync::atomic::Ordering::Relaxed); for entry in self.undo.iter_mut().chain(self.redo.iter_mut()) { self.pending_sidecar_repairs.append(&mut entry.retry_sidecars); @@ -446,6 +494,8 @@ impl Session { self.metadata_presets = prefs.metadata_presets; self.filter_presets = prefs.filter_presets; self.curve_presets = prefs.curve_presets; + self.mask_effects = prefs.mask_effects; + self.mask_tool_effects = prefs.mask_tool_effects; self.label_sets = prefs.label_sets; self.lut_profiles = prefs.lut_profiles; crate::cmd::lut_profiles::register_all(self); @@ -454,7 +504,13 @@ impl Session { self.recent_keywords = prefs.recent_keywords; self.import_defaults = prefs.import; self.cache_mb = prefs.cache_mb; + self.slideshow = prefs.slideshow.sanitized(); + self.slide_layout = prefs.slide_layout.sanitized(); + self.slide_overlays = prefs.slide_overlays.sanitized(); + self.slide_titles = prefs.slide_titles.sanitized(); self.forget_local_days = prefs.forget_local_days.unwrap_or(lightcraft_catalog::DEFAULT_FORGET_DAYS); + self.hold_view = prefs.hold_view; + self.refresh_view(); self.smart_previews_dir = prefs.smart_previews_dir.filter(|_| on_disk).map(PathBuf::from); if let Some(d) = &self.smart_previews_dir { self.media.smart_dir = Some(d.clone()); @@ -504,6 +560,7 @@ impl Session { self.media.clear_sources(); if on_disk { self.media.attach_disk_cache(&dir.join("thumbs"), self.cache_bytes()); + self.tidy_thumbnail_cache(); } let view_written = self.view_json(); self.library = Some(Library { @@ -690,6 +747,8 @@ impl Session { metadata_presets: self.metadata_presets.clone(), filter_presets: self.filter_presets.clone(), curve_presets: self.curve_presets.clone(), + mask_effects: self.mask_effects.clone(), + mask_tool_effects: self.mask_tool_effects.clone(), label_sets: self.label_sets.clone(), lut_profiles: self.lut_profiles.clone(), keyword_sets: self.keyword_sets.clone(), @@ -699,6 +758,11 @@ impl Session { cache_mb: self.cache_mb, smart_previews_dir: self.smart_previews_dir.as_ref().map(|d| d.to_string_lossy().to_string()), forget_local_days: Some(self.forget_local_days), + slideshow: self.slideshow.clone(), + slide_layout: self.slide_layout.clone(), + slide_overlays: self.slide_overlays.clone(), + slide_titles: self.slide_titles.clone(), + hold_view: self.hold_view, }) .unwrap_or_default(); let Some(lib) = self.library.as_mut() else { return Ok(()) }; diff --git a/crates/engine/src/lightroom_catalog.rs b/crates/engine/src/lightroom_catalog.rs index e841362ea..777206bcb 100644 --- a/crates/engine/src/lightroom_catalog.rs +++ b/crates/engine/src/lightroom_catalog.rs @@ -16,7 +16,8 @@ use serde_json::{Value, json}; type Row = BTreeMap; const COMMAND: &str = "library.importLightroom"; -const MAX_FILE: u64 = 1 << 30; +/// Largest catalog database or WAL read into memory (4 GiB). The whole file is held in RAM, so the cap stays finite. +const MAX_FILE: u64 = 1 << 32; fn error(why: impl Into) -> crate::EngineError { crate::EngineError::BadParams { cmd: COMMAND.into(), msg: why.into() } @@ -234,10 +235,14 @@ pub(crate) fn validate_for_job(data: &CatalogImport) -> Result<(), String> { fn bytes(path: &Path) -> Result, String> { use std::io::Read; let f = std::fs::File::open(path).map_err(|e| format!("{}: {e}", path.display()))?; - if f.metadata().map_err(|e| e.to_string())?.len() > MAX_FILE { - return Err("catalog or WAL exceeds 1 GiB import limit".into()); + let len = f.metadata().map_err(|e| e.to_string())?.len(); + if len > MAX_FILE { + return Err("catalog or WAL exceeds 4 GiB import limit".into()); } let mut out = Vec::new(); + // Reserve the exact size up front: growing by doubling would briefly need about twice the catalog's size. + let wanted = usize::try_from(len).map_err(|_| "catalog is too large for this platform".to_string())?; + out.try_reserve_exact(wanted).map_err(|_| "not enough memory to read the catalog".to_string())?; f.take(MAX_FILE + 1).read_to_end(&mut out).map_err(|e| e.to_string())?; if out.len() as u64 > MAX_FILE { return Err("catalog grew beyond import limit".into()); diff --git a/crates/engine/src/media.rs b/crates/engine/src/media.rs index 0688410ed..50983c86c 100644 --- a/crates/engine/src/media.rs +++ b/crates/engine/src/media.rs @@ -62,7 +62,7 @@ impl SettingsHashes { } /// Bump when the pipeline's output changes, to invalidate cached thumbnails. -pub const RENDER_CACHE_VERSION: u64 = 62; +pub const RENDER_CACHE_VERSION: u64 = 63; /// 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]; @@ -225,9 +225,13 @@ impl DecodedSource { } /// What to render these pixels against: the decoder's facts, else `header` (the catalog's) - /// with any stored camera tone curve. + /// with any stored camera tone curve. The original's size comes from `header` either way: the + /// decoder only sees the level it decoded. pub fn info_or(&self, header: SourceInfo) -> SourceInfo { - self.info.clone().unwrap_or_else(|| SourceInfo { camera_tone: self.camera_tone.or(header.camera_tone), ..header }) + match &self.info { + Some(info) => SourceInfo { native_long: info.native_long.or(header.native_long), ..info.clone() }, + None => SourceInfo { camera_tone: self.camera_tone.or(header.camera_tone), ..header }, + } } } @@ -752,6 +756,8 @@ pub struct RenderJob { pub view_cache: Option<(Arc, Hash128)>, /// Render for this monitor profile ([`Self::with_display`]). pub display: Option>, + /// Compose the render into a slideshow slide ([`Self::with_slide`]). + pub slide: Option>, /// A face tile that can be cut from the raw's embedded preview, tried before `source` /// ([`crate::Session::face_job`]; `None` for every other job). pub preview_cut: Option, @@ -849,7 +855,8 @@ impl RenderJob { /// Gets its own result key. Thumbnail jobs (rendered-thumbnail cache) are left as they are: /// [`crate::display::present`] converts their sRGB results. pub fn with_display(mut self, display: Option>) -> Self { - if self.cache.is_some() || self.request.depth != lightcraft_pipeline::OutputDepth::U8 { + // (a slide is composed in sRGB, backdrop colours included; the frontend presents it) + if self.cache.is_some() || self.slide.is_some() || self.request.depth != lightcraft_pipeline::OutputDepth::U8 { return self; } let k = |d: &Option>| d.as_ref().map_or(0, |d| d.id()).wrapping_mul(0xd6e8_feb8_6659_fd93); @@ -883,7 +890,30 @@ impl RenderJob { } } + /// Compose the render into a slideshow slide (layout, options, backdrop): the result is the + /// slide, not the photo. Gets its own result key; the photo's cached view preview stays the + /// photo. The slide stays sRGB ([`Self::with_display`] leaves it alone). + pub fn with_slide(mut self, slide: Arc) -> Self { + let k = |s: &Option>| s.as_ref().map_or(0, |s| s.key()).wrapping_mul(0xa24b_aed4_963e_e407); + self.key ^= k(&self.slide); + self.key ^= k(&Some(slide.clone())); + self.slide = Some(slide); + self + } + + /// [`Self::run_photo`], composed into its slide when this is a slide job (inside [`Self::run`]'s guard). fn run_unguarded(self) -> RenderResult { + let slide = self.slide.clone(); + let mut r = self.run_photo(); + if let (Some(slide), Ok(rendered)) = (slide, r.rendered.as_mut()) { + rendered.image = slide.compose(&rendered.image); + rendered.deep = None; + } + r + } + + /// The photo's render (without slide composition). + fn run_photo(self) -> RenderResult { if let Some((cache, key)) = &self.cache && let Some(img) = cache.get_at(self.cache_generation, *key) { @@ -1131,9 +1161,16 @@ pub fn content_key(p: &Photo) -> String { pub fn source_info(p: &Photo) -> SourceInfo { // Procedural demo scenes are scene-referred HDR (like raw files): use the filmic tone map. + // (They are drawn afresh at every size, so they have no original size.) if matches!(p.source, Source::Demo { .. }) { return SourceInfo { raw: true, ..Default::default() }; } + // the thumbnail and preview levels are smaller copies: noise reduction works in the original's pixels (#960) + let native_long = Some(p.width.max(p.height) as usize).filter(|&n| n > 0); + SourceInfo { native_long, ..header_info(p) } +} + +fn header_info(p: &Photo) -> SourceInfo { // A raw shown from its embedded preview is a rendered (display-referred) JPEG: relative white // balance and the display tone curve, like any other rendered file. if p.develops_raw() { @@ -1227,6 +1264,7 @@ impl crate::Session { stages: None, view_cache: None, display: None, + slide: None, preview_cut: None, }) } @@ -1279,6 +1317,7 @@ impl crate::Session { stages: None, view_cache: None, display: None, + slide: None, preview_cut: None, }) } @@ -1332,6 +1371,7 @@ impl crate::Session { stages: None, view_cache: None, display: None, + slide: None, preview_cut, }) } @@ -1525,7 +1565,7 @@ impl crate::Session { /// says and what a smart preview renders with. Only for raws edited in Kelvin. fn learn_as_shot_wb(&mut self, id: PhotoId, info: &SourceInfo) { let Some(p) = self.catalog.photo(id) else { return }; - if !(info.raw && !info.relative_wb && p.develops_raw() && !p.relative_wb()) { + if !info.raw || info.relative_wb || !p.develops_raw() || p.relative_wb() { return; } let read = (info.as_shot_temp, info.as_shot_tint); @@ -1720,6 +1760,7 @@ mod thumbnail_hash_tests { file_size: 99, content_hash: Some("replacement".into()), preview_only: None, + preview_checked: String::new(), }) .unwrap(); s.media.forget(PhotoId(1)); @@ -1757,6 +1798,21 @@ mod thumbnail_hash_tests { mod tests { use super::*; + #[test] + fn renders_know_the_original_size_whichever_level_they_come_from() { + // issue #960: noise reduction works in the original's pixels, so a grid cell drawn from the + // 512-px thumbnail level denoises like a scaled-down export + let p = Photo::new(PhotoId(1), Source::File { path: "/photos/a.ARW".into() }, "a.ARW", "ARW", 6000, 4000, ""); + assert_eq!(source_info(&p).native_long, Some(6000)); + let decoded = SourceInfo { raw: true, as_shot_temp: 4800.0, ..Default::default() }; + let thumb = DecodedSource::new(Arc::new(Rgb32f::new(8, 8)), Some(decoded.clone())); + let info = thumb.info_or(source_info(&p)); + assert_eq!(info, SourceInfo { native_long: Some(6000), ..decoded }, "the decoder's facts, the catalog's size"); + let unknown = Photo::new(PhotoId(2), Source::File { path: "/photos/b.jpg".into() }, "b.jpg", "JPEG", 0, 0, ""); + assert_eq!(source_info(&unknown).native_long, None); + assert_eq!(source_info(&Photo::new(PhotoId(3), Source::Demo { scene: 0 }, "d", "ARW", 600, 400, "")).native_long, None); + } + #[test] fn denoise_mix_is_reserved_before_the_source_enters_the_cache() { let mut plain = Rgb32f::new(8, 8); diff --git a/crates/engine/src/merge.rs b/crates/engine/src/merge.rs index ef9fd00f9..ed61c0509 100644 --- a/crates/engine/src/merge.rs +++ b/crates/engine/src/merge.rs @@ -12,6 +12,7 @@ use std::sync::Arc; use lightcraft_catalog::{PhotoId, Source}; use lightcraft_develop::DevelopSettings; +use lightcraft_merge::pano::MAX_MEGAPIXELS; use lightcraft_merge::{Deghost, Frame, HdrOptions, PanoOptions, Projection}; use lightcraft_pipeline::{Quality, RenderRequest, SourceInfo}; use lightcraft_raster::{Rgb32f, Rgba8}; @@ -20,6 +21,10 @@ use serde_json::{Value, json}; use crate::{EngineError, Result, Session}; +/// Why `merge.hdrBatch` found nothing to merge. +pub const NO_BRACKET_SETS: &str = + "no bracket sets among these photos: select stacks, or photos taken less than a second apart at different exposures"; + /// Long edge of the frames of a preview merge. pub const PREVIEW_EDGE: usize = 1024; @@ -105,20 +110,18 @@ impl MergeJob { use rayon::prelude::*; let done = std::sync::atomic::AtomicUsize::new(0); let n = self.sources.len(); - let frames: std::result::Result, String> = self - .sources - .par_iter() - .map(|(_, path)| { - let bytes = (self.read)(path)?; - // a raw without colour matrices of its own starts from the colour it has when opened alone (#965) - let f = lightcraft_merge::load_frame_with(&bytes, self.preview, orient, &crate::camera_preview::starting_colour_tags) - .map_err(|e| format!("{path}: {e}")); - let k = done.fetch_add(1, std::sync::atomic::Ordering::Relaxed) + 1; - progress(span.0 + (span.1 - span.0) * k as f32 / n as f32, "Reading photos"); - f - }) - .collect(); - frames + let one = |(_, path): &(PhotoId, String)| { + let bytes = (self.read)(path)?; + // a raw without colour matrices of its own starts from the colour it has when opened alone (#965) + let f = lightcraft_merge::load_frame_with(&bytes, self.preview, orient, &crate::camera_preview::starting_colour_tags) + .map_err(|e| format!("{path}: {e}")); + let k = done.fetch_add(1, std::sync::atomic::Ordering::Relaxed) + 1; + progress(span.0 + (span.1 - span.0) * k as f32 / n as f32, "Reading photos"); + f + }; + // Full-size photos are decoded one at a time (the demosaic is parallel inside): decoding several 60 MP raws at + // once multiplied the decoders' scratch memory, and a 7-frame bracket ran out of memory. + if self.preview.is_some() { self.sources.par_iter().map(one).collect() } else { self.sources.iter().map(one).collect() } } /// Run the merge. `progress(fraction, stage)` returns false to cancel. @@ -149,8 +152,9 @@ impl MergeJob { MergeKind::Panorama { pano } => { let frames = self.load(true, progress, (0.0, 0.25))?; let p = sub(0.25, 0.9); - let r = lightcraft_merge::stitch(frames, pano, &p).map_err(err)?; - let info = pano_info(&r, &self.sources); + let fit = fit_in_memory(pano, pixels(&frames), crate::memory::total_ram()); + let r = lightcraft_merge::stitch(frames, &fit, &p).map_err(err)?; + let info = pano_info(&r, &self.sources, pano, &fit); ( r.image, r.color, @@ -192,8 +196,9 @@ impl MergeJob { merged.push(f); } let p = sub(0.6, 0.9); - let r = lightcraft_merge::stitch(merged, pano, &p).map_err(err)?; - let mut info = pano_info(&r, &self.sources); + let fit = fit_in_memory(pano, pixels(&merged), crate::memory::total_ram()); + let r = lightcraft_merge::stitch(merged, &fit, &p).map_err(err)?; + let mut info = pano_info(&r, &self.sources, pano, &fit); info["bracket"] = json!(b); ( r.image, @@ -230,7 +235,20 @@ impl MergeJob { } } -fn pano_info(r: &lightcraft_merge::PanoResult, sources: &[(PhotoId, String)]) -> Value { +/// `asked` is what the command asked for, `fit` what ran (held to memory): when the panorama came +/// out smaller than the photos' own resolution, `limit` says why (`memory`, `size`: Lightroom's +/// 512 MP / 65,000 px, or `asked`). +fn pano_info(r: &lightcraft_merge::PanoResult, sources: &[(PhotoId, String)], asked: &PanoOptions, fit: &PanoOptions) -> Value { + let megapixels = (r.image.width * r.image.height) as f64 / 1e6; + let native = r.native_megapixels; + let why = if fit.max_megapixels < asked.max_megapixels && fit.max_megapixels < native { + "memory" + } else if asked.max_megapixels < MAX_MEGAPIXELS && asked.max_megapixels < native { + "asked" + } else { + "size" + }; + let limit = (megapixels < native * 0.98).then_some(why); json!({ "projection": r.projection, "used": r.used.iter().filter_map(|&i| sources.get(i).map(|s| s.0.0)).collect::>(), @@ -238,9 +256,46 @@ fn pano_info(r: &lightcraft_merge::PanoResult, sources: &[(PhotoId, String)]) -> "rmsPx": r.rms, "gains": r.gains, "crop": r.crop, + "megapixels": megapixels, + "nativeMegapixels": native, + "limit": limit, }) } +/// Peak memory of a panorama per pixel of its output (bytes), measured as the peak resident size +/// of synthetic stitches of 20–81 MP from 38–154 MP of photos, rounded up from the smaller ones +/// (the larger need less): while the photos are projected they are still held, beside the tiles +/// ([`PANO_TILE_BYTES`] on top of the photos' own 12 bytes a pixel); the blend that follows, the +/// photos freed, peaks at [`PANO_BLEND_BYTES`]. Writing the DNG takes less than either. +const PANO_TILE_BYTES: f64 = 36.0; +const PANO_BLEND_BYTES: f64 = 68.0; + +/// What a merge may use of the computer's memory: three quarters of it (the rest is the system's +/// and the other apps'), less the app's own memory budget (its caches). +fn merge_memory(ram: Option) -> f64 { + // a computer whose memory we can't read (Windows) is taken to have 8 GB + let ram = ram.unwrap_or(8 << 30) as f64; + (ram * 0.75 - crate::memory::budget() as f64).max(0.0) +} + +/// `pano` with its size held to what fits in memory beside its photos (`frame_px` pixels, 12 +/// bytes each, loaded already): a panorama of five 51 MP photos may have up to 166 MP on a 16 GB +/// computer, 355 MP on 32 GB. Never below 40 megapixels, the size every panorama had before it +/// followed the photos' resolution, and never above what was asked. +pub(crate) fn fit_in_memory(pano: &PanoOptions, frame_px: f64, ram: Option) -> PanoOptions { + let room = merge_memory(ram); + let fits = ((room - frame_px * 12.0) / PANO_TILE_BYTES).min(room / PANO_BLEND_BYTES) / 1e6; + PanoOptions { max_megapixels: pano.max_megapixels.min(fits.max(MIN_FITTED_MEGAPIXELS)), ..pano.clone() } +} + +/// The pixels of `frames`. +fn pixels(frames: &[Frame]) -> f64 { + frames.iter().map(|f| (f.width() * f.height()) as f64).sum() +} + +/// The smallest size [`fit_in_memory`] holds a panorama to (megapixels). +pub(crate) const MIN_FITTED_MEGAPIXELS: f64 = 40.0; + /// Bracket size from EXIF: the smallest period of the exposure sequence (≥ 2). pub fn detect_bracket(frames: &[Frame]) -> Option { let ev: Vec = frames.iter().map(|f| f.exposure).collect::>()?; @@ -331,7 +386,8 @@ pub fn parse(id: &str, p: &Value) -> Result<(MergeKind, MergeFinish)> { boundary_warp: p.get("boundaryWarp").and_then(Value::as_f64).unwrap_or(0.0).clamp(0.0, 100.0), auto_crop: p.get("autoCrop").and_then(Value::as_bool).unwrap_or(false), fill_edges: p.get("fillEdges").and_then(Value::as_bool).unwrap_or(false), - max_megapixels: p.get("maxMegapixels").and_then(Value::as_f64).unwrap_or(40.0), + // the photos' own resolution unless asked (held to what fits in memory when it runs) + max_megapixels: p.get("maxMegapixels").and_then(Value::as_f64).filter(|v| *v > 0.0).unwrap_or(MAX_MEGAPIXELS).min(MAX_MEGAPIXELS), }; let kind = match id { "merge.hdr" => MergeKind::Hdr { hdr }, diff --git a/crates/engine/src/pdf.rs b/crates/engine/src/pdf.rs new file mode 100644 index 000000000..963535f5a --- /dev/null +++ b/crates/engine/src/pdf.rs @@ -0,0 +1,110 @@ +//! A minimal PDF 1.4 writer for image pages: one JPEG XObject per page, explicit byte lengths and +//! a classic cross-reference table. Used by the contact sheet ([`crate::contact_sheet`]) and the +//! slideshow PDF export ([`crate::slideshow_export`]). It streams into any writer, so a long +//! slideshow is never held in memory; the page tree and catalog are written last, once the +//! number of pages is known. + +use std::io::Write; + +/// Objects 1 and 2 are the catalog and the page tree; pages take three objects each from 3. +const FIRST_PAGE: usize = 3; + +pub(crate) struct Pdf { + w: W, + /// Bytes written so far. + pos: usize, + /// Each object's offset, by object number (index 0 unused). + offsets: Vec, + pages: Vec, +} + +fn io(e: std::io::Error) -> String { + format!("PDF: {e}") +} + +impl Pdf { + pub(crate) fn new(w: W) -> Result { + let mut p = Pdf { w, pos: 0, offsets: vec![0; FIRST_PAGE], pages: Vec::new() }; + p.put(b"%PDF-1.4\n%\xe2\xe3\xcf\xd3\n")?; + Ok(p) + } + + /// Bytes written so far. + pub(crate) fn len(&self) -> usize { + self.pos + } + + /// Pages added so far. + pub(crate) fn pages(&self) -> usize { + self.pages.len() + } + + fn put(&mut self, b: &[u8]) -> Result<(), String> { + self.w.write_all(b).map_err(io)?; + self.pos = self.pos.saturating_add(b.len()); + Ok(()) + } + + fn object(&mut self, id: usize, body: &[u8]) -> Result<(), String> { + if self.offsets.len() <= id { + self.offsets.resize(id + 1, 0); + } + if let Some(o) = self.offsets.get_mut(id) { + *o = self.pos; + } + self.put(format!("{id} 0 obj\n").as_bytes())?; + self.put(body)?; + self.put(b"\nendobj\n") + } + + fn stream(&mut self, id: usize, dictionary: &str, bytes: &[u8]) -> Result<(), String> { + let mut body = format!("<< {dictionary} /Length {} >>\nstream\n", bytes.len()).into_bytes(); + body.extend_from_slice(bytes); + body.extend_from_slice(b"\nendstream"); + self.object(id, &body) + } + + /// A page of `points` (width, height) filled by `jpg` (`width × height` pixels, RGB). + /// `extra` goes into the page dictionary (e.g. a display duration and transition). + pub(crate) fn image_page(&mut self, jpg: &[u8], width: usize, height: usize, points: (f64, f64), extra: &str) -> Result<(), String> { + let id = FIRST_PAGE + self.pages.len() * 3; + let (w, h) = points; + self.object( + id, + format!( + "<< /Type /Page /Parent 2 0 R /MediaBox [0 0 {w:.2} {h:.2}] /Resources << /XObject << /Im {} 0 R >> >> /Contents {} 0 R{extra} >>", + id + 2, + id + 1 + ) + .as_bytes(), + )?; + self.stream(id + 1, "", format!("q {w:.2} 0 0 {h:.2} 0 0 cm /Im Do Q").as_bytes())?; + self.stream( + id + 2, + &format!("/Type /XObject /Subtype /Image /Width {width} /Height {height} /ColorSpace /DeviceRGB /BitsPerComponent 8 /Filter /DCTDecode"), + jpg, + )?; + self.pages.push(id); + Ok(()) + } + + /// Write the page tree, the catalog (with `catalog_extra` in its dictionary, e.g. a page + /// mode), the cross-reference table and the trailer. + pub(crate) fn finish(mut self, catalog_extra: &str) -> Result { + if self.pages.is_empty() { + return Err("PDF: no pages".into()); + } + let kids = self.pages.iter().map(|i| format!("{i} 0 R")).collect::>().join(" "); + self.object(2, format!("<< /Type /Pages /Count {} /Kids [{kids}] >>", self.pages.len()).as_bytes())?; + self.object(1, format!("<< /Type /Catalog /Pages 2 0 R{catalog_extra} >>").as_bytes())?; + let start = self.pos; + let mut xref = format!("xref\n0 {}\n0000000000 65535 f \n", self.offsets.len()).into_bytes(); + for offset in self.offsets.iter().skip(1) { + xref.extend_from_slice(format!("{offset:010} 00000 n \n").as_bytes()); + } + xref.extend_from_slice(format!("trailer\n<< /Size {} /Root 1 0 R >>\nstartxref\n{start}\n%%EOF\n", self.offsets.len()).as_bytes()); + self.put(&xref)?; + self.w.flush().map_err(io)?; + Ok(self.w) + } +} diff --git a/crates/engine/src/presets.rs b/crates/engine/src/presets.rs index c3675e227..0dee84f3e 100644 --- a/crates/engine/src/presets.rs +++ b/crates/engine/src/presets.rs @@ -155,7 +155,7 @@ pub fn parse_preset_file(name: &str, bytes: &[u8]) -> Result, String pub fn expand_preset_paths(paths: &[String]) -> Vec { let preset_file = |p: &Path| { let ext = p.extension().map(|e| e.to_string_lossy().to_ascii_lowercase()).unwrap_or_default(); - ext == LCPRESET_EXT || ["xmp", "lrtemplate", "zip", "lmp", "mplumpack"].contains(&ext.as_str()) + ext == LCPRESET_EXT || ["xmp", "lrtemplate", "zip", "lmp", "mplumpack", crate::cmd::mask_effect_files::EFFECT_EXT].contains(&ext.as_str()) }; let mut out = Vec::new(); for p in paths { diff --git a/crates/engine/src/rename.rs b/crates/engine/src/rename.rs index e49355c08..6408b71e8 100644 --- a/crates/engine/src/rename.rs +++ b/crates/engine/src/rename.rs @@ -245,6 +245,19 @@ pub fn expand(template: &str, p: &Photo, seq: usize) -> String { /// The template's tokens replaced for `p` (no extension added, nothing sanitized). A bare `{seq}` /// is zero-padded to `seq_width` digits. pub fn expand_tokens(template: &str, p: &Photo, seq: usize, seq_width: usize) -> String { + expand_tokens_with(template, p, seq, seq_width, &|_, _, _| None) +} + +/// [`expand_tokens`] with more tokens: `extra(name, arg, tag)` (the name lower-cased, the text +/// after `:`, the whole tag without braces) answers a token first; `None` leaves it to the rename +/// tokens (and an unknown one stays as typed). Slideshow text overlays add their tokens this way. +pub fn expand_tokens_with( + template: &str, + p: &Photo, + seq: usize, + seq_width: usize, + extra: &dyn Fn(&str, Option<&str>, &str) -> Option, +) -> String { let (stem, ext) = split_ext(&p.file_name); let mut out = String::new(); let mut rest = template; @@ -257,31 +270,35 @@ pub fn expand_tokens(template: &str, p: &Photo, seq: usize, seq_width: usize) -> }; let tok = &rest[i + 1..i + j]; let (name, arg) = tok.split_once(':').map(|(a, b)| (a, Some(b))).unwrap_or((tok, None)); - let v = match name.trim().to_ascii_lowercase().as_str() { - "name" | "filename" => stem.to_string(), - "num" => { - let digits = stem.chars().rev().take_while(char::is_ascii_digit).count(); - stem[stem.len() - digits..].to_string() - } - "seq" | "n" => { - let w: usize = arg.and_then(|a| a.parse().ok()).unwrap_or(seq_width).min(9); - format!("{seq:0w$}") - } - "date" => format_date(p.date(), arg.unwrap_or("%Y%m%d")), - "folder" => match &p.source { - Source::File { path } => { - Path::new(path).parent().and_then(Path::file_name).map(|f| f.to_string_lossy().to_string()).unwrap_or_default() + let lower = name.trim().to_ascii_lowercase(); + let v = match extra(&lower, arg, tok) { + Some(v) => v, + None => match lower.as_str() { + "name" | "filename" => stem.to_string(), + "num" => { + let digits = stem.chars().rev().take_while(char::is_ascii_digit).count(); + stem[stem.len() - digits..].to_string() } - Source::Demo { .. } => String::new(), + "seq" | "n" => { + let w: usize = arg.and_then(|a| a.parse().ok()).unwrap_or(seq_width).min(9); + format!("{seq:0w$}") + } + "date" => format_date(p.date(), arg.unwrap_or("%Y%m%d")), + "folder" => match &p.source { + Source::File { path } => { + Path::new(path).parent().and_then(Path::file_name).map(|f| f.to_string_lossy().to_string()).unwrap_or_default() + } + Source::Demo { .. } => String::new(), + }, + "camera" => p.meta.camera.clone(), + "lens" => p.meta.lens.clone(), + "iso" => p.meta.iso.map(|v| v.to_string()).unwrap_or_default(), + "rating" => p.rating.to_string(), + "title" => p.meta.title.clone(), + "creator" => p.meta.creator.clone(), + "ext" => ext.to_string(), + _ => format!("{{{tok}}}"), }, - "camera" => p.meta.camera.clone(), - "lens" => p.meta.lens.clone(), - "iso" => p.meta.iso.map(|v| v.to_string()).unwrap_or_default(), - "rating" => p.rating.to_string(), - "title" => p.meta.title.clone(), - "creator" => p.meta.creator.clone(), - "ext" => ext.to_string(), - _ => format!("{{{tok}}}"), }; out.push_str(&v); rest = &rest[i + j + 1..]; diff --git a/crates/engine/src/sidecar.rs b/crates/engine/src/sidecar.rs index c67a2c84f..89b476959 100644 --- a/crates/engine/src/sidecar.rs +++ b/crates/engine/src/sidecar.rs @@ -63,16 +63,24 @@ pub fn sidecar_path(original: &str, naming: SidecarNaming) -> PathBuf { } /// An existing sidecar for `original`: the preferred naming first, then the other one (also -/// accepting an upper-case `.XMP`). +/// accepting `.XMP` and `.Xmp`, and a full name whose photo extension differs in case, such as +/// `IMG_1.cr3.xmp` beside `IMG_1.CR3`, which a case-sensitive file system would otherwise miss). pub fn find_sidecar(original: &str, naming: SidecarNaming) -> Option { let other = if naming == SidecarNaming::Stem { SidecarNaming::Full } else { SidecarNaming::Stem }; + let ext = Path::new(original).extension().map(|e| e.to_string_lossy().to_string()).unwrap_or_default(); + let base = Path::new(original).with_extension(""); + let recased = |e: String| (e != ext).then(|| format!("{}.{e}", base.to_string_lossy())); + let full_variants: Vec = [recased(ext.to_lowercase()), recased(ext.to_uppercase())].into_iter().flatten().collect(); [naming, other] .into_iter() .flat_map(|n| { - let p = sidecar_path(original, n); - let upper = p.with_extension("XMP"); - [p, upper] + let mut v = vec![sidecar_path(original, n)]; + if n == SidecarNaming::Full { + v.extend(full_variants.iter().map(|o| sidecar_path(o, n))); + } + v }) + .flat_map(|p| [p.clone(), p.with_extension("XMP"), p.with_extension("Xmp")]) .find(|p| p.is_file()) } @@ -502,10 +510,10 @@ pub fn read_packet(original: &str, kind: MediaKind, naming: SidecarNaming) -> Op /// Photo ids touched by an op (for auto-write). pub(crate) fn op_photos(op: &Op, out: &mut Vec) { match op { - Op::SetRating { id, .. } | Op::SetFlag { id, .. } | Op::SetLabel { id, .. } | Op::SetDevelop { id, .. } | Op::SetMeta { id, .. } => { - if !out.contains(id) { - out.push(*id); - } + Op::SetRating { id, .. } | Op::SetFlag { id, .. } | Op::SetLabel { id, .. } | Op::SetDevelop { id, .. } | Op::SetMeta { id, .. } + if !out.contains(id) => + { + out.push(*id); } Op::Batch { ops } => ops.iter().for_each(|o| op_photos(o, out)), _ => {} @@ -713,13 +721,61 @@ impl Session { } let owners = StemOwners::among(&self.catalog, &ids); for id in ids { - if self.catalog.photo(id).is_some_and(|p| file_path(p).is_some() && p.copy_of.is_none()) - && let Err(e) = self.save_sidecar_with(id, &owners) - { - log::warn!("auto-write XMP: {e}"); + if self.catalog.photo(id).is_some_and(|p| file_path(p).is_some() && p.copy_of.is_none()) { + match self.save_sidecar_with(id, &owners) { + Ok(_) => { + self.unsaved_sidecars.remove(&id); + } + Err(e) => { + // kept for a retry once the file is reachable: a network share that dropped out mustn't leave + // the sidecar silently behind the edit + log::warn!("auto-write XMP: {e}"); + self.unsaved_sidecars.insert(id); + } + } } } } + + /// How many photos have an automatic sidecar write waiting (it failed: the folder offline, read-only or full). + pub fn unsaved_sidecars(&self) -> usize { + self.unsaved_sidecars.len() + } + + /// Write the waiting sidecars whose originals are reachable again (the availability cache answers without + /// touching the disk for the others); photos that are gone, or with auto-write off, are dropped from the wait. + /// Returns how many were written. + pub fn retry_unsaved_sidecars(&mut self) -> usize { + if !self.xmp.auto_write { + self.unsaved_sidecars.clear(); + return 0; + } + let mut ready = Vec::new(); + for id in self.unsaved_sidecars.clone() { + match self.catalog.photo(id).filter(|p| p.copy_of.is_none()).and_then(|p| file_path(p).map(str::to_owned)) { + None => { + self.unsaved_sidecars.remove(&id); + } + Some(path) if self.media.availability.exists(&path) == Some(true) => ready.push(id), + Some(_) => {} + } + } + if ready.is_empty() { + return 0; + } + let owners = StemOwners::among(&self.catalog, &ready); + let mut written = 0; + for id in ready { + match self.save_sidecar_with(id, &owners) { + Ok(_) => { + self.unsaved_sidecars.remove(&id); + written += 1; + } + Err(e) => log::debug!("retry XMP: {e}"), + } + } + written + } } #[cfg(test)] diff --git a/crates/engine/src/slide.rs b/crates/engine/src/slide.rs new file mode 100644 index 000000000..009a2e2ac --- /dev/null +++ b/crates/engine/src/slide.rs @@ -0,0 +1,203 @@ +//! Slideshow slides in the engine: the library's slide layout ([`SlideSettings`], Lightroom +//! Classic's Layout, Options and Backdrop panels; the `slideshow.layout` command), its overlays +//! ([`crate::slide_overlay`], `slideshow.overlays`) and render jobs that compose a photo into its +//! slide ([`SlideCompose`], [`crate::media::RenderJob::with_slide`]). +//! +//! The composer itself is `lightcraft-slideshow` (no UI, no catalog): playback, the Settings +//! preview and `slideshow.preview` all compose through it, so they draw the same pixels. + +use std::sync::{Arc, Mutex, PoisonError}; + +use lightcraft_catalog::PhotoId; +use lightcraft_raster::Rgba8; +pub use lightcraft_slideshow::{Aspect, Layout, SlideSettings}; + +use crate::media::RenderJob; + +/// The last rendered background image: (its render key, the image). Shared by the slide jobs of +/// a session, so the backdrop photo is rendered once, not once per slide. +pub type BackdropCache = Mutex)>>; + +/// The background image of a slide: a library photo's render job and the shared cache it goes to. +#[derive(Clone)] +pub struct Backdrop { + pub photo: PhotoId, + job: Box, + cache: Arc, +} + +impl Backdrop { + /// The rendered background image (from the cache when it holds this render; otherwise + /// rendered now, on the calling thread). `None` when it can't be rendered (the slide then + /// shows the backdrop colour alone). + pub fn image(&self) -> Option> { + let key = self.job.key; + if let Some((k, img)) = self.cache.lock().unwrap_or_else(PoisonError::into_inner).as_ref() + && *k == key + { + return Some(img.clone()); + } + let img = Arc::new((*self.job).clone().run().rendered.ok()?.image); + *self.cache.lock().unwrap_or_else(PoisonError::into_inner) = Some((key, img.clone())); + Some(img) + } +} + +/// How to turn a photo's render into its slide: the layout, the slide's size in pixels and the +/// background image, if any. +#[derive(Clone)] +pub struct SlideCompose { + pub settings: SlideSettings, + pub width: usize, + pub height: usize, + pub backdrop: Option, + /// The overlays resolved for the slide's photo (`None`: none draw). + pub overlays: Option>, +} + +impl SlideCompose { + /// What makes the slide's pixels differ (for render keys). + pub fn key(&self) -> u64 { + self.settings.pixel_key() + ^ (self.width as u64).wrapping_mul(0x9e37_79b9_7f4a_7c15) + ^ (self.height as u64).wrapping_mul(0xc2b2_ae3d_27d4_eb4f) + ^ self.backdrop.as_ref().map_or(0, |b| b.job.key.rotate_left(29)) + ^ self.overlays.as_ref().map_or(0, |o| o.key().rotate_left(43)) + } + + /// The slide showing `photo` (rendered at [`lightcraft_slideshow::photo_size`], or any size). + pub fn compose(&self, photo: &Rgba8) -> Rgba8 { + let bg = self.backdrop.as_ref().and_then(Backdrop::image); + let Some(o) = &self.overlays else { + return lightcraft_slideshow::compose(photo, bg.as_deref(), &self.settings, self.width, self.height); + }; + let (mut img, l) = + lightcraft_slideshow::compose_with(photo, bg.as_deref(), &self.settings, self.width, self.height, &mut |img, l| o.draw_behind(img, l)); + o.draw_front(&mut img, &l); + img + } +} + +impl crate::Session { + /// The aspect (width / height) photo `id` shows at: cropped, straightened, lens-corrected. + pub fn slide_photo_aspect(&self, id: PhotoId) -> Option { + let p = self.catalog.photo(id)?; + let frame = lightcraft_pipeline::geometry::Frame::with_lens( + p.width.max(1) as usize, + p.height.max(1) as usize, + &p.develop, + true, + p.embedded_lens.as_ref(), + ); + let a = frame.aspect(); + Some(if a.is_finite() && a > 0.0 { a } else { 1.5 }) + } + + /// Does a slide show nothing but the photo fitted on black (the plain layout and no + /// overlays)? Playback then shows the photo's own render. + pub fn slides_plain(&self) -> bool { + self.slide_layout.is_plain() && !self.slide_overlays.any() + } + + /// The composition of `settings` at `width × height`, with its background image's render job + /// (the photo `settings.background_image` names, when it is in the catalog) and the library's + /// overlays resolved for `photo`. + pub fn slide_compose(&mut self, settings: &SlideSettings, width: usize, height: usize, photo: Option) -> Arc { + let overlays = self.slide_overlays.clone(); + self.slide_compose_with(settings, &overlays, width, height, photo) + } + + /// [`Self::slide_compose`] with `overlays` in place of the library's (a template's preview). + pub fn slide_compose_with( + &mut self, + settings: &SlideSettings, + overlays: &crate::slide_overlay::OverlaySettings, + width: usize, + height: usize, + photo: Option, + ) -> Arc { + let settings = settings.clone().sanitized(); + let backdrop = settings.background_image.filter(|_| settings.background_opacity > 0.0).and_then(|bid| { + let id = PhotoId(bid); + let aspect = self.slide_photo_aspect(id)?; + let fill = + SlideSettings { zoom_to_fill: true, margin_left: 0.0, margin_right: 0.0, margin_top: 0.0, margin_bottom: 0.0, ..settings.clone() }; + let (w, h) = lightcraft_slideshow::photo_size(&fill, aspect, width, height); + let job = self.render_job(id, w, h, false, true)?; + Some(Backdrop { photo: id, job: Box::new(job), cache: self.slide_backdrop.clone() }) + }); + let overlays = overlays.any().then(|| { + let p = photo.and_then(|id| self.catalog.photo(id)); + Arc::new(crate::slide_overlay::SlideOverlays::resolve(overlays, p.map(|p| &**p))) + }); + Arc::new(SlideCompose { settings, width, height, backdrop, overlays }) + } + + /// A render job whose result is photo `id`'s slide at `width × height` with the library's + /// slide layout: the photo is rendered at the size its cell needs (its long edge at most + /// `photo_edge`, when given), then composed on the worker. The photo's render is kept as its + /// view preview, like the loupe's. + pub fn slide_job(&mut self, id: PhotoId, width: usize, height: usize, photo_edge: Option) -> Option { + let settings = self.slide_layout.clone(); + self.slide_job_with(&settings, id, width, height, photo_edge, true) + } + + /// [`Self::slide_job`] with `settings` in place of the library's layout; `view` keeps the + /// photo's render as its view preview. + pub fn slide_job_with( + &mut self, + settings: &SlideSettings, + id: PhotoId, + width: usize, + height: usize, + photo_edge: Option, + view: bool, + ) -> Option { + let overlays = self.slide_overlays.clone(); + self.slide_job_look(settings, &overlays, id, width, height, photo_edge, view) + } + + /// [`Self::slide_job_with`] with `overlays` in place of the library's (a template's preview). + #[allow(clippy::too_many_arguments)] + pub fn slide_job_look( + &mut self, + settings: &SlideSettings, + overlays: &crate::slide_overlay::OverlaySettings, + id: PhotoId, + width: usize, + height: usize, + photo_edge: Option, + view: bool, + ) -> Option { + let aspect = self.slide_photo_aspect(id)?; + let (mut w, mut h) = lightcraft_slideshow::photo_size(settings, aspect, width, height); + if let Some(edge) = photo_edge.filter(|e| *e > 0 && w.max(h) > *e) { + let k = edge as f64 / w.max(h) as f64; + (w, h) = (((w as f64 * k).round() as usize).max(1), ((h as f64 * k).round() as usize).max(1)); + } + let job = if view { self.loupe_job(id, w.max(8), h.max(8), true)? } else { self.render_job(id, w.max(8), h.max(8), false, true)? }; + let compose = self.slide_compose_with(settings, overlays, width, height, Some(id)); + Some(job.with_slide(compose)) + } + + /// Photo `id`'s slide at `width × height` with `settings`, rendered now on this thread. + pub fn slide_now(&mut self, settings: &SlideSettings, id: PhotoId, width: usize, height: usize) -> Result { + let overlays = self.slide_overlays.clone(); + self.slide_now_look(settings, &overlays, id, width, height) + } + + /// [`Self::slide_now`] with `overlays` in place of the library's. + pub fn slide_now_look( + &mut self, + settings: &SlideSettings, + overlays: &crate::slide_overlay::OverlaySettings, + id: PhotoId, + width: usize, + height: usize, + ) -> Result { + let job = self.slide_job_look(settings, overlays, id, width, height, None, false).ok_or("no such photo")?; + let r = job.run(); + self.accept(&r); + r.rendered.map(|r| r.image) + } +} diff --git a/crates/engine/src/slide_overlay.rs b/crates/engine/src/slide_overlay.rs new file mode 100644 index 000000000..409739335 --- /dev/null +++ b/crates/engine/src/slide_overlay.rs @@ -0,0 +1,1344 @@ +//! Slideshow overlays (Lightroom Classic's Slideshow ▸ Overlays panel): the identity plate (text +//! or an image file), the photo's rating as stars, text overlays with tokens (file name, title, +//! caption, capture date, exposure, camera, lens, rating…) and the export watermark. +//! [`OverlaySettings`] is a library preference (`prefs.json`), edited with the +//! `slideshow.overlays` command; [`SlideOverlays`] is those settings resolved for one photo (its +//! texts expanded, its rating) and drawn onto the composed slide. +//! +//! **Where the drawing lives:** text needs the fonts (Inter plus the craft-fonts CJK fallbacks, +//! [`crate::text`]) and tokens need the catalog's photo, both in the engine, so overlays are a +//! second pass in the engine over `lightcraft_slideshow::compose_with`'s slide (its `behind` hook +//! draws an identity plate set to "render behind image" before the photo). The composer stays a +//! layout-and-pixels crate with no fonts and no catalog. +//! +//! **Resolution independence:** sizes and offsets are fractions of the slide's shorter side (also +//! for overlays anchored to the photo), so a slide at any size has the same proportions. An +//! overlay is placed at one of nine anchors of the slide or of the photo, moved by its offset +//! (x right, y down) and kept on the slide. +//! +//! **Never crash:** settings are validated by [`OverlaySettings::merged`] and clamped by +//! [`OverlaySettings::sanitized`]; text is cut at [`TEXT_MAX_CHARS`]; an unreadable identity +//! plate image draws nothing (and `slideshow.overlays` reports it); unknown tokens stay as typed +//! (as in rename and export file names) and are reported. + +use std::sync::Arc; + +use lightcraft_catalog::Photo; +use lightcraft_raster::{Image, Plane, Rgba8}; +use lightcraft_slideshow::{Layout, Rect, hex, parse_color}; +use serde::{Deserialize, Serialize}; +use serde_json::{Map, Value, json}; + +use crate::export::{Anchor, Watermark}; + +/// Smallest text, plate and star size (fraction of the slide's shorter side). +pub const SIZE_MIN: f64 = 0.005; +/// Largest text line height. +pub const TEXT_SIZE_MAX: f64 = 0.3; +/// Largest identity plate height. +pub const PLATE_SIZE_MAX: f64 = 0.5; +/// Largest star. +pub const STAR_SIZE_MAX: f64 = 0.15; +/// Largest offset either way (fraction of the shorter side). +pub const OFFSET_MAX: f64 = 1.0; +/// Longest shadow offset and radius (fractions of the shorter side). +pub const SHADOW_OFFSET_MAX: f64 = 0.05; +pub const SHADOW_RADIUS_MAX: f64 = 0.05; +/// Most text overlays. +pub const MAX_TEXTS: usize = 16; +/// Longest overlay text or template (characters; longer is cut). +pub const TEXT_MAX_CHARS: usize = 1000; + +/// What an overlay is anchored to. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub enum RelativeTo { + #[default] + Slide, + /// The visible photo. + Photo, +} + +impl RelativeTo { + fn id(self) -> &'static str { + match self { + RelativeTo::Slide => "slide", + RelativeTo::Photo => "photo", + } + } +} + +/// The nine anchors, in reading order (for pickers and tests). +pub const ANCHORS: [Anchor; 9] = [ + Anchor::TopLeft, + Anchor::Top, + Anchor::TopRight, + Anchor::Left, + Anchor::Center, + Anchor::Right, + Anchor::BottomLeft, + Anchor::Bottom, + Anchor::BottomRight, +]; + +/// An anchor's id (`topLeft`…). +pub fn anchor_id(a: Anchor) -> &'static str { + match a { + Anchor::TopLeft => "topLeft", + Anchor::Top => "top", + Anchor::TopRight => "topRight", + Anchor::Left => "left", + Anchor::Center => "center", + Anchor::Right => "right", + Anchor::BottomLeft => "bottomLeft", + Anchor::Bottom => "bottom", + Anchor::BottomRight => "bottomRight", + } +} + +/// Where an overlay goes: an anchor of the slide or the photo, and an offset from it. +#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)] +#[serde(default, rename_all = "camelCase")] +pub struct Placement { + pub anchor: Anchor, + /// Fractions of the slide's shorter side, x to the right, y down. + pub offset_x: f64, + pub offset_y: f64, + pub relative_to: RelativeTo, +} + +impl Default for Placement { + fn default() -> Self { + Placement { anchor: Anchor::BottomRight, offset_x: -0.03, offset_y: -0.03, relative_to: RelativeTo::Slide } + } +} + +impl Placement { + fn sanitized(mut self) -> Self { + self.offset_x = clamp_or(self.offset_x, -OFFSET_MAX, OFFSET_MAX, 0.0); + self.offset_y = clamp_or(self.offset_y, -OFFSET_MAX, OFFSET_MAX, 0.0); + self + } + + fn json(&self, o: &mut Map) { + o.insert("anchor".into(), json!(anchor_id(self.anchor))); + o.insert("offsetX".into(), json!(self.offset_x)); + o.insert("offsetY".into(), json!(self.offset_y)); + o.insert("relativeTo".into(), json!(self.relative_to.id())); + } + + /// Apply a placement key; `false` when `k` isn't one. + fn merge(&mut self, ctx: &str, k: &str, v: &Value) -> Result { + match k { + "anchor" => { + self.anchor = serde_json::from_value(v.clone()).map_err(|_| { + format!("`{ctx}.anchor` must be one of topLeft|top|topRight|left|center|right|bottomLeft|bottom|bottomRight, not {v}") + })? + } + "offsetX" => self.offset_x = number(ctx, k, v)?, + "offsetY" => self.offset_y = number(ctx, k, v)?, + "relativeTo" => { + self.relative_to = match v.as_str() { + Some("slide") => RelativeTo::Slide, + Some("photo") => RelativeTo::Photo, + _ => return Err(format!("`{ctx}.relativeTo` must be \"slide\" or \"photo\", not {v}")), + } + } + _ => return Ok(false), + } + Ok(true) + } + + /// The box of a `w × h` overlay on the slide `l`: at the anchor of the slide or the visible + /// photo, moved by the offset, then kept on the slide (when it fits). + pub fn place(&self, l: &Layout, w: f64, h: f64) -> Rect { + let c = match self.relative_to { + RelativeTo::Slide => l.slide, + RelativeTo::Photo => l.visible, + }; + let short = l.slide.width().min(l.slide.height()); + use Anchor::*; + let x = match self.anchor { + TopLeft | Left | BottomLeft => c.x0, + Top | Center | Bottom => (c.x0 + c.x1 - w) / 2.0, + TopRight | Right | BottomRight => c.x1 - w, + } + self.offset_x * short; + let y = match self.anchor { + TopLeft | Top | TopRight => c.y0, + Left | Center | Right => (c.y0 + c.y1 - h) / 2.0, + BottomLeft | Bottom | BottomRight => c.y1 - h, + } + self.offset_y * short; + let s = l.slide; + let fit = |v: f64, lo: f64, hi: f64, len: f64| if v.is_finite() { v.min(hi - len).max(lo) } else { lo }; + let (x, y) = (fit(x, s.x0, s.x1, w), fit(y, s.y0, s.y1, h)); + Rect { x0: x, y0: y, x1: x + w, y1: y + h } + } +} + +/// A soft drop shadow under text. +#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)] +#[serde(default, rename_all = "camelCase")] +pub struct TextShadow { + pub on: bool, + pub opacity: f64, + /// Fraction of the slide's shorter side. + pub offset: f64, + /// Blur radius, fraction of the slide's shorter side. + pub radius: f64, + /// Degrees counter-clockwise from the right (−45 = down right). + pub angle: f64, +} + +impl Default for TextShadow { + fn default() -> Self { + TextShadow { on: false, opacity: 0.6, offset: 0.004, radius: 0.004, angle: -45.0 } + } +} + +/// The identity plate: a line of text, or an image file (PNG / JPEG…, its transparency kept). +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +#[serde(default, rename_all = "camelCase")] +pub struct IdentityPlate { + pub enabled: bool, + pub text: String, + /// An image file drawn instead of the text (empty = the text). + pub image: String, + /// Height: the text's line height or the image's height, fraction of the shorter side. + pub size: f64, + pub color: [u8; 3], + pub opacity: f64, + pub place: Placement, + /// Draw it before the photo, so the photo covers it where they meet. + pub behind: bool, +} + +impl Default for IdentityPlate { + fn default() -> Self { + IdentityPlate { + enabled: false, + text: "LightCraft".into(), + image: String::new(), + size: 0.05, + color: [255, 255, 255], + opacity: 0.8, + place: Placement { anchor: Anchor::TopLeft, offset_x: 0.03, offset_y: 0.03, relative_to: RelativeTo::Slide }, + behind: false, + } + } +} + +/// The photo's star rating, drawn as stars. +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +#[serde(default, rename_all = "camelCase")] +pub struct RatingStars { + pub enabled: bool, + pub color: [u8; 3], + /// A star's height, fraction of the shorter side. + pub size: f64, + pub place: Placement, +} + +impl Default for RatingStars { + fn default() -> Self { + RatingStars { + enabled: false, + color: [235, 235, 235], + size: 0.03, + place: Placement { anchor: Anchor::TopRight, offset_x: -0.015, offset_y: 0.015, relative_to: RelativeTo::Photo }, + } + } +} + +/// A text overlay: a template with tokens (`{title}`, `{caption}`, `{exposure}`…; see [`TOKENS`]). +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +#[serde(default, rename_all = "camelCase")] +pub struct TextOverlay { + pub text: String, + /// Line height, fraction of the shorter side. + pub size: f64, + pub color: [u8; 3], + pub opacity: f64, + pub place: Placement, + pub shadow: TextShadow, +} + +impl Default for TextOverlay { + fn default() -> Self { + TextOverlay { + text: "{file}".into(), + size: 0.03, + color: [255, 255, 255], + opacity: 0.9, + place: Placement { anchor: Anchor::Bottom, offset_x: 0.0, offset_y: -0.02, relative_to: RelativeTo::Slide }, + shadow: TextShadow::default(), + } + } +} + +/// Every overlay of a slide. +#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)] +#[serde(default, rename_all = "camelCase")] +pub struct OverlaySettings { + pub identity_plate: IdentityPlate, + pub rating_stars: RatingStars, + pub texts: Vec, + /// Draw [`Self::watermark`] on the photo. + pub show_watermark: bool, + /// The export watermark's settings (sizes relative to the photo, as in export). + pub watermark: Watermark, +} + +/// Tokens text overlays add to the rename tokens ([`crate::rename::TOKENS`]): tag, meaning. +pub const TOKENS: &[(&str, &str)] = &[ + ("{file}", "File name with its extension"), + ("{caption}", "Caption metadata"), + ("{copyright}", "Copyright metadata"), + ("{exposure}", "Shutter speed and aperture (1/250 s at f/8)"), + ("{shutter}", "Shutter speed"), + ("{aperture}", "Aperture (f/8)"), + ("{focal}", "Focal length (50 mm)"), + ("{date}", "Capture date as YYYY-MM-DD (or {date:%d.%m.%Y} …)"), +]; + +fn clamp_or(v: f64, lo: f64, hi: f64, fallback: f64) -> f64 { + if v.is_finite() { v.clamp(lo, hi) } else { fallback } +} + +fn wrap_angle(a: f64, fallback: f64) -> f64 { + if !a.is_finite() { + return fallback; + } + let w = (a + 180.0).rem_euclid(360.0) - 180.0; + if w <= -180.0 { 180.0 } else { w } +} + +/// `s` cut at [`TEXT_MAX_CHARS`] characters. +fn cut(s: &mut String) { + if let Some((i, _)) = s.char_indices().nth(TEXT_MAX_CHARS) { + s.truncate(i); + } +} + +fn number(ctx: &str, k: &str, v: &Value) -> Result { + match v.as_f64() { + Some(x) if x.is_finite() => Ok(x), + _ => Err(format!("`{ctx}.{k}` must be a finite number, not {v}")), + } +} + +fn boolean(ctx: &str, k: &str, v: &Value) -> Result { + v.as_bool().ok_or_else(|| format!("`{ctx}.{k}` must be true or false, not {v}")) +} + +fn string(ctx: &str, k: &str, v: &Value) -> Result { + let mut s = v.as_str().ok_or_else(|| format!("`{ctx}.{k}` must be text, not {v}"))?.to_string(); + cut(&mut s); + Ok(s) +} + +fn colour(ctx: &str, k: &str, v: &Value) -> Result<[u8; 3], String> { + parse_color(v).ok_or_else(|| format!("`{ctx}.{k}` must be \"#rrggbb\" or [r, g, b] (0–255), not {v}")) +} + +fn object<'a>(ctx: &str, v: &'a Value) -> Result<&'a Map, String> { + v.as_object().ok_or_else(|| format!("`{ctx}` must be an object, not {v}")) +} + +fn unknown(ctx: &str, k: &str, keys: &str) -> String { + format!("unknown key `{ctx}.{k}` ({keys})") +} + +impl IdentityPlate { + fn sanitized(mut self) -> Self { + let d = IdentityPlate::default(); + cut(&mut self.text); + cut(&mut self.image); + self.size = clamp_or(self.size, SIZE_MIN, PLATE_SIZE_MAX, d.size); + self.opacity = clamp_or(self.opacity, 0.0, 1.0, d.opacity); + self.place = self.place.sanitized(); + self + } + + fn to_json(&self) -> Value { + let mut o = Map::new(); + o.insert("enabled".into(), json!(self.enabled)); + o.insert("text".into(), json!(self.text)); + o.insert("image".into(), json!(self.image)); + o.insert("size".into(), json!(self.size)); + o.insert("color".into(), json!(hex(self.color))); + o.insert("opacity".into(), json!(self.opacity)); + self.place.json(&mut o); + o.insert("behind".into(), json!(self.behind)); + Value::Object(o) + } + + fn merge(&mut self, v: &Value) -> Result<(), String> { + const C: &str = "identityPlate"; + for (k, v) in object(C, v)? { + if self.place.merge(C, k, v)? { + continue; + } + match k.as_str() { + "enabled" => self.enabled = boolean(C, k, v)?, + "text" => self.text = string(C, k, v)?, + "image" => self.image = string(C, k, v)?.trim().to_string(), + "size" => self.size = number(C, k, v)?, + "color" => self.color = colour(C, k, v)?, + "opacity" => self.opacity = number(C, k, v)?, + "behind" => self.behind = boolean(C, k, v)?, + _ => return Err(unknown(C, k, "enabled, text, image, size, color, opacity, anchor, offsetX, offsetY, relativeTo, behind")), + } + } + Ok(()) + } +} + +impl RatingStars { + fn sanitized(mut self) -> Self { + self.size = clamp_or(self.size, SIZE_MIN, STAR_SIZE_MAX, RatingStars::default().size); + self.place = self.place.sanitized(); + self + } + + fn to_json(&self) -> Value { + let mut o = Map::new(); + o.insert("enabled".into(), json!(self.enabled)); + o.insert("color".into(), json!(hex(self.color))); + o.insert("size".into(), json!(self.size)); + self.place.json(&mut o); + Value::Object(o) + } + + fn merge(&mut self, v: &Value) -> Result<(), String> { + const C: &str = "ratingStars"; + for (k, v) in object(C, v)? { + if self.place.merge(C, k, v)? { + continue; + } + match k.as_str() { + "enabled" => self.enabled = boolean(C, k, v)?, + "color" => self.color = colour(C, k, v)?, + "size" => self.size = number(C, k, v)?, + _ => return Err(unknown(C, k, "enabled, color, size, anchor, offsetX, offsetY, relativeTo")), + } + } + Ok(()) + } +} + +impl TextOverlay { + fn sanitized(mut self) -> Self { + let d = TextOverlay::default(); + cut(&mut self.text); + self.size = clamp_or(self.size, SIZE_MIN, TEXT_SIZE_MAX, d.size); + self.opacity = clamp_or(self.opacity, 0.0, 1.0, d.opacity); + self.place = self.place.sanitized(); + let ds = TextShadow::default(); + let sh = &mut self.shadow; + sh.opacity = clamp_or(sh.opacity, 0.0, 1.0, ds.opacity); + sh.offset = clamp_or(sh.offset, 0.0, SHADOW_OFFSET_MAX, ds.offset); + sh.radius = clamp_or(sh.radius, 0.0, SHADOW_RADIUS_MAX, ds.radius); + sh.angle = wrap_angle(sh.angle, ds.angle); + self + } + + fn to_json(&self) -> Value { + let mut o = Map::new(); + o.insert("text".into(), json!(self.text)); + o.insert("size".into(), json!(self.size)); + o.insert("color".into(), json!(hex(self.color))); + o.insert("opacity".into(), json!(self.opacity)); + self.place.json(&mut o); + o.insert("shadow".into(), json!(self.shadow.on)); + o.insert("shadowOpacity".into(), json!(self.shadow.opacity)); + o.insert("shadowOffset".into(), json!(self.shadow.offset)); + o.insert("shadowRadius".into(), json!(self.shadow.radius)); + o.insert("shadowAngle".into(), json!(self.shadow.angle)); + Value::Object(o) + } + + fn merge(&mut self, ctx: &str, v: &Value) -> Result<(), String> { + for (k, v) in object(ctx, v)? { + if self.place.merge(ctx, k, v)? { + continue; + } + match k.as_str() { + "text" => self.text = string(ctx, k, v)?, + "size" => self.size = number(ctx, k, v)?, + "color" => self.color = colour(ctx, k, v)?, + "opacity" => self.opacity = number(ctx, k, v)?, + "shadow" => self.shadow.on = boolean(ctx, k, v)?, + "shadowOpacity" => self.shadow.opacity = number(ctx, k, v)?, + "shadowOffset" => self.shadow.offset = number(ctx, k, v)?, + "shadowRadius" => self.shadow.radius = number(ctx, k, v)?, + "shadowAngle" => self.shadow.angle = number(ctx, k, v)?, + _ => { + return Err(unknown( + ctx, + k, + "text, size, color, opacity, anchor, offsetX, offsetY, relativeTo, shadow, shadowOpacity, shadowOffset, shadowRadius, shadowAngle", + )); + } + } + } + Ok(()) + } +} + +/// The keys [`OverlaySettings::merged`] takes. +const KEYS: &str = "identityPlate, ratingStars, texts, addText, removeText, showWatermark, watermark"; + +impl OverlaySettings { + /// Every value in range (non-finite numbers take the defaults), at most [`MAX_TEXTS`] texts. + pub fn sanitized(mut self) -> Self { + self.identity_plate = self.identity_plate.sanitized(); + self.rating_stars = self.rating_stars.sanitized(); + self.texts.truncate(MAX_TEXTS); + self.texts = self.texts.into_iter().map(TextOverlay::sanitized).collect(); + let d = Watermark::default(); + let w = &mut self.watermark; + let f = |v: f32, lo: f32, hi: f32, fb: f32| if v.is_finite() { v.clamp(lo, hi) } else { fb }; + w.size = f(w.size, 0.005, 0.5, d.size); + w.opacity = f(w.opacity, 0.0, 1.0, d.opacity); + w.inset_x = f(w.inset_x, 0.0, 0.4, d.inset_x); + w.inset_y = f(w.inset_y, 0.0, 0.4, d.inset_y); + if !matches!(w.rotation, 0 | 90 | 180 | 270) { + w.rotation = 0; + } + w.image_width = f(w.image_width, 0.01, 1.0, d.image_width); + cut(&mut w.text); + w.target = None; + self + } + + /// Does any overlay draw (on some photo)? + pub fn any(&self) -> bool { + let p = &self.identity_plate; + (p.enabled && p.opacity > 0.0 && (!p.text.trim().is_empty() || !p.image.trim().is_empty())) + || self.rating_stars.enabled + || self.texts.iter().any(|t| t.opacity > 0.0 && !t.text.trim().is_empty()) + || (self.show_watermark && (!self.watermark.text.trim().is_empty() || !self.watermark.image.trim().is_empty())) + } + + pub fn to_json(&self) -> Value { + let wm = serde_json::to_value(&self.watermark).unwrap_or(Value::Null); + json!({ + "identityPlate": self.identity_plate.to_json(), + "ratingStars": self.rating_stars.to_json(), + "texts": self.texts.iter().map(TextOverlay::to_json).collect::>(), + "showWatermark": self.show_watermark, + "watermark": wm, + }) + } + + /// These settings with the keys `p` names changed, validated and clamped; a wrong type or an + /// unknown key is an error and nothing changes. `texts` replaces the list (each entry merged + /// onto the one at its index, or a new default text); `addText` appends one (an object, `{}` + /// for the default); `removeText` removes the one at an index; `watermark` takes the export + /// watermark's keys (or text alone) merged onto the current one. + pub fn merged(&self, p: &Value) -> Result { + let Some(obj) = p.as_object() else { return Err("parameters must be an object".into()) }; + let mut s = self.clone(); + for (k, v) in obj { + match k.as_str() { + "identityPlate" => s.identity_plate.merge(v)?, + "ratingStars" => s.rating_stars.merge(v)?, + "texts" => { + let list = v.as_array().ok_or_else(|| format!("`texts` must be a list of text overlays, not {v}"))?; + if list.len() > MAX_TEXTS { + return Err(format!("at most {MAX_TEXTS} text overlays, not {}", list.len())); + } + let mut next = Vec::with_capacity(list.len()); + for (i, item) in list.iter().enumerate() { + let mut t = s.texts.get(i).cloned().unwrap_or_default(); + t.merge(&format!("texts[{i}]"), item)?; + next.push(t); + } + s.texts = next; + } + "addText" => { + if s.texts.len() >= MAX_TEXTS { + return Err(format!("at most {MAX_TEXTS} text overlays")); + } + let mut t = TextOverlay::default(); + t.merge("addText", v)?; + s.texts.push(t); + } + "removeText" => { + let i = v.as_u64().ok_or_else(|| format!("`removeText` must be the index of a text overlay, not {v}"))?; + let i = usize::try_from(i).ok().filter(|i| *i < s.texts.len()).ok_or_else(|| format!("no text overlay {i}"))?; + s.texts.remove(i); + } + "showWatermark" => s.show_watermark = boolean("", k, v).map_err(|_| format!("`showWatermark` must be true or false, not {v}"))?, + "watermark" => { + crate::export::ExportOptions::validate_watermark("slideshow.overlays", v).map_err(|e| e.to_string())?; + s.watermark = match v { + Value::String(t) => Watermark { text: t.clone(), ..s.watermark.clone() }, + _ => { + let mut cur = serde_json::to_value(&s.watermark).map_err(|e| e.to_string())?; + if let (Some(c), Some(o)) = (cur.as_object_mut(), v.as_object()) { + for (k, v) in o.iter().filter(|(_, v)| !v.is_null()) { + if k == "inset" { + // the legacy single margin sets whichever of the two the patch leaves out + for axis in ["insetX", "insetY"] { + if !o.contains_key(axis) { + c.insert(axis.into(), v.clone()); + } + } + } else { + c.insert(k.clone(), v.clone()); + } + } + } + serde_json::from_value(cur).map_err(|e| format!("`watermark`: {e}"))? + } + }; + } + other => return Err(format!("unknown setting `{other}` ({KEYS})")), + } + } + Ok(s.sanitized()) + } + + /// A hash of the settings (for render keys). Stable within a run. + pub fn pixel_key(&self) -> u64 { + use std::hash::{Hash, Hasher}; + let mut h = std::collections::hash_map::DefaultHasher::new(); + self.clone().sanitized().to_json().to_string().hash(&mut h); + h.finish() + } + + /// Problems worth telling the user about: an identity plate image that can't be read, tags in + /// text overlays that aren't tokens (they show as typed). + pub fn warnings(&self) -> Vec { + let mut out = Vec::new(); + let p = &self.identity_plate; + if p.enabled && !p.image.trim().is_empty() && crate::export::watermark_logo(p.image.trim()).is_none() { + out.push(format!("identity plate image {} can't be read; nothing is drawn", p.image.trim())); + } + for (i, t) in self.texts.iter().enumerate() { + for tag in unknown_tokens(&t.text) { + out.push(format!("text overlay {}: {tag} isn't a token and shows as typed", i + 1)); + } + } + out + } +} + +/// An exposure as "1/250 s at f/8" (either part alone when the other is unknown). +fn exposure(p: &Photo) -> String { + let parts: Vec = [shutter(p), aperture(p)].into_iter().filter(|s| !s.is_empty()).collect(); + parts.join(" at ") +} + +fn shutter(p: &Photo) -> String { + let s = p.meta.shutter.trim(); + if s.is_empty() || s.ends_with('s') || s.ends_with('"') { s.to_string() } else { format!("{s} s") } +} + +fn aperture(p: &Photo) -> String { + p.meta.aperture.filter(|a| a.is_finite() && *a > 0.0).map(|a| format!("f/{a:.1}").replace(".0", "")).unwrap_or_default() +} + +/// The overlay tokens beyond the rename ones; `{seq}` has no meaning on a slide and stays as typed. +fn overlay_token(p: &Photo, name: &str, arg: Option<&str>, tag: &str) -> Option { + Some(match name { + "file" => p.file_name.clone(), + "caption" => p.meta.caption.clone(), + "copyright" => p.meta.copyright.clone(), + "exposure" => exposure(p), + "shutter" => shutter(p), + "aperture" => aperture(p), + "focal" => p.meta.focal_mm.filter(|f| f.is_finite() && *f > 0.0).map(|f| format!("{f:.0} mm")).unwrap_or_default(), + "date" if arg.is_none() => crate::rename::expand_tokens("{date:%Y-%m-%d}", p, 1, 1), + "seq" | "n" => format!("{{{tag}}}"), + _ => return None, + }) +} + +/// `template`'s tokens replaced for `p`: the rename tokens ([`crate::rename::expand_tokens`]) and +/// [`TOKENS`]. Unknown tags stay as typed. +pub fn expand(template: &str, p: &Photo) -> String { + crate::rename::expand_tokens_with(template, p, 1, 1, &|name, arg, tag| overlay_token(p, name, arg, tag)) +} + +/// The `{…}` tags in `template` that aren't tokens. +pub fn unknown_tokens(template: &str) -> Vec { + let p = crate::rename::sample_photo(); + let mut out = Vec::new(); + let mut rest = template; + while let Some(i) = rest.find('{') { + let Some(after) = rest.get(i..) else { break }; + let Some(j) = after.find('}') else { break }; + let Some(tag) = after.get(..j + 1) else { break }; + if expand(tag, &p) == tag && !out.iter().any(|t| t == tag) { + out.push(tag.to_string()); + } + rest = after.get(j + 1..).unwrap_or(""); + } + out +} + +/// What an overlay box is, for [`SlideOverlays::boxes`]. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Kind { + IdentityPlate, + RatingStars, + /// The text overlay at this index. + Text(usize), + Watermark, +} + +impl Kind { + pub fn id(&self) -> String { + match self { + Kind::IdentityPlate => "identityPlate".into(), + Kind::RatingStars => "ratingStars".into(), + Kind::Text(i) => format!("text{i}"), + Kind::Watermark => "watermark".into(), + } + } +} + +/// The overlays of one photo's slide: the settings, its texts expanded, its rating. +#[derive(Clone, Debug, PartialEq)] +pub struct SlideOverlays { + pub settings: OverlaySettings, + /// One per text overlay, tokens replaced. + pub texts: Vec, + pub rating: u8, +} + +/// Gap between stars, as a share of a star's size. +const STAR_GAP: f64 = 0.2; + +impl SlideOverlays { + /// `settings` resolved for `photo` (`None`: no photo, so no tokens and no rating). + pub fn resolve(settings: &OverlaySettings, photo: Option<&Photo>) -> SlideOverlays { + let settings = settings.clone().sanitized(); + let texts = settings.texts.iter().map(|t| photo.map(|p| expand(&t.text, p)).unwrap_or_else(|| t.text.clone())).collect(); + SlideOverlays { settings, texts, rating: photo.map_or(0, |p| p.rating.min(5)) } + } + + /// What makes the drawn overlays differ (for render keys). + pub fn key(&self) -> u64 { + use std::hash::{Hash, Hasher}; + let mut h = std::collections::hash_map::DefaultHasher::new(); + self.settings.pixel_key().hash(&mut h); + self.texts.hash(&mut h); + self.rating.hash(&mut h); + h.finish() + } + + fn short(l: &Layout) -> f64 { + l.slide.width().min(l.slide.height()).max(1.0) + } + + /// The identity plate's text layout, or its image scaled to its size. + fn plate(&self, l: &Layout) -> Option { + let p = &self.settings.identity_plate; + if !p.enabled || p.opacity <= 0.0 { + return None; + } + let short = Self::short(l); + let px = (p.size * short).max(1.0); + if !p.image.trim().is_empty() { + let img = crate::export::watermark_logo(p.image.trim())?; + if img.width == 0 || img.height == 0 { + return None; + } + let h = px.round().clamp(1.0, l.slide.height().max(1.0)); + let w = (h * img.width as f64 / img.height as f64).round().clamp(1.0, l.slide.width().max(1.0)); + return Some(PlateContent::Image(img, w as usize, h as usize)); + } + let text = p.text.trim(); + crate::text::TextLayout::new(text, px as f32, false, crate::fonts::CRAFT_FONTS).map(PlateContent::Text) + } + + fn text_layout(&self, i: usize, l: &Layout) -> Option { + let t = self.settings.texts.get(i)?; + let text = self.texts.get(i)?.trim(); + if t.opacity <= 0.0 { + return None; + } + crate::text::TextLayout::new(text, (t.size * Self::short(l)).max(1.0) as f32, false, crate::fonts::CRAFT_FONTS) + } + + fn stars_size(&self, l: &Layout) -> Option<(usize, f64)> { + let r = &self.settings.rating_stars; + let n = usize::from(self.rating.min(5)); + (r.enabled && n > 0).then(|| (n, (r.size * Self::short(l)).max(2.0))) + } + + /// Where each overlay goes on the slide `l` (the ones that draw something). + pub fn boxes(&self, l: &Layout) -> Vec<(Kind, Rect)> { + let mut out = Vec::new(); + let s = &self.settings; + if let Some(c) = self.plate(l) { + let (w, h) = c.size(); + out.push((Kind::IdentityPlate, s.identity_plate.place.place(l, w, h))); + } + if let Some((n, size)) = self.stars_size(l) { + let w = n as f64 * size + (n as f64 - 1.0) * size * STAR_GAP; + out.push((Kind::RatingStars, s.rating_stars.place.place(l, w, size))); + } + for (i, t) in s.texts.iter().enumerate() { + if let Some(tl) = self.text_layout(i, l) { + out.push((Kind::Text(i), t.place.place(l, tl.width as f64, tl.height as f64))); + } + } + if s.show_watermark && (!s.watermark.text.trim().is_empty() || !s.watermark.image.trim().is_empty()) { + out.push((Kind::Watermark, l.visible)); + } + out + } + + /// Draw the overlays drawn behind the photo (an identity plate with "render behind image"). + pub fn draw_behind(&self, img: &mut Rgba8, l: &Layout) { + if self.settings.identity_plate.behind { + self.draw_plate(img, l); + } + } + + /// Draw the overlays over the composed slide: the watermark on the photo, then the identity + /// plate (unless it is behind), the rating stars and the text overlays. + pub fn draw_front(&self, img: &mut Rgba8, l: &Layout) { + let s = &self.settings; + if s.show_watermark { + let (x0, y0, x1, y1) = pixels(&l.visible, img.width, img.height); + if x1 > x0 && y1 > y0 { + crate::export::draw_watermark_in(img, &s.watermark, x0, y0, x1 - x0, y1 - y0); + } + } + if !s.identity_plate.behind { + self.draw_plate(img, l); + } + if let Some((n, size)) = self.stars_size(l) { + let w = n as f64 * size + (n as f64 - 1.0) * size * STAR_GAP; + let b = s.rating_stars.place.place(l, w, size); + for i in 0..n { + let x = b.x0 + i as f64 * size * (1.0 + STAR_GAP); + star(img, x, b.y0, size, s.rating_stars.color); + } + } + for (i, t) in s.texts.iter().enumerate() { + let Some(tl) = self.text_layout(i, l) else { continue }; + let b = t.place.place(l, tl.width as f64, tl.height as f64); + let short = Self::short(l); + if t.shadow.on && t.shadow.opacity > 0.0 { + let (d, a) = (t.shadow.offset * short, t.shadow.angle.to_radians()); + let (dx, dy) = (d * a.cos(), -d * a.sin()); + shadow(img, &tl, b.x0 + dx, b.y0 + dy, t.shadow.radius * short / 2.0, (t.shadow.opacity * t.opacity) as f32); + } + draw_text(img, &tl, b.x0, b.y0, t.color, t.opacity as f32); + } + } + + fn draw_plate(&self, img: &mut Rgba8, l: &Layout) { + let p = &self.settings.identity_plate; + let Some(c) = self.plate(l) else { return }; + let (w, h) = c.size(); + let b = p.place.place(l, w, h); + match c { + PlateContent::Text(tl) => draw_text(img, &tl, b.x0, b.y0, p.color, p.opacity as f32), + PlateContent::Image(src, w, h) => { + if let Some(scaled) = scaled_graphic(p.image.trim(), &src, w, h) { + draw_graphic(img, &scaled, b.x0.round() as i64, b.y0.round() as i64, p.opacity as f32); + } + } + } + } +} + +enum PlateContent { + Text(crate::text::TextLayout), + Image(Arc, usize, usize), +} + +impl PlateContent { + fn size(&self) -> (f64, f64) { + match self { + PlateContent::Text(t) => (t.width as f64, t.height as f64), + PlateContent::Image(_, w, h) => (*w as f64, *h as f64), + } + } +} + +/// Whole pixel bounds of `r` clipped to `w × h`. +fn pixels(r: &Rect, w: usize, h: usize) -> (usize, usize, usize, usize) { + let c = |v: f64, max: usize| if v.is_finite() { v.round().clamp(0.0, max as f64) as usize } else { 0 }; + let (x0, y0) = (c(r.x0, w), c(r.y0, h)); + (x0, y0, c(r.x1, w).max(x0), c(r.y1, h).max(y0)) +} + +/// Blend `c` into pixel (`x`, `y`) at `k` (0–1); outside the image does nothing. +fn blend(img: &mut Rgba8, x: i64, y: i64, k: f32, c: [u8; 3]) { + if x < 0 || y < 0 || k <= 0.0 { + return; + } + let (x, y) = (x as usize, y as usize); + if x >= img.width || y >= img.height { + return; + } + let w = img.width; + if let Some(p) = img.data.get_mut(y * w + x) { + let k = k.min(1.0); + for i in 0..3 { + p[i] = (p[i] as f32 + (c[i] as f32 - p[i] as f32) * k).round().clamp(0.0, 255.0) as u8; + } + } +} + +fn draw_text(img: &mut Rgba8, tl: &crate::text::TextLayout, x: f64, y: f64, c: [u8; 3], opacity: f32) { + let (w, h) = (img.width as f32, img.height as f32); + tl.draw_in(x as f32, y as f32, w, h, |gx, gy, cov| blend(img, i64::from(gx), i64::from(gy), cov * opacity, c)); +} + +/// A soft black shadow of the text at (`x`, `y`): its coverage blurred by `sigma` pixels. Only +/// the part that can reach the image is rasterised. +fn shadow(img: &mut Rgba8, tl: &crate::text::TextLayout, x: f64, y: f64, sigma: f64, opacity: f32) { + let pad = (sigma * 3.0).ceil().clamp(0.0, 512.0); + let (iw, ih) = (img.width as f64, img.height as f64); + let mx0 = (x - pad).floor().max(-pad); + let my0 = (y - pad).floor().max(-pad); + let mx1 = (x + tl.width as f64 + pad).ceil().min(iw + pad); + let my1 = (y + tl.height as f64 + pad).ceil().min(ih + pad); + if !(mx1 > mx0 && my1 > my0) || !mx0.is_finite() || !my0.is_finite() { + return; + } + let (w, h) = ((mx1 - mx0) as usize, (my1 - my0) as usize); + let mut mask = Plane::new(w, h); + tl.draw_in((x - mx0) as f32, (y - my0) as f32, w as f32, h as f32, |gx, gy, cov| { + if gx >= 0 + && gy >= 0 + && (gx as usize) < w + && (gy as usize) < h + && let Some(m) = mask.data.get_mut(gy as usize * w + gx as usize) + { + *m = (*m + cov).min(1.0); + } + }); + let mask = if sigma > 0.3 { lightcraft_raster::blur::gaussian(&mask, sigma as f32) } else { mask }; + let (ox, oy) = (mx0 as i64, my0 as i64); + for (i, k) in mask.data.iter().enumerate() { + if *k > 1e-3 { + blend(img, ox + (i % w) as i64, oy + (i / w) as i64, k * opacity, [0, 0, 0]); + } + } +} + +/// The identity plate image scaled to its size, premultiplied: (file, its decoded image, w, h) → +/// pixels, so the slides of a slideshow don't each scale a large logo again. +type ScaledCache = std::sync::Mutex>)>>; +static SCALED: ScaledCache = std::sync::Mutex::new(None); + +/// `src` (decoded from `path`) scaled to `w × h` (premultiplied, so transparent edges stay clean). +fn scaled_graphic(path: &str, src: &Arc, w: usize, h: usize) -> Option>> { + if src.width == 0 || src.height == 0 || w == 0 || h == 0 || src.data.len() != src.width * src.height { + return None; + } + let id = Arc::as_ptr(src) as usize; + let mut cache = SCALED.lock().unwrap_or_else(std::sync::PoisonError::into_inner); + if let Some((p, i, cw, ch, img)) = cache.as_ref() + && (p.as_str(), *i, *cw, *ch) == (path, id, w, h) + { + return Some(img.clone()); + } + let pre: Image<[f32; 4]> = Image::from_fn(src.width, src.height, |x, y| { + let p = src.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 = Arc::new(lightcraft_raster::resample::resize(&pre, w, h, lightcraft_raster::resample::Filter::Mitchell)); + *cache = Some((path.to_string(), id, w, h, scaled.clone())); + Some(scaled) +} + +/// The scaled graphic at (`x`, `y`), blended with its alpha × `opacity`. +fn draw_graphic(img: &mut Rgba8, scaled: &Image<[f32; 4]>, x: i64, y: i64, opacity: f32) { + for yy in 0..scaled.height { + for xx in 0..scaled.width { + let [r, g, b, a] = scaled.get(xx, yy); + let a = a.clamp(0.0, 1.0); + if a <= 1e-4 { + continue; + } + let c = [r, g, b].map(|v| (v / a).round().clamp(0.0, 255.0) as u8); + blend(img, x + xx as i64, y + yy as i64, a * opacity, c); + } + } +} + +/// The ten points of a five-pointed star in a `size` box at (`x`, `y`), point up. +fn star_points(x: f64, y: f64, size: f64) -> [(f64, f64); 10] { + let r = size / 2.0; + // the star's height is r (1 + cos 36°); centre it vertically in the box + let cy = y + size / 2.0 + r * (1.0 - (36f64).to_radians().cos()) / 2.0; + let cx = x + size / 2.0; + let inner = r * 0.382; + let mut pts = [(0.0, 0.0); 10]; + for (i, p) in pts.iter_mut().enumerate() { + let a = (90.0 + 36.0 * i as f64).to_radians(); + let rr = if i % 2 == 0 { r } else { inner }; + *p = (cx + rr * a.cos(), cy - rr * a.sin()); + } + pts +} + +/// A filled star: even-odd scanlines, four a pixel row, with exact horizontal coverage. +fn star(img: &mut Rgba8, x: f64, y: f64, size: f64, c: [u8; 3]) { + if !(x.is_finite() && y.is_finite() && size.is_finite() && size > 0.0) { + return; + } + let pts = star_points(x, y, size); + let (x0, y0) = (x.floor() as i64, y.floor() as i64); + let n = size.ceil() as usize + 2; + let mut row = vec![0f32; n]; + let mut xs: Vec = Vec::with_capacity(pts.len()); + for py in y0..y0 + n as i64 { + row.fill(0.0); + for s in 0..4 { + let sy = py as f64 + (s as f64 + 0.5) / 4.0; + xs.clear(); + for (i, &(xi, yi)) in pts.iter().enumerate() { + let Some(&(xj, yj)) = pts.get((i + pts.len() - 1) % pts.len()) else { continue }; + if (yi > sy) != (yj > sy) { + xs.push((xj - xi) * (sy - yi) / (yj - yi) + xi); + } + } + xs.sort_by(f64::total_cmp); + for &[a, b] in xs.as_chunks::<2>().0 { + let (a, b) = (a - x0 as f64, b - x0 as f64); + let first = a.floor().max(0.0) as usize; + let last = (b.ceil().max(0.0) as usize).min(n); + for (i, v) in row.iter_mut().enumerate().take(last).skip(first) { + let cover = (b.min(i as f64 + 1.0) - a.max(i as f64)).max(0.0); + *v += cover as f32 / 4.0; + } + } + } + for (i, k) in row.iter().enumerate() { + if *k > 0.0 { + blend(img, x0 + i as i64, py, k.min(1.0), c); + } + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn layout(w: usize, h: usize) -> Layout { + lightcraft_slideshow::layout(&lightcraft_slideshow::SlideSettings::default().merged(&json!({"margins": 0.1})).unwrap(), 1.5, w, h) + } + + fn photo() -> Photo { + let mut p = crate::rename::sample_photo(); + p.rating = 3; + p.meta.caption = "Morning fog".into(); + p.meta.shutter = "1/250".into(); + p.meta.aperture = Some(8.0); + p.meta.focal_mm = Some(50.0); + p + } + + #[test] + fn tokens_expand_like_the_rename_tokens_plus_slide_ones() { + let p = photo(); + assert_eq!(expand("{caption}", &p), "Morning fog"); + assert_eq!(expand("{exposure}", &p), "1/250 s at f/8"); + assert_eq!(expand("{focal} · ISO {iso}", &p), format!("50 mm · ISO {}", p.meta.iso.unwrap_or_default())); + assert_eq!(expand("{file}", &p), p.file_name); + assert_eq!(expand("{name}", &p), crate::rename::expand_tokens("{name}", &p, 1, 1), "rename tokens work"); + assert_eq!(expand("{date}", &p), crate::rename::expand_tokens("{date:%Y-%m-%d}", &p, 1, 1)); + assert_eq!(expand("{rating}", &p), "3"); + // unknown and slide-less tags show as typed, and are reported + assert_eq!(expand("{sparkle} {seq} {", &p), "{sparkle} {seq} {"); + assert_eq!(unknown_tokens("{title} {sparkle} {seq} {é}"), vec!["{sparkle}", "{seq}", "{é}"]); + // a photo without metadata: empty values, no panic + let mut bare = p.clone(); + bare.meta = Default::default(); + assert_eq!(expand("[{exposure}|{camera}|{caption}|{focal}]", &bare), "[|||]"); + } + + #[test] + fn nine_anchors_place_inside_the_slide_with_their_offsets() { + let l = layout(1000, 600); + let short = 600.0; + for anchor in ANCHORS { + for relative_to in [RelativeTo::Slide, RelativeTo::Photo] { + let at = |ox: f64, oy: f64| Placement { anchor, offset_x: ox, offset_y: oy, relative_to }.place(&l, 100.0, 40.0); + let b = at(0.0, 0.0); + assert!(b.x0 >= 0.0 && b.y0 >= 0.0 && b.x1 <= 1000.0 && b.y1 <= 600.0, "{anchor:?} {relative_to:?}: {b:?}"); + assert_eq!((b.width(), b.height()), (100.0, 40.0)); + let c = if relative_to == RelativeTo::Slide { l.slide } else { l.visible }; + let (cx, cy) = ((b.x0 + b.x1) / 2.0, (b.y0 + b.y1) / 2.0); + let id = anchor_id(anchor); + let want_x = if id.ends_with("eft") { + c.x0 + 50.0 + } else if id.ends_with("ight") { + c.x1 - 50.0 + } else { + (c.x0 + c.x1) / 2.0 + }; + let want_y = if id.starts_with("top") { + c.y0 + 20.0 + } else if id.starts_with("bottom") { + c.y1 - 20.0 + } else { + (c.y0 + c.y1) / 2.0 + }; + assert!((cx - want_x).abs() < 1e-9 && (cy - want_y).abs() < 1e-9, "{id} {relative_to:?}: {b:?}"); + // an inward offset moves it by that share of the shorter side + let (sx, sy) = (if id.ends_with("ight") { -0.02 } else { 0.02 }, if id.starts_with("bottom") { -0.03 } else { 0.03 }); + let m = at(sx, sy); + assert!((m.x0 - b.x0 - sx * short).abs() < 1e-9 && (m.y0 - b.y0 - sy * short).abs() < 1e-9, "{id}: {m:?}"); + } + } + // pushed off the slide: kept on it + let b = Placement { anchor: Anchor::BottomRight, offset_x: 0.5, offset_y: 0.5, relative_to: RelativeTo::Slide }.place(&l, 100.0, 40.0); + assert_eq!((b.x1, b.y1), (1000.0, 600.0)); + } + + fn blank(w: usize, h: usize) -> Rgba8 { + Image::filled(w, h, [0, 0, 0, 255]) + } + + fn lit(img: &Rgba8, r: &Rect, min: u8) -> usize { + let (x0, y0, x1, y1) = pixels(r, img.width, img.height); + (y0..y1).flat_map(|y| (x0..x1).map(move |x| (x, y))).filter(|&(x, y)| img.get(x, y)[1] >= min).count() + } + + #[test] + fn rating_stars_count_matches_the_rating() { + let l = layout(800, 500); + let s = OverlaySettings::default().merged(&json!({"ratingStars": {"enabled": true, "color": "#00ff00", "size": 0.05}})).unwrap(); + let mut p = photo(); + let mut widths = Vec::new(); + for rating in 0..=5u8 { + p.rating = rating; + let o = SlideOverlays::resolve(&s, Some(&p)); + let boxes = o.boxes(&l); + let mut img = blank(800, 500); + o.draw_front(&mut img, &l); + let Some((_, b)) = boxes.iter().find(|(k, _)| *k == Kind::RatingStars) else { + assert_eq!(rating, 0, "no stars only for an unrated photo"); + assert_eq!(lit(&img, &l.slide, 1), 0); + continue; + }; + widths.push(b.width()); + // count the stars: separate runs of lit columns along the box's middle row + let y = ((b.y0 + b.y1) / 2.0) as usize; + let mut runs = 0; + let mut was = false; + for x in 0..800 { + let on = img.get(x, y)[1] > 128; + runs += usize::from(on && !was); + was = on; + } + assert_eq!(runs, usize::from(rating), "{rating} stars"); + } + assert!(widths.windows(2).all(|w| w[1] > w[0])); + } + + #[test] + fn text_with_a_shadow_draws_the_shadow_at_its_angle() { + let l = layout(800, 500); + let s = OverlaySettings::default() + .merged( + &json!({"texts": [{"text": "Shadow", "size": 0.08, "color": "#ffffff", "opacity": 1, "anchor": "center", "offsetX": 0, "offsetY": 0, + "shadow": true, "shadowOpacity": 1, "shadowOffset": 0.02, "shadowRadius": 0, "shadowAngle": -90}]}), + ) + .unwrap(); + let o = SlideOverlays::resolve(&s, Some(&photo())); + let b = o.boxes(&l)[0].1; + let mut grey = Image::filled(800, 500, [128, 128, 128, 255]); + o.draw_front(&mut grey, &l); + // straight down by 10 px: dark pixels below the text box's glyphs, none above + let dark = |r: Rect| { + let (x0, y0, x1, y1) = pixels(&r, 800, 500); + (y0..y1).flat_map(|y| (x0..x1).map(move |x| (x, y))).filter(|&(x, y)| grey.get(x, y)[0] < 40).count() + }; + assert!(dark(Rect { y0: b.y1 - 2.0, y1: b.y1 + 10.0, ..b }) > 20, "the shadow falls below"); + assert_eq!(dark(Rect { y0: b.y0 - 12.0, y1: b.y0, ..b }), 0, "none above"); + // without the shadow: no dark pixels at all + let off = s.merged(&json!({"texts": [{"shadow": false}]})).unwrap(); + let mut grey2 = Image::filled(800, 500, [128, 128, 128, 255]); + SlideOverlays::resolve(&off, Some(&photo())).draw_front(&mut grey2, &l); + assert!(grey2.data.iter().all(|p| p[0] >= 128)); + } + + #[test] + fn identity_plate_image_draws_at_its_opacity_and_a_missing_one_is_reported() { + let dir = std::env::temp_dir().join(format!("lc-plate-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + let path = dir.join("plate.png"); + let red = Image::filled(40, 20, [255u8, 0, 0, 255]); + let png = lightcraft_codecs::encode_png(&lightcraft_codecs::EncodeImage::rgba8(&red), &lightcraft_codecs::EncodeMeta::default()).unwrap(); + std::fs::write(&path, png).unwrap(); + let l = layout(800, 500); + let s = OverlaySettings::default() + .merged(&json!({"identityPlate": {"enabled": true, "image": path.to_string_lossy(), "size": 0.1, "opacity": 0.5, "anchor": "topLeft", "offsetX": 0, "offsetY": 0}})) + .unwrap(); + assert!(s.warnings().is_empty()); + let o = SlideOverlays::resolve(&s, None); + let b = o.boxes(&l)[0].1; + assert_eq!((b.x0, b.y0, b.width(), b.height()), (0.0, 0.0, 100.0, 50.0), "50 px high (10 % of 500), its aspect kept"); + let mut img = blank(800, 500); + o.draw_front(&mut img, &l); + let p = img.get(50, 25); + assert!((p[0] as i32 - 128).abs() <= 2 && p[1] == 0, "half-opaque red over black: {p:?}"); + assert_eq!(img.get(150, 25)[0], 0, "only inside its box"); + // behind the image: the photo covers it + let behind = s.merged(&json!({"identityPlate": {"behind": true, "anchor": "center"}})).unwrap(); + let photo_img = Image::filled(30, 20, [0u8, 0, 255, 255]); + let ob = SlideOverlays::resolve(&behind, None); + let (img, l2) = + lightcraft_slideshow::compose_with(&photo_img, None, &lightcraft_slideshow::SlideSettings::default(), 800, 500, &mut |i, l| { + ob.draw_behind(i, l) + }); + let _ = l2; + assert_eq!(img.get(400, 250), [0, 0, 255, 255], "the photo is on top"); + // a missing file: reported, nothing drawn, no panic + let gone = s.merged(&json!({"identityPlate": {"image": dir.join("nope.png").to_string_lossy()}})).unwrap(); + assert_eq!(gone.warnings().len(), 1); + let mut img = blank(800, 500); + SlideOverlays::resolve(&gone, None).draw_front(&mut img, &l); + assert!(img.data.iter().all(|p| p[0] == 0)); + let _ = std::fs::remove_dir_all(&dir); + } + + #[test] + fn overlays_are_resolution_independent() { + let s = OverlaySettings::default() + .merged(&json!({ + "identityPlate": {"enabled": true, "text": "Studio", "size": 0.06}, + "ratingStars": {"enabled": true, "size": 0.04}, + "texts": [{"text": "{caption}", "size": 0.04, "anchor": "bottomLeft", "offsetX": 0.02, "offsetY": -0.02, "relativeTo": "photo"}], + })) + .unwrap(); + let o = SlideOverlays::resolve(&s, Some(&photo())); + let (a, b) = (o.boxes(&layout(800, 500)), o.boxes(&layout(1600, 1000))); + assert_eq!(a.len(), 3); + for ((ka, ra), (kb, rb)) in a.iter().zip(&b) { + assert_eq!(ka, kb); + for (va, vb) in [(ra.x0, rb.x0), (ra.y0, rb.y0), (ra.x1, rb.x1), (ra.y1, rb.y1)] { + assert!((va * 2.0 - vb).abs() <= 4.0, "{ka:?}: {ra:?} vs {rb:?}"); + } + } + } + + #[test] + fn watermark_takes_the_export_insets_and_rotation() { + let s = OverlaySettings::default().merged(&json!({"watermark": {"inset": 0.1, "insetY": 0.2, "rotation": 90}})).unwrap(); + assert_eq!((s.watermark.inset_x, s.watermark.inset_y, s.watermark.rotation), (0.1, 0.2, 90)); + let s = s.merged(&json!({"watermark": {"insetX": 0.05}})).unwrap(); + assert_eq!((s.watermark.inset_x, s.watermark.inset_y), (0.05, 0.2)); + assert!(OverlaySettings::default().merged(&json!({"watermark": {"rotation": 45}})).is_err()); + } + + #[test] + fn hostile_settings_are_refused_or_clamped_and_never_panic() { + let d = OverlaySettings::default(); + for bad in [ + json!({"identityPlate": {"size": "big"}}), + json!({"identityPlate": {"anchor": "middle"}}), + json!({"identityPlate": 3}), + json!({"ratingStars": {"color": "#zzzzzz"}}), + json!({"texts": {"text": "a"}}), + json!({"texts": [{"relativeTo": "page"}]}), + json!({"texts": [{"sparkle": 1}]}), + json!({"texts": vec![json!({}); MAX_TEXTS + 1]}), + json!({"removeText": 0}), + json!({"removeText": -1}), + json!({"watermark": {"size": 9}}), + json!({"watermark": 5}), + json!({"showWatermark": "yes"}), + json!({"glitter": true}), + json!([1]), + ] { + assert!(d.merged(&bad).is_err(), "{bad}"); + } + let s = d + .merged(&json!({"identityPlate": {"size": 1e300, "opacity": -3, "offsetX": -1e9}, "ratingStars": {"size": 0}, "addText": {"size": -1, "shadowAngle": 900, "shadowRadius": 7}})) + .unwrap(); + assert_eq!((s.identity_plate.size, s.identity_plate.opacity, s.identity_plate.place.offset_x), (PLATE_SIZE_MAX, 0.0, -OFFSET_MAX)); + assert_eq!(s.rating_stars.size, SIZE_MIN); + assert_eq!((s.texts[0].size, s.texts[0].shadow.angle, s.texts[0].shadow.radius), (SIZE_MIN, 180.0, SHADOW_RADIUS_MAX)); + assert!(d.merged(&json!({"removeText": 0})).is_err()); + assert!(s.merged(&json!({"removeText": 0})).unwrap().texts.is_empty()); + // round trips (command JSON and prefs.json) + assert_eq!(d.merged(&s.to_json()).unwrap(), s); + let back: OverlaySettings = serde_json::from_str(&serde_json::to_string(&s).unwrap()).unwrap(); + assert_eq!(back, s); + // NaN / ∞ from a hand-edited prefs file + let mut wild = s.clone(); + wild.identity_plate.size = f64::NAN; + wild.texts[0].place.offset_y = f64::INFINITY; + wild.watermark.size = f32::NAN; + let w = wild.sanitized(); + assert_eq!( + (w.identity_plate.size, w.texts[0].place.offset_y, w.watermark.size), + (IdentityPlate::default().size, 0.0, Watermark::default().size) + ); + // drawing hostile text at every size: empty, huge, CJK, RTL, control characters + let huge = "W".repeat(50_000); + for text in ["", " ", "\n\n", huge.as_str(), "日本語のテキスト", "שלום עולם", "مرحبا", "a\u{0}b\u{7}c\r\n", "{caption}{"] + { + let s = d + .merged(&json!({"identityPlate": {"enabled": true, "text": text, "size": PLATE_SIZE_MAX}, "ratingStars": {"enabled": true}, "addText": {"text": text, "size": TEXT_SIZE_MAX, "shadow": true, "shadowRadius": SHADOW_RADIUS_MAX}, "showWatermark": true, "watermark": text})) + .unwrap(); + assert!(s.identity_plate.text.chars().count() <= TEXT_MAX_CHARS); + let mut bare = photo(); + bare.meta = Default::default(); + for (w, h) in [(1, 1), (17, 9), (320, 200)] { + let l = lightcraft_slideshow::layout(&lightcraft_slideshow::SlideSettings::default(), f64::NAN, w, h); + let o = SlideOverlays::resolve(&s, Some(&bare)); + let mut img = blank(l.slide.width() as usize, l.slide.height() as usize); + o.draw_behind(&mut img, &l); + o.draw_front(&mut img, &l); + for (_, b) in o.boxes(&l) { + assert!(b.x0.is_finite() && b.y1.is_finite()); + } + } + } + } + + /// Huge text and stars on a big slide cost what shows: glyphs off the slide aren't rasterised. + #[test] + fn huge_overlays_on_a_big_slide_stay_cheap() { + let long = "Wide ".repeat(400); + let s = OverlaySettings::default() + .merged(&json!({ + "ratingStars": {"enabled": true, "size": STAR_SIZE_MAX}, + "addText": {"text": long, "size": TEXT_SIZE_MAX, "shadow": true, "shadowRadius": SHADOW_RADIUS_MAX, "anchor": "topLeft", "offsetX": 0, "offsetY": 0}, + })) + .unwrap(); + let mut p = photo(); + p.rating = 5; + let l = lightcraft_slideshow::layout(&lightcraft_slideshow::SlideSettings::default(), 1.5, 4000, 2500); + let mut img = blank(4000, 2500); + let t = std::time::Instant::now(); + SlideOverlays::resolve(&s, Some(&p)).draw_front(&mut img, &l); + assert!(t.elapsed().as_secs_f64() < 20.0, "{:?}", t.elapsed()); + assert!(img.get(100, 300)[0] > 0, "the text's start shows"); + } + + /// CJK text draws with the craft-fonts faces (skips without them). + #[test] + fn cjk_text_uses_the_craft_fonts() { + if crate::fonts::cjk(crate::fonts::CRAFT_FONTS).next().is_none() { + eprintln!("skipped: built without craft-fonts (CRAFT_FONTS_DIR)"); + return; + } + let cjk = crate::text::TextLayout::new("日本語", 40.0, false, crate::fonts::CRAFT_FONTS).unwrap(); + let tofu = crate::text::TextLayout::new("日本語", 40.0, false, &[]).unwrap(); + let count = |t: &crate::text::TextLayout| { + let mut n = 0usize; + t.draw(0.0, 0.0, |_, _, c| n += usize::from(c > 0.5)); + n + }; + assert_ne!(count(&cjk), count(&tofu), "real glyphs, not missing-glyph boxes"); + } +} diff --git a/crates/engine/src/slide_template.rs b/crates/engine/src/slide_template.rs new file mode 100644 index 000000000..32e26ed59 --- /dev/null +++ b/crates/engine/src/slide_template.rs @@ -0,0 +1,632 @@ +//! Slideshow templates (Lightroom Classic's Template Browser): a named snapshot of how a +//! slideshow looks and plays — the Playback settings, the slide's Layout / Options / Backdrop, +//! the Overlays and the Titles — as one JSON document with a format version. +//! +//! Built-in templates are our own designs ([`builtins`], JSON files beside this module). User +//! templates are files in the settings folder, `/slideshow-templates/.json` +//! ([`Session::slide_templates_dir`]); they are shared by every library on the machine, like +//! Lightroom's templates. Every section is read through the settings' own `merged()` (so a +//! hand-edited or hostile file can never set a value the panels couldn't), files are capped in +//! size and number, a file that can't be used is reported and skipped, and file names are made +//! from the template's name so they can never leave the templates folder. +//! +//! Applying a template replaces the four settings in one step and saves them with the library. +//! Library preferences have no undo, so the settings it replaced are kept for the session as +//! "Previous settings" ([`PREVIOUS`]): applying that puts them back (and applying it again goes +//! forward). "Prepare previews" is a machine preference, not part of a look: templates neither +//! save nor change it. + +use std::path::{Path, PathBuf}; +use std::sync::Arc; + +use serde_json::{Map, Value, json}; + +use crate::Session; +use crate::slide::SlideSettings; +use crate::slide_overlay::OverlaySettings; +use crate::slide_title::TitleSettings; +use crate::slideshow::PlaybackSettings; + +/// The `format` of a template document. +pub const FORMAT: &str = "lightcraft.slideshowTemplate"; +/// The newest document version this build reads (and the one it writes). +pub const VERSION: u64 = 1; +/// Largest template file read (bytes). +pub const MAX_FILE: u64 = 256 * 1024; +/// Most user templates listed (the rest of a folder that holds more are reported, not read). +pub const MAX_TEMPLATES: usize = 1000; +/// Directory entries looked at before giving up on a huge folder. +const MAX_SCAN: usize = 20_000; +/// Longest template name (characters). +pub const NAME_MAX: usize = 80; +/// Longest description (characters). +pub const DESCRIPTION_MAX: usize = 500; +/// Longest file stem made from a name (characters). +const STEM_MAX: usize = 64; +/// The folder under the settings folder user templates live in. +pub const DIR_NAME: &str = "slideshow-templates"; +/// Id prefix of built-in templates. +pub const BUILTIN: &str = "builtin:"; +/// Id prefix of user templates (followed by the file stem). +pub const USER: &str = "user:"; +/// Id of the settings before the last template was applied. +pub const PREVIOUS: &str = "previous"; + +/// Everything a template sets. +#[derive(Clone, Debug, Default, PartialEq)] +pub struct SlideLook { + pub playback: PlaybackSettings, + pub layout: SlideSettings, + pub overlays: OverlaySettings, + pub titles: TitleSettings, +} + +impl SlideLook { + /// The library's current slideshow settings. + pub fn of(s: &Session) -> SlideLook { + SlideLook { + playback: s.slideshow.clone(), + layout: s.slide_layout.clone(), + overlays: s.slide_overlays.clone(), + titles: s.slide_titles.clone(), + } + } + + /// The four sections as JSON (playback without `preparePreviews`, which isn't part of a look). + pub fn sections(&self) -> Map { + let mut playback = self.playback.to_json(); + if let Some(o) = playback.as_object_mut() { + o.remove("preparePreviews"); + } + let mut m = Map::new(); + m.insert("playback".into(), playback); + m.insert("layout".into(), self.layout.to_json()); + m.insert("overlays".into(), self.overlays.to_json()); + m.insert("titles".into(), self.titles.to_json()); + m + } + + /// Read the sections of a template document: each one merged onto the defaults by the + /// settings' own validation (a missing section is the defaults; a wrong type, an unknown key + /// or a non-finite number is an error). + fn from_sections(doc: &Map) -> Result { + fn section(doc: &Map, key: &str, merge: impl Fn(&Value) -> Result, default: T) -> Result { + match doc.get(key) { + None | Some(Value::Null) => Ok(default), + Some(v @ Value::Object(_)) => merge(v).map_err(|e| format!("{key}: {e}")), + Some(v) => Err(format!("`{key}` must be an object, not {v}")), + } + } + let mut playback = section(doc, "playback", |v| PlaybackSettings::default().merged(v), PlaybackSettings::default())?; + playback.prepare_previews = PlaybackSettings::default().prepare_previews; + Ok(SlideLook { + playback, + layout: section(doc, "layout", |v| SlideSettings::default().merged(v), SlideSettings::default())?, + overlays: section(doc, "overlays", |v| OverlaySettings::default().merged(v), OverlaySettings::default())?, + titles: section(doc, "titles", |v| TitleSettings::default().merged(v), TitleSettings::default())?, + }) + } + + /// What doesn't work with this look in `s`'s library: a background photo that isn't in it, + /// an identity plate or watermark image file that can't be read, unknown tokens, a title + /// screen with nothing to show. The look still applies; these draw nothing. + pub fn warnings(&self, s: &Session) -> Vec { + let mut out = Vec::new(); + if let Some(id) = self.layout.background_image + && s.catalog.photo(lightcraft_catalog::PhotoId(id)).is_none() + { + out.push(format!("the background image (photo {id}) isn't in this library; the backdrop colour shows instead")); + } + out.extend(self.overlays.warnings()); + let w = &self.overlays.watermark; + if self.overlays.show_watermark && !w.image.trim().is_empty() && crate::export::watermark_logo(w.image.trim()).is_none() { + out.push(format!("watermark image {} can't be read; nothing is drawn", w.image.trim())); + } + for t in self.titles.warnings(&self.overlays) { + if !out.contains(&t) { + out.push(t); + } + } + out + } +} + +/// A template: built in or a user's file. +#[derive(Clone, Debug, PartialEq)] +pub struct Template { + /// `builtin:` or `user:`. + pub id: String, + pub name: String, + pub description: String, + pub builtin: bool, + /// The file a user template is read from. + pub path: Option, + pub look: SlideLook, +} + +impl Template { + /// The template as a document (what a user template's file holds, and what export writes). + pub fn to_doc(&self) -> Value { + doc(&self.name, &self.description, &self.look) + } + + /// The template for listings (no settings). + pub fn info(&self) -> Value { + let mut v = json!({ + "id": self.id, + "name": self.name, + "builtin": self.builtin, + "group": if self.builtin { "builtin" } else { "user" }, + }); + if !self.description.is_empty() { + v["description"] = json!(self.description); + } + if let Some(p) = &self.path { + v["path"] = json!(p.to_string_lossy()); + } + v + } +} + +/// A template document for `look`. +pub fn doc(name: &str, description: &str, look: &SlideLook) -> Value { + let mut m = Map::new(); + m.insert("format".into(), json!(FORMAT)); + m.insert("version".into(), json!(VERSION)); + m.insert("name".into(), json!(name)); + if !description.is_empty() { + m.insert("description".into(), json!(description)); + } + m.extend(look.sections()); + Value::Object(m) +} + +/// A template document read and checked: its name (`None` when it has none), description and look. +#[derive(Clone, Debug, PartialEq)] +pub struct Parsed { + pub name: Option, + pub description: String, + pub look: SlideLook, +} + +/// Read a template document. Refused: more than [`MAX_FILE`] bytes, not JSON, another format, a +/// version from a newer LightCraft, a bad name, unknown keys and any setting its section's +/// validation refuses. +pub fn parse(bytes: &[u8]) -> Result { + if bytes.len() as u64 > MAX_FILE { + return Err(format!("{} bytes is too large for a slideshow template (at most {} KB)", bytes.len(), MAX_FILE / 1024)); + } + let text = std::str::from_utf8(bytes).map_err(|_| "not a slideshow template (not UTF-8 text)".to_string())?; + let text = text.trim_start_matches('\u{feff}'); + let v: Value = serde_json::from_str(text).map_err(|e| format!("not a slideshow template: {e}"))?; + let Value::Object(o) = v else { return Err("not a slideshow template (not a JSON object)".into()) }; + match o.get("format").and_then(Value::as_str) { + Some(FORMAT) => {} + Some(other) => return Err(format!("not a slideshow template (format `{other}`, expected `{FORMAT}`)")), + None => return Err(format!("not a slideshow template (no `format`: \"{FORMAT}\")")), + } + match o.get("version") { + Some(v) => match v.as_u64() { + Some(n) if n > VERSION => { + return Err(format!( + "made by a newer LightCraft (template version {n}; this one reads version {VERSION}): update LightCraft to use it" + )); + } + Some(n) if n >= 1 => {} + _ => return Err(format!("`version` must be a whole number from 1, not {v}")), + }, + None => return Err("no `version`".into()), + } + for k in o.keys() { + if !matches!(k.as_str(), "format" | "version" | "name" | "description" | "playback" | "layout" | "overlays" | "titles") { + return Err(format!("unknown key `{k}` (format, version, name, description, playback, layout, overlays, titles)")); + } + } + let name = match o.get("name") { + None | Some(Value::Null) => None, + Some(Value::String(n)) if n.trim().is_empty() => None, + Some(Value::String(n)) => Some(valid_name(n)?), + Some(v) => return Err(format!("`name` must be text, not {v}")), + }; + let description = match o.get("description") { + None | Some(Value::Null) => String::new(), + Some(Value::String(d)) => clean_text(d, DESCRIPTION_MAX), + Some(v) => return Err(format!("`description` must be text, not {v}")), + }; + Ok(Parsed { name, description, look: SlideLook::from_sections(&o)? }) +} + +/// `s` without control characters, trimmed, at most `max` characters. +fn clean_text(s: &str, max: usize) -> String { + s.chars().filter(|c| !c.is_control()).take(max).collect::().trim().to_string() +} + +/// A template name as stored: trimmed, 1–[`NAME_MAX`] characters, no control characters. +pub fn valid_name(name: &str) -> Result { + let n = name.trim(); + if n.is_empty() { + return Err("a template needs a name".into()); + } + if n.chars().any(char::is_control) { + return Err("a template name can't contain control characters (line breaks, tabs…)".into()); + } + if n.chars().count() > NAME_MAX { + return Err(format!("a template name has at most {NAME_MAX} characters")); + } + Ok(n.to_string()) +} + +/// The file stem a template called `name` is saved as: letters, digits, spaces, `-`, `_`, `(` +/// and `)` kept, anything else (path separators, dots, `:`, `*`…) `_`; trimmed; at most 64 +/// characters; Windows' reserved device names get a `_`. `None` when nothing usable is left. +pub fn file_stem(name: &str) -> Option { + let s: String = + name.trim().chars().map(|c| if c.is_alphanumeric() || matches!(c, ' ' | '-' | '_' | '(' | ')') { c } else { '_' }).take(STEM_MAX).collect(); + let s = s.trim().to_string(); + if s.is_empty() || s.chars().all(|c| c == '_') { + return None; + } + let upper = s.to_ascii_uppercase(); + let reserved = matches!(upper.as_str(), "CON" | "PRN" | "AUX" | "NUL") + || ((upper.starts_with("COM") || upper.starts_with("LPT")) && upper.len() == 4 && upper.ends_with(|c: char| c.is_ascii_digit())); + Some(if reserved { format!("{s}_") } else { s }) +} + +/// The stem of a user template id, when it is one this module could have made (so it can't +/// name a file outside the templates folder). +fn user_stem(id: &str) -> Option<&str> { + let stem = id.strip_prefix(USER)?; + (file_stem(stem).as_deref() == Some(stem)).then_some(stem) +} + +/// The built-in templates: our own designs (no Lightroom template, name or design copied). +const BUILTINS: &[(&str, &str)] = &[ + ("plain-black", include_str!("slideshow_templates/plain-black.json")), + ("gallery-wall", include_str!("slideshow_templates/gallery-wall.json")), + ("edge-to-edge", include_str!("slideshow_templates/edge-to-edge.json")), + ("cinema-wide", include_str!("slideshow_templates/cinema-wide.json")), + ("shooting-data", include_str!("slideshow_templates/shooting-data.json")), +]; + +/// The built-in templates (one that doesn't parse — a bug the tests catch — is left out). +pub fn builtins() -> Vec